2c8d509f380c389a7c21d18b6daba060a0e4c7ea
[platform/upstream/kmscon.git] / src / wlt_main.c
1 /*
2  * wlt - Wayland Terminal
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
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.
24  */
25
26 /*
27  * Wayland Terminal main application
28  */
29
30 #include <errno.h>
31 #include <paths.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/signalfd.h>
37 #include <wayland-client.h>
38 #include "conf.h"
39 #include "eloop.h"
40 #include "font.h"
41 #include "log.h"
42 #include "shl_dlist.h"
43 #include "shl_misc.h"
44 #include "wlt_main.h"
45 #include "wlt_terminal.h"
46 #include "wlt_theme.h"
47 #include "wlt_toolkit.h"
48
49 #define LOG_SUBSYSTEM "wlt"
50
51 struct wlt_app {
52         struct ev_eloop *eloop;
53         struct wlt_display *disp;
54         struct wlt_window *wnd;
55 };
56
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. */
60
61 void kmscon_module_ref(struct kmscon_module *m)
62 {
63 }
64
65 void kmscon_module_unref(struct kmscon_module *m)
66 {
67 }
68
69 static void sig_generic(struct ev_eloop *eloop, struct signalfd_siginfo *info,
70                         void *data)
71 {
72         struct wlt_app *app = data;
73
74         ev_eloop_exit(app->eloop);
75         log_info("terminating due to caught signal %d", info->ssi_signo);
76 }
77
78 static void window_close(struct wlt_window *wnd, void *data)
79 {
80         struct wlt_app *app = data;
81
82         log_info("closing window");
83         ev_eloop_exit(app->eloop);
84 }
85
86 static void terminal_close(struct wlt_terminal *term, unsigned int event,
87                            void *data)
88 {
89         struct wlt_app *app = data;
90
91         if (event == WLT_TERMINAL_HUP) {
92                 log_info("closing pty");
93                 ev_eloop_exit(app->eloop);
94         }
95 }
96
97 static int window_init(struct wlt_app *app)
98 {
99         int ret;
100         struct wlt_theme *theme;
101         struct wlt_terminal *term;
102
103         ret = wlt_display_create_window(app->disp, &app->wnd,
104                                         600, 400, app);
105         if (ret) {
106                 log_error("cannot create wayland window");
107                 return ret;
108         }
109         wlt_window_set_close_cb(app->wnd, window_close);
110
111         ret = wlt_theme_new(&theme, app->wnd);
112         if (ret) {
113                 log_error("cannot create theme");
114                 return ret;
115         }
116
117         ret = wlt_terminal_new(&term, app->wnd);
118         if (ret) {
119                 log_error("cannot create terminal");
120                 return ret;
121         }
122
123         ret = wlt_terminal_open(term, terminal_close, app);
124         if (ret) {
125                 log_error("cannot open terminal");
126                 return ret;
127         }
128
129         return 0;
130 }
131
132 static void display_event(struct wlt_display *disp, unsigned int event,
133                           void *data)
134 {
135         struct wlt_app *app = data;
136         int ret;
137
138         switch (event) {
139         case WLT_DISPLAY_READY:
140                 log_info("wayland display initialized");
141                 ret = window_init(app);
142                 if (ret)
143                         ev_eloop_exit(app->eloop);
144                 break;
145         case WLT_DISPLAY_HUP:
146                 log_info("wayland display connection lost");
147                 ev_eloop_exit(app->eloop);
148                 break;
149         }
150 }
151
152 static void destroy_app(struct wlt_app *app)
153 {
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);
160 }
161
162 static int setup_app(struct wlt_app *app)
163 {
164         int ret;
165
166         ret = ev_eloop_new(&app->eloop, log_llog, NULL);
167         if (ret)
168                 goto err_app;
169
170         ret = ev_eloop_register_signal_cb(app->eloop, SIGTERM,
171                                                 sig_generic, app);
172         if (ret)
173                 goto err_app;
174
175         ret = ev_eloop_register_signal_cb(app->eloop, SIGINT,
176                                                 sig_generic, app);
177         if (ret)
178                 goto err_app;
179
180         ret = wlt_display_new(&app->disp, app->eloop);
181         if (ret)
182                 goto err_app;
183
184         ret = wlt_display_register_cb(app->disp, display_event, app);
185         if (ret)
186                 goto err_app;
187
188         return 0;
189
190 err_app:
191         destroy_app(app);
192         return ret;
193 }
194
195 struct wlt_conf_t wlt_conf;
196 #define WLT_CONF_FROM_FIELD(_mem, _name) \
197         shl_offsetof((_mem), struct wlt_conf_t, _name)
198
199 static void print_help()
200 {
201         /*
202          * Usage/Help information
203          * This should be scaled to a maximum of 80 characters per line:
204          *
205          * 80 char 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"
211          */
212         fprintf(stderr,
213                 "Usage:\n"
214                 "\t%1$s [options]\n"
215                 "\t%1$s -h [options]\n"
216                 "\t%1$s -l [options] -- /bin/sh [sh-arguments]\n"
217                 "\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"
220                 "\n"
221                 "General Options:\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"
226                 "\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"
242                 "\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"
262                 "\n"
263                 "Font Options:\n"
264                 "\t    --font-engine <engine>  [pango]\n"
265                 "\t                              Font engine\n"
266                 "\t    --font-size <points>    [15]\n"
267                 "\t                              Font size in points\n"
268                 "\t    --font-name <name>      [monospace]\n"
269                 "\t                              Font name\n"
270                 "\t    --font-dpi <dpi>        [96]\n"
271                 "\t                              Force DPI value for all fonts\n"
272                 "\n"
273                 "Input Options:\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",
278                 "wlterm");
279         /*
280          * 80 char line:
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"
286          */
287 }
288
289 static int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
290                             int idx)
291 {
292         struct wlt_conf_t *conf = WLT_CONF_FROM_FIELD(opt->mem, debug);
293
294         /* --debug implies --verbose */
295         if (conf->debug)
296                 conf->verbose = true;
297
298         return 0;
299 }
300
301 static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
302                            int idx)
303 {
304         struct wlt_conf_t *conf = WLT_CONF_FROM_FIELD(opt->mem, help);
305
306         /* exit after printing --help information */
307         if (conf->help) {
308                 print_help();
309                 conf->exit = true;
310         }
311
312         return 0;
313 }
314
315 static char *def_argv[] = { NULL, "-i", NULL };
316
317 static int aftercheck_login(struct conf_option *opt, int argc, char **argv,
318                             int idx)
319 {
320         struct wlt_conf_t *conf = WLT_CONF_FROM_FIELD(opt->mem, login);
321         int ret;
322
323         if (!argv)
324                 return 0;
325
326         /* parse "--login [...] -- args" arguments */
327         if (argv && conf->login) {
328                 if (idx >= argc) {
329                         fprintf(stderr, "Arguments for --login missing\n");
330                         return -EFAULT;
331                 }
332
333                 conf->argv = &argv[idx];
334                 ret = argc - idx;
335         } else if (!conf->argv) {
336                 def_argv[0] = getenv("SHELL") ? : _PATH_BSHELL;
337                 conf->argv = def_argv;
338                 ret = 0;
339         } else {
340                 ret = 0;
341         }
342
343         return ret;
344 }
345
346 static int copy_login(struct conf_option *opt, const struct conf_option *src)
347 {
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);
350         int ret;
351         char **t;
352
353         ret = shl_dup_array(&t, s->argv);
354         if (ret)
355                 return ret;
356
357         free(conf->argv);
358         conf->argv = t;
359
360         return 0;
361 }
362
363 static struct conf_grab def_grab_scroll_up =
364                 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Up);
365
366 static struct conf_grab def_grab_scroll_down =
367                 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Down);
368
369 static struct conf_grab def_grab_page_up =
370                 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Prior);
371
372 static struct conf_grab def_grab_page_down =
373                 CONF_SINGLE_GRAB(SHL_SHIFT_MASK, XKB_KEY_Next);
374
375 static struct conf_grab def_grab_fullscreen =
376                 CONF_SINGLE_GRAB(0, XKB_KEY_F11);
377
378 static struct conf_grab def_grab_zoom_in =
379                 CONF_SINGLE_GRAB(SHL_CONTROL_MASK, XKB_KEY_plus);
380
381 static struct conf_grab def_grab_zoom_out =
382                 CONF_SINGLE_GRAB(SHL_CONTROL_MASK, XKB_KEY_minus);
383
384 static struct conf_grab def_grab_copy =
385                 CONF_SINGLE_GRAB(SHL_LOGO_MASK, XKB_KEY_c);
386
387 static struct conf_grab def_grab_paste =
388                 CONF_SINGLE_GRAB(SHL_LOGO_MASK, XKB_KEY_v);
389
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),
395
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),
400
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),
410
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),
415
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),
418 };
419
420 int main(int argc, char **argv)
421 {
422         int ret;
423         struct wlt_app app;
424         struct conf_ctx *conf;
425
426         ret = conf_ctx_new(&conf, options, sizeof(options) / sizeof(*options),
427                            &wlt_conf);
428         if (ret)
429                 goto err_out;
430
431         ret = conf_ctx_parse_argv(conf, argc, argv);
432         if (ret)
433                 goto err_conf;
434
435         if (wlt_conf.exit) {
436                 conf_ctx_free(conf);
437                 return EXIT_SUCCESS;
438         }
439
440         if (!wlt_conf.debug && !wlt_conf.verbose && wlt_conf.silent)
441                 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
442         else
443                 log_set_config(&LOG_CONFIG_INFO(wlt_conf.debug,
444                                                 wlt_conf.verbose));
445
446         log_print_init("wlterm");
447
448         kmscon_font_register(&kmscon_font_8x16_ops);
449         kmscon_font_register(&kmscon_font_pango_ops);
450
451         memset(&app, 0, sizeof(app));
452         ret = setup_app(&app);
453         if (ret)
454                 goto err_unload;
455
456         ev_eloop_run(app.eloop, -1);
457
458         destroy_app(&app);
459         kmscon_font_unregister(kmscon_font_pango_ops.name);
460         kmscon_font_unregister(kmscon_font_8x16_ops.name);
461         conf_ctx_free(conf);
462         log_info("exiting");
463
464         return EXIT_SUCCESS;
465
466 err_unload:
467         kmscon_font_unregister(kmscon_font_pango_ops.name);
468         kmscon_font_unregister(kmscon_font_8x16_ops.name);
469 err_conf:
470         conf_ctx_free(conf);
471 err_out:
472         log_err("cannot initialize wlterm, errno %d: %s", ret, strerror(-ret));
473         return -ret;
474 }