• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6602669847

22 Oct 2023 08:23AM UTC coverage: 44.094% (-8.4%) from 52.519%
6602669847

push

github

web-flow
Add XInput in preparation for gamepad triggers + add xmldoc (#50)

* Add XInput in preparation for gamepad triggers + add xmldoc

* add parent-child relation between mapped options

* add remove child mappings option + remove debug context menu

* x86 > AnyCPU (gives more sensible errors in WinForms Designer) - Fixed Designer failing on MappingForm

* Fix polling gamepad

* Add gamepad input trigger and config control + Fix mapping list not updating correctly

* remove broken and quite useless test

* update readme with warning regarding #46 + update readme special thanks

* add warning on same gamepad id trigger and action

* attempt to give github actions more time before timeout (tests sometimes fail)

* use current default mapping profile from app (so we dont have to copy it to tests everytime manually)

* Add gamepad button trigger

* Add multi-property edit mode

* Show physical gamepad connection warning

* give even more time for tests so they dont fail

* Block arming mappings if simulated gamepad index collides with physical gamepad

* Cleanup + add GamePad Trigger Trigger

* Fix combined trigger remove not working

* Separate stick action and allow custom scaling

* improve stick feeling

* fix parent picker

* update default mappings

* add trigger and try work out conflicts between physical and simulated devices (no luck yet)

* Fix simulated gamepad recognized as physical

* Show gamepad devices in UI

* fix mapping form + add more multi-edit type support

* make reverse mapping tool more useful

* easily setup/update reverse mappings while creating/updating a mapping

* update readme screenshots + prefer 'Arm' terminology for enabling profile

* support more nullable types in mapping profile

* simplify groups

* add configurable grouping

* prevent wonky sorting across groups

* Add easy switch group option

* Fix being able to create corrupt profile

* fail loading ... (continued)

762 of 2383 branches covered (0.0%)

Branch coverage included in aggregate %.

3060 of 3060 new or added lines in 106 files covered. (100.0%)

3897 of 8183 relevant lines covered (47.62%)

12635.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.16
/Core/Key2Joy.Core/Key2JoyManager.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics;
4
using System.IO;
5
using System.Linq;
6
using System.Reflection;
7
using System.Windows.Forms;
8
using CommonServiceLocator;
9
using Key2Joy.Config;
10
using Key2Joy.Contracts.Mapping;
11
using Key2Joy.Contracts.Mapping.Actions;
12
using Key2Joy.Contracts.Mapping.Triggers;
13
using Key2Joy.Interop;
14
using Key2Joy.Interop.Commands;
15
using Key2Joy.LowLevelInput.SimulatedGamePad;
16
using Key2Joy.LowLevelInput.XInput;
17
using Key2Joy.Mapping;
18
using Key2Joy.Mapping.Actions.Logic;
19
using Key2Joy.Mapping.Triggers.Keyboard;
20
using Key2Joy.Mapping.Triggers.Mouse;
21
using Key2Joy.Plugins;
22
using Key2Joy.Util;
23

24
namespace Key2Joy;
25

26
public delegate bool AppCommandRunner(AppCommand command);
27

28
public class Key2JoyManager : IKey2JoyManager, IMessageFilter
29
{
30
    /// <summary>
31
    /// Directory where plugins are located
32
    /// </summary>
33
    public const string PluginsDirectory = "Plugins";
34

35
    public event EventHandler<StatusChangedEventArgs> StatusChanged;
36

37
    public static Key2JoyManager instance;
38

39
    public static Key2JoyManager Instance
40
    {
41
        get
42
        {
43
            if (instance == null)
6!
44
            {
45
                throw new Exception("Key2JoyManager not initialized using InitSafely yet!");
×
46
            }
47

48
            return instance;
6✔
49
        }
50
    }
51

52
    /// <summary>
53
    /// Trigger listeners that should explicitly loaded. This ensures that they're available for scripts
54
    /// even if no mapping option is mapped to be triggered by it.
55
    /// </summary>
56
    public IList<AbstractTriggerListener> ExplicitTriggerListeners { get; set; }
8✔
57

58
    private const string READY_MESSAGE = "Key2Joy is ready";
59
    private static AppCommandRunner commandRunner;
60
    private MappingProfile armedProfile;
61
    private IHaveHandleAndInvoke handleAndInvoker;
62
    private readonly List<IWndProcHandler> wndProcListeners = new();
2✔
63

64
    private Key2JoyManager()
2✔
65
    { }
2✔
66

67
    /// <summary>
68
    /// Ensures Key2Joy is running and ready to accept commands as long as the main loop does not end.
69
    /// </summary>
70
    /// <param name="commandRunner"></param>
71
    /// <param name="mainLoop"></param>
72
    /// <param name="configManager">Optionally a custom config manager (probably only useful for unit testing)</param>
73
    public static void InitSafely(AppCommandRunner commandRunner, Action<PluginSet> mainLoop, IConfigManager configManager = null)
74
    {
75
        // Setup dependency injection and services
76
        var serviceLocator = new DependencyServiceLocator();
2✔
77
        ServiceLocator.SetLocatorProvider(() => serviceLocator);
11✔
78

79
        instance = new Key2JoyManager
2✔
80
        {
2✔
81
            ExplicitTriggerListeners = new List<AbstractTriggerListener>()
2✔
82
            {
2✔
83
                // Always add these listeners so scripts can ask them if stuff has happened.
2✔
84
                KeyboardTriggerListener.Instance,
2✔
85
                MouseButtonTriggerListener.Instance,
2✔
86
                MouseMoveTriggerListener.Instance
2✔
87
            }
2✔
88
        };
2✔
89
        serviceLocator.Register<IKey2JoyManager>(instance);
2✔
90

91
#pragma warning disable IDE0001 // Simplify Names
92
        serviceLocator.Register<IConfigManager>(configManager ??= new ConfigManager());
2!
93
#pragma warning restore IDE0001 // Simplify Names
94

95
        var gamePadService = new SimulatedGamePadService();
2✔
96
        serviceLocator.Register<ISimulatedGamePadService>(gamePadService);
2✔
97

98
        var xInputService = new XInputService();
2✔
99
        serviceLocator.Register<IXInputService>(xInputService);
2✔
100

101
        var commandRepository = new CommandRepository();
2✔
102
        serviceLocator.Register<ICommandRepository>(commandRepository);
2✔
103

104
        // Load plugins
105
        var pluginDirectoriesPaths = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
2✔
106
        pluginDirectoriesPaths = Path.Combine(pluginDirectoriesPaths, PluginsDirectory);
2✔
107

108
        PluginSet plugins = new(pluginDirectoriesPaths);
2✔
109
        plugins.LoadAll();
2✔
110
        plugins.RefreshPluginTypes();
2✔
111

112
        foreach (var loadState in plugins.AllPluginLoadStates.Values)
8✔
113
        {
114
            if (loadState.LoadState == PluginLoadStates.FailedToLoad)
2!
115
            {
116
                System.Windows.MessageBox.Show(
×
117
                    $"One of your plugins located at {loadState.AssemblyPath} failed to load. This was the error: " +
×
118
                    loadState.LoadErrorMessage,
×
119
                    "Failed to load plugin!",
×
120
                    System.Windows.MessageBoxButton.OK,
×
121
                    System.Windows.MessageBoxImage.Warning
×
122
                );
×
123
            }
124
        }
125

126
        Key2JoyManager.commandRunner = commandRunner;
2✔
127

128
        var interopServer = new InteropServer(instance, commandRepository);
2✔
129

130
        try
131
        {
132
            interopServer.RestartListening();
2✔
133
            mainLoop(plugins);
2✔
134
        }
2✔
135
        finally
136
        {
137
            interopServer.StopListening();
2✔
138
            gamePadService.ShutDown();
2✔
139
        }
2✔
140
    }
2✔
141

142
    // Run the event on the same thread as the main control/form
143
    public void CallOnUiThread(Action action) => this.handleAndInvoker.Invoke(action);
×
144

145
    internal static bool RunAppCommand(AppCommand command) => commandRunner != null && commandRunner(command);
×
146

147
    public bool PreFilterMessage(ref System.Windows.Forms.Message m)
148
    {
149
        for (var i = 0; i < this.wndProcListeners.Count; i++)
×
150
        {
151
            // Check if the proc listeners haven't changed (this can happen when a plugin opens a MessageBox, the user aborts, and we then close the messagebox)
152
            if (i >= this.wndProcListeners.Count)
×
153
            {
154
                Debug.WriteLine("Key2JoyManager.PreFilterMessage: wndProcListeners changed while processing message!");
155
                break;
156
            }
157

158
            var wndProcListener = this.wndProcListeners[i];
×
159

160
            wndProcListener.WndProc(new Contracts.Mapping.Message(m.HWnd, m.Msg, m.WParam, m.LParam));
×
161
        }
162

163
        return false;
×
164
    }
165

166
    public void SetHandlerWithInvoke(IHaveHandleAndInvoke handleAndInvoker)
167
    {
168
        this.handleAndInvoker = handleAndInvoker;
×
169
        Application.AddMessageFilter(this);
×
170

171
        Console.WriteLine(READY_MESSAGE);
×
172
    }
×
173

174
    public bool GetIsArmed(MappingProfile profile = null)
175
    {
176
        if (profile == null)
×
177
        {
178
            return this.armedProfile != null;
×
179
        }
180

181
        return this.armedProfile == profile;
×
182
    }
183

184
    /// <inheritdoc/>
185
    public void ArmMappings(MappingProfile profile)
186
    {
187
        this.armedProfile = profile;
2✔
188

189
        var allListeners = new List<AbstractTriggerListener>();
2✔
190
        allListeners.AddRange(this.ExplicitTriggerListeners);
2✔
191

192
        var allActions = (IList<AbstractAction>)profile.MappedOptions.Select(m => m.Action).ToList();
4✔
193

194
        var xInputService = ServiceLocator.Current.GetInstance<IXInputService>();
2✔
195
        // We must recognize physical devices before any simulated ones are added.
196
        // Otherwise we wont be able to tell the difference.
197
        xInputService.RecognizePhysicalDevices();
2✔
198
        xInputService.StartPolling();
2✔
199

200
        try
201
        {
202
            foreach (var mappedOption in profile.MappedOptions)
8✔
203
            {
204
                if (mappedOption.Trigger == null)
2✔
205
                {
206
                    continue;
207
                }
208

209
                var listener = mappedOption.Trigger.GetTriggerListener();
2✔
210

211
                if (!allListeners.Contains(listener))
2✔
212
                {
213
                    allListeners.Add(listener);
2✔
214
                }
215

216
                if (listener is IWndProcHandler listenerWndProcHAndler)
2!
217
                {
218
                    this.wndProcListeners.Add(listenerWndProcHAndler);
×
219
                }
220

221
                mappedOption.Action.OnStartListening(listener, ref allActions);
2✔
222
                listener.AddMappedOption(mappedOption);
2✔
223
            }
224

225
            var allListenersForSharing = (IList<AbstractTriggerListener>)allListeners;
2✔
226

227
            foreach (var listener in allListeners)
8✔
228
            {
229
                if (listener is IWndProcHandler listenerWndProcHAndler)
2!
230
                {
231
                    listenerWndProcHAndler.Handle = this.handleAndInvoker.Handle;
×
232
                }
233

234
                listener.StartListening(ref allListenersForSharing);
2✔
235
            }
236

237
            StatusChanged?.Invoke(this, new StatusChangedEventArgs
2!
238
            {
2✔
239
                IsEnabled = true,
2✔
240
                Profile = this.armedProfile
2✔
241
            });
2✔
242
        }
2✔
243
        catch (MappingArmingFailedException ex)
244
        {
245
            //cleanup
246
            this.DisarmMappings();
×
247
            throw ex;
×
248
        }
249
    }
2✔
250

251
    public void DisarmMappings()
252
    {
253
        var listeners = this.ExplicitTriggerListeners;
2✔
254
        this.wndProcListeners.Clear();
2✔
255

256
        // Clear all intervals
257
        IdPool.CancelAll();
2✔
258

259
        foreach (var mappedOption in this.armedProfile.MappedOptions)
8✔
260
        {
261
            if (mappedOption.Trigger == null)
2✔
262
            {
263
                continue;
264
            }
265

266
            var listener = mappedOption.Trigger.GetTriggerListener();
2✔
267
            mappedOption.Action.OnStopListening(listener);
2✔
268

269
            if (!listeners.Contains(listener))
2✔
270
            {
271
                listeners.Add(listener);
2✔
272
            }
273
        }
274

275
        foreach (var listener in listeners)
8✔
276
        {
277
            listener.StopListening();
2✔
278
        }
279

280
        var xInputService = ServiceLocator.Current.GetInstance<IXInputService>();
2✔
281
        xInputService.StopPolling();
2✔
282

283
        var gamePadService = ServiceLocator.Current.GetInstance<ISimulatedGamePadService>();
2✔
284
        gamePadService.EnsureAllUnplugged();
2✔
285

286
        this.armedProfile = null;
2✔
287

288
        StatusChanged?.Invoke(this, new StatusChangedEventArgs
2!
289
        {
2✔
290
            IsEnabled = false,
2✔
291
        });
2✔
292
    }
×
293

294
    /// <summary>
295
    /// Starts Key2Joy, pausing until it's ready
296
    /// </summary>
297
    public static void StartKey2Joy(bool startMinimized = true, bool pauseUntilReady = true)
298
    {
299
        var configManager = ServiceLocator.Current.GetInstance<IConfigManager>();
×
300
        var executablePath = configManager.GetConfigState().LastInstallPath;
×
301

302
        if (executablePath == null)
×
303
        {
304
            Console.WriteLine("Error! Key2Joy executable path is not known, please start Key2Joy at least once!");
×
305
            return;
×
306
        }
307

308
        if (!File.Exists(executablePath))
×
309
        {
310
            Console.WriteLine("Error! Key2Joy executable path is invalid, please start Key2Joy at least once (and don't move the executable)!");
×
311
            return;
×
312
        }
313

314
        Process process = new()
×
315
        {
×
316
            StartInfo = new ProcessStartInfo
×
317
            {
×
318
                FileName = executablePath,
×
319
                Arguments = startMinimized ? "--minimized" : "",
×
320
                UseShellExecute = false,
×
321
                RedirectStandardOutput = true
×
322
            }
×
323
        };
×
324

325
        process.Start();
×
326

327
        if (!pauseUntilReady)
×
328
        {
329
            return;
×
330
        }
331

332
        while (!process.StandardOutput.EndOfStream)
×
333
        {
334
            if (process.StandardOutput.ReadLine() == READY_MESSAGE)
×
335
            {
336
                break;
337
            }
338
        }
339
    }
×
340
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc