]> git.armaanb.net Git - dmenu.git/blobdiff - main.c
fixed fallback
[dmenu.git] / main.c
diff --git a/main.c b/main.c
index 0f07773029c7912ced5e4122d6fc2c3ce547442f..fcabf01226f212e9495d3abc593b4f35c495b6e4 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,7 +1,4 @@
-/* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
- * (C)opyright MMVI-MMVII Sander van Dijk <a dot h dot vandijk at gmail dot com>
- * See LICENSE file for license details.
- */
+/* See LICENSE file for copyright and license details. */
 #include "dmenu.h"
 #include <ctype.h>
 #include <locale.h>
@@ -108,11 +105,17 @@ drawmenu(void) {
        XFlush(dpy);
 }
 
-static void
+static Bool
 grabkeyboard(void) {
-       while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
-                        GrabModeAsync, CurrentTime) != GrabSuccess)
+       unsigned int len;
+
+       for(len = 1000; len; len--) {
+               if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
+                       == GrabSuccess)
+                       break;
                usleep(1000);
+       }
+       return len > 0;
 }
 
 static unsigned long
@@ -130,6 +133,8 @@ initfont(const char *fontstr) {
        char *def, **missing;
        int i, n;
 
+       if(!fontstr || fontstr[0] == '\0')
+               eprint("error, cannot load font: '%s'\n", fontstr);
        missing = NULL;
        if(dc.font.set)
                XFreeFontSet(dpy, dc.font.set);
@@ -155,14 +160,24 @@ initfont(const char *fontstr) {
                if(dc.font.xfont)
                        XFreeFont(dpy, dc.font.xfont);
                dc.font.xfont = NULL;
-               if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
-                       eprint("error, cannot load font: '%s'\n", fontstr);
+               if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))) {
+                       if(!(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
+                               eprint("error, cannot load font: '%s'\n", fontstr);
+               }
                dc.font.ascent = dc.font.xfont->ascent;
                dc.font.descent = dc.font.xfont->descent;
        }
        dc.font.height = dc.font.ascent + dc.font.descent;
 }
 
+static int
+strido(const char *text, const char *pattern) {
+       for(; *text && *pattern; text++)
+               if (*text == *pattern)
+                       pattern++;
+       return !*pattern;
+}                                  
+
 static void
 match(char *pattern) {
        unsigned int plen;
@@ -187,6 +202,19 @@ match(char *pattern) {
        for(i = allitems; i; i=i->next)
                if(plen && strncmp(pattern, i->text, plen)
                                && strstr(i->text, pattern)) {
+                       if(!j)                               
+                               item = i;                              
+                       else                                     
+                               j->right = i;                          
+                       i->left = j;      
+                       i->right = NULL;                         
+                       j = i;                                      
+                       nitem++;                                       
+               }                                              
+       for(i = allitems; i; i=i->next)                            
+               if(plen && strncmp(pattern, i->text, plen)             
+                               && !strstr(i->text, pattern)          
+                               && strido(i->text,pattern)) { 
                        if(!j)
                                item = i;
                        else
@@ -210,6 +238,13 @@ kpress(XKeyEvent * e) {
        len = strlen(text);
        buf[0] = 0;
        num = XLookupString(e, buf, sizeof buf, &ksym, 0);
+       if(IsKeypadKey(ksym)) { 
+               if(ksym == XK_KP_Enter) {
+                       ksym = XK_Return;
+               } else if(ksym >= XK_KP_0 && ksym <= XK_KP_9) {
+                       ksym = (ksym - XK_KP_0) + XK_0;
+               }
+       }
        if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
                        || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
                        || IsPrivateKeypadKey(ksym))
@@ -423,29 +458,29 @@ main(int argc, char *argv[]) {
 
        /* command line args */
        for(i = 1; i < argc; i++)
-               if(!strncmp(argv[i], "-b", 3)) {
+               if(!strcmp(argv[i], "-b")) {
                        bottom = True;
                }
-               else if(!strncmp(argv[i], "-fn", 4)) {
+               else if(!strcmp(argv[i], "-fn")) {
                        if(++i < argc) font = argv[i];
                }
-               else if(!strncmp(argv[i], "-nb", 4)) {
+               else if(!strcmp(argv[i], "-nb")) {
                        if(++i < argc) normbg = argv[i];
                }
-               else if(!strncmp(argv[i], "-nf", 4)) {
+               else if(!strcmp(argv[i], "-nf")) {
                        if(++i < argc) normfg = argv[i];
                }
-               else if(!strncmp(argv[i], "-p", 3)) {
+               else if(!strcmp(argv[i], "-p")) {
                        if(++i < argc) prompt = argv[i];
                }
-               else if(!strncmp(argv[i], "-sb", 4)) {
+               else if(!strcmp(argv[i], "-sb")) {
                        if(++i < argc) selbg = argv[i];
                }
-               else if(!strncmp(argv[i], "-sf", 4)) {
+               else if(!strcmp(argv[i], "-sf")) {
                        if(++i < argc) selfg = argv[i];
                }
-               else if(!strncmp(argv[i], "-v", 3))
-                       eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
+               else if(!strcmp(argv[i], "-v"))
+                       eprint("dmenu-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
                else
                        usage();
        setlocale(LC_CTYPE, "");
@@ -456,10 +491,10 @@ main(int argc, char *argv[]) {
        root = RootWindow(dpy, screen);
        if(isatty(STDIN_FILENO)) {
                maxname = readstdin();
-               grabkeyboard();
+               running = grabkeyboard();
        }
        else { /* prevent keypress loss */
-               grabkeyboard();
+               running = grabkeyboard();
                maxname = readstdin();
        }
        /* init modifier map */