-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.c
438 lines (351 loc) · 14.6 KB
/
main.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
#include <gtk/gtk.h>
#include <stdio.h>
#include <vte/vte.h>
#include "config.default.h"
static const gchar *colors[] = {
BLACK, RED, GREEN, YELLOW, BLUE,
MAGENTA, CYAN, WHITE, BRIGHT_BLACK, BRIGHT_RED,
BRIGHT_GREEN, BRIGHT_YELLOW, BRIGHT_BLUE, BRIGHT_MAGENTA, BRIGHT_CYAN,
BRIGHT_WHITE};
const char *background_color = BACKGROUND_COLOR;
const char *foreground_color = FOREGROUND_COLOR;
const gboolean enable_hyperlinks = TRUE;
const gboolean hide_mouse_cursor = TRUE;
const gboolean cursor_blink = FALSE;
const gint scrollback_size = 50000;
static gint initial_font_size = 0;
size_t get_color_pallete(GdkRGBA **gcolors_p) {
const size_t colors_count = sizeof(colors) / sizeof(colors[0]);
*gcolors_p = calloc(sizeof(GdkRGBA), colors_count);
if (*gcolors_p == NULL) {
fprintf(stderr, "failed to allocate memory for GdkRGBA");
return -1;
}
for (size_t i = 0; i < colors_count; i++) {
GdkRGBA rgba;
fprintf(stdout, "[%li] Parsing: %s\n", i, colors[i]);
if (!gdk_rgba_parse(&rgba, colors[i])) {
fprintf(stderr, "failed to parse color: %s\n", colors[i]);
return -1;
}
(*gcolors_p)[i] = rgba;
}
return colors_count;
}
void spawn_cb(VteTerminal *terminal, GPid pid, GError *error,
gpointer user_data) {
if (error != NULL && pid == -1) {
g_printerr("failed to launch shell:%s\n", error->message);
g_error_free(error);
exit(1);
} else {
fprintf(stderr, "Shell launched: %i\n", pid);
}
}
#ifdef PIPECMD
// retrieve the entire text displayed in the current window
void accel_pipecmd(GtkAccelGroup *accel_group, GObject *acceleratable,
guint keyval, GdkModifierType modifier) {
VteTerminal *terminal =
g_object_get_data(G_OBJECT(acceleratable), "terminal");
char *text = vte_terminal_get_text(terminal, NULL, NULL, NULL);
if (fork() == 0) {
FILE *fd = popen(PIPECMD, "w");
if (fd == NULL) {
free(text);
exit(1);
return;
}
fwrite(text, 1, strlen(text), fd);
fflush(fd);
fclose(fd);
free(text);
exit(0);
}
free(text);
}
#endif
// whenever the terminal bell is triggered propagate the event to the window
void bell(VteTerminal *terminal, gpointer user_data) {
GtkWidget *window = user_data;
g_assert(terminal != NULL);
g_assert(window != NULL);
gtk_window_set_urgency_hint(GTK_WINDOW(window), TRUE);
}
// Update the appliations windows title to the new title of the current
// shell/process
void window_title_changed(VteTerminal *terminal, gpointer user_data) {
GtkWidget *window = user_data;
g_assert(terminal != NULL);
g_assert(window != NULL);
const char *title = vte_terminal_get_window_title(terminal);
g_assert(title != NULL);
gtk_window_set_title(GTK_WINDOW(window), title);
}
gint get_current_font_size(VteTerminal *terminal) {
g_assert(terminal != NULL);
PangoFontDescription *desc =
pango_font_description_copy(vte_terminal_get_font(terminal));
g_assert(desc != NULL);
gint size = pango_font_description_get_size(desc);
pango_font_description_free(desc);
return size / PANGO_SCALE;
}
// Reset the font size to the initial value we recorded during startup
void reset_font_size(VteTerminal *terminal) {
fprintf(stderr, "reset font size\n");
g_assert(terminal != NULL);
PangoFontDescription *desc =
pango_font_description_copy(vte_terminal_get_font(terminal));
g_assert(desc != NULL);
fprintf(stderr, "font size: %i\n", initial_font_size);
pango_font_description_set_size(desc, initial_font_size * PANGO_SCALE);
vte_terminal_set_font(terminal, desc);
pango_font_description_free(desc);
}
// Increase the font size on request
void increase_font_size(VteTerminal *terminal) {
fprintf(stderr, "increase font size\n");
fprintf(stderr, "terminal is %s\n", G_OBJECT_TYPE_NAME(terminal));
g_assert(terminal != NULL);
PangoFontDescription *desc =
pango_font_description_copy(vte_terminal_get_font(terminal));
g_assert(desc != NULL);
gint size = pango_font_description_get_size(desc);
fprintf(stderr, "font size: %i\n", size / PANGO_SCALE);
pango_font_description_set_size(desc, size + PANGO_SCALE);
vte_terminal_set_font(terminal, desc);
pango_font_description_free(desc);
}
// Decrease the font size on request
void decrease_font_size(VteTerminal *terminal) {
fprintf(stderr, "decrease font size\n");
g_assert(terminal != NULL);
PangoFontDescription *desc =
pango_font_description_copy(vte_terminal_get_font(terminal));
g_assert(desc != NULL);
gint size = pango_font_description_get_size(desc);
fprintf(stderr, "font size: %i\n", size / PANGO_SCALE);
pango_font_description_set_size(desc, size - PANGO_SCALE);
vte_terminal_set_font(terminal, desc);
pango_font_description_free(desc);
}
// handlers of the resize signals
void signal_increase_font_size(VteTerminal *terminal, gpointer user_data) {
increase_font_size(terminal);
}
void signal_decrease_font_size(VteTerminal *terminal, gpointer user_data) {
decrease_font_size(terminal);
}
gboolean accel_reset_font_size(GtkAccelGroup *accel_group,
GObject *acceleratable, guint keyval,
GdkModifierType modifier) {
VteTerminal *terminal =
g_object_get_data(G_OBJECT(acceleratable), "terminal");
reset_font_size(terminal);
return TRUE;
}
gboolean accel_increase_font_size(GtkAccelGroup *accel_group,
GObject *acceleratable, guint keyval,
GdkModifierType modifier) {
VteTerminal *terminal =
g_object_get_data(G_OBJECT(acceleratable), "terminal");
increase_font_size(terminal);
return TRUE;
}
gboolean accel_decrease_font_size(GtkAccelGroup *accel_group,
GObject *acceleratable, guint keyval,
GdkModifierType modifier) {
VteTerminal *terminal =
g_object_get_data(G_OBJECT(acceleratable), "terminal");
decrease_font_size(terminal);
return TRUE;
}
gboolean accel_paste_primary(GtkAccelGroup *accel_group, GObject *acceleratable,
guint keyval, GdkModifierType modifier) {
VteTerminal *terminal =
g_object_get_data(G_OBJECT(acceleratable), "terminal");
fprintf(stderr, "paste primary\n");
vte_terminal_paste_primary(terminal);
return TRUE;
}
gboolean accel_paste_clipboard(GtkAccelGroup *accel_group,
GObject *acceleratable, guint keyval,
GdkModifierType modifier) {
VteTerminal *terminal =
g_object_get_data(G_OBJECT(acceleratable), "terminal");
fprintf(stderr, "paste clipboard\n");
vte_terminal_paste_clipboard(terminal);
return TRUE;
}
gboolean accel_input_modal(GtkAccelGroup *accel_group, GObject *acceleratable,
guint keyval, GdkModifierType modifier) {
VteTerminal *terminal =
g_object_get_data(G_OBJECT(acceleratable), "terminal");
GtkWindow *window = GTK_WINDOW(gtk_widget_get_parent(GTK_WIDGET(terminal)));
GtkWidget *dialog = gtk_dialog_new_with_buttons(
"Modal input", NULL, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
"Ok", GTK_RESPONSE_ACCEPT, "Cancel", GTK_RESPONSE_REJECT, NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
GtkWidget *entry = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry), "");
gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
gtk_container_add(GTK_CONTAINER(content_area), entry);
gtk_widget_show(entry);
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(window));
gtk_window_set_keep_above(GTK_WINDOW(window), TRUE);
int result = gtk_dialog_run(GTK_DIALOG(dialog));
switch (result) {
case GTK_RESPONSE_ACCEPT:
fprintf(stderr, "Accept input\n");
const char *text = gtk_entry_get_text(GTK_ENTRY(entry));
vte_terminal_feed_child(terminal, text, -1);
break;
default:
fprintf(stderr, "Not accepted\n");
}
gtk_window_set_keep_above(GTK_WINDOW(window), FALSE);
gtk_widget_destroy(dialog);
return TRUE;
}
int main(int argc, char *argv[]) {
GtkWidget *window, *terminal;
GdkRGBA *colors = NULL;
GdkRGBA color;
const size_t colors_count = get_color_pallete(&colors);
if (colors_count <= 0 || colors == NULL) {
return 1;
}
/* Initialise GTK, the window and the terminal */
gtk_init(&argc, &argv);
terminal = vte_terminal_new();
vte_terminal_set_colors(VTE_TERMINAL(terminal), NULL, NULL, colors,
colors_count);
free(colors);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "at");
gtk_widget_set_opacity(GTK_WIDGET(window), BACKGROUND_OPACITY);
gdk_rgba_parse(&color, foreground_color);
vte_terminal_set_color_foreground(VTE_TERMINAL(terminal), &color);
gdk_rgba_parse(&color, background_color);
vte_terminal_set_color_background(VTE_TERMINAL(terminal), &color);
vte_terminal_set_scrollback_lines(VTE_TERMINAL(terminal), scrollback_size);
/* Enable Hyperlinks */
if (enable_hyperlinks) {
vte_terminal_set_allow_hyperlink(VTE_TERMINAL(terminal), TRUE);
}
/* Hide the mouse cursor after inactivity */
if (hide_mouse_cursor) {
vte_terminal_set_mouse_autohide(VTE_TERMINAL(terminal), TRUE);
}
/* Disable cursor blinking */
if (!cursor_blink) {
vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(terminal),
VTE_CURSOR_BLINK_OFF);
}
/* Connect some signals */
g_signal_connect(window, "delete-event", gtk_main_quit, NULL);
// g_signal_connect(window, "key_press_event", G_CALLBACK(on_key_press),
// VTE_TERMINAL(terminal));
g_signal_connect(terminal, "child-exited", gtk_main_quit, NULL);
g_signal_connect(terminal, "window-title-changed",
G_CALLBACK(window_title_changed), GTK_WINDOW(window));
g_signal_connect(terminal, "bell", G_CALLBACK(bell), GTK_WINDOW(window));
g_signal_connect(terminal, "increase-font-size",
G_CALLBACK(signal_increase_font_size), GTK_WINDOW(window));
g_signal_connect(terminal, "decrease-font-size",
G_CALLBACK(signal_decrease_font_size), GTK_WINDOW(window));
/* hook the custom accelerators into the application */
GtkAccelGroup *accelg = gtk_accel_group_new();
/* reset font size */
gtk_accel_group_connect(accelg, /* group */
gdk_keyval_from_name(RESET_FONT_KEYVAL),
RESET_FONT_MODIFIER_MASK, /* key & mask */
GTK_ACCEL_LOCKED, /* flags */
g_cclosure_new(G_CALLBACK(accel_reset_font_size),
terminal, NULL) /* callback */
);
/* increment font size */
gtk_accel_group_connect(accelg, /* group */
gdk_keyval_from_name(INCREMENT_FONT_KEYVAL),
INCREMENT_FONT_MODIFIER_MASK, /* key & mask */
GTK_ACCEL_LOCKED, /* flags */
g_cclosure_new(G_CALLBACK(accel_increase_font_size),
terminal, NULL) /* callback */
);
/* decrement font size */
gtk_accel_group_connect(accelg, /* group */
gdk_keyval_from_name(DECREMENT_FONT_KEYVAL),
DECREMENT_FONT_MODIFIER_MASK, /* key & mask */
GTK_ACCEL_LOCKED, /* flags */
g_cclosure_new(G_CALLBACK(accel_decrease_font_size),
terminal, NULL) /* callback */
);
/* paste primary */
gtk_accel_group_connect(accelg, /* group */
gdk_keyval_from_name(PASTE_PRIMARY_KEYVAL),
PASTE_PRIMARY_MODIFIER_MASK, /* key & mask */
GTK_ACCEL_LOCKED, /* flags */
g_cclosure_new(G_CALLBACK(accel_paste_primary),
terminal, NULL) /* callback */
);
/* paste clipboard */
gtk_accel_group_connect(accelg, /* group */
gdk_keyval_from_name(PASTE_CLIPBOARD_KEYVAL),
PASTE_CLIPBOARD_MODIFIER_MASK, /* key & mask */
GTK_ACCEL_LOCKED, /* flags */
g_cclosure_new(G_CALLBACK(accel_paste_clipboard),
terminal, NULL) /* callback */
);
/* pipecmd */
#ifdef PIPECMD
gtk_accel_group_connect(
accelg, /* group */
gdk_keyval_from_name(PIPECMD_KEYVAL),
PIPECMD_MODIFIER_MASK, /* key & mask */
GTK_ACCEL_LOCKED, /* flags */
g_cclosure_new(G_CALLBACK(accel_pipecmd), terminal, NULL) /* callback */
);
#else
#warning "No PIPECMD configured. The pipecmd feature will not work."
#endif
gtk_accel_group_connect(
accelg, /* group */
gdk_keyval_from_name(MODAL_INPUT_KEYVAL), /* key & mask */
MODAL_INPUT_MODIFIER_MASK, /* flags */
GTK_ACCEL_LOCKED,
g_cclosure_new(G_CALLBACK(accel_input_modal), terminal,
NULL) /* callback */
);
gtk_window_add_accel_group(GTK_WINDOW(window), accelg);
// record the initial font size so we can reset the font to that value
initial_font_size = get_current_font_size(VTE_TERMINAL(terminal));
fprintf(stderr, "initial font size: %i\n", initial_font_size);
// attach the terminal as private data to the window
g_object_set_data(G_OBJECT(window), "terminal", terminal);
/* Start a new shell */
gchar **envp = g_get_environ();
gchar **command =
(gchar *[]){g_strdup(g_environ_getenv(envp, "SHELL")), NULL};
if (argc > 1) {
command = &argv[1];
}
fprintf(stderr, "launch command: %s\n", command[0]);
g_strfreev(envp);
vte_terminal_spawn_async(VTE_TERMINAL(terminal), VTE_PTY_DEFAULT,
NULL, /* working directory */
command, /* command */
NULL, /* environment */
G_SPAWN_DEFAULT, /* spawn flags */
NULL, NULL, /* child setup */
NULL, /* child pid */
-1, /* timeout */
NULL, /* cancellable */
spawn_cb, /* callback */
NULL); /* user_data */
/* Put widgets together and run the main loop */
gtk_container_add(GTK_CONTAINER(window), terminal);
gtk_widget_show_all(window);
gtk_main();
}