-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.c
773 lines (718 loc) · 21.7 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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <gtk/gtk.h>
#include "jsmn.h"
#include "config.h" /* Generated by meson */
#ifdef LAYERSHELL
#include <gtk-layer-shell/gtk-layer-shell.h>
#endif
#ifdef LAYERSHELL
static const int exclusive_level = -1;
#endif
#ifdef LAYERSHELL
static gboolean protocol = TRUE;
#else
static gboolean protocol = FALSE;
#endif
typedef struct
{
char *label;
char *action;
char *text;
float yalign;
float xalign;
guint bind;
gboolean circular;
} button;
static const int default_size = 100;
static char *command = NULL;
static char *layout_path = NULL;
static char *css_path = NULL;
static button *buttons = NULL;
static GtkWidget *gtk_window = NULL;
static int num_buttons = 0;
static int draw = 0;
static int num_of_monitors = 0;
static GtkWindow **window = NULL;
static int buttons_per_row = 3;
static int primary_monitor = -1;
static int margin[] = {230, 230, 230, 230};
static int space[] = {0, 0};
static gboolean show_bind = FALSE;
static gboolean no_span = FALSE;
static gboolean layershell = FALSE;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"layout", required_argument, NULL, 'l'},
{"version", no_argument, NULL, 'v'},
{"css", required_argument, NULL, 'C'},
{"margin", required_argument, NULL, 'm'},
{"margin-top", required_argument, NULL, 'T'},
{"margin-bottom", required_argument, NULL, 'B'},
{"margin-left", required_argument, NULL, 'L'},
{"margin-right", required_argument, NULL, 'R'},
{"buttons-per-row", required_argument, NULL, 'b'},
{"column-spacing", required_argument, NULL, 'c'},
{"row-spacing", required_argument, NULL, 'r'},
{"protocol", required_argument, NULL, 'p'},
{"show-binds", no_argument, NULL, 's'},
{"no-span", no_argument, NULL, 'n'},
{"primary-monitor", required_argument, NULL, 'P'},
{0, 0, 0, 0}};
static const char *help =
"Usage: wlogout [options...]\n"
"\n"
" -h, --help Show help message and stop\n"
" -l, --layout </path/to/layout> Specify a layout file\n"
" -v, --version Show version number and stop\n"
" -C, --css </path/to/css> Specify a css file\n"
" -b, --buttons-per-row <0-x> Set the number of buttons per row\n"
" -c --column-spacing <0-x> Set space between buttons columns\n"
" -r --row-spacing <0-x> Set space between buttons rows\n"
" -m, --margin <0-x> Set margin around buttons\n"
" -L, --margin-left <0-x> Set margin for left of buttons\n"
" -R, --margin-right <0-x> Set margin for right of buttons\n"
" -T, --margin-top <0-x> Set margin for top of buttons\n"
" -B, --margin-bottom <0-x> Set margin for bottom of buttons\n"
" -p, --protocol <protocol> Use layer-shell or xdg protocol\n"
" -s, --show-binds Show the keybinds on their "
"corresponding button\n"
" -n, --no-span Stops from spanning across "
"multiple monitors\n"
" -P, --primary-monitor <0-x> Set the primary monitor\n";
static gboolean process_args(int argc, char *argv[])
{
while (TRUE)
{
int option_index = 0;
int c = getopt_long(argc, argv, "hl:vc:m:b:T:R:L:B:r:c:p:C:sP:n",
long_options, &option_index);
if (c == -1)
{
break;
}
switch (c)
{
case 'm':
margin[0] = atoi(optarg);
margin[1] = atoi(optarg);
margin[2] = atoi(optarg);
margin[3] = atoi(optarg);
break;
case 'L':
margin[2] = atoi(optarg);
break;
case 'T':
margin[0] = atoi(optarg);
break;
case 'B':
margin[1] = atoi(optarg);
break;
case 'R':
margin[3] = atoi(optarg);
break;
case 'c':
space[1] = atoi(optarg);
break;
case 'r':
space[0] = atoi(optarg);
break;
case 'l':
layout_path = g_strdup(optarg);
break;
case 'v':
g_print("wlogout %s\n", PROJECT_VERSION);
return TRUE;
case 'C':
css_path = g_strdup(optarg);
break;
case 'b':
buttons_per_row = atoi(optarg);
break;
case 'p':
if (strcmp("layer-shell", optarg) == 0)
{
protocol = TRUE;
}
else if (strcmp("xdg", optarg) == 0)
{
protocol = FALSE;
}
else
{
g_print("%s is an invalid protocol\n", optarg);
return TRUE;
}
break;
case 's':
show_bind = TRUE;
break;
case 'P':
primary_monitor = atoi(optarg);
break;
case 'n':
no_span = TRUE;
break;
case '?':
case 'h':
default:
g_print("%s\n", help);
return TRUE;
}
}
return FALSE;
}
static gboolean get_layout_path()
{
char *buf = malloc(default_size * sizeof(char));
if (!buf)
{
fprintf(stderr, "Failed to allocate memory\n");
}
char *tmp = getenv("XDG_CONFIG_HOME");
char *config_path = g_strdup(tmp);
if (!config_path)
{
config_path = getenv("HOME");
int n = snprintf(buf, default_size, "%s/.config", config_path);
if (n != 0)
{
free(buf);
buf = malloc((default_size * sizeof(char)) + (sizeof(char) * n));
snprintf(buf, (default_size * sizeof(char)) + (sizeof(char) * n),
"%s/.config", config_path);
}
config_path = g_strdup(buf);
}
int n = snprintf(buf, default_size, "%s/wlogout/layout", config_path);
if (n != 0)
{
free(buf);
buf = malloc((default_size * sizeof(char)) + (sizeof(char) * n));
snprintf(buf, (default_size * sizeof(char)) + (sizeof(char) * n),
"%s/wlogout/layout", config_path);
}
free(config_path);
if (layout_path)
{
free(buf);
return FALSE;
}
else if (access(buf, F_OK) != -1)
{
layout_path = g_strdup(buf);
free(buf);
return FALSE;
}
else if (access("/etc/wlogout/layout", F_OK) != -1)
{
layout_path = "/etc/wlogout/layout";
free(buf);
return FALSE;
}
else if (access("/usr/local/etc/wlogout/layout", F_OK) != -1)
{
layout_path = "/usr/local/etc/wlogout/layout";
free(buf);
return FALSE;
}
else
{
free(buf);
return TRUE;
}
}
static gboolean get_css_path()
{
char *buf = malloc(default_size * sizeof(char));
if (!buf)
{
fprintf(stderr, "Failed to allocate memory\n");
}
char *tmp = getenv("XDG_CONFIG_HOME");
char *config_path = g_strdup(tmp);
if (!config_path)
{
config_path = getenv("HOME");
int n = snprintf(buf, default_size, "%s/.config", config_path);
if (n != 0)
{
free(buf);
buf = malloc((default_size * sizeof(char)) + (sizeof(char) * n));
snprintf(buf, (default_size * sizeof(char)) + (sizeof(char) * n),
"%s/.config", config_path);
}
config_path = g_strdup(buf);
}
int n = snprintf(buf, default_size, "%s/wlogout/style.css", config_path);
if (n != 0)
{
free(buf);
buf = malloc((default_size * sizeof(char)) + (sizeof(char) * n));
snprintf(buf, (default_size * sizeof(char)) + (sizeof(char) * n),
"%s/wlogout/style.css", config_path);
}
free(config_path);
if (css_path)
{
free(buf);
return FALSE;
}
else if (access(buf, F_OK) != -1)
{
css_path = g_strdup(buf);
free(buf);
return FALSE;
}
else if (access("/etc/wlogout/style.css", F_OK) != -1)
{
css_path = "/etc/wlogout/style.css";
free(buf);
return FALSE;
}
else if (access("/usr/local/etc/wlogout/style.css", F_OK) != -1)
{
css_path = "/usr/local/etc/wlogout/style.css";
free(buf);
return FALSE;
}
else
{
return TRUE;
}
}
static gboolean background_clicked(GtkWidget *widget, GdkEventButton event,
gpointer user_data)
{
for (int i = 0; i < num_of_monitors; i++)
{
if (i != primary_monitor)
{
gtk_widget_destroy(GTK_WIDGET(window[i]));
}
}
gtk_main_quit();
return TRUE;
}
static char *get_substring(char *s, int start, int end, char *buf)
{
memcpy(s, &buf[start], (end - start));
s[end - start] = '\0';
return s;
}
static gboolean get_buttons(FILE *json)
{
fseek(json, 0L, SEEK_END);
int length = ftell(json);
rewind(json);
char *buffer = malloc(length);
if (!buffer)
{
g_warning("Failed to allocate memory\n");
return TRUE;
}
fread(buffer, 1, length, json);
jsmn_parser p;
jsmntok_t *tok = malloc(default_size * sizeof(jsmntok_t));
if (!tok)
{
free(buffer);
g_warning("Failed to allocate memory\n");
return TRUE;
}
jsmn_init(&p);
int numtok, i = 1;
do
{
numtok = jsmn_parse(&p, buffer, length, tok, default_size * i);
if (numtok == JSMN_ERROR_NOMEM)
{
i++;
jsmntok_t *tmp =
realloc(tok, ((default_size * i) * sizeof(jsmntok_t)));
if (!tmp)
{
free(tok);
return FALSE;
}
else if (tmp != tok)
{
tok = tmp;
}
}
else
{
break;
}
} while (TRUE);
if (numtok < 0)
{
free(tok);
g_warning("Failed to parse JSON data\n");
return TRUE;
}
for (int i = 0; i < numtok; i++)
{
if (tok[i].type == JSMN_OBJECT)
{
num_buttons++;
buttons[num_buttons - 1].yalign = 0.9;
buttons[num_buttons - 1].xalign = 0.5;
buttons[num_buttons - 1].circular = FALSE;
}
else if (tok[i].type == JSMN_STRING)
{
int length = tok[i].end - tok[i].start;
char tmp[length + 1];
get_substring(tmp, tok[i].start, tok[i].end, buffer);
i++;
length = tok[i].end - tok[i].start;
if (strcmp(tmp, "label") == 0)
{
char buf[length + 1];
get_substring(buf, tok[i].start, tok[i].end, buffer);
buttons[num_buttons - 1].label =
malloc(sizeof(char) * (length + 1));
strcpy(buttons[num_buttons - 1].label, buf);
}
else if (strcmp(tmp, "action") == 0)
{
char buf[length + 1];
get_substring(buf, tok[i].start, tok[i].end, buffer);
buttons[num_buttons - 1].action =
malloc(sizeof(char) * length + 1);
strcpy(buttons[num_buttons - 1].action, buf);
}
else if (strcmp(tmp, "text") == 0)
{
char buf[length + 1];
get_substring(buf, tok[i].start, tok[i].end, buffer);
/* Add a small buffer to allocated memory so the keybind
* can easily be concatenated later if needed */
int keybind_buffer = sizeof(guint) + (sizeof(char) * 2);
buttons[num_buttons - 1].text =
malloc((sizeof(char) * (length + 1)) + keybind_buffer);
strcpy(buttons[num_buttons - 1].text, buf);
}
else if (strcmp(tmp, "keybind") == 0)
{
if (length != 1)
{
fprintf(stderr, "Invalid keybind\n");
}
else
{
buttons[num_buttons - 1].bind = buffer[tok[i].start];
}
}
else if (strcmp(tmp, "height") == 0)
{
if (tok[i].type != JSMN_PRIMITIVE ||
!isdigit(buffer[tok[i].start]))
{
fprintf(stderr, "Invalid height\n");
}
else
{
buttons[num_buttons - 1].yalign = buffer[tok[i].start];
}
}
else if (strcmp(tmp, "width") == 0)
{
if (tok[i].type != JSMN_PRIMITIVE ||
!isdigit(buffer[tok[i].start]))
{
fprintf(stderr, "Invalid width\n");
}
else
{
buttons[num_buttons - 1].xalign = buffer[tok[i].start];
}
}
else if (strcmp(tmp, "circular") == 0)
{
if (tok[i].type != JSMN_PRIMITIVE)
{
fprintf(stderr, "Invalid boolean\n");
}
else
{
if (buffer[tok[i].start] == 't')
{
buttons[num_buttons - 1].circular = TRUE;
}
else
{
buttons[num_buttons - 1].circular = FALSE;
}
}
}
else
{
g_warning("Invalid key %s\n", tmp);
return TRUE;
}
}
else
{
g_warning("Invalid JSON Data\n");
return TRUE;
}
}
free(tok);
free(buffer);
fclose(json);
return FALSE;
}
static void execute(GtkWidget *widget, char *action)
{
command = malloc(strlen(action) * sizeof(char) + 1);
strcpy(command, action);
gtk_widget_destroy(gtk_window);
for (int i = 0; i < num_of_monitors; i++)
{
if (i != primary_monitor)
{
gtk_widget_destroy(GTK_WIDGET(window[i]));
}
}
gtk_main_quit();
}
static gboolean check_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
if (event->keyval == GDK_KEY_Escape)
{
gtk_main_quit();
return TRUE;
}
for (int i = 0; i < num_buttons; i++)
{
if (buttons[i].bind == event->keyval)
{
execute(NULL, buttons[i].action);
return TRUE;
}
}
return FALSE;
}
static void set_fullscreen(GtkWindow *win, int monitor, gboolean keyboard)
{
if (!layershell && protocol)
{
#ifdef LAYERSHELL
g_warning("Falling back to xdg protocol");
#else
g_warning("wlogout was compiled without layer-shell support\n"
"Falling back to xdg protocol");
#endif
}
if (protocol && layershell)
{
#ifdef LAYERSHELL
GdkMonitor *mon =
gdk_display_get_monitor(gdk_display_get_default(), monitor);
gtk_layer_init_for_window(win);
gtk_layer_set_layer(win, GTK_LAYER_SHELL_LAYER_OVERLAY);
gtk_layer_set_namespace(win, "logout_dialog");
gtk_layer_set_exclusive_zone(win, exclusive_level);
for (int j = 0; j < GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER; j++)
{
gtk_layer_set_anchor(win, j, TRUE);
}
gtk_layer_set_monitor(win, mon);
gtk_layer_set_keyboard_interactivity(win, keyboard);
#endif
}
else
{
if (monitor < 0)
{
gtk_window_fullscreen(win);
}
else
{
gtk_window_fullscreen_on_monitor(win, gdk_screen_get_default(),
monitor);
}
}
}
static void get_monitor(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
/* For some reason gtk only returns the correct monitor after the window
* has been drawn twice */
if (draw != 2)
{
draw++;
return;
}
GdkDisplay *display = gdk_display_get_default();
GdkMonitor *active_monitor = gdk_display_get_monitor_at_window(
display, gtk_widget_get_window(gtk_window));
num_of_monitors = gdk_display_get_n_monitors(display);
window = malloc(num_of_monitors * sizeof(GtkWindow *));
GtkWidget **box = malloc(num_of_monitors * sizeof(GtkWidget *));
GdkMonitor **monitors = malloc(num_of_monitors * sizeof(GdkMonitor *));
for (int i = 0; i < num_of_monitors; i++)
{
window[i] = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
box[i] = gtk_event_box_new();
monitors[i] = gdk_display_get_monitor(display, i);
if (monitors[i] == active_monitor)
{
primary_monitor = i;
}
}
for (int i = 0; i < num_of_monitors; i++)
{
if (i != primary_monitor)
{
set_fullscreen(window[i], i, FALSE);
}
}
for (int i = 0; i < num_of_monitors; i++)
{
if (i != primary_monitor)
{
// add event box to exit when clicking the background
gtk_container_add(GTK_CONTAINER(window[i]), box[i]);
g_signal_connect(box[i], "button-press-event",
G_CALLBACK(background_clicked), NULL);
gtk_widget_show_all(GTK_WIDGET(window[i]));
}
}
g_signal_handlers_disconnect_by_func(gtk_window, G_CALLBACK(get_monitor),
NULL);
}
static void load_buttons(GtkContainer *container)
{
GtkWidget *grid = gtk_grid_new();
gtk_container_add(container, grid);
gtk_grid_set_row_spacing(GTK_GRID(grid), space[0]);
gtk_grid_set_column_spacing(GTK_GRID(grid), space[1]);
gtk_widget_set_margin_top(grid, margin[0]);
gtk_widget_set_margin_bottom(grid, margin[1]);
gtk_widget_set_margin_start(grid, margin[2]);
gtk_widget_set_margin_end(grid, margin[3]);
int num_col = 0;
if ((num_buttons % buttons_per_row) == 0)
{
num_col = (num_buttons / buttons_per_row);
}
else
{
num_col = (num_buttons / buttons_per_row) + 1;
}
GtkWidget *but[buttons_per_row][num_col];
int count = 0;
for (int i = 0; i < buttons_per_row; i++)
{
for (int j = 0; j < num_col; j++)
{
if (buttons[count].text && show_bind)
{
strcat(buttons[count].text, "[");
strcat(buttons[count].text, (char *)&buttons[count].bind);
strcat(buttons[count].text, "]");
}
but[i][j] = gtk_button_new_with_label(buttons[count].text);
gtk_widget_set_name(but[i][j], buttons[count].label);
gtk_label_set_yalign(
GTK_LABEL(gtk_bin_get_child(GTK_BIN(but[i][j]))),
buttons[count].yalign);
gtk_label_set_xalign(
GTK_LABEL(gtk_bin_get_child(GTK_BIN(but[i][j]))),
buttons[count].xalign);
if (buttons[count].circular)
{
gtk_style_context_add_class(
gtk_widget_get_style_context(but[i][j]), "circular");
}
g_signal_connect(but[i][j], "clicked", G_CALLBACK(execute),
buttons[count].action);
gtk_widget_set_hexpand(but[i][j], TRUE);
gtk_widget_set_vexpand(but[i][j], TRUE);
gtk_grid_attach(GTK_GRID(grid), but[i][j], i, j, 1, 1);
count++;
}
}
}
static void load_css()
{
GtkCssProvider *css = gtk_css_provider_new();
GError *error = NULL;
gtk_css_provider_load_from_path(css, css_path, &error);
if (error)
{
g_warning("%s", error->message);
g_clear_error(&error);
}
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(css),
GTK_STYLE_PROVIDER_PRIORITY_USER);
}
int main(int argc, char *argv[])
{
buttons = malloc(sizeof(button) * default_size);
g_set_prgname("wlogout");
gtk_init(&argc, &argv);
if (process_args(argc, argv))
{
return 0;
}
if (get_layout_path())
{
g_warning("Failed to find a layout\n");
return 1;
}
if (get_css_path())
{
g_warning("Failed to find css file\n");
}
FILE *inptr = fopen(layout_path, "r");
if (!inptr)
{
g_warning("Failed to open %s\n", layout_path);
return 2;
}
if (get_buttons(inptr))
{
fclose(inptr);
return 3;
}
#ifdef LAYERSHELL
layershell = gtk_layer_is_supported();
#endif
GtkWindow *active_window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
set_fullscreen(active_window, primary_monitor, TRUE);
gtk_window = GTK_WIDGET(active_window);
g_signal_connect(gtk_window, "key_press_event", G_CALLBACK(check_key),
NULL);
if (!no_span)
{
/* The compositor will only tell us what monitor wlogouts on after
* after gtk_main() is called and its been drawn */
g_signal_connect_after(gtk_window, "draw", G_CALLBACK(get_monitor),
NULL);
}
GtkWidget *active_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(gtk_window), active_box);
g_signal_connect(active_box, "button-press-event",
G_CALLBACK(background_clicked), NULL);
load_buttons(GTK_CONTAINER(active_box));
load_css();
gtk_widget_show_all(gtk_window);
gtk_main();
system(command);
for (int i = 0; i < num_buttons; i++)
{
free(buttons[i].label);
free(buttons[i].action);
free(buttons[i].text);
}
free(buttons);
free(command);
}