Clearer structure

This commit is contained in:
Marc Riera Irigoyen 2022-10-16 16:30:41 +02:00
parent b6a1a9f5bb
commit a5580bdbde
10 changed files with 60 additions and 52 deletions

2
.vscode/launch.json vendored
View file

@ -8,7 +8,7 @@
"name": "Run main program", "name": "Run main program",
"type": "python", "type": "python",
"request": "launch", "request": "launch",
"program": "ddgo-converter.py", "program": "ddgo-converter/ddgo-converter.py",
"console": "integratedTerminal", "console": "integratedTerminal",
"justMyCode": true "justMyCode": true
} }

View file

@ -0,0 +1,12 @@
from enum import Enum
from hashlib import sha1
class EmulatedGamepad:
class GamepadType(Enum):
DGOC44U = 0
PS1 = 1
def __init__(self):
super().__init__()
self.type

View file

@ -0,0 +1,35 @@
from enum import Enum
from hashlib import sha1
class PhysicalGamepad:
class GamepadType(Enum):
UNKNOWN = 0
CLASSIC = 1
NSWITCH = 2
def __init__(self, vid, pid, name):
super().__init__()
self.vid = vid
self.pid = pid
self.name = name
self.id = self._get_gamepad_id()
self.hash = self._get_gamepad_hash()
self.type = self.get_gamepad_type()
self.config = []
def _get_gamepad_id(self):
vid = format(self.vid, "x").zfill(4)
pid = format(self.pid, "x").zfill(4)
id = str(vid + ":" + pid)
return id
def _get_gamepad_hash(self):
hash = sha1(str(self.id + ":" + self.name).encode('utf-8')).hexdigest()
return hash
def get_gamepad_type(self):
match self.vid, self.pid:
case 0x0f0d, 0x00c1:
return self.GamepadType.NSWITCH
return self.GamepadType.UNKNOWN

View file

@ -2,7 +2,7 @@ import sys
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem, QHeaderView from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem, QHeaderView
from gui.main_ui import Ui_MainWindow from gui.main_ui import Ui_MainWindow
from handlers.gamepad import Gamepad from gamepads.physical import PhysicalGamepad
from models.gamepad import GamepadModel from models.gamepad import GamepadModel
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
@ -32,7 +32,7 @@ class MainWindow(QMainWindow):
def controller_list_selection_changed(self): def controller_list_selection_changed(self):
enabled = False enabled = False
rows = self._gui.tableView_physicalControllerList.selectionModel().selectedRows() rows = self._gui.tableView_physicalControllerList.selectionModel().selectedRows()
if rows and self.gamepad_model.gamepads[rows[0].row()].type != Gamepad.GamepadType.UNKNOWN: if rows and self.gamepad_model.gamepads[rows[0].row()].type != PhysicalGamepad.GamepadType.UNKNOWN:
enabled = True enabled = True
self._gui.pushButton_emulatedControllerStart.setEnabled(enabled) self._gui.pushButton_emulatedControllerStart.setEnabled(enabled)

View file

@ -1,49 +1,7 @@
from enum import Enum from enum import Enum
from evdev import InputDevice, list_devices, ecodes as e, UInput, AbsInfo from evdev import InputDevice, list_devices, ecodes as e, UInput, AbsInfo
from hashlib import sha1 from gamepads.physical import PhysicalGamepad
from gamepads.emulated import EmulatedGamepad
class Gamepad:
class GamepadType(Enum):
UNKNOWN = 0
CLASSIC = 1
NSWITCH = 2
def __init__(self, vid, pid, name):
super().__init__()
self.vid = vid
self.pid = pid
self.name = name
self.id = self._get_gamepad_id()
self.hash = self._get_gamepad_hash()
self.type = self._get_gamepad_type()
self.config = []
def _get_gamepad_id(self):
vid = format(self.vid, "x").zfill(4)
pid = format(self.pid, "x").zfill(4)
id = str(vid + ":" + pid)
return id
def _get_gamepad_hash(self):
hash = sha1(str(self.id + ":" + self.name).encode('utf-8')).hexdigest()
return hash
def get_gamepad_type(self):
match self.vid, self.pid:
case 0x0f0d, 0x00c1:
return self.GamepadType.NSWITCH
return self.GamepadType.UNKNOWN
class EmulatedGamepad:
class GamepadType(Enum):
DGOC44U = 0
PS1 = 1
def __init__(self, type):
super().__init__()
self.type = type
class GamepadHandler: class GamepadHandler:
def __init__(self): def __init__(self):
@ -53,10 +11,10 @@ class GamepadHandler:
gamepads = [] gamepads = []
devices = [InputDevice(path) for path in list_devices()] devices = [InputDevice(path) for path in list_devices()]
for device in devices: for device in devices:
gamepads.append(Gamepad(device.info.vendor, device.info.product, device.name)) gamepads.append(PhysicalGamepad(device.info.vendor, device.info.product, device.name))
return gamepads return gamepads
def start_gamepad_emulator(gamepad: Gamepad, emulated_gamepad: EmulatedGamepad): def start_gamepad_emulator(gamepad, emulated_gamepad):
realtype = gamepad.type realtype = gamepad.type
emutype = emulated_gamepad.type emutype = emulated_gamepad.type
print(str(realtype)+str(emutype)) print(str(realtype)+str(emutype))

View file

@ -1,5 +1,5 @@
from PyQt5.QtCore import Qt, QVariant, QAbstractTableModel from PyQt5.QtCore import Qt, QVariant, QAbstractTableModel
from handlers.gamepad import Gamepad from gamepads.physical import PhysicalGamepad
headers = ["ID", "Name", "Status"] headers = ["ID", "Name", "Status"]
@ -16,7 +16,7 @@ class GamepadModel(QAbstractTableModel):
case 1: case 1:
return self.gamepads[index.row()].name return self.gamepads[index.row()].name
case 2: case 2:
if self.gamepads[index.row()].type == Gamepad.GamepadType.UNKNOWN: if self.gamepads[index.row()].type == PhysicalGamepad.GamepadType.UNKNOWN:
return "Not configured" return "Not configured"
else: else:
return "Configured" return "Configured"

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
evdev
hashlib
PyQt5