btk_dbus.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/python3
  2. #
  3. # thanhle Bluetooth keyboard/Mouse emulator DBUS Service
  4. #
  5. import dbus
  6. DBUS_OBJ_BTK_NAME = "org.thanhle.btkbservice"
  7. DBUS_OBJ_BTK_PATH = "/org/thanhle/btkbservice"
  8. class BTKbService(dbus.service.Object):
  9. def __init__(self, device):
  10. # set up as a dbus service
  11. dbus_conn = dbus.service.BusName(DBUS_OBJ_BTK_NAME, bus=dbus.SystemBus())
  12. dbus.service.Object.__init__(self, dbus_conn, DBUS_OBJ_BTK_PATH)
  13. # setup our device
  14. self.device = device
  15. @dbus.service.method(DBUS_OBJ_BTK_NAME, in_signature="yay")
  16. def send_keys(self, modifier_byte, keys):
  17. state = [0xA1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
  18. state[2] = int(modifier_byte)
  19. count = 4
  20. for key_code in keys:
  21. if count < 10:
  22. state[count] = int(key_code)
  23. count += 1
  24. self.device.send_string(state)
  25. @dbus.service.method(DBUS_OBJ_BTK_NAME, in_signature="yay")
  26. def send_mouse(self, modifier_byte, keys):
  27. state = [0xA1, 2, 0, 0, 0, 0]
  28. count = 2
  29. for key_code in keys:
  30. if count < 6:
  31. state[count] = int(key_code)
  32. count += 1
  33. self.device.send_string(state)