2 * wlt - Wayland Terminal
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.
27 * Wayland Terminal main application
36 #include <sys/signalfd.h>
37 #include <wayland-client.h>
41 #include "shl_dlist.h"
45 #include "wlt_terminal.h"
46 #include "wlt_theme.h"
47 #include "wlt_toolkit.h"
49 #define LOG_SUBSYSTEM "wlt"
52 struct ev_eloop *eloop;
53 struct wlt_display *disp;
54 struct wlt_window *wnd;
57 /* TODO: The font layer depends on kmscon modules. However, the WLT applications
58 * doesn't use them. Therefore, we provide dummy kmscon_module_* helpers here
59 * which satisfy the dependencies and allow us to link to font.so. */
61 void kmscon_module_ref(struct kmscon_module *m)
65 void kmscon_module_unref(struct kmscon_module *m)
69 static void sig_generic(struct ev_eloop *eloop, struct signalfd_siginfo *info,
72 struct wlt_app *app = data;
74 ev_eloop_exit(app->eloop);
75 log_info("terminating due to caught signal %d", info->ssi_signo);
78 static void window_close(struct wlt_window *wnd, void *data)
80 struct wlt_app *app = data;
82 log_info("closing window");
83 ev_eloop_exit(app->eloop);
86 static void terminal_close(struct wlt_terminal *term, unsigned int event,
89 struct wlt_app *app = data;
91 if (event == WLT_TERMINAL_HUP) {
92 log_info("closing pty");
93 ev_eloop_exit(app->eloop);
97 static int window_init(struct wlt_app *app)
100 struct wlt_theme *theme;
101 struct wlt_terminal *term;
103 ret = wlt_display_create_window(app->disp, &app->wnd,
106 log_error("cannot create wayland window");
109 wlt_window_set_close_cb(app->wnd, window_close);
111 ret = wlt_theme_new(&theme, app->wnd);
113 log_error("cannot create theme");
117 ret = wlt_terminal_new(&term, app->wnd);
119 log_error("cannot create terminal");
123 ret = wlt_terminal_open(term, terminal_close, app);
125 log_error("cannot open terminal");
132 static void display_event(struct wlt_display *disp, unsigned int event,
135 struct wlt_app *app = data;
139 case WLT_DISPLAY_READY:
140 log_info("wayland display initialized");
141 ret = window_init(app);
143 ev_eloop_exit(app->eloop);
145 case WLT_DISPLAY_HUP:
146 log_info("wayland display connection lost");
147 ev_eloop_exit(app->eloop);
152 static void destroy_app(struct wlt_app *app)
154 wlt_window_unref(app->wnd);
155 wlt_display_unregister_cb(app->disp, display_event, app);
156 wlt_display_unref(app->disp);
157 ev_eloop_unregister_signal_cb(app->eloop, SIGINT, sig_generic, app);
158 ev_eloop_unregister_signal_cb(app->eloop, SIGTERM, sig_generic, app);
159 ev_eloop_unref(app->eloop);
162 static int setup_app(struct wlt_app *app)
166 ret = ev_eloop_new(&app->eloop, log_llog, NULL);
170 ret = ev_eloop_register_signal_cb(app->eloop, SIGTERM,
175 ret = ev_eloop_register_signal_cb(app->eloop, SIGINT,
180 ret = wlt_display_new(&app->disp, app->eloop);
184 ret = wlt_display_register_cb(app->disp, display_event, app);
195 struct wlt_conf_t wlt_conf;
196 #define WLT_CONF_FROM_FIELD(_mem, _name) \
197 shl_offsetof((_mem), struct wlt_conf_t, _name)
199 static void print_help()
202 * Usage/Help information
203 * This should be scaled to a maximum of 80 characters per line:
206 * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
207 * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
208 * 80 char line starting with tab:
209 * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 |
210 * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
215 "\t%1$s -h [options]\n"
216 "\t%1$s -l [options] -- /bin/sh [sh-arguments]\n"
218 "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
219 "given multiple times, only the last argument matters if not otherwise stated.\n"
222 "\t-h, --help [off] Print this help and exit\n"
223 "\t-v, --verbose [off] Print verbose messages\n"
224 "\t --debug [off] Enable debug mode\n"
225 "\t --silent [off] Suppress notices and warnings\n"
227 "Terminal Options:\n"
228 "\t-l, --login [/bin/sh]\n"
229 "\t Start the given login process instead\n"
230 "\t of the default process; all arguments\n"
231 "\t following '--' will be be parsed as\n"
232 "\t argv to this process. No more options\n"
233 "\t after '--' will be parsed so use it at\n"
234 "\t the end of the argument string\n"
235 "\t-t, --term <TERM> [xterm-256color]\n"
236 "\t Value of the TERM environment variable\n"
237 "\t for the child process\n"
238 "\t --palette <name> [default]\n"
239 "\t Select the used color palette\n"
240 "\t --sb-size <num> [1000]\n"
241 "\t Size of the scrollback-buffer in lines\n"
243 "Keyboard Shortcuts and Grabs:\n"
244 "\t --grab-scroll-up <grab> [<Shift>Up]\n"
245 "\t Shortcut to scroll up\n"
246 "\t --grab-scroll-down <grab> [<Shift>Down]\n"
247 "\t Shortcut to scroll down\n"
248 "\t --grab-page-up <grab> [<Shift>Prior]\n"
249 "\t Shortcut to scroll page up\n"
250 "\t --grab-page-down <grab> [<Shift>Next]\n"
251 "\t Shortcut to scroll page down\n"
252 "\t --grab-fullscreen <grab> [F11]\n"
253 "\t Shortcut to toggle fullscreen mode\n"
254 "\t --grab-zoom-in <grab> [<Ctrl>plus]\n"
255 "\t Shortcut to increase font size\n"
256 "\t --grab-zoom-out <grab> [<Ctrl>minus]\n"
257 "\t Shortcut to decrease font size\n"
258 "\t --grab-copy <grab> [<Logo>c]\n"
259 "\t Copy selected text\n"
260 "\t --grab-paste <grab> [<Logo>v]\n"
261 "\t Paste selection buffer\n"
264 "\t --font-engine <engine> [pango]\n"
266 "\t --font-size <points> [15]\n"
267 "\t Font size in points\n"
268 "\t --font-name <name> [monospace]\n"
270 "\t --font-dpi <dpi> [96]\n"
271 "\t Force DPI value for all fonts\n"
274 "\t --xkb-repeat-delay <msecs> [250]\n"
275 "\t Initial delay for key-repeat in ms\n"
276 "\t --xkb-repeat-rate <msecs> [50]\n"
277 "\t Delay between two key-repeats in ms\n",
281 * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
282 * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
283 * 80 char line starting with tab:
284 * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 |
285 * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
289 static int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
292 struct wlt_conf_t *conf = WLT_CONF_FROM_FIELD(opt->mem, debug);
294 /* --debug implies --verbose */
296 conf->verbose = true;
301 static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
304 struct wlt_conf_t *conf = WLT_CONF_FROM_FIELD(opt->mem, help);
306 /* exit after printing --help information */
315 static char *def_argv[] = { NULL, "-i", NULL };
317 static int aftercheck_login(struct conf_option *opt, int argc, char **argv,
320 struct wlt_conf_t *conf = WLT_CONF_FROM_FIELD(opt->mem, login);
326 /* parse "--login [...] -- args" arguments */
327 if (argv && conf->login) {
329 fprintf(stderr, "Arguments for --login missing\n");
333 conf->argv = &argv[idx];
335 } else if (!conf->argv) {
336 def_argv[0] = getenv("SHELL") ? : _PATH_BSHELL;
337 conf->argv = def_argv;
346 static int copy_login(struct conf_option *opt, const struct conf_option *src)
348 struct wlt_conf_t *conf = WLT_CONF_FROM_FIELD(opt->mem, login);
349 struct wlt_conf_t *s = WLT_CONF_FROM_FIELD(src->mem, login);
353 ret = shl_dup_array(&t, s->argv);
363 static struct conf_grab def_grab_scroll_up =
364 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Up);
366 static struct conf_grab def_grab_scroll_down =
367 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Down);
369 static struct conf_grab def_grab_page_up =
370 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Prior);
372 static struct conf_grab def_grab_page_down =
373 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Next);
375 static struct conf_grab def_grab_fullscreen =
376 CONF_SINGLE_GRAB(0, XKB_KEY_F11);
378 static struct conf_grab def_grab_zoom_in =
379 CONF_SINGLE_GRAB(SHL_CONTROL_MASK, XKB_KEY_plus);
381 static struct conf_grab def_grab_zoom_out =
382 CONF_SINGLE_GRAB(SHL_CONTROL_MASK, XKB_KEY_minus);
384 static struct conf_grab def_grab_copy =
385 CONF_SINGLE_GRAB(SHL_LOGO_MASK, XKB_KEY_c);
387 static struct conf_grab def_grab_paste =
388 CONF_SINGLE_GRAB(SHL_LOGO_MASK, XKB_KEY_v);
390 struct conf_option options[] = {
391 CONF_OPTION_BOOL_FULL('h', "help", aftercheck_help, NULL, NULL, &wlt_conf.help, false),
392 CONF_OPTION_BOOL('v', "verbose", &wlt_conf.verbose, false),
393 CONF_OPTION_BOOL_FULL(0, "debug", aftercheck_debug, NULL, NULL, &wlt_conf.debug, false),
394 CONF_OPTION_BOOL(0, "silent", &wlt_conf.silent, false),
396 CONF_OPTION_BOOL_FULL('l', "login", aftercheck_login, copy_login, NULL, &wlt_conf.login, false),
397 CONF_OPTION_STRING('t', "term", &wlt_conf.term, "xterm-256color"),
398 CONF_OPTION_STRING(0, "palette", &wlt_conf.palette, NULL),
399 CONF_OPTION_UINT(0, "sb-size", &wlt_conf.sb_size, 1000),
401 CONF_OPTION_GRAB(0, "grab-scroll-up", &wlt_conf.grab_scroll_up, &def_grab_scroll_up),
402 CONF_OPTION_GRAB(0, "grab-scroll-down", &wlt_conf.grab_scroll_down, &def_grab_scroll_down),
403 CONF_OPTION_GRAB(0, "grab-page-up", &wlt_conf.grab_page_up, &def_grab_page_up),
404 CONF_OPTION_GRAB(0, "grab-page-down", &wlt_conf.grab_page_down, &def_grab_page_down),
405 CONF_OPTION_GRAB(0, "grab-fullscreen", &wlt_conf.grab_fullscreen, &def_grab_fullscreen),
406 CONF_OPTION_GRAB(0, "grab-zoom-in", &wlt_conf.grab_zoom_in, &def_grab_zoom_in),
407 CONF_OPTION_GRAB(0, "grab-zoom-out", &wlt_conf.grab_zoom_out, &def_grab_zoom_out),
408 CONF_OPTION_GRAB(0, "grab-copy", &wlt_conf.grab_copy, &def_grab_copy),
409 CONF_OPTION_GRAB(0, "grab-paste", &wlt_conf.grab_paste, &def_grab_paste),
411 CONF_OPTION_STRING(0, "font-engine", &wlt_conf.font_engine, "pango"),
412 CONF_OPTION_UINT(0, "font-size", &wlt_conf.font_size, 12),
413 CONF_OPTION_STRING(0, "font-name", &wlt_conf.font_name, "monospace"),
414 CONF_OPTION_UINT(0, "font-dpi", &wlt_conf.font_ppi, 96),
416 CONF_OPTION_UINT(0, "xkb-repeat-delay", &wlt_conf.xkb_repeat_delay, 250),
417 CONF_OPTION_UINT(0, "xkb-repeat-rate", &wlt_conf.xkb_repeat_rate, 50),
420 int main(int argc, char **argv)
424 struct conf_ctx *conf;
426 ret = conf_ctx_new(&conf, options, sizeof(options) / sizeof(*options),
431 ret = conf_ctx_parse_argv(conf, argc, argv);
440 if (!wlt_conf.debug && !wlt_conf.verbose && wlt_conf.silent)
441 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
443 log_set_config(&LOG_CONFIG_INFO(wlt_conf.debug,
446 log_print_init("wlterm");
448 kmscon_font_register(&kmscon_font_8x16_ops);
449 kmscon_font_register(&kmscon_font_pango_ops);
451 memset(&app, 0, sizeof(app));
452 ret = setup_app(&app);
456 ev_eloop_run(app.eloop, -1);
459 kmscon_font_unregister(kmscon_font_pango_ops.name);
460 kmscon_font_unregister(kmscon_font_8x16_ops.name);
467 kmscon_font_unregister(kmscon_font_pango_ops.name);
468 kmscon_font_unregister(kmscon_font_8x16_ops.name);
472 log_err("cannot initialize wlterm, errno %d: %s", ret, strerror(-ret));