control.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import functools
  2. import socket
  3. import struct
  4. import time
  5. import module.device.method.scrcpy.const as const
  6. def inject(control_type: int):
  7. """
  8. Inject control code, with this inject, we will be able to do unit test
  9. Args:
  10. control_type: event to send, TYPE_*
  11. """
  12. def wrapper(f):
  13. @functools.wraps(f)
  14. def inner(self, *args, **kwargs):
  15. package = struct.pack(">B", control_type) + f(self, *args, **kwargs)
  16. if self.control_socket is not None:
  17. with self.control_socket_lock:
  18. self.control_socket.send(package)
  19. return package
  20. return inner
  21. return wrapper
  22. class ControlSender:
  23. def __init__(self, parent):
  24. self.parent = parent
  25. @property
  26. def control_socket(self):
  27. return self.parent._scrcpy_control_socket
  28. @property
  29. def control_socket_lock(self):
  30. return self.parent._scrcpy_control_socket_lock
  31. @property
  32. def resolution(self):
  33. return self.parent._scrcpy_resolution
  34. @inject(const.TYPE_INJECT_KEYCODE)
  35. def keycode(
  36. self, keycode: int, action: int = const.ACTION_DOWN, repeat: int = 0
  37. ) -> bytes:
  38. """
  39. Send keycode to device
  40. Args:
  41. keycode: const.KEYCODE_*
  42. action: ACTION_DOWN | ACTION_UP
  43. repeat: repeat count
  44. """
  45. return struct.pack(">Biii", action, keycode, repeat, 0)
  46. @inject(const.TYPE_INJECT_TEXT)
  47. def text(self, text: str) -> bytes:
  48. """
  49. Send text to device
  50. Args:
  51. text: text to send
  52. """
  53. buffer = text.encode("utf-8")
  54. return struct.pack(">i", len(buffer)) + buffer
  55. @inject(const.TYPE_INJECT_TOUCH_EVENT)
  56. def touch(
  57. self, x: int, y: int, action: int = const.ACTION_DOWN, touch_id: int = -1
  58. ) -> bytes:
  59. """
  60. Touch screen
  61. Args:
  62. x: horizontal position
  63. y: vertical position
  64. action: ACTION_DOWN | ACTION_UP | ACTION_MOVE
  65. touch_id: Default using virtual id -1, you can specify it to emulate multi finger touch
  66. """
  67. x, y = max(x, 0), max(y, 0)
  68. return struct.pack(
  69. ">BqiiHHHi",
  70. action,
  71. touch_id,
  72. int(x),
  73. int(y),
  74. int(self.resolution[0]),
  75. int(self.resolution[1]),
  76. 0xFFFF,
  77. 1,
  78. )
  79. @inject(const.TYPE_INJECT_SCROLL_EVENT)
  80. def scroll(self, x: int, y: int, h: int, v: int) -> bytes:
  81. """
  82. Scroll screen
  83. Args:
  84. x: horizontal position
  85. y: vertical position
  86. h: horizontal movement
  87. v: vertical movement
  88. """
  89. x, y = max(x, 0), max(y, 0)
  90. return struct.pack(
  91. ">iiHHii",
  92. int(x),
  93. int(y),
  94. int(self.resolution[0]),
  95. int(self.resolution[1]),
  96. int(h),
  97. int(v),
  98. )
  99. @inject(const.TYPE_BACK_OR_SCREEN_ON)
  100. def back_or_turn_screen_on(self, action: int = const.ACTION_DOWN) -> bytes:
  101. """
  102. If the screen is off, it is turned on only on ACTION_DOWN
  103. Args:
  104. action: ACTION_DOWN | ACTION_UP
  105. """
  106. return struct.pack(">B", action)
  107. @inject(const.TYPE_EXPAND_NOTIFICATION_PANEL)
  108. def expand_notification_panel(self) -> bytes:
  109. """
  110. Expand notification panel
  111. """
  112. return b""
  113. @inject(const.TYPE_EXPAND_SETTINGS_PANEL)
  114. def expand_settings_panel(self) -> bytes:
  115. """
  116. Expand settings panel
  117. """
  118. return b""
  119. @inject(const.TYPE_COLLAPSE_PANELS)
  120. def collapse_panels(self) -> bytes:
  121. """
  122. Collapse all panels
  123. """
  124. return b""
  125. def get_clipboard(self) -> str:
  126. """
  127. Get clipboard
  128. """
  129. # Since this function need socket response, we can't auto inject it any more
  130. s: socket.socket = self.control_socket
  131. with self.control_socket_lock:
  132. # Flush socket
  133. s.setblocking(False)
  134. while True:
  135. try:
  136. s.recv(1024)
  137. except BlockingIOError:
  138. break
  139. s.setblocking(True)
  140. # Read package
  141. package = struct.pack(">B", const.TYPE_GET_CLIPBOARD)
  142. s.send(package)
  143. (code,) = struct.unpack(">B", s.recv(1))
  144. assert code == 0
  145. (length,) = struct.unpack(">i", s.recv(4))
  146. return s.recv(length).decode("utf-8")
  147. @inject(const.TYPE_SET_CLIPBOARD)
  148. def set_clipboard(self, text: str, paste: bool = False) -> bytes:
  149. """
  150. Set clipboard
  151. Args:
  152. text: the string you want to set
  153. paste: paste now
  154. """
  155. buffer = text.encode("utf-8")
  156. return struct.pack(">?i", paste, len(buffer)) + buffer
  157. @inject(const.TYPE_SET_SCREEN_POWER_MODE)
  158. def set_screen_power_mode(self, mode: int = const.POWER_MODE_NORMAL) -> bytes:
  159. """
  160. Set screen power mode
  161. Args:
  162. mode: POWER_MODE_OFF | POWER_MODE_NORMAL
  163. """
  164. return struct.pack(">b", mode)
  165. @inject(const.TYPE_ROTATE_DEVICE)
  166. def rotate_device(self) -> bytes:
  167. """
  168. Rotate device
  169. """
  170. return b""
  171. def swipe(
  172. self,
  173. start_x: int,
  174. start_y: int,
  175. end_x: int,
  176. end_y: int,
  177. move_step_length: int = 5,
  178. move_steps_delay: float = 0.005,
  179. ) -> None:
  180. """
  181. Swipe on screen
  182. Args:
  183. start_x: start horizontal position
  184. start_y: start vertical position
  185. end_x: start horizontal position
  186. end_y: end vertical position
  187. move_step_length: length per step
  188. move_steps_delay: sleep seconds after each step
  189. :return:
  190. """
  191. self.touch(start_x, start_y, const.ACTION_DOWN)
  192. next_x = start_x
  193. next_y = start_y
  194. if end_x > self.resolution[0]:
  195. end_x = self.resolution[0]
  196. if end_y > self.resolution[1]:
  197. end_y = self.resolution[1]
  198. decrease_x = True if start_x > end_x else False
  199. decrease_y = True if start_y > end_y else False
  200. while True:
  201. if decrease_x:
  202. next_x -= move_step_length
  203. if next_x < end_x:
  204. next_x = end_x
  205. else:
  206. next_x += move_step_length
  207. if next_x > end_x:
  208. next_x = end_x
  209. if decrease_y:
  210. next_y -= move_step_length
  211. if next_y < end_y:
  212. next_y = end_y
  213. else:
  214. next_y += move_step_length
  215. if next_y > end_y:
  216. next_y = end_y
  217. self.touch(next_x, next_y, const.ACTION_MOVE)
  218. if next_x == end_x and next_y == end_y:
  219. self.touch(next_x, next_y, const.ACTION_UP)
  220. break
  221. time.sleep(move_steps_delay)