-
Notifications
You must be signed in to change notification settings - Fork 47
/
joystick_gremlin.py
324 lines (271 loc) · 10.3 KB
/
joystick_gremlin.py
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
# -*- coding: utf-8; -*-
# Copyright (C) 2015 - 2024 Lionel Ott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Main UI of JoystickGremlin.
"""
import argparse
import ctypes
import logging
import os
import sys
import time
import traceback
from pathlib import Path
from typing import Any, Dict
# Import QtMultimedia so pyinstaller doesn't miss it
from PySide6 import QtCore, QtGui, QtQml, QtQuick, QtWidgets
import resources
import dill
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["QT_QUICK_CONTROLS_MATERIAL_VARIANT"] = "Normal"
os.environ["QT_QUICK_CONTROLS_UNIVERSAL_THEME"] = "Light"
# os.environ["QT_QUICK_CONTROLS_HOVER_ENABLED"] = "true"
# 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.config
import gremlin.error
import gremlin.plugin_manager
import gremlin.types
import gremlin.signal
import gremlin.ui.backend
import gremlin.ui.config
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.FileHandler(config["logfile"])
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("Starting Joystick Gremlin R14")
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 = "Uncaught exception:\n"
msg += " ".join(traceback.format_exception(exception_type, value, trace))
logging.getLogger("system").error(msg)
gremlin.util.display_error(msg)
def shutdown_cleanup() -> None:
"""Handles cleanup before terminating Gremlin."""
# Terminate potentially running EventListener loop
event_listener = gremlin.event_handler.EventListener()
event_listener.terminate()
# Terminate profile runner
backend = gremlin.ui.backend.Backend()
backend.runner.stop()
# Relinquish control over all VJoy devices used
gremlin.joystick_handling.VJoyProxy.reset()
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", "general", "plugin_directory",
PropertyType.String, "",
"Directory containing additional action plugins", {},
True
)
if __name__ == "__main__":
# 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"
)
args, _ = parser.parse_known_args()
# Configure 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"
})
syslog = logging.getLogger("system")
# Setup the configuration system
register_config_options()
# Show unhandled exceptions 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
# +-------------------------------------------------------------------------
# | Initialize QT system
# +-------------------------------------------------------------------------
# debug = QtQml.QQmlDebuggingEnabler()
QtCore.QLoggingCategory.setFilterRules("qt.qml.binding.removal.info=true")
# Initialize QT components
#QtWebEngine.QtWebEngine.initialize()
# 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)
# Create user interface
app_id = u"joystick.gremlin"
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
app = QtWidgets.QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon("gfx/icon.png"))
app.setApplicationDisplayName("Joystick Gremlin")
# Configure QSettings to keep QT happy
app.setOrganizationName("H2IK")
app.setOrganizationDomain("https://whitemagic.github.io/JoystickGremlin/")
app.setApplicationName("Joystick Gremlin")
# Ensure joystick devices are correctly setup
dill.DILL.init()
gremlin.joystick_handling.joystick_devices_initialization()
# Create application and UI engine
engine = QtQml.QQmlApplicationEngine(parent=app)
engine.addImportPath(".")
QtCore.QDir.addSearchPath(
"core_plugins",
gremlin.util.resource_path("action_plugins/")
)
cfg = Configuration()
user_plugins_path = Path(cfg.value("global", "general", "plugin_directory"))
if user_plugins_path.is_dir():
QtCore.QDir.addSearchPath(
"user_plugins",
str(user_plugins_path)
)
# +-------------------------------------------------------------------------
# | Register data types for use in QML
# +-------------------------------------------------------------------------
# Create and register backend and signal objects
backend = gremlin.ui.backend.Backend()
backend.newProfile()
engine.rootContext().setContextProperty("backend", backend)
engine.rootContext().setContextProperty("signal", gremlin.signal.signal)
# Load plugin code and UI elements
syslog.info("Initializing plugins")
gremlin.plugin_manager.PluginManager()
# +-------------------------------------------------------------------------
# | Start Gremlin UI
# +-------------------------------------------------------------------------
# Load icon fonts
if QtGui.QFontDatabase.addApplicationFont(":/BootstrapIcons") < 0:
syslog.error("Failed to load BootstrapIcons")
# Initialize main UI
engine.load(QtCore.QUrl.fromLocalFile("qml/Main.qml"))
if not engine.rootObjects():
sys.exit(-1)
# Check if vJoy is properly setup and if not display an error
# and terminate Gremlin
# try:
# syslog.info("Checking vJoy installation")
# vjoy_working = len([
# dev for dev in gremlin.joystick_handling.joystick_devices()
# if dev.is_virtual
# ]) != 0
#
# if not vjoy_working:
# logging.getLogger("system").error(
# "vJoy is not present or incorrectly setup."
# )
# raise gremlin.error.GremlinError(
# "vJoy is not present or incorrectly setup."
# )
#
# except (gremlin.error.GremlinError, dill.DILLError) as e:
# error_display = QtWidgets.QMessageBox(
# QtWidgets.QMessageBox.Critical,
# "Error",
# e.value,
# QtWidgets.QMessageBox.Ok
# )
# error_display.show()
# app.exec_()
#
# gremlin.joystick_handling.VJoyProxy.reset()
# event_listener = gremlin.event_handler.EventListener()
# event_listener.terminate()
# sys.exit(0)
# Load 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):
backend.loadProfile(args.profile)
else:
last_profile = Path(Configuration().value(
"global", "internal", "last_profile")
)
if last_profile.is_file():
backend.loadProfile(str(last_profile))
# if args.enable:
# ui.ui.actionActivate.setChecked(True)
# ui.activate(True)
# if args.start_minimized:
# ui.setHidden(True)
# Run UI
syslog.info("Gremlin UI launching")
app.aboutToQuit.connect(shutdown_cleanup)
app.exec()
syslog.info("Gremlin UI terminated")
syslog.info("Terminating Gremlin")
sys.exit(0)