12 #include <X11/Xutil.h>
14 #include <X11/Xatom.h>
16 #define PROGNAME "xitems"
18 #define LEN(A) (sizeof(A)/sizeof((A)[0]))
20 /* usage -- print usage information and die. */
25 "usage: " PROGNAME " [-font font] [-bg colour] [-fg colour]\n"
26 " [-sbg colour] [-sfg colour] [-bw width]\n"
27 " [-hp padding] [-vp padding] [-x x] [-y y]\n");
31 /* die -- print formatted string, and exit with the status eval. */
33 die(int eval, const char *fmt, ...)
35 fputs(PROGNAME ": ", stderr);
39 vfprintf(stderr, fmt, argp);
51 /* doubly-linked cyclic list */
53 size_t len; /* length of string */
55 KeySym *ks; /* NoSym-terminated array of keysyms */
58 bool dirty; /* should be redrawn */
61 static struct item *first = NULL; /* first member of the list */
62 static struct item *selected = NULL;
64 /* command-line options and X resources */
65 static char *o_font = NULL;
66 static char *o_bg = NULL, *o_fg = NULL, *o_sbg = NULL, *o_sfg = NULL;
67 static int o_x = -1, o_y = -1;
69 static int o_hp = -1, o_vp = -1;
72 static Display *dpy = NULL;
75 static GC gc_sel, gc_norm;
76 static Pixmap pm_sel, pm_norm; /* background pixmaps */
77 static XFontStruct *font;
78 static int height, width; /* height and width of one item */
80 /* newsel -- mark the item at position y (from top) as selected. */
84 struct item *unselected = selected;
93 if (y >= top && y < top+height)
96 selected = selected->next;
97 } while (selected != first->prev);
100 if (unselected != selected)
101 unselected->dirty = selected->dirty = true;
104 /* redraw -- redraw the entire window */
108 struct item *it = first;
116 if (it == selected) {
124 XCopyArea(dpy, pm, win, gc, 0, 0, width, height, 0, y);
125 XDrawString(dpy, win, gc, o_hp, y + o_vp+font->ascent,
133 } while (it != first);
136 /* succeed -- exit successfully. If print is true, also print selected item. */
140 if (print && selected)
145 /* scroll -- select the previous, or next item, depending on dir. */
147 scroll(enum direction dir)
150 selected->dirty = true;
151 selected = (dir == DIR_UP) ? selected->prev : selected->next;
153 selected = (dir == DIR_UP) ? first->prev : first;
154 selected->dirty = true;
157 /* proc -- body of the main event-reading loop. */
166 static bool inwin = false;
168 XNextEvent(dpy, &ev);
170 /* XXX try to avoid full redraws */
176 newsel(ev.xbutton.y);
185 if (ev.xbutton.button == Button4) {
188 } else if (ev.xbutton.button == Button5) {
198 XLookupString(&ke, dummy, 0, &ks, NULL);
200 if (ke.state & ControlMask) {
249 /* grabptr -- try to grab pointer for a second */
253 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
256 for (i = 0; i < 1000; ++i) {
257 if (XGrabPointer(dpy, RootWindow(dpy, screen), True,
258 ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None,
259 CurrentTime) == GrabSuccess)
261 nanosleep(&ts, NULL);
263 die(1, "couldn't grab pointer");
266 /* grabkb -- try to grab keyboard for a second */
270 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
273 for (i = 0; i < 1000; ++i) {
274 if (XGrabKeyboard(dpy, RootWindow(dpy, screen), True,
275 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
277 nanosleep(&ts, NULL);
279 die(1, "couldn't grab keyboard");
282 /* setfocus -- try setting focus to the menu for a second */
286 struct timespec ts = { .tv_sec = 0, .tv_nsec = 1000000 };
290 for (i = 0; i < 1000; ++i) {
291 XGetInputFocus(dpy, &focuswin, &dummy);
294 XSetInputFocus(dpy, win, RevertToParent, CurrentTime);
295 nanosleep(&ts, NULL);
297 die(1, "couldn't grab keyboard");
301 * setupx -- create and map a window for n items; assign values to the X
310 XClassHint ch = {PROGNAME, PROGNAME};
311 XSetWindowAttributes swa = {
312 .override_redirect = True,
314 .event_mask = ExposureMask | StructureNotifyMask |
315 KeyPressMask | ButtonPressMask | ButtonReleaseMask |
316 PointerMotionMask | LeaveWindowMask | EnterWindowMask,
319 if (!(font = XLoadQueryFont(dpy, o_font)))
320 die(1, "couldn't load font");
321 height = font->ascent + font->descent + o_vp;
326 if ((w = XTextWidth(font, it->s, it->len) + o_hp*2) > width)
329 } while (it != first);
331 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), o_bg, &col, &dummy);
332 swa.background_pixel = col.pixel;
333 win = XCreateWindow(dpy, RootWindow(dpy, screen), o_x, o_y,
334 width, n*height, o_bw, CopyFromParent, CopyFromParent,
335 CopyFromParent, CWOverrideRedirect | CWBackPixel | CWEventMask |
337 XSetClassHint(dpy, win, &ch);
340 * Foreground here means the colour with which to draw the background
341 * pixmap, i.e. the actual background colour.
343 gcv.foreground = col.pixel;
344 gc_norm = XCreateGC(dpy, win, GCForeground, &gcv);
345 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), o_sbg, &col,
347 gcv.foreground = col.pixel;
348 gc_sel = XCreateGC(dpy, win, GCForeground, &gcv);
350 pm_sel = XCreatePixmap(dpy, win, width, height,
351 DefaultDepth(dpy, screen));
352 pm_norm = XCreatePixmap(dpy, win, width, height,
353 DefaultDepth(dpy, screen));
354 XFillRectangle(dpy, pm_sel, gc_sel, 0, 0, width, height);
355 XFillRectangle(dpy, pm_norm, gc_norm, 0, 0, width, height);
358 * Since the background pixmaps are already created, the GCs can be
361 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), o_fg, &col,
363 XSetForeground(dpy, gc_norm, col.pixel);
364 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), o_sfg, &col,
366 XSetForeground(dpy, gc_sel, col.pixel);
371 XMapRaised(dpy, win);
377 * Create a new struct item, and insert it a after the it item. The rest of the
378 * arguments are values that the new item receives. On success return pointer
379 * to the new item. Return NULL otherwise.
382 insitem(struct item *it, char *s, size_t len) {
384 if (!(new = calloc(1, sizeof *new)))
388 new->next = it->next;
389 it->next->prev = new;
392 new->prev = new->next = new;
395 new->ks = NULL; /* TODO */
401 * mkitems -- create a list of items from stdin, and return pointer to the
402 * new list's first element. If np is not NULL, set its value to the number of
403 * elements in the list. Return NULL on error.
408 struct item *last = NULL;
414 /* XXX take keysyms into account */
415 while ((linelen = getline(&line, &linesize, stdin)) != -1) {
417 line[--linelen] = '\0'; /* get rid of '\n' */
418 if (!(it = insitem(last, line, linelen)))
419 die(1, "couldn't insert new item");
428 return last ? last->next : NULL;
432 * {s,i}default -- return the {char *,int} value of the X default, or def
436 sdefault(char *opt, char *def)
438 char *val = XGetDefault(dpy, PROGNAME, opt);
439 return val ? val : def;
443 idefault(char *opt, int def)
445 char *val = XGetDefault(dpy, PROGNAME, opt);
446 return val ? atoi(val) : def;
449 /* nextarg -- safely skip the current command-line option */
451 nextarg(int *argcp, char **argvp[])
459 * {s,i}arg -- return the {char *,int} argument of the current command-line
463 sarg(int *argcp, char **argvp[])
465 nextarg(argcp, argvp);
470 iarg(int *argcp, char **argvp[])
472 nextarg(argcp, argvp);
473 return atoi(**argvp);
477 * xitems -- pop-up menu for X, constructed from stdin, and printing user choice
481 main(int argc, char *argv[])
485 for (--argc, ++argv; argc > 0; --argc, ++argv)
486 if (argv[0][0] == '-')
487 switch (argv[0][1]) {
489 switch (argv[0][2]) {
491 o_bg = sarg(&argc, &argv);
494 o_bw = iarg(&argc, &argv);
502 switch (argv[0][2]) {
504 o_fg = sarg(&argc, &argv);
506 case 'o': /* -font */
507 o_font = sarg(&argc, &argv);
515 o_hp = iarg(&argc, &argv);
518 switch (argv[0][2]) {
520 o_sbg = sarg(&argc, &argv);
523 o_sfg = sarg(&argc, &argv);
531 o_vp = iarg(&argc, &argv);
534 o_x = iarg(&argc, &argv);
537 o_y = iarg(&argc, &argv);
544 if (!(dpy = XOpenDisplay(NULL)))
545 die(1, "couldn't open display");
546 screen = DefaultScreen(dpy);
549 o_bg = sdefault("background", "white");
551 o_fg = sdefault("foreground", "black");
553 o_font = sdefault("font", "fixed");
555 o_sbg = sdefault("selectedBackground", "black");
557 o_sfg = sdefault("selectedForeground", "white");
559 o_bw = idefault("borderWidth", 1);
561 o_hp = idefault("horizontalPadding", 2);
563 o_vp = idefault("verticalPadding", 1);
565 if (o_x == -1 || o_y == -1) {
571 xp = (o_x == -1) ? &o_x : &i;
572 yp = (o_y == -1) ? &o_y : &i;
574 XQueryPointer(dpy, RootWindow(dpy, screen), &w, &w, xp, yp, &i,
578 if (!(first = selected = mkitems(&n))) {
580 die(1, "coulnd't create items list");