- char *p, buf[sizeof text];
- unsigned int len = 0, max = 0;
- Item *i, *new;
-
- i = NULL;
- while(fgets(buf, sizeof buf, stdin)) {
- len = strlen(buf);
- if(buf[len-1] == '\n')
- buf[--len] = '\0';
- if(!(p = strdup(buf)))
- eprint("cannot strdup %u bytes\n", len);
- if((max = MAX(max, len)) == len)
- maxname = p;
- if(!(new = malloc(sizeof *new)))
- eprint("cannot malloc %u bytes\n", sizeof *new);
- new->next = new->left = new->right = NULL;
- new->text = p;
- if(!i)
- allitems = new;
- else
- i->next = new;
- i = new;
+ char buf[sizeof text], *p;
+ Item *item, **end;
+
+ for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
+ if((p = strchr(buf, '\n')))
+ *p = '\0';
+ if(!(item = malloc(sizeof *item)))
+ eprintf("cannot malloc %u bytes\n", sizeof *item);
+ if(!(item->text = strdup(buf)))
+ eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
+ item->next = item->left = item->right = NULL;
+ inputw = MAX(inputw, dc_textw(dc, item->text));
+ }
+}
+
+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] = dc_color(dc, normbgcolor);
+ normcol[ColFG] = dc_color(dc, normfgcolor);
+ selcol[ColBG] = dc_color(dc, selbgcolor);
+ selcol[ColFG] = dc_color(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(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);