Skip to content

Commit

Permalink
Linter and types
Browse files Browse the repository at this point in the history
  • Loading branch information
C0rn3j committed Oct 28, 2024
1 parent 732f6c5 commit b57ba35
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 82 deletions.
7 changes: 2 additions & 5 deletions scc/drivers/ds5drv.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,11 @@ def __repr__(self):
return "<DS5Controller %s>" % (self.get_id(),)

def _generate_id(self):
"""
ID is generated as 'ds5' or 'ds5:X' where 'X' starts as 1 and increases
as controllers with same ids are connected.
"""
"""ID is generated as 'ds5' or 'ds5:X' where 'X' starts as 1 and increases as controllers with same ids are connected."""
magic_number = 1
id = "ds5"
while id in self.daemon.get_active_ids():
id = "ds5:%s" % (magic_number,)
id = f"ds5:{magic_number}"
magic_number += 1
return id

Expand Down
4 changes: 2 additions & 2 deletions scc/drivers/evdevdrv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Universal driver for gamepads managed by evdev.
"""Universal driver for gamepads managed by evdev
Handles no devices by default. Instead of trying to guess which evdev device
is a gamepad and which user actually wants to be handled by SCC, list of enabled
Expand Down Expand Up @@ -397,7 +397,7 @@ def handle_new_device(self, syspath: str, *bunchofnones) -> bool:
try:
dev = evdev.InputDevice(eventnode)
assert dev.path == eventnode
config_fn = "evdev-%s.json" % (dev.name.strip().replace("/", ""),)
config_fn = "evdev-{}.json".format(dev.name.strip().replace("/", ""))
config_file = os.path.join(get_config_path(), "devices", config_fn)
except OSError as ose:
if ose.errno == 13:
Expand Down
81 changes: 40 additions & 41 deletions scc/drivers/steamdeck.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""SCC - Steam Deck Driver.
"""SCC - Steam Deck Driver
Based on sc_by_cable and steamdeck.c
Expand Down Expand Up @@ -66,35 +66,35 @@ class DeckInput(ctypes.Structure):


class DeckButton(IntEnum):
DOTS = 0b100000000000000000000000000000000000000000000000000
RSTICKTOUCH = 0b000100000000000000000000000000000000000000000000000
LSTICKTOUCH = 0b000010000000000000000000000000000000000000000000000
RGRIP2 = 0b000000001000000000000000000000000000000000000000000
LGRIP2 = 0b000000000100000000000000000000000000000000000000000
RSTICKPRESS = 0b000000000000000000000000100000000000000000000000000
LSTICKPRESS = 0b000000000000000000000000000010000000000000000000000
DOTS = 0b100000000000000000000000000000000000000000000000000
RSTICKTOUCH = 0b000100000000000000000000000000000000000000000000000
LSTICKTOUCH = 0b000010000000000000000000000000000000000000000000000
RGRIP2 = 0b000000001000000000000000000000000000000000000000000
LGRIP2 = 0b000000000100000000000000000000000000000000000000000
RSTICKPRESS = 0b000000000000000000000000100000000000000000000000000
LSTICKPRESS = 0b000000000000000000000000000010000000000000000000000
# bit 21 unused?
RPADTOUCH = 0b000000000000000000000000000000100000000000000000000
LPADTOUCH = 0b000000000000000000000000000000010000000000000000000
RPADPRESS = 0b000000000000000000000000000000001000000000000000000
LPADPRESS = 0b000000000000000000000000000000000100000000000000000
RGRIP = 0b000000000000000000000000000000000010000000000000000
LGRIP = 0b000000000000000000000000000000000001000000000000000
START = 0b000000000000000000000000000000000000100000000000000
C = 0b000000000000000000000000000000000000010000000000000
BACK = 0b000000000000000000000000000000000000001000000000000
DPAD_DOWN = 0b000000000000000000000000000000000000000100000000000
DPAD_LEFT = 0b000000000000000000000000000000000000000010000000000
DPAD_RIGHT = 0b000000000000000000000000000000000000000001000000000
DPAD_UP = 0b000000000000000000000000000000000000000000100000000
A = 0b000000000000000000000000000000000000000000010000000
X = 0b000000000000000000000000000000000000000000001000000
B = 0b000000000000000000000000000000000000000000000100000
Y = 0b000000000000000000000000000000000000000000000010000
LB = 0b000000000000000000000000000000000000000000000001000
RB = 0b000000000000000000000000000000000000000000000000100
LT = 0b000000000000000000000000000000000000000000000000010
RT = 0b000000000000000000000000000000000000000000000000001
RPADTOUCH = 0b000000000000000000000000000000100000000000000000000
LPADTOUCH = 0b000000000000000000000000000000010000000000000000000
RPADPRESS = 0b000000000000000000000000000000001000000000000000000
LPADPRESS = 0b000000000000000000000000000000000100000000000000000
RGRIP = 0b000000000000000000000000000000000010000000000000000
LGRIP = 0b000000000000000000000000000000000001000000000000000
START = 0b000000000000000000000000000000000000100000000000000
C = 0b000000000000000000000000000000000000010000000000000
BACK = 0b000000000000000000000000000000000000001000000000000
DPAD_DOWN = 0b000000000000000000000000000000000000000100000000000
DPAD_LEFT = 0b000000000000000000000000000000000000000010000000000
DPAD_RIGHT = 0b000000000000000000000000000000000000000001000000000
DPAD_UP = 0b000000000000000000000000000000000000000000100000000
A = 0b000000000000000000000000000000000000000000010000000
X = 0b000000000000000000000000000000000000000000001000000
B = 0b000000000000000000000000000000000000000000000100000
Y = 0b000000000000000000000000000000000000000000000010000
LB = 0b000000000000000000000000000000000000000000000001000
RB = 0b000000000000000000000000000000000000000000000000100
LT = 0b000000000000000000000000000000000000000000000000010
RT = 0b000000000000000000000000000000000000000000000000001


