Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. import os, subprocess, shlex
  2.  
  3. cmd_debug_events = 'libinput debug-events'
  4. cmd_list_devices = 'libinput list-devices'
  5. cmd_dev_kernel = ''
  6.  
  7. def run(cmd, check=True):
  8. 'Run subprocess function and return standard output or None'
  9. # Maintain subprocess compatibility with python 3.4 so use
  10. # check_output() rather than run().
  11. try:
  12. stdout = subprocess.check_output(cmd, universal_newlines=True,
  13. stderr=(None if check else subprocess.DEVNULL))
  14. except Exception as e:
  15. stdout = None
  16. if check:
  17. print(str(e), file=sys.stderr)
  18.  
  19. return stdout
  20.  
  21. def get_devices_list():
  22. 'Get list of devices and their attributes (as a dict) from libinput'
  23. stdout = run(cmd_list_devices.split())
  24.  
  25. if stdout:
  26. dev = {}
  27. for line in stdout.splitlines():
  28. line = line.strip()
  29. if line and ':' in line:
  30. key, value = line.split(':', maxsplit=1)
  31. dev[key.strip().lower()] = value.strip()
  32. elif dev:
  33. yield dev
  34. dev = {}
  35.  
  36. for dev in get_devices_list():
  37. if dev['device'].find('Touchpad') >= 0:
  38. cmd_dev_kernel = '--device {}'.format(dev['kernel'])
  39. break
  40.  
  41.  
  42. X_SENS = 60
  43. X_INTERVAL = 60
  44. Y_SENS = 50
  45. Y_INTERVAL = 40
  46.  
  47. class Gesture:
  48. def __init__(self):
  49. self.alt_down = False
  50. self.clear()
  51.  
  52. def begin(self, num):
  53. self.clear()
  54. self.num_of_point = num
  55.  
  56. def clear(self):
  57. self.num_of_point = 0
  58. self.direct = 0
  59. self.clear_point()
  60.  
  61. if self.alt_down:
  62. os.system('xdotool keyup alt')
  63. self.alt_down = False
  64.  
  65.  
  66. def clear_point(self):
  67. self.move_x_axis = self.move_y_axis = 0
  68.  
  69. def add_point(self, x, y):
  70. self.move_x_axis += x
  71. self.move_y_axis += y
  72. self.classification()
  73.  
  74. def classification(self):
  75. direct = 0
  76. #check continous
  77. if self.move_y_axis < -Y_INTERVAL:
  78. direct = 1
  79. elif self.move_y_axis > Y_INTERVAL:
  80. direct = 2
  81. if self.move_x_axis < -X_INTERVAL:
  82. direct = 3
  83. elif self.move_x_axis > X_INTERVAL:
  84. direct = 4
  85.  
  86. if direct and direct == self.direct:
  87. self.c_move(direct)
  88. return
  89.  
  90. direct = 0
  91. #check first movement
  92. if self.move_y_axis < -Y_SENS:
  93. direct = 1
  94. elif self.move_y_axis > Y_SENS:
  95. direct = 2
  96. if self.move_x_axis < -X_SENS:
  97. direct = 3
  98. elif self.move_x_axis > X_SENS:
  99. direct = 4
  100.  
  101. if direct:
  102. self.s_move(direct)
  103. return
  104.  
  105. def c_move(self, direct):
  106. self.clear_point()
  107. self.direct = direct
  108. print('c', self.direct)
  109.  
  110. if self.num_of_point == 4:
  111. if direct == 3:
  112. os.system('xdotool key Shift+Tab')
  113. elif direct == 4:
  114. os.system('xdotool key Tab')
  115.  
  116. def s_move(self, direct):
  117. self.clear_point()
  118. prev_direct = self.direct
  119. self.direct = direct
  120.  
  121. print('s', self.direct)
  122. if self.num_of_point == 3:
  123. if direct == 3:
  124. os.system('xdotool key alt+Right')
  125. self.clear()
  126. elif direct == 4:
  127. os.system('xdotool key alt+Left')
  128. self.clear()
  129. elif self.num_of_point == 4:
  130. if direct == 1: #up
  131. self.clear()
  132. os.system('xdotool key ctrl+alt+Up')
  133. elif direct == 2:
  134. self.clear()
  135. os.system('xdotool key ctrl+alt+Down')
  136. elif direct == 3:
  137. os.system('xdotool keydown alt key Shift+Tab')
  138. self.alt_down = True
  139. elif direct == 4:
  140. os.system('xdotool keydown alt key Tab')
  141. self.alt_down = True
  142.  
  143. if cmd_dev_kernel:
  144. command = 'stdbuf -oL -- {} {}'.format(cmd_debug_events, cmd_dev_kernel)
  145. cmd = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE,
  146. bufsize=1, universal_newlines=True)
  147.  
  148. gesture = Gesture()
  149.  
  150. while True:
  151. for line in cmd.stdout:
  152. try:
  153.  
  154. line = line.strip().split()
  155. if line[1] == 'GESTURE_SWIPE_END':
  156. gesture.clear()
  157.  
  158. elif line[1] == 'GESTURE_SWIPE_BEGIN':
  159. gesture.begin(int(line[3]))
  160.  
  161. elif line[1] == 'GESTURE_SWIPE_UPDATE':
  162. acc_x, acc_y = ''.join(line[4:]).split('(')[0].split('/')[:2]
  163. acc_x, acc_y = float(acc_x), float(acc_y)
  164.  
  165. gesture.add_point(acc_x, acc_y)
  166. except Exception as e:
  167. print(e)
  168. gesture.clear()
  169. # os.system('xdotool keyup alt')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement