4 * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include <sys/signalfd.h>
38 #include "static_misc.h"
44 struct ev_eloop *eloop;
45 struct ev_eloop *vt_eloop;
47 struct uterm_vt_master *vtm;
48 struct uterm_monitor *mon;
49 struct shl_dlist seats;
53 struct shl_dlist list;
54 struct kmscon_app *app;
56 struct uterm_monitor_seat *useat;
61 struct uterm_input *input;
63 struct shl_dlist videos;
67 struct shl_dlist list;
68 struct uterm_monitor_dev *vdev;
69 struct uterm_video *video;
72 static void sig_generic(struct ev_eloop *eloop, struct signalfd_siginfo *info,
75 struct kmscon_app *app = data;
77 ev_eloop_exit(app->eloop);
78 log_info("terminating due to caught signal %d", info->ssi_signo);
81 static int vt_event(struct uterm_vt *vt, unsigned int action, void *data)
83 struct kmscon_seat *seat = data;
84 struct shl_dlist *iter;
85 struct kmscon_video *vid;
87 if (action == UTERM_VT_ACTIVATE) {
89 uterm_input_wake_up(seat->input);
91 shl_dlist_for_each(iter, &seat->videos) {
92 vid = shl_dlist_entry(iter, struct kmscon_video, list);
93 uterm_video_wake_up(vid->video);
95 kmscon_ui_wake_up(seat->ui);
96 } else if (action == UTERM_VT_DEACTIVATE) {
97 kmscon_ui_sleep(seat->ui);
98 shl_dlist_for_each(iter, &seat->videos) {
99 vid = shl_dlist_entry(iter, struct kmscon_video, list);
100 uterm_video_sleep(vid->video);
103 uterm_input_sleep(seat->input);
110 static void seat_new(struct kmscon_app *app,
111 struct uterm_monitor_seat *useat,
114 struct kmscon_seat *seat;
120 if (kmscon_conf.all_seats) {
123 for (i = 0; kmscon_conf.seats[i]; ++i) {
124 if (!strcmp(kmscon_conf.seats[i], sname)) {
132 log_info("ignoring seat %s as not specified in seat-list",
137 seat = malloc(sizeof(*seat));
140 memset(seat, 0, sizeof(*seat));
143 shl_dlist_init(&seat->videos);
145 seat->sname = strdup(sname);
147 log_err("cannot allocate memory for seat name");
151 ret = uterm_input_new(&seat->input, app->eloop,
152 kmscon_conf.xkb_layout,
153 kmscon_conf.xkb_variant,
154 kmscon_conf.xkb_options);
158 ret = uterm_vt_allocate(app->vtm, &seat->vt, seat->sname,
159 seat->input, vt_event, seat);
163 ret = kmscon_ui_new(&seat->ui, app->eloop, seat->input);
167 uterm_monitor_set_seat_data(seat->useat, seat);
168 shl_dlist_link(&app->seats, &seat->list);
170 log_info("new seat %s", seat->sname);
174 uterm_vt_deallocate(seat->vt);
176 uterm_input_unref(seat->input);
183 static void seat_free(struct kmscon_seat *seat)
185 log_info("free seat %s", seat->sname);
187 shl_dlist_unlink(&seat->list);
188 uterm_monitor_set_seat_data(seat->useat, NULL);
189 kmscon_ui_free(seat->ui);
190 uterm_input_unref(seat->input);
191 uterm_vt_deallocate(seat->vt);
196 static void seat_add_video(struct kmscon_seat *seat,
197 struct uterm_monitor_dev *dev,
203 struct kmscon_video *vid;
205 if ((type == UTERM_MONITOR_FBDEV) != !!kmscon_conf.use_fbdev)
208 vid = malloc(sizeof(*vid));
211 memset(vid, 0, sizeof(*vid));
214 if (kmscon_conf.use_fbdev)
215 mode = UTERM_VIDEO_FBDEV;
216 else if (kmscon_conf.dumb)
217 mode = UTERM_VIDEO_DUMB;
219 mode = UTERM_VIDEO_DRM;
221 ret = uterm_video_new(&vid->video, seat->app->eloop, mode, node);
223 if (mode == UTERM_VIDEO_DRM) {
224 log_info("cannot create drm device; trying dumb drm mode");
225 ret = uterm_video_new(&vid->video, seat->app->eloop,
226 UTERM_VIDEO_DUMB, node);
234 kmscon_ui_add_video(seat->ui, vid->video);
236 uterm_video_wake_up(vid->video);
237 shl_dlist_link(&seat->videos, &vid->list);
239 log_debug("new graphics device on seat %s", seat->sname);
244 log_warning("cannot add video object %s on %s", node, seat->sname);
248 static void seat_rm_video(struct kmscon_seat *seat,
249 struct uterm_monitor_dev *dev)
251 struct shl_dlist *iter;
252 struct kmscon_video *vid;
254 shl_dlist_for_each(iter, &seat->videos) {
255 vid = shl_dlist_entry(iter, struct kmscon_video, list);
256 if (vid->vdev != dev)
259 log_debug("free graphics device on seat %s", seat->sname);
261 kmscon_ui_remove_video(seat->ui, vid->video);
262 uterm_video_unref(vid->video);
263 shl_dlist_unlink(&vid->list);
270 static void seat_hotplug_video(struct kmscon_seat *seat,
271 struct uterm_monitor_dev *dev)
273 struct shl_dlist *iter;
274 struct kmscon_video *vid;
276 shl_dlist_for_each(iter, &seat->videos) {
277 vid = shl_dlist_entry(iter, struct kmscon_video, list);
278 if (vid->vdev != dev)
281 uterm_video_poll(vid->video);
286 static void monitor_event(struct uterm_monitor *mon,
287 struct uterm_monitor_event *ev,
290 struct kmscon_app *app = data;
291 struct kmscon_seat *seat;
294 case UTERM_MONITOR_NEW_SEAT:
295 seat_new(app, ev->seat, ev->seat_name);
297 case UTERM_MONITOR_FREE_SEAT:
299 seat_free(ev->seat_data);
301 case UTERM_MONITOR_NEW_DEV:
302 seat = ev->seat_data;
305 if (ev->dev_type == UTERM_MONITOR_DRM ||
306 ev->dev_type == UTERM_MONITOR_FBDEV)
307 seat_add_video(seat, ev->dev, ev->dev_type,
309 else if (ev->dev_type == UTERM_MONITOR_INPUT)
310 uterm_input_add_dev(seat->input, ev->dev_node);
312 case UTERM_MONITOR_FREE_DEV:
313 seat = ev->seat_data;
316 if (ev->dev_type == UTERM_MONITOR_DRM ||
317 ev->dev_type == UTERM_MONITOR_FBDEV)
318 seat_rm_video(seat, ev->dev);
319 else if (ev->dev_type == UTERM_MONITOR_INPUT)
320 uterm_input_remove_dev(seat->input, ev->dev_node);
322 case UTERM_MONITOR_HOTPLUG_DEV:
323 seat = ev->seat_data;
326 seat_hotplug_video(seat, ev->dev);
331 static void destroy_app(struct kmscon_app *app)
333 uterm_monitor_unref(app->mon);
334 uterm_vt_master_unref(app->vtm);
335 ev_eloop_unregister_signal_cb(app->eloop, SIGINT, sig_generic, app);
336 ev_eloop_unregister_signal_cb(app->eloop, SIGTERM, sig_generic, app);
337 ev_eloop_rm_eloop(app->vt_eloop);
338 ev_eloop_unref(app->eloop);
341 static int setup_app(struct kmscon_app *app)
345 ret = ev_eloop_new(&app->eloop, log_llog);
349 ret = ev_eloop_register_signal_cb(app->eloop, SIGTERM,
354 ret = ev_eloop_register_signal_cb(app->eloop, SIGINT,
359 ret = ev_eloop_new_eloop(app->eloop, &app->vt_eloop);
363 ret = uterm_vt_master_new(&app->vtm, app->vt_eloop);
367 shl_dlist_init(&app->seats);
369 ret = uterm_monitor_new(&app->mon, app->eloop, monitor_event, app);
373 uterm_monitor_scan(app->mon);
382 struct kmscon_conf_t kmscon_conf;
384 static void print_help()
387 * Usage/Help information
388 * This should be scaled to a maximum of 80 characters per line:
391 * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
392 * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
393 * 80 char line starting with tab:
394 * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 |
395 * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
400 "\t%1$s -h [options]\n"
401 "\t%1$s -l [options] -- /bin/sh [sh-arguments]\n"
403 "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
404 "given multiple times, only the last argument matters if not otherwise stated.\n"
407 "\t-h, --help [off] Print this help and exit\n"
408 "\t-v, --verbose [off] Print verbose messages\n"
409 "\t --debug [off] Enable debug mode\n"
410 "\t --silent [off] Suppress notices and warnings\n"
411 "\t-s, --switchvt [off] Automatically switch to VT\n"
412 "\t --seats <list,of,seats> [seat0] Select seats or pass 'all' to make\n"
413 "\t kmscon run on all seats\n"
415 "Terminal Options:\n"
416 "\t-l, --login [/bin/sh]\n"
417 "\t Start the given login process instead\n"
418 "\t of the default process; all arguments\n"
419 "\t following '--' will be be parsed as\n"
420 "\t argv to this process. No more options\n"
421 "\t after '--' will be parsed so use it at\n"
422 "\t the end of the argument string\n"
423 "\t-t, --term <TERM> [xterm-256color]\n"
424 "\t Value of the TERM environment variable\n"
425 "\t for the child process\n"
426 "\t --palette <name> [default]\n"
427 "\t Select the used color palette\n"
428 "\t --sb-size <num> [1000]\n"
429 "\t Size of the scrollback-buffer in lines\n"
432 "\t --fbdev [off] Use fbdev instead of DRM\n"
433 "\t --dumb [off] Use dumb DRM instead of hardware-\n"
434 "\t accelerated DRM devices\n"
435 "\t --fps [50] Limit frame-rate\n"
436 "\t --render-engine <eng> [-] Console renderer\n"
437 "\t --render-timing [off] Print renderer timing information\n"
439 "Input Device Options:\n"
440 "\t --xkb-layout <layout> [us] Set XkbLayout for input devices\n"
441 "\t --xkb-variant <variant> [-] Set XkbVariant for input devices\n"
442 "\t --xkb-options <options> [-] Set XkbOptions for input devices\n"
444 "\t --grab-scroll-up <grab> [<Shift>Up]\n"
445 "\t Shortcut to scroll up\n"
446 "\t --grab-scroll-down <grab> [<Shift>Down]\n"
447 "\t Shortcut to scroll down\n"
448 "\t --grab-page-up <grab> [<Shift>Prior]\n"
449 "\t Shortcut to scroll page up\n"
450 "\t --grab-page-down <grab> [<Shift>Next]\n"
451 "\t Shortcut to scroll page down\n"
454 "\t --font-engine <engine> [pango]\n"
456 "\t --font-size <points> [15]\n"
457 "\t Font size in points\n"
458 "\t --font-name <name> [monospace]\n"
463 * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
464 * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
465 * 80 char line starting with tab:
466 * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 |
467 * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
471 static int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
474 /* --debug implies --verbose */
475 if (kmscon_conf.debug)
476 kmscon_conf.verbose = 1;
481 static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
484 /* exit after printing --help information */
485 if (kmscon_conf.help) {
487 kmscon_conf.exit = true;
493 static char *def_argv[] = { NULL, "-i", NULL };
495 static int aftercheck_login(struct conf_option *opt, int argc, char **argv,
500 /* parse "--login [...] -- args" arguments */
501 if (kmscon_conf.login) {
503 fprintf(stderr, "Arguments for --login missing\n");
507 kmscon_conf.argv = &argv[idx];
510 def_argv[0] = getenv("SHELL") ? : _PATH_BSHELL;
511 kmscon_conf.argv = def_argv;
518 static int aftercheck_seats(struct conf_option *opt, int argc, char **argv,
521 if (kmscon_conf.seats[0] &&
522 !kmscon_conf.seats[1] &&
523 !strcmp(kmscon_conf.seats[0], "all"))
524 kmscon_conf.all_seats = true;
529 static char *def_seats[] = { "seat0", NULL };
531 static struct uterm_input_grab def_grab_scroll_up = {
532 .mods = UTERM_SHIFT_MASK,
533 .keysym = XKB_KEY_Up,
536 static struct uterm_input_grab def_grab_scroll_down = {
537 .mods = UTERM_SHIFT_MASK,
538 .keysym = XKB_KEY_Down,
541 static struct uterm_input_grab def_grab_page_up = {
542 .mods = UTERM_SHIFT_MASK,
543 .keysym = XKB_KEY_Prior,
546 static struct uterm_input_grab def_grab_page_down = {
547 .mods = UTERM_SHIFT_MASK,
548 .keysym = XKB_KEY_Next,
551 struct conf_option options[] = {
552 CONF_OPTION_BOOL('h', "help", aftercheck_help, &kmscon_conf.help, false),
553 CONF_OPTION_BOOL('v', "verbose", NULL, &kmscon_conf.verbose, false),
554 CONF_OPTION_BOOL(0, "debug", aftercheck_debug, &kmscon_conf.debug, false),
555 CONF_OPTION_BOOL(0, "silent", NULL, &kmscon_conf.silent, false),
556 CONF_OPTION_BOOL(0, "fbdev", NULL, &kmscon_conf.use_fbdev, false),
557 CONF_OPTION_BOOL(0, "dumb", NULL, &kmscon_conf.dumb, false),
558 CONF_OPTION_UINT(0, "fps", NULL, &kmscon_conf.fps, 50),
559 CONF_OPTION_STRING(0, "render-engine", NULL, &kmscon_conf.render_engine, NULL),
560 CONF_OPTION_BOOL(0, "render-timing", NULL, &kmscon_conf.render_timing, false),
561 CONF_OPTION_BOOL('s', "switchvt", NULL, &kmscon_conf.switchvt, false),
562 CONF_OPTION_BOOL('l', "login", aftercheck_login, &kmscon_conf.login, false),
563 CONF_OPTION_STRING('t', "term", NULL, &kmscon_conf.term, "xterm-256color"),
564 CONF_OPTION_STRING(0, "palette", NULL, &kmscon_conf.palette, NULL),
565 CONF_OPTION_UINT(0, "sb-size", NULL, &kmscon_conf.sb_size, 1000),
566 CONF_OPTION_GRAB(0, "grab-scroll-up", NULL, &kmscon_conf.grab_scroll_up, &def_grab_scroll_up),
567 CONF_OPTION_GRAB(0, "grab-scroll-down", NULL, &kmscon_conf.grab_scroll_down, &def_grab_scroll_down),
568 CONF_OPTION_GRAB(0, "grab-page-up", NULL, &kmscon_conf.grab_page_up, &def_grab_page_up),
569 CONF_OPTION_GRAB(0, "grab-page-down", NULL, &kmscon_conf.grab_page_down, &def_grab_page_down),
570 CONF_OPTION_STRING(0, "xkb-layout", NULL, &kmscon_conf.xkb_layout, "us"),
571 CONF_OPTION_STRING(0, "xkb-variant", NULL, &kmscon_conf.xkb_variant, ""),
572 CONF_OPTION_STRING(0, "xkb-options", NULL, &kmscon_conf.xkb_options, ""),
573 CONF_OPTION_STRING(0, "font-engine", NULL, &kmscon_conf.font_engine, "pango"),
574 CONF_OPTION_UINT(0, "font-size", NULL, &kmscon_conf.font_size, 15),
575 CONF_OPTION_STRING(0, "font-name", NULL, &kmscon_conf.font_name, "monospace"),
576 CONF_OPTION_STRING_LIST(0, "seats", aftercheck_seats, &kmscon_conf.seats, def_seats),
579 int main(int argc, char **argv)
582 struct kmscon_app app;
585 onum = sizeof(options) / sizeof(*options);
586 ret = conf_parse_argv(options, onum, argc, argv);
590 if (kmscon_conf.exit) {
591 conf_free(options, onum);
595 if (!kmscon_conf.debug && !kmscon_conf.verbose && kmscon_conf.silent)
596 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
598 log_set_config(&LOG_CONFIG_INFO(kmscon_conf.debug,
599 kmscon_conf.verbose));
601 log_print_init("kmscon");
603 ret = conf_parse_all_files(options, onum);
607 kmscon_font_unifont_load();
608 kmscon_font_8x16_load();
609 kmscon_font_pango_load();
610 kmscon_font_freetype2_load();
611 kmscon_text_bbulk_load();
612 kmscon_text_bblit_load();
613 kmscon_text_gltex_load();
615 memset(&app, 0, sizeof(app));
616 ret = setup_app(&app);
620 if (kmscon_conf.switchvt) {
621 /* TODO: implement automatic VT switching */
624 ev_eloop_run(app.eloop, -1);
626 if (kmscon_conf.switchvt) {
627 /* The VT subsystem needs to acknowledge the VT-leave so if it
628 * returns -EINPROGRESS we need to wait for the VT-leave SIGUSR2
629 * signal to arrive. Therefore, we use a separate eloop object
630 * which is used by the VT system only. Therefore, waiting on
631 * this eloop allows us to safely wait 50ms for the SIGUSR2 to
633 * We use a timeout of 100ms to avoid haning on exit.
634 * We could also wait on app.eloop but this would allow other
635 * subsystems to continue receiving events and this is not what
638 if (ret == -EINPROGRESS)
639 ev_eloop_run(app.vt_eloop, 50);
643 kmscon_text_gltex_unload();
644 kmscon_text_bblit_unload();
645 kmscon_text_bbulk_unload();
646 kmscon_font_freetype2_unload();
647 kmscon_font_pango_unload();
648 kmscon_font_8x16_unload();
649 kmscon_font_unifont_unload();
650 conf_free(options, onum);
656 kmscon_text_gltex_unload();
657 kmscon_text_bblit_unload();
658 kmscon_text_bbulk_unload();
659 kmscon_font_freetype2_unload();
660 kmscon_font_pango_unload();
661 kmscon_font_8x16_unload();
662 kmscon_font_unifont_unload();
664 conf_free(options, onum);
665 log_err("cannot initialize kmscon, errno %d: %s", ret, strerror(-ret));