DIRECTLY_TRANSLATABLE_BUTTONS = (0
Expand All @@ -111,13 +111,12 @@ def map_button(i, from_, to):
return to if (i.buttons & from_) else 0


def map_dpad(i, low, hi):
def map_dpad(i, low, hi) -> int:
if (i.buttons & low) != 0:
return STICK_PAD_MIN
elif (i.buttons & hi) != 0:
if (i.buttons & hi) != 0:
return STICK_PAD_MAX
else:
return 0
return 0


def apply_deadzone(value, deadzone):
Expand Down Expand Up @@ -156,20 +155,20 @@ def set_gyro_enabled(self, enabled):
# Always on on deck
pass

def get_gyro_enabled(self):
def get_gyro_enabled(self) -> bool:
# Always on on deck
return True

def get_type(self):
def get_type(self) -> str:
return "deck"

def __repr__(self):
return "<Deck %s>" % (self.get_id(),)
def __repr__(self) -> str:
return f"<Deck {self.get_id()}>"

def get_gui_config_file(self):
def get_gui_config_file(self) -> str:
return "deck.config.json"

def configure(self, idle_timeout=None, enable_gyros=None, led_level=None):
def configure(self, idle_timeout=None, enable_gyros=None, led_level=None) -> None:
FORMAT = b'>BBBB60x'
# Timeout & Gyros
self._driver.overwrite_control(self._ccidx, struct.pack(
Expand All @@ -183,7 +182,7 @@ def clear_mappings(self):

def on_serial_got(self):
log.debug("Got SteamDeck with serial %s", self._serial)
self._id = "deck%s" % (self._serial,)
self._id = f"deck{self._serial}"
self.set_input_interrupt(ENDPOINT, 64, self._on_input)

def _on_input(self, endpoint, data):
Expand Down Expand Up @@ -239,7 +238,7 @@ def turnoff(self):


def init(daemon, config):
""" Registers hotplug callback for controller dongle """
"""Register hotplug callback for controller dongle"""
def cb(device, handle):
return Deck(device, handle, daemon)

Expand Down
67 changes: 33 additions & 34 deletions scc/drivers/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def __init__(self, device: USBDevice, handle: USBDeviceHandle) -> None:
self._transfer_list = []


def set_input_interrupt(self, endpoint, size, callback):
"""Set up input transfer.
def set_input_interrupt(self, endpoint: int, size: int, callback) -> None:
"""Set up input transfer
callback(endpoint, data) is called repeadedly with every packed received.
"""
def callback_wrapper(transfer):
def callback_wrapper(transfer) -> None:
if (transfer.getStatus() != usb1.TRANSFER_COMPLETED or
transfer.getActualLength() != size):
return
Expand Down Expand Up @@ -98,16 +98,16 @@ def make_request(self, index, callback, data, size=64):
"""
self._rmsg.append((
(
0x21, # request_type
0x09, # request
0x0300, # value
index, data
), index, size, callback
0x21, # request_type
0x09, # request
0x0300, # value
index, data,
), index, size, callback,
))


def flush(self):
"""Flushes all prepared control messages to the device."""
"""Flush all prepared control messages to the device."""
while len(self._cmsg):
msg = self._cmsg.pop()
self.handle.controlWrite(*msg)
Expand All @@ -116,10 +116,10 @@ def flush(self):
msg, index, size, callback = self._rmsg.pop()
self.handle.controlWrite(*msg)
data = self.handle.controlRead(
0xA1, # request_type
0x01, # request
0x0300, # value
index, size
0xA1, # request_type
0x01, # request
0x0300, # value
index, size,
)
callback(data)

Expand All @@ -135,19 +135,18 @@ def force_restart(self):


def claim(self, number):
"""
Helper method; Remembers list of claimed interfaces and allows to
unclaim them all at once using unclaim() method or automatically when
device is closed.
"""Remember list of claimed interfaces and allow to unclaim them all at once using unclaim() method
or automatically when device is closed.
"""
self.handle.claimInterface(number)
self._claimed.append(number)


def claim_by(self, klass, subclass, protocol):
"""
Claims all interfaces with specified parameters.
Returns number of claimed interfaces
def claim_by(self, klass, subclass, protocol) -> int:
"""Claim all interfaces with specified parameters.
Return number of claimed interfaces
"""
rv = 0
for inter in self.device[0]:
Expand Down Expand Up @@ -197,7 +196,7 @@ def __init__(self):
self._started = False
self._retry_devices = []
self._retry_devices_timer = 0
self._ctx = None # Set by start method
self._ctx = None # Set by start method
self._changed = 0


Expand Down Expand Up @@ -250,8 +249,8 @@ def handle_new_device(self, syspath: str, vendor: int, product: int) -> bool | N
return
if self.daemon:
self.daemon.add_error(
"usb:%s:%s" % (tp[0], tp[1]),
"Failed to open USB device: %s" % (e,)
f"usb:{tp[0]}:{tp[1]}",
f"Failed to open USB device: {e}",
)
return
else:
Expand All @@ -269,8 +268,8 @@ def handle_new_device(self, syspath: str, vendor: int, product: int) -> bool | N
return False
if self.daemon:
self.daemon.add_error(
"usb:%s:%s" % (tp[0], tp[1]),
"Failed to claim USB device: %s" % (e,)
f"usb:{tp[0]}:{tp[1]}",
f"Failed to claim USB device: {e}",
)
self._retry_devices.append((syspath, tp))
device.close()
Expand All @@ -279,14 +278,14 @@ def handle_new_device(self, syspath: str, vendor: int, product: int) -> bool | N
self._devices[device] = handled_device
self._syspaths[syspath] = device
log.debug("USB device added: %.4x:%.4x", *tp)
self.daemon.remove_error("usb:%s:%s" % (tp[0], tp[1]))
self.daemon.remove_error(f"usb:{tp[0]}:{tp[1]}")
return True
log.warning("Known USB device ignored: %.4x:%.4x", *tp)
device.close()
return False


def handle_removed_device(self, syspath: str, vendor: int, product: int):
def handle_removed_device(self, syspath: str, vendor: int, product: int) -> None:
if syspath in self._syspaths:
device = self._syspaths[syspath]
handled_device = self._devices[device]
Expand All @@ -296,11 +295,11 @@ def handle_removed_device(self, syspath: str, vendor: int, product: int):
try:
device.close()
except usb1.USBErrorNoDevice:
# Safe to ignore, happens when device is physiucally removed
# Safe to ignore, happens when device is physically removed
pass


def register_hotplug_device(self, callback, vendor_id: int, product_id: int, on_failure):
def register_hotplug_device(self, callback, vendor_id: int, product_id: int, on_failure) -> None:
self._known_ids[vendor_id, product_id] = callback
if on_failure:
self._fail_cbs[vendor_id, product_id] = on_failure
Expand All @@ -310,7 +309,7 @@ def register_hotplug_device(self, callback, vendor_id: int, product_id: int, on_
log.debug("Registered USB driver for %.4x:%.4x", vendor_id, product_id)


def unregister_hotplug_device(self, callback, vendor_id, product_id):
def unregister_hotplug_device(self, callback, vendor_id: int, product_id: int) -> None:
if self._known_ids.get((vendor_id, product_id)) == callback:
del self._known_ids[vendor_id, product_id]
if (vendor_id, product_id) in self._fail_cbs:
Expand Down Expand Up @@ -347,13 +346,13 @@ def init(daemon: SCCDaemon, config: dict) -> bool:
daemon.add_mainloop(_usb.mainloop)
return True

def start(daemon: SCCDaemon):
def start(daemon: SCCDaemon) -> None:
_usb.start()


def register_hotplug_device(callback, vendor_id: int, product_id: int, on_failure=None):
def register_hotplug_device(callback, vendor_id: int, product_id: int, on_failure=None) -> None:
_usb.register_hotplug_device(callback, vendor_id, product_id, on_failure)


def unregister_hotplug_device(callback, vendor_id: int, product_id: int):
def unregister_hotplug_device(callback, vendor_id: int, product_id: int) -> None:
_usb.unregister_hotplug_device(callback, vendor_id, product_id)

0 comments on commit b57ba35

Please sign in to comment.