Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import pyudev
- import subprocess
- from time import sleep
- import re
- import os
- def message(title, message):
- os.system('notify-send "' + title + '" "'+message + '"')
- def get_name_of_devices():
- get_names = subprocess.Popen(
- ['xsetwacom', '--list'],
- stdout=subprocess.PIPE
- )
- k = str(get_names.communicate()[0])[2:-3]
- k = re.sub(r'[\s]+', ' ', k) # убрали лишние пробелы
- k = re.sub(r'[\s]+$', '', k) # пробел на конце
- k = re.sub(r'[\s]*\\tid: \d*', '', k) # убрали инфу об id
- k = re.split(r'[\s]*\\n', k) # и разделили на 2 строки
- for i in range(len(k)):
- k[i] = re.split(r'[\s]*\\t', k[i])
- k[i][1] = re.sub(r'type: ', '', k[i][1]) # type - STYLUS / PAD
- # на выходе получаем что-то вроде:
- # [['GAOMON Gaomon Tablet Pen stylus', 'STYLUS'],
- # ['GAOMON Gaomon Tablet Pad pad', 'PAD']]
- if k[0][1] == 'STYLUS': # название стилуса возвращает первым
- return k[0][0], k[1][0]
- else:
- return k[1][0], k[0][0]
- def binding(start_command, binds):
- errors = ''
- for keys in binds:
- # stdin=subprocess.PIPE,
- # stdout=subprocess.PIPE,
- status = subprocess.Popen(start_command + keys,
- stderr=subprocess.PIPE)
- status.wait()
- errors += status.stderr.read().decode()
- return errors
- #########################################################################
- stylus_binds = [['3', 'key Ctrl z']]
- pad_binds = [ ['1', 'key P'],
- ['2', 'key Shift e'],
- ['3', 'key Ctrl s'],
- ['8', 'key Ctrl y']
- ]
- context = pyudev.Context()
- monitor = pyudev.Monitor.from_netlink(context)
- monitor.filter_by(subsystem='input')
- for action, device in monitor:
- if action == 'add' and 'js0' in str(device):
- print('Был подключен Gaomon S620 graphic tablet!')
- message('Gaomon S620', 'Был подключен')
- for try_bind in range(0, 3):
- sleep(5)
- stylus_name, pad_name = get_name_of_devices()
- for_errors = binding(['xsetwacom', '--set', stylus_name, 'Button'],
- stylus_binds)
- for_errors += binding(['xsetwacom', '--set', pad_name, 'Button'],
- pad_binds)
- if for_errors:
- message('Gaomon S620', 'Обнаружены ошибки!\n' + for_errors)
- else:
- message('Gaomon S620', 'Успешный бинд!')
- break
Advertisement
Add Comment
Please, Sign In to add comment