X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=main.c;h=7b4365e59ff09b7ed349070b2c8b54a34a0848de;hb=5fd7af18c6b24bffd4701156feffcbc0db3b1d69;hp=3203014d8db280d1fd5241cedaca8d243f41559f;hpb=1716159e05e712962c3a4c60091f6cd6b573461f;p=dmenu.git diff --git a/main.c b/main.c index 3203014..7b4365e 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,4 @@ -/* - * (C)opyright MMVI Anselm R. Garbe +/* (C)opyright MMVI Anselm R. Garbe * (C)opyright MMVI Sander van Dijk * See LICENSE file for license details. */ @@ -47,7 +46,6 @@ calcoffsets(void) { if(!curr) return; - w = cmdw + 2 * SPACE; for(next = curr; next; next=next->right) { tw = textw(next->text); @@ -57,7 +55,6 @@ calcoffsets(void) { if(w > mw) break; } - w = cmdw + 2 * SPACE; for(prev = curr; prev && prev->left; prev=prev->left) { tw = textw(prev->left->text); @@ -78,18 +75,15 @@ drawmenu(void) { dc.w = mw; dc.h = mh; drawtext(NULL, dc.norm); - /* print command */ if(cmdw && item) dc.w = cmdw; drawtext(text[0] ? text : NULL, dc.norm); dc.x += cmdw; - if(curr) { dc.w = SPACE; drawtext((curr && curr->left) ? "<" : NULL, dc.norm); dc.x += dc.w; - /* determine maximum items */ for(i = curr; i != next; i=i->right) { dc.w = textw(i->text); @@ -98,7 +92,6 @@ drawmenu(void) { drawtext(i->text, (sel == i) ? dc.sel : dc.norm); dc.x += dc.w; } - dc.x = mw - SPACE; dc.w = SPACE; drawtext(next ? ">" : NULL, dc.norm); @@ -114,11 +107,9 @@ match(char *pattern) { if(!pattern) return; - plen = strlen(pattern); item = j = NULL; nitem = 0; - for(i = allitems; i; i=i->next) if(!plen || !strncmp(pattern, i->text, plen)) { if(!j) @@ -142,7 +133,6 @@ match(char *pattern) { j = i; nitem++; } - curr = prev = next = sel = item; calcoffsets(); } @@ -157,12 +147,10 @@ kpress(XKeyEvent * e) { len = strlen(text); buf[0] = 0; num = XLookupString(e, buf, sizeof(buf), &ksym, 0); - if(IsFunctionKey(ksym) || IsKeypadKey(ksym) || IsMiscFunctionKey(ksym) || IsPFKey(ksym) || IsPrivateKeypadKey(ksym)) return; - /* first check if a control mask is omitted */ if(e->state & ControlMask) { switch (ksym) { @@ -208,10 +196,8 @@ kpress(XKeyEvent * e) { } break; case XK_Return: - if(e->state & ShiftMask) { - if(text) - fprintf(stdout, "%s", text); - } + if((e->state & ShiftMask) && text) + fprintf(stdout, "%s", text); else if(sel) fprintf(stdout, "%s", sel->text); else if(text) @@ -263,7 +249,6 @@ readstdin(void) { maxname = p; max = len; } - new = emalloc(sizeof(Item)); new->next = new->left = new->right = NULL; new->text = p; @@ -285,20 +270,41 @@ DC dc = {0}; int main(int argc, char *argv[]) { + char *font = FONT; char *maxname; + char *normbg = NORMBGCOLOR; + char *normfg = NORMFGCOLOR; + char *selbg = SELBGCOLOR; + char *selfg = SELFGCOLOR; fd_set rd; + int i; struct timeval timeout; - Item *i; + Item *itm; XEvent ev; XSetWindowAttributes wa; - if(argc == 2 && !strncmp("-v", argv[1], 3)) { - fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); - exit(EXIT_SUCCESS); - } - else if(argc != 1) - eprint("usage: dmenu [-v]\n"); - + timeout.tv_usec = 0; + timeout.tv_sec = 3; + /* command line args */ + for(i = 1; i < argc; i++) + if(!strncmp(argv[i], "-font", 6)) + font = argv[++i]; + else if(!strncmp(argv[i], "-normbg", 8)) + normbg = argv[++i]; + else if(!strncmp(argv[i], "-normfg", 8)) + normfg = argv[++i]; + else if(!strncmp(argv[i], "-selbg", 7)) + selbg = argv[++i]; + else if(!strncmp(argv[i], "-selfg", 7)) + selfg = argv[++i]; + else if(!strncmp(argv[i], "-t", 3)) + timeout.tv_sec = atoi(argv[++i]); + else if(!strncmp(argv[i], "-v", 3)) { + fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); + exit(EXIT_SUCCESS); + } + else + eprint("usage: dmenu [-font ] [-{norm,sel}{bg,fg} ] [-t ] [-v]\n", stdout); dpy = XOpenDisplay(0); if(!dpy) eprint("dmenu: cannot open display\n"); @@ -313,46 +319,37 @@ main(int argc, char *argv[]) { while(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime) != GrabSuccess) usleep(1000); - - timeout.tv_usec = 0; - timeout.tv_sec = STDIN_TIMEOUT; FD_ZERO(&rd); FD_SET(STDIN_FILENO, &rd); if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1) goto UninitializedEnd; maxname = readstdin(); - /* style */ - dc.sel[ColBG] = getcolor(SELBGCOLOR); - dc.sel[ColFG] = getcolor(SELFGCOLOR); - dc.norm[ColBG] = getcolor(NORMBGCOLOR); - dc.norm[ColFG] = getcolor(NORMFGCOLOR); - setfont(FONT); - + dc.norm[ColBG] = getcolor(normbg); + dc.norm[ColFG] = getcolor(normfg); + dc.sel[ColBG] = getcolor(selbg); + dc.sel[ColFG] = getcolor(selfg); + setfont(font); + /* menu window */ wa.override_redirect = 1; wa.background_pixmap = ParentRelative; wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask; - mx = my = 0; mw = DisplayWidth(dpy, screen); mh = dc.font.height + 2; - win = XCreateWindow(dpy, root, mx, my, mw, mh, 0, DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm)); - /* pixmap */ dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen)); dc.gc = XCreateGC(dpy, root, 0, 0); XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); - if(maxname) cmdw = textw(maxname); if(cmdw > mw / 3) cmdw = mw / 3; - text[0] = 0; match(text); XMapRaised(dpy, win); @@ -374,11 +371,12 @@ main(int argc, char *argv[]) { } } + /* cleanup */ while(allitems) { - i = allitems->next; + itm = allitems->next; free(allitems->text); free(allitems); - allitems = i; + allitems = itm; } if(dc.font.set) XFreeFontSet(dpy, dc.font.set); @@ -390,6 +388,5 @@ main(int argc, char *argv[]) { UninitializedEnd: XUngrabKeyboard(dpy, CurrentTime); XCloseDisplay(dpy); - return ret; }