forked from phillbush/xmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmenu.c
1942 lines (1743 loc) · 50.3 KB
/
xmenu.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
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <locale.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/Xinerama.h>
#include <Imlib2.h>
/* macros */
#define MAXPATHS 128 /* maximal number of paths to look for icons */
#define ICONPATH "ICONPATH" /* environment variable name */
#define CLASS "XMenu"
#define LEN(x) (sizeof (x) / sizeof (x[0]))
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
#define GETNUM(n, s) { \
unsigned long __TMP__; \
if ((__TMP__ = strtoul((s), NULL, 10)) < INT_MAX) \
(n) = __TMP__; \
}
/* Actions for the main loop */
enum {
ACTION_NOP = 0,
ACTION_CLEAR = 1<<0, /* clear text */
ACTION_SELECT = 1<<1, /* select item */
ACTION_MAP = 1<<2, /* remap menu windows */
ACTION_DRAW = 1<<3, /* redraw menu windows */
ACTION_WARP = 1<<4, /* warp the pointer */
};
/* enum for keyboard menu navigation */
enum { ITEMPREV, ITEMNEXT, ITEMFIRST, ITEMLAST };
/* enum for text alignment */
enum {LeftAlignment, CenterAlignment, RightAlignment};
/* color enum */
enum {ColorFG, ColorBG, ColorLast};
/* EWMH atoms */
enum {NetWMName, NetWMWindowType, NetWMWindowTypePopupMenu, NetLast};
/* configuration structure */
struct Config {
/* the values below are set by config.h */
const char *font;
const char *background_color;
const char *foreground_color;
const char *selbackground_color;
const char *selforeground_color;
const char *separator_color;
const char *border_color;
int width_pixels;
int height_pixels;
int border_pixels;
int max_items;
int separator_pixels;
int gap_pixels;
int triangle_width;
int triangle_height;
int iconpadding;
int horzpadding;
int alignment;
/* the values below are set by options */
int monitor;
int posx, posy; /* rootmenu position */
/* the value below is computed by xmenu */
int iconsize;
};
/* draw context structure */
struct DC {
XftColor normal[ColorLast];
XftColor selected[ColorLast];
XftColor border;
XftColor separator;
GC gc;
FcPattern *pattern;
XftFont **fonts;
size_t nfonts;
};
/* menu item structure */
struct Item {
char *label; /* string to be drawed on menu */
char *output; /* string to be outputed when item is clicked */
char *file; /* filename of the icon */
int y; /* item y position relative to menu */
int h; /* item height */
int textw; /* text width */
struct Item *prev; /* previous item */
struct Item *next; /* next item */
struct Menu *submenu; /* submenu spawned by clicking on item */
Drawable sel, unsel; /* pixmap for selected and unselected item */
Imlib_Image icon;
};
/* monitor geometry structure */
struct Monitor {
int x, y, w, h; /* monitor geometry */
};
/* menu structure */
struct Menu {
struct Menu *parent; /* parent menu */
struct Item *caller; /* item that spawned the menu */
struct Item *list; /* list of items contained by the menu */
struct Item *first; /* first item displayed on the menu */
struct Item *selected; /* item currently selected in the menu */
int x, y, w, h; /* menu geometry */
int hasicon; /* whether the menu has item with icons */
int drawn; /* whether the menu was already drawn */
int maxtextw; /* maximum text width */
int level; /* menu level relative to root */
int overflow; /* whether the menu is higher than the monitor */
Window win; /* menu window to map on the screen */
XIC xic; /* input context */
};
/* X stuff */
static Display *dpy;
static Visual *visual;
static Window rootwin;
static Colormap colormap;
static XrmDatabase xdb;
static XClassHint classh;
static char *xrm;
static struct DC dc;
static Atom utf8string;
static Atom wmdelete;
static Atom netatom[NetLast];
static int screen;
static int depth;
static XIM xim;
/* flags */
static int iflag = 0; /* whether to disable icons */
static int rflag = 0; /* whether to disable right-click */
static int mflag = 0; /* whether the user specified a monitor with -p */
static int pflag = 0; /* whether the user specified a position with -p */
static int wflag = 0; /* whether to let the window manager control XMenu */
static int rootmodeflag = 0; /* wheter to run in root mode */
static int passclickflag = 0; /* whether to pass click to root window */
static int firsttime = 1; /* set to 0 after first run */
/* arguments */
static unsigned int button = 0; /* button to trigger pmenu in root mode */
static unsigned int modifier = 0; /* modifier to trigger pmenu */
/* icons paths */
static char *iconstring = NULL; /* string read from getenv */
static char *iconpaths[MAXPATHS]; /* paths to icon directories */
static int niconpaths = 0; /* number of paths to icon directories */
/* include config variable */
#include "config.h"
/* show usage */
static void
usage(void)
{
(void)fprintf(stderr, "usage: xmenu [-irw] [-p position] [title]\n");
exit(1);
}
/* maximum int */
static int
max(int x, int y)
{
return x > y ? x : y;
}
/* minimum int */
static int
min(int x, int y)
{
return x < y ? x : y;
}
/* call malloc checking for error */
static void *
emalloc(size_t size)
{
void *p;
if ((p = malloc(size)) == NULL)
err(1, "malloc");
return p;
}
/* call strdup checking for error */
static char *
estrdup(const char *s)
{
char *t;
if ((t = strdup(s)) == NULL)
err(1, "strdup");
return t;
}
/* parse position string from -p, put results on config.posx, config.posy, and config.monitor */
static void
parseposition(char *optarg)
{
long n;
char *s = optarg;
char *endp;
n = strtol(s, &endp, 10);
if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != 'x')
goto error;
config.posx = n;
s = endp+1;
n = strtol(s, &endp, 10);
if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s)
goto error;
config.posy = n;
if (*endp == ':') {
s = endp+1;
mflag = 1;
if (strncasecmp(s, "CUR", 3) == 0) {
config.monitor = -1;
endp = s+3;
} else {
n = strtol(s, &endp, 10);
if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0')
goto error;
config.monitor = n;
}
} else if (*endp != '\0') {
goto error;
}
return;
error:
errx(1, "improper position: %s", optarg);
}
/* get configuration from X resources */
static void
getresources(void)
{
char *type;
XrmValue xval;
if (xrm == NULL || xdb == NULL)
return;
if (XrmGetResource(xdb, "xmenu.borderWidth", "*", &type, &xval) == True)
GETNUM(config.border_pixels, xval.addr)
if (XrmGetResource(xdb, "xmenu.separatorWidth", "*", &type, &xval) == True)
GETNUM(config.separator_pixels, xval.addr)
if (XrmGetResource(xdb, "xmenu.height", "*", &type, &xval) == True)
GETNUM(config.height_pixels, xval.addr)
if (XrmGetResource(xdb, "xmenu.width", "*", &type, &xval) == True)
GETNUM(config.width_pixels, xval.addr)
if (XrmGetResource(xdb, "xmenu.gap", "*", &type, &xval) == True)
GETNUM(config.gap_pixels, xval.addr)
if (XrmGetResource(xdb, "xmenu.maxItems", "*", &type, &xval) == True)
GETNUM(config.max_items, xval.addr)
if (XrmGetResource(xdb, "xmenu.background", "*", &type, &xval) == True)
config.background_color = xval.addr;
if (XrmGetResource(xdb, "xmenu.foreground", "*", &type, &xval) == True)
config.foreground_color = xval.addr;
if (XrmGetResource(xdb, "xmenu.selbackground", "*", &type, &xval) == True)
config.selbackground_color = xval.addr;
if (XrmGetResource(xdb, "xmenu.selforeground", "*", &type, &xval) == True)
config.selforeground_color = xval.addr;
if (XrmGetResource(xdb, "xmenu.separator", "*", &type, &xval) == True)
config.separator_color = xval.addr;
if (XrmGetResource(xdb, "xmenu.border", "*", &type, &xval) == True)
config.border_color = xval.addr;
if (XrmGetResource(xdb, "xmenu.font", "*", &type, &xval) == True)
config.font = xval.addr;
if (XrmGetResource(xdb, "xmenu.alignment", "*", &type, &xval) == True) {
if (strcasecmp(xval.addr, "center") == 0)
config.alignment = CenterAlignment;
else if (strcasecmp(xval.addr, "left") == 0)
config.alignment = LeftAlignment;
else if (strcasecmp(xval.addr, "right") == 0)
config.alignment = RightAlignment;
}
}
/* set button global variable */
static void
setbutton(char *s)
{
size_t len;
if ((len = strlen(s)) < 1)
return;
switch (s[len-1]) {
case '1': button = Button1; break;
case '2': button = Button2; break;
case '3': button = Button3; break;
default: button = atoi(&s[len-1]); break;
}
}
/* set modifier global variable */
static void
setmodifier(char *s)
{
size_t len;
if ((len = strlen(s)) < 1)
return;
switch (s[len-1]) {
case '1': modifier = Mod1Mask; break;
case '2': modifier = Mod2Mask; break;
case '3': modifier = Mod3Mask; break;
case '4': modifier = Mod4Mask; break;
case '5': modifier = Mod5Mask; break;
default:
if (strcasecmp(s, "Alt") == 0) {
modifier = Mod1Mask;
} else if (strcasecmp(s, "Super") == 0) {
modifier = Mod4Mask;
}
break;
}
}
/* parse icon path string */
static void
parseiconpaths(char *s)
{
if (s == NULL)
return;
free(iconstring);
iconstring = estrdup(s);
niconpaths = 0;
for (s = strtok(iconstring, ":"); s != NULL; s = strtok(NULL, ":")) {
if (niconpaths < MAXPATHS) {
iconpaths[niconpaths++] = s;
}
}
}
/* get configuration from command-line options */
static void
getoptions(int argc, char *argv[])
{
int ch;
char *s, *t;
classh.res_class = CLASS;
classh.res_name = argv[0];
if ((s = strrchr(argv[0], '/')) != NULL)
classh.res_name = s + 1;
parseiconpaths(getenv(ICONPATH));
while ((ch = getopt(argc, argv, "ip:rwx:X:")) != -1) {
switch (ch) {
case 'i':
iflag = 1;
break;
case 'p':
pflag = 1;
parseposition(optarg);
break;
case 'r':
rflag = 1;
break;
case 'w':
wflag = 1;
break;
case 'X':
passclickflag = 1;
/* PASSTHROUGH */
case 'x':
rootmodeflag = 1;
s = optarg;
setbutton(s);
if ((t = strchr(s, '-')) == NULL)
return;
*t = '\0';
setmodifier(s);
break;
default:
usage();
break;
}
}
if (rootmodeflag)
wflag = 0;
argc -= optind;
argv += optind;
if (argc != 0)
usage();
return;
}
/* parse font string */
static void
parsefonts(const char *s)
{
const char *p;
char buf[1024];
size_t nfont = 0;
dc.nfonts = 1;
for (p = s; *p; p++)
if (*p == ',')
dc.nfonts++;
if ((dc.fonts = calloc(dc.nfonts, sizeof *dc.fonts)) == NULL)
err(1, "calloc");
p = s;
while (*p != '\0') {
size_t i;
i = 0;
while (isspace(*p))
p++;
while (i < sizeof buf && *p != '\0' && *p != ',')
buf[i++] = *p++;
if (i >= sizeof buf)
errx(1, "font name too long");
if (*p == ',')
p++;
buf[i] = '\0';
if (nfont == 0)
if ((dc.pattern = FcNameParse((FcChar8 *)buf)) == NULL)
errx(1, "the first font in the cache must be loaded from a font string");
if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL)
errx(1, "could not load font");
}
}
/* get color from color string */
static void
ealloccolor(const char *s, XftColor *color)
{
if(!XftColorAllocName(dpy, visual, colormap, s, color))
errx(1, "could not allocate color: %s", s);
}
/* query monitor information and cursor position */
static void
getmonitor(struct Monitor *mon)
{
XineramaScreenInfo *info = NULL;
Window dw; /* dummy variable */
int di; /* dummy variable */
unsigned du; /* dummy variable */
int cursx, cursy; /* cursor position */
int nmons;
int i;
XQueryPointer(dpy, rootwin, &dw, &dw, &cursx, &cursy, &di, &di, &du);
mon->x = mon->y = 0;
mon->w = DisplayWidth(dpy, screen);
mon->h = DisplayHeight(dpy, screen);
if ((info = XineramaQueryScreens(dpy, &nmons)) != NULL) {
int selmon = 0;
if (!mflag || config.monitor < 0 || config.monitor >= nmons) {
for (i = 0; i < nmons; i++) {
if (BETWEEN(cursx, info[i].x_org, info[i].x_org + info[i].width) &&
BETWEEN(cursy, info[i].y_org, info[i].y_org + info[i].height)) {
selmon = i;
break;
}
}
} else {
selmon = config.monitor;
}
mon->x = info[selmon].x_org;
mon->y = info[selmon].y_org;
mon->w = info[selmon].width;
mon->h = info[selmon].height;
XFree(info);
}
if (!pflag) {
config.posx = cursx;
config.posy = cursy;
} else if (mflag) {
config.posx += mon->x;
config.posy += mon->y;
}
}
/* init draw context */
static void
initdc(void)
{
/* get color pixels */
ealloccolor(config.background_color, &dc.normal[ColorBG]);
ealloccolor(config.foreground_color, &dc.normal[ColorFG]);
ealloccolor(config.selbackground_color, &dc.selected[ColorBG]);
ealloccolor(config.selforeground_color, &dc.selected[ColorFG]);
ealloccolor(config.separator_color, &dc.separator);
ealloccolor(config.border_color, &dc.border);
/* parse fonts */
parsefonts(config.font);
/* create common GC */
dc.gc = XCreateGC(dpy, rootwin, 0, NULL);
}
/* calculate icon size */
static void
initiconsize(void)
{
config.iconsize = config.height_pixels - config.iconpadding * 2;
}
/* intern atoms */
static void
initatoms(void)
{
utf8string = XInternAtom(dpy, "UTF8_STRING", False);
wmdelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
netatom[NetWMWindowTypePopupMenu] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
}
/* allocate an item */
static struct Item *
allocitem(const char *label, const char *output, char *file)
{
struct Item *item;
item = emalloc(sizeof *item);
if (label == NULL) {
item->label = NULL;
item->output = NULL;
} else {
item->label = estrdup(label);
if (label == output) {
item->output = item->label;
} else {
item->output = estrdup(output);
}
}
if (file == NULL) {
item->file = NULL;
} else {
item->file = estrdup(file);
}
item->y = 0;
item->h = 0;
item->next = NULL;
item->submenu = NULL;
item->icon = NULL;
return item;
}
/* allocate a menu and create its window */
static struct Menu *
allocmenu(struct Menu *parent, struct Item *list, int level)
{
XSetWindowAttributes swa;
struct Menu *menu;
menu = emalloc(sizeof *menu);
*menu = (struct Menu){
.parent = parent,
.list = list,
.level = level,
.first = NULL,
};
swa.override_redirect = (wflag) ? False : True;
swa.background_pixel = dc.normal[ColorBG].pixel;
swa.border_pixel = dc.border.pixel;
swa.save_under = True; /* pop-up windows should save_under*/
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask
| PointerMotionMask | LeaveWindowMask;
if (wflag)
swa.event_mask |= StructureNotifyMask;
menu->win = XCreateWindow(dpy, rootwin, 0, 0, 1, 1, config.border_pixels,
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBackPixel |
CWBorderPixel | CWEventMask | CWSaveUnder,
&swa);
menu->xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
XNClientWindow, menu->win, XNFocusWindow, menu->win, NULL);
if (menu->xic == NULL)
errx(1, "XCreateIC: could not obtain input method");
return menu;
}
/* build the menu tree */
static struct Menu *
buildmenutree(int level, const char *label, const char *output, char *file)
{
static struct Menu *prevmenu = NULL; /* menu the previous item was added to */
static struct Menu *rootmenu = NULL; /* menu to be returned */
struct Item *curritem = NULL; /* item currently being read */
struct Item *item; /* dummy item for loops */
struct Menu *menu; /* dummy menu for loops */
int i;
/* create the item */
curritem = allocitem(label, output, file);
/* put the item in the menu tree */
if (prevmenu == NULL) { /* there is no menu yet */
menu = allocmenu(NULL, curritem, level);
rootmenu = menu;
prevmenu = menu;
curritem->prev = NULL;
} else if (level < prevmenu->level) { /* item is continuation of a parent menu */
/* go up the menu tree until find the menu this item continues */
for (menu = prevmenu, i = level;
menu != NULL && i != prevmenu->level;
menu = menu->parent, i++)
;
if (menu == NULL)
errx(1, "improper indentation detected");
/* find last item in the new menu */
for (item = menu->list; item->next != NULL; item = item->next)
;
prevmenu = menu;
item->next = curritem;
curritem->prev = item;
} else if (level == prevmenu->level) { /* item is a continuation of current menu */
/* find last item in the previous menu */
for (item = prevmenu->list; item->next != NULL; item = item->next)
;
item->next = curritem;
curritem->prev = item;
} else if (level > prevmenu->level) { /* item begins a new menu */
menu = allocmenu(prevmenu, curritem, level);
/* find last item in the previous menu */
for (item = prevmenu->list; item->next != NULL; item = item->next)
;
/* a separator is no valid root for a submenu */
if (!item->label)
errx(1, "a separator is no valid root for a submenu");
prevmenu = menu;
menu->caller = item;
item->submenu = menu;
curritem->prev = NULL;
}
if (curritem->file)
prevmenu->hasicon = 1;
return rootmenu;
}
/* create menus and items from the stdin */
static struct Menu *
parsestdin(void)
{
struct Menu *rootmenu;
char *s, buf[BUFSIZ];
char *file, *label, *output;
int level = 0;
rootmenu = NULL;
while (fgets(buf, BUFSIZ, stdin) != NULL) {
/* get the indentation level */
level = strspn(buf, "\t");
/* get the label */
s = level + buf;
label = strtok(s, "\t\n");
/* get the filename */
file = NULL;
if (label != NULL && strncmp(label, "IMG:", 4) == 0) {
file = label + 4;
label = strtok(NULL, "\t\n");
}
/* get the output */
output = strtok(NULL, "\n");
if (output == NULL) {
output = label;
} else {
while (*output == '\t')
output++;
}
rootmenu = buildmenutree(level, label, output, file);
}
return rootmenu;
}
/* get next utf8 char from s return its codepoint and set next_ret to pointer to end of character */
static FcChar32
getnextutf8char(const char *s, const char **next_ret)
{
static const unsigned char utfbyte[] = {0x80, 0x00, 0xC0, 0xE0, 0xF0};
static const unsigned char utfmask[] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static const FcChar32 utfmin[] = {0, 0x00, 0x80, 0x800, 0x10000};
static const FcChar32 utfmax[] = {0, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
/* 0xFFFD is the replacement character, used to represent unknown characters */
static const FcChar32 unknown = 0xFFFD;
FcChar32 ucode; /* FcChar32 type holds 32 bits */
size_t usize = 0; /* n' of bytes of the utf8 character */
size_t i;
*next_ret = s+1;
/* get code of first byte of utf8 character */
for (i = 0; i < sizeof utfmask; i++) {
if (((unsigned char)*s & utfmask[i]) == utfbyte[i]) {
usize = i;
ucode = (unsigned char)*s & ~utfmask[i];
break;
}
}
/* if first byte is a continuation byte or is not allowed, return unknown */
if (i == sizeof utfmask || usize == 0)
return unknown;
/* check the other usize-1 bytes */
s++;
for (i = 1; i < usize; i++) {
*next_ret = s+1;
/* if byte is nul or is not a continuation byte, return unknown */
if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0])
return unknown;
/* 6 is the number of relevant bits in the continuation byte */
ucode = (ucode << 6) | ((unsigned char)*s & ~utfmask[0]);
s++;
}
/* check if ucode is invalid or in utf-16 surrogate halves */
if (!BETWEEN(ucode, utfmin[usize], utfmax[usize])
|| BETWEEN (ucode, 0xD800, 0xDFFF))
return unknown;
return ucode;
}
/* get which font contains a given code point */
static XftFont *
getfontucode(FcChar32 ucode)
{
FcCharSet *fccharset = NULL;
FcPattern *fcpattern = NULL;
FcPattern *match = NULL;
XftFont *retfont = NULL;
XftResult result;
size_t i;
for (i = 0; i < dc.nfonts; i++)
if (XftCharExists(dpy, dc.fonts[i], ucode) == FcTrue)
return dc.fonts[i];
/* create a charset containing our code point */
fccharset = FcCharSetCreate();
FcCharSetAddChar(fccharset, ucode);
/* create a pattern akin to the dc.pattern but containing our charset */
if (fccharset) {
fcpattern = FcPatternDuplicate(dc.pattern);
FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
}
/* find pattern matching fcpattern */
if (fcpattern) {
FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
FcDefaultSubstitute(fcpattern);
match = XftFontMatch(dpy, screen, fcpattern, &result);
}
/* if found a pattern, open its font */
if (match) {
retfont = XftFontOpenPattern(dpy, match);
if (retfont && XftCharExists(dpy, retfont, ucode) == FcTrue) {
if ((dc.fonts = realloc(dc.fonts, dc.nfonts+1)) == NULL)
err(1, "realloc");
dc.fonts[dc.nfonts] = retfont;
return dc.fonts[dc.nfonts++];
} else {
XftFontClose(dpy, retfont);
}
}
/* in case no fount was found, return the first one */
return dc.fonts[0];
}
/* draw text into XftDraw, return width of text glyphs */
static int
drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text)
{
int textwidth = 0;
while (*text) {
XftFont *currfont;
XGlyphInfo ext;
FcChar32 ucode;
const char *next;
size_t len;
ucode = getnextutf8char(text, &next);
currfont = getfontucode(ucode);
len = next - text;
XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)text, len, &ext);
textwidth += ext.xOff;
if (draw) {
int texty;
texty = y + (h - (currfont->ascent + currfont->descent))/2 + currfont->ascent;
XftDrawStringUtf8(draw, color, currfont, x, texty, (XftChar8 *)text, len);
x += ext.xOff;
}
text = next;
}
return textwidth;
}
/* setup the height, width and icon of the items of a menu */
static void
setupitems(struct Menu *menu, struct Monitor *mon)
{
Pixmap pix;
struct Item *item;
int itemwidth;
int menuh;
int maxh;
menu->first = menu->list;
menu->w = config.width_pixels;
menu->maxtextw = 0;
menuh = 0;
maxh = config.max_items > 3 ? (2 + config.max_items) * config.height_pixels : mon->h;
for (item = menu->list; item != NULL; item = item->next) {
item->y = menuh;
if (item->label == NULL) /* height for separator item */
item->h = config.separator_pixels;
else
item->h = config.height_pixels;
menuh += item->h;
if (!menu->overflow) {
if (menu->h + config.height_pixels * 2 < maxh - config.border_pixels * 2) {
menu->h = menuh;
} else {
menu->overflow = 1;
menu->h += config.height_pixels * 2;
}
}
if (item->label)
item->textw = drawtext(NULL, NULL, 0, 0, 0, item->label);
else
item->textw = 0;
/*
* set menu width
*
* the item width depends on the size of its label (item->textw),
* and it is only used to calculate the width of the menu (which
* is equal to the width of the largest item).
*
* the horizontal padding appears 4 times through the width of a
* item: before and after its icon, and before and after its triangle.
* if the iflag is set (icons are disabled) then the horizontal
* padding appears 3 times: before the label and around the triangle.
*/
itemwidth = item->textw + config.triangle_width + config.horzpadding * 3;
itemwidth += (iflag || !menu->hasicon) ? 0 : config.iconsize + config.horzpadding;
menu->w = max(menu->w, itemwidth);
menu->maxtextw = max(menu->maxtextw, item->textw);
}
if (!menu->overflow) {
XSetWindowBackground(dpy, menu->win, dc.normal[ColorBG].pixel);
} else {
pix = XCreatePixmap(dpy, menu->win, menu->w, menu->h, depth);
XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel);
XFillRectangle(dpy, pix, dc.gc, 0, 0, menu->w, menu->h);
XSetForeground(dpy, dc.gc, dc.normal[ColorFG].pixel);
XFillPolygon(
dpy, pix, dc.gc,
(XPoint []){
{menu->w / 2 - 3, config.height_pixels / 2 + 2},
{3, -4},
{3, 4},
{-6, 0},
},
4, Convex, CoordModePrevious
);
XFillPolygon(
dpy, pix, dc.gc,
(XPoint []){
{menu->w / 2 - 3, menu->h - config.height_pixels / 2 - 2},
{3, 4},
{3, -4},
{-6, 0},
},
4, Convex, CoordModePrevious
);
XSetWindowBackgroundPixmap(dpy, menu->win, pix);
XFreePixmap(dpy, pix);
}
}
/* setup the position of a menu */
static void
setupmenupos(struct Menu *menu, struct Monitor *mon)
{
int width, height;
width = menu->w + config.border_pixels * 2;
height = menu->h + config.border_pixels * 2;
if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
if (pflag || (config.posx >= mon->x && mon->x + mon->w - config.posx >= width))
menu->x = config.posx;
else if (config.posx > width)
menu->x = config.posx - width;
if (pflag || (config.posy >= mon->y && mon->y + mon->h - config.posy >= height))
menu->y = config.posy;
else if (mon->y + mon->h > height)
menu->y = mon->y + mon->h - height;
} else { /* else, calculate in respect to parent menu */
int parentwidth;
parentwidth = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels;
if (mon->x + mon->w - parentwidth >= width)
menu->x = parentwidth;
else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels)
menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels;
if (mon->y + mon->h - (menu->caller->y + menu->parent->y) >= height)
menu->y = menu->caller->y + menu->parent->y;
else if (mon->y + mon->h > height)
menu->y = mon->y + mon->h - height;
}
}
/* recursivelly setup menu configuration and its pixmap */
static void
setupmenu(struct Menu *menu, struct Monitor *mon)
{
char *title;
struct Item *item;
XWindowChanges changes;
XSizeHints sizeh;
XTextProperty wintitle;
/* setup size and position of menus */
setupitems(menu, mon);
setupmenupos(menu, mon);
/* update menu geometry */
changes.height = menu->h;
changes.width = menu->w;
changes.x = menu->x;
changes.y = menu->y;
XConfigureWindow(dpy, menu->win, CWWidth | CWHeight | CWX | CWY, &changes);
if (firsttime) {
/* set window title (used if wflag is on) */
if (menu->parent == NULL) {
title = classh.res_name;
} else if (menu->caller->output) {
title = menu->caller->output;
} else {
title = "\0";
}