Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
8 #include <X11/keysym.h>
10 #include <X11/Xutil.h>
13 typedef struct Item Item
;
16 Item
*next
; /* traverses all items */
17 Item
*left
, *right
; /* traverses items matching current search pattern */
20 /* forward declarations */
21 static void appenditem(Item
*i
, Item
**list
, Item
**last
);
22 static void calcoffsetsh(void);
23 static void calcoffsetsv(void);
24 static char *cistrstr(const char *s
, const char *sub
);
25 static void cleanup(void);
26 static void dinput(void);
27 static void drawitem(const char *s
, unsigned long col
[ColLast
]);
28 static void drawmenuh(void);
29 static void drawmenuv(void);
30 static void match(void);
31 static void readstdin(void);
34 static char **argp
= NULL
;
35 static char *maxname
= NULL
;
36 static unsigned int cmdw
= 0;
37 static unsigned int lines
= 0;
38 static Item
*allitems
= NULL
; /* first of all items */
39 static Item
*item
= NULL
; /* first of pattern matching items */
40 static Item
*sel
= NULL
;
41 static Item
*next
= NULL
;
42 static Item
*prev
= NULL
;
43 static Item
*curr
= NULL
;
44 static int (*fstrncmp
)(const char *, const char *, size_t) = strncmp
;
45 static char *(*fstrstr
)(const char *, const char *) = strstr
;
46 static void (*calcoffsets
)(void) = calcoffsetsh
;
49 appenditem(Item
*i
, Item
**list
, Item
**last
) {
63 w
= promptw
+ cmdw
+ textw(&dc
, "<") + textw(&dc
, ">");
64 for(x
= w
, next
= curr
; next
; next
= next
->right
)
65 if((x
+= MIN(textw(&dc
, next
->text
), mw
/ 3)) > mw
)
67 for(x
= w
, prev
= curr
; prev
&& prev
->left
; prev
= prev
->left
)
68 if((x
+= MIN(textw(&dc
, prev
->left
->text
), mw
/ 3)) > mw
)
77 for(i
= 0; i
< lines
&& next
; i
++)
79 mh
= (dc
.font
.height
+ 2) * (i
+ 1);
80 for(i
= 0; i
< lines
&& prev
&& prev
->left
; i
++)
85 cistrstr(const char *s
, const char *sub
) {
91 if((c
= tolower(*sub
++)) != '\0') {
95 if((csub
= *s
++) == '\0')
98 while(tolower(csub
) != c
);
100 while(strncasecmp(s
, sub
, len
) != 0);
111 itm
= allitems
->next
;
112 free(allitems
->text
);
117 XDestroyWindow(dpy
, win
);
118 XUngrabKeyboard(dpy
, CurrentTime
);
127 execvp("dinput", argp
);
128 eprint("cannot exec dinput\n");
137 drawbox(&dc
, normcol
);
138 dc
.h
= dc
.font
.height
+ 2;
139 dc
.y
= topbar
? 0 : mh
- dc
.h
;
143 drawbox(&dc
, selcol
);
144 drawtext(&dc
, prompt
, selcol
);
149 if(cmdw
&& item
&& lines
== 0)
151 drawtext(&dc
, text
, normcol
);
156 commitdraw(&dc
, win
);
160 drawitem(const char *s
, unsigned long col
[ColLast
]) {
162 unsigned int w
= textnw(&dc
, text
, strlen(text
));
165 drawtext(&dc
, s
, col
);
166 for(p
= fstrstr(s
, text
); *text
&& (p
= fstrstr(p
, text
)); p
++)
167 drawline(&dc
, textnw(&dc
, s
, p
-s
) + dc
.h
/2 - 1, dc
.h
-2, w
, 1, col
);
175 dc
.w
= textw(&dc
, "<");
176 drawtext(&dc
, curr
->left
? "<" : NULL
, normcol
);
178 for(i
= curr
; i
!= next
; i
= i
->right
) {
179 dc
.w
= MIN(textw(&dc
, i
->text
), mw
/ 3);
180 drawitem(i
->text
, (sel
== i
) ? selcol
: normcol
);
183 dc
.w
= textw(&dc
, ">");
185 drawtext(&dc
, next
? ">" : NULL
, normcol
);
191 XWindowAttributes wa
;
193 dc
.y
= topbar
? dc
.h
: 0;
195 for(i
= curr
; i
!= next
; i
= i
->right
) {
196 drawitem(i
->text
, (sel
== i
) ? selcol
: normcol
);
199 if(!XGetWindowAttributes(dpy
, win
, &wa
))
200 eprint("cannot get window attributes");
201 XMoveResizeWindow(dpy
, win
, wa
.x
, wa
.y
+ (topbar
? 0 : wa
.height
- mh
), mw
, mh
);
205 kpress(XKeyEvent
*e
) {
206 char buf
[sizeof text
];
212 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
213 if(ksym
== XK_KP_Enter
)
215 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
216 ksym
= (ksym
- XK_KP_0
) + XK_0
;
217 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
218 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
219 || IsPrivateKeypadKey(ksym
))
221 /* first check if a control mask is omitted */
222 if(e
->state
& ControlMask
) {
223 switch(tolower(ksym
)) {
265 while(i
-- > 0 && text
[i
] == ' ');
266 while(i
-- > 0 && text
[i
] != ' ');
274 num
= MIN(num
, sizeof text
);
275 if(num
&& !iscntrl((int) buf
[0])) {
276 memcpy(text
+ len
, buf
, num
+ 1);
284 for(i
= 1; len
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[len
- i
]); i
++);
294 while(sel
&& sel
->right
)
305 if(!sel
|| !sel
->left
)
308 if(sel
->right
== curr
) {
326 if(e
->state
& ShiftMask
)
328 fprintf(stdout
, "%s", sel
? sel
->text
: text
);
333 if(!sel
|| !sel
->right
)
343 strncpy(text
, sel
->text
, sizeof text
);
353 Item
*i
, *itemend
, *lexact
, *lprefix
, *lsubstr
, *exactend
, *prefixend
, *substrend
;
356 item
= lexact
= lprefix
= lsubstr
= itemend
= exactend
= prefixend
= substrend
= NULL
;
357 for(i
= allitems
; i
; i
= i
->next
)
358 if(!fstrncmp(text
, i
->text
, len
+ 1))
359 appenditem(i
, &lexact
, &exactend
);
360 else if(!fstrncmp(text
, i
->text
, len
))
361 appenditem(i
, &lprefix
, &prefixend
);
362 else if(fstrstr(i
->text
, text
))
363 appenditem(i
, &lsubstr
, &substrend
);
370 itemend
->right
= lprefix
;
371 lprefix
->left
= itemend
;
379 itemend
->right
= lsubstr
;
380 lsubstr
->left
= itemend
;
385 curr
= prev
= next
= sel
= item
;
391 char *p
, buf
[sizeof text
];
392 unsigned int len
= 0, max
= 0;
396 while(fgets(buf
, sizeof buf
, stdin
)) {
398 if(buf
[len
-1] == '\n')
400 if(!(p
= strdup(buf
)))
401 eprint("cannot strdup %u bytes\n", len
);
402 if((max
= MAX(max
, len
)) == len
)
404 if(!(new = malloc(sizeof *new)))
405 eprint("cannot malloc %u bytes\n", sizeof *new);
406 new->next
= new->left
= new->right
= NULL
;
417 main(int argc
, char *argv
[]) {
420 /* command line args */
422 for(i
= 1; i
< argc
; i
++)
423 if(!strcmp(argv
[i
], "-i")) {
424 fstrncmp
= strncasecmp
;
427 else if(!strcmp(argv
[i
], "-b"))
429 else if(!strcmp(argv
[i
], "-l")) {
430 if(++i
< argc
) lines
= atoi(argv
[i
]);
432 calcoffsets
= calcoffsetsv
;
434 else if(!strcmp(argv
[i
], "-fn")) {
435 if(++i
< argc
) font
= argv
[i
];
437 else if(!strcmp(argv
[i
], "-nb")) {
438 if(++i
< argc
) normbgcolor
= argv
[i
];
440 else if(!strcmp(argv
[i
], "-nf")) {
441 if(++i
< argc
) normfgcolor
= argv
[i
];
443 else if(!strcmp(argv
[i
], "-p")) {
444 if(++i
< argc
) prompt
= argv
[i
];
446 else if(!strcmp(argv
[i
], "-sb")) {
447 if(++i
< argc
) selbgcolor
= argv
[i
];
449 else if(!strcmp(argv
[i
], "-sf")) {
450 if(++i
< argc
) selfgcolor
= argv
[i
];
452 else if(!strcmp(argv
[i
], "-v")) {
453 printf("dmenu-"VERSION
", © 2006-2010 dmenu engineers, see LICENSE for details\n");
457 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
458 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr
);
461 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
462 fprintf(stderr
, "dmenu: warning: no locale support\n");
463 if(!(dpy
= XOpenDisplay(NULL
)))
464 eprint("cannot open display\n");
465 if(atexit(&cleanup
) != 0)
466 eprint("cannot register cleanup\n");
467 screen
= DefaultScreen(dpy
);
468 root
= RootWindow(dpy
, screen
);
469 if(!(argp
= malloc(sizeof *argp
* (argc
+2))))
470 eprint("cannot malloc %u bytes\n", sizeof *argp
* (argc
+2));
471 memcpy(argp
+ 2, argv
+ 1, sizeof *argp
* argc
);
477 cmdw
= MIN(textw(&dc
, maxname
), mw
/ 3);