-
Notifications
You must be signed in to change notification settings - Fork 72
/
app.cc
3959 lines (3401 loc) · 162 KB
/
app.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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2001,2002 Red Hat, Inc.
* Copyright © 2014, 2017 Christian Persch
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <locale.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gi18n.h>
#include <glib-unix.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gtk/gtk.h>
#include <cairo/cairo-gobject.h>
#include <vte/vte.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include "std-glue.hh"
#include "cairo-glue.hh"
#include "glib-glue.hh"
#include "gtk-glue.hh"
#include "libc-glue.hh"
#include "pango-glue.hh"
#include "pcre2-glue.hh"
#include "refptr.hh"
#include "vte-glue.hh"
/* options */
static void G_GNUC_PRINTF(2, 3)
verbose_fprintf(FILE* fp,
char const* format,
...);
#define verbose_print(...) verbose_fprintf(stdout, __VA_ARGS__)
#define verbose_printerr(...) verbose_fprintf(stderr, __VA_ARGS__)
#define CONFIG_GROUP "VteApp Configuration"
static consteval auto
gtk_if(auto v3,
auto v4) noexcept -> auto {
if constexpr (VTE_GTK == 3)
return v3;
else if constexpr (VTE_GTK == 4)
return v4;
else
__builtin_unreachable();
}
class Options {
public:
gboolean allow_window_ops{false};
gboolean audible_bell{false};
gboolean a11y{gtk_if(true, false)};
gboolean backdrop{false};
gboolean bidi{true};
gboolean bold_is_bright{false};
gboolean bold{true};
gboolean builtin_dingus{true};
gboolean console{false};
gboolean context_menu{true};
gboolean debug{false};
gboolean decorations{true};
gboolean fallback_scrolling{true};
gboolean feed_stdin{false};
gboolean geometry_hints{true};
gboolean hyperlink{true};
gboolean icon_title{false};
gboolean keep{false};
gboolean kinetic_scrolling{true};
gboolean object_notifications{false};
gboolean overlay_scrollbar{false};
gboolean pty{true};
gboolean require_systemd_scope{false};
gboolean reverse{false};
gboolean rewrap{true};
gboolean scroll_on_insert{false};
gboolean scroll_on_keystroke{true};
gboolean scroll_on_output{false};
gboolean scroll_unit_is_pixels{false};
gboolean scrollbar{true};
gboolean shaping{true};
gboolean shell{true};
gboolean sixel{true};
gboolean systemd_scope{true};
gboolean test_mode{false};
gboolean track_clipboard_targets{false};
gboolean use_scrolled_window{false};
gboolean use_theme_colors{false};
gboolean version{false};
gboolean whole_window_transparent{false};
gboolean xfill{true};
gboolean yfill{true};
bool bg_color_set{false};
bool fg_color_set{false};
bool cursor_bg_color_set{false};
bool cursor_fg_color_set{false};
bool hl_bg_color_set{false};
bool hl_fg_color_set{false};
cairo_extend_t background_extend{CAIRO_EXTEND_NONE};
char* command{nullptr};
char* encoding{nullptr};
char* font_string{nullptr};
char* geometry{nullptr};
char* output_filename{nullptr};
char* title{nullptr};
char* word_char_exceptions{nullptr};
char* working_directory{nullptr};
char** dingus{nullptr};
char** exec_argv{nullptr};
char** environment{nullptr};
GdkPixbuf* background_pixbuf{nullptr};
GdkRGBA bg_color{1.0, 1.0, 1.0, 1.0};
GdkRGBA fg_color{0.0, 0.0, 0.0, 1.0};
GdkRGBA cursor_bg_color{};
GdkRGBA cursor_fg_color{};
GdkRGBA hl_bg_color{};
GdkRGBA hl_fg_color{};
int cjk_ambiguous_width{1};
int extra_margin{-1};
int scrollback_lines{-1 /* infinite */};
int transparency_percent{-1};
int verbosity{0};
double cell_height_scale{1.0};
double cell_width_scale{1.0};
VteCursorBlinkMode cursor_blink_mode{VTE_CURSOR_BLINK_SYSTEM};
VteCursorShape cursor_shape{VTE_CURSOR_SHAPE_BLOCK};
VteTextBlinkMode text_blink_mode{VTE_TEXT_BLINK_ALWAYS};
VteAlign xalign{VteAlign(-1)};
VteAlign yalign{VteAlign(-1)};
vte::glib::RefPtr<GtkCssProvider> css{};
#if VTE_GTK == 3
gboolean argb_visual{true};
gboolean double_buffer{true};
#endif /* VTE_GTK == 3 */
~Options() {
g_clear_object(&background_pixbuf);
g_free(command);
g_free(encoding);
g_free(font_string);
g_free(geometry);
g_free(output_filename);
g_free(word_char_exceptions);
g_free(working_directory);
g_strfreev(dingus);
g_strfreev(exec_argv);
g_strfreev(environment);
}
auto fds()
{
auto fds = std::vector<int>{};
fds.reserve(m_fds.size());
for (auto& fd : m_fds)
fds.emplace_back(fd.get());
return fds;
}
auto map_fds()
{
return m_map_fds;
}
auto environment_for_spawn() const noexcept
{
auto envv = g_get_environ();
// Merge in extra variables
if (environment) {
for (auto i = 0; environment[i]; ++i) {
auto const eq = strchr(environment[i], '=');
if (eq) {
auto const var = vte::glib::take_string(g_strndup(environment[i], eq - environment[i]));
envv = g_environ_setenv(envv, var.get(), eq + 1, true);
} else {
envv = g_environ_unsetenv(envv, environment[i]);
}
}
}
// Cleanup environment
// List of variables and prefixes copied from gnome-terminal.
for (auto const& var : {"COLORTERM",
"COLUMNS",
"DESKTOP_STARTUP_ID",
"EXIT_CODE",
"EXIT_STATUS",
"GIO_LAUNCHED_DESKTOP_FILE",
"GIO_LAUNCHED_DESKTOP_FILE_PID",
"GJS_DEBUG_OUTPUT",
"GJS_DEBUG_TOPICS",
"GNOME_DESKTOP_ICON",
"INVOCATION_ID",
"JOURNAL_STREAM",
"LINES",
"LISTEN_FDNAMES",
"LISTEN_FDS",
"LISTEN_PID",
"MAINPID",
"MANAGERPID",
"NOTIFY_SOCKET",
"NOTIFY_SOCKET",
"PIDFILE",
"PWD",
"REMOTE_ADDR",
"REMOTE_PORT",
"SERVICE_RESULT",
"SHLVL",
"TERM",
"VTE_VERSION",
"WATCHDOG_PID",
"WATCHDOG_USEC",
"WINDOWID"}) {
envv = g_environ_unsetenv(envv, var);
}
for (auto const& prefix : {"GNOME_TERMINAL_",
// other terminals
"FOOT_",
"ITERM2_",
"MC_",
"MINTTY_",
"PUTTY_",
"RXVT_",
"TERM_",
"URXVT_",
"WEZTERM_",
"XTERM_"}) {
for (auto i = 0; envv[i]; ++i) {
if (!g_str_has_prefix (envv[i], prefix))
continue;
auto const eq = strchr(envv[i], '=');
g_assert(eq);
auto const var = vte::glib::take_string(g_strndup(envv[i], eq - envv[i]));
envv = g_environ_unsetenv(envv, var.get());
}
}
return vte::glib::take_strv(envv);
}
private:
std::vector<vte::libc::FD> m_fds{};
std::vector<int> m_map_fds{};
bool parse_enum(GType type,
char const* str,
int& value,
GError** error)
{
GEnumClass* enum_klass = reinterpret_cast<GEnumClass*>(g_type_class_ref(type));
GEnumValue* enum_value = g_enum_get_value_by_nick(enum_klass, str);
if (enum_value == nullptr) {
g_type_class_unref(enum_klass);
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Failed to parse enum value \"%s\" as type \"%s\"",
str, g_quark_to_string(g_type_qname(type)));
return false;
}
value = enum_value->value;
g_type_class_unref(enum_klass);
return true;
}
bool parse_width_enum(char const* str,
int& value,
GError** error)
{
int v = 1;
if (str == nullptr || g_str_equal(str, "narrow"))
v = 1;
else if (g_str_equal(str, "wide"))
v = 2;
else {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Failed to parse \"%s\" as width (allowed values are \"narrow\" or \"wide\")", str);
return false;
}
value = v;
return true;
}
bool parse_color(char const* str,
GdkRGBA* value,
bool* value_set,
GError** error)
{
GdkRGBA color;
if (!gdk_rgba_parse(&color, str)) {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Failed to parse \"%s\" as color", str);
if (value_set)
*value_set = false;
return false;
}
*value = color;
if (value_set)
*value_set = true;
return true;
}
int parse_fd_arg(char const* arg,
char** end_ptr,
GError** error)
{
errno = 0;
char* end = nullptr;
auto const v = g_ascii_strtoll(arg, &end, 10);
if (errno || end == arg || v < G_MININT || v > G_MAXINT) {
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Failed to parse \"%s\" as file descriptor number", arg);
return -1;
}
if (v == -1) {
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"\"%s\" is not a valid file descriptor number", arg);
return -1;
}
if (end_ptr) {
*end_ptr = end;
} else if (*end) {
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Extra characters after number in \"%s\"", arg);
return -1;
}
return int(v);
}
bool parse_fd_arg(char const* str,
GError** error)
{
char *end = nullptr;
auto fd = parse_fd_arg(str, &end, error);
if (fd == -1)
return FALSE;
auto map_to = int{};
if (*end == '=' || *end == ':') {
map_to = parse_fd_arg(end + 1, nullptr, error);
if (map_to == -1)
return false;
if (map_to == STDIN_FILENO ||
map_to == STDOUT_FILENO ||
map_to == STDERR_FILENO) {
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Cannot map file descriptor to %d (reserved)", map_to);
return false;
}
} else if (*end) {
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Failed to parse \"%s\" as file descriptor assignment", str);
return false;
} else {
map_to = fd;
}
/* N:M assigns, N=M assigns a dup of N. Always dup stdin/out/err since
* we need to output messages ourself there, too.
*/
auto new_fd = int{};
if (*end == '=' || fd < 3) {
new_fd = vte::libc::fd_dup_cloexec(fd, 3);
if (new_fd == -1) {
auto errsv = vte::libc::ErrnoSaver{};
g_set_error (error, G_IO_ERROR, g_io_error_from_errno(errsv),
"Failed to duplicate file descriptor %d: %s",
fd, g_strerror(errsv));
return false;
}
} else {
new_fd = fd;
if (vte::libc::fd_set_cloexec(fd) == -1) {
auto errsv = vte::libc::ErrnoSaver{};
g_set_error (error, G_IO_ERROR, g_io_error_from_errno(errsv),
"Failed to set cloexec on file descriptor %d: %s",
fd, g_strerror(errsv));
return false;
}
}
m_fds.emplace_back(new_fd);
m_map_fds.emplace_back(map_to);
return true;
}
bool parse_geometry(char const* str,
GError** error)
{
g_free(geometry);
geometry = g_strdup(str);
return true;
}
#if VTE_GTK == 4
static void
parse_css_error_cb(GtkCssProvider* provider,
void* section,
GError* error,
GError** ret_error) noexcept
{
if (!error)
return;
if (error->domain == GTK_CSS_PARSER_WARNING)
verbose_printerr("Warning parsing CSS: %s", error->message);
else
*ret_error = g_error_copy(error);
}
#endif /* VTE_GTK == 4 */
bool
parse_css(char const* value,
GError** error)
{
auto provider = vte::glib::take_ref(gtk_css_provider_new());
#if VTE_GTK == 3
if (!gtk_css_provider_load_from_path(provider.get(), value, error))
return false;
g_object_set_data_full(G_OBJECT(provider.get()), "VTEAPP_PATH",
g_strdup(value), GDestroyNotify(g_free));
#elif VTE_GTK == 4
GError* err = nullptr;
auto const id = g_signal_connect(provider.get(), "parsing-error",
G_CALLBACK(parse_css_error_cb), &err);
gtk_css_provider_load_from_path(provider.get(), value);
g_signal_handler_disconnect(provider.get(), id);
if (err) {
g_propagate_prefixed_error(error, err,
"Error parsing CSS file \"%s\": ",
value);
return false;
}
#endif /* VTE_GTK */
css = std::move(provider);
return true;
}
static gboolean
parse_background_image(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
g_clear_object(&that->background_pixbuf);
that->background_pixbuf = gdk_pixbuf_new_from_file(value, error);
if (that->background_pixbuf)
g_object_set_data_full(G_OBJECT(that->background_pixbuf), "VTEAPP_PATH",
g_strdup(value), GDestroyNotify(g_free));
return that->background_pixbuf != nullptr;
}
static gboolean
parse_background_extend(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
int v;
auto rv = that->parse_enum(CAIRO_GOBJECT_TYPE_EXTEND, value, v, error);
if (rv)
that->background_extend = cairo_extend_t(v);
return rv;
}
static gboolean
parse_cjk_width(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
return that->parse_width_enum(value, that->cjk_ambiguous_width, error);
};
static gboolean
parse_cursor_blink(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
int v;
auto rv = that->parse_enum(VTE_TYPE_CURSOR_BLINK_MODE, value, v, error);
if (rv)
that->cursor_blink_mode = VteCursorBlinkMode(v);
return rv;
}
static gboolean
parse_cursor_shape(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
int v;
auto rv = that->parse_enum(VTE_TYPE_CURSOR_SHAPE, value, v, error);
if (rv)
that->cursor_shape = VteCursorShape(v);
return rv;
}
static gboolean
parse_bg_color(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
bool set;
return that->parse_color(value, &that->bg_color, &set, error);
}
static gboolean
parse_css_file(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
return that->parse_css(value, error);
}
static gboolean
parse_fd(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
return that->parse_fd_arg(value, error);
}
static gboolean
parse_fg_color(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
bool set;
return that->parse_color(value, &that->fg_color, &set, error);
}
static gboolean
parse_cursor_bg_color(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
return that->parse_color(value, &that->cursor_bg_color, &that->cursor_bg_color_set, error);
}
static gboolean
parse_cursor_fg_color(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
return that->parse_color(value, &that->cursor_fg_color, &that->cursor_fg_color_set, error);
}
static gboolean
parse_hl_bg_color(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
return that->parse_color(value, &that->hl_bg_color, &that->hl_bg_color_set, error);
}
static gboolean
parse_hl_fg_color(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
return that->parse_color(value, &that->hl_fg_color, &that->hl_fg_color_set, error);
}
static gboolean
parse_text_blink(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
int v;
auto rv = that->parse_enum(VTE_TYPE_TEXT_BLINK_MODE, value, v, error);
if (rv)
that->text_blink_mode = VteTextBlinkMode(v);
return rv;
}
static gboolean
parse_verbosity(char const* option, char const* value, void* data, GError** error)
{
Options* that = static_cast<Options*>(data);
that->verbosity++;
return true;
}
static gboolean
parse_xalign(char const* option, char const* value, void* data, GError** error)
{
auto const that = static_cast<Options*>(data);
auto v = int{};
auto const rv = that->parse_enum(VTE_TYPE_ALIGN, value, v, error);
if (rv)
that->xalign = VteAlign(v);
return rv;
}
static gboolean
parse_yalign(char const* option, char const* value, void* data, GError** error)
{
auto const that = static_cast<Options*>(data);
auto v = int{};
auto const rv = that->parse_enum(VTE_TYPE_ALIGN, value, v, error);
if (rv)
that->yalign = VteAlign(v);
return rv;
}
char const*
default_config_path() {
auto const dh = g_get_user_config_dir();
auto ini_path = vte::glib::take_string(g_build_filename(dh, "vteapp.ini", nullptr));
return g_intern_string(ini_path.get());
}
bool
load_config(GKeyFile* keyfile,
char const* path,
GError** error)
{
if (!g_key_file_load_from_file(keyfile,
path ? path : default_config_path(),
GKeyFileFlags(G_KEY_FILE_KEEP_COMMENTS |
G_KEY_FILE_KEEP_TRANSLATIONS),
error))
return false;
// Load the config from the keyfile. Don't error out on invalid
// data, just ignore it.
auto load_bool_option = [&](char const* key,
gboolean* ptr) noexcept -> void
{
auto err = vte::glib::Error{};
auto const v = g_key_file_get_boolean(keyfile, CONFIG_GROUP, key, err);
if (!err.error())
*ptr = v != false;
};
load_bool_option("AllowWindowOps", &allow_window_ops);
load_bool_option("AudibleBell", &audible_bell);
load_bool_option("Accessibility", &a11y);
load_bool_option("Backdrop", &backdrop);
load_bool_option("BboldIsBright", &bold_is_bright);
load_bool_option("BiDi", &bidi);
load_bool_option("Bold", &bold);
load_bool_option("BuiltinDingus", &builtin_dingus);
load_bool_option("ContextMenu", &context_menu);
load_bool_option("Debug", &debug);
load_bool_option("Decorations", &decorations);
load_bool_option("FallbackScrolling", &fallback_scrolling);
load_bool_option("GeometryHints", &geometry_hints);
load_bool_option("Hyperlink", &hyperlink);
load_bool_option("Keep", &keep);
load_bool_option("KineticScrolling", &kinetic_scrolling);
load_bool_option("ObjectNotifications", &object_notifications);
load_bool_option("OverlayScrollbar", &overlay_scrollbar);
load_bool_option("Pty", &pty);
load_bool_option("RequireSystemdScope", &require_systemd_scope);
load_bool_option("Reverse", &reverse);
load_bool_option("Rewrap", &rewrap);
load_bool_option("ScrollUnitIsPixels", &scroll_unit_is_pixels);
load_bool_option("Scrollbar", &scrollbar);
load_bool_option("ScrolledWindow", &use_scrolled_window);
load_bool_option("Shaping", &shaping);
load_bool_option("Shell", &shell);
load_bool_option("Sixel", &sixel);
load_bool_option("SystemdScope", &systemd_scope);
load_bool_option("TestMode", &test_mode);
load_bool_option("TrackClipboardTargets", &track_clipboard_targets);
load_bool_option("UseThemeColors", &use_theme_colors);
load_bool_option("WholeWindowTransparent", &whole_window_transparent);
load_bool_option("XFill", &xfill);
load_bool_option("YFill", &yfill);
#if VTE_GTK == 3
load_bool_option("ArgbVisual", &argb_visual);
load_bool_option("DoubleBuffer", &double_buffer);
#endif /* VTE_GTK == 3 */
auto load_color_option = [&](char const* key,
GdkRGBA* ptr,
bool* setptr) noexcept -> void
{
auto str = vte::glib::take_string
(g_key_file_get_string(keyfile, CONFIG_GROUP, key, nullptr));
if (!str)
return;
parse_color(str.get(), ptr, setptr, nullptr);
};
load_color_option("BackgroundColor", &bg_color, &bg_color_set);
load_color_option("CursorBackgroundColor", &cursor_bg_color, &cursor_bg_color_set);
load_color_option("CursorForegroundColor", &cursor_fg_color, &cursor_fg_color_set);
load_color_option("ForegroundColor", &fg_color, &fg_color_set);
load_color_option("HighlightBackgroundColor", &hl_bg_color, &hl_bg_color_set);
load_color_option("HighlightForegroundColor", &hl_fg_color, &hl_fg_color_set);
auto load_double_option = [&](char const* key,
double* ptr) noexcept -> void
{
auto err = vte::glib::Error{};
auto const v = g_key_file_get_double(keyfile, CONFIG_GROUP, key, err);
if (!err.error() &&
std::isfinite(v))
*ptr = v;
};
load_double_option("CellHeightScale", &cell_height_scale);
load_double_option("CellWidthScale", &cell_width_scale);
auto load_enum_option = [&](char const* key,
GType enum_type,
auto* ptr) noexcept -> void
{
auto str = vte::glib::take_string
(g_key_file_get_string(keyfile, CONFIG_GROUP, key, nullptr));
if (!str)
return;
auto v = 0;
if (parse_enum(enum_type, str.get(), v, nullptr))
*ptr = (decltype(*ptr))v;
};
load_enum_option("BackgroundExtend", CAIRO_GOBJECT_TYPE_EXTEND, &background_extend);
load_enum_option("TextBlink", VTE_TYPE_TEXT_BLINK_MODE, &text_blink_mode);
load_enum_option("CursorBlink", VTE_TYPE_CURSOR_BLINK_MODE, &cursor_blink_mode);
load_enum_option("CursorShape", VTE_TYPE_CURSOR_SHAPE, &cursor_shape);
load_enum_option("XAlign", VTE_TYPE_ALIGN, &xalign);
load_enum_option("YAlign", VTE_TYPE_ALIGN, &yalign);
auto load_int_option = [&](char const* key,
int* ptr) noexcept -> void
{
auto err = vte::glib::Error{};
auto v = g_key_file_get_integer(keyfile, CONFIG_GROUP, key, err);
if (!err.error())
*ptr = v;
};
load_int_option("ExtraMargin", &extra_margin);
load_int_option("ScrollbackLines", &scrollback_lines);
load_int_option("Transparent", &transparency_percent); // 0..100
load_int_option("Verbosity", &verbosity);
auto load_string_option = [&](char const* key,
char** ptr,
bool raw = false) noexcept -> void
{
auto str = vte::glib::take_string
(g_key_file_get_string(keyfile, CONFIG_GROUP, key, nullptr));
// FIXME: decode to allow non-UTF-8 if @raw
if (*ptr)
g_free(*ptr);
*ptr = str.release();
};
load_string_option("Encoding", &encoding);
load_string_option("Font", &font_string);
load_string_option("Geometry", &geometry);
load_string_option("Title", &title);
load_string_option("WordCharExceptions", &word_char_exceptions);
load_string_option("WorkingDirectory", &working_directory, true);
auto load_strv_option = [&](char const* key,
char*** ptr,
bool raw = false) noexcept -> void
{
auto len = gsize{0};
auto strv = vte::glib::take_strv
(g_key_file_get_string_list(keyfile, CONFIG_GROUP, key, &len, nullptr));
// FIXME: decode to allow non-UTF-8 if @raw
if (*ptr)
g_strfreev(*ptr);
*ptr = strv.release();
};
load_strv_option("Dingu", &dingus, false);
load_strv_option("Env", &environment, true);
// load bgimage option
{
auto str = vte::glib::take_string
(g_key_file_get_string(keyfile, CONFIG_GROUP, "BackgroundImage", nullptr));
if (str) {
g_clear_object(&background_pixbuf);
background_pixbuf = gdk_pixbuf_new_from_file(str.get(), nullptr);
if (background_pixbuf)
g_object_set_data_full(G_OBJECT(background_pixbuf), "VTEAPP_PATH",
g_strdup(str.get()), GDestroyNotify(g_free));
}
}
// load CJK width option
{
auto str = vte::glib::take_string
(g_key_file_get_string(keyfile, CONFIG_GROUP, "CJKWidth", nullptr));
if (str) {
auto v = 1;
if (parse_width_enum(str.get(), v, nullptr))
cjk_ambiguous_width = v;
}
};
// load css option
{
auto str = vte::glib::take_string
(g_key_file_get_string(keyfile, CONFIG_GROUP, "CssFile", nullptr));
if (str)
parse_css(str.get(), nullptr);
};
// Note that the following command line options don't
// have a config file equivalent:
// --fd, --feed-stdin, --output-file, --version
return true;
}
bool
save_config(GKeyFile* keyfile,
char const* save_path,
GError** error)
{
// Store the config into the keyfile.
auto const defopt = Options{}; // for default values
auto save_bool_option = [&](char const* key,
bool v,
bool dv) noexcept -> void
{
if (v == dv)
return;
g_key_file_set_boolean(keyfile, CONFIG_GROUP, key, v);
};
save_bool_option("AllowWindowOps" , allow_window_ops, defopt.allow_window_ops);
save_bool_option("AudibleBell" , audible_bell, defopt.audible_bell);
save_bool_option("Accessibility", a11y, defopt.a11y);
save_bool_option("Backdrop" , backdrop, defopt.backdrop);
save_bool_option("BboldIsBright" , bold_is_bright, defopt.bold_is_bright);
save_bool_option("BiDi" , bidi, defopt.bidi);
save_bool_option("Bold" , bold, defopt.bold);
save_bool_option("BuiltinDingus" , builtin_dingus, defopt.builtin_dingus);
save_bool_option("ContextMenu" , context_menu, defopt.context_menu);
save_bool_option("Debug" , debug, defopt.debug);
save_bool_option("Decorations" , decorations, defopt.decorations);
save_bool_option("FallbackScrolling" , fallback_scrolling, defopt.fallback_scrolling);
save_bool_option("GeometryHints" , geometry_hints, defopt.geometry_hints);
save_bool_option("Hyperlink" , hyperlink, defopt.hyperlink);
save_bool_option("Keep" , keep, defopt.keep);
save_bool_option("KineticScrolling" , kinetic_scrolling, defopt.kinetic_scrolling);
save_bool_option("ObjectNotifications" , object_notifications, defopt.object_notifications);
save_bool_option("OverlayScrollbar" , overlay_scrollbar, defopt.overlay_scrollbar);
save_bool_option("Pty" , pty, defopt.pty);
save_bool_option("RequireSystemdScope" , require_systemd_scope, defopt.require_systemd_scope);
save_bool_option("Reverse" , reverse, defopt.reverse);
save_bool_option("Rewrap" , rewrap, defopt.rewrap);
save_bool_option("ScrollUnitIsPixels" , scroll_unit_is_pixels, defopt.scroll_unit_is_pixels);
save_bool_option("Scrollbar" , scrollbar, defopt.scrollbar);
save_bool_option("ScrolledWindow" , use_scrolled_window, defopt.use_scrolled_window);
save_bool_option("Shaping" , shaping, defopt.shaping);
save_bool_option("Shell" , shell, defopt.shell);
save_bool_option("Sixel" , sixel, defopt.sixel);
save_bool_option("SystemdScope" , systemd_scope, defopt.systemd_scope);
save_bool_option("TestMode" , test_mode, defopt.test_mode);
save_bool_option("TrackClipboardTargets" , track_clipboard_targets, defopt.track_clipboard_targets);
save_bool_option("UseThemeColors" , use_theme_colors, defopt.use_theme_colors);
save_bool_option("WholeWindowTransparent" , whole_window_transparent, defopt.whole_window_transparent);
save_bool_option("XFill" , xfill, defopt.xfill);
save_bool_option("YFill" , yfill, defopt.yfill);
#if VTE_GTK == 3
save_bool_option("ArgbVisual" , argb_visual, defopt.argb_visual);
save_bool_option("DoubleBuffer" , double_buffer, defopt.double_buffer);
#endif /* VTE_GTK == 3 */
auto save_color_option = [&](char const* key,
GdkRGBA const& color,
bool set) noexcept -> void
{
if (!set)
return;
auto str = vte::glib::take_string(gdk_rgba_to_string(&color));
g_key_file_set_string(keyfile, CONFIG_GROUP, key, str.get());
};
save_color_option("BackgroundColor", bg_color, bg_color_set);
save_color_option("CursorBackgroundColor", cursor_bg_color, cursor_bg_color_set);
save_color_option("CursorForegroundColor", cursor_fg_color, cursor_fg_color_set);
save_color_option("ForegroundColor", fg_color, fg_color_set);
save_color_option("HighlightBackgroundColor", hl_bg_color, hl_bg_color_set);
save_color_option("HighlightForegroundColor", hl_fg_color, hl_fg_color_set);
auto save_double_option = [&](char const* key,
double v,
double dv) noexcept -> void
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (v == dv)
return;
#pragma GCC diagnostic pop
g_key_file_set_double(keyfile, CONFIG_GROUP, key, v);
};
save_double_option("CellHeightScale" , cell_height_scale, defopt.cell_height_scale);
save_double_option("CellWidthScale" , cell_width_scale, defopt.cell_width_scale);
auto save_enum_option = [&](char const* key,
GType enum_type,
auto v,
auto dv) noexcept -> void
{
if (v == dv)
return;
auto enum_klass = reinterpret_cast<GEnumClass*>(g_type_class_ref(enum_type));
if (auto const ev = g_enum_get_value(enum_klass, int(v))) {
g_key_file_set_string(keyfile, CONFIG_GROUP, key, ev->value_nick);
}
g_type_class_unref(enum_klass);
};
save_enum_option("BackgroundExtend" , CAIRO_GOBJECT_TYPE_EXTEND, background_extend, defopt.background_extend);
save_enum_option("TextBlink" , VTE_TYPE_TEXT_BLINK_MODE, text_blink_mode, defopt.text_blink_mode);
save_enum_option("CursorBlink" , VTE_TYPE_CURSOR_BLINK_MODE, cursor_blink_mode, defopt.cursor_blink_mode);
save_enum_option("CursorShape" , VTE_TYPE_CURSOR_SHAPE, cursor_shape, defopt.cursor_shape);
save_enum_option("XAlign" , VTE_TYPE_ALIGN, xalign, defopt.xalign);
save_enum_option("YAlign" , VTE_TYPE_ALIGN, yalign, defopt.yalign);
auto save_int_option = [&](char const* key,
int v,
int dv) noexcept -> void
{
if (v == dv)
return;
g_key_file_set_integer(keyfile, CONFIG_GROUP, key, v);
};
save_int_option("ExtraMargin" , extra_margin, defopt.extra_margin);
save_int_option("ScrollbackLines" , scrollback_lines, defopt.scrollback_lines);
save_int_option("Transparent" , transparency_percent, defopt.transparency_percent); // 0..100
save_int_option("Verbosity" , verbosity, defopt.verbosity);
auto save_string_option = [&](char const* key,
char const* v,
char const* dv,
bool raw = false) noexcept -> void
{
if (!v || g_strcmp0(v, dv) == 0)
return;
// FIXME: encode to allow non-UTF-8 if @raw
if (raw && !g_utf8_validate(v, -1, nullptr))
return;
g_key_file_set_string(keyfile, CONFIG_GROUP, key, v);
};
save_string_option("Encoding" , encoding, defopt.encoding);
save_string_option("Font" , font_string, defopt.font_string);
save_string_option("Geometry" , geometry, defopt.geometry);
save_string_option("Title" , title, defopt.title);
save_string_option("WordCharExceptions" , word_char_exceptions, defopt.word_char_exceptions);
save_string_option("WorkingDirectory" , working_directory, defopt.working_directory, true);
auto save_strv_option = [&](char const* key,
char const* const* v,
char const* const* dv,
bool raw = false) noexcept -> void