Skip to content

Commit 0f2bc14

Browse files
authored
Merge pull request #4059 from cmitu/joy2key-fix6
joy2key: match config by Product/Vendor IDs
2 parents 247d203 + 73d4bb9 commit 0f2bc14

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

scriptmodules/admin/joy2key/joy2key_sdl.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,19 @@ class InputDev(object):
118118
Class representing a joystick device config.
119119
Maps the inputs of the device to event names
120120
name: the device's name
121-
guid: the GUID, as retuned by SDL
121+
guid: the GUID, as returned by SDL
122+
pid: the ProductID of the device
123+
vid: the VendorID of the device
122124
hats - a dictionary of { <HatNo>: list(<HatValue>, <Event>) }
123125
buttons - a dict of { <ButtonNo>: <Event> }
124126
axis - a dict of { <AxisNo>: list(<AxisDirection>, <Event>) }
125127
"""
126128

127-
def __init__(self, _name: str, _guid: str):
129+
def __init__(self, _name: str, _vid: int, _pid: int):
128130
self.name = _name
129-
self.guid = _guid
131+
self.guid = None
132+
self.vid = _vid
133+
self.pid = _pid
130134
self.axis = {}
131135
self.buttons = {}
132136
self.hats = {}
@@ -153,7 +157,7 @@ def get_axis_event(self, index: int, value: int) -> list:
153157
return None
154158

155159
def __str__(self) -> str:
156-
return str(f'{self.name}, hats: {self.hats}, buttons: {self.buttons}, axis: {self.axis}')
160+
return str(f'{self.name} (P:{self.pid}, V:{self.vid}), hats: {self.hats}, buttons: {self.buttons}, axis: {self.axis}')
157161

158162

159163
def generic_event_map(input: str, event_map: dict) -> str:
@@ -227,7 +231,7 @@ def get_all_ra_config(def_buttons: list) -> list:
227231
"""
228232
ra_config_list = []
229233
# add a generic mapping at index 0, to be used for un-configured joysticks
230-
generic_dev = InputDev("*", "*")
234+
generic_dev = InputDev("*", None, None)
231235
generic_dev.add_mappings(
232236
{}, # no axis
233237
{0: 'b', 1: 'a', 3: 'y', 4: 'x'}, # 4 buttons
@@ -236,7 +240,7 @@ def get_all_ra_config(def_buttons: list) -> list:
236240
ra_config_list.append(generic_dev)
237241
js_cfg_dir = CONFIG_DIR + '/all/retroarch-joypads/'
238242

239-
config = ConfigParser(delimiters="=", strict=False, interpolation=None)
243+
config = ConfigParser(delimiters="=", strict=False, interpolation=None, converters={'int': (lambda s: s.strip('"'))})
240244
for file in os.listdir(js_cfg_dir):
241245
# skip non '.cfg' files
242246
if not file.endswith('.cfg') or file.startswith('.'):
@@ -247,8 +251,12 @@ def get_all_ra_config(def_buttons: list) -> list:
247251
config.clear()
248252
# ConfigParser needs a section, make up a section to appease it
249253
config.read_string('[device]\n' + cfg_file.read())
254+
LOG.debug(f'Parsing config "{file}"')
250255
conf_vals = config['device']
251256
dev_name = conf_vals['input_device'].strip('"')
257+
# fallback to None if there are no PID/VID in the configuration
258+
dev_vid = conf_vals.getint('input_vendor_id', None)
259+
dev_pid = conf_vals.getint('input_product_id', None)
252260

253261
# translate the RetroArch inputs from the configuration file
254262
axis, buttons, hats = {}, {}, {}
@@ -268,9 +276,10 @@ def get_all_ra_config(def_buttons: list) -> list:
268276
axis.setdefault(input_index, []).append((input_value, event_name))
269277
else:
270278
continue
271-
ra_dev_config = InputDev(dev_name, None)
279+
ra_dev_config = InputDev(dev_name, dev_vid, dev_pid)
272280
ra_dev_config.add_mappings(axis, buttons, hats)
273281
ra_config_list.append(ra_dev_config)
282+
LOG.debug(f'Added config for "{dev_name}" from "{file}"')
274283
except Exception as e:
275284
LOG.warning(f'Parsing error for {file}: {e}')
276285
continue
@@ -370,12 +379,16 @@ def handle_new_input(e: SDL_Event, axis_norm_value: int = 0) -> bool:
370379
stick = joystick.SDL_JoystickOpen(event.jdevice.which)
371380
name = joystick.SDL_JoystickName(stick).decode('utf-8')
372381
guid = create_string_buffer(33)
382+
vid = joystick.SDL_JoystickGetVendor(stick)
383+
pid = joystick.SDL_JoystickGetProduct(stick)
384+
373385
_SDL_JoystickGetGUIDString(joystick.SDL_JoystickGetGUID(stick), guid, 33)
374-
LOG.debug(f'Joystick #{joystick.SDL_JoystickInstanceID(stick)} {name} added')
386+
LOG.debug(f'Joystick #{joystick.SDL_JoystickInstanceID(stick)} {name} (P:{pid}, V:{vid}) added')
375387
conf_found = False
376-
# try to find a configuration for the joystick
388+
# try to find a configuration for the joystick, based on name, GUID and Vendor/Product IDs
377389
for key, dev_conf in enumerate(configs):
378-
if dev_conf.name == str(name) or dev_conf.guid == guid.value.decode():
390+
if dev_conf.name == str(name) or dev_conf.guid == guid.value.decode() or \
391+
(dev_conf.pid == str(pid) and dev_conf.vid == str(vid)):
379392
# Add the matching joystick configuration to the watched list
380393
active_devices[joystick.SDL_JoystickInstanceID(stick)] = (key, stick)
381394
LOG.debug(f'Added configuration for known device {configs[key]}')

0 commit comments

Comments
 (0)