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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
* xine_frontend_kbd.c: Forward (local) console key presses to VDR (server)
*
* See the main source file 'xineliboutput.c' for copyright information and
* how to reach the author.
*
* $Id: xine_frontend_kbd.c,v 1.4 2012-06-06 07:39:56 phintuka Exp $
*
*/
#include "features.h"
#include <inttypes.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#define LOG_MODULENAME "[console] "
#include "logdefs.h"
#include "xine_frontend.h"
#include "xine_frontend_kbd.h"
/*
* stdin (keyboard/slave mode) reading
*/
/* static data */
static pthread_t kbd_thread;
static struct termios tm, saved_tm;
/* xine_frontend_main.c: */
extern int gui_hotkeys;
/*
* read_key()
*
* Try to read single char from stdin.
*
* Returns: >=0 char readed
* -1 nothing to read
* -2 fatal error
*/
#define READ_KEY_ERROR -2
#define READ_KEY_EAGAIN -1
static int read_key(void)
{
unsigned char ch;
int err;
struct pollfd pfd;
pfd.fd = STDIN_FILENO;
pfd.events = POLLIN;
errno = 0;
pthread_testcancel();
if (1 == (err = poll(&pfd, 1, 50))) {
pthread_testcancel();
if (1 == (err = read(STDIN_FILENO, &ch, 1)))
return (int)ch;
if (err < 0)
LOGERR("read_key: read(stdin) failed");
else
LOGERR("read_key: read(stdin) failed: no stdin");
return READ_KEY_ERROR;
} else if (err < 0 && errno != EINTR) {
LOGERR("read_key: poll(stdin) failed");
return READ_KEY_ERROR;
}
pthread_testcancel();
return READ_KEY_EAGAIN;
}
/*
* read_key_seq()
*
* Read a key sequence from stdin.
* Key sequence is either normal key or escape sequence.
*
* Returns the key or escape sequence as uint64_t.
*
* Originally copied from vdr:
* remote.c: General Remote Control handling
* Copyright (C) 2000, 2003, 2006, 2008 Klaus Schmidinger
*/
#define READ_KEY_SEQ_ERROR 0xffffffff
static uint64_t read_key_seq(void)
{
/* from vdr, remote.c */
uint64_t k = 0;
int key1;
if ((key1 = read_key()) >= 0) {
k = key1;
if (key1 == 0x1B) {
/* Start of escape sequence */
if ((key1 = read_key()) >= 0) {
k <<= 8;
k |= key1 & 0xFF;
switch (key1) {
case 0x4F: /* 3-byte sequence */
if ((key1 = read_key()) >= 0) {
k <<= 8;
k |= key1 & 0xFF;
}
break;
case 0x5B: /* 3- or more-byte sequence */
if ((key1 = read_key()) >= 0) {
k <<= 8;
k |= key1 & 0xFF;
switch (key1) {
case 0x31 ... 0x3F: /* more-byte sequence */
case 0x5B: /* strange, may apparently occur */
do {
if ((key1 = read_key()) < 0)
break; /* Sequence ends here */
k <<= 8;
k |= key1 & 0xFF;
} while (key1 != 0x7E);
break;
default:;
}
}
break;
default:;
}
}
}
}
if (key1 == READ_KEY_ERROR)
return READ_KEY_SEQ_ERROR;
return k;
}
/*
* kbd_receiver_thread()
*
* Read key(sequence)s from stdin and pass those to frontend.
*/
static void kbd_receiver_thread_cleanup(void *arg)
{
if (isatty(STDIN_FILENO)) {
tcsetattr(STDIN_FILENO, TCSANOW, &saved_tm);
}
if (isatty(STDOUT_FILENO)) {
int status;
status = system("setterm -cursor on");
if (status < 0)
LOGMSG("system(\"setterm -cursor on\") failed\n");
}
LOGMSG("Keyboard thread terminated");
}
static void *kbd_receiver_thread(void *fe_gen)
{
frontend_t *fe = (frontend_t*)fe_gen;
uint64_t code = 0;
char str[64];
if (isatty(STDOUT_FILENO)) {
int status;
status = system("setterm -cursor off");
if (status < 0)
LOGMSG("system(\"setterm -cursor off\") failed\n");
status = system("setterm -blank off");
if (status < 0)
LOGMSG("system(\"setterm -blank off\") failed\n");
}
if (isatty(STDIN_FILENO)) {
/* Set stdin to deliver keypresses without buffering whole lines */
tcgetattr(STDIN_FILENO, &saved_tm);
if (tcgetattr(STDIN_FILENO, &tm) == 0) {
tm.c_iflag = 0;
tm.c_lflag &= ~(ICANON | ECHO);
tm.c_cc[VMIN] = 0;
tm.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &tm);
}
}
pthread_cleanup_push(kbd_receiver_thread_cleanup, NULL);
do {
alarm(0);
errno = 0;
code = read_key_seq();
alarm(3); /* watchdog */
if (code == 0)
continue;
if (code == READ_KEY_SEQ_ERROR)
break;
if (code == 27) {
fe->send_event(fe, "QUIT");
break;
}
if (gui_hotkeys) {
if (code == 'f' || code == 'F') {
fe->send_event(fe, "TOGGLE_FULLSCREEN");
continue;
}
if (code == 'p' || code == 'P') {
fe->send_event(fe, "POWER_OFF");
continue;
}
if (code == 'd' || code == 'D') {
fe->send_event(fe, "TOGGLE_DEINTERLACE");
continue;
}
}
snprintf(str, sizeof(str), "%016" PRIX64, code);
fe->send_input_event(fe, "KBD", str, 0, 0);
} while (fe->xine_is_finished(fe, 0) != FE_XINE_EXIT);
alarm(0);
LOGDBG("Keyboard thread terminating");
pthread_cleanup_pop(1);
pthread_exit(NULL);
return NULL; /* never reached */
}
/*
* slave_receiver_thread()
*
* Read slave mode commands from stdin
* Interpret and execute valid commands
*/
static void slave_receiver_thread_cleanup(void *arg)
{
/* restore terminal settings */
tcsetattr(STDIN_FILENO, TCSANOW, &saved_tm);
LOGDBG("Slave mode receiver terminated");
}
static void *slave_receiver_thread(void *fe_gen)
{
frontend_t *fe = (frontend_t*)fe_gen;
char str[128], *pt;
tcgetattr(STDIN_FILENO, &saved_tm);
pthread_cleanup_push(slave_receiver_thread_cleanup, NULL);
do {
errno = 0;
str[0] = 0;
pthread_testcancel();
if (!fgets(str, sizeof(str), stdin))
break;
pthread_testcancel();
if (NULL != (pt = strchr(str, '\r')))
*pt = 0;
if (NULL != (pt = strchr(str, '\n')))
*pt = 0;
if (!strncasecmp(str, "QUIT", 4)) {
fe->send_event(fe, "QUIT");
break;
}
if (!strncasecmp(str, "FULLSCREEN", 10)) {
if (strpbrk(str + 10, "01"))
fe->send_event(fe, str);
else
fe->send_event(fe, "TOGGLE_FULLSCREEN");
continue;
}
if (!strncasecmp(str, "DEINTERLACE ", 12)) {
fe->send_event(fe, str);
continue;
}
if (!strncasecmp(str, "HITK ", 5)) {
fe->send_input_event(fe, NULL, str+5, 0, 0);
continue;
}
LOGMSG("Unknown slave mode command: %s", str);
} while (fe->xine_is_finished(fe, 0) != FE_XINE_EXIT);
LOGDBG("Slave mode receiver terminating");
pthread_cleanup_pop(1);
pthread_exit(NULL);
return NULL; /* never reached */
}
/*
* kbd_start()
*
* Start keyboard/slave mode reader thread
*/
void kbd_start(frontend_t *fe, int slave_mode)
{
int err;
if ((err = pthread_create (&kbd_thread,
NULL,
slave_mode ? slave_receiver_thread : kbd_receiver_thread,
(void*)fe)) != 0) {
fprintf(stderr, "Can't create new thread for keyboard (%s)\n",
strerror(err));
}
}
/*
* kbd_stop()
*
* Stop keyboard/slave mode reader thread
*/
void kbd_stop(void)
{
void *p;
pthread_cancel(kbd_thread);
pthread_join(kbd_thread, &p);
}
|