+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 != None)
+ paste(ev.xselection.property);
+ break;
+ case VisibilityNotify:
+ if(ev.xvisibility.state != VisibilityUnobscured)
+ XRaiseWindow(dc.dpy, win);
+ break;
+ }
+}
+
+void
+setup(void) {
+ int x, y;
+#ifdef XINERAMA
+ int i, n;
+ XineramaScreenInfo *info;
+#endif
+ XSetWindowAttributes wa;
+
+ normcol[ColBG] = getcolor(&dc, normbgcolor);
+ normcol[ColFG] = getcolor(&dc, normfgcolor);
+ selcol[ColBG] = getcolor(&dc, selbgcolor);
+ selcol[ColFG] = getcolor(&dc, selfgcolor);
+
+ /* input window geometry */
+ mh = (dc.font.height + 2) * (lines + 1);
+#ifdef XINERAMA
+ if((info = XineramaQueryScreens(dc.dpy, &n))) {
+ int di;
+ unsigned int du;
+ Window dw;
+
+ XQueryPointer(dc.dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
+ for(i = 0; i < n; i++)
+ if(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);
+ }
+
+ /* input 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);
+
+ match();
+ grabkeyboard();
+ setupdraw(&dc, win);
+ inputw = MIN(inputw, mw / 3);
+ utf8 = XInternAtom(dc.dpy, "UTF8_STRING", False);
+ XMapRaised(dc.dpy, win);
+}
+
+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);
+}
+