-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpmenu.c
2173 lines (1960 loc) · 53.2 KB
/
pmenu.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 <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <locale.h>
#include <math.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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/shape.h>
#include <X11/extensions/Xinerama.h>
#include <Imlib2.h>
#include "ctrlfnt.h"
#define SHELL "sh"
#define CLASS "PMenu"
#define NAME "pmenu"
#define TTPAD 4 /* padding for the tooltip */
#define TTVERT 30 /* vertical distance from mouse to place tooltip */
#define MAXPATHS 128 /* maximal number of paths to look for icons */
#define ICONPATH "ICONPATH" /* environment variable name */
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
#define FLAG(f, b) (((f) & (b)) == (b))
#define RETURN_FAILURE (-1)
#define RETURN_SUCCESS 0
#define ATOMS \
X(NET_WM_WINDOW_TYPE) \
X(NET_WM_WINDOW_TYPE_TOOLTIP) \
X(NET_WM_WINDOW_TYPE_POPUP_MENU)
#define RESOURCES \
X(_SELECT_BG, "Selbackground", "selbackground") \
X(_SELECT_FG, "Selforeground", "selforeground") \
X(BORDER_CLR, "BorderColor", "borderColor") \
X(BORDER_WID, "BorderWidth", "borderWidth") \
X(DIAMETER, "DiameterWidth", "diameterWidth") \
X(FACE_NAME, "FaceName", "faceName") \
X(FACE_SIZE, "FaceSize", "faceSize") \
X(NORMAL_BG, "Background", "background") \
X(NORMAL_FG, "Foreground", "foreground") \
X(SELECT_BG, "ActiveBackground", "activeBackground") \
X(SELECT_FG, "ActiveForeground", "activeForeground") \
X(SHADOW_BOT, "BottomShadowColor", "bottomShadowColor") \
X(SHADOW_MID, "MiddleShadowColor", "middleShadowColor") \
X(SHADOW_TOP, "TopShadowColor", "topShadowColor") \
X(SHADOW_WID, "ShadowThickness", "shadowThickness")
#define DEF_BORDER 2
#define DEF_DIAMETER 200
#define DEF_COLOR_BG (XRenderColor){ .red = 0x0000, .green = 0x0000, .blue = 0x0000, .alpha = 0xFFFF }
#define DEF_COLOR_FG (XRenderColor){ .red = 0xFFFF, .green = 0xFFFF, .blue = 0xFFFF, .alpha = 0xFFFF }
#define DEF_COLOR_SELBG (XRenderColor){ .red = 0x3400, .green = 0x6500, .blue = 0xA400, .alpha = 0xFFFF }
#define DEF_COLOR_SELFG (XRenderColor){ .red = 0xFFFF, .green = 0xFFFF, .blue = 0xFFFF, .alpha = 0xFFFF }
#define DEF_COLOR_BORDER (XRenderColor){ .red = 0x3100, .green = 0x3100, .blue = 0x3100, .alpha = 0xFFFF }
#define SEPARATOR_BEG 0.14
#define SEPARATOR_END 0.37
enum {
SCHEME_NORMAL,
SCHEME_SELECT,
SCHEME_BORDER,
SCHEME_LAST,
};
enum {
COLOR_BG = 0,
COLOR_FG = 1,
COLOR_TOP = 0,
COLOR_BOT = 1,
COLOR_LAST = 2,
};
enum {
TRIANGLE_WIDTH = 3,
TRIANGLE_HEIGHT = 7,
TRIANGLE_DISTANCE = 6,
TTBORDER = 1,
};
enum Resource {
#define X(res, s1, s2) res,
RESOURCES
NRESOURCES
#undef X
};
enum Atoms {
#define X(atom) atom,
ATOMS
NATOMS
#undef X
};
enum {NO_CMD = 0, CMD_NOTRUN = 1, CMD_RUN = 2};
struct Slice {
struct Slice *prev, *next;
struct Menu *submenu; /* submenu spawned by clicking on slice */
struct Menu *parent;
char *label; /* string to be drawed on the slice */
char *output; /* string to be outputed when slice is clicked */
char *file; /* filename of the icon */
size_t labellen; /* strlen(label) */
int iscmd; /* whether output is actually a command to popen */
unsigned slicen;
int x, y; /* position of the pointer of the slice */
int labelx, labely; /* position of the label */
int iconx, icony; /* position of the icon */
double anglea, angleb; /* angle of the borders of the slice */
int drawn; /* whether the pixmap have been drawn */
Drawable pixmap; /* pixmap containing the pie menu with the slice selected */
Picture picture; /* XRender picture */
Imlib_Image icon; /* icon */
int ttdrawn; /* whether the pixmap for the tooltip have been drawn */
int ttw; /* tooltip width */
Window tooltip; /* tooltip that appears when hovering a slice */
Drawable ttpix; /* pixmap for the tooltip */
Picture ttpict; /* pixmap for the tooltip */
};
struct Menu {
struct Menu *parent; /* parent menu */
struct Slice *caller; /* slice that spawned the menu */
struct Slice *list; /* list of slices contained by the pie menu */
struct Slice *selected; /* slice currently selected in the menu */
unsigned nslices; /* number of slices */
int x, y; /* menu position */
double half; /* angle of half a slice of the pie menu */
int level; /* menu level relative to root */
int drawn; /* whether the pixmap have been drawn */
Drawable pixmap; /* pixmap to draw the menu on */
Picture picture; /* XRender picture */
Window win; /* menu window to map on the screen */
};
struct Pie {
Display *display;
Visual *visual;
Window rootwin;
Colormap colormap;
XRenderPictFormat *xformat, *alphaformat;
int screen;
int depth;
Atom atoms[NATOMS];
XClassHint classh;
Window dummy;
struct {
XRenderColor chans;
Pixmap pix;
Picture pict;
} colors[SCHEME_LAST][COLOR_LAST];
CtrlFontSet *fontset;
int fonth;
Pixmap clip;
Picture gradient;
struct {
XrmClass class;
XrmName name;
} application, resources[NRESOURCES];
int fulldiameter; /* diameter of the pie + 2*border*/
int diameter; /* diameter of the pie */
int radius; /* radius of the pie */
int border; /* border of the pie */
int tooltiph;
int triangleinner;
int triangleouter;
int separatorbeg;
int separatorend;
double triangleangle;
double innerangle;
double outerangle;
};
/* The pie bitmap structure */
static struct Pie pie = { 0 };
/* flags */
static int harddiameter = 0;
static int execcommand = 0;
static int rootmodeflag = 0; /* wheter to run in root mode */
static int nowarpflag = 0; /* whether to disable pointer warping */
static int passclickflag = 0; /* whether to pass click to root window */
/* 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 */
static void
usage(void)
{
(void)fprintf(stderr, "usage: pmenu [-ew] [-d diameter] [-N name] [(-x|-X) [modifier-]button]\n");
exit(1);
}
static Window
createwindow(int width, int height, long eventmask)
{
return XCreateWindow(
pie.display,
pie.rootwin,
0, 0,
width,
height,
0,
pie.depth,
InputOutput,
pie.visual,
CWBackPixel | CWEventMask | CWColormap |
CWBorderPixel | CWOverrideRedirect | CWSaveUnder,
&(XSetWindowAttributes){
.border_pixel = 0,
.background_pixel = 0,
.colormap = pie.colormap,
.event_mask = eventmask,
.save_under = True, /* pop-up windows should save_under */
.override_redirect = True,
}
);
}
/* call strdup checking for error */
static char *
estrdup(const char *s)
{
char *t;
if ((t = strdup(s)) == NULL)
err(1, "strdup");
return t;
}
/* call fork checking for error; exit on error */
static pid_t
efork(void)
{
pid_t pid;
if ((pid = fork()) < 0)
err(1, "fork");
return pid;
}
/* call execlp on sh -c checking for error; exit on error */
static void
eexecsh(const char *cmd)
{
if (execlp(SHELL, SHELL, "-c", cmd, NULL) == -1) {
err(1, SHELL);
}
}
/* set button global variable */
static void
setbutton(char *s)
{
char *endp;
long l;
l = strtol(s, &endp, 10);
if (s[0] != '\0' && *endp == '\0' && l > 0 && l <= 100)
button = l;
}
/* set modifier global variable */
static void
setmodifier(char *s)
{
if (strncasecmp(s, "Mod", 3) == 0)
s += 3;
if (*s == '\0')
return;
switch (s[0]) {
case '0': modifier = AnyModifier; break;
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 options */
static void
getoptions(int argc, char **argv)
{
double l;
int ch;
char *endp, *s, *t;
pie.classh.res_class = CLASS;
pie.classh.res_name = getenv("RESOURCES_NAME");
if (pie.classh.res_name == NULL && argv[0] != NULL && argv[0][0] != '\0') {
if ((pie.classh.res_name = strrchr(argv[0], '/')) != NULL) {
pie.classh.res_name++;
} else {
pie.classh.res_name = argv[0];
}
}
if (pie.classh.res_name == NULL)
pie.classh.res_name = NAME;
parseiconpaths(getenv(ICONPATH));
while ((ch = getopt(argc, argv, "d:eN:wx:X:P:r:m:p")) != -1) {
switch (ch) {
case 'd':
l = strtol(optarg, &endp, 10);
if (optarg[0] != '\0' && *endp == '\0' && l > 0 && l <= 100) {
harddiameter = 1;
pie.diameter = l;
}
break;
case 'e':
execcommand = !execcommand;
break;
case 'N':
pie.classh.res_name = optarg;
break;
case 'w':
nowarpflag = 1;
break;
case 'X':
passclickflag = 1;
/* PASSTHROUGH */
case 'x':
rootmodeflag = 1;
s = optarg;
setmodifier(s);
t = s;
if ((t = strchr(s, '-')) != NULL)
t++;
setbutton(t);
break;
/* the options below are deprecated and may be removed in the future */
case 'P':
parseiconpaths(optarg);
break;
case 'p':
passclickflag = 1;
break;
case 'r':
rootmodeflag = 1;
setbutton(optarg);
break;
case 'm':
setmodifier(optarg);
break;
default:
usage();
break;
}
}
if (argc - optind > 0) {
usage();
}
}
/* call malloc checking for error */
static void *
emalloc(size_t size)
{
void *p;
if ((p = malloc(size)) == NULL)
err(1, "malloc");
return p;
}
/* allocate an slice */
static struct Slice *
allocslice(const char *label, const char *output, char *file)
{
struct Slice *slice;
slice = emalloc(sizeof *slice);
slice->label = (label != NULL) ? estrdup(label) : NULL;
slice->file = (file != NULL) ? estrdup(file) : NULL;
slice->labellen = (slice->label != NULL) ? strlen(slice->label) : 0;
slice->y = 0;
slice->next = NULL;
slice->submenu = NULL;
slice->icon = NULL;
if (output && *output == '$') {
output++;
while (isspace(*output))
output++;
slice->output = estrdup(output);
slice->iscmd = CMD_NOTRUN;
} else {
slice->output = (label == output) ? slice->label : estrdup(output);
slice->iscmd = NO_CMD;
}
return slice;
}
/* allocate a menu */
static struct Menu *
allocmenu(struct Menu *parent, struct Slice *list, int level)
{
XSizeHints sizeh;
struct Menu *menu;
menu = emalloc(sizeof *menu);
/* create menu window */
menu->win = createwindow(
pie.fulldiameter, pie.fulldiameter,
KeyPressMask | ButtonPressMask |
ButtonReleaseMask | PointerMotionMask | LeaveWindowMask
);
/* Set window type */
XChangeProperty(pie.display, menu->win, pie.atoms[NET_WM_WINDOW_TYPE], XA_ATOM, 32,
PropModeReplace, (unsigned char *)&pie.atoms[NET_WM_WINDOW_TYPE_POPUP_MENU], 1);
XShapeCombineMask(pie.display, menu->win, ShapeClip, 0, 0, pie.clip, ShapeSet);
XShapeCombineMask(pie.display, menu->win, ShapeBounding, 0, 0, pie.clip, ShapeSet);
/* set window manager hints */
sizeh.flags = USPosition | PMaxSize | PMinSize;
sizeh.min_width = sizeh.max_width = pie.fulldiameter;
sizeh.min_height = sizeh.max_height = pie.fulldiameter;
XSetWMProperties(pie.display, menu->win, NULL, NULL, NULL, 0, &sizeh, NULL, &pie.classh);
/* set menu variables */
menu->parent = parent;
menu->list = list;
menu->caller = NULL;
menu->selected = NULL;
menu->nslices = 0;
menu->x = 0;
menu->y = 0;
menu->level = level;
/* create pixmap and picture */
menu->pixmap = XCreatePixmap(
pie.display,
menu->win,
pie.fulldiameter,
pie.fulldiameter,
pie.depth
);
menu->picture = XRenderCreatePicture(
pie.display,
menu->pixmap,
pie.xformat,
0,
NULL
);
menu->drawn = 0;
return menu;
}
/* build the menu tree */
static struct Menu *
buildmenutree(struct Menu *rootmenu, int level, const char *label, const char *output, char *file)
{
static struct Menu *prevmenu; /* menu the previous slice was added to */
struct Slice *currslice = NULL; /* slice currently being read */
struct Slice *slice; /* dummy slice for loops */
struct Menu *menu; /* dummy menu for loops */
int i;
if (rootmenu == NULL)
prevmenu = NULL;
/* create the slice */
currslice = allocslice(label, output, file);
/* put the slice in the menu tree */
if (prevmenu == NULL) { /* there is no menu yet */
menu = allocmenu(NULL, currslice, level);
rootmenu = menu;
prevmenu = menu;
currslice->prev = NULL;
} else if (level < prevmenu->level) { /* slice is continuation of a parent menu */
/* go up the menu tree until find the menu this slice 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 slice in the new menu */
for (slice = menu->list; slice->next != NULL; slice = slice->next)
;
prevmenu = menu;
slice->next = currslice;
currslice->prev = slice;
} else if (level == prevmenu->level) { /* slice is a continuation of current menu */
/* find last slice in the previous menu */
for (slice = prevmenu->list; slice->next != NULL; slice = slice->next)
;
slice->next = currslice;
currslice->prev = slice;
} else if (level > prevmenu->level) { /* slice begins a new menu */
menu = allocmenu(prevmenu, currslice, level);
/* find last slice in the previous menu */
for (slice = prevmenu->list; slice->next != NULL; slice = slice->next)
;
prevmenu = menu;
menu->caller = slice;
slice->submenu = menu;
currslice->prev = NULL;
}
prevmenu->nslices++;
return rootmenu;
}
/* create menus and slices from the stdin */
static struct Menu *
parse(FILE *fp, int initlevel)
{
struct Menu *rootmenu;
char *s, buf[BUFSIZ];
char *file, *label, *output;
int level;
rootmenu = NULL;
while (fgets(buf, BUFSIZ, fp) != NULL) {
/* get the indentation level */
level = strspn(buf, "\t");
/* get the label */
s = level + buf;
label = strtok(s, "\t\n");
if (label == NULL)
errx(1, "empty item");
/* 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(rootmenu, initlevel + level, label, output, file);
}
return rootmenu;
}
/* check if path is absolute or relative to current directory */
static int
isabsolute(const char *s)
{
return s[0] == '/' || (s[0] == '.' && (s[1] == '/' || (s[1] == '.' && s[2] == '/')));
}
/* load image from file and scale it to size; return the image and its size */
static Imlib_Image
loadicon(const char *file, int size, int *width_ret, int *height_ret)
{
Imlib_Image icon = NULL;
Imlib_Load_Error errcode;
char path[PATH_MAX];
const char *errstr;
int width;
int height;
int i;
if (*file == '\0') {
warnx("could not load icon (file name is blank)");
return NULL;
}
if (isabsolute(file))
icon = imlib_load_image_with_error_return(file, &errcode);
else {
for (i = 0; i < niconpaths; i++) {
snprintf(path, sizeof(path), "%s/%s", iconpaths[i], file);
icon = imlib_load_image_with_error_return(path, &errcode);
if (icon != NULL || errcode != IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST) {
break;
}
}
}
if (icon == NULL) {
switch (errcode) {
case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
errstr = "file does not exist";
break;
case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
errstr = "file is directory";
break;
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
errstr = "permission denied";
break;
case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
errstr = "unknown file format";
break;
case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
errstr = "path too long";
break;
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
errstr = "improper path";
break;
case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
errstr = "too many symbolic links";
break;
case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
errstr = "out of memory";
break;
case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
errstr = "out of file descriptors";
break;
default:
errstr = "unknown error";
break;
}
warnx("could not load icon (%s): %s", errstr, file);
return NULL;
}
imlib_context_set_image(icon);
width = imlib_image_get_width();
height = imlib_image_get_height();
if (width > height) {
*width_ret = size;
*height_ret = (height * size) / width;
} else {
*width_ret = (width * size) / height;
*height_ret = size;
}
icon = imlib_create_cropped_scaled_image(0, 0, width, height,
*width_ret, *height_ret);
return icon;
}
/* setup position of and content of menu's slices */
/* recursivelly setup menu configuration and its pixmap */
static void
setslices(struct Menu *menu)
{
struct Slice *slice;
double a = 0.0;
unsigned n = 0;
int textwidth;
int w, h;
menu->half = M_PI / menu->nslices;
for (slice = menu->list; slice; slice = slice->next) {
slice->parent = menu;
slice->slicen = n++;
slice->anglea = a - menu->half;
slice->angleb = a + menu->half;
/* get length of slice->label rendered in the font */
if (slice->label != NULL)
textwidth = ctrlfnt_width(pie.fontset, slice->label, strlen(slice->label));
else
textwidth = 0;
/* get position of slice's label */
slice->labelx = pie.border + pie.radius + ((pie.radius*2)/3 * cos(a)) - (textwidth / 2);
slice->labely = pie.border + pie.radius - ((pie.radius*2)/3 * sin(a)) - (pie.fonth / 2);
/* get position of submenu */
slice->x = pie.radius + (pie.diameter * (cos(a) * 0.9));
slice->y = pie.radius - (pie.diameter * (sin(a) * 0.9));
/* create icon */
if (slice->file != NULL) {
int maxiconsize = (pie.radius + 1) / 2;
int iconw, iconh; /* icon width and height */
int iconsize; /* requested icon size */
int xdiff, ydiff;
xdiff = pie.radius * 0.5 - (pie.radius * (cos(menu->half) * 0.8));
ydiff = pie.radius * (sin(menu->half) * 0.8);
iconsize = sqrt(xdiff * xdiff + ydiff * ydiff);
iconsize = MIN(maxiconsize, iconsize);
if ((slice->icon = loadicon(slice->file, iconsize, &iconw, &iconh)) != NULL) {
slice->iconx = pie.border + pie.radius + (pie.radius * (cos(a) * 0.6)) - iconw / 2;
slice->icony = pie.border + pie.radius - (pie.radius * (sin(a) * 0.6)) - iconh / 2;
}
free(slice->file);
slice->file = NULL;
}
/* create pixmap */
slice->pixmap = XCreatePixmap(
pie.display,
menu->win,
pie.fulldiameter,
pie.fulldiameter,
pie.depth
);
slice->picture = XRenderCreatePicture(
pie.display,
slice->pixmap,
pie.xformat,
0,
NULL
);
slice->drawn = 0;
/* create tooltip */
slice->ttdrawn = 0;
if (textwidth > 0) {
slice->ttw = textwidth + 2 * TTPAD;
w = slice->ttw + TTBORDER * 2;
h = pie.tooltiph + TTBORDER * 2;
slice->tooltip = createwindow(w, h, 0);
slice->ttpix = XCreatePixmap(
pie.display,
slice->tooltip,
w, h,
pie.depth
);
slice->ttpict = XRenderCreatePicture(
pie.display,
slice->ttpix,
pie.xformat,
0, NULL
);
XChangeProperty(
pie.display,
slice->tooltip,
pie.atoms[NET_WM_WINDOW_TYPE],
XA_ATOM, 32,
PropModeReplace,
(unsigned char *)&pie.atoms[NET_WM_WINDOW_TYPE_TOOLTIP],
1
);
} else {
slice->ttw = 0;
slice->tooltip = None;
slice->ttpix = None;
slice->ttpict = None;
}
/* call recursivelly */
if (slice->submenu != NULL) {
setslices(slice->submenu);
}
a += menu->half * 2;
}
}
/* query monitor information and cursor position */
static void
getmonitor(XRectangle *monitor, XPoint *pointer)
{
XineramaScreenInfo *info = NULL;
Window dw; /* dummy variable */
int di; /* dummy variable */
unsigned du; /* dummy variable */
int nmons;
int i;
int x, y;
XQueryPointer(pie.display, pie.rootwin, &dw, &dw, &x, &y, &di, &di, &du);
pointer->x = x;
pointer->y = y;
monitor->x = monitor->y = 0;
monitor->width = DisplayWidth(pie.display, pie.screen);
monitor->height = DisplayHeight(pie.display, pie.screen);
if ((info = XineramaQueryScreens(pie.display, &nmons)) != NULL) {
int selmon = 0;
for (i = 0; i < nmons; i++) {
if (BETWEEN(pointer->x, info[i].x_org, info[i].x_org + info[i].width) &&
BETWEEN(pointer->y, info[i].y_org, info[i].y_org + info[i].height)) {
selmon = i;
break;
}
}
monitor->x = info[selmon].x_org;
monitor->y = info[selmon].y_org;
monitor->width = info[selmon].width;
monitor->height = info[selmon].height;
XFree(info);
}
}
/* try to grab pointer, we may have to wait for another process to ungrab */
static void
grabpointer(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
int i;
for (i = 0; i < 1000; i++) {
if (XGrabPointer(pie.display, pie.rootwin, True, ButtonPressMask,
GrabModeAsync, GrabModeAsync, None,
None, CurrentTime) == GrabSuccess)
return;
nanosleep(&ts, NULL);
}
errx(1, "could not grab pointer");
}
/* try to grab keyboard, we may have to wait for another process to ungrab */
static void
grabkeyboard(void)
{
struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
int i;
for (i = 0; i < 1000; i++) {
if (XGrabKeyboard(pie.display, pie.rootwin, True, GrabModeAsync,
GrabModeAsync, CurrentTime) == GrabSuccess)
return;
nanosleep(&ts, NULL);
}
errx(1, "could not grab keyboard");
}
/* setup the position of a menu */
static void
placemenu(struct Menu *menu, XRectangle *monitor, XPoint *pointer)
{
struct Slice *slice;
XWindowChanges changes;
Window w1; /* dummy variable */
int x, y; /* position of the center of the menu */
Bool ret;
if (menu->parent == NULL) {
x = pointer->x;
y = pointer->y;
} else {
ret = XTranslateCoordinates(pie.display, menu->parent->win, pie.rootwin,
menu->caller->x, menu->caller->y,
&x, &y, &w1);
if (ret == False)
errx(EXIT_FAILURE, "menus are on different screens");
}
menu->x = monitor->x;
menu->y = monitor->y;
if (x - monitor->x >= pie.radius + pie.border) {
if (monitor->x + monitor->width - x >= pie.radius + pie.border)
menu->x = x - pie.radius - pie.border;
else if (monitor->x + monitor->width >= pie.fulldiameter)
menu->x = monitor->x + monitor->width - pie.fulldiameter;
}
if (y - monitor->y >= pie.radius + pie.border) {
if (monitor->y + monitor->height - y >= pie.radius + pie.border)
menu->y = y - pie.radius - pie.border;
else if (monitor->y + monitor->height >= pie.fulldiameter)
menu->y = monitor->y + monitor->height - pie.fulldiameter;
}
changes.x = menu->x;
changes.y = menu->y;
XConfigureWindow(pie.display, menu->win, CWX | CWY, &changes);
for (slice = menu->list; slice != NULL; slice = slice->next) {
if (slice->submenu != NULL) {
placemenu(slice->submenu, monitor, pointer);
}
}
}
/* get menu of given window */
static struct Menu *
getmenu(struct Menu *currmenu, Window win)
{
struct Menu *menu;
for (menu = currmenu; menu != NULL; menu = menu->parent)
if (menu->win == win)
return menu;
return NULL;
}
/* get slice of given menu and position */
static struct Slice *
getslice(struct Menu *menu, int x, int y)
{
struct Slice *slice;
double angle;
int r;
if (menu == NULL)
return NULL;
x -= pie.border + pie.radius;
y -= pie.border + pie.radius;
y = -y;
/* if the cursor is in the middle circle, it is in no slice */
r = sqrt(x * x + y * y);
if (r <= pie.separatorbeg)
return NULL;
angle = atan2(y, x);
if (angle < 0.0) {
if (angle > -menu->half)
return menu->list;
angle = (2 * M_PI) + angle;
}
for (slice = menu->list; slice; slice = slice->next)
if (angle >= slice->anglea && angle < slice->angleb)
return slice;
return NULL;
}
/* map tooltip and place it on given position */
static void
maptooltip(struct Slice *slice, XRectangle *monitor, XPoint *tooltippos)
{
tooltippos->y += TTVERT;
if (slice->icon == NULL || slice->label == NULL)
return;
if (tooltippos->y + pie.tooltiph + 2 > monitor->y + monitor->height)
tooltippos->y = monitor->y + monitor->height - pie.tooltiph - 2;
if (tooltippos->x + slice->ttw + 2 > monitor->x + monitor->width)
tooltippos->x = monitor->x + monitor->width - slice->ttw - 2;
XMoveWindow(pie.display, slice->tooltip, tooltippos->x, tooltippos->y);