blob: 62612c59893bb86cf755cfdbfa546e04c3e714e9 [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
Dominik Behr93899452014-08-18 22:16:21 -070042struct {
43 struct udev *udev;
44 struct udev_monitor *udev_monitor;
45 int udev_fd;
46 unsigned int ndevs;
47 struct input_dev *devs;
David Sodmanbf3f2842014-11-12 08:26:58 -080048 struct keyboard_state kbd_state;
David Sodmanbbcb0522014-09-19 10:34:07 -070049 dbus_t *dbus;
David Sodmanbf3f2842014-11-12 08:26:58 -080050 uint32_t current_terminal;
51 terminal_t *terminals[MAX_TERMINALS];
Dominik Behr93899452014-08-18 22:16:21 -070052} input = {
53 .udev = NULL,
54 .udev_monitor = NULL,
55 .udev_fd = -1,
56 .ndevs = 0,
57 .devs = NULL,
David Sodmanbf3f2842014-11-12 08:26:58 -080058 .dbus = NULL,
59 .current_terminal = 0
Dominik Behr93899452014-08-18 22:16:21 -070060};
61
David Sodmane0cc8312014-11-18 11:16:36 -080062static void report_user_activity(int activity_type)
63{
David Sodman19e4f9d2015-03-10 11:11:09 -070064 dbus_bool_t allow_off = false;
David Sodman8ef20062015-01-06 09:23:40 -080065 if (!input.dbus)
66 return;
67
David Sodmane0cc8312014-11-18 11:16:36 -080068 dbus_method_call1(input.dbus, kPowerManagerServiceName,
69 kPowerManagerServicePath,
70 kPowerManagerInterface,
71 kHandleUserActivityMethod,
David Sodman19e4f9d2015-03-10 11:11:09 -070072 DBUS_TYPE_INT32, &activity_type);
David Sodmane0cc8312014-11-18 11:16:36 -080073
74 switch (activity_type) {
75 case USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS:
76 (void)dbus_method_call0(input.dbus,
77 kPowerManagerServiceName,
78 kPowerManagerServicePath,
79 kPowerManagerInterface,
80 kIncreaseScreenBrightnessMethod);
81 break;
82 case USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS:
David Sodman19e4f9d2015-03-10 11:11:09 -070083 /*
84 * Shouldn't allow the screen to go
85 * completely off while frecon is active
86 * so passing false to allow_off
87 */
88 (void)dbus_method_call1(input.dbus,
David Sodmane0cc8312014-11-18 11:16:36 -080089 kPowerManagerServiceName,
90 kPowerManagerServicePath,
91 kPowerManagerInterface,
David Sodman19e4f9d2015-03-10 11:11:09 -070092 kDecreaseScreenBrightnessMethod,
93 DBUS_TYPE_BOOLEAN, &allow_off);
David Sodmane0cc8312014-11-18 11:16:36 -080094 break;
95 }
96}
97
David Sodmanbf3f2842014-11-12 08:26:58 -080098static int input_special_key(struct input_key_event *ev)
99{
100 unsigned int i;
David Sodmanb0697c22014-12-12 10:29:25 -0800101 terminal_t *terminal;
David Sodmanbf3f2842014-11-12 08:26:58 -0800102
103 uint32_t ignore_keys[] = {
104 BTN_TOUCH, // touchpad events
105 BTN_TOOL_FINGER,
106 BTN_TOOL_DOUBLETAP,
107 BTN_TOOL_TRIPLETAP,
108 BTN_TOOL_QUADTAP,
109 BTN_TOOL_QUINTTAP,
110 BTN_LEFT, // mouse buttons
111 BTN_RIGHT,
112 BTN_MIDDLE,
113 BTN_SIDE,
114 BTN_EXTRA,
115 BTN_FORWARD,
116 BTN_BACK,
117 BTN_TASK
118 };
119
David Sodmanafba0d92015-01-27 19:07:46 -0800120 terminal = input.terminals[input.current_terminal];
121
David Sodmanbf3f2842014-11-12 08:26:58 -0800122 for (i = 0; i < ARRAY_SIZE(ignore_keys); i++)
123 if (ev->code == ignore_keys[i])
124 return 1;
125
126 switch (ev->code) {
127 case KEY_LEFTSHIFT:
128 case KEY_RIGHTSHIFT:
129 input.kbd_state.shift_state = ! !ev->value;
130 return 1;
131 case KEY_LEFTCTRL:
132 case KEY_RIGHTCTRL:
133 input.kbd_state.control_state = ! !ev->value;
134 return 1;
135 case KEY_LEFTALT:
136 case KEY_RIGHTALT:
137 input.kbd_state.alt_state = ! !ev->value;
138 return 1;
David Sodmane0cc8312014-11-18 11:16:36 -0800139 case KEY_LEFTMETA: // search key
140 input.kbd_state.search_state = ! !ev->value;
141 return 1;
David Sodmanbf3f2842014-11-12 08:26:58 -0800142 }
143
David Sodmanafba0d92015-01-27 19:07:46 -0800144 if (term_is_active(terminal)) {
145 if (input.kbd_state.shift_state && ev->value) {
146 switch (ev->code) {
147 case KEY_PAGEUP:
David Sodmane0cc8312014-11-18 11:16:36 -0800148 term_page_up(input.terminals[input.current_terminal]);
149 return 1;
David Sodmanafba0d92015-01-27 19:07:46 -0800150 case KEY_PAGEDOWN:
David Sodmane0cc8312014-11-18 11:16:36 -0800151 term_page_down(input.terminals[input.current_terminal]);
152 return 1;
David Sodmanafba0d92015-01-27 19:07:46 -0800153 case KEY_UP:
154 term_line_up(input.terminals[input.current_terminal]);
155 return 1;
156 case KEY_DOWN:
157 term_line_down(input.terminals[input.current_terminal]);
158 return 1;
159 }
160 }
161
162 if (input.kbd_state.search_state && ev->value) {
163 switch (ev->code) {
164 case KEY_UP:
165 term_page_up(input.terminals[input.current_terminal]);
166 return 1;
167 case KEY_DOWN:
168 term_page_down(input.terminals[input.current_terminal]);
169 return 1;
170 }
171 }
172
173 if (!(input.kbd_state.search_state || input.kbd_state.alt_state ||
174 input.kbd_state.control_state) &&
175 ev->value && (ev->code >= KEY_F1) && (ev->code <= KEY_F10)) {
176 switch (ev->code) {
177 case KEY_F1:
178 case KEY_F2:
179 case KEY_F3:
180 case KEY_F4:
181 case KEY_F5:
182 break;
183 case KEY_F6:
184 case KEY_F7:
185 report_user_activity(USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS -
186 (ev->code - KEY_F6));
187 break;
188 case KEY_F8:
189 case KEY_F9:
190 case KEY_F10:
191 break;
192 }
193 return 1;
David Sodmane0cc8312014-11-18 11:16:36 -0800194 }
195 }
196
David Sodmanbf3f2842014-11-12 08:26:58 -0800197 if (input.kbd_state.alt_state && input.kbd_state.control_state && ev->value) {
David Sodman1d1c67f2015-03-12 11:01:08 -0700198 /*
199 * Special case for key sequence that is used by external program. Just
200 * explicitly ignore here and do nothing.
201 */
202 if (input.kbd_state.shift_state)
203 return 1;
204
David Sodmanb0697c22014-12-12 10:29:25 -0800205 if (ev->code == KEY_F1) {
David Sodmanb0697c22014-12-12 10:29:25 -0800206 if (term_is_active(terminal)) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700207 term_deactivate(terminal);
208 if (input.terminals[SPLASH_TERMINAL] != NULL) {
209 term_activate(input.terminals[SPLASH_TERMINAL]);
210 } else {
211 if (input.dbus != NULL)
212 (void)dbus_method_call0(input.dbus,
213 kLibCrosServiceName,
214 kLibCrosServicePath,
215 kLibCrosServiceInterface,
216 kTakeDisplayOwnership);
217 }
David Sodman8ef20062015-01-06 09:23:40 -0800218 }
David Sodmanf0a925a2015-05-04 11:19:19 -0700219 } else if ((ev->code >= KEY_F2) && (ev->code < KEY_F2 + MAX_STD_TERMINALS)) {
David Sodman8ef20062015-01-06 09:23:40 -0800220 if (input.dbus != NULL)
David Sodmanbf3f2842014-11-12 08:26:58 -0800221 (void)dbus_method_call0(input.dbus,
222 kLibCrosServiceName,
223 kLibCrosServicePath,
224 kLibCrosServiceInterface,
David Sodman8ef20062015-01-06 09:23:40 -0800225 kReleaseDisplayOwnership);
David Sodmanbf3f2842014-11-12 08:26:58 -0800226 if (term_is_active(terminal))
David Sodmanf0a925a2015-05-04 11:19:19 -0700227 term_deactivate(terminal);
David Sodmanbf3f2842014-11-12 08:26:58 -0800228 input.current_terminal = ev->code - KEY_F2;
229 terminal = input.terminals[input.current_terminal];
230 if (terminal == NULL) {
231 input.terminals[input.current_terminal] =
David Sodmanf0a925a2015-05-04 11:19:19 -0700232 term_init(true, NULL);
David Sodmanbf3f2842014-11-12 08:26:58 -0800233 terminal =
234 input.terminals[input.current_terminal];
David Sodman8ef20062015-01-06 09:23:40 -0800235 term_activate(terminal);
David Sodmanbf3f2842014-11-12 08:26:58 -0800236 if (!term_is_valid(terminal)) {
237 LOG(ERROR, "Term init failed");
238 return 1;
239 }
240 }
David Sodmanf0a925a2015-05-04 11:19:19 -0700241 term_activate(input.terminals[input.current_terminal]);
David Sodmanbf3f2842014-11-12 08:26:58 -0800242 }
243
244 return 1;
245
246 }
247
248 return 0;
249}
250
251static void input_get_keysym_and_unicode(struct input_key_event *event,
252 uint32_t *keysym, uint32_t *unicode)
253{
254 struct {
255 uint32_t code;
256 uint32_t keysym;
257 } non_ascii_keys[] = {
258 { KEY_ESC, KEYSYM_ESC},
259 { KEY_HOME, KEYSYM_HOME},
260 { KEY_LEFT, KEYSYM_LEFT},
261 { KEY_UP, KEYSYM_UP},
262 { KEY_RIGHT, KEYSYM_RIGHT},
263 { KEY_DOWN, KEYSYM_DOWN},
264 { KEY_PAGEUP, KEYSYM_PAGEUP},
265 { KEY_PAGEDOWN, KEYSYM_PAGEDOWN},
266 { KEY_END, KEYSYM_END},
267 { KEY_INSERT, KEYSYM_INSERT},
268 { KEY_DELETE, KEYSYM_DELETE},
269 };
270
271 for (unsigned i = 0; i < ARRAY_SIZE(non_ascii_keys); i++) {
272 if (non_ascii_keys[i].code == event->code) {
273 *keysym = non_ascii_keys[i].keysym;
274 *unicode = -1;
275 return;
276 }
277 }
278
279 if (event->code >= ARRAY_SIZE(keysym_table) / 2) {
280 *keysym = '?';
281 } else {
282 *keysym = keysym_table[event->code * 2 + input.kbd_state.shift_state];
283 if ((input.kbd_state.control_state) && isascii(*keysym))
284 *keysym = tolower(*keysym) - 'a' + 1;
285 }
286
287 *unicode = *keysym;
288}
289
Dominik Behr93899452014-08-18 22:16:21 -0700290static int input_add(const char *devname)
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700291{
Dominik Behr93899452014-08-18 22:16:21 -0700292 int ret = 0, fd = -1;
293 /* for some reason every device has a null enumerations and notifications
294 of every device come with NULL string first */
295 if (!devname) {
296 ret = -EINVAL;
297 goto errorret;
298 }
299 ret = fd = open(devname, O_RDONLY);
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700300 if (fd < 0)
Dominik Behr93899452014-08-18 22:16:21 -0700301 goto errorret;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700302
Dominik Behr93899452014-08-18 22:16:21 -0700303 struct input_dev *newdevs =
304 realloc(input.devs, (input.ndevs + 1) * sizeof (struct input_dev));
305 if (!newdevs) {
306 ret = -ENOMEM;
307 goto closefd;
308 }
309 input.devs = newdevs;
310 input.devs[input.ndevs].fd = fd;
311 input.devs[input.ndevs].path = strdup(devname);
312 if (!input.devs[input.ndevs].path) {
313 ret = -ENOMEM;
314 goto closefd;
315 }
316 input.ndevs++;
317
318 return fd;
319
320closefd:
321 close(fd);
322errorret:
323 return ret;
324}
325
326static void input_remove(const char *devname)
327{
328 if (!devname) {
329 return;
330 }
331 unsigned int u;
332 for (u = 0; u < input.ndevs; u++) {
333 if (!strcmp(devname, input.devs[u].path)) {
334 free(input.devs[u].path);
335 close(input.devs[u].fd);
336 input.ndevs--;
337 if (u != input.ndevs) {
338 input.devs[u] = input.devs[input.ndevs];
339 }
340 return;
341 }
342 }
343}
344
Dominik Behr93899452014-08-18 22:16:21 -0700345
346int input_init()
347{
348 input.udev = udev_new();
349 if (!input.udev)
350 return -ENOENT;
351 input.udev_monitor = udev_monitor_new_from_netlink(input.udev, "udev");
352 if (!input.udev_monitor) {
353 udev_unref(input.udev);
354 return -ENOENT;
355 }
356 udev_monitor_filter_add_match_subsystem_devtype(input.udev_monitor, "input",
357 NULL);
358 udev_monitor_enable_receiving(input.udev_monitor);
359 input.udev_fd = udev_monitor_get_fd(input.udev_monitor);
360
361 struct udev_enumerate *udev_enum;
362 struct udev_list_entry *devices, *deventry;
363 udev_enum = udev_enumerate_new(input.udev);
364 udev_enumerate_add_match_subsystem(udev_enum, "input");
365 udev_enumerate_scan_devices(udev_enum);
366 devices = udev_enumerate_get_list_entry(udev_enum);
367 udev_list_entry_foreach(deventry, devices) {
368 const char *syspath;
369 struct udev_device *dev;
370 syspath = udev_list_entry_get_name(deventry);
371 dev = udev_device_new_from_syspath(input.udev, syspath);
372 input_add(udev_device_get_devnode(dev));
373 udev_device_unref(dev);
374 }
375 udev_enumerate_unref(udev_enum);
376
377 if (!isatty(fileno(stdout)))
378 setbuf(stdout, NULL);
379
David Sodmanbbcb0522014-09-19 10:34:07 -0700380 if (input.ndevs == 0) {
381 LOG(ERROR, "No valid inputs for terminal");
382 exit(EXIT_SUCCESS);
383 }
384
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700385 return 0;
386}
387
388void input_close()
389{
Dominik Behr93899452014-08-18 22:16:21 -0700390 unsigned int u;
391 for (u = 0; u < input.ndevs; u++) {
392 free(input.devs[u].path);
393 close(input.devs[u].fd);
394 }
395 free(input.devs);
396 input.devs = NULL;
397 input.ndevs = 0;
398
399 udev_monitor_unref(input.udev_monitor);
400 input.udev_monitor = NULL;
401 udev_unref(input.udev);
402 input.udev = NULL;
403 input.udev_fd = -1;
404
David Sodmanbbcb0522014-09-19 10:34:07 -0700405 dbus_destroy(input.dbus);
406
407}
408
409void input_set_dbus(dbus_t* dbus)
410{
411 input.dbus = dbus;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700412}
413
Dominik Behr93899452014-08-18 22:16:21 -0700414int input_setfds(fd_set * read_set, fd_set * exception_set)
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700415{
Dominik Behr93899452014-08-18 22:16:21 -0700416 unsigned int u;
417 int max = -1;
418 for (u = 0; u < input.ndevs; u++) {
419 FD_SET(input.devs[u].fd, read_set);
420 FD_SET(input.devs[u].fd, exception_set);
421 if (input.devs[u].fd > max)
422 max = input.devs[u].fd;
423 }
424
425 FD_SET(input.udev_fd, read_set);
426 FD_SET(input.udev_fd, exception_set);
427 if (input.udev_fd > max)
428 max = input.udev_fd;
429 return max;
430}
431
Dominik Behr93899452014-08-18 22:16:21 -0700432struct input_key_event *input_get_event(fd_set * read_set,
433 fd_set * exception_set)
434{
435 unsigned int u;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700436 struct input_event ev;
437 int ret;
438
Dominik Behr93899452014-08-18 22:16:21 -0700439 if (FD_ISSET(input.udev_fd, exception_set)) {
440 /* udev died on us? */
David Sodmanbbcb0522014-09-19 10:34:07 -0700441 LOG(ERROR, "Exception on udev fd");
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700442 }
443
Dominik Behr93899452014-08-18 22:16:21 -0700444 if (FD_ISSET(input.udev_fd, read_set)
445 && !FD_ISSET(input.udev_fd, exception_set)) {
446 /* we got an udev notification */
447 struct udev_device *dev =
448 udev_monitor_receive_device(input.udev_monitor);
449 if (dev) {
450 if (!strcmp("add", udev_device_get_action(dev))) {
451 input_add(udev_device_get_devnode(dev));
452 } else
453 if (!strcmp("remove", udev_device_get_action(dev)))
454 {
455 input_remove(udev_device_get_devnode(dev));
456 }
457 udev_device_unref(dev);
458 }
459 }
460
461 for (u = 0; u < input.ndevs; u++) {
462 if (FD_ISSET(input.devs[u].fd, read_set)
463 && !FD_ISSET(input.devs[u].fd, exception_set)) {
464 ret =
465 read(input.devs[u].fd, &ev, sizeof (struct input_event));
Michael Spang24d20122015-04-22 13:58:16 -0400466 if (ret < 0) {
467 if (errno == EINTR || errno == EAGAIN)
468 continue;
469 if (errno != ENODEV) {
470 LOG(ERROR, "read: %s: %s", input.devs[u].path,
471 strerror(errno));
472 }
473 input_remove(input.devs[u].path);
474 return NULL;
475 } else if (ret < (int) sizeof (struct input_event)) {
David Sodmanbbcb0522014-09-19 10:34:07 -0700476 LOG(ERROR, "expected %d bytes, got %d",
Dominik Behr93899452014-08-18 22:16:21 -0700477 (int) sizeof (struct input_event), ret);
478 return NULL;
479 }
480
481 if (ev.type == EV_KEY) {
482 struct input_key_event *event =
483 malloc(sizeof (*event));
484 event->code = ev.code;
485 event->value = ev.value;
Dominik Behr93899452014-08-18 22:16:21 -0700486 return event;
487 }
488 }
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700489 }
490
491 return NULL;
492}
493
David Sodmanf0a925a2015-05-04 11:19:19 -0700494int input_process(terminal_t* splash_term, uint32_t usec)
495{
496 terminal_t *terminal;
497 terminal_t *new_terminal;
498 fd_set read_set, exception_set;
499 int maxfd;
500 int sstat;
501 struct timeval tm;
502 struct timeval *ptm;
503
504 terminal = input.terminals[input.current_terminal];
505
506 FD_ZERO(&read_set);
507 FD_ZERO(&exception_set);
508
509 if (input.dbus) {
510 dbus_add_fd(input.dbus, &read_set, &exception_set);
511 maxfd = dbus_get_fd(input.dbus) + 1;
512 } else {
513 maxfd = 0;
514 }
515
516 maxfd = MAX(maxfd, input_setfds(&read_set, &exception_set)) + 1;
517
518 for (int i = 0; i < MAX_TERMINALS; i++) {
519 if (term_is_valid(input.terminals[i])) {
520 term_add_fd(input.terminals[i], &read_set, &exception_set);
521 maxfd = MAX(maxfd, term_fd(input.terminals[i])) + 1;
522 term_dispatch_io(input.terminals[i], &read_set);
523 }
524 }
525
526 if (usec) {
527 ptm = &tm;
528 tm.tv_sec = 0;
529 tm.tv_usec = usec;
530 } else
531 ptm = NULL;
532
533 sstat = select(maxfd, &read_set, NULL, &exception_set, ptm);
534 if (sstat == 0)
535 return 0;
536
537
538 if (input.dbus)
539 dbus_dispatch_io(input.dbus);
540
541 if (term_exception(terminal, &exception_set))
542 return -1;
543
544 struct input_key_event *event;
545 event = input_get_event(&read_set, &exception_set);
546 if (event) {
547 if (!input_special_key(event) && event->value) {
548 uint32_t keysym, unicode;
549 // current_terminal can possibly change during
550 // execution of input_special_key
551 terminal = input.terminals[input.current_terminal];
552 if (term_is_active(terminal)) {
553 // Only report user activity when the terminal is active
554 report_user_activity(USER_ACTIVITY_OTHER);
555 input_get_keysym_and_unicode(
556 event, &keysym, &unicode);
557 term_key_event(terminal,
558 keysym, unicode);
559 }
560 }
561 input_put_event(event);
562 }
563
564 for (int i = 0; i < MAX_TERMINALS; i++) {
565 if (term_is_valid(input.terminals[i])) {
566 term_add_fd(input.terminals[i], &read_set, &exception_set);
567 term_dispatch_io(input.terminals[i], &read_set);
568 }
569 }
570
571 if (term_is_valid(terminal)) {
572 if (term_is_child_done(terminal)) {
573 if (terminal == input.terminals[SPLASH_TERMINAL]) {
574 /*
575 * Note: reference is not lost because it is still referenced
576 * by the splash_t structure which will ultimately destroy
577 * it, once it's safe to do so
578 */
579 input.terminals[SPLASH_TERMINAL] = NULL;
580 return -1;
581 }
582 input.terminals[input.current_terminal] = term_init(true, NULL);
583 new_terminal = input.terminals[input.current_terminal];
584 if (!term_is_valid(new_terminal)) {
585 return -1;
586 }
587 term_activate(new_terminal);
588 term_close(terminal);
589 }
590 }
591
592 return 0;
593}
594
David Sodmanbf3f2842014-11-12 08:26:58 -0800595int input_run(bool standalone)
596{
David Sodmanbf3f2842014-11-12 08:26:58 -0800597 terminal_t* terminal;
David Sodmanf0a925a2015-05-04 11:19:19 -0700598 int status;
David Sodmanbf3f2842014-11-12 08:26:58 -0800599
600 if (standalone) {
David Sodman8ef20062015-01-06 09:23:40 -0800601 if (input.dbus) {
602 (void)dbus_method_call0(input.dbus,
603 kLibCrosServiceName,
604 kLibCrosServicePath,
605 kLibCrosServiceInterface,
606 kReleaseDisplayOwnership);
607 }
David Sodmanbf3f2842014-11-12 08:26:58 -0800608
David Sodmanf0a925a2015-05-04 11:19:19 -0700609 input.terminals[input.current_terminal] = term_init(true, NULL);
David Sodmanbf3f2842014-11-12 08:26:58 -0800610 terminal = input.terminals[input.current_terminal];
David Sodman8ef20062015-01-06 09:23:40 -0800611 term_activate(terminal);
David Sodmanbf3f2842014-11-12 08:26:58 -0800612 }
613
614 while (1) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700615 status = input_process(NULL, 0);
616 if (status != 0) {
617 LOG(ERROR, "input process returned %d", status);
618 break;
David Sodmanbf3f2842014-11-12 08:26:58 -0800619 }
620 }
621
622 return 0;
623}
624
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -0700625void input_put_event(struct input_key_event *event)
626{
627 free(event);
628}
David Sodmanbbcb0522014-09-19 10:34:07 -0700629
David Sodman8ef20062015-01-06 09:23:40 -0800630terminal_t* input_create_term(int vt)
631{
632 terminal_t* terminal;
633
634 if (vt == 0)
635 return input.terminals[input.current_terminal];
636
637 terminal = input.terminals[vt-1];
638 if (term_is_active(terminal))
639 return terminal;
640
641 if (terminal == NULL) {
David Sodmanf0a925a2015-05-04 11:19:19 -0700642 input.terminals[vt-1] = term_init(false, NULL);
David Sodman8ef20062015-01-06 09:23:40 -0800643 terminal = input.terminals[vt-1];
644 if (!term_is_valid(terminal)) {
645 LOG(ERROR, "create_term: Term init failed");
646 }
647 }
648
649 return terminal;
650}
651
David Sodmanf0a925a2015-05-04 11:19:19 -0700652terminal_t* input_create_splash_term(video_t* video)
653{
654 input.terminals[SPLASH_TERMINAL] = term_init(false, video);
655 return input.terminals[SPLASH_TERMINAL];
656}
657
658void input_destroy_splash_term()
659{
660 input.terminals[SPLASH_TERMINAL] = NULL;
661}
662
David Sodman8ef20062015-01-06 09:23:40 -0800663void input_set_current(terminal_t* terminal)
664{
665 int i;
666
David Sodmanf0a925a2015-05-04 11:19:19 -0700667 if (!terminal) {
668 input.terminals[input.current_terminal] = NULL;
669 input.current_terminal = 0;
David Sodman8ef20062015-01-06 09:23:40 -0800670 return;
David Sodmanf0a925a2015-05-04 11:19:19 -0700671 }
David Sodman8ef20062015-01-06 09:23:40 -0800672
673 for (i = 0; i < MAX_TERMINALS; i++) {
674 if (terminal == input.terminals[i]) {
675 input.current_terminal = i;
676 return;
677 }
678 }
679}
680
681unsigned int input_get_maxterminals()
682{
David Sodmanf0a925a2015-05-04 11:19:19 -0700683 return MAX_STD_TERMINALS;
David Sodman8ef20062015-01-06 09:23:40 -0800684}