Xinqi Bao's Git

fixed vlist select
[dmenu.git] / dmenu.c
1 /* See LICENSE file for copyright and license details. */
2 #include <ctype.h>
3 #include <locale.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <X11/keysym.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include "dmenu.h"
12
13 typedef struct Item Item;
14 struct Item {
15 char *text;
16 Item *next; /* traverses all items */
17 Item *left, *right; /* traverses items matching current search pattern */
18 };
19
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(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);
32
33 /* variables */
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;
47
48 void
49 appenditem(Item *i, Item **list, Item **last) {
50 if(!(*last))
51 *list = i;
52 else
53 (*last)->right = i;
54 i->left = *last;
55 i->right = NULL;
56 *last = i;
57 }
58
59 void
60 calcoffsetsh(void) {
61 unsigned int w, x;
62
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)
66 break;
67 for(x = w, prev = curr; prev && prev->left; prev = prev->left)
68 if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
69 break;
70 }
71
72 void
73 calcoffsetsv(void) {
74 unsigned int i;
75
76 next = prev = curr;
77 for(i = 0; i < lines && next; i++)
78 next = next->right;
79 mh = (dc.font.height + 2) * (i + 1);
80 for(i = 0; i < lines && prev && prev->left; i++)
81 prev = prev->left;
82 }
83
84 char *
85 cistrstr(const char *s, const char *sub) {
86 int c, csub;
87 unsigned int len;
88
89 if(!sub)
90 return (char *)s;
91 if((c = tolower(*sub++)) != '\0') {
92 len = strlen(sub);
93 do {
94 do {
95 if((csub = *s++) == '\0')
96 return NULL;
97 }
98 while(tolower(csub) != c);
99 }
100 while(strncasecmp(s, sub, len) != 0);
101 s--;
102 }
103 return (char *)s;
104 }
105
106 void
107 cleanup(void) {
108 Item *itm;
109
110 while(allitems) {
111 itm = allitems->next;
112 free(allitems->text);
113 free(allitems);
114 allitems = itm;
115 }
116 cleanupdraw(&dc);
117 XDestroyWindow(dpy, win);
118 XUngrabKeyboard(dpy, CurrentTime);
119 XCloseDisplay(dpy);
120 }
121
122 void
123 dinput(void) {
124 cleanup();
125 argp[0] = "dinput";
126 argp[1] = text;
127 execvp("dinput", argp);
128 eprint("cannot exec dinput\n");
129 }
130
131 void
132 drawbar(void) {
133 dc.x = 0;
134 dc.y = 0;
135 dc.w = mw;
136 dc.h = mh;
137 drawbox(&dc, normcol);
138 dc.h = dc.font.height + 2;
139 dc.y = topbar ? 0 : mh - dc.h;
140 /* print prompt? */
141 if(prompt) {
142 dc.w = promptw;
143 drawtext(&dc, prompt, selcol);
144 dc.x += dc.w;
145 }
146 dc.w = mw - dc.x;
147 /* print command */
148 if(cmdw && item && lines == 0)
149 dc.w = cmdw;
150 drawtext(&dc, text, normcol);
151 if(lines > 0)
152 drawmenuv();
153 else if(curr)
154 drawmenuh();
155 commitdraw(&dc, win);
156 }
157
158 void
159 drawitem(char *s, unsigned long col[ColLast]) {
160 drawbox(&dc, col);
161 drawtext(&dc, s, col);
162 }
163
164 void
165 drawmenuh(void) {
166 Item *i;
167
168 dc.x += cmdw;
169 dc.w = textw(&dc, "<");
170 drawtext(&dc, curr->left ? "<" : NULL, normcol);
171 dc.x += dc.w;
172 for(i = curr; i != next; i = i->right) {
173 dc.w = MIN(textw(&dc, i->text), mw / 3);
174 drawitem(i->text, (sel == i) ? selcol : normcol);
175 dc.x += dc.w;
176 }
177 dc.w = textw(&dc, ">");
178 dc.x = mw - dc.w;
179 drawtext(&dc, next ? ">" : NULL, normcol);
180 }
181
182 void
183 drawmenuv(void) {
184 Item *i;
185 XWindowAttributes wa;
186
187 dc.y = topbar ? dc.h : 0;
188 dc.w = mw - dc.x;
189 for(i = curr; i != next; i = i->right) {
190 drawitem(i->text, (sel == i) ? selcol : normcol);
191 dc.y += dc.h;
192 }
193 if(!XGetWindowAttributes(dpy, win, &wa))
194 eprint("cannot get window attributes");
195 XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
196 }
197
198 void
199 kpress(XKeyEvent *e) {
200 char buf[sizeof text];
201 int num;
202 unsigned int i, len;
203 KeySym ksym;
204
205 len = strlen(text);
206 num = XLookupString(e, buf, sizeof buf, &ksym, NULL);
207 if(ksym == XK_KP_Enter)
208 ksym = XK_Return;
209 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
210 ksym = (ksym - XK_KP_0) + XK_0;
211 else if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
212 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
213 || IsPrivateKeypadKey(ksym))
214 return;
215 /* first check if a control mask is omitted */
216 if(e->state & ControlMask) {
217 switch(tolower(ksym)) {
218 default:
219 return;
220 case XK_a:
221 ksym = XK_Home;
222 break;
223 case XK_b:
224 ksym = XK_Left;
225 break;
226 case XK_c:
227 ksym = XK_Escape;
228 break;
229 case XK_e:
230 ksym = XK_End;
231 break;
232 case XK_f:
233 ksym = XK_Right;
234 break;
235 case XK_h:
236 ksym = XK_BackSpace;
237 break;
238 case XK_i:
239 ksym = XK_Tab;
240 break;
241 case XK_j:
242 case XK_m:
243 ksym = XK_Return;
244 break;
245 case XK_n:
246 ksym = XK_Down;
247 break;
248 case XK_p:
249 ksym = XK_Up;
250 break;
251 case XK_u:
252 text[0] = '\0';
253 match();
254 break;
255 case XK_w:
256 if(len == 0)
257 return;
258 i = len;
259 while(i-- > 0 && text[i] == ' ');
260 while(i-- > 0 && text[i] != ' ');
261 text[++i] = '\0';
262 match();
263 break;
264 }
265 }
266 switch(ksym) {
267 default:
268 num = MIN(num, sizeof text);
269 if(num && !iscntrl((int) buf[0])) {
270 memcpy(text + len, buf, num + 1);
271 len += num;
272 match();
273 }
274 break;
275 case XK_BackSpace:
276 if(len == 0)
277 return;
278 for(i = 1; len - i > 0 && !IS_UTF8_1ST_CHAR(text[len - i]); i++);
279 len -= i;
280 text[len] = '\0';
281 match();
282 break;
283 case XK_End:
284 while(next) {
285 sel = curr = next;
286 calcoffsets();
287 }
288 while(sel && sel->right)
289 sel = sel->right;
290 break;
291 case XK_Escape:
292 exit(EXIT_FAILURE);
293 case XK_Home:
294 sel = curr = item;
295 calcoffsets();
296 break;
297 case XK_Left:
298 case XK_Up:
299 if(!sel || !sel->left)
300 return;
301 sel = sel->left;
302 if(sel->right == curr) {
303 curr = prev;
304 calcoffsets();
305 }
306 break;
307 case XK_Next:
308 if(!next)
309 return;
310 sel = curr = next;
311 calcoffsets();
312 break;
313 case XK_Prior:
314 if(!prev)
315 return;
316 sel = curr = prev;
317 calcoffsets();
318 break;
319 case XK_Return:
320 if(e->state & ShiftMask)
321 dinput();
322 fprintf(stdout, "%s", sel ? sel->text : text);
323 fflush(stdout);
324 exit(EXIT_SUCCESS);
325 case XK_Right:
326 case XK_Down:
327 if(!sel || !sel->right)
328 return;
329 sel = sel->right;
330 if(sel == next) {
331 curr = next;
332 calcoffsets();
333 }
334 break;
335 case XK_Tab:
336 if(sel)
337 strncpy(text, sel->text, sizeof text);
338 dinput();
339 break;
340 }
341 drawbar();
342 }
343
344 void
345 match(void) {
346 unsigned int len;
347 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
348
349 len = strlen(text);
350 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
351 for(i = allitems; i; i = i->next)
352 if(!fstrncmp(text, i->text, len + 1))
353 appenditem(i, &lexact, &exactend);
354 else if(!fstrncmp(text, i->text, len))
355 appenditem(i, &lprefix, &prefixend);
356 else if(fstrstr(i->text, text))
357 appenditem(i, &lsubstr, &substrend);
358 if(lexact) {
359 item = lexact;
360 itemend = exactend;
361 }
362 if(lprefix) {
363 if(itemend) {
364 itemend->right = lprefix;
365 lprefix->left = itemend;
366 }
367 else
368 item = lprefix;
369 itemend = prefixend;
370 }
371 if(lsubstr) {
372 if(itemend) {
373 itemend->right = lsubstr;
374 lsubstr->left = itemend;
375 }
376 else
377 item = lsubstr;
378 }
379 curr = prev = next = sel = item;
380 calcoffsets();
381 }
382
383 void
384 readstdin(void) {
385 char *p, buf[sizeof text];
386 unsigned int len = 0, max = 0;
387 Item *i, *new;
388
389 i = NULL;
390 while(fgets(buf, sizeof buf, stdin)) {
391 len = strlen(buf);
392 if(buf[len-1] == '\n')
393 buf[--len] = '\0';
394 if(!(p = strdup(buf)))
395 eprint("cannot strdup %u bytes\n", len);
396 if((max = MAX(max, len)) == len)
397 maxname = p;
398 if(!(new = malloc(sizeof *new)))
399 eprint("cannot malloc %u bytes\n", sizeof *new);
400 new->next = new->left = new->right = NULL;
401 new->text = p;
402 if(!i)
403 allitems = new;
404 else
405 i->next = new;
406 i = new;
407 }
408 }
409
410 int
411 main(int argc, char *argv[]) {
412 unsigned int i;
413
414 /* command line args */
415 progname = "dmenu";
416 for(i = 1; i < argc; i++)
417 if(!strcmp(argv[i], "-i")) {
418 fstrncmp = strncasecmp;
419 fstrstr = cistrstr;
420 }
421 else if(!strcmp(argv[i], "-b"))
422 topbar = False;
423 else if(!strcmp(argv[i], "-l")) {
424 if(++i < argc) lines = atoi(argv[i]);
425 if(lines > 0)
426 calcoffsets = calcoffsetsv;
427 }
428 else if(!strcmp(argv[i], "-fn")) {
429 if(++i < argc) font = argv[i];
430 }
431 else if(!strcmp(argv[i], "-nb")) {
432 if(++i < argc) normbgcolor = argv[i];
433 }
434 else if(!strcmp(argv[i], "-nf")) {
435 if(++i < argc) normfgcolor = argv[i];
436 }
437 else if(!strcmp(argv[i], "-p")) {
438 if(++i < argc) prompt = argv[i];
439 }
440 else if(!strcmp(argv[i], "-sb")) {
441 if(++i < argc) selbgcolor = argv[i];
442 }
443 else if(!strcmp(argv[i], "-sf")) {
444 if(++i < argc) selfgcolor = argv[i];
445 }
446 else if(!strcmp(argv[i], "-v")) {
447 printf("dmenu-"VERSION", © 2006-2010 dmenu engineers, see LICENSE for details\n");
448 exit(EXIT_SUCCESS);
449 }
450 else {
451 fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
452 " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
453 exit(EXIT_FAILURE);
454 }
455 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
456 fprintf(stderr, "dmenu: warning: no locale support\n");
457 if(!(dpy = XOpenDisplay(NULL)))
458 eprint("cannot open display\n");
459 if(atexit(&cleanup) != 0)
460 eprint("cannot register cleanup\n");
461 screen = DefaultScreen(dpy);
462 root = RootWindow(dpy, screen);
463 if(!(argp = malloc(sizeof *argp * (argc+2))))
464 eprint("cannot malloc %u bytes\n", sizeof *argp * (argc+2));
465 memcpy(argp + 2, argv + 1, sizeof *argp * argc);
466
467 readstdin();
468 grabkeyboard();
469 setup(lines);
470 if(maxname)
471 cmdw = MIN(textw(&dc, maxname), mw / 3);
472 match();
473 run();
474 return 0;
475 }