Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 0228c62

Browse files
committed
Exclusive mode added
1 parent 4e92580 commit 0228c62

13 files changed

+123
-26
lines changed

XOutput/Input/DirectInput/Devices.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public IEnumerable<DirectDevice> GetInputDevices(bool allDevices)
6262
}
6363

6464
joystick.Properties.BufferSize = 128;
65-
6665
directDevices.Add(new DirectDevice(deviceInstance, joystick));
6766
}
6867
return directDevices;

XOutput/Input/DirectInput/DirectDevice.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using SharpDX.DirectInput;
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Linq;
56
using System.Text;
67
using System.Threading;
@@ -47,6 +48,27 @@ public DPadDirection DPad
4748
}
4849
}
4950
}
51+
52+
private bool isExclusive;
53+
public bool IsExclusive
54+
{
55+
get { return isExclusive; }
56+
set
57+
{
58+
if (value != isExclusive) {
59+
isExclusive = value;
60+
joystick.Unacquire();
61+
if (isExclusive) {
62+
joystick.SetCooperativeLevel(Process.GetCurrentProcess().MainWindowHandle, CooperativeLevel.Exclusive | CooperativeLevel.Background);
63+
}
64+
else
65+
{
66+
joystick.SetCooperativeLevel(Process.GetCurrentProcess().MainWindowHandle, CooperativeLevel.NonExclusive | CooperativeLevel.Background);
67+
}
68+
joystick.Acquire();
69+
}
70+
}
71+
}
5072

5173
private readonly DeviceInstance deviceInstance;
5274
private readonly Joystick joystick;
@@ -156,8 +178,11 @@ public bool RefreshInput()
156178
{
157179
if (!disposed)
158180
{
159-
joystick.Poll();
160-
InputChanged?.Invoke();
181+
try
182+
{
183+
joystick.Poll();
184+
InputChanged?.Invoke();
185+
} catch (Exception) { }
161186
return true;
162187
}
163188
return false;

XOutput/Input/GameController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ public class GameController : IDisposable
1919
public InputMapperBase Mapper => mapper;
2020
public string DisplayName => inputDevice.DisplayName;
2121
public int ControllerCount => controllerCount;
22+
public bool IsExclusive
23+
{
24+
get { return mapper.IsExclusive; }
25+
set
26+
{
27+
if(value != IsExclusive)
28+
{
29+
mapper.IsExclusive = value;
30+
inputDevice.IsExclusive = value;
31+
}
32+
}
33+
}
2234

2335
private static readonly Controllers controllers = new Controllers();
2436

XOutput/Input/IInputDevice.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public interface IInputDevice : IDevice, IDisposable
2121
/// If the controller has DPad
2222
/// </summary>
2323
bool HasDPad { get; }
24+
25+
bool IsExclusive { get; set; }
2426
}
2527
}

XOutput/Input/Keyboard/Keyboard.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public sealed class Keyboard : IInputDevice
1515
public bool Connected => true;
1616
public bool HasDPad => false;
1717
public DPadDirection DPad => DPadDirection.None;
18+
public bool IsExclusive { get => false; set { } }
1819

1920
public IEnumerable<Enum> Buttons => buttons;
2021
public IEnumerable<Enum> Axes => new Enum[0];

XOutput/Input/Mapper/DirectToXInputMapper.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ namespace XOutput.Input.Mapper
1313
/// </summary>
1414
public sealed class DirectToXInputMapper : InputMapperBase
1515
{
16+
private const string EXCLUSIVE = "Exclusive";
17+
private bool isExclusive;
18+
public override bool IsExclusive
19+
{
20+
get => isExclusive;
21+
set
22+
{
23+
if (isExclusive != value)
24+
{
25+
isExclusive = value;
26+
}
27+
}
28+
}
29+
1630
/// <summary>
1731
/// Gets a new mapper from dictionary.
1832
/// </summary>
@@ -21,32 +35,27 @@ public sealed class DirectToXInputMapper : InputMapperBase
2135
public static DirectToXInputMapper Parse(Dictionary<string, string> data)
2236
{
2337
DirectToXInputMapper mapper = new DirectToXInputMapper();
24-
foreach(var mapping in FromDictionary(data, typeof(DirectInputTypes)))
38+
if(data.ContainsKey(EXCLUSIVE))
39+
{
40+
mapper.IsExclusive = data[EXCLUSIVE] == "true";
41+
data.Remove(EXCLUSIVE);
42+
}
43+
else
44+
{
45+
mapper.IsExclusive = false;
46+
}
47+
foreach (var mapping in FromDictionary(data, typeof(DirectInputTypes)))
2548
{
2649
mapper.mappings.Add(mapping.Key, mapping.Value);
2750
}
2851
return mapper;
2952
}
3053

31-
/// <summary>
32-
/// Serializes the mapper object into string.
33-
/// </summary>
34-
/// <returns></returns>
35-
public override string ToString()
54+
public override Dictionary<string, string> ToDictionary()
3655
{
37-
StringBuilder sb = new StringBuilder();
38-
foreach (var mapping in mappings)
39-
{
40-
sb.Append(mapping.Key);
41-
sb.Append(";");
42-
sb.Append(mapping.Value.InputType);
43-
sb.Append(";");
44-
sb.Append((int)Math.Round(mapping.Value.MinValue * 100));
45-
sb.Append(";");
46-
sb.Append((int)Math.Round(mapping.Value.MaxValue * 100));
47-
sb.Append(Environment.NewLine);
48-
}
49-
return sb.ToString();
56+
var dict = base.ToDictionary();
57+
dict[EXCLUSIVE] = IsExclusive ? "true" : "false";
58+
return dict;
5059
}
5160
}
5261
}

