Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, subprocess, shlex
- cmd_debug_events = 'libinput debug-events'
- cmd_list_devices = 'libinput list-devices'
- cmd_dev_kernel = ''
- def run(cmd, check=True):
- 'Run subprocess function and return standard output or None'
- # Maintain subprocess compatibility with python 3.4 so use
- # check_output() rather than run().
- try:
- stdout = subprocess.check_output(cmd, universal_newlines=True,
- stderr=(None if check else subprocess.DEVNULL))
- except Exception as e:
- stdout = None
- if check:
- print(str(e), file=sys.stderr)
- return stdout
- def get_devices_list():
- 'Get list of devices and their attributes (as a dict) from libinput'
- stdout = run(cmd_list_devices.split())
- if stdout:
- dev = {}
- for line in stdout.splitlines():
- line = line.strip()
- if line and ':' in line:
- key, value = line.split(':', maxsplit=1)
- dev[key.strip().lower()] = value.strip()
- elif dev:
- yield dev
- dev = {}
- for dev in get_devices_list():
- if dev['device'].find('Touchpad') >= 0:
- cmd_dev_kernel = '--device {}'.format(dev['kernel'])
- break
- X_SENS = 60
- X_INTERVAL = 60
- Y_SENS = 50
- Y_INTERVAL = 40
- class Gesture:
- def __init__(self):
- self.alt_down = False
- self.clear()
- def begin(self, num):
- self.clear()
- self.num_of_point = num
- def clear(self):
- self.num_of_point = 0
- self.direct = 0
- self.clear_point()
- if self.alt_down:
- os.system('xdotool keyup alt')
- self.alt_down = False
- def clear_point(self):
- self.move_x_axis = self.move_y_axis = 0
- def add_point(self, x, y):
- self.move_x_axis += x
- self.move_y_axis += y
- self.classification()
- def classification(self):
- direct = 0
- #check continous
- if self.move_y_axis < -Y_INTERVAL:
- direct = 1
- elif self.move_y_axis > Y_INTERVAL:
- direct = 2
- if self.move_x_axis < -X_INTERVAL:
- direct = 3
- elif self.move_x_axis > X_INTERVAL:
- direct = 4
- if direct and direct == self.direct:
- self.c_move(direct)
- return
- direct = 0
- #check first movement
- if self.move_y_axis < -Y_SENS:
- direct = 1
- elif self.move_y_axis > Y_SENS:
- direct = 2
- if self.move_x_axis < -X_SENS:
- direct = 3
- elif self.move_x_axis > X_SENS:
- direct = 4
- if direct:
- self.s_move(direct)
- return
- def c_move(self, direct):
- self.clear_point()
- self.direct = direct
- print('c', self.direct)
- if self.num_of_point == 4:
- if direct == 3:
- os.system('xdotool key Shift+Tab')
- elif direct == 4:
- os.system('xdotool key Tab')
- def s_move(self, direct):
- self.clear_point()
- prev_direct = self.direct
- self.direct = direct
- print('s', self.direct)
- if self.num_of_point == 3:
- if direct == 3:
- os.system('xdotool key alt+Right')
- self.clear()
- elif direct == 4:
- os.system('xdotool key alt+Left')
- self.clear()
- elif self.num_of_point == 4:
- if direct == 1: #up
- self.clear()
- os.system('xdotool key ctrl+alt+Up')
- elif direct == 2:
- self.clear()
- os.system('xdotool key ctrl+alt+Down')
- elif direct == 3:
- os.system('xdotool keydown alt key Shift+Tab')
- self.alt_down = True
- elif direct == 4:
- os.system('xdotool keydown alt key Tab')
- self.alt_down = True
- if cmd_dev_kernel:
- command = 'stdbuf -oL -- {} {}'.format(cmd_debug_events, cmd_dev_kernel)
- cmd = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE,
- bufsize=1, universal_newlines=True)
- gesture = Gesture()
- while True:
- for line in cmd.stdout:
- try:
- line = line.strip().split()
- if line[1] == 'GESTURE_SWIPE_END':
- gesture.clear()
- elif line[1] == 'GESTURE_SWIPE_BEGIN':
- gesture.begin(int(line[3]))
- elif line[1] == 'GESTURE_SWIPE_UPDATE':
- acc_x, acc_y = ''.join(line[4:]).split('(')[0].split('/')[:2]
- acc_x, acc_y = float(acc_x), float(acc_y)
- gesture.add_point(acc_x, acc_y)
- except Exception as e:
- print(e)
- gesture.clear()
- # os.system('xdotool keyup alt')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement