X-Git-Url: https://git.xinqibao.xyz/dmenu.git/blobdiff_plain/bba30e26863c75e66678f9d028d394883d86150a..18dcf738967a45208e880b72ce273afdd93ee6c7:/dinput.c?ds=sidebyside diff --git a/dinput.c b/dinput.c index 52118e5..0bf2679 100644 --- a/dinput.c +++ b/dinput.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -25,13 +24,13 @@ static void cleanup(void); static void drawcursor(void); static void drawinput(void); -static void eprint(const char *errstr, ...); static Bool grabkeyboard(void); -static void kpress(XKeyEvent * e); +static void kpress(XKeyEvent *e); static void run(void); static void setup(Bool topbar); #include "config.h" +#include "draw.h" /* variables */ static char *prompt = NULL; @@ -39,18 +38,19 @@ static char text[4096]; static int promptw = 0; static int ret = 0; static int screen; -static unsigned int mw, mh; static unsigned int cursor = 0; static unsigned int numlockmask = 0; +static unsigned int mw, mh; +static unsigned long normcol[ColLast]; +static unsigned long selcol[ColLast]; static Bool running = True; +static DC dc; static Display *dpy; -static Window parent, win; - -#include "draw.c" +static Window win, parent; void cleanup(void) { - dccleanup(); + cleanupdraw(&dc); XDestroyWindow(dpy, win); XUngrabKeyboard(dpy, CurrentTime); } @@ -59,9 +59,9 @@ void drawcursor(void) { XRectangle r = { dc.x, dc.y + 2, 1, dc.font.height - 2 }; - r.x += textnw(text, cursor) + dc.font.height / 2; + r.x += textnw(&dc, text, cursor) + dc.font.height / 2; - XSetForeground(dpy, dc.gc, dc.norm[ColFG]); + XSetForeground(dpy, dc.gc, normcol[ColFG]); XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); } @@ -72,30 +72,20 @@ drawinput(void) dc.y = 0; dc.w = mw; dc.h = mh; - drawtext(NULL, dc.norm); + drawtext(&dc, NULL, normcol, False); /* print prompt? */ if(prompt) { dc.w = promptw; - drawtext(prompt, dc.sel); + drawtext(&dc, prompt, selcol, False); dc.x += dc.w; } dc.w = mw - dc.x; - drawtext(*text ? text : NULL, dc.norm); + drawtext(&dc, *text ? text : NULL, normcol, False); drawcursor(); XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0); XFlush(dpy); } -void -eprint(const char *errstr, ...) { - va_list ap; - - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); - va_end(ap); - exit(EXIT_FAILURE); -} - Bool grabkeyboard(void) { unsigned int len; @@ -110,7 +100,7 @@ grabkeyboard(void) { } void -kpress(XKeyEvent * e) { +kpress(XKeyEvent *e) { char buf[sizeof text]; int num; unsigned int i, len; @@ -150,6 +140,7 @@ kpress(XKeyEvent * e) { ksym = XK_BackSpace; break; case XK_j: + case XK_m: ksym = XK_Return; break; case XK_k: @@ -173,7 +164,7 @@ kpress(XKeyEvent * e) { FILE *fp; char *s; if(!(fp = popen("sselp", "r"))) - eprint("dinput: cannot popen sselp\n"); + eprint("cannot popen sselp\n"); s = fgets(buf, sizeof buf, fp); pclose(fp); if(s == NULL) @@ -242,7 +233,7 @@ run(void) { /* main event loop */ while(running && !XNextEvent(dpy, &ev)) - switch (ev.type) { + switch(ev.type) { case KeyPress: kpress(&ev.xkey); break; @@ -278,15 +269,20 @@ setup(Bool topbar) { } XFreeModifiermap(modmap); - initfont(font); + dc.dpy = dpy; + normcol[ColBG] = getcolor(&dc, normbgcolor); + normcol[ColFG] = getcolor(&dc, normfgcolor); + selcol[ColBG] = getcolor(&dc, selbgcolor); + selcol[ColFG] = getcolor(&dc, selfgcolor); + initfont(&dc, font); - /* menu window */ + /* input window */ wa.override_redirect = True; wa.background_pixmap = ParentRelative; - wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask | VisibilityChangeMask; + wa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; - /* menu window geometry */ - mh = (dc.font.height + 2); + /* input window geometry */ + mh = dc.font.height + 2; #if XINERAMA if(parent == RootWindow(dpy, screen) && XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) { i = 0; @@ -318,9 +314,9 @@ setup(Bool topbar) { DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - dcsetup(); + setupdraw(&dc, win); if(prompt) - promptw = MIN(textw(prompt), mw / 5); + promptw = MIN(textw(&dc, prompt), mw / 5); cursor = strlen(text); XMapRaised(dpy, win); } @@ -331,6 +327,7 @@ main(int argc, char *argv[]) { Bool topbar = True; /* command line args */ + progname = argv[0]; for(i = 1; i < argc; i++) if(!strcmp(argv[i], "-b")) topbar = False; @@ -365,7 +362,7 @@ main(int argc, char *argv[]) { if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) fprintf(stderr, "dinput: warning: no locale support\n"); if(!(dpy = XOpenDisplay(NULL))) - eprint("dinput: cannot open display\n"); + eprint("cannot open display\n"); screen = DefaultScreen(dpy); if(!parent) parent = RootWindow(dpy, screen);