/* xmenu * a simple fuzzy-selection menu made with Xlib */ #include #include #include #include #include "ui.h" #include "dynarr.h" static Display *dsp; static Window win; static int scr; static unsigned long fg, bg; static XSizeHints szhint; static GC gc; static Font freg, fital; /* TODO: center menu on screen */ void ui_init(int argc, char **argv) { dsp = XOpenDisplay(""); scr = DefaultScreen(dsp); fg = BlackPixel(dsp, scr); bg = WhitePixel(dsp, scr); szhint = (XSizeHints) { 0 }; szhint.width = 350; szhint.height = 250; szhint.flags = PSize; win = XCreateSimpleWindow( dsp, DefaultRootWindow(dsp), szhint.x, szhint.y, szhint.width, szhint.height, 5 /* border width */, fg, bg); /* TODO: replace with modern XSetWMProperties */ char title[] = "xmenu"; XSetStandardProperties(dsp, win, title, title, None, argv, argc, &szhint); gc = XCreateGC(dsp, win, 0, 0); XSetBackground(dsp, gc, bg); XSetForeground(dsp, gc, fg); XSelectInput(dsp, win, ButtonPressMask | KeyPressMask | ExposureMask); XMapRaised(dsp, win); freg = XLoadFont(dsp, "-*-new century schoolbook-medium-r-*-*-24-*-*-*-*-*-*-*"); fital = XLoadFont(dsp, "-*-new century schoolbook-medium-i-*-*-24-*-*-*-*-*-*-*"); } UiKey xksym_to_uik(KeySym sym) { switch (sym) { case XK_Escape: return UIK_ESCAPE; case XK_BackSpace: return UIK_BACKSPACE; case XK_Up: return UIK_UP; case XK_Down: return UIK_DOWN; case XK_Left: return UIK_LEFT; case XK_Right: return UIK_RIGHT; case XK_Return: return UIK_RETURN; case XK_Home: return UIK_HOME; case XK_End: return UIK_END; case XK_Page_Down: return UIK_PGDN; case XK_Page_Up: return UIK_PGUP; default: return UIK_UNKNOWN; } } int ui_wait_event(UiEvent *e) { XEvent ev; KeySym sym; XNextEvent(dsp, &ev); switch (ev.type) { case KeyPress: e->type = UI_KEY_DOWN; e->key.strn = XLookupString(&ev.xkey, e->key.str, 10, &sym, 0); e->key.key = xksym_to_uik(sym); return 1; case KeyRelease: e->type = UI_KEY_UP; e->key.strn = XLookupString(&ev.xkey, e->key.str, 10, &sym, 0); e->key.key = xksym_to_uik(sym); return 1; case Expose: e->type = UI_REDRAW; return 1; } fprintf(stderr, "[Unknown event %d]\n", ev.type); return 0; } void ui_draw(Str input, int inpi, int seli, Str *optv, int optc) { XWindowAttributes attr; XClearWindow(dsp, win); XGetWindowAttributes(dsp, win, &attr); /* draw input */ XSetFont(dsp, gc, fital); XDrawString(dsp, win, gc, 16, 24, input.s, input.n); XFontStruct *f = XQueryFont(dsp, XGContextFromGC(gc)); int w = XTextWidth(f, input.s, inpi); XDrawLine(dsp, win, gc, 16 + w, 24 - f->ascent, 16 + w, 24 + f->descent); /* draw options */ XSetFont(dsp, gc, freg); for (int i = 0; i < optc; i++) { if (i == seli) { int w = XTextWidth(XQueryFont(dsp, XGContextFromGC(gc)), optv[i].s, optv[i].n); XFillRectangle(dsp, win, gc, 16, 28 + i * 24 + 8, w, 24); XSetForeground(dsp, gc, bg); XDrawString(dsp, win, gc, 16, 48 + i * 24 + 8, optv[i].s, optv[i].n); XSetForeground(dsp, gc, fg); } else { XDrawString(dsp, win, gc, 16, 48 + i * 24 + 8, optv[i].s, optv[i].n); } } } void ui_fini(void) { XUnloadFont(dsp, freg); XUnloadFont(dsp, fital); XFreeGC(dsp, gc); XDestroyWindow(dsp, win); XCloseDisplay(dsp); }