forked from thestinger/termite
/
termite.cc
718 lines (624 loc) · 26.4 KB
/
termite.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
* Copyright (C) 2013 Daniel Micay
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <array>
#include <functional>
#include <map>
#include <vector>
#include <gtk/gtk.h>
#include <vte/vte.h>
#include "util/maybe.hh"
using namespace std::placeholders;
/* Allow scales a bit smaller and a bit larger than the usual pango ranges */
#define TERMINAL_SCALE_XXX_SMALL (PANGO_SCALE_XX_SMALL/1.2)
#define TERMINAL_SCALE_XXXX_SMALL (TERMINAL_SCALE_XXX_SMALL/1.2)
#define TERMINAL_SCALE_XXXXX_SMALL (TERMINAL_SCALE_XXXX_SMALL/1.2)
#define TERMINAL_SCALE_XXX_LARGE (PANGO_SCALE_XX_LARGE*1.2)
#define TERMINAL_SCALE_XXXX_LARGE (TERMINAL_SCALE_XXX_LARGE*1.2)
#define TERMINAL_SCALE_XXXXX_LARGE (TERMINAL_SCALE_XXXX_LARGE*1.2)
#define TERMINAL_SCALE_MINIMUM (TERMINAL_SCALE_XXXXX_SMALL/1.2)
#define TERMINAL_SCALE_MAXIMUM (TERMINAL_SCALE_XXXXX_LARGE*1.2)
static const std::vector<double> zoom_factors = {
TERMINAL_SCALE_MINIMUM,
TERMINAL_SCALE_XXXXX_SMALL,
TERMINAL_SCALE_XXXX_SMALL,
TERMINAL_SCALE_XXX_SMALL,
PANGO_SCALE_XX_SMALL,
PANGO_SCALE_X_SMALL,
PANGO_SCALE_SMALL,
PANGO_SCALE_MEDIUM,
PANGO_SCALE_LARGE,
PANGO_SCALE_X_LARGE,
PANGO_SCALE_XX_LARGE,
TERMINAL_SCALE_XXX_LARGE,
TERMINAL_SCALE_XXXX_LARGE,
TERMINAL_SCALE_XXXXX_LARGE,
TERMINAL_SCALE_MAXIMUM
};
struct config_info {
gboolean dynamic_title;
gboolean urgent_on_bell;
gboolean size_hints;
gboolean modify_other_keys;
gboolean fullscreen;
char *config_file;
gdouble font_scale;
};
struct keybind_info {
GtkWindow *window;
VteTerminal *vte;
config_info config;
std::function<void (GtkWindow *)> fullscreen_toggle;
};
static void window_title_cb(VteTerminal *vte, gboolean *dynamic_title);
static gboolean window_state_cb(GtkWindow *window, GdkEventWindowState *event, keybind_info *info);
static gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, keybind_info *info);
static void bell_cb(GtkWidget *vte, gboolean *urgent_on_bell);
static gboolean focus_cb(GtkWindow *window);
static void get_vte_padding(VteTerminal *vte, int *left, int *top, int *right, int *bottom);
static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info, char **icon);
static void set_config(GtkWindow *window, VteTerminal *vte, config_info *info,
char **icon, GKeyFile *config);
static std::function<void ()> reload_config;
static void override_background_color(GtkWidget *widget, GdkRGBA *rgba) {
GtkCssProvider *provider = gtk_css_provider_new();
gchar *colorstr = gdk_rgba_to_string(rgba);
char *css = g_strdup_printf("* { background-color: %s; }", colorstr);
gtk_css_provider_load_from_data(provider, css, -1, nullptr);
g_free(colorstr);
g_free(css);
gtk_style_context_add_provider(gtk_widget_get_style_context(widget),
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(provider);
}
static const std::map<int, const char *> modify_table = {
{ GDK_KEY_Tab, "\033[27;5;9~" },
{ GDK_KEY_Return, "\033[27;5;13~" },
{ GDK_KEY_apostrophe, "\033[27;5;39~" },
{ GDK_KEY_comma, "\033[27;5;44~" },
{ GDK_KEY_minus, "\033[27;5;45~" },
{ GDK_KEY_period, "\033[27;5;46~" },
{ GDK_KEY_0, "\033[27;5;48~" },
{ GDK_KEY_1, "\033[27;5;49~" },
{ GDK_KEY_2, "\033[27;5;50~" },
{ GDK_KEY_3, "\033[27;5;51~" },
{ GDK_KEY_4, "\033[27;5;52~" },
{ GDK_KEY_5, "\033[27;5;53~" },
{ GDK_KEY_6, "\033[27;5;54~" },
{ GDK_KEY_7, "\033[27;5;55~" },
{ GDK_KEY_8, "\033[27;5;56~" },
{ GDK_KEY_9, "\033[27;5;57~" },
{ GDK_KEY_semicolon, "\033[27;5;59~" },
{ GDK_KEY_equal, "\033[27;5;61~" },
{ GDK_KEY_exclam, "\033[27;6;33~" },
{ GDK_KEY_quotedbl, "\033[27;6;34~" },
{ GDK_KEY_numbersign, "\033[27;6;35~" },
{ GDK_KEY_dollar, "\033[27;6;36~" },
{ GDK_KEY_percent, "\033[27;6;37~" },
{ GDK_KEY_ampersand, "\033[27;6;38~" },
{ GDK_KEY_parenleft, "\033[27;6;40~" },
{ GDK_KEY_parenright, "\033[27;6;41~" },
{ GDK_KEY_asterisk, "\033[27;6;42~" },
{ GDK_KEY_plus, "\033[27;6;43~" },
{ GDK_KEY_colon, "\033[27;6;58~" },
{ GDK_KEY_less, "\033[27;6;60~" },
{ GDK_KEY_greater, "\033[27;6;62~" },
{ GDK_KEY_question, "\033[27;6;63~" },
};
static const std::map<int, const char *> modify_meta_table = {
{ GDK_KEY_Tab, "\033[27;13;9~" },
{ GDK_KEY_Return, "\033[27;13;13~" },
{ GDK_KEY_apostrophe, "\033[27;13;39~" },
{ GDK_KEY_comma, "\033[27;13;44~" },
{ GDK_KEY_minus, "\033[27;13;45~" },
{ GDK_KEY_period, "\033[27;13;46~" },
{ GDK_KEY_0, "\033[27;13;48~" },
{ GDK_KEY_1, "\033[27;13;49~" },
{ GDK_KEY_2, "\033[27;13;50~" },
{ GDK_KEY_3, "\033[27;13;51~" },
{ GDK_KEY_4, "\033[27;13;52~" },
{ GDK_KEY_5, "\033[27;13;53~" },
{ GDK_KEY_6, "\033[27;13;54~" },
{ GDK_KEY_7, "\033[27;13;55~" },
{ GDK_KEY_8, "\033[27;13;56~" },
{ GDK_KEY_9, "\033[27;13;57~" },
{ GDK_KEY_semicolon, "\033[27;13;59~" },
{ GDK_KEY_equal, "\033[27;13;61~" },
{ GDK_KEY_exclam, "\033[27;14;33~" },
{ GDK_KEY_quotedbl, "\033[27;14;34~" },
{ GDK_KEY_numbersign, "\033[27;14;35~" },
{ GDK_KEY_dollar, "\033[27;14;36~" },
{ GDK_KEY_percent, "\033[27;14;37~" },
{ GDK_KEY_ampersand, "\033[27;14;38~" },
{ GDK_KEY_parenleft, "\033[27;14;40~" },
{ GDK_KEY_parenright, "\033[27;14;41~" },
{ GDK_KEY_asterisk, "\033[27;14;42~" },
{ GDK_KEY_plus, "\033[27;14;43~" },
{ GDK_KEY_colon, "\033[27;14;58~" },
{ GDK_KEY_less, "\033[27;14;60~" },
{ GDK_KEY_greater, "\033[27;14;62~" },
{ GDK_KEY_question, "\033[27;14;63~" },
};
static gboolean modify_key_feed(GdkEventKey *event, keybind_info *info,
const std::map<int, const char *>& table) {
if (info->config.modify_other_keys) {
unsigned int keyval = gdk_keyval_to_lower(event->keyval);
auto entry = table.find((int)keyval);
if (entry != table.end()) {
vte_terminal_feed_child(info->vte, entry->second, -1);
return TRUE;
}
}
return FALSE;
}
static void set_size_hints(GtkWindow *window, VteTerminal *vte) {
static const GdkWindowHints wh = (GdkWindowHints)(GDK_HINT_RESIZE_INC | GDK_HINT_MIN_SIZE |
GDK_HINT_BASE_SIZE);
const int char_width = (int)vte_terminal_get_char_width(vte);
const int char_height = (int)vte_terminal_get_char_height(vte);
int padding_left, padding_top, padding_right, padding_bottom;
get_vte_padding(vte, &padding_left, &padding_top, &padding_right, &padding_bottom);
GdkGeometry hints;
hints.base_width = char_width + padding_left + padding_right;
hints.base_height = char_height + padding_top + padding_bottom;
hints.min_width = hints.base_width;
hints.min_height = hints.base_height;
hints.width_inc = char_width;
hints.height_inc = char_height;
gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &hints, wh);
}
/* {{{ CALLBACKS */
void window_title_cb(VteTerminal *vte, gboolean *dynamic_title) {
const char *const title = *dynamic_title ? vte_terminal_get_window_title(vte) : nullptr;
gtk_window_set_title(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(vte))),
title ? title : "termite");
}
static void reset_font_scale(VteTerminal *vte, gdouble scale) {
vte_terminal_set_font_scale(vte, scale);
}
static void increase_font_scale(VteTerminal *vte) {
gdouble scale = vte_terminal_get_font_scale(vte);
for (auto it = zoom_factors.begin(); it != zoom_factors.end(); ++it) {
if ((*it - scale) > 1e-6) {
vte_terminal_set_font_scale(vte, *it);
return;
}
}
}
static void decrease_font_scale(VteTerminal *vte) {
gdouble scale = vte_terminal_get_font_scale(vte);
for (auto it = zoom_factors.rbegin(); it != zoom_factors.rend(); ++it) {
if ((scale - *it) > 1e-6) {
vte_terminal_set_font_scale(vte, *it);
return;
}
}
}
gboolean window_state_cb(GtkWindow *, GdkEventWindowState *event, keybind_info *info) {
if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)
info->fullscreen_toggle = gtk_window_unfullscreen;
else
info->fullscreen_toggle = gtk_window_fullscreen;
return FALSE;
}
gboolean key_press_cb(VteTerminal *vte, GdkEventKey *event, keybind_info *info) {
const guint modifiers = event->state & gtk_accelerator_get_default_mod_mask();
if (info->config.fullscreen && event->keyval == GDK_KEY_F11) {
info->fullscreen_toggle(info->window);
return TRUE;
}
if (modifiers == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) {
switch (gdk_keyval_to_lower(event->keyval)) {
case GDK_KEY_plus:
increase_font_scale(vte);
return TRUE;
case GDK_KEY_equal:
reset_font_scale(vte, info->config.font_scale);
return TRUE;
case GDK_KEY_c:
#if VTE_CHECK_VERSION(0, 50, 0)
vte_terminal_copy_clipboard_format(vte, VTE_FORMAT_TEXT);
#else
vte_terminal_copy_clipboard(vte);
#endif
return TRUE;
case GDK_KEY_v:
vte_terminal_paste_clipboard(vte);
return TRUE;
case GDK_KEY_r:
reload_config();
return TRUE;
case GDK_KEY_l:
vte_terminal_reset(vte, TRUE, TRUE);
return TRUE;
default:
if (modify_key_feed(event, info, modify_table))
return TRUE;
}
} else if ((modifiers == (GDK_CONTROL_MASK|GDK_MOD1_MASK)) ||
(modifiers == (GDK_CONTROL_MASK|GDK_MOD1_MASK|GDK_SHIFT_MASK))) {
if (modify_key_feed(event, info, modify_meta_table))
return TRUE;
} else if (modifiers == GDK_CONTROL_MASK) {
switch (gdk_keyval_to_lower(event->keyval)) {
case GDK_KEY_plus:
increase_font_scale(vte);
return TRUE;
case GDK_KEY_minus:
decrease_font_scale(vte);
return TRUE;
case GDK_KEY_equal:
reset_font_scale(vte, info->config.font_scale);
return TRUE;
default:
if (modify_key_feed(event, info, modify_table))
return TRUE;
}
}
return FALSE;
}
static void bell_cb(GtkWidget *vte, gboolean *urgent_on_bell) {
if (*urgent_on_bell) {
gtk_window_set_urgency_hint(GTK_WINDOW(gtk_widget_get_toplevel(vte)), TRUE);
}
}
gboolean focus_cb(GtkWindow *window) {
gtk_window_set_urgency_hint(window, FALSE);
return FALSE;
}
/* }}} */
void get_vte_padding(VteTerminal *vte, int *left, int *top, int *right, int *bottom) {
GtkBorder border;
gtk_style_context_get_padding(gtk_widget_get_style_context(GTK_WIDGET(vte)),
gtk_widget_get_state_flags(GTK_WIDGET(vte)),
&border);
*left = border.left;
*right = border.right;
*top = border.top;
*bottom = border.bottom;
}
/* {{{ CONFIG LOADING */
template<typename T>
maybe<T> get_config(T (*get)(GKeyFile *, const char *, const char *, GError **),
GKeyFile *config, const char *group, const char *key) {
GError *error = nullptr;
maybe<T> value = get(config, group, key, &error);
if (error) {
g_error_free(error);
return {};
}
return value;
}
auto get_config_integer(std::bind(get_config<int>, g_key_file_get_integer,
_1, _2, _3));
auto get_config_string(std::bind(get_config<char *>, g_key_file_get_string,
_1, _2, _3));
auto get_config_double(std::bind(get_config<double>, g_key_file_get_double,
_1, _2, _3));
static maybe<GdkRGBA> get_config_color(GKeyFile *config, const char *section, const char *key) {
if (auto s = get_config_string(config, section, key)) {
GdkRGBA color;
if (gdk_rgba_parse(&color, *s)) {
g_free(*s);
return color;
}
g_printerr("invalid color string: %s\n", *s);
g_free(*s);
}
return {};
}
static void load_theme(GtkWindow *window, VteTerminal *vte, GKeyFile *config) {
std::array<GdkRGBA, 256> palette;
char color_key[] = "color000";
for (unsigned i = 0; i < palette.size(); i++) {
snprintf(color_key, sizeof(color_key), "color%u", i);
if (auto color = get_config_color(config, "colors", color_key)) {
palette[i] = *color;
} else if (i < 16) {
palette[i].blue = (((i & 4) ? 0xc000 : 0) + (i > 7 ? 0x3fff: 0)) / 65535.0;
palette[i].green = (((i & 2) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
palette[i].red = (((i & 1) ? 0xc000 : 0) + (i > 7 ? 0x3fff : 0)) / 65535.0;
palette[i].alpha = 0;
} else if (i < 232) {
const unsigned j = i - 16;
const unsigned r = j / 36, g = (j / 6) % 6, b = j % 6;
const unsigned red = (r == 0) ? 0 : r * 40 + 55;
const unsigned green = (g == 0) ? 0 : g * 40 + 55;
const unsigned blue = (b == 0) ? 0 : b * 40 + 55;
palette[i].red = (red | red << 8) / 65535.0;
palette[i].green = (green | green << 8) / 65535.0;
palette[i].blue = (blue | blue << 8) / 65535.0;
palette[i].alpha = 0;
} else if (i < 256) {
const unsigned shade = 8 + (i - 232) * 10;
palette[i].red = palette[i].green = palette[i].blue = (shade | shade << 8) / 65535.0;
palette[i].alpha = 0;
}
}
vte_terminal_set_colors(vte, nullptr, nullptr, palette.data(), palette.size());
if (auto color = get_config_color(config, "colors", "foreground")) {
vte_terminal_set_color_foreground(vte, &*color);
vte_terminal_set_color_bold(vte, &*color);
}
if (auto color = get_config_color(config, "colors", "foreground_bold")) {
vte_terminal_set_color_bold(vte, &*color);
}
if (auto color = get_config_color(config, "colors", "background")) {
vte_terminal_set_color_background(vte, &*color);
override_background_color(GTK_WIDGET(window), &*color);
}
if (auto color = get_config_color(config, "colors", "cursor")) {
vte_terminal_set_color_cursor(vte, &*color);
}
if (auto color = get_config_color(config, "colors", "cursor_foreground")) {
vte_terminal_set_color_cursor_foreground(vte, &*color);
}
if (auto color = get_config_color(config, "colors", "highlight")) {
vte_terminal_set_color_highlight(vte, &*color);
}
}
static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info, char **icon) {
const std::string default_path = "/termite/config";
GKeyFile *config = g_key_file_new();
GError *error = nullptr;
gboolean loaded = FALSE;
if (info->config_file) {
loaded = g_key_file_load_from_file(config,
info->config_file,
G_KEY_FILE_NONE, &error);
if (!loaded)
g_printerr("%s parsing failed: %s\n", info->config_file,
error->message);
}
if (!loaded) {
loaded = g_key_file_load_from_file(config,
(g_get_user_config_dir() + default_path).c_str(),
G_KEY_FILE_NONE, &error);
if (!loaded)
g_printerr("%s parsing failed: %s\n", (g_get_user_config_dir() + default_path).c_str(),
error->message);
}
for (const char *const *dir = g_get_system_config_dirs();
!loaded && *dir; dir++) {
loaded = g_key_file_load_from_file(config, (*dir + default_path).c_str(),
G_KEY_FILE_NONE, &error);
if (!loaded)
g_printerr("%s parsing failed: %s\n", (*dir + default_path).c_str(),
error->message);
}
if (loaded) {
set_config(window, vte, info, icon, config);
}
g_key_file_free(config);
}
static void set_config(GtkWindow *window, VteTerminal *vte, config_info *info,
char **icon, GKeyFile *config) {
auto cfg_bool = [config](const char *key, gboolean value) {
return get_config<gboolean>(g_key_file_get_boolean,
config, "options", key).get_value_or(value);
};
vte_terminal_set_scroll_on_output(vte, cfg_bool("scroll_on_output", FALSE));
vte_terminal_set_scroll_on_keystroke(vte, cfg_bool("scroll_on_keystroke", TRUE));
vte_terminal_set_audible_bell(vte, cfg_bool("audible_bell", FALSE));
vte_terminal_set_mouse_autohide(vte, cfg_bool("mouse_autohide", FALSE));
vte_terminal_set_allow_bold(vte, cfg_bool("allow_bold", TRUE));
#if VTE_CHECK_VERSION (0, 49, 1)
vte_terminal_set_allow_hyperlink(vte, cfg_bool("hyperlinks", FALSE));
#endif
#if VTE_CHECK_VERSION (0, 51, 2)
vte_terminal_set_bold_is_bright(vte, cfg_bool("bold_is_bright", TRUE));
#endif
info->dynamic_title = cfg_bool("dynamic_title", TRUE);
info->urgent_on_bell = cfg_bool("urgent_on_bell", TRUE);
info->size_hints = cfg_bool("size_hints", FALSE);
info->modify_other_keys = cfg_bool("modify_other_keys", FALSE);
info->fullscreen = cfg_bool("fullscreen", TRUE);
info->font_scale = vte_terminal_get_font_scale(vte);
if (auto s = get_config_string(config, "options", "font")) {
PangoFontDescription *font = pango_font_description_from_string(*s);
vte_terminal_set_font(vte, font);
pango_font_description_free(font);
g_free(*s);
}
if (auto i = get_config_integer(config, "options", "scrollback_lines")) {
vte_terminal_set_scrollback_lines(vte, *i);
}
if (auto s = get_config_string(config, "options", "cursor_blink")) {
if (!g_ascii_strcasecmp(*s, "system")) {
vte_terminal_set_cursor_blink_mode(vte, VTE_CURSOR_BLINK_SYSTEM);
} else if (!g_ascii_strcasecmp(*s, "on")) {
vte_terminal_set_cursor_blink_mode(vte, VTE_CURSOR_BLINK_ON);
} else if (!g_ascii_strcasecmp(*s, "off")) {
vte_terminal_set_cursor_blink_mode(vte, VTE_CURSOR_BLINK_OFF);
}
g_free(*s);
}
if (auto s = get_config_string(config, "options", "cursor_shape")) {
if (!g_ascii_strcasecmp(*s, "block")) {
vte_terminal_set_cursor_shape(vte, VTE_CURSOR_SHAPE_BLOCK);
} else if (!g_ascii_strcasecmp(*s, "ibeam")) {
vte_terminal_set_cursor_shape(vte, VTE_CURSOR_SHAPE_IBEAM);
} else if (!g_ascii_strcasecmp(*s, "underline")) {
vte_terminal_set_cursor_shape(vte, VTE_CURSOR_SHAPE_UNDERLINE);
}
g_free(*s);
}
if (icon) {
if (auto s = get_config_string(config, "options", "icon_name")) {
*icon = *s;
}
}
if (info->size_hints) {
set_size_hints(GTK_WINDOW(window), vte);
}
load_theme(window, vte, config);
}/*}}}*/
static void exit_with_status(VteTerminal *, int status) {
gtk_main_quit();
exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
}
static void exit_with_success(VteTerminal *) {
gtk_main_quit();
exit(EXIT_SUCCESS);
}
static char *get_user_shell_with_fallback() {
if (const char *env = g_getenv("SHELL") ) {
if (!((env != NULL) && (env[0] == '\0')))
return g_strdup(env);
}
if (char *command = vte_get_user_shell()) {
if (!((command != NULL) && (command[0] == '\0')))
return command;
}
return g_strdup("/bin/sh");
}
static void on_alpha_screen_changed(GtkWindow *window, GdkScreen *, void *) {
GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(window));
GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
if (!visual)
visual = gdk_screen_get_system_visual(screen);
gtk_widget_set_visual(GTK_WIDGET(window), visual);
}
int main(int argc, char **argv) {
GError *error = nullptr;
const char *const term = "xterm-termite";
char *directory = nullptr;
gboolean version = FALSE, hold = FALSE;
GOptionContext *context = g_option_context_new(nullptr);
char *role = nullptr, *execute = nullptr, *config_file = nullptr;
char *title = nullptr, *icon = nullptr;
const GOptionEntry entries[] = {
{"version", 'v', 0, G_OPTION_ARG_NONE, &version, "Version info", nullptr},
{"exec", 'e', 0, G_OPTION_ARG_STRING, &execute, "Command to execute", "COMMAND"},
{"role", 'r', 0, G_OPTION_ARG_STRING, &role, "The role to use", "ROLE"},
{"title", 't', 0, G_OPTION_ARG_STRING, &title, "Window title", "TITLE"},
{"directory", 'd', 0, G_OPTION_ARG_STRING, &directory, "Change to directory", "DIRECTORY"},
{"hold", 0, 0, G_OPTION_ARG_NONE, &hold, "Remain open after child process exits", nullptr},
{"config", 'c', 0, G_OPTION_ARG_STRING, &config_file, "Path of config file", "CONFIG"},
{"icon", 'i', 0, G_OPTION_ARG_STRING, &icon, "Icon", "ICON"},
{nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr}
};
g_option_context_add_main_entries(context, entries, nullptr);
g_option_context_add_group(context, gtk_get_option_group(TRUE));
if (!g_option_context_parse(context, &argc, &argv, &error)) {
g_printerr("option parsing failed: %s\n", error->message);
g_clear_error (&error);
return EXIT_FAILURE;
}
g_option_context_free(context);
if (version) {
g_print("termite %s\n", TERMITE_VERSION);
return EXIT_SUCCESS;
}
if (directory) {
if (chdir(directory) == -1) {
perror("chdir");
return EXIT_FAILURE;
}
g_free(directory);
}
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *vte_widget = vte_terminal_new();
VteTerminal *vte = VTE_TERMINAL(vte_widget);
if (role) {
gtk_window_set_role(GTK_WINDOW(window), role);
g_free(role);
}
char **command_argv;
char *default_argv[2] = {nullptr, nullptr};
if (execute) {
int argcp;
char **argvp;
g_shell_parse_argv(execute, &argcp, &argvp, &error);
if (error) {
g_printerr("failed to parse command: %s\n", error->message);
return EXIT_FAILURE;
}
command_argv = argvp;
} else {
default_argv[0] = get_user_shell_with_fallback();
command_argv = default_argv;
}
keybind_info info {
GTK_WINDOW(window), vte,
{FALSE, FALSE, FALSE, FALSE, FALSE, config_file, 0},
gtk_window_fullscreen
};
load_config(GTK_WINDOW(window), vte, &info.config, icon ? nullptr : &icon);
reload_config = [&]{
load_config(GTK_WINDOW(window), vte, &info.config, nullptr);
};
signal(SIGUSR1, [](int){ reload_config(); });
gtk_container_add(GTK_CONTAINER(window), vte_widget);
gtk_window_set_decorated(GTK_WINDOW(window), false);
if (!hold) {
g_signal_connect(vte, "child-exited", G_CALLBACK(exit_with_status), nullptr);
}
g_signal_connect(window, "destroy", G_CALLBACK(exit_with_success), nullptr);
g_signal_connect(vte, "key-press-event", G_CALLBACK(key_press_cb), &info);
g_signal_connect(vte, "bell", G_CALLBACK(bell_cb), &info.config.urgent_on_bell);
g_signal_connect(window, "focus-in-event", G_CALLBACK(focus_cb), nullptr);
g_signal_connect(window, "focus-out-event", G_CALLBACK(focus_cb), nullptr);
on_alpha_screen_changed(GTK_WINDOW(window), nullptr, nullptr);
g_signal_connect(window, "screen-changed", G_CALLBACK(on_alpha_screen_changed), nullptr);
if (info.config.fullscreen) {
g_signal_connect(window, "window-state-event", G_CALLBACK(window_state_cb), &info);
}
if (title) {
info.config.dynamic_title = FALSE;
gtk_window_set_title(GTK_WINDOW(window), title);
g_free(title);
} else {
g_signal_connect(vte, "window-title-changed", G_CALLBACK(window_title_cb),
&info.config.dynamic_title);
if (execute) {
gtk_window_set_title(GTK_WINDOW(window), execute);
} else {
window_title_cb(vte, &info.config.dynamic_title);
}
}
if (icon) {
gtk_window_set_icon_name(GTK_WINDOW(window), icon);
g_free(icon);
}
gtk_widget_grab_focus(vte_widget);
gtk_widget_show_all(window);
char **env = g_get_environ();
env = g_environ_setenv(env, "TERM", term, TRUE);
GPid child_pid;
if (vte_terminal_spawn_sync(vte, VTE_PTY_DEFAULT, nullptr, command_argv, env,
G_SPAWN_SEARCH_PATH, nullptr, nullptr, &child_pid, nullptr,
&error)) {
vte_terminal_watch_child(vte, child_pid);
} else {
g_printerr("the command failed to run: %s\n", error->message);
return EXIT_FAILURE;
}
int width, height, padding_left, padding_top, padding_right, padding_bottom;
const long char_width = vte_terminal_get_char_width(vte);
const long char_height = vte_terminal_get_char_height(vte);
gtk_window_get_size(GTK_WINDOW(window), &width, &height);
get_vte_padding(vte, &padding_left, &padding_top, &padding_right, &padding_bottom);
vte_terminal_set_size(vte,
(width - padding_left - padding_right) / char_width,
(height - padding_top - padding_bottom) / char_height);
g_strfreev(env);
gtk_main();
return EXIT_FAILURE; // child process did not cause termination
}
// vim: et:sts=4:sw=4:cino=(0:cc=100