-
Notifications
You must be signed in to change notification settings - Fork 15
/
zterm.c
1405 lines (1168 loc) · 43.8 KB
/
zterm.c
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
#define _GNU_SOURCE
#include "zterm.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gio/gio.h>
#include <vte/vte.h>
#include <stdbool.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/time.h>
#include <ctype.h>
#define PCRE2_CODE_UNIT_WIDTH 0
#include <pcre2.h>
extern char **environ;
#ifdef HAVE_LIBBSD
# include <bsd/string.h>
#endif
GdkRGBA colors[256] = {
#include "256colors.h"
};
int _vfprintf(FILE *io, const char *fmt, va_list args)
{
// Only bother with timestamps if STDIN is a tty, otherwise we're probably running in a context that ends up in syslog/the systemd journal with it's own timestamp.
if (isatty(0)) {
char buf[32] = { 0 }; // With some extra space, because.
int millisec;
struct tm *tm_info;
struct timeval tv;
gettimeofday(&tv, NULL);
millisec = lrint(tv.tv_usec/1000.0);
if (millisec >= 1000) {
millisec -= 1000;
tv.tv_sec++;
}
tm_info = localtime(&tv.tv_sec);
strftime(buf, sizeof(buf) - 1, "%Y-%m-%d %H:%M:%S", tm_info);
fprintf(io, "%s.%03d: ", buf, millisec);
}
int ret = vfprintf(io, fmt, args);
fflush(io);
return ret;
}
int _fprintf(bool print, FILE *io, const char *fmt, ...)
{
va_list args;
if (!print) {
return 0;
}
va_start (args, fmt);
int ret = _vfprintf(io, fmt, args);
va_end(args);
return ret;
}
// This exists just to ensure that the arguments are always evaluated.
int _fnullf(FILE *io, const char *fmt, ...)
{
return 0;
}
int start_width = 1024;
int start_height = 768;
int char_width = 0;
int char_height = 0;
unsigned int bind_mask = (GDK_MODIFIER_MASK & ~GDK_LOCK_MASK) ^ (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK | GDK_BUTTON4_MASK | GDK_BUTTON5_MASK);;
terms_t terms;
window_t windows[MAX_WINDOWS];
GtkApplication *app;
static gboolean button_event (GtkGesture *gesture, GdkEventSequence *sequence, int64_t term_n, window_t *window);
static gboolean term_button_event (GtkGesture *gesture, GdkEventSequence *sequence, gpointer user_data);
static gboolean window_button_event (GtkGesture *gesture, GdkEventSequence *sequence, gpointer user_data);
int new_window (void);
void destroy_window (int i);
void add_button(GtkWidget *widget, long int term_n, int window_i);
static void
print_widget_size(GtkWidget *widget, const char *name)
{
int x, y;
GtkRequisition minimum, natural;
debugf("%s realized: %d, scale factor: %d", name, gtk_widget_get_realized(widget), gtk_widget_get_scale_factor(widget));
gtk_widget_get_preferred_size(widget, &minimum, &natural);
debugf("%s preferred minimum width x height: %dx%d", name, minimum.width, minimum.height);
debugf("%s preferred natural width x height: %dx%d", name, natural.width, natural.height);
gtk_widget_get_size_request(widget, &x, &y);
debugf("%s size request width x height: %dx%d", name, x, y);
debugf("%s allocated width x height: %dx%d (%x x %x)", name, gtk_widget_get_width(widget), gtk_widget_get_height(widget), gtk_widget_get_width(widget), gtk_widget_get_height(widget));
}
static void
temu_reorder (void)
{
int window_i;
int i, j;
for (window_i = 0; window_i < MAX_WINDOWS; window_i++) {
if (windows[window_i].window) {
for (i = j = 0; i < terms.n_active; i++) {
if (terms.active[i].term && terms.active[i].window == window_i) {
gtk_notebook_reorder_child (windows[window_i].notebook, terms.active[i].term, j++);
}
}
}
}
}
bool
term_find (GtkWidget *term, int *i)
{
for (*i = 0; *i < terms.n_active; *i = *i + 1) {
if (terms.active[*i].term == GTK_WIDGET(term)) {
return true;
}
}
return false; // Default to terminal 0.
}
static void
temu_window_title_change (VteTerminal *terminal, long int n)
{
char window_str[16] = { 0 };
const char *title_str = "zterm";
gchar new_str[64] = { 0 };
int max_windows = 0;
int window_i = terms.active[n].window;
int notebook_i = 0;
for (int i = 0; i < MAX_WINDOWS; i++) {
if (windows[i].window) {
max_windows++;
}
}
if (max_windows != 1) {
snprintf (window_str, sizeof (window_str), "[%d]: ", window_i + 1);
}
#if VTE_CHECK_VERSION(0, 77, 0)
title_str = vte_terminal_get_termprop_string_by_id(terminal, VTE_PROPERTY_ID_XTERM_TITLE, NULL);
#else
title_str = vte_terminal_get_window_title(terminal);
#endif
if (snprintf(new_str, sizeof(new_str) - 1, "%s%s [%ld]", window_str, title_str ? title_str : "zterm", n + 1) < 0) {
return; // Memory allocation issue.
}
gtk_window_set_title(GTK_WINDOW(windows[window_i].window), new_str);
notebook_i = gtk_notebook_page_num (windows[window_i].notebook, GTK_WIDGET (terminal));
if (notebook_i >= 0) {
gtk_notebook_set_menu_label_text (windows[window_i].notebook, GTK_WIDGET (terminal), new_str);
gtk_notebook_set_tab_label_text (windows[window_i].notebook, GTK_WIDGET (terminal), new_str);
}
}
static void
temu_window_title_changed(VteTerminal *terminal, gpointer data)
{
gint n = (long) data;
temu_window_title_change (terminal, n);
}
static void
prune_windows (void)
{
int active = 0;
int pruned = 0;
for (int i = 0; i < MAX_WINDOWS; i++) {
if (windows[i].window) {
int active_pages = gtk_notebook_get_n_pages(windows[i].notebook);
debugf("%d pages for notebook %p of window %d", gtk_notebook_get_n_pages(windows[i].notebook), windows[i].notebook, i);
active += active_pages;
if (active_pages <= 0) {
destroy_window (i);
pruned++;
}
}
}
if (active != terms.alive) {
errorf("Found %d active tabs, but %d terms alive.", active, terms.alive);
}
if (active <= 0) {
debugf("Attempting to exit...");
g_application_quit(G_APPLICATION(app));
}
if (pruned) {
debugf("Pruned %d windows.", pruned);
rebuild_menus();
}
debugf("");
}
static gboolean
term_died (VteTerminal *term, int status)
{
int n = -1, window_i = -1;
if (!term_find(GTK_WIDGET(term), &n)) {
return false;
}
window_i = terms.active[n].window;
if (WIFEXITED(status)) {
infof("Term %d exited. Exit code: %d", n, WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
infof("Term %d killed. Signal: %d", n, WTERMSIG(status));
} else {
infof("Term %d died. Status: %d", n, status);
}
// If the user hits the close button for the window, the terminal may be unrealized before term_died is called.
if (n == -1 || window_i == -1) {
debugf("Unable to find term that died. (%d / %p / %p), terms.active[0].term: %p", status, term, GTK_WIDGET(term), terms.active[0].term);
prune_windows ();
debugf("");
return false;
}
debugf ("Removing dead term %d from window %d.", n, window_i);
int i = gtk_notebook_page_num(windows[window_i].notebook, GTK_WIDGET(term));
if (gtk_notebook_get_current_page(windows[window_i].notebook) == i) {
gtk_notebook_prev_page (windows[window_i].notebook);
}
gtk_widget_set_visible(GTK_WIDGET(term), false);
if (i != -1) {
debugf("About to remove notebook page %d", i);
gtk_notebook_remove_page(windows[window_i].notebook, i);
debugf("");
}
prune_windows ();
debugf("");
return true;
}
static gboolean
term_unrealized (VteTerminal *term, gpointer user_data)
{
int n = (long) user_data;
debugf("Got unrealize for term %d.", n);
if (terms.active[n].moving) {
debugf("Not destroying term %d because we are moving it.", n);
return false;
}
g_object_unref (G_OBJECT(term));
terms.active[n].spawned--;
terms.active[n].term = NULL;
terms.alive--;
if (!terms.alive) {
debugf("Attempting to exit...");
g_application_quit(G_APPLICATION(app));
}
debugf("");
return true;
}
void
term_set_window (int n, int window_i)
{
GtkWidget *term = terms.active[n].term;
// Create a new window if we are passed a non-existant window.
if (!windows[window_i].window) {
window_i = new_window ();
debugf ("Setting term %d to NEW window %d.", n, window_i);
}
// Set the new active window before removing from the old window.
// This is done first so that all of the window titles come out right.
terms.active[n].window = window_i;
/*
* When we remove the terminal from the existing notebook, this may cause
* it to be unrealized.
*
* When that happens, we really don't want it to actually be destroyed, or
* for that to trigger an exit if it's the only terminal.
*
* As such, the moving indicator prevents those things.
*/
terms.active[n].moving++;
// Remove from any previous window.
for (int i = 0; i < MAX_WINDOWS; i++) {
if (windows[i].window) {
if (gtk_notebook_page_num (windows[i].notebook, term) >= 0) {
debugf("Removing term %d from window %d.", n, i);
gtk_notebook_prev_page (windows[i].notebook);
debugf("%d pages for notebook %p of window %d", gtk_notebook_get_n_pages(windows[i].notebook), windows[i].notebook, i);
//gtk_notebook_remove_page(windows[i].notebook, gtk_notebook_page_num(windows[i].notebook, GTK_WIDGET(term)));
gtk_notebook_detach_tab(windows[i].notebook, GTK_WIDGET(term));
debugf("%d pages for notebook %p of window %d", gtk_notebook_get_n_pages(windows[i].notebook), windows[i].notebook, i);
}
}
}
if (terms.color_schemes[windows[window_i].color_scheme].name[0]) {
vte_terminal_set_colors (VTE_TERMINAL (term), &terms.color_schemes[windows[window_i].color_scheme].foreground, &terms.color_schemes[windows[window_i].color_scheme].background, &colors[0], MIN(256, sizeof (colors) / sizeof(colors[0])));
} else {
vte_terminal_set_colors (VTE_TERMINAL (term), NULL, NULL, &colors[0], MIN(256, sizeof (colors) / sizeof(colors[0])));
}
int i = gtk_notebook_append_page (windows[window_i].notebook, term, NULL);
gtk_notebook_set_current_page(windows[window_i].notebook, i);
gtk_widget_set_can_focus(term, true);
gtk_widget_realize(term);
gtk_widget_set_visible(term, true);
gtk_widget_grab_focus(term);
terms.active[n].moving--;
prune_windows ();
temu_reorder ();
}
void
term_config (GtkWidget *term, int window_i)
{
static bool manage_fc_timestamp = false;
if (terms.font) {
/*
* This is all to workaround https://gitlab.gnome.org/GNOME/gtk/-/issues/7039
*
* The very short version: Check to see if gtk-fontconfig-timestamp has been set.
*
* If it has, we can pretend that gtk is going to do the right thing.
*
* It won't until the first bug described in the issue is fixed, but
* that's not too hard for the to workaround. (Make a change, then make
* another change. The first change will then be picked up correctly.)
*
* If it has not been set, then we need to do it ourselves.
*
* Use a static variable to check if we are still managing it.
*
* NOTE: Sadly, just setting gtk-fontconfig-timestamp to different
* values in sequence isn't enough.
*
* FcConfigUptoDate needs to show as false during both increments.
*
* Sadly, we don't really have any way to trigger that.
*
* Without talking to the fontconfig layer of pango ourselves, we don't
* really have any good options here, except for the user to make a
* fontconfig change, hit reload config settings, make a second change,
* and then hit reload config settings again.
*/
int timestamp = 0;
g_object_get(gtk_settings_get_default(), "gtk-fontconfig-timestamp", ×tamp, NULL);
if (timestamp == 0 || manage_fc_timestamp) {
manage_fc_timestamp = true;
timestamp++;
g_object_set(gtk_settings_get_default(), "gtk-fontconfig-timestamp", timestamp, NULL);
}
PangoFontDescription *font = pango_font_description_from_string(terms.font);
if (font) {
vte_terminal_set_font (VTE_TERMINAL (term), font);
pango_font_description_free (font);
} else {
errorf("Unable to load font '%s'", terms.font);
}
}
vte_terminal_set_word_char_exceptions (VTE_TERMINAL (term), terms.word_char_exceptions);
vte_terminal_set_audible_bell (VTE_TERMINAL (term), terms.audible_bell);
vte_terminal_set_font_scale (VTE_TERMINAL (term), terms.font_scale);
vte_terminal_set_scroll_on_output (VTE_TERMINAL (term), terms.scroll_on_output);
vte_terminal_set_scroll_on_keystroke (VTE_TERMINAL (term), terms.scroll_on_keystroke);
vte_terminal_set_bold_is_bright (VTE_TERMINAL (term), terms.bold_is_bright);
vte_terminal_set_cursor_blink_mode (VTE_TERMINAL (term), VTE_CURSOR_BLINK_OFF);
vte_terminal_set_cursor_shape (VTE_TERMINAL (term), VTE_CURSOR_SHAPE_BLOCK);
vte_terminal_set_scrollback_lines (VTE_TERMINAL (term), terms.scrollback_lines);
vte_terminal_set_mouse_autohide (VTE_TERMINAL (term), terms.mouse_autohide);
vte_terminal_set_enable_sixel (VTE_TERMINAL (term), true);
vte_terminal_set_allow_hyperlink (VTE_TERMINAL (term), true);
#if VTE_CHECK_VERSION(0, 77, 0)
vte_terminal_set_enable_legacy_osc777 (VTE_TERMINAL (term), true);
#endif
// FIXME: Should this be in the config?
VteRegex *regex;
regex = vte_regex_new_for_match("([a-z]+://(\\w+(:\\w+)@)?[^\\s]*)", -1, PCRE2_NEVER_BACKSLASH_C | PCRE2_UTF | PCRE2_MULTILINE | PCRE2_CASELESS, NULL);
if (regex) {
int ret = vte_terminal_match_add_regex(VTE_TERMINAL (term), regex, 0);
debugf("regex: %p, ret: %d", regex, ret);
} else {
debugf("regex failed to compile, we should add error handling.");
}
if (terms.color_schemes[windows[window_i].color_scheme].name[0]) {
vte_terminal_set_colors (VTE_TERMINAL (term), &terms.color_schemes[windows[window_i].color_scheme].foreground, &terms.color_schemes[windows[window_i].color_scheme].background, &colors[0], MIN(256, sizeof (colors) / sizeof(colors[0])));
} else {
vte_terminal_set_colors (VTE_TERMINAL (term), NULL, NULL, &colors[0], MIN(256, sizeof (colors) / sizeof(colors[0])));
}
char_width = vte_terminal_get_char_width(VTE_TERMINAL (term));
char_height = vte_terminal_get_char_height(VTE_TERMINAL (term));
debugf("setting size request: %dx%d", char_width * 2, char_height * 2);
gtk_widget_set_size_request (windows[window_i].window, char_width * 2, char_height * 2);
}
static void spawn_callback (VteTerminal *term, GPid pid, GError *error, gpointer user_data)
{
debugf("term: %p, pid: %d, error: %p, user_data: %p", term, pid, error, user_data);
if (error != NULL) {
errorf("error: domain: 0x%x, code: 0x%x, message: %s", error->domain, error->code, error->message);
term_died(term, -1); // This is a horrible hack.
}
}
static gboolean
term_spawn (gpointer data)
{
int n = (long int) data;
int realized = gtk_widget_get_realized(terms.active[n].term);
debugf("For terminal %d, spawned: %d, realized: %d, cmd: %s.", n, terms.active[n].spawned, realized, terms.active[n].cmd);
if (!realized) {
return true;
}
if (!terms.active[n].spawned) {
char **env = environ;
if (terms.active[n].env != NULL) {
env = terms.active[n].env;
}
if (terms.active[n].cmd) {
char *argv[] = {
"/bin/sh",
"-c",
terms.active[n].cmd,
NULL
};
debugf("Spawning with args: %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
vte_terminal_spawn_async (VTE_TERMINAL (terms.active[n].term), VTE_PTY_DEFAULT, NULL, argv, env, G_SPAWN_DEFAULT, NULL, NULL, NULL, -1, NULL, spawn_callback, NULL);
} else if (terms.active[n].argv != NULL && terms.active[n].argv[0] != NULL) {
debugf("Spawning with: %p '%s'", terms.active[n].argv, terms.active[n].argv[0]);
for (int i = 0; terms.active[n].argv[i] != NULL; i++) {
debugf("argv[%d]: '%s'", i, terms.active[n].argv[i]);
}
vte_terminal_spawn_async (VTE_TERMINAL (terms.active[n].term), VTE_PTY_DEFAULT, NULL, terms.active[n].argv, env, G_SPAWN_DEFAULT, NULL, NULL, NULL, -1, NULL, spawn_callback, NULL);
} else {
struct passwd *pass = getpwuid(getuid());
char *argv[] = {
pass->pw_shell,
"--login",
NULL
};
debugf("term: %p, shell: '%s'", VTE_TERMINAL (terms.active[n].term), pass->pw_shell);
debugf("Spawning with args: %s %s", argv[0], argv[1]);
vte_terminal_spawn_async (VTE_TERMINAL (terms.active[n].term), VTE_PTY_DEFAULT, NULL, argv, env, G_SPAWN_DEFAULT, NULL, NULL, NULL, 5000, NULL, spawn_callback, NULL);
}
terms.active[n].spawned++;
add_button(GTK_WIDGET(terms.active[n].term), n, -1);
// Workaround a bug where the cursor may not be drawn when we first switch to a new terminal.
vte_terminal_set_cursor_blink_mode (VTE_TERMINAL (terms.active[n].term), VTE_CURSOR_BLINK_ON);
vte_terminal_set_cursor_blink_mode (VTE_TERMINAL (terms.active[n].term), VTE_CURSOR_BLINK_OFF);
return false;
}
return true;
}
static void
term_show (GtkWidget *widget, void *data)
{
int n = (long int) data;
debugf("Show for terminal %d, spawned: %d, cmd: %s.", n, terms.active[n].spawned, terms.active[n].cmd);
}
static void
term_realized (GtkWidget *widget, void *data)
{
int n = (long int) data;
debugf("For terminal %d, spawned: %d, cmd: %s.", n, terms.active[n].spawned, terms.active[n].cmd);
if (!terms.active[n].spawned) {
g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, &term_spawn, data, NULL);
}
}
static void
term_map (GtkWidget *widget, void *data)
{
int n = (long int) data;
debugf("For terminal %d, spawned: %d, cmd: %s.", n, terms.active[n].spawned, terms.active[n].cmd);
// print_widget_size(GTK_WIDGET(widget), "term");
}
static void term_hover_uri_changed (VteTerminal *term, gchar *uri, GdkRectangle *bbox, gpointer user_data)
{
int64_t n = (int64_t) user_data;
debugf("uri: %s, n: %ld", uri, n);
if (terms.active[n].hyperlink_uri != NULL) {
free(terms.active[n].hyperlink_uri);
terms.active[n].hyperlink_uri = NULL;
}
if (uri != NULL) {
terms.active[n].hyperlink_uri = strdup(uri);
}
}
static void term_resize_window (VteTerminal *term, guint width, guint height, gpointer user_data)
{
int64_t n = (int64_t) user_data;
debugf("target size: %dx%d, term %d", width, height, n);
}
static void term_increase_font_size (VteTerminal *term, gpointer user_data)
{
int64_t n = (int64_t) user_data;
debugf("term %d", n);
}
static void term_decrease_font_size (VteTerminal *term, gpointer user_data)
{
int64_t n = (int64_t) user_data;
debugf("term %d", n);
}
static gboolean term_setup_context_menu (VteTerminal *term, VteEventContext *context, gpointer user_data)
{
int64_t n = (int64_t) user_data;
window_t *window = &windows[terms.active[n].window];
debugf("context: %p, term %d", context, n);
if (window->menu_hyperlink_uri) {
free(window->menu_hyperlink_uri);
window->menu_hyperlink_uri = NULL;
}
if (!context) {
vte_terminal_set_context_menu_model (term, NULL);
window->menu_x = window->menu_y = 0;
return true;
}
vte_event_context_get_coordinates(context, &window->menu_x, &window->menu_y);
if (terms.active[n].hyperlink_uri) {
window->menu_hyperlink_uri = strdup(terms.active[n].hyperlink_uri);
debugf("Setting menu hyperlink uri: %s", window->menu_hyperlink_uri);
}
vte_terminal_set_context_menu_model (term, window->menu_model);
debugf("Done.");
return true;
}
#if VTE_CHECK_VERSION(0, 77, 0)
static void term_termprop_changed (VteTerminal *term, int id, VtePropertyType type, VtePropertyFlags flags, const char *name, int64_t n)
{
const char *resolved_name;
int prop = 0;
gboolean bvalue;
int64_t ivalue;
uint64_t uivalue;
double dvalue;
GdkRGBA color_value;
const char *svalue;
const uint8_t *data_value;
size_t value_len;
if (vte_query_termprop(name, &resolved_name, &prop, &type, &flags)) {
switch (type) {
case VTE_PROPERTY_INVALID:
case VTE_PROPERTY_VALUELESS:
debugf("termprop: %s, type: VALUELESS %d, term: %d", name, type, n);
break;
case VTE_PROPERTY_BOOL:
vte_terminal_get_termprop_bool_by_id(term, prop, &bvalue);
debugf("termprop: %s, type: BOOL %d, value: %d, term: %d", name, type, bvalue, n);
break;
case VTE_PROPERTY_INT:
vte_terminal_get_termprop_int_by_id(term, prop, &ivalue);
debugf("termprop: %s, type: INT %d, value: %d, term: %d", name, type, ivalue, n);
break;
case VTE_PROPERTY_UINT:
vte_terminal_get_termprop_uint_by_id(term, prop, &uivalue);
debugf("termprop: %s, type: UINT %d, value: %d, term: %d", name, type, uivalue, n);
break;
case VTE_PROPERTY_DOUBLE:
vte_terminal_get_termprop_double_by_id(term, prop, &dvalue);
debugf("termprop: %s, type: DOUBLE %d, value: %d, term: %d", name, type, dvalue, n);
break;
case VTE_PROPERTY_RGB:
case VTE_PROPERTY_RGBA:
vte_terminal_get_termprop_rgba_by_id(term, prop, &color_value);
debugf("termprop: %s, type: RGBA %d, value: _, term: %d", name, type, n);
break;
case VTE_PROPERTY_STRING:
svalue = vte_terminal_get_termprop_string_by_id(term, prop, &value_len);
debugf("termprop: %s, type: STRING %d, value: %s, len: %d, term: %d", name, type, svalue, value_len, n);
break;
case VTE_PROPERTY_DATA:
case VTE_PROPERTY_UUID:
data_value = vte_terminal_get_termprop_data_by_id(term, prop, &value_len);
debugf("termprop: %s, type: DATA %d, value: %s, len: %d, term: %d", name, type, data_value, value_len, n);
break;
case VTE_PROPERTY_URI:
GUri *uri_value = vte_terminal_ref_termprop_uri_by_id(term, prop);
if (uri_value == NULL) {
debugf("Unable to get URI.");
break;
}
svalue = g_uri_to_string(uri_value);
debugf("termprop: %s, type: URI %d, value: %s, term: %d", name, type, svalue, n);
free((void *) svalue);
g_uri_unref(uri_value);
break;
}
} else {
debugf("Unable to get termprop info for %s on term %d", name, n);
}
}
static gboolean term_termprops_changed (VteTerminal *term, int const *props, int n_props, gpointer user_data)
{
int64_t n = (int64_t) user_data;
const char *name;
VtePropertyType type;
VtePropertyFlags flags;
debugf("%d termprops changed on term %d.", n_props, n);
for (int i = 0; i < n_props; i++) {
if (vte_query_termprop_by_id(props[i], &name, &type, &flags)) {
debugf("Prop %d / %s: type %d, flags %d", props[i], name, type, flags);
term_termprop_changed (term, props[i], type, flags, name, n);
}
}
return false;
}
#endif
void
term_switch (long n, char *cmd, char **argv, char **env, int window_i)
{
if (n >= terms.n_active) {
errorf("ERROR! Attempting to switch to term %ld, while terms.n_active is %d.", n, terms.n_active);
return;
}
if (!terms.active[n].term) {
GtkWidget *term;
term = vte_terminal_new();
g_object_ref (G_OBJECT(term));
gtk_widget_set_visible(term, true);
gtk_widget_set_hexpand (term, true);
gtk_widget_set_vexpand (term, true);
g_signal_connect_after (G_OBJECT (term), "child-exited", G_CALLBACK (term_died), (void *) n);
g_signal_connect_after (G_OBJECT (term), "unrealize", G_CALLBACK (term_unrealized), (void *) n);
g_signal_connect (G_OBJECT(term), "window_title_changed", G_CALLBACK(temu_window_title_changed), (void *) n);
g_signal_connect_after (G_OBJECT (term), "realize", G_CALLBACK (term_realized), (void *) n);
g_signal_connect_after (G_OBJECT (term), "show", G_CALLBACK (term_show), (void *) n);
g_signal_connect_after (G_OBJECT (term), "map", G_CALLBACK (term_map), (void *) n);
g_signal_connect_after (G_OBJECT (term), "hyperlink_hover_uri_changed", G_CALLBACK (term_hover_uri_changed), (void *) n);
g_signal_connect_after (G_OBJECT (term), "resize_window", G_CALLBACK (term_resize_window), (void *) n);
g_signal_connect_after (G_OBJECT (term), "increase_font_size", G_CALLBACK (term_increase_font_size), (void *) n);
g_signal_connect_after (G_OBJECT (term), "decrease_font_size", G_CALLBACK (term_decrease_font_size), (void *) n);
g_signal_connect (G_OBJECT (term), "setup_context_menu", G_CALLBACK (term_setup_context_menu), (void *) n);
#if VTE_CHECK_VERSION(0, 77, 0)
g_signal_connect (G_OBJECT (term), "termprops_changed", G_CALLBACK (term_termprops_changed), (void *) n);
#endif
terms.active[n].cmd = cmd;
terms.active[n].argv = argv;
terms.active[n].env = env;
terms.active[n].term = term;
terms.alive++;
term_set_window (n, window_i);
term_config(term, window_i);
gtk_window_present (GTK_WINDOW(windows[window_i].window));
}
if (window_i != terms.active[n].window) {
window_i = terms.active[n].window;
gtk_window_present (GTK_WINDOW(windows[window_i].window));
}
gtk_notebook_set_current_page (windows[window_i].notebook, gtk_notebook_page_num (windows[window_i].notebook, terms.active[n].term));
temu_window_title_change (VTE_TERMINAL(terms.active[n].term), n);
gtk_widget_grab_focus (GTK_WIDGET(terms.active[n].term));
}
/*
#undef FUNC_DEBUG
#define FUNC_DEBUG false
*/
static void
term_switch_page (GtkNotebook *notebook, GtkWidget *page, gint page_num, gpointer user_data)
{
VteTerminal *term;
int i;
term = VTE_TERMINAL(gtk_notebook_get_nth_page (notebook, page_num));
debugf("page_num: %d, current_page: %d, page: %p, term: %p, notebook: %p, user_data: %p",
page_num, gtk_notebook_get_current_page (notebook), page, term, notebook, user_data);
if (term_find(GTK_WIDGET(term), &i)) {
long n = i;
temu_window_title_changed (term, (void *) n);
gtk_widget_grab_focus (GTK_WIDGET(term));
}
}
/*
#undef FUNC_DEBUG
#define FUNC_DEBUG true
*/
static void show_menu(window_t *window, double x, double y)
{
GdkRectangle rect = { .width = 1, .height = 1 };
if (x == -1 && y == -1) {
GdkDisplay *display = gdk_display_get_default();
GdkSeat *seat = gdk_display_get_default_seat(display);
GdkDevice *pointer = gdk_seat_get_pointer(seat);
GdkModifierType mask;
GdkSurface *surface = gtk_native_get_surface(GTK_NATIVE(GTK_WINDOW(window->window)));
gdk_surface_get_device_position(GDK_SURFACE(surface), pointer, &x, &y, &mask);
}
rect.x = x;
rect.y = y;
if (window->menu_hyperlink_uri) {
free(window->menu_hyperlink_uri);
window->menu_hyperlink_uri = NULL;
}
GtkWidget *widget = gtk_notebook_get_nth_page(window->notebook, gtk_notebook_get_current_page(window->notebook));
int n;
term_find(widget, &n);
window->menu_x = x;
window->menu_y = y;
if (terms.active[n].hyperlink_uri) {
window->menu_hyperlink_uri = strdup(terms.active[n].hyperlink_uri);
debugf("Setting menu hyperlink uri: %s", window->menu_hyperlink_uri);
}
gtk_popover_set_has_arrow (GTK_POPOVER(window->menu), true);
gtk_widget_set_halign(window->menu, GTK_ALIGN_START);
gtk_widget_set_valign(window->menu, GTK_ALIGN_START);
gtk_popover_set_pointing_to(GTK_POPOVER(window->menu), &rect);
gtk_popover_popup(GTK_POPOVER(window->menu));
}
static gboolean
term_key_event (GtkEventControllerKey *key_controller, guint keyval, guint keycode, GdkModifierType state, gpointer user_data)
{
window_t *window = (window_t *) user_data;
bind_t *cur;
GtkWidget *widget;
guint keyval_lower = keyval;
/*
* Key bindings that involve shift are a problem, because we may get the
* upper case keyval (C instead of c), and yet modern gtk_accelerator_parse
* will always convert the keyval to lowercase.
*
* We could do something outright absurd, like using gtk_accelerator_name
* followed by gtk_accelerator_parse to normalize the key press, but the
* performance impact of that makes it feel like a bad plan.
*
* So if it's an alpha character, make a lower case version, and check the
* binding against that as well.
*
* Note: isalpha's argument is an int, however it is documented that it
* must be either an unsigned int, or the constant EOF.
*
* Notably, it segfaults for some values above 0xFF on my system.
*/
if (keyval < 0xFF && isalpha(keyval)) {
keyval_lower = tolower(keyval);
}
#if 0
gchar *name = gtk_accelerator_name(keyval, state);
gchar *label = gtk_accelerator_get_label(keyval, state);
debugf ("keyval: %d (%s), state: 0x%x, %d, name: '%s', label: '%s'", keyval, gdk_keyval_name(keyval), state, state, name, label);
g_free(label);
g_free(name);
#endif
for (cur = terms.keys; cur; cur = cur->next) {
#if 0
gchar *name = gtk_accelerator_name(cur->key_min, cur->state);
debugf("key_min: %d (%s), key_max: %d (%s), state: 0x%x (%s)", cur->key_min, gdk_keyval_name(cur->key_min), cur->key_max, gdk_keyval_name(cur->key_max), cur->state, name);
g_free(name);
#endif
if (((keyval >= cur->key_min) && (keyval <= cur->key_max)) ||
((keyval_lower >= cur->key_min) && (keyval_lower <= cur->key_max))) {
if ((state & bind_mask) == cur->state) {
switch (cur->action) {
case BIND_ACT_SWITCH:
term_switch (cur->base + (keyval - cur->key_min), cur->cmd, cur->argv, cur->env, window - &windows[0]);
break;
case BIND_ACT_CUT:
debugf("Cut text");
widget = gtk_notebook_get_nth_page(window->notebook, gtk_notebook_get_current_page(window->notebook));
vte_terminal_copy_clipboard_format (VTE_TERMINAL(widget), VTE_FORMAT_TEXT);
break;
case BIND_ACT_CUT_HTML:
debugf("Cut HTML");
widget = gtk_notebook_get_nth_page(window->notebook, gtk_notebook_get_current_page(window->notebook));
vte_terminal_copy_clipboard_format (VTE_TERMINAL(widget), VTE_FORMAT_HTML);
break;
case BIND_ACT_PASTE:
debugf("Paste");
widget = gtk_notebook_get_nth_page(window->notebook, gtk_notebook_get_current_page(window->notebook));
vte_terminal_paste_clipboard (VTE_TERMINAL(widget));
break;
case BIND_ACT_MENU:
show_menu(window, -1, -1);
break;
case BIND_ACT_NEXT_TERM:
gtk_notebook_next_page(GTK_NOTEBOOK(window->notebook));
break;
case BIND_ACT_PREV_TERM:
gtk_notebook_prev_page(GTK_NOTEBOOK(window->notebook));
break;
case BIND_ACT_OPEN_URI:
case BIND_ACT_CUT_URI:
int n;
widget = gtk_notebook_get_nth_page(window->notebook, gtk_notebook_get_current_page(window->notebook));
if (term_find(widget, &n)) {
process_uri(n, window, cur->action, -1, -1, false);
return true;
}
break;
default:
debugf("Fell into impossible key binding case.");
return false;
}
//debugf("action: %d", cur->action);
return true;
}
}
}
return false;
}
static gboolean term_button_event (GtkGesture *gesture, GdkEventSequence *sequence, gpointer user_data)
{
long int n = (long int) user_data;
window_t *window = &windows[terms.active[n].window];
// debugf("Got button: %d, n: %ld", gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture)), n);
return button_event(gesture, sequence, n, window);
}
static
gboolean window_button_event (GtkGesture *gesture, GdkEventSequence *sequence, gpointer user_data)
{
window_t *window = (window_t *) user_data;
debugf("Got window button: %d", gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture)));
return button_event (gesture, sequence, -1, window);
}
static void file_launch_callback (GObject *source, GAsyncResult *result, gpointer data)
{
GtkFileLauncher *launcher = (GtkFileLauncher *) data;
GError *error = NULL;
debugf();
bool ret = gtk_file_launcher_launch_finish(launcher, result, &error);
debugf("ret: %d, error: %p, result: %p", ret, error, result);
if (error) {
debugf("Error opening URI: domain: %x, code: %d, message: %s", error->domain, error->code, error->message);
g_free(error->message);
g_free(error);
}
}
gboolean process_uri(int64_t term_n, window_t *window, bind_actions_t action, double x, double y, bool menu)
{
GError *error = NULL;
const char *uri;
/*
* There are two different kinds of URIs for us to deal with.
*
* The first is stuff that's just printed in URI format.
*
* The second is URIs given via OSC codes, such as how `eza --hyperlink` works.
*
* And we get called in roughly three different contexts, key bindings, mouse bindings, and menus.
*
* For the first case, a text URI, we must use vte_terminal_check_match_at
* with the coordinates of the mouse pointer.
*
* For the key binding case, we'll need to find the current location of the
* pointer.
*
* For the mouse binding case, we'll be passed the location the click
* happened.
*
* For the menu case, we recorded the location of the pointer at the time
* that the menu was opened.
*
*
* For the second case, a URI given OSC codes, the only time we know about
* it is when we get a signal with the URI in it, which happens at
* mouseover time, or when it leaves, with no URI.
*
* In that case, both the key binding and mouse binding cases look at the
* terminal's hyperlink_uri variable, and the menu case looks at the
* window's menu_hyperlink_uri variable, which gets set from the terminal's
* hyperlink_uri variable at the time the window is opened.
*
* It would be bloody _great_ if printed URIs and URIs via OSC code would
* work roughly the same way, either with a signal with the URI on mouse
* over, or a function to check for a URI via OSC code with coordinates
* passed to it.
*
* But that's not what we actually have, ah well.