XOutput/Input/Mapper/InputMapperBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ namespace XOutput.Input.Mapper
99
{
1010
public abstract class InputMapperBase
1111
{
12-
private static readonly char SPLIT_CHAR = ',';
12+
private const char SPLIT_CHAR = ',';
1313
protected readonly Dictionary<XInputTypes, MapperData> mappings = new Dictionary<XInputTypes, MapperData>();
14+
public abstract bool IsExclusive { get; set; }
1415

1516
/// <summary>
1617
/// Sets the mapping for a given XInput.
@@ -39,7 +40,7 @@ public MapperData GetMapping(XInputTypes? type)
3940
return mappings[type.Value];
4041
}
4142

42-
public Dictionary<string, string> ToDictionary()
43+
public virtual Dictionary<string, string> ToDictionary()
4344
{
4445
var dict = new Dictionary<string, string>();
4546
foreach(var mapping in mappings)

XOutput/Input/Mapper/KeyboardToXInputMapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace XOutput.Input.Mapper
1111
{
1212
public class KeyboardToXInputMapper : InputMapperBase
1313
{
14+
public override bool IsExclusive { get => false; set { } }
15+
1416
/// <summary>
1517
/// Gets a new mapper from dictionary.
1618
/// </summary>

XOutput/Resources/languages.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ DirectInputTypes.Button64=B64
107107
Keyboard=Keyboard
108108
Warning=Warning
109109
Error=Error
110+
ExclusiveMode=Exclusive Mode (disable DirectInput from this device to other applications)
110111
VersionCheckError=There was an error while checking for updates.
111112
VersionCheckNewRelease=This is a development release.
112113
VersionCheckUpToDate=The software is up to date.
@@ -234,6 +235,7 @@ DirectInputTypes.Button64=G64
234235
Keyboard=Billentyűzet
235236
Warning=Figyelmeztetés
236237
Error=Hiba
238+
ExclusiveMode=Kizárólagos mód (csak ez az alkalmazás olvashat DirectInput-ot az eszközről)
237239
VersionCheckError=Hiba történt a frissítés keresése közben.
238240
VersionCheckNewRelease=Ez egy fejlesztői verzió.
239241
VersionCheckUpToDate=Az alkalmazás legfrissebb verzióját használja.

XOutput/UI/View/ControllerSettings.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
xmlns:Converters="clr-namespace:XOutput.UI.Converters"
77
xmlns:local="clr-namespace:XOutput.UI.View"
88
mc:Ignorable="d"
9-
Title="{Binding Model.Title}" Width="1010" Height="800" MinWidth="1010"
10-
d:DataContext="{d:DesignInstance Type=local:AutoConfigureViewModel, IsDesignTimeCreatable=False}"
9+
Title="{Binding Model.Title}" Width="1010" Height="830" MinWidth="1010"
10+
d:DataContext="{d:DesignInstance Type=local:ControllerSettingsViewModel, IsDesignTimeCreatable=False}"
1111
Loaded="Window_Loaded"
1212
Closed="Window_Closed">
1313
<Window.Resources>
1414
<ItemsPanelTemplate x:Key="VerticalStackPanel">
1515
<StackPanel Orientation="Vertical" Focusable="False"/>
1616
</ItemsPanelTemplate>
1717
<Converters:LanguageConverter x:Key="LanguageConverter"/>
18+
<Converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
1819
<Converters:BoolInverterConverter x:Key="BoolInverterConverter"/>
1920
<Converters:DynamicLanguageConverter x:Key="DynamicLanguageConverter"/>
2021
<Converters:EnumerableNotEmptyToVisibilityConverter x:Key="EnumerableNotEmptyToVisibilityConverter"/>
@@ -64,6 +65,9 @@
6465
<ItemsControl ItemsSource="{Binding Model.MapperButtonViews}" ItemsPanel="{StaticResource VerticalStackPanel}" Focusable="False"/>
6566
</GroupBox>
6667
<Button Height="30" Margin="5" Click="Button_Click" Content="{Binding LanguageModel.Data, Converter={StaticResource LanguageConverter}, ConverterParameter='ConfigureAll'}"/>
68+
<CheckBox IsChecked="{Binding Model.IsExclusive}" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"
69+
Visibility="{Binding Model.CanSetExclusive, Converter={StaticResource BoolToVisibilityConverter}}"
70+
Content="{Binding LanguageModel.Data, Converter={StaticResource LanguageConverter}, ConverterParameter='ExclusiveMode'}"/>
6771
</StackPanel>
6872
</ScrollViewer>
6973
</GroupBox>

0 commit comments

Comments
 (0)