Skip to content

Commit 9e3f65e

Browse files
committed
Tear out hardcoded libusb1 and enum libraries. WHAT THE HELL. libusb1 is now a real dependency.
Also some linter changes as is customary
1 parent b0a46ec commit 9e3f65e

File tree

11 files changed

+84
-4769
lines changed

11 files changed

+84
-4769
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Donation links for kozec, who is the original developer, can be found on the [ol
4545
- [python-evdev](https://python-evdev.readthedocs.io/en/latest/)
4646
- [python-pylibacl](http://pylibacl.k1024.org/)
4747
- [python-vdf](https://pypi.org/project/vdf/)
48+
- python-libusb1
4849
- [gtk-layer-shell](https://github.com/wmww/gtk-layer-shell)
4950

5051
### Installing

scc/cemuhook_server.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
#!/usr/bin/env python3
2-
"""
3-
SC-Controller - Daemon - CemuHookUDP motion provider
1+
"""SC-Controller - Daemon - CemuHookUDP motion provider.
42
53
Accepts all connections from clients and sends data captured
64
by 'cemuhook' actions to them.
75
"""
8-
from scc.tools import find_library
9-
from scc.lib.enum import IntEnum
10-
from ctypes import c_uint32, c_int, c_bool, c_char_p, c_size_t, c_float
11-
from ctypes import create_string_buffer
12-
import logging, os, socket
6+
import logging
7+
import os
8+
import socket
9+
from ctypes import c_bool, c_char_p, c_float, c_int, c_size_t, create_string_buffer
10+
from datetime import datetime, timedelta
11+
from enum import IntEnum
1312
from threading import Thread
1413
from time import sleep
15-
from datetime import datetime, timedelta
14+
15+
from scc.tools import find_library
16+
1617
log = logging.getLogger("CemuHook")
1718

1819
BUFFER_SIZE = 1024

scc/device_monitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def add_callback(self, subsystem: str, vendor_id: int, product_id: int, added_cb
5252
"""
5353
key = (subsystem, vendor_id, product_id)
5454
# TODO type this, might need 3.11 for https://docs.python.org/3/library/typing.html#typing.Self ?
55-
# print(f"add_callback for subsystem {subsystem} for {vendor_id}:{product_id} - existing {self.dev_added_cbs}; of type {type(self.dev_added_cbs)} with existing removals {self.dev_removed_cbs}; of type {type(self.dev_removed_cbs)}")
55+
# print(f"add_callback for subsystem {subsystem} for {vendor_id}:{product_id} - existing {self.dev_added_cbs}; of type {type(self.dev_added_cbs)} with existing removals {self.dev_removed_cbs}; of type {type(self.dev_removed_cbs)}\n")
5656
# print(f" {added_cb}; of type {type(added_cb)} with additional removals {added_cb}; of type {type(added_cb)}\n")
5757
assert key not in self.dev_added_cbs, (f"DeviceMonitor add_callback found an already present device: \n {key[0], key[1], key[2]} in hex: {hex(key[1]), hex(key[2])}\n in existing DeviceMonitor cbs:\n {self.dev_added_cbs}\n \
5858
Perhaps a custom device was added to config that we already considered earlier? Check $config_dir/scc/devices/")

scc/drivers/sc_by_cable.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
#!/usr/bin/env python3
2-
"""
3-
SC Controller - Steam Controller Driver
1+
"""SC Controller - Steam Controller Driver.
42
53
Called and used when single Steam Controller is connected directly by USB cable.
64
75
Shares a lot of classes with sc_dongle.py
86
"""
97

10-
from scc.lib.usb1 import USBError
8+
import logging
9+
import struct
10+
11+
from usb1 import USBError
12+
1113
from scc.drivers.usb import USBDevice, register_hotplug_device
12-
from .sc_dongle import ControllerInput, TUP_FORMAT
13-
from .sc_dongle import SCStatus, SCController
14-
import struct, logging
14+
15+
from .sc_dongle import TUP_FORMAT, ControllerInput, SCController, SCStatus
1516

1617
VENDOR_ID = 0x28de
1718
PRODUCT_ID = 0x1102
@@ -22,48 +23,48 @@
2223
log = logging.getLogger("SCCable")
2324

2425
def init(daemon, config):
25-
""" Registers hotplug callback for controller dongle """
26+
"""Register hotplug callback for controller dongle."""
2627
def cb(device, handle):
2728
return SCByCable(device, handle, daemon)
28-
29+
2930
register_hotplug_device(cb, VENDOR_ID, PRODUCT_ID)
3031
return True
3132

3233

3334
class SCByCable(USBDevice, SCController):
3435
FORMAT1 = b'>BBBBB13sB2s'
35-
36+
3637
def __init__(self, device, handle, daemon):
3738
self.daemon = daemon
3839
USBDevice.__init__(self, device, handle)
3940
SCController.__init__(self, self, CONTROLIDX, ENDPOINT)
4041
self._ready = False
4142
self._last_tup = None
4243
daemon.add_mainloop(self._timer)
43-
44+
4445
self.claim_by(klass=3, subclass=0, protocol=0)
4546
self.read_serial()
46-
47-
47+
48+
4849
def generate_serial(self):
4950
self._serial = "%s:%s" % (self.device.getBusNumber(), self.device.getPortNumber())
50-
51-
51+
52+
5253
def disconnected(self):
5354
# Overrided to skip returning serial# to pool.
5455
pass
55-
56-
56+
57+
5758
def __repr__(self):
5859
return "<SCByCable %s>" % (self.get_id(),)
59-
60-
61-
def on_serial_got(self):
60+
61+
62+
def on_serial_got(self):
6263
log.debug("Got wired SC with serial %s", self._serial)
6364
self._id = "sc%s" % (self._serial,)
64-
self.set_input_interrupt(ENDPOINT, 64, self._wait_input)
65-
66-
65+
self.set_input_interrupt(ENDPOINT, 64, self._wait_input)
66+
67+
6768
def _wait_input(self, endpoint, data):
6869
tup = ControllerInput._make(struct.unpack(TUP_FORMAT, data))
6970
if not self._ready:
@@ -72,8 +73,8 @@ def _wait_input(self, endpoint, data):
7273
self._ready = True
7374
if tup.status == SCStatus.INPUT:
7475
self._last_tup = tup
75-
76-
76+
77+
7778
def _timer(self):
7879
m = self.get_mapper()
7980
if m:
@@ -89,15 +90,15 @@ def _timer(self):
8990
log.exception(e)
9091
log.error("Error while communicating with device, baling out...")
9192
self.force_restart()
92-
93-
93+
94+
9495
def close(self):
9596
if self._ready:
9697
self.daemon.remove_controller(self)
9798
self._ready = False
9899
self.daemon.remove_mainloop(self._timer)
99100
USBDevice.close(self)
100-
101-
101+
102+
102103
def turnoff(self):
103104
log.warning("Ignoring request to turn off wired controller.")

scc/drivers/steamdeck.py

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#!/usr/bin/env python3
2-
"""
3-
SCC - Steam Deck Driver
1+
"""SCC - Steam Deck Driver.
42
53
Based on sc_by_cable and steamdeck.c
64
@@ -10,23 +8,23 @@
108
to not do so periodically.
119
"""
1210

13-
from scc.lib import IntEnum
14-
from scc.lib.usb1 import USBError
11+
import ctypes
12+
import logging
13+
import struct
14+
15+
from scc.constants import STICK_PAD_MAX, STICK_PAD_MIN, ControllerFlags, SCButtons
16+
from scc.drivers.sc_dongle import SCController, SCPacketType
1517
from scc.drivers.usb import USBDevice, register_hotplug_device
16-
from scc.constants import STICK_PAD_MIN, STICK_PAD_MAX
17-
from scc.constants import SCButtons, ControllerFlags
18-
from scc.drivers.sc_dongle import ControllerInput, SCController, SCPacketType
19-
import struct, logging, ctypes
20-
21-
22-
VENDOR_ID = 0x28de
23-
PRODUCT_ID = 0x1205
24-
ENDPOINT = 3
25-
CONTROLIDX = 2
26-
PACKET_SIZE = 128
27-
UNLIZARD_INTERVAL = 100
18+
from scc.lib import IntEnum
19+
20+
VENDOR_ID = 0x28de
21+
PRODUCT_ID = 0x1205
22+
ENDPOINT = 3
23+
CONTROLIDX = 2
24+
PACKET_SIZE = 128
25+
UNLIZARD_INTERVAL = 100
2826
# Basically, sticks on deck tend to return to non-zero position
29-
STICK_DEADZONE = 3000
27+
STICK_DEADZONE = 3000
3028

3129
log = logging.getLogger("deck")
3230

@@ -41,7 +39,7 @@ class DeckInput(ctypes.Structure):
4139
('lpad_y', ctypes.c_int16),
4240
('rpad_x', ctypes.c_int16),
4341
('rpad_y', ctypes.c_int16),
44-
42+
4543
('accel_x', ctypes.c_int16),
4644
('accel_y', ctypes.c_int16),
4745
('accel_z', ctypes.c_int16),
@@ -52,14 +50,14 @@ class DeckInput(ctypes.Structure):
5250
('q2', ctypes.c_uint16),
5351
('q3', ctypes.c_uint16),
5452
('q4', ctypes.c_uint16),
55-
53+
5654
('ltrig', ctypes.c_uint16),
5755
('rtrig', ctypes.c_uint16),
5856
('stick_x', ctypes.c_int16),
5957
('stick_y', ctypes.c_int16),
6058
('rstick_x', ctypes.c_int16),
6159
('rstick_y', ctypes.c_int16),
62-
60+
6361
# Values above are readed directly from deck
6462
# Values bellow are converted so mapper can understand them
6563
('dpad_x', ctypes.c_int16),
@@ -135,59 +133,59 @@ class Deck(USBDevice, SCController):
135133
| ControllerFlags.IS_DECK
136134
| ControllerFlags.HAS_RSTICK
137135
)
138-
136+
139137
def __init__(self, device, handle, daemon):
140138
self.daemon = daemon
141139
USBDevice.__init__(self, device, handle)
142140
SCController.__init__(self, self, CONTROLIDX, ENDPOINT)
143141
self._old_state = DeckInput()
144142
self._input = DeckInput()
145143
self._ready = False
146-
144+
147145
self.claim_by(klass=3, subclass=0, protocol=0)
148146
self.read_serial()
149-
147+
150148
def generate_serial(self):
151149
self._serial = "%s:%s" % (self.device.getBusNumber(), self.device.getPortNumber())
152-
150+
153151
def disconnected(self):
154152
# Overrided to skip returning serial# to pool.
155153
pass
156-
154+
157155
def set_gyro_enabled(self, enabled):
158156
# Always on on deck
159157
pass
160-
158+
161159
def get_gyro_enabled(self):
162160
# Always on on deck
163161
return True
164-
162+
165163
def get_type(self):
166164
return "deck"
167-
165+
168166
def __repr__(self):
169167
return "<Deck %s>" % (self.get_id(),)
170-
168+
171169
def get_gui_config_file(self):
172170
return "deck.config.json"
173-
171+
174172
def configure(self, idle_timeout=None, enable_gyros=None, led_level=None):
175173
FORMAT = b'>BBBB60x'
176174
# Timeout & Gyros
177175
self._driver.overwrite_control(self._ccidx, struct.pack(
178176
FORMAT, SCPacketType.CONFIGURE, 0x03, 0x08, 0x07))
179-
177+
180178
def clear_mappings(self):
181179
FORMAT = b'>BB62x'
182180
# Timeout & Gyros
183181
self._driver.overwrite_control(self._ccidx,
184182
struct.pack(FORMAT, SCPacketType.CLEAR_MAPPINGS, 0x01))
185-
183+
186184
def on_serial_got(self):
187185
log.debug("Got SteamDeck with serial %s", self._serial)
188186
self._id = "deck%s" % (self._serial,)
189-
self.set_input_interrupt(ENDPOINT, 64, self._on_input)
190-
187+
self.set_input_interrupt(ENDPOINT, 64, self._on_input)
188+
191189
def _on_input(self, endpoint, data):
192190
if not self._ready:
193191
self.daemon.add_controller(self)
@@ -199,7 +197,7 @@ def _on_input(self, endpoint, data):
199197
if self._input.seq % UNLIZARD_INTERVAL == 0:
200198
# Keeps lizard mode from happening
201199
self.clear_mappings()
202-
200+
203201
# Handle dpad
204202
self._input.dpad_x = map_dpad(self._input, DeckButton.DPAD_LEFT, DeckButton.DPAD_RIGHT)
205203
self._input.dpad_y = map_dpad(self._input, DeckButton.DPAD_DOWN, DeckButton.DPAD_UP)
@@ -222,20 +220,20 @@ def _on_input(self, endpoint, data):
222220
self._input.stick_y = apply_deadzone(self._input.stick_y, STICK_DEADZONE)
223221
self._input.rstick_x = apply_deadzone(self._input.rstick_x, STICK_DEADZONE)
224222
self._input.rstick_y = apply_deadzone(self._input.rstick_y, STICK_DEADZONE)
225-
223+
226224
# Invert Gyro Roll to match Steam Controller coordinate system
227225
self._input.groll = -self._input.groll
228226

229227
m = self.get_mapper()
230228
if m:
231229
self.mapper.input(self, self._old_state, self._input)
232-
230+
233231
def close(self):
234232
if self._ready:
235233
self.daemon.remove_controller(self)
236234
self._ready = False
237235
USBDevice.close(self)
238-
236+
239237
def turnoff(self):
240238
log.warning("Ignoring request to turn off steamdeck.")
241239

@@ -244,7 +242,6 @@ def init(daemon, config):
244242
""" Registers hotplug callback for controller dongle """
245243
def cb(device, handle):
246244
return Deck(device, handle, daemon)
247-
245+
248246
register_hotplug_device(cb, VENDOR_ID, PRODUCT_ID)
249247
return True
250-

scc/drivers/usb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import traceback
1616
from typing import TYPE_CHECKING
1717

18-
from scc.lib import usb1
18+
import usb1
1919

2020
if TYPE_CHECKING:
2121
from scc.sccdaemon import SCCDaemon

scc/lib/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
#!/usr/bin/env python3
2-
3-
from .enum import Enum, IntEnum, unique
1+
from enum import Enum, IntEnum, unique

0 commit comments

Comments
 (0)