blob: bd22f64945fee117fd37723862a954cda7d750cb [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
Dominik Behr93899452014-08-18 22:16:21 -0700315 struct input_dev *newdevs =
316 realloc(input.devs, (input.ndevs + 1) * sizeof (struct input_dev));
317 if (!newdevs) {
318 ret = -ENOMEM;
319 goto closefd;
320 }
321 input.devs = newdevs;
322 input.devs[input.ndevs].fd = fd;
323 input.devs[input.ndevs].path = strdup(devname);
324 if (!input.devs[input.ndevs].path) {
325 ret = -ENOMEM;
326 goto closefd;
327 }
328 input.ndevs++;
329
330 return fd;
331
332closefd:
333 close(fd);
334errorret:
335 return ret;
336}
337
338static void input_remove(const char *devname)
339{
340 if (!devname) {
341 return;
342 }
343 unsigned int u;
344 for (u = 0; u < input.ndevs; u++) {
345 if (!strcmp(devname, input.devs[u].path)) {
346 free(input.devs[u].path);
347 close(input.devs[u].fd);
348 input.ndevs--;
349 if (u != input.ndevs) {
350 input.devs[u] = input.devs[input.ndevs];
351 }
352 return;
353 }
354 }
355}
356
Dominik Behr93899452014-08-18 22:16:21 -0700357
358int input_init()
359{
360 input.udev = udev_new();
361 if (!input.udev)
362 return -ENOENT;
363 input.udev_monitor = udev_monitor_new_from_netlink(input.udev, "udev");
364 if (!input.udev_monitor) {
365 udev_unref(input.udev);
366 return -ENOENT;
367 }
368 udev_monitor_filter_add_match_subsystem_devtype(input.udev_monitor, "input",
369 NULL);
370 udev_monitor_enable_receiving(input.udev_monitor);
371 input.udev_fd = udev_monitor_get_fd(input.udev_monitor);
372
373 struct udev_enumerate *udev_enum;
374 struct udev_list_entry *devices, *deventry;
375 udev_enum = udev_enumerate_new(input.udev);
376 udev_enumerate_add_match_subsystem(udev_enum, "input");
377 udev_enumerate_scan_devices(udev_enum);
378 devices = udev_enumerate_get_list_entry(udev_enum);
379 udev_list_entry_foreach(deventry, devices) {
380 const char *syspath;
381 struct udev_device *dev;
382 syspath = udev_list_entry_get_name(deventry);
383 dev = udev_device_new_from_syspath(input.udev, syspath);
384 input_add(udev_device_get_devnode(dev));
385 udev_device_unref(dev);
386 }
387 udev_enumerate_unref(udev_enum);
388
389 if (!isatty(fileno(stdout)))
390 setbuf(stdout, NULL);
391
David Sodmanbbcb0522014-09-19 10:34:07 -0700392 if (input.ndevs == 0) {
393 LOG(ERROR, "No valid inputs for terminal");
394 exit(EXIT_SUCCESS);
395 }
396
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700397 return 0;
398}
399
400void input_close()
401{
Dominik Behr93899452014-08-18 22:16:21 -0700402 unsigned int u;
403 for (u = 0; u < input.ndevs; u++) {
404 free(input.devs[u].path);
405 close(input.devs[u].fd);
406 }
407 free(input.devs);
408 input.devs = NULL;
409 input.ndevs = 0;
410
411 udev_monitor_unref(input.udev_monitor);
412 input.udev_monitor = NULL;
413 udev_unref(input.udev);
414 input.udev = NULL;
415 input.udev_fd = -1;
416
David Sodmanbbcb0522014-09-19 10:34:07 -0700417 dbus_destroy(input.dbus);
418
419}
420
421void input_set_dbus(dbus_t* dbus)
422{
423 input.dbus = dbus;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700424}
425
Dominik Behr93899452014-08-18 22:16:21 -0700426int input_setfds(fd_set * read_set, fd_set * exception_set)
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700427{
Dominik Behr93899452014-08-18 22:16:21 -0700428 unsigned int u;
429 int max = -1;
430 for (u = 0; u < input.ndevs; u++) {
431 FD_SET(input.devs[u].fd, read_set);
432 FD_SET(input.devs[u].fd, exception_set);
433 if (input.devs[u].fd > max)
434 max = input.devs[u].fd;
435 }
436
437 FD_SET(input.udev_fd, read_set);
438 FD_SET(input.udev_fd, exception_set);
439 if (input.udev_fd > max)
440 max = input.udev_fd;
441 return max;
442}
443
Dominik Behr93899452014-08-18 22:16:21 -0700444struct input_key_event *input_get_event(fd_set * read_set,
445 fd_set * exception_set)
446{
447 unsigned int u;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700448 struct input_event ev;
449 int ret;
450
Dominik Behr93899452014-08-18 22:16:21 -0700451 if (FD_ISSET(input.udev_fd, exception_set)) {
452 /* udev died on us? */
David Sodmanbbcb0522014-09-19 10:34:07 -0700453 LOG(ERROR, "Exception on udev fd");
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700454 }
455
Dominik Behr93899452014-08-18 22:16:21 -0700456 if (FD_ISSET(input.udev_fd, read_set)
457 && !FD_ISSET(input.udev_fd, exception_set)) {
458 /* we got an udev notification */
459 struct udev_device *dev =
460 udev_monitor_receive_device(input.udev_monitor);
461 if (dev) {
462 if (!strcmp("add", udev_device_get_action(dev))) {
463 input_add(udev_device_get_devnode(dev));
464 } else
465 if (!strcmp("remove", udev_device_get_action(dev)))
466 {
467 input_remove(udev_device_get_devnode(dev));
468 }
469 udev_device_unref(dev);
470 }
471 }
472
473 for (u = 0; u < input.ndevs; u++) {
474 if (FD_ISSET(input.devs[u].fd, read_set)
475 && !FD_ISSET(input.devs[u].fd, exception_set)) {
476 ret =
477 read(input.devs[u].fd, &ev, sizeof (struct input_event));
Michael Spang24d20122015-04-22 13:58:16 -0400478 if (ret < 0) {
479 if (errno == EINTR || errno == EAGAIN)
480 continue;
481 if (errno != ENODEV) {
482 LOG(ERROR, "read: %s: %s", input.devs[u].path,
483 strerror(errno));
484 }
485 input_remove(input.devs[u].path);
486 return NULL;
487 } else if (ret < (int) sizeof (struct input_event)) {
David Sodmanbbcb0522014-09-19 10:34:07 -0700488 LOG(ERROR, "expected %d bytes, got %d",
Dominik Behr93899452014-08-18 22:16:21 -0700489 (int) sizeof (struct input_event), ret);
490 return NULL;
491 }
492
493 if (ev.type == EV_KEY) {
494 struct input_key_event *event =
495 malloc(sizeof (*event));
496 event->code = ev.code;
497 event->value = ev.value;
Dominik Behr93899452014-08-18 22:16:21 -0700498 return event;
499 }
500 }
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700501 }
502
503 return NULL;
504}
505
David Sodmanf0a925a2015-05-04 11:19:19 -0700506int input_process(terminal_t* splash_term, uint32_t usec)
507{
508 terminal_t *terminal;
509 terminal_t *new_terminal;
510 fd_set read_set, exception_set;
511 int maxfd;
512 int sstat;
513 struct timeval tm;
514 struct timeval *ptm;
515
516 terminal = input.terminals[input.current_terminal];
517
518 FD_ZERO(&read_set);
519 FD_ZERO(&exception_set);
520
521 if (input.dbus) {
522 dbus_add_fd(input.dbus, &read_set, &exception_set);
523 maxfd = dbus_get_fd(input.dbus) + 1;
524 } else {
525 maxfd = 0;
526 }
527
528 maxfd = MAX(maxfd, input_setfds(&read_set, &exception_set)) + 1;
529
530 for (int i = 0; i < MAX_TERMINALS; i++) {
531 if (term_is_valid(input.terminals[i])) {
532 term_add_fd(input.terminals[i], &read_set, &exception_set);
533 maxfd = MAX(maxfd, term_fd(input.terminals[i])) + 1;
534 term_dispatch_io(input.terminals[i], &read_set);
535 }
536 }
537
538 if (usec) {
539 ptm = &tm;
540 tm.tv_sec = 0;
541 tm.tv_usec = usec;
542 } else
543 ptm = NULL;
544
545 sstat = select(maxfd, &read_set, NULL, &exception_set, ptm);
546 if (sstat == 0)
547 return 0;
548
549
550 if (input.dbus)
551 dbus_dispatch_io(input.dbus);
552
553 if (term_exception(terminal, &exception_set))
554 return -1;
555
556 struct input_key_event *event;
557 event = input_get_event(&read_set, &exception_set);
558 if (event) {
559 if (!input_special_key(event) && event->value) {
560 uint32_t keysym, unicode;
561 // current_terminal can possibly change during
562 // execution of input_special_key
563 terminal = input.terminals[input.current_terminal];
564 if (term_is_active(terminal)) {
565 // Only report user activity when the terminal is active
566 report_user_activity(USER_ACTIVITY_OTHER);
567 input_get_keysym_and_unicode(
568 event, &keysym, &unicode);
569 term_key_event(terminal,
570 keysym, unicode);
571 }
572 }
573 input_put_event(event);
574 }
575
576 for (int i = 0; i < MAX_TERMINALS; i++) {
577 if (term_is_valid(input.terminals[i])) {
578 term_add_fd(input.terminals[i], &read_set, &exception_set);
579 term_dispatch_io(input.terminals[i], &read_set);
580 }
581 }
582
583 if (term_is_valid(terminal)) {
584 if (term_is_child_done(terminal)) {
585 if (terminal == input.terminals[SPLASH_TERMINAL]) {
586 /*
587 * Note: reference is not lost because it is still referenced
588 * by the splash_t structure which will ultimately destroy
589 * it, once it's safe to do so
590 */
591 input.terminals[SPLASH_TERMINAL] = NULL;
592 return -1;
593 }
594 input.terminals[input.current_terminal] = term_init(true, NULL);
595 new_terminal = input.terminals[input.current_terminal];
596 if (!term_is_valid(new_terminal)) {
597 return -1;
598 }
599 term_activate(new_terminal);
600 term_close(terminal);
601 }
602 }
603
604 return 0;
605}
606
David Sodmanbf3f2842014-11-12 08:26:58 -0800607int input_run(bool standalone)
608{
David Sodmanbf3f2842014-11-12 08:26:58 -0800609 terminal_t* terminal;
David Sodmanf0a925a2015-05-04 11:19:19 -0700610 int status;
David Sodmanbf3f2842014-11-12 08:26:58 -0800611
612 if (standalone) {
David Sodman8ef20062015-01-06 09:23:40 -0800613 if (input.dbus) {
614 (void)dbus_method_call0(input.dbus,
615 kLibCrosServiceName,
616 kLibCrosServicePath,
617 kLibCrosServiceInterface,
618 kReleaseDisplayOwnership);
619 }
David Sodmanbf3f2842014-11-12 08:26:58 -0800620
David Sodmanf0a925a2015-05-04 11:19:19 -0700621 input.terminals[input.current_terminal] = term_init(true, NULL);
David Sodmanbf3f2842014-11-12 08:26:58 -0800622 terminal = input.terminals[input.current_terminal];
David Sodman8ef20062015-01-06 09:23:40 -0800623 term_activate(terminal);
David Sodmanbf3f2842014-11-12 08:26:58 -0800624 }
625
626 while (1) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700627 status = input_process(NULL, 0);
628 if (status != 0) {
629 LOG(ERROR, "input process returned %d", status);
630 break;
David Sodmanbf3f2842014-11-12 08:26:58 -0800631 }
632 }
633
634 return 0;
635}
636
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700637void input_put_event(struct input_key_event *event)
638{
639 free(event);
640}
David Sodmanbbcb0522014-09-19 10:34:07 -0700641
David Sodman8ef20062015-01-06 09:23:40 -0800642terminal_t* input_create_term(int vt)
643{
644 terminal_t* terminal;
645
646 if (vt == 0)
647 return input.terminals[input.current_terminal];
648
649 terminal = input.terminals[vt-1];
650 if (term_is_active(terminal))
651 return terminal;
652
653 if (terminal == NULL) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700654 input.terminals[vt-1] = term_init(false, NULL);
David Sodman8ef20062015-01-06 09:23:40 -0800655 terminal = input.terminals[vt-1];
656 if (!term_is_valid(terminal)) {
657 LOG(ERROR, "create_term: Term init failed");
658 }
659 }
660
661 return terminal;
662}
663
David Sodmanf0a925a2015-05-04 11:19:19 -0700664terminal_t* input_create_splash_term(video_t* video)
665{
666 input.terminals[SPLASH_TERMINAL] = term_init(false, video);
667 return input.terminals[SPLASH_TERMINAL];
668}
669
670void input_destroy_splash_term()
671{
672 input.terminals[SPLASH_TERMINAL] = NULL;
673}
674
David Sodman8ef20062015-01-06 09:23:40 -0800675void input_set_current(terminal_t* terminal)
676{
677 int i;
678
David Sodmanf0a925a2015-05-04 11:19:19 -0700679 if (!terminal) {
680 input.terminals[input.current_terminal] = NULL;
681 input.current_terminal = 0;
David Sodman8ef20062015-01-06 09:23:40 -0800682 return;
David Sodmanf0a925a2015-05-04 11:19:19 -0700683 }
David Sodman8ef20062015-01-06 09:23:40 -0800684
685 for (i = 0; i < MAX_TERMINALS; i++) {
686 if (terminal == input.terminals[i]) {
687 input.current_terminal = i;
688 return;
689 }
690 }
691}
692
693unsigned int input_get_maxterminals()
694{
David Sodmanf0a925a2015-05-04 11:19:19 -0700695 return MAX_STD_TERMINALS;
David Sodman8ef20062015-01-06 09:23:40 -0800696}