- if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
- fprintf(stderr, "dmenu: warning: no locale support\n");
- if(!(dpy = XOpenDisplay(NULL)))
- eprint("cannot open display\n");
- if(atexit(&cleanup) != 0)
- eprint("cannot register cleanup\n");
- screen = DefaultScreen(dpy);
- root = RootWindow(dpy, screen);
- if(!(argp = malloc(sizeof *argp * (argc+2))))
- eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
- memcpy(argp + 2, argv + 1, sizeof *argp * argc);
-
- readstdin();
- grabkeyboard();
- setup(lines);
- if(maxname)
- cmdw = MIN(textw(&dc, maxname), mw / 3);
+ }
+}
+
+void
+setup(void) {
+ int x, y, screen = DefaultScreen(dc->dpy);
+ Window root = RootWindow(dc->dpy, screen);
+ XSetWindowAttributes swa;
+ XIM xim;
+#ifdef XINERAMA
+ int n;
+ XineramaScreenInfo *info;
+#endif
+
+ normcol[ColBG] = getcolor(dc, normbgcolor);
+ normcol[ColFG] = getcolor(dc, normfgcolor);
+ selcol[ColBG] = getcolor(dc, selbgcolor);
+ selcol[ColFG] = getcolor(dc, selfgcolor);
+
+ utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
+
+ /* 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 w, dw;
+ XWindowAttributes wa;
+
+ XGetInputFocus(dc->dpy, &w, &di);
+ if(w != root && w != PointerRoot && w != None && XGetWindowAttributes(dc->dpy, w, &wa))
+ XTranslateCoordinates(dc->dpy, w, root, wa.x, wa.y, &x, &y, &dw);
+ else
+ XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du);
+ for(i = 0; i < n-1; 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);
+ }
+ promptw = prompt ? textw(dc, prompt) : 0;
+ inputw = MIN(inputw, mw/3);