-
Notifications
You must be signed in to change notification settings - Fork 0
/
umacro.py
206 lines (162 loc) · 5.57 KB
/
umacro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import subprocess
import pyperclip
import keyboard
import evdev
import sys
import yaml
import signal
from shutil import which
from evdev import InputDevice, categorize, ecodes
# Check if sudo or not
# Import umacro_conf
# Open YAML config file
with open("/etc/umacro/umacro_conf.yml", 'r') as f:
config = yaml.safe_load(f)
# debug key check
debug_key_check = False
# Event key pressed value
key_pressed = None
# Sub command of
config_command_to_execute = "NULL"
# clip board array of size 10.
# can store 10 clips
copy_array = [0] * 10
# value of last clip to stop overriding
xsel_old_value = "NULL"
# Class for text colors
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[34m'
OKGREEN = '\033[32m'
OKYELLOW = '\033[33m'
WARNING = '\033[93m'
FAIL = '\033[31m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# ---------------------
# INITIAL PART
# ---------------------
# input device list
def list_input_devices_and_select():
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
list_item = 0
devices.reverse()
for device in devices:
print(bcolors.OKBLUE, list_item, ") ", bcolors.ENDC, bcolors.OKYELLOW, device.path, device.name, device.phys,
bcolors.ENDC)
list_item += 1
user_selected_input_device_number = int(input("Enter device number to use as macro:"))
if user_selected_input_device_number > list_item or user_selected_input_device_number < 0:
print(bcolors.FAIL, "Please select an appropriate number", bcolors.ENDC)
exit()
return devices[user_selected_input_device_number].path
# ---------------------
# Umacro FEATURES
# ---------------------
# function to copy
def copy_to_clipboard():
global key_pressed
global xsel_old_value
number_pressed = int(key_pressed[-1])
xsel_current_value = os.popen('xsel').read()
if xsel_current_value != xsel_old_value:
xsel_old_value = xsel_current_value
copy_array[number_pressed] = xsel_current_value
pyperclip.copy(copy_array[number_pressed])
# print("Number Pressed: ", number_pressed)
# print("xsel_current_value: ", xsel_current_value)
# print("Current value to copy: ", copy_array[number_pressed])
# print(*copy_array, sep = ", ")
# print("-------------------------------------")
# function to paste
# ctrl + v
def paste():
keyboard.press_and_release('ctrl+v')
# terminal paste
def term_paste():
keyboard.press_and_release('ctrl+shift+v')
# Execute command
def execute(cmd):
if which("xdotool"):
subprocess.call(["xdotool", "type", cmd])
else:
keyboard.write(cmd, delay=0, restore_state_after=True, exact=None)
keyboard.press_and_release('enter')
# function to execute shell script
def execute_shell(cmd):
subprocess.Popen(cmd, shell=True, executable='/bin/bash')
# Type
def typeto(cmd):
if which("xdotool"):
subprocess.call(["xdotool", "type", cmd])
else:
keyboard.write(cmd, delay=0, restore_state_after=True, exact=None)
# Send keystroke
def keystroke(cmd):
keyboard.press_and_release(cmd)
# ---------------------
# MAIN FUNCTION
# ---------------------
# Init input device
def main():
# dev = InputDevice(list_input_devices_and_select())
# dev.grab()
global dev
global key_pressed
# Loop to catch key strokes
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
key = categorize(event)
if key.keystate == key.key_down:
if debug_key_check:
print(key.keycode)
key_pressed = key.keycode
if key.keycode in config:
key_pressed_config_value = config[key.keycode]
config_command = key_pressed_config_value.split(' ', 1)[0]
function_name_call = config_command + '()'
if config_command == 'execute_shell':
config_command_to_execute = key_pressed_config_value.split(' ', 1)[1]
function_name_call = config_command + '(' + config_command_to_execute + ')'
if config_command == 'execute':
config_command_to_execute = key_pressed_config_value.split(' ', 1)[1]
function_name_call = config_command + '(' + config_command_to_execute + ')'
if config_command == 'typeto':
config_command_to_execute = key_pressed_config_value.split(' ', 1)[1]
function_name_call = config_command + '(' + config_command_to_execute + ')'
if config_command == 'keystroke':
config_command_to_execute = key_pressed_config_value.split(' ', 1)[1]
function_name_call = config_command + '(' + config_command_to_execute + ')'
eval(function_name_call)
# Process related
pidfile = "/tmp/umacro.pid"
if len(sys.argv) > 1:
if sys.argv[1] == "terminate":
f = open(pidfile, "r").read()
print("Terminating Process", f)
os.kill(int(f), signal.SIGTERM)
os.remove(pidfile)
sys.exit()
if sys.argv[1] == "check-key":
dev = InputDevice(list_input_devices_and_select())
dev.grab()
debug_key_check = True
main()
else:
pass
if os.path.isfile(pidfile):
print("Other instance of Umacro is still running.")
sys.exit()
try:
dev = InputDevice(list_input_devices_and_select())
dev.grab()
n = os.fork()
if n:
pid = str(n)
open(pidfile, 'w').write(pid)
sys.exit()
main()
finally:
pass