Skip to content

Commit fa6b49d

Browse files
authored
Implemented grabbing input device (#58)
* Implemented grabbing input device * Chmod scripts executable * Bump version * Chmod main script executable * Use shell wrapper script that activates venv Add pip constraints * Chmod scripts * Remove dot imports * Add blank line * Modify log messages * Bump version * Update performs a clean reinstall * Fix executable path * Preserve repo ownership * Fix stat dir * Remove constraints * Re-enable constraints * Preserve currently checked out branch * Add cleanup function * Add cd to parent dir * Adapt reboot hint * Adapt to latest script changes * Remove unnecessary blank lines * Update to v0.6.6 * Add adafruit-circuitpython-typing * Set fixed version * Fix missing = * Reduce delays * Keep track of relayed devices * Add debug output * Initialize gadgets once and share across module * Move device functions to __init__.py * Move device functions back to relay.py * Remove temporary debug output Bump version * Defer USB device init * Check gadget state on relay init Rename symbols * Improve gadget init debug output * Update to v0.6.7 * Show versions before updating * Change to project dir first * Use variables instead of hard-coded paths * Make user owner of all files * Leave chmod to install.sh * Preserve ownership before installing * Bump version --------- Co-authored-by: Hannes U @cylonid Co-authored-by: Benjamin T @quaxalber
1 parent 2230a61 commit fa6b49d

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Follow these steps to install and configure the project:
151151
└─1326 python3.11 /home/user/bluetooth_2_usb/bluetooth_2_usb.py --auto_discover
152152

153153
Dec 02 23:16:37 pi0w systemd[1]: Started bluetooth_2_usb.service - Bluetooth to USB HID relay.
154-
Dec 02 23:16:39 pi0w bluetooth_2_usb[1326]: 23-12-02 23:16:39 [INFO] Launching Bluetooth 2 USB v0.6.7
154+
Dec 02 23:16:39 pi0w bluetooth_2_usb[1326]: 23-12-02 23:16:39 [INFO] Launching Bluetooth 2 USB v0.7.0
155155
Dec 02 23:16:39 pi0w bluetooth_2_usb[1326]: 23-12-02 23:16:39 [INFO] Discovering input devices...
156156
Dec 02 23:16:42 pi0w bluetooth_2_usb[1326]: 23-12-02 23:16:42 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "a1:b2:c3:d4:e5:f6"
157157
Dec 02 23:16:45 pi0w bluetooth_2_usb[1326]: 23-12-02 23:16:45 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "a1:b2:c3:d4:e5:f6"
@@ -368,7 +368,7 @@ Here's a few things you could try:
368368
user@pi0w:~ $ sudo service bluetooth_2_usb stop && sudo bluetooth_2_usb -ad ; sudo service bluetooth_2_usb start
369369
23-12-12 13:03:28 [DEBUG] CLI args: Namespace(device_ids=None, auto_discover=True, debug=True, log_to_file=False, log_path='/var/log/bluetooth_2_usb/bluetooth_2_usb.log', version=False, list_devices=False)
370370
23-12-12 13:03:28 [DEBUG] Logging to stdout
371-
23-12-12 13:03:28 [INFO] Launching Bluetooth 2 USB v0.6.7
371+
23-12-12 13:03:28 [INFO] Launching Bluetooth 2 USB v0.7.0
372372
23-12-12 13:03:28 [INFO] Discovering input devices...
373373
23-12-12 13:03:28 [DEBUG] Auto-discovery enabled. Relaying all input devices.
374374
23-12-12 13:03:28 [DEBUG] Initializing USB gadgets...

bluetooth_2_usb.py

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
_logger = get_logger()
16-
_VERSION = "0.6.7"
16+
_VERSION = "0.7.0"
1717
_VERSIONED_NAME = f"Bluetooth 2 USB v{_VERSION}"
1818

1919

@@ -48,7 +48,7 @@ async def _main() -> NoReturn:
4848
_logger.debug(log_handlers_message)
4949
_logger.info(f"Launching {_VERSIONED_NAME}")
5050

51-
controller = RelayController(args.device_ids, args.auto_discover)
51+
controller = RelayController(args.device_ids, args.auto_discover, args.grab_devices)
5252
await controller.async_relay_devices()
5353

5454

bluetooth_2_usb.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ After=multi-user.target
44

55
[Service]
66
User=root
7-
ExecStart=/usr/bin/bluetooth_2_usb --auto_discover
7+
ExecStart=/usr/bin/bluetooth_2_usb --auto_discover --grab_devices
88
Environment=PYTHONUNBUFFERED=1
99
Restart=on-failure
1010

src/bluetooth_2_usb/args.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def parse_args() -> Namespace:
7373
default=False,
7474
help="List all available input devices and exit.",
7575
)
76+
parser.add_argument(
77+
"--grab_devices",
78+
"-g",
79+
action="store_true",
80+
default=False,
81+
help="Grab the input devices, i.e., suppress any events on your relay device (RPi).",
82+
)
7683

7784
args = parser.parse_args()
7885

src/bluetooth_2_usb/relay.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ def matches(self, device: InputDevice) -> bool:
106106

107107

108108
class DeviceRelay:
109-
def __init__(self, input_device: InputDevice):
109+
def __init__(self, input_device: InputDevice, grab_device: bool = False):
110110
self._input_device = input_device
111+
if grab_device:
112+
self._input_device.grab()
111113
if not all_gadgets_ready():
112114
init_usb_gadgets()
113115

@@ -178,12 +180,16 @@ class RelayController:
178180
"""
179181

180182
def __init__(
181-
self, device_identifiers: list[str] = None, auto_discover: bool = False
183+
self,
184+
device_identifiers: list[str] = None,
185+
auto_discover: bool = False,
186+
grab_devices: bool = False,
182187
) -> None:
183188
if not device_identifiers:
184189
device_identifiers = []
185190
self._device_ids = [DeviceIdentifier(id) for id in device_identifiers]
186191
self._auto_discover = auto_discover
192+
self._grab_devices = grab_devices
187193
self._cancelled = False
188194
self._device_relay_paths: list[str] = []
189195

@@ -230,7 +236,7 @@ def _create_task(self, device: InputDevice, task_group: TaskGroup) -> None:
230236

231237
async def _async_relay_events(self, device: InputDevice) -> NoReturn:
232238
try:
233-
relay = DeviceRelay(device)
239+
relay = DeviceRelay(device, self._grab_devices)
234240
self._device_relay_paths.append(device.path)
235241
_logger.info(f"Activated {repr(relay)}")
236242
await relay.async_relay_events_loop()

0 commit comments

Comments
 (0)