blob: 739b3a3db9a94e4db2d23574594a8381d8d0a5e5 [file] [log] [blame]
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07001/*
2 * Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
David Sodmanbf3f2842014-11-12 08:26:58 -08007#include <ctype.h>
David Sodman8ef20062015-01-06 09:23:40 -08008#include <errno.h>
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07009#include <fcntl.h>
David Sodman8ef20062015-01-06 09:23:40 -080010#include <libudev.h>
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070011#include <stdint.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
Dominik Behr93899452014-08-18 22:16:21 -070015#include <sys/select.h>
David Sodman8ef20062015-01-06 09:23:40 -080016#include <unistd.h>
17
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070018#include "input.h"
David Sodman8ef20062015-01-06 09:23:40 -080019
David Sodmanbbcb0522014-09-19 10:34:07 -070020#include "dbus_interface.h"
21#include "dbus.h"
David Sodmanbf3f2842014-11-12 08:26:58 -080022#include "keysym.h"
David Sodmanbbcb0522014-09-19 10:34:07 -070023#include "util.h"
David Sodman8ef20062015-01-06 09:23:40 -080024
David Sodmanf0a925a2015-05-04 11:19:19 -070025#define MAX_STD_TERMINALS (3)
26#define NUM_SPLASH_TERMINAL (1)
27#define MAX_TERMINALS (MAX_STD_TERMINALS + NUM_SPLASH_TERMINAL)
28#define SPLASH_TERMINAL (MAX_TERMINALS - 1)
David Sodmanbf3f2842014-11-12 08:26:58 -080029
Dominik Behr93899452014-08-18 22:16:21 -070030struct input_dev {
31 int fd;
32 char *path;
33};
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070034
David Sodmanbf3f2842014-11-12 08:26:58 -080035struct keyboard_state {
36 int shift_state;
37 int control_state;
38 int alt_state;
David Sodmane0cc8312014-11-18 11:16:36 -080039 int search_state;
David Sodmanbf3f2842014-11-12 08:26:58 -080040};
41
Dylan Reid342aed02015-06-18 12:15:52 -070042/*
43 * structure to keep input state:
44 * udev - for udev events.
45 * udev_monitor - used to listen for udev events.
46 * udev_fd - to poll for udev messages.
47 * ndevs - number of input devices.
48 * devs - input devices to listen to.
49 * kbd_state - tracks modifier keys that are pressed.
50 * dbus - where to send dbus events.
51 * current_terminal - the currently selected terminal.
52 * terminals - list of all terminals that have been created.
53 */
Dominik Behr93899452014-08-18 22:16:21 -070054struct {
55 struct udev *udev;
56 struct udev_monitor *udev_monitor;
57 int udev_fd;
58 unsigned int ndevs;
59 struct input_dev *devs;
David Sodmanbf3f2842014-11-12 08:26:58 -080060 struct keyboard_state kbd_state;
David Sodmanbbcb0522014-09-19 10:34:07 -070061 dbus_t *dbus;
David Sodmanbf3f2842014-11-12 08:26:58 -080062 uint32_t current_terminal;
63 terminal_t *terminals[MAX_TERMINALS];
Dominik Behr93899452014-08-18 22:16:21 -070064} input = {
65 .udev = NULL,
66 .udev_monitor = NULL,
67 .udev_fd = -1,
68 .ndevs = 0,
69 .devs = NULL,
David Sodmanbf3f2842014-11-12 08:26:58 -080070 .dbus = NULL,
71 .current_terminal = 0
Dominik Behr93899452014-08-18 22:16:21 -070072};
73
David Sodmane0cc8312014-11-18 11:16:36 -080074static void report_user_activity(int activity_type)
75{
David Sodman19e4f9d2015-03-10 11:11:09 -070076 dbus_bool_t allow_off = false;
David Sodman8ef20062015-01-06 09:23:40 -080077 if (!input.dbus)
78 return;
79
David Sodmane0cc8312014-11-18 11:16:36 -080080 dbus_method_call1(input.dbus, kPowerManagerServiceName,
81 kPowerManagerServicePath,
82 kPowerManagerInterface,
83 kHandleUserActivityMethod,
David Sodman19e4f9d2015-03-10 11:11:09 -070084 DBUS_TYPE_INT32, &activity_type);
David Sodmane0cc8312014-11-18 11:16:36 -080085
86 switch (activity_type) {
87 case USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS:
88 (void)dbus_method_call0(input.dbus,
89 kPowerManagerServiceName,
90 kPowerManagerServicePath,
91 kPowerManagerInterface,
92 kIncreaseScreenBrightnessMethod);
93 break;
94 case USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS:
David Sodman19e4f9d2015-03-10 11:11:09 -070095 /*
96 * Shouldn't allow the screen to go
97 * completely off while frecon is active
98 * so passing false to allow_off
99 */
100 (void)dbus_method_call1(input.dbus,
David Sodmane0cc8312014-11-18 11:16:36 -0800101 kPowerManagerServiceName,
102 kPowerManagerServicePath,
103 kPowerManagerInterface,
David Sodman19e4f9d2015-03-10 11:11:09 -0700104 kDecreaseScreenBrightnessMethod,
105 DBUS_TYPE_BOOLEAN, &allow_off);
David Sodmane0cc8312014-11-18 11:16:36 -0800106 break;
107 }
108}
109
David Sodmanbf3f2842014-11-12 08:26:58 -0800110static int input_special_key(struct input_key_event *ev)
111{
112 unsigned int i;
David Sodmanb0697c22014-12-12 10:29:25 -0800113 terminal_t *terminal;
David Sodmanbf3f2842014-11-12 08:26:58 -0800114
115 uint32_t ignore_keys[] = {
116 BTN_TOUCH, // touchpad events
117 BTN_TOOL_FINGER,
118 BTN_TOOL_DOUBLETAP,
119 BTN_TOOL_TRIPLETAP,
120 BTN_TOOL_QUADTAP,
121 BTN_TOOL_QUINTTAP,
122 BTN_LEFT, // mouse buttons
123 BTN_RIGHT,
124 BTN_MIDDLE,
125 BTN_SIDE,
126 BTN_EXTRA,
127 BTN_FORWARD,
128 BTN_BACK,
129 BTN_TASK
130 };
131
David Sodmanafba0d92015-01-27 19:07:46 -0800132 terminal = input.terminals[input.current_terminal];
133
David Sodmanbf3f2842014-11-12 08:26:58 -0800134 for (i = 0; i < ARRAY_SIZE(ignore_keys); i++)
135 if (ev->code == ignore_keys[i])
136 return 1;
137
138 switch (ev->code) {
139 case KEY_LEFTSHIFT:
140 case KEY_RIGHTSHIFT:
141 input.kbd_state.shift_state = ! !ev->value;
142 return 1;
143 case KEY_LEFTCTRL:
144 case KEY_RIGHTCTRL:
145 input.kbd_state.control_state = ! !ev->value;
146 return 1;
147 case KEY_LEFTALT:
148 case KEY_RIGHTALT:
149 input.kbd_state.alt_state = ! !ev->value;
150 return 1;
David Sodmane0cc8312014-11-18 11:16:36 -0800151 case KEY_LEFTMETA: // search key
152 input.kbd_state.search_state = ! !ev->value;
153 return 1;
David Sodmanbf3f2842014-11-12 08:26:58 -0800154 }
155
David Sodmanafba0d92015-01-27 19:07:46 -0800156 if (term_is_active(terminal)) {
157 if (input.kbd_state.shift_state && ev->value) {
158 switch (ev->code) {
159 case KEY_PAGEUP:
David Sodmane0cc8312014-11-18 11:16:36 -0800160 term_page_up(input.terminals[input.current_terminal]);
161 return 1;
David Sodmanafba0d92015-01-27 19:07:46 -0800162 case KEY_PAGEDOWN:
David Sodmane0cc8312014-11-18 11:16:36 -0800163 term_page_down(input.terminals[input.current_terminal]);
164 return 1;
David Sodmanafba0d92015-01-27 19:07:46 -0800165 case KEY_UP:
166 term_line_up(input.terminals[input.current_terminal]);
167 return 1;
168 case KEY_DOWN:
169 term_line_down(input.terminals[input.current_terminal]);
170 return 1;
171 }
172 }
173
174 if (input.kbd_state.search_state && ev->value) {
175 switch (ev->code) {
176 case KEY_UP:
177 term_page_up(input.terminals[input.current_terminal]);
178 return 1;
179 case KEY_DOWN:
180 term_page_down(input.terminals[input.current_terminal]);
181 return 1;
182 }
183 }
184
185 if (!(input.kbd_state.search_state || input.kbd_state.alt_state ||
186 input.kbd_state.control_state) &&
187 ev->value && (ev->code >= KEY_F1) && (ev->code <= KEY_F10)) {
188 switch (ev->code) {
189 case KEY_F1:
190 case KEY_F2:
191 case KEY_F3:
192 case KEY_F4:
193 case KEY_F5:
194 break;
195 case KEY_F6:
196 case KEY_F7:
197 report_user_activity(USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS -
198 (ev->code - KEY_F6));
199 break;
200 case KEY_F8:
201 case KEY_F9:
202 case KEY_F10:
203 break;
204 }
205 return 1;
David Sodmane0cc8312014-11-18 11:16:36 -0800206 }
207 }
208
David Sodmanbf3f2842014-11-12 08:26:58 -0800209 if (input.kbd_state.alt_state && input.kbd_state.control_state && ev->value) {
David Sodman1d1c67f2015-03-12 11:01:08 -0700210 /*
211 * Special case for key sequence that is used by external program. Just
212 * explicitly ignore here and do nothing.
213 */
214 if (input.kbd_state.shift_state)
215 return 1;
216
David Sodmanb0697c22014-12-12 10:29:25 -0800217 if (ev->code == KEY_F1) {
David Sodmanb0697c22014-12-12 10:29:25 -0800218 if (term_is_active(terminal)) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700219 term_deactivate(terminal);
220 if (input.terminals[SPLASH_TERMINAL] != NULL) {
221 term_activate(input.terminals[SPLASH_TERMINAL]);
222 } else {
223 if (input.dbus != NULL)
224 (void)dbus_method_call0(input.dbus,
225 kLibCrosServiceName,
226 kLibCrosServicePath,
227 kLibCrosServiceInterface,
228 kTakeDisplayOwnership);
229 }
David Sodman8ef20062015-01-06 09:23:40 -0800230 }
David Sodmanf0a925a2015-05-04 11:19:19 -0700231 } else if ((ev->code >= KEY_F2) && (ev->code < KEY_F2 + MAX_STD_TERMINALS)) {
David Sodman8ef20062015-01-06 09:23:40 -0800232 if (input.dbus != NULL)
David Sodmanbf3f2842014-11-12 08:26:58 -0800233 (void)dbus_method_call0(input.dbus,
234 kLibCrosServiceName,
235 kLibCrosServicePath,
236 kLibCrosServiceInterface,
David Sodman8ef20062015-01-06 09:23:40 -0800237 kReleaseDisplayOwnership);
David Sodmanbf3f2842014-11-12 08:26:58 -0800238 if (term_is_active(terminal))
David Sodmanf0a925a2015-05-04 11:19:19 -0700239 term_deactivate(terminal);
David Sodmanbf3f2842014-11-12 08:26:58 -0800240 input.current_terminal = ev->code - KEY_F2;
241 terminal = input.terminals[input.current_terminal];
242 if (terminal == NULL) {
243 input.terminals[input.current_terminal] =
David Sodmanf0a925a2015-05-04 11:19:19 -0700244 term_init(true, NULL);
David Sodmanbf3f2842014-11-12 08:26:58 -0800245 terminal =
246 input.terminals[input.current_terminal];
David Sodman8ef20062015-01-06 09:23:40 -0800247 term_activate(terminal);
David Sodmanbf3f2842014-11-12 08:26:58 -0800248 if (!term_is_valid(terminal)) {
249 LOG(ERROR, "Term init failed");
250 return 1;
251 }
252 }
David Sodmanf0a925a2015-05-04 11:19:19 -0700253 term_activate(input.terminals[input.current_terminal]);
David Sodmanbf3f2842014-11-12 08:26:58 -0800254 }
255
256 return 1;
257
258 }
259
260 return 0;
261}
262
263static void input_get_keysym_and_unicode(struct input_key_event *event,
264 uint32_t *keysym, uint32_t *unicode)
265{
266 struct {
267 uint32_t code;
268 uint32_t keysym;
269 } non_ascii_keys[] = {
270 { KEY_ESC, KEYSYM_ESC},
271 { KEY_HOME, KEYSYM_HOME},
272 { KEY_LEFT, KEYSYM_LEFT},
273 { KEY_UP, KEYSYM_UP},
274 { KEY_RIGHT, KEYSYM_RIGHT},
275 { KEY_DOWN, KEYSYM_DOWN},
276 { KEY_PAGEUP, KEYSYM_PAGEUP},
277 { KEY_PAGEDOWN, KEYSYM_PAGEDOWN},
278 { KEY_END, KEYSYM_END},
279 { KEY_INSERT, KEYSYM_INSERT},
280 { KEY_DELETE, KEYSYM_DELETE},
281 };
282
283 for (unsigned i = 0; i < ARRAY_SIZE(non_ascii_keys); i++) {
284 if (non_ascii_keys[i].code == event->code) {
285 *keysym = non_ascii_keys[i].keysym;
286 *unicode = -1;
287 return;
288 }
289 }
290
291 if (event->code >= ARRAY_SIZE(keysym_table) / 2) {
292 *keysym = '?';
293 } else {
294 *keysym = keysym_table[event->code * 2 + input.kbd_state.shift_state];
295 if ((input.kbd_state.control_state) && isascii(*keysym))
296 *keysym = tolower(*keysym) - 'a' + 1;
297 }
298
299 *unicode = *keysym;
300}
301
Dominik Behr93899452014-08-18 22:16:21 -0700302static int input_add(const char *devname)
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700303{
Dominik Behr93899452014-08-18 22:16:21 -0700304 int ret = 0, fd = -1;
305 /* for some reason every device has a null enumerations and notifications
306 of every device come with NULL string first */
307 if (!devname) {
308 ret = -EINVAL;
309 goto errorret;
310 }
311 ret = fd = open(devname, O_RDONLY);
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700312 if (fd < 0)
Dominik Behr93899452014-08-18 22:16:21 -0700313 goto errorret;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700314
David Sodmaneef3fd22015-08-20 15:08:52 -0700315 ret = ioctl(fd, EVIOCGRAB, (void *) 1);
316 if (!ret) {
317 ret = ioctl(fd, EVIOCGRAB, (void *) 0);
318 if (ret)
319 LOG(ERROR,
320 "EVIOCGRAB succeeded but the corresponding ungrab failed: %m");
321 } else {
322 LOG(ERROR, "Evdev device %s grabbed by another process",
323 devname);
324 ret = -EBUSY;
325 goto closefd;
326 }
327
Dominik Behr93899452014-08-18 22:16:21 -0700328 struct input_dev *newdevs =
329 realloc(input.devs, (input.ndevs + 1) * sizeof (struct input_dev));
330 if (!newdevs) {
331 ret = -ENOMEM;
332 goto closefd;
333 }
334 input.devs = newdevs;
335 input.devs[input.ndevs].fd = fd;
336 input.devs[input.ndevs].path = strdup(devname);
337 if (!input.devs[input.ndevs].path) {
338 ret = -ENOMEM;
339 goto closefd;
340 }
341 input.ndevs++;
342
343 return fd;
344
345closefd:
346 close(fd);
347errorret:
348 return ret;
349}
350
351static void input_remove(const char *devname)
352{
353 if (!devname) {
354 return;
355 }
356 unsigned int u;
357 for (u = 0; u < input.ndevs; u++) {
358 if (!strcmp(devname, input.devs[u].path)) {
359 free(input.devs[u].path);
360 close(input.devs[u].fd);
361 input.ndevs--;
362 if (u != input.ndevs) {
363 input.devs[u] = input.devs[input.ndevs];
364 }
365 return;
366 }
367 }
368}
369
Dominik Behr93899452014-08-18 22:16:21 -0700370
371int input_init()
372{
373 input.udev = udev_new();
374 if (!input.udev)
375 return -ENOENT;
376 input.udev_monitor = udev_monitor_new_from_netlink(input.udev, "udev");
377 if (!input.udev_monitor) {
378 udev_unref(input.udev);
379 return -ENOENT;
380 }
381 udev_monitor_filter_add_match_subsystem_devtype(input.udev_monitor, "input",
382 NULL);
383 udev_monitor_enable_receiving(input.udev_monitor);
384 input.udev_fd = udev_monitor_get_fd(input.udev_monitor);
385
386 struct udev_enumerate *udev_enum;
387 struct udev_list_entry *devices, *deventry;
388 udev_enum = udev_enumerate_new(input.udev);
389 udev_enumerate_add_match_subsystem(udev_enum, "input");
390 udev_enumerate_scan_devices(udev_enum);
391 devices = udev_enumerate_get_list_entry(udev_enum);
392 udev_list_entry_foreach(deventry, devices) {
393 const char *syspath;
394 struct udev_device *dev;
395 syspath = udev_list_entry_get_name(deventry);
396 dev = udev_device_new_from_syspath(input.udev, syspath);
397 input_add(udev_device_get_devnode(dev));
398 udev_device_unref(dev);
399 }
400 udev_enumerate_unref(udev_enum);
401
402 if (!isatty(fileno(stdout)))
403 setbuf(stdout, NULL);
404
David Sodmanbbcb0522014-09-19 10:34:07 -0700405 if (input.ndevs == 0) {
406 LOG(ERROR, "No valid inputs for terminal");
407 exit(EXIT_SUCCESS);
408 }
409
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700410 return 0;
411}
412
413void input_close()
414{
Dominik Behr93899452014-08-18 22:16:21 -0700415 unsigned int u;
416 for (u = 0; u < input.ndevs; u++) {
417 free(input.devs[u].path);
418 close(input.devs[u].fd);
419 }
420 free(input.devs);
421 input.devs = NULL;
422 input.ndevs = 0;
423
424 udev_monitor_unref(input.udev_monitor);
425 input.udev_monitor = NULL;
426 udev_unref(input.udev);
427 input.udev = NULL;
428 input.udev_fd = -1;
429
David Sodmanbbcb0522014-09-19 10:34:07 -0700430 dbus_destroy(input.dbus);
431
432}
433
434void input_set_dbus(dbus_t* dbus)
435{
436 input.dbus = dbus;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700437}
438
Dominik Behr93899452014-08-18 22:16:21 -0700439int input_setfds(fd_set * read_set, fd_set * exception_set)
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700440{
Dominik Behr93899452014-08-18 22:16:21 -0700441 unsigned int u;
442 int max = -1;
443 for (u = 0; u < input.ndevs; u++) {
444 FD_SET(input.devs[u].fd, read_set);
445 FD_SET(input.devs[u].fd, exception_set);
446 if (input.devs[u].fd > max)
447 max = input.devs[u].fd;
448 }
449
450 FD_SET(input.udev_fd, read_set);
451 FD_SET(input.udev_fd, exception_set);
452 if (input.udev_fd > max)
453 max = input.udev_fd;
454 return max;
455}
456
Dominik Behr93899452014-08-18 22:16:21 -0700457struct input_key_event *input_get_event(fd_set * read_set,
458 fd_set * exception_set)
459{
460 unsigned int u;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700461 struct input_event ev;
462 int ret;
463
Dominik Behr93899452014-08-18 22:16:21 -0700464 if (FD_ISSET(input.udev_fd, exception_set)) {
465 /* udev died on us? */
David Sodmanbbcb0522014-09-19 10:34:07 -0700466 LOG(ERROR, "Exception on udev fd");
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700467 }
468
Dominik Behr93899452014-08-18 22:16:21 -0700469 if (FD_ISSET(input.udev_fd, read_set)
470 && !FD_ISSET(input.udev_fd, exception_set)) {
471 /* we got an udev notification */
472 struct udev_device *dev =
473 udev_monitor_receive_device(input.udev_monitor);
474 if (dev) {
475 if (!strcmp("add", udev_device_get_action(dev))) {
476 input_add(udev_device_get_devnode(dev));
477 } else
478 if (!strcmp("remove", udev_device_get_action(dev)))
479 {
480 input_remove(udev_device_get_devnode(dev));
481 }
482 udev_device_unref(dev);
483 }
484 }
485
486 for (u = 0; u < input.ndevs; u++) {
487 if (FD_ISSET(input.devs[u].fd, read_set)
488 && !FD_ISSET(input.devs[u].fd, exception_set)) {
489 ret =
490 read(input.devs[u].fd, &ev, sizeof (struct input_event));
Michael Spang24d20122015-04-22 13:58:16 -0400491 if (ret < 0) {
492 if (errno == EINTR || errno == EAGAIN)
493 continue;
494 if (errno != ENODEV) {
495 LOG(ERROR, "read: %s: %s", input.devs[u].path,
496 strerror(errno));
497 }
498 input_remove(input.devs[u].path);
499 return NULL;
500 } else if (ret < (int) sizeof (struct input_event)) {
David Sodmanbbcb0522014-09-19 10:34:07 -0700501 LOG(ERROR, "expected %d bytes, got %d",
Dominik Behr93899452014-08-18 22:16:21 -0700502 (int) sizeof (struct input_event), ret);
503 return NULL;
504 }
505
506 if (ev.type == EV_KEY) {
507 struct input_key_event *event =
508 malloc(sizeof (*event));
509 event->code = ev.code;
510 event->value = ev.value;
Dominik Behr93899452014-08-18 22:16:21 -0700511 return event;
512 }
513 }
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700514 }
515
516 return NULL;
517}
518
David Sodmanf0a925a2015-05-04 11:19:19 -0700519int input_process(terminal_t* splash_term, uint32_t usec)
520{
521 terminal_t *terminal;
522 terminal_t *new_terminal;
523 fd_set read_set, exception_set;
524 int maxfd;
525 int sstat;
526 struct timeval tm;
527 struct timeval *ptm;
528
529 terminal = input.terminals[input.current_terminal];
530
531 FD_ZERO(&read_set);
532 FD_ZERO(&exception_set);
533
534 if (input.dbus) {
535 dbus_add_fd(input.dbus, &read_set, &exception_set);
536 maxfd = dbus_get_fd(input.dbus) + 1;
537 } else {
538 maxfd = 0;
539 }
540
541 maxfd = MAX(maxfd, input_setfds(&read_set, &exception_set)) + 1;
542
543 for (int i = 0; i < MAX_TERMINALS; i++) {
544 if (term_is_valid(input.terminals[i])) {
545 term_add_fd(input.terminals[i], &read_set, &exception_set);
546 maxfd = MAX(maxfd, term_fd(input.terminals[i])) + 1;
547 term_dispatch_io(input.terminals[i], &read_set);
548 }
549 }
550
551 if (usec) {
552 ptm = &tm;
553 tm.tv_sec = 0;
554 tm.tv_usec = usec;
555 } else
556 ptm = NULL;
557
558 sstat = select(maxfd, &read_set, NULL, &exception_set, ptm);
559 if (sstat == 0)
560 return 0;
561
562
563 if (input.dbus)
564 dbus_dispatch_io(input.dbus);
565
566 if (term_exception(terminal, &exception_set))
567 return -1;
568
569 struct input_key_event *event;
570 event = input_get_event(&read_set, &exception_set);
571 if (event) {
572 if (!input_special_key(event) && event->value) {
573 uint32_t keysym, unicode;
574 // current_terminal can possibly change during
575 // execution of input_special_key
576 terminal = input.terminals[input.current_terminal];
577 if (term_is_active(terminal)) {
578 // Only report user activity when the terminal is active
579 report_user_activity(USER_ACTIVITY_OTHER);
580 input_get_keysym_and_unicode(
581 event, &keysym, &unicode);
582 term_key_event(terminal,
583 keysym, unicode);
584 }
585 }
586 input_put_event(event);
587 }
588
589 for (int i = 0; i < MAX_TERMINALS; i++) {
590 if (term_is_valid(input.terminals[i])) {
591 term_add_fd(input.terminals[i], &read_set, &exception_set);
592 term_dispatch_io(input.terminals[i], &read_set);
593 }
594 }
595
596 if (term_is_valid(terminal)) {
597 if (term_is_child_done(terminal)) {
598 if (terminal == input.terminals[SPLASH_TERMINAL]) {
599 /*
600 * Note: reference is not lost because it is still referenced
601 * by the splash_t structure which will ultimately destroy
602 * it, once it's safe to do so
603 */
604 input.terminals[SPLASH_TERMINAL] = NULL;
605 return -1;
606 }
David Sodman30a94ef2015-07-26 17:37:12 -0700607 input.terminals[input.current_terminal] =
608 term_init(true, term_getvideo(terminal));
David Sodmanf0a925a2015-05-04 11:19:19 -0700609 new_terminal = input.terminals[input.current_terminal];
610 if (!term_is_valid(new_terminal)) {
611 return -1;
612 }
613 term_activate(new_terminal);
614 term_close(terminal);
615 }
616 }
617
618 return 0;
619}
620
David Sodmanbf3f2842014-11-12 08:26:58 -0800621int input_run(bool standalone)
622{
David Sodmanbf3f2842014-11-12 08:26:58 -0800623 terminal_t* terminal;
David Sodmanf0a925a2015-05-04 11:19:19 -0700624 int status;
David Sodmanbf3f2842014-11-12 08:26:58 -0800625
626 if (standalone) {
David Sodman8ef20062015-01-06 09:23:40 -0800627 if (input.dbus) {
628 (void)dbus_method_call0(input.dbus,
629 kLibCrosServiceName,
630 kLibCrosServicePath,
631 kLibCrosServiceInterface,
632 kReleaseDisplayOwnership);
633 }
David Sodmanbf3f2842014-11-12 08:26:58 -0800634
David Sodmanf0a925a2015-05-04 11:19:19 -0700635 input.terminals[input.current_terminal] = term_init(true, NULL);
David Sodmanbf3f2842014-11-12 08:26:58 -0800636 terminal = input.terminals[input.current_terminal];
David Sodman8ef20062015-01-06 09:23:40 -0800637 term_activate(terminal);
David Sodmanbf3f2842014-11-12 08:26:58 -0800638 }
639
640 while (1) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700641 status = input_process(NULL, 0);
642 if (status != 0) {
643 LOG(ERROR, "input process returned %d", status);
644 break;
David Sodmanbf3f2842014-11-12 08:26:58 -0800645 }
646 }
647
648 return 0;
649}
650
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700651void input_put_event(struct input_key_event *event)
652{
653 free(event);
654}
David Sodmanbbcb0522014-09-19 10:34:07 -0700655
David Sodman8ef20062015-01-06 09:23:40 -0800656terminal_t* input_create_term(int vt)
657{
658 terminal_t* terminal;
659
660 if (vt == 0)
661 return input.terminals[input.current_terminal];
662
663 terminal = input.terminals[vt-1];
664 if (term_is_active(terminal))
665 return terminal;
666
667 if (terminal == NULL) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700668 input.terminals[vt-1] = term_init(false, NULL);
David Sodman8ef20062015-01-06 09:23:40 -0800669 terminal = input.terminals[vt-1];
670 if (!term_is_valid(terminal)) {
671 LOG(ERROR, "create_term: Term init failed");
672 }
673 }
674
675 return terminal;
676}
677
David Sodmanf0a925a2015-05-04 11:19:19 -0700678terminal_t* input_create_splash_term(video_t* video)
679{
680 input.terminals[SPLASH_TERMINAL] = term_init(false, video);
681 return input.terminals[SPLASH_TERMINAL];
682}
683
684void input_destroy_splash_term()
685{
686 input.terminals[SPLASH_TERMINAL] = NULL;
687}
688
David Sodman8ef20062015-01-06 09:23:40 -0800689void input_set_current(terminal_t* terminal)
690{
691 int i;
692
David Sodmanf0a925a2015-05-04 11:19:19 -0700693 if (!terminal) {
694 input.terminals[input.current_terminal] = NULL;
695 input.current_terminal = 0;
David Sodman8ef20062015-01-06 09:23:40 -0800696 return;
David Sodmanf0a925a2015-05-04 11:19:19 -0700697 }
David Sodman8ef20062015-01-06 09:23:40 -0800698
699 for (i = 0; i < MAX_TERMINALS; i++) {
700 if (terminal == input.terminals[i]) {
701 input.current_terminal = i;
702 return;
703 }
704 }
705}
706
707unsigned int input_get_maxterminals()
708{
David Sodmanf0a925a2015-05-04 11:19:19 -0700709 return MAX_STD_TERMINALS;
David Sodman8ef20062015-01-06 09:23:40 -0800710}