-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathjoystick_gremlin.py
More file actions
443 lines (375 loc) · 14.5 KB
/
joystick_gremlin.py
File metadata and controls
443 lines (375 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# -*- coding: utf-8; -*-
# SPDX-License-Identifier: GPL-3.0-only
from __future__ import annotations
import argparse
import ctypes
import logging
import logging.handlers
import os
import sys
import time
import traceback
from pathlib import Path
from typing import Any, Dict, List
# Import QtMultimedia so pyinstaller doesn't miss it.
from PySide6 import QtCore, QtGui, QtQml, QtQuick, QtWidgets
import resources
import dill
import vjoy.vjoy
from gremlin.config import Configuration
from gremlin.types import PropertyType
# Figure out the location of the code / executable and change the working
# directory accordingly.
install_path = os.path.normcase(os.path.dirname(os.path.abspath(sys.argv[0])))
os.chdir(install_path)
# Setting some global QT configurations.
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Universal"
# os.environ["QML_IMPORT_TRACE"] = "1"
# os.environ["QSG_RHI"] = "1"
# Path mangling to ensure Gremlin can run indepent of the CWD and
# ensure configuration folder is created in time.
import gremlin.util
sys.path.insert(0, gremlin.util.userprofile_path())
gremlin.util.setup_userprofile()
import gremlin.audio_player
import gremlin.config
import gremlin.error
import gremlin.device_initialization
import gremlin.event_handler
import gremlin.mode_manager
import gremlin.plugin_manager
import gremlin.types
import gremlin.signal
import gremlin.ui.action_image_generator
import gremlin.ui.backend
import gremlin.ui.option
import gremlin.ui.tools
import gremlin.ui.util
def configure_logger(config: Dict[str, Any]) -> None:
"""Creates a new logger instance.
Args:
config: configuration information for the new logger
"""
logger = logging.getLogger(config["name"])
logger.setLevel(config["level"])
handler = logging.handlers.RotatingFileHandler(
config["logfile"],
maxBytes=1 * 1024 * 1024,
backupCount=1
)
handler.setLevel(config["level"])
formatter = logging.Formatter(config["format"], "%Y-%m-%d %H:%M:%S")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.debug("-" * 80)
logger.debug(time.strftime("%Y-%m-%d %H:%M"))
logger.debug(f"Starting Joystick Gremlin {gremlin.util.get_code_release()}")
logger.debug("-" * 80)
def exception_hook(exception_type, value, trace) -> None:
"""Logs any uncaught exceptions.
Args:
exception_type: type of exception being caught
value: content of the exception
trace: the stack trace which produced the exception
"""
msg = " ".join(traceback.format_exception(exception_type, value, trace))
logging.getLogger("system").error(f"Unhandled exception: {msg}")
gremlin.signal.display_error("An unhandled exception occured.", msg)
def shutdown_cleanup() -> None:
"""Handles cleanup before terminating Gremlin."""
# Terminate potentially running EventListener loop.
gremlin.event_handler.EventListener().terminate()
# Terminate profile runner.
backend = gremlin.ui.backend.Backend()
backend.runner.stop()
backend.process_monitor.stop()
# Relinquish control over all VJoy devices used.
vjoy.vjoy.VJoyProxy.reset()
gremlin.audio_player.AudioPlayer().stop()
def register_config_options() -> None:
cfg = gremlin.config.Configuration()
cfg.register(
"global", "internal", "last-mode",
PropertyType.String, "Default",
"Name of the last active mode", {}
)
cfg.register(
"global", "internal", "last-profile",
PropertyType.String, "",
"Most recently used profile", {}
)
cfg.register(
"global", "internal", "recent-profiles",
PropertyType.List, [],
"List of recently opened profiles", {}
)
cfg.register(
"global", "internal", "last-known-version",
PropertyType.String, gremlin.util.get_code_version(),
"Last known version of Gremlin.", {}
)
cfg.register(
"global", "general", "check-for-updates",
PropertyType.Bool, True,
"Check for new Gremlin versions online upon start.", {}, True
)
cfg.register(
"global", "general", "plugin-directory",
PropertyType.Path, "",
"Directory containing additional action plugins", {"is_folder": True}, True
)
cfg.register(
"action", "general", "action-priorities",
PropertyType.List, [],
"Priority order of the actions", {}, True
)
cfg.register(
"global", "general", "device-change-behavior",
PropertyType.Selection, "Reload",
"Action Gremlin takes when a joystick is connected or disconnected.",
{"valid_options": ["Disable", "Ignore", "Reload"]}, True
)
cfg.register(
"global", "general", "dark-mode",
PropertyType.Bool, False,
"Use the dark mode UI (requires restart).", {}, True
)
cfg.register(
"global", "general", "refresh-axis-on-activation",
PropertyType.Bool, True,
"Use known physical device state to perform actions using these values "
"upon profile activation.", {}, True
)
cfg.register(
"global", "general", "refresh-axis-on-mode-change",
PropertyType.Bool, True,
"Force an update of all axes by emitting axis events upon a mode change.",
{}, True
)
cfg.register(
"global", "general", "input-highlighting",
PropertyType.Bool, True,
"Select the input in the UI by using an input on the physical device. "
"Selects only inputs if the active tab matches the device.",
{}, True
)
cfg.register(
"profile", "automation", "enable-auto-loading",
PropertyType.Bool, False,
"Enable the automatic loading and activation of profiles based on the "
"specified executable and profile combinations.",
{}, True
)
cfg.register(
"profile", "automation", "remain-active-on-focus-loss",
PropertyType.Bool, False,
"Keep the profile active when the monitored executable loses focus and "
"the newly focused executable does not have a profile assigned to it.",
{}, True
)
cfg.register(
"profile", "automation", "entries-auto-loading",
PropertyType.List, [],
"List of executable and profile combinations for automatic loading.",
{}, False
)
def configure_loggers() -> None:
"""Configures logging for system and user events."""
configure_logger({
"name": "system",
"level": logging.DEBUG,
"logfile": os.path.join(gremlin.util.userprofile_path(), "system.log"),
"format": "%(asctime)s %(levelname)10s %(message)s"
})
configure_logger({
"name": "user",
"level": logging.DEBUG,
"logfile": os.path.join(gremlin.util.userprofile_path(), "user.log"),
"format": "%(asctime)s %(message)s"
})
def update_action_priorities() -> None:
cfg = gremlin.config.Configuration()
key = ["action", "general", "action-priorities"]
priorities = []
if cfg.exists(*key):
priorities = cfg.value(*key)
priority_names = [v[0] for v in priorities]
# Obtain the list of currently available plugins with an alphabetical
# order for all but the most important actions.
priority_actions = ["Map to vJoy", "Macro", "Response Curve"]
plugin_names = [
p.name for p in
gremlin.plugin_manager.PluginManager().repository.values()
]
plugin_names = [n for n in priority_actions if n in plugin_names] + \
sorted([n for n in plugin_names if n not in priority_actions])
for tag in plugin_names:
if tag not in priority_names:
priorities.append((tag, True))
to_delete = []
for i, tag in enumerate(priority_names):
if tag not in plugin_names:
to_delete.append(i)
for idx in reversed(to_delete):
del priorities[idx]
cfg.set(*key, priorities)
class JoystickGremlinApp(QtWidgets.QApplication):
def __init__(self, argv: List[str]) -> None:
# Parse command line arguments.
parser = argparse.ArgumentParser()
parser.add_argument(
"--profile",
help="Path to the profile to load on startup",
)
parser.add_argument(
"--enable",
help="Enable Joystick Gremlin upon launch",
action="store_true"
)
parser.add_argument(
"--start-minimized",
help="Start Joystick Gremlin minimized",
action="store_true"
)
cmd_args, qt_argv = parser.parse_known_args(argv)
# Run the parent constructor with remaining arguments.
super().__init__(qt_argv)
# Initialize various components.
configure_loggers()
self.syslog = logging.getLogger("system")
register_config_options()
# Ensure unhandled exceptions are shown to the user when running a
# compiled version of Joystick Gremlin.
executable_name = os.path.split(sys.executable)[-1]
if executable_name == "joystick_gremlin.exe":
sys.excepthook = exception_hook
sys.excepthook = exception_hook
# Initialize joystick device handling.
self.syslog.info("Initializing joystick devices")
dill.DILL.init()
device_initialization_error = None
try:
gremlin.device_initialization.joystick_devices_initialization()
except gremlin.error.GremlinError as e:
device_initialization_error = str(e)[1:-1]
self.initialize_qt()
# If an error was detected during device initialization, the error
# will be displayed before Gremlin quits.
if device_initialization_error is not None:
self.engine.load(QtCore.QUrl.fromLocalFile(
gremlin.util.resource_path("qml/MainFailure.qml"))
)
self.engine.rootContext().setContextProperty(
"errorString",
device_initialization_error
)
self.aboutToQuit.connect(shutdown_cleanup)
return
# Load plugin code and UI elements
self.syslog.info("Initializing plugins")
gremlin.plugin_manager.PluginManager()
# Purge configuration options that have not been registered and update
# the action priority information.
self.cfg.purge_unused()
update_action_priorities()
# Initialize main UI.
self.engine.load(QtCore.QUrl.fromLocalFile(
gremlin.util.resource_path("qml/Main.qml"))
)
if not self.engine.rootObjects():
sys.exit(-1)
self.process_cmd_args(cmd_args)
self.backend.check_for_updates()
# Run UI.
self.syslog.info("Gremlin UI launching")
self.aboutToQuit.connect(shutdown_cleanup)
def process_cmd_args(self, args: argparse.Namespace) -> None:
# Load the profile specified by the user on the command line, otherwise
# attempt to load the previously loaded profile
if args.profile is not None and os.path.isfile(args.profile):
self.backend.loadProfile(args.profile)
else:
last_profile = Path(Configuration().value(
"global", "internal", "last-profile")
)
if last_profile.is_file():
self.backend.loadProfile(str(last_profile))
if args.enable:
self.backend.activate_gremlin(True)
if args.start_minimized:
self.backend.minimize()
def initialize_qt(self) -> None:
QtCore.QLoggingCategory.setFilterRules(
"qt.qml.binding.removal.info=true"
)
# Prevent blurry fonts that Qt seems to like
# QtQuick.QQuickWindow.setTextRenderType(
# QtQuick.QQuickWindow.NativeTextRendering
# )
# Use software rendering to prevent flickering on variable refresh rate
# displays.
# QtQuick.QQuickWindow.setSceneGraphBackend("software")
# QtQuick.QQuickWindow.setGraphicsApi(QtQuick.QSGRendererInterface.OpenGL)
# Set application information.
app_id = u"joystick.gremlin"
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
self.setWindowIcon(
QtGui.QIcon(gremlin.util.resource_path("gfx/icon.png"))
)
self.setApplicationDisplayName(
f"Joystick Gremlin {gremlin.util.get_code_release()}"
)
self.setOrganizationName("H2IK")
self.setOrganizationDomain(
"https://whitemagic.github.io/JoystickGremlin/"
)
self.setApplicationName("Joystick Gremlin")
# Change application wide font.
self.setFont(QtGui.QFont("Segoe UI", 11))
# Load font used for icons.
if QtGui.QFontDatabase.addApplicationFont(":/BootstrapIcons") < 0:
self.syslog.error("Failed to load BootstrapIcons")
# Create application and UI engine.
self.engine = QtQml.QQmlApplicationEngine(parent=self)
self.engine.addImportPath(".")
self.engine.addImportPath(str(Path(__file__).parent / "qml2"))
QtQml.qmlRegisterSingletonType(
QtCore.QUrl.fromLocalFile(
str(Path(__file__).parent / "qml" / "Style.qml")
),
"Gremlin.Style", 1, 0, "Style"
)
QtCore.QDir.addSearchPath(
"core_plugins",
gremlin.util.resource_path("action_plugins/")
)
QtCore.QDir.addSearchPath("qml", gremlin.util.resource_path("qml/"))
self.cfg = Configuration()
user_plugins_path = Path(self.cfg.value("global", "general", "plugin-directory"))
if user_plugins_path.is_dir():
QtCore.QDir.addSearchPath(
"user_plugins",
str(user_plugins_path)
)
# Create and register backend and signal objects
self.backend = gremlin.ui.backend.Backend(self.engine)
self.backend.newProfile()
# Register image provider for action summaries
action_image_provider = \
gremlin.ui.action_image_generator.ActionSummaryImageProvider()
self.engine.addImageProvider("action_summary", action_image_provider)
self.engine.rootContext().setContextProperty("backend", self.backend)
self.engine.rootContext().setContextProperty(
"uiState", self.backend.ui_state
)
self.engine.rootContext().setContextProperty(
"signal", gremlin.signal.signal
)
def main() -> int:
# Create Joystick Gremlin instance and run it.
app = JoystickGremlinApp(sys.argv)
app.exec()
logging.getLogger("system").info("Terminating Gremlin")
return 0
if __name__ == "__main__":
sys.exit(main())