-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
662 lines (579 loc) · 18.4 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
/**
* mygtkmenui - read description file and generate menu
*
* Copyright (C) 2004-2011 John Vorthman
* 2012-2014 Jan Pacner ([email protected])
*
* gcc -o mygtkmenui `pkg-config --cflags --libs gtk+-3.0` main.c
* gtk+-2.0
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 2, as published
* by the Free Software Foundation.
*
* 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, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//FIXME dumblob's long term wishlist
// add -d debug flag (all the current messages should go there)
// search (case-insensitive + partial token match) using /
// position submenus such that
// the middle item is right next to currently selected item in
// the parent menu
// OR
// the first non-submenu item gets selected (if none, select
// first submenu item)
// possibility to change font from the menuDescFile?
// disable selection cycling when entering the first/last item
// icon upscaling (only downscaling is currently implemented)
// cache somehow the scaled icons (or defer the reading & scaling up
// to the first (sub)menu kickoff) for fast menu show-up
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>
#include <math.h>
#if ! GTK_CHECK_VERSION (2, 18, 0)
#define gtk_widget_get_visible(w) GTK_WIDGET_VISIBLE (w)
#endif
#define MAX_LINE_LENGTH 768
#define MAX_MENU_ENTRIES 1024
#define MAX_SUBMENU_DEPTH 8
#define MAX_ICON_SIZE 256
#define MIN_ICON_SIZE 8
#define MAX_PATH_LEN 256
#define LOCK_NAME "mygtkmenui_lockfile"
#define ENV_LOCK_FILE_PREFIX "XDG_CACHE_HOME"
#define printHelpMsg(app_name) \
g_print ("mygtkmenui - myGtkMenu improved\n" \
"USAGE: %s -h\n" \
" %s -- [filename]\n" \
" if no `filename' is given, use stdin\n" \
" see the attached menu description file for more info\n", \
(app_name), (app_name));
/* prototypes */
int ReadLine();
static void RunItem (char *);
static void QuitMenu (char *);
gboolean Get2Numbers (char *);
static void menu_position (GtkMenu *, gint *, gint *, gboolean *, gpointer);
int already_running (void);
void gtk_menu_shell_select_some (GtkMenuShell *);
/* globals */
char Line[MAX_LINE_LENGTH], data[MAX_LINE_LENGTH];
int depth, lineNum, menuX, menuY;
FILE *pFile;
GtkWidget *menu[MAX_SUBMENU_DEPTH];
int main (int argc, char *argv[]) {
int Mode; // What kind of input are we looking for?
int Kind; // Type of input actually read
int curDepth; // Root menu is depth = 0
int curItem; // number of menu entries
gint w, h; // Size of menu icon
gboolean bSetMenuPos = FALSE;
int i;
char Item[MAX_LINE_LENGTH], Cmd[MAX_MENU_ENTRIES][MAX_LINE_LENGTH];
GtkWidget *menuItem, *SubmenuItem = NULL;
GtkWidget *image;
GdkPixbuf *Pixbuf;
GError *error = NULL;
struct stat statbuf;
// some hot keys (keybindings) get carried away and start
// many instances => use a lock file
int i_already_running = already_running();
if (i_already_running == 1) {
fprintf(stderr, "Already running, will quit.\n");
return EXIT_FAILURE;
}
else if (i_already_running == 2) {
fprintf (stderr, "%s: Error in routine already_running(), will quit.\n",
argv[0]);
return EXIT_FAILURE;
}
if ((argc == 2 || argc == 3) && ! strcmp(argv[1], "--")) {
if (argc == 3) {
printf ("Reading the file: %s\n", argv[2]);
pFile = fopen (argv[2], "r");
}
else {
printf ("Reading menu-description from stdin...\n");
pFile = stdin;
}
}
else {
printHelpMsg(argv[0]);
return EXIT_SUCCESS;
}
if (pFile == NULL) {
fprintf (stderr, "Cannot open the file.\n");
return EXIT_FAILURE;
}
// manipulates with the content of argc/argv (e.g. -- disappears both
// from argc and argv)
if (! gtk_init_check(&argc, &argv)) {
g_print("Error, cannot initialize gtk.\n");
return EXIT_FAILURE;
}
menu[0] = gtk_menu_new();
/* handle et .c oh un keys as arrow-keys down up left right on dvorak
keyboard layout and jkqgG keys as in less(1)
see menu class in gtk/gtkmenu.c from GTK upstream */
guint key;
GdkModifierType mod;
GtkBindingSet *binding_set = gtk_binding_set_by_class(
GTK_MENU_GET_CLASS (menu[0]));
gtk_accelerator_parse("e", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_NEXT);
gtk_accelerator_parse("t", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_NEXT);
gtk_accelerator_parse("j", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_NEXT);
//gtk_accelerator_parse (".", &key, &mod); // why the hell does this not work?
gtk_binding_entry_add_signal (binding_set, GDK_KEY_period, 0,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_PREV);
gtk_accelerator_parse("c", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_PREV);
gtk_accelerator_parse("k", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_PREV);
gtk_accelerator_parse("o", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_PARENT);
gtk_accelerator_parse("h", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_PARENT);
gtk_accelerator_parse("u", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_CHILD);
gtk_accelerator_parse("n", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_CHILD);
//gtk_accelerator_parse("l", &key, &mod); // why the hell does this not work?
gtk_binding_entry_add_signal (binding_set, GDK_KEY_l, mod,
"move-current", 1, GTK_TYPE_MENU_DIRECTION_TYPE, GTK_MENU_DIR_CHILD);
gtk_accelerator_parse("g", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-scroll", 1, GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_START);
gtk_accelerator_parse("<Shift>g", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"move-scroll", 1, GTK_TYPE_SCROLL_TYPE, GTK_SCROLL_END);
gtk_accelerator_parse("q", &key, &mod);
gtk_binding_entry_add_signal (binding_set, key, mod,
"cancel", 0);
if (! gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h)) {
w = 30;
h = 30;
};
curItem = 0;
Mode = 0;
curDepth = 0;
// FIXME Read next line and get the keyword kind
while ((Kind = ReadLine()) != 0) {
if (Kind == -1) {
Mode = -1;
fprintf(stderr, "Bad syntax \"%s\"\n", data);
}
if (depth > curDepth) {
g_print ("Keyword found at incorrect indentation.\n");
Mode = -1;
}
else if (depth < curDepth) { // Close submenu
curDepth = depth;
}
if (Mode == 0) { // Starting new sequence. Whats next?
if (Kind == 1)
Mode = 1; // New item
else if (Kind == 4)
Mode = 4; // New submenu
else if (Kind == 5)
Mode = 6; // New separator
else if (Kind == 6)
Mode = 7; // New icon size
else if (Kind == 7)
Mode = 8; // Set menu position
else { // Problems
g_print ("Keyword out of order.\n");
Mode = -1;
}
}
switch (Mode) {
case 1: // Starting new menu item
if (curItem >= MAX_MENU_ENTRIES) {
g_print ("Exceeded maximum number of menu items.\n");
Mode = -1;
break;
}
strcpy (Item, data);
Mode = 2;
break;
case 2: // Still making new menu item
if (Kind != 2) { // Problems if keyword 'cmd=' not found
g_print ("Missing keyword 'cmd=' (after 'item=').\n");
Mode = -1;
break;
}
strcpy (Cmd[curItem], data);
Mode = 3;
break;
case 3: // Still making new menu item
if (Kind != 3) { // Problems if keyword 'icon=' not found
g_print ("Missing keyword 'icon=' (after 'cmd=').\n");
Mode = -1;
break;
}
// Insert item into menu
// FIXME deprecated, use gtk_menu_item_new_with_mnemonic(Item)
menuItem = gtk_image_menu_item_new_with_mnemonic(Item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu[curDepth]), menuItem);
g_signal_connect_swapped (menuItem, "activate",
G_CALLBACK (RunItem), Cmd[curItem]);
curItem++;
#define ADD_MENU_ITEM(img) \
Mode = 0; \
if (data[0] == '\0') break; /* no icon */ \
/* If data is a dir name, program will hang. */ \
stat (data, &statbuf); \
if (! S_ISREG(statbuf.st_mode)) { \
g_print("%d: Error, %s is not a icon file.\n", lineNum, data); \
break; \
} \
Pixbuf = gdk_pixbuf_new_from_file_at_size(data, w, h, &error); \
if (Pixbuf == NULL) { \
g_print("%d: %s\n", lineNum, error->message); \
g_error_free(error); \
error = NULL; \
} \
image = gtk_image_new_from_pixbuf(Pixbuf); \
/* FIXME GtkImageMenuItem is deprecated - replace it with GMenu from GIO? */ \
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(img), image);
/*gtk_image_menu_item_set_image(GtkImageMenuItem(img), image);*/
ADD_MENU_ITEM(menuItem)
break;
case 4: // Start submenu
if (curDepth >= MAX_SUBMENU_DEPTH) {
g_print ("Maximum submenu depth exceeded.\n");
Mode = -1;
break;
}
// FIXME deprecated, use gtk_menu_item_new_with_mnemonic(data)
SubmenuItem = gtk_image_menu_item_new_with_mnemonic (data);
gtk_menu_shell_append(GTK_MENU_SHELL(menu[curDepth]), SubmenuItem);
curDepth++;
menu[curDepth] = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (SubmenuItem),
menu[curDepth]);
Mode = 5;
break;
case 5: // Adding image to new submenu
if (Kind != 3) { // Problems if keyword 'icon=' not found
g_print ("Missing keyword 'icon=' (after 'submenu=').\n");
Mode = -1;
break;
}
ADD_MENU_ITEM(SubmenuItem)
break;
case 6: // Insert separator into menu
menuItem = gtk_separator_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (menu[curDepth]), menuItem);
Mode = 0;
break;
case 7: // Change menu icon size
i = atoi (data);
if ((i < MIN_ICON_SIZE) || (i > MAX_ICON_SIZE)) {
g_print("Illegal size for menu icon.\n");
Mode = -1;
break;
}
w = i;
h = i;
g_print("New icon size = %d.\n", w);
Mode = 0;
break;
case 8: // Set menu position
if (Get2Numbers (data)) {
bSetMenuPos = TRUE;
//g_print ("Menu position = %d, %d.\n", menuX, menuY);
Mode = 0;
}
else {
g_print ("Error reading menu position.\n");
Mode = -1;
}
break;
default:
Mode = -1;
}
if (Mode == -1) {
g_print ("%d: >>>>%s\n", lineNum, Line);
break;
}
}
fclose (pFile);
// inform user
if (curItem == 0)
gtk_menu_shell_append (GTK_MENU_SHELL (menu[0]),
//FIXME deprecated, use gtk_menu_item_new_with_mnemonic("...")
gtk_image_menu_item_new_with_mnemonic("<no content to display>"));
gtk_widget_show_all (menu[0]);
g_signal_connect_swapped (menu[0], "deactivate",
G_CALLBACK(QuitMenu), NULL);
gtk_menu_shell_select_some (GTK_MENU_SHELL (menu[0]));
// Keep trying until startup
while (! gtk_widget_get_visible(menu[0])) {
// button (or key) is getting released
usleep(50000);
if (bSetMenuPos)
gtk_menu_popup (GTK_MENU (menu[0]), NULL, NULL,
(GtkMenuPositionFunc) menu_position,
NULL, 0, gtk_get_current_event_time ());
else
gtk_menu_popup (GTK_MENU (menu[0]), NULL, NULL, NULL, NULL, 0,
gtk_get_current_event_time ());
gtk_main_iteration();
}
gtk_main ();
return 0;
}
#if (GTK_MAJOR_VERSION == 2)
#define CHILDREN menu_shell->children
#else
/* first item of menu_shell->priv->children is the needed pointer */
#define CHILDREN ((struct { GList *x; } *)menu_shell->priv)->x
#endif
/* select some item (in this case the middle one) */
void gtk_menu_shell_select_some (GtkMenuShell *menu_shell) {
GList *tmp_list = CHILDREN;
guint i = 0;
/* count all */
while (tmp_list) {
i++;
tmp_list = tmp_list->next;
}
guint middle = trunc(i / 2);
i = 0;
tmp_list = CHILDREN;
while (tmp_list) {
if (i++ == middle) {
gtk_menu_shell_select_item (menu_shell, GTK_WIDGET(tmp_list->data));
return;
}
tmp_list = tmp_list->next;
}
}
void menu_position (GtkMenu *menu, gint *x, gint *y, gboolean *push_in,
gpointer data) {
/* handle given position as the center of this window (menu) */
GtkRequisition req;
#if (GTK_MAJOR_VERSION == 2)
gtk_widget_size_request(GTK_WIDGET(menu), &req);
#else
gtk_widget_get_preferred_size(GTK_WIDGET(menu), &req, NULL);
#endif
*x = menuX - (req.width >> 1);
*y = menuY - (req.height >> 1);
*push_in = TRUE;
}
gboolean Get2Numbers (char *data) {
int n, i;
n = strlen (data);
if ((n == 0) | !isdigit (data[0]))
return FALSE;
menuX = atoi (data);
i = 0;
// Skip over first number
while (isdigit (data[i])) {
i++;
if (i == n)
return FALSE;
}
// Find start of the next number
while (! isdigit (data[i])) {
i++;
if (i == n) return FALSE;
}
menuY = atoi (&data[i]);
return TRUE;
}
static void RunItem (char *cmd) {
GError *error = NULL;
//g_print ("Run: %s\n", cmd);
if (! cmd) return;
if (! g_spawn_command_line_async(cmd, &error)) {
g_print ("Error running command.\n");
g_print ("%s\n", error->message);
g_error_free (error);
}
gtk_main_quit ();
}
static void QuitMenu(char *msg) {
gtk_main_quit();
}
// return rest of the string after cut off using regexp |^pattern *= *|
char *str_starts_with_pattern (char *str, char *pattern) {
for (;;) {
if (*str == '\0') return NULL;
// pattern matches
if (*pattern == '\0') {
for (; *str == ' '; ++str);
if (*str != '=') return NULL;
for (++str; *str == ' '; ++str);
// empty string (containing only '\0') allowed
return str;
}
// in the middle of pattern
else if (*str == *pattern) {
++str;
++pattern;
}
else {
return NULL;
}
}
}
/* return -1 error
* 0 EOF
* >0 keyword
*/
/* FIXME return kind of line, menu depth, and stripped text */
int ReadLine() {
char *chop;
int i, len, Kind;
char tmp[MAX_LINE_LENGTH];
char *str2;
len = 0;
while (len == 0) {
if (fgets(Line, MAX_LINE_LENGTH, pFile) == NULL)
return 0;
strcpy (tmp, Line);
lineNum++;
// remove comments
chop = strchr (tmp, '#');
if (chop != 0)
*chop = '\0';
len = strlen (tmp);
// remove trailing spaces & CR/LF
if (len > 0) {
chop = &tmp[len - 1];
while ((chop >= tmp) && (isspace(*chop) != 0)) {
*chop = '\0';
chop--;
}
len = strlen(tmp);
}
};
if (len >= MAX_LINE_LENGTH) {
strncpy(data, tmp, MAX_LINE_LENGTH);
data[MAX_LINE_LENGTH - 1] = '\0';
return (-1);
}
// menu depth
for (depth = 0, i = 0; i < len; i++) {
if (tmp[i] == '\t')
depth++;
else
break;
}
// remove leading white space
str2 = tmp;
while ((*str2 == ' ') || (*str2 == '\t')) {
str2++;
len--;
}
for (i = 0; i <= len; i++) tmp[i] = str2[i];
if (! strcmp(tmp, "separator")) {
str2 = tmp;
Kind = 5;
}
else if ((str2 = str_starts_with_pattern (tmp, "cmd" )) != NULL) {
Kind = 2;
}
else if ((str2 = str_starts_with_pattern (tmp, "item" )) != NULL) {
Kind = 1;
}
else if ((str2 = str_starts_with_pattern (tmp, "icon" )) != NULL) {
Kind = 3;
}
else if ((str2 = str_starts_with_pattern (tmp, "submenu" )) != NULL) {
Kind = 4;
}
else if ((str2 = str_starts_with_pattern (tmp, "menupos" )) != NULL) {
Kind = 7;
}
else if ((str2 = str_starts_with_pattern (tmp, "iconsize")) != NULL) {
Kind = 6;
}
/* its a bad line */
else {
str2 = tmp;
Kind = -1;
}
strcpy (data, str2);
return Kind;
}
/*
* return 0 not_running
* 1 already_running
* 2 some_error
*/
int already_running(void) {
struct flock fl;
int fd;
char *home;
int n;
int ret = 2;
char *lock_path;
/* a buffer to hold the path name to our lock file */
if ((lock_path = malloc(MAX_PATH_LEN + 1)) == NULL) {
fprintf(stderr,
"Error, malloc failed");
exit(1);
}
/* we need to place the lock file in ENV_LOCK_FILE_PREFIX if it is set,
else in ~/.cache */
if ((home = getenv(ENV_LOCK_FILE_PREFIX)) != NULL) {
n = snprintf(lock_path, MAX_PATH_LEN, "%s/%s", home, LOCK_NAME);
} else if ((home = getenv("HOME")) != NULL) {
/* try for ~/.cache instead */
n = snprintf(lock_path, MAX_PATH_LEN, "%s/.cache/%s", home, LOCK_NAME);
} else {
fprintf(stderr,
"Error, could not get env variables " ENV_LOCK_FILE_PREFIX " or HOME\n");
goto Done;
}
if (n > MAX_PATH_LEN || lock_path == NULL) {
fprintf(stderr, "Error, path name too long: %s.\n", lock_path);
goto Done;
}
fd = open(lock_path, O_RDWR | O_CREAT, 0600);
if (fd < 0) {
fprintf(stderr, "Error opening %s.\n", lock_path);
goto Done;
}
fl.l_start = 0;
fl.l_len = 0;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(fd, F_SETLK, &fl) < 0)
ret = 1;
else
ret = 0;
Done:
free(lock_path);
return ret;
}