From e51f79cd425660bd590739d483d2c4fadd6093ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ribeiro=20Bezerra?= Date: Sat, 16 May 2020 15:42:40 -0300 Subject: [PATCH] Fix multiple client/multiple controller support, fix button presses not being registered, fix controller mapping --- Nintendo Switch Combined Joy-Cons.json | 8 ++-- Nintendo Switch Left Joy-Con.json | 8 ++-- Nintendo Switch Pro Controller.json | 8 ++-- Nintendo Switch Right Joy-Con.json | 8 ++-- joycond-cemuhook.py | 55 ++++++++++++-------------- 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/Nintendo Switch Combined Joy-Cons.json b/Nintendo Switch Combined Joy-Cons.json index 2958a73..5ef04b3 100644 --- a/Nintendo Switch Combined Joy-Cons.json +++ b/Nintendo Switch Combined Joy-Cons.json @@ -1,8 +1,8 @@ { "left_analog_x": "ABS_X", - "left_analog_y": "ABS_Y", + "left_analog_y": "-ABS_Y", "right_analog_x": "ABS_RX", - "right_analog_y": "ABS_RY", + "right_analog_y": "-ABS_RY", "dpad_up": "BTN_DPAD_UP", "dpad_down": "BTN_DPAD_DOWN", "dpad_left": "BTN_DPAD_LEFT", @@ -17,8 +17,8 @@ "button_r1": "BTN_TR", "button_r2": "BTN_TR2", "button_r3": "BTN_THUMBR", - "button_share": "BTN_Z", - "button_options": "BTN_SELECT", + "button_share": "BTN_SELECT", + "button_options": "BTN_START", "button_ps": "BTN_MODE", "motion_y": null, "motion_x": null, diff --git a/Nintendo Switch Left Joy-Con.json b/Nintendo Switch Left Joy-Con.json index 2958a73..5ef04b3 100644 --- a/Nintendo Switch Left Joy-Con.json +++ b/Nintendo Switch Left Joy-Con.json @@ -1,8 +1,8 @@ { "left_analog_x": "ABS_X", - "left_analog_y": "ABS_Y", + "left_analog_y": "-ABS_Y", "right_analog_x": "ABS_RX", - "right_analog_y": "ABS_RY", + "right_analog_y": "-ABS_RY", "dpad_up": "BTN_DPAD_UP", "dpad_down": "BTN_DPAD_DOWN", "dpad_left": "BTN_DPAD_LEFT", @@ -17,8 +17,8 @@ "button_r1": "BTN_TR", "button_r2": "BTN_TR2", "button_r3": "BTN_THUMBR", - "button_share": "BTN_Z", - "button_options": "BTN_SELECT", + "button_share": "BTN_SELECT", + "button_options": "BTN_START", "button_ps": "BTN_MODE", "motion_y": null, "motion_x": null, diff --git a/Nintendo Switch Pro Controller.json b/Nintendo Switch Pro Controller.json index 3296e62..93c4f60 100644 --- a/Nintendo Switch Pro Controller.json +++ b/Nintendo Switch Pro Controller.json @@ -1,8 +1,8 @@ { "left_analog_x": "ABS_X", - "left_analog_y": "ABS_Y", + "left_analog_y": "-ABS_Y", "right_analog_x": "ABS_RX", - "right_analog_y": "ABS_RY", + "right_analog_y": "-ABS_RY", "dpad_up": "-ABS_HAT0Y", "dpad_down": "ABS_HAT0Y", "dpad_left": "-ABS_HAT0X", @@ -17,8 +17,8 @@ "button_r1": "BTN_TR", "button_r2": "BTN_TR2", "button_r3": "BTN_THUMBR", - "button_share": "BTN_Z", - "button_options": "BTN_SELECT", + "button_share": "BTN_SELECT", + "button_options": "BTN_START", "button_ps": "BTN_MODE", "motion_y": null, "motion_x": null, diff --git a/Nintendo Switch Right Joy-Con.json b/Nintendo Switch Right Joy-Con.json index 2958a73..5ef04b3 100644 --- a/Nintendo Switch Right Joy-Con.json +++ b/Nintendo Switch Right Joy-Con.json @@ -1,8 +1,8 @@ { "left_analog_x": "ABS_X", - "left_analog_y": "ABS_Y", + "left_analog_y": "-ABS_Y", "right_analog_x": "ABS_RX", - "right_analog_y": "ABS_RY", + "right_analog_y": "-ABS_RY", "dpad_up": "BTN_DPAD_UP", "dpad_down": "BTN_DPAD_DOWN", "dpad_left": "BTN_DPAD_LEFT", @@ -17,8 +17,8 @@ "button_r1": "BTN_TR", "button_r2": "BTN_TR2", "button_r3": "BTN_THUMBR", - "button_share": "BTN_Z", - "button_options": "BTN_SELECT", + "button_share": "BTN_SELECT", + "button_options": "BTN_START", "button_ps": "BTN_MODE", "motion_y": null, "motion_x": null, diff --git a/joycond-cemuhook.py b/joycond-cemuhook.py index 7625a5d..e8bb1c8 100644 --- a/joycond-cemuhook.py +++ b/joycond-cemuhook.py @@ -276,37 +276,30 @@ def _req_ports(self, message, address): def _req_data(self, message, address): flags = self._compat_ord(message[24]) reg_id = self._compat_ord(message[25]) + slot_id = self._compat_ord(message[21]) # reg_mac = message[26:32] if flags == 0 and reg_id == 0: # TODO: Check MAC if address not in self.clients: print('[udp] Client connected: {0[0]}:{0[1]}'.format(address)) - self.clients[address] = time.time() - - def _handle_request(self, request): - message, address = request - - # client_id = message[12:16] - msg_type = message[16:20] - - if msg_type == Message.Types['version']: - return - elif msg_type == Message.Types['ports']: - self._req_ports(message, address) - elif msg_type == Message.Types['data']: - self._req_data(message, address) - else: - print('Unknown message type: ' + str(msg_type)) + self.clients[address] = { + "timestamp": time.time(), + "controllers": [0,0,0,0] + } + else: + self.clients[address]["timestamp"] = time.time() + self.clients[address]["controllers"][slot_id] = 1 - def _res_data(self, message): + def _res_data(self, controller_index, message): now = time.time() - for address, timestamp in self.clients.copy().items(): - if now - timestamp < 5: - self.sock.sendto(message, address) - else: - print('[udp] Client disconnected: {0[0]}:{0[1]}'.format(address)) - del self.clients[address] + for address, data in self.clients.copy().items(): + if data["controllers"][controller_index] == 1: + if now - data["timestamp"] < 5: + self.sock.sendto(message, address) + else: + print('[udp] Client disconnected: {0[0]}:{0[1]}'.format(address)) + del self.clients[address] def _handle_request(self, request): message, address = request @@ -362,10 +355,14 @@ def report(self, device): buttons1 |= int(abs_to_button(device_state.get("dpad_left", 0x00))/255) << 7 buttons2 = 0x00 - buttons2 |= int(abs_to_button(device_state.get("button_l2", 0x00)/255)) - buttons2 |= int(abs_to_button(device_state.get("button_r2", 0x00)/255)) << 1 - buttons2 |= int(abs_to_button(device_state.get("button_l1", 0x00)/255)) << 2 - buttons2 |= int(abs_to_button(device_state.get("button_r1", 0x00)/255)) << 3 + buttons2 |= int(abs_to_button(device_state.get("button_l2", 0x00))/255) + buttons2 |= int(abs_to_button(device_state.get("button_r2", 0x00))/255) << 1 + buttons2 |= int(abs_to_button(device_state.get("button_l1", 0x00))/255) << 2 + buttons2 |= int(abs_to_button(device_state.get("button_r1", 0x00))/255) << 3 + buttons2 |= int(abs_to_button(device_state.get("button_triangle", 0x00))/255) << 4 + buttons2 |= int(abs_to_button(device_state.get("button_circle", 0x00))/255) << 5 + buttons2 |= int(abs_to_button(device_state.get("button_cross", 0x00))/255) << 6 + buttons2 |= int(abs_to_button(device_state.get("button_square", 0x00))/255) << 7 data.extend([ buttons1, @@ -443,7 +440,7 @@ def report(self, device): for sensor in sensors: data.extend(struct.pack('