forked from stb-tester/stb-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stbt_control.py
executable file
·294 lines (238 loc) · 8.75 KB
/
stbt_control.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python3
"""Send remote control signals using the PC keyboard or from the command line.
"""
import argparse
import collections
import contextlib
import curses
import curses.ascii
import io
import math
import os
import sys
import threading
import time
from textwrap import dedent
import _stbt.control
from _stbt.config import ConfigurationError, get_config
SPECIAL_CHARS = {
curses.ascii.SP: "Space",
curses.ascii.NL: "Enter",
curses.ascii.TAB: "Tab",
curses.ascii.ESC: "Escape",
curses.KEY_UP: "Up",
curses.KEY_DOWN: "Down",
curses.KEY_LEFT: "Left",
curses.KEY_RIGHT: "Right",
curses.KEY_BACKSPACE: "Backspace",
curses.KEY_PPAGE: "PageUp",
curses.KEY_NPAGE: "PageDown",
curses.KEY_HOME: "Home",
curses.KEY_END: "End",
curses.KEY_IC: "Insert",
curses.KEY_DC: "Delete",
}
def main(argv):
args = argparser().parse_args(argv[1:])
if args.help_keymap:
sys.exit(show_help_keymap())
control = _stbt.control.uri_to_control(args.control, None)
if args.remote_control_key: # Send a single key and exit
control.press(args.remote_control_key)
else: # Interactive
for key in main_loop(args.control, args.keymap):
control.press(key)
def argparser():
parser = argparse.ArgumentParser()
parser.prog = "stbt control"
parser.description = ("Send remote control signals using the PC keyboard "
"or from the command line.")
parser.add_argument(
"--help-keymap", action='store_true', default=False,
help="Show description of the keymap file format and exit.")
parser.add_argument(
"--keymap", default=default_keymap_file(),
help="Load keymap from KEYMAP file; defaults to %(default)s. "
"See `%(prog)s --help-keymap` for details.")
parser.add_argument(
"--control", default=get_config("global", "control"),
help="Equivalent to the --control parameter of `stbt run`. "
"See `man stbt` for available control types and configuration.")
parser.add_argument(
"remote_control_key", default=None, nargs='?',
help=(
"The name of a remote control key as in the control's config file "
"(that is /etc/lirc/lircd.conf in case of a LIRC control device). "
"Specifying this argument sends remote_control_key and exits. "
"Omitting this argument brings up the printed keymap."))
return parser
def show_help_keymap():
"""Keymap File
A keymap file stores the mappings between keyboard keys and remote control
keys. One line of the file stores one key mapping in the following format:
<keyboard key> <remote control key> [<display name>]
<keyboard key> is an ASCII character or one of the following keywords:
Space, Enter, Tab, Escape, Up, Down, Left, Right, Backspace,
PageUp, PageDown, Home, End, Insert, Delete
Be careful that keywords are case sensitive.
<remote control key> is the same as in the command line arguments. It
cannot contain white spaces.
<display name> is an optional alias for <remote control key> to show in the
on-screen keymap; e.g. "m MENU Main Menu" displays "Main Menu" but sends
the "MENU" remote control key when "m" is pressed on the PC keyboard. It
may consist of multiple words but cannot be longer than 15 characters.
Comments start with '//' (double slash) and last until the end of line.
Example keymap:
m MENU Main Menu
Enter OK
c CLOSE Close // Go back to live TV
The keymap file to use can be specified by:
1. Passing the filename of the keymap to the --keymap=<filename> command
line argument.
2. Or setting the configuration value `control.keymap` to the filename of
the keymap file
3. Copying it into $XDG_CONFIG_PATH/stbt/control.conf (defaults to
$HOME/.config/stbt/control.conf).
"""
print(globals()["show_help_keymap"].__doc__)
def main_loop(control_uri, keymap_file):
try:
keymap = load_keymap(open(keymap_file, "r"))
except IOError:
error("Failed to load keymap file '%s'\n"
"(see 'stbt control --help' for details of the keymap file)."
% keymap_file)
timer = None
with terminal() as term:
_, term_width = term.getmaxyx()
printed_keymap = dedent("""\
Keymap: {keymap_file}
Control: {control_uri}
{mapping}
""").format(keymap_file=keymap_file[:term_width - len("Keymap: ")],
control_uri=control_uri[:77] + (
control_uri[77:] and "..."),
mapping=keymap_string(keymap))
if keymap_fits_terminal(term, printed_keymap):
term.addstr(printed_keymap)
else:
raise EnvironmentError(
"Unable to print keymap because the terminal is too small. "
"Please resize the terminal window.")
while True: # Main loop
keycode = term.getch()
if keycode == ord('q'): # 'q' for 'Quit' is pre-defined
return
control_key, _ = keymap.get(decoded(keycode), (None, None))
if timer:
timer.cancel()
clear_last_command(term)
if control_key:
yield control_key
term.addstr(str(control_key))
timer = threading.Timer(1, clear_last_command, [term])
timer.start()
time.sleep(.2)
curses.flushinp()
def clear_last_command(term):
term.move(term.getyx()[0], 0)
term.clrtoeol()
def keymap_fits_terminal(term, printed_keymap):
term_y, term_x = term.getmaxyx()
lines = printed_keymap.split("\n")
return term_y > len(lines) and term_x > max(len(line) for line in lines)
@contextlib.contextmanager
def terminal():
term = curses.initscr()
curses.noecho()
curses.cbreak()
term.keypad(1)
term.immedok(1)
try:
yield term
finally:
term.immedok(0)
term.keypad(0)
curses.nocbreak()
curses.echo()
curses.endwin()
def keymap_string(keymap):
"""
>>> print(keymap_string({"m": ("MENU", "Main Menu")}).strip())
q - <Quit> m - Main Menu
"""
keylist = ["%15s - %-15s" % (kb_key, mapping[1])
for kb_key, mapping in keymap.items()]
keylist.insert(0, "%15s - %-15s" % ("q", "<Quit>"))
middle = int(math.ceil(float(len(keylist)) / 2))
rows = [
"%s %s" % (
keylist[i],
keylist[middle + i] if middle + i < len(keylist) else "")
for i in range(middle)]
return "\n".join(rows)
def decoded(keycode):
"""
>>> decoded(curses.KEY_BACKSPACE)
'Backspace'
>>> decoded(120)
'x'
"""
if keycode in SPECIAL_CHARS:
return SPECIAL_CHARS[keycode]
try:
return chr(keycode)
except ValueError:
return None
def load_keymap(keymap_file):
keymap = collections.OrderedDict()
for line in keymap_file:
items = line.split("//")[0].split()
if len(items) < 2:
continue
if items[0] in keymap:
raise ConfigurationError(
"Multiple remote control keys assigned to keyboard key '%s' "
"in the keymap file" % items[0])
if len(items) == 2:
keymap[items[0]] = (items[1],) * 2
else:
keymap[items[0]] = (items[1], " ".join(items[2:]))
validate(items[0])
return keymap
def test_load_keymap():
keymap = load_keymap(io.StringIO(
"Backspace BACK Go back to previous\n" # Test display name
"Enter OK //Move forward\n")) # Test comments
assert keymap["Backspace"] == ("BACK", "Go back to previous")
assert keymap["Enter"] == ("OK", "OK")
def test_load_keymap_with_duplicated_key():
try:
load_keymap(io.StringIO("o OK\no OPEN\n"))
assert False, "load_keymap should have thrown ConfigurationError"
except ConfigurationError:
pass
def validate(keyname):
if keyname in SPECIAL_CHARS.values():
return
try:
ord(keyname)
except TypeError:
raise ValueError("Invalid keyboard key in the keymap file: " + keyname)
def test_validate():
try:
validate("Invalid")
assert False
except ValueError:
pass
def default_keymap_file():
config_dir = os.environ.get(
'XDG_CONFIG_HOME', '%s/.config' % os.environ['HOME'])
return get_config("control", "keymap", "") or \
os.path.join(config_dir, "stbt", "control.conf")
def error(s):
sys.stderr.write("%s: error: %s\n" % (
os.path.basename(sys.argv[0]), str(s)))
sys.exit(1)
if __name__ == "__main__":
main(sys.argv)