+void
+run(void) {
+ XEvent ev;
+
+ while(!XNextEvent(dc->dpy, &ev))
+ switch(ev.type) {
+ case Expose:
+ if(ev.xexpose.count == 0)
+ drawmenu();
+ break;
+ case KeyPress:
+ keypress(&ev.xkey);
+ break;
+ case SelectionNotify:
+ if(ev.xselection.property == utf8)
+ paste();
+ break;
+ case VisibilityNotify:
+ if(ev.xvisibility.state != VisibilityUnobscured)
+ XRaiseWindow(dc->dpy, win);
+ break;
+ }
+}
+
+void
+setup(void) {
+ int x, y, screen;
+ XSetWindowAttributes wa;
+#ifdef XINERAMA
+ int n;
+ XineramaScreenInfo *info;
+#endif
+
+ screen = DefaultScreen(dc->dpy);
+ root = RootWindow(dc->dpy, screen);
+ utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
+
+ normcol[ColBG] = getcolor(dc, normbgcolor);
+ normcol[ColFG] = getcolor(dc, normfgcolor);
+ selcol[ColBG] = getcolor(dc, selbgcolor);
+ selcol[ColFG] = getcolor(dc, selfgcolor);
+
+ /* menu geometry */
+ bh = dc->font.height + 2;
+ lines = MAX(lines, 0);
+ mh = (lines + 1) * bh;
+#ifdef XINERAMA
+ if((info = XineramaQueryScreens(dc->dpy, &n))) {
+ int i, di;
+ unsigned int du;
+ Window dw;
+
+ XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
+ for(i = 0; i < n; i++)
+ if((monitor == info[i].screen_number)
+ || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)))
+ break;
+ x = info[i].x_org;
+ y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
+ mw = info[i].width;
+ XFree(info);
+ }
+ else
+#endif
+ {
+ x = 0;
+ y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh;
+ mw = DisplayWidth(dc->dpy, screen);
+ }
+ /* menu window */
+ wa.override_redirect = True;
+ wa.background_pixmap = ParentRelative;
+ wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
+ win = XCreateWindow(dc->dpy, root, x, y, mw, mh, 0,
+ DefaultDepth(dc->dpy, screen), CopyFromParent,
+ DefaultVisual(dc->dpy, screen),
+ CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
+
+ grabkeyboard();
+ resizedc(dc, mw, mh);
+ inputw = MIN(inputw, mw/3);
+ promptw = prompt ? textw(dc, prompt) : 0;
+ XMapRaised(dc->dpy, win);
+ text[0] = '\0';
+ match();
+}
+
+void
+usage(void) {
+ fputs("usage: dmenu [-b] [-i] [-l lines] [-p prompt] [-fn font] [-nb color]\n"
+ " [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
+ exit(EXIT_FAILURE);
+}
+