-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiduck.py
executable file
·187 lines (171 loc) · 4.91 KB
/
piduck.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
#!/usr/bin/env python3
import argparse
import readline
from importlib import import_module
from time import sleep
import sys
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def string(string):
for char in string:
pharse_p2(char, [[], []], True)
sleep(string_delay / 100)
def pharse_p1(line):
global default_delay
global string_delay
global last_line
if line == "":
return
elif line == " ":
command = [" "]
else:
command = line.split()
if command[0] == "DELAY":
sleep(int(command[1]) / 100)
return
elif command[0] == "REM":
return
elif command[0] == "REPEAT":
try:
for i in range(int(command[1])):
pharse_p1(last_line)
except RecursionError:
eprint("RecursionError!")
exit(4)
return
elif command[0] == "DEFAULTCHARDELAY":
string_delay = int(command[1])
return
elif command[0] == "DEFAULTDELAY":
default_delay = int(command[1])
return
elif command[0] == "STRING":
sleep(default_delay / 100) # DEFAULT_DELAY
string(line[len(command[0] + " ") :])
last_line = line
return
else:
pharse_p2(line, [[], []], False)
last_line = line
return
def pharse_p2(line, known, deltrue):
if line == "":
return
elif line == " ":
command = [" "]
else:
command = line.split()
if command[0] in keymap.commap:
known[0].append(keymap.commap[command[0]])
if len(command) > 1:
pharse_p2(" ".join(command[1:]), known, True)
else:
out(known)
return
elif command[0] in keymap.c1map:
known[1].append(keymap.c1map[command[0]])
if len(command) > 1:
pharse_p2(" ".join(command[1:]), known, True)
else:
out(known)
return
elif command[0] in keymap.c2map:
pharse_p2(keymap.c2map[command[0]] + " " + " ".join(command[1:]), known, True)
return
elif command[0] in aliasmap:
pharse_p2(aliasmap[command[0]] + " " + " ".join(command[1:]), known, True)
return
elif command[0] == "CHAR":
if command[1].isdigit():
known[1].append(int(command[1]))
if len(command) > 2:
pharse_p2(" ".join(command[2:]), known, True)
else:
out(known)
return
else:
eprint('Could not find "' + command[0] + '"')
exit(2)
def out(ccl):
rep = ""
if len(ccl[0]) > 0:
for e in ccl[0]:
rep = rep + chr(e)
else:
rep = rep + chr(0)
rep = rep + chr(0)
for e in ccl[1]:
rep = rep + chr(e)
rep = rep + chr(0) * (8 - len(rep))
with open("/dev/hidg0", "rb+") as fd:
fd.write(rep.encode())
fd.write((chr(0) * 8).encode())
def main():
if piargs.input is not None:
file1 = open(piargs.input, "r")
while True:
line = file1.readline()
if not line:
break
parse_p1(line)
file1.close()
else:
while True:
try:
line = input("d# ")
except EOFError:
print("EOF")
break
if not line:
break
pharse_p1(line)
if __name__ == "__main__":
last_line = ""
key_layout = "us"
default_delay = 10
string_delay = 1
aliasmap = {
"CTRL": "LCTRL",
"SHIFT": "LSHIFT",
"ALT": "LALT",
"META": "LMETA",
"CONTROL": "CTRL",
"GUI": "META",
"ESCAPE": "ESC",
"RIGHTARROW": "RIGHT",
"LEFTARROW": "LEFT",
"DOWNARROW": "DOWN",
"UPARROW": "UP",
"CTRL-ALT": "CTRL ALT",
"CTRL-SHIFT": "CTRL SHIFT",
"DEFAULT_DELAY": "DEFAULTDELAY",
" ": "SPACE",
"BREAK": "PAUSE",
}
piparser = argparse.ArgumentParser()
piparser.add_argument("-i", "--input", help="File input")
piparser.add_argument(
"-l", "--keyboardlayoutcode", help="Language codes specified by ISO639-1:2002"
)
piparser.add_argument("-d", "--defaultdelay", help="The default delay of execution")
piparser.add_argument(
"-s", "--defaultchardelay", help="The default char delay of execution"
)
piargs = piparser.parse_args()
if piargs.keyboardlayoutcode is not None:
key_layout = piargs.keyboardlayoutcode
if piargs.defaultdelay is not None:
default_delay = piargs.defaultdelay
if piargs.defaultchardelay is not None:
string_delay = piargs.defaultchardelay
try:
keymap = import_module("pd_key_maps.keymap_" + key_layout)
except ModuleNotFoundError:
eprint('Keymap "' + key_layout + '" could not be found')
exit(3)
try:
main()
except KeyboardInterrupt:
print("EOF")
pass
exit(0)