initial import
[dmenu.git] / main.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
4  * See LICENSE file for license details.
5  */
6
7 #include "dmenu.h"
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <X11/cursorfont.h>
15 #include <X11/Xutil.h>
16 #include <X11/keysym.h>
17
18 typedef struct Item Item;
19
20 struct Item {
21         Item *next;             /* traverses all items */
22         Item *left, *right;     /* traverses items matching current search pattern */
23         char *text;
24 };
25
26 static Display *dpy;
27 static Window root;
28 static Window win;
29 static Bool done = False;
30
31 static Item *allitem = NULL;    /* first of all items */
32 static Item *item = NULL;       /* first of pattern matching items */
33 static Item *sel = NULL;
34 static Item *nextoff = NULL;
35 static Item *prevoff = NULL;
36 static Item *curroff = NULL;
37
38 static int screen, mx, my, mw, mh;
39 static char *title = NULL;
40 static char text[4096];
41 static int ret = 0;
42 static int nitem = 0;
43 static unsigned int cmdw = 0;
44 static unsigned int tw = 0;
45 static unsigned int cw = 0;
46 static const int seek = 30;             /* 30px */
47
48 static Brush brush = {0};
49
50 static void draw_menu();
51 static void kpress(XKeyEvent * e);
52
53 static char version[] = "dmenu - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
54
55 static void
56 update_offsets()
57 {
58         unsigned int tw, w = cmdw + 2 * seek;
59
60         if(!curroff)
61                 return;
62
63         for(nextoff = curroff; nextoff; nextoff=nextoff->right) {
64                 tw = textw(&brush.font, nextoff->text);
65                 if(tw > mw / 3)
66                         tw = mw / 3;
67                 w += tw + brush.font.height;
68                 if(w > mw)
69                         break;
70         }
71
72         w = cmdw + 2 * seek;
73         for(prevoff = curroff; prevoff && prevoff->left; prevoff=prevoff->left) {
74                 tw = textw(&brush.font, prevoff->left->text);
75                 if(tw > mw / 3)
76                         tw = mw / 3;
77                 w += tw + brush.font.height;
78                 if(w > mw)
79                         break;
80         }
81 }
82
83 static void
84 update_items(char *pattern)
85 {
86         unsigned int plen = strlen(pattern);
87         Item *i, *j;
88
89         if(!pattern)
90                 return;
91
92         if(!title || *pattern)
93                 cmdw = cw;
94         else
95                 cmdw = tw;
96
97         item = j = NULL;
98         nitem = 0;
99
100         for(i = allitem; i; i=i->next)
101                 if(!plen || !strncmp(pattern, i->text, plen)) {
102                         if(!j)
103                                 item = i;
104                         else
105                                 j->right = i;
106                         i->left = j;
107                         i->right = NULL;
108                         j = i;
109                         nitem++;
110                 }
111         for(i = allitem; i; i=i->next)
112                 if(plen && strncmp(pattern, i->text, plen)
113                                 && strstr(i->text, pattern)) {
114                         if(!j)
115                                 item = i;
116                         else
117                                 j->right = i;
118                         i->left = j;
119                         i->right = NULL;
120                         j = i;
121                         nitem++;
122                 }
123
124         curroff = prevoff = nextoff = sel = item;
125
126         update_offsets();
127 }
128
129 /* creates brush structs for brush mode drawing */
130 static void
131 draw_menu()
132 {
133         Item *i;
134
135         brush.x = 0;
136         brush.y = 0;
137         brush.w = mw;
138         brush.h = mh;
139         draw(dpy, &brush, False, 0);
140
141         /* print command */
142         if(!title || text[0]) {
143                 cmdw = cw;
144                 if(cmdw && item)
145                         brush.w = cmdw;
146                 draw(dpy, &brush, False, text);
147         }
148         else {
149                 cmdw = tw;
150                 brush.w = cmdw;
151                 draw(dpy, &brush, False, title);
152         }
153         brush.x += brush.w;
154
155         if(curroff) {
156                 brush.w = seek;
157                 draw(dpy, &brush, False, (curroff && curroff->left) ? "<" : 0);
158                 brush.x += brush.w;
159
160                 /* determine maximum items */
161                 for(i = curroff; i != nextoff; i=i->right) {
162                         brush.border = False;
163                         brush.w = textw(&brush.font, i->text);
164                         if(brush.w > mw / 3)
165                                 brush.w = mw / 3;
166                         brush.w += brush.font.height;
167                         if(sel == i) {
168                                 swap((void **)&brush.fg, (void **)&brush.bg);
169                                 draw(dpy, &brush, True, i->text);
170                                 swap((void **)&brush.fg, (void **)&brush.bg);
171                         }
172                         else
173                                 draw(dpy, &brush, False, i->text);
174                         brush.x += brush.w;
175                 }
176
177                 brush.x = mw - seek;
178                 brush.w = seek;
179                 draw(dpy, &brush, False, nextoff ? ">" : 0);
180         }
181         XCopyArea(dpy, brush.drawable, win, brush.gc, 0, 0, mw, mh, 0, 0);
182         XFlush(dpy);
183 }
184
185 static void
186 kpress(XKeyEvent * e)
187 {
188         KeySym ksym;
189         char buf[32];
190         int num, prev_nitem;
191         unsigned int i, len = strlen(text);
192
193         buf[0] = 0;
194         num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
195
196         if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
197                         || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
198                         || IsPrivateKeypadKey(ksym))
199                 return;
200
201         /* first check if a control mask is omitted */
202         if(e->state & ControlMask) {
203                 switch (ksym) {
204                 default:        /* ignore other control sequences */
205                         return;
206                         break;
207                 case XK_h:
208                         ksym = XK_BackSpace;
209                         break;
210                 case XK_U:
211                 case XK_u:
212                         text[0] = 0;
213                         update_items(text);
214                         draw_menu();
215                         return;
216                         break;
217                 case XK_bracketleft:
218                         ksym = XK_Escape;
219                         break;
220                 }
221         }
222         switch(ksym) {
223         case XK_Left:
224                 if(!(sel && sel->left))
225                         return;
226                 sel=sel->left;
227                 if(sel->right == curroff) {
228                         curroff = prevoff;
229                         update_offsets();
230                 }
231                 break;
232         case XK_Tab:
233                 if(!sel)
234                         return;
235                 strncpy(text, sel->text, sizeof(text));
236                 update_items(text);
237                 break;
238         case XK_Right:
239                 if(!(sel && sel->right))
240                         return;
241                 sel=sel->right;
242                 if(sel == nextoff) {
243                         curroff = nextoff;
244                         update_offsets();
245                 }
246                 break;
247         case XK_Return:
248                 if(e->state & ShiftMask) {
249                         if(text)
250                                 fprintf(stdout, "%s", text);
251                 }
252                 else if(sel)
253                         fprintf(stdout, "%s", sel->text);
254                 else if(text)
255                         fprintf(stdout, "%s", text);
256                 fflush(stdout);
257                 done = True;
258                 break;
259         case XK_Escape:
260                 ret = 1;
261                 done = True;
262                 break;
263         case XK_BackSpace:
264                 if((i = len)) {
265                         prev_nitem = nitem;
266                         do {
267                                 text[--i] = 0;
268                                 update_items(text);
269                         } while(i && nitem && prev_nitem == nitem);
270                         update_items(text);
271                 }
272                 break;
273         default:
274                 if(num && !iscntrl((int) buf[0])) {
275                         buf[num] = 0;
276                         if(len > 0)
277                                 strncat(text, buf, sizeof(text));
278                         else
279                                 strncpy(text, buf, sizeof(text));
280                         update_items(text);
281                 }
282         }
283         draw_menu();
284 }
285
286 static char *
287 read_allitems()
288 {
289         static char *maxname = NULL;
290         char *p, buf[1024];
291         unsigned int len = 0, max = 0;
292         Item *i, *new;
293
294         i = 0;
295         while(fgets(buf, sizeof(buf), stdin)) {
296                 len = strlen(buf);
297                 if (buf[len - 1] == '\n')
298                         buf[len - 1] = 0;
299                 p = estrdup(buf);
300                 if(max < len) {
301                         maxname = p;
302                         max = len;
303                 }
304
305                 new = emalloc(sizeof(Item));
306                 new->next = new->left = new->right = NULL;
307                 new->text = p;
308                 if(!i)
309                         allitem = new;
310                 else 
311                         i->next = new;
312                 i = new;
313         }
314
315         return maxname;
316 }
317
318 int
319 main(int argc, char *argv[])
320 {
321         int i;
322         XSetWindowAttributes wa;
323         char *maxname;
324         XEvent ev;
325
326         /* command line args */
327         for(i = 1; i < argc; i++) {
328                 if (argv[i][0] == '-')
329                         switch (argv[i][1]) {
330                         case 'v':
331                                 fprintf(stdout, "%s", version);
332                                 exit(0);
333                                 break;
334                         case 't':
335                                 if(++i < argc) {
336                                         title = argv[i];
337                                         break;
338                                 }
339                         default:
340                                 eprint("usage: dmenu [-v] [-t <title>]\n");
341                                 break;
342                         }
343                 else
344                         eprint("usage: dmenu [-v] [-t <title>]\n");
345         }
346
347         dpy = XOpenDisplay(0);
348         if(!dpy)
349                 eprint("dmenu: cannot open dpy\n");
350         screen = DefaultScreen(dpy);
351         root = RootWindow(dpy, screen);
352
353         maxname = read_allitems();
354
355         /* grab as early as possible, but after reading all items!!! */
356         while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
357                          GrabModeAsync, CurrentTime) != GrabSuccess)
358                 usleep(1000);
359
360         /* style */
361         loadcolors(dpy, screen, &brush, BGCOLOR, FGCOLOR, BORDERCOLOR);
362         loadfont(dpy, &brush.font, FONT);
363
364         wa.override_redirect = 1;
365         wa.background_pixmap = ParentRelative;
366         wa.event_mask = ExposureMask | ButtonPressMask | KeyPressMask;
367
368         mx = my = 0;
369         mw = DisplayWidth(dpy, screen);
370         mh = texth(&brush.font);
371
372         win = XCreateWindow(dpy, root, mx, my, mw, mh, 0,
373                         DefaultDepth(dpy, screen), CopyFromParent,
374                         DefaultVisual(dpy, screen),
375                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
376         XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
377         XFlush(dpy);
378
379         /* pixmap */
380         brush.gc = XCreateGC(dpy, root, 0, 0);
381         brush.drawable = XCreatePixmap(dpy, win, mw, mh,
382                         DefaultDepth(dpy, screen));
383         XFlush(dpy);
384
385         if(maxname)
386                 cw = textw(&brush.font, maxname) + brush.font.height;
387         if(cw > mw / 3)
388                 cw = mw / 3;
389
390         if(title) {
391                 tw = textw(&brush.font, title) + brush.font.height;
392                 if(tw > mw / 3)
393                         tw = mw / 3;
394         }
395
396         cmdw = title ? tw : cw;
397
398         text[0] = 0;
399         update_items(text);
400         XMapRaised(dpy, win);
401         draw_menu();
402         XFlush(dpy);
403
404         /* main event loop */
405         while(!done && !XNextEvent(dpy, &ev)) {
406                 switch (ev.type) {
407                 case KeyPress:
408                         kpress(&ev.xkey);
409                         break;
410                 case Expose:
411                         if(ev.xexpose.count == 0)
412                                 draw_menu();
413                         break;
414                 default:
415                         break;
416                 }
417         }
418
419         XUngrabKeyboard(dpy, CurrentTime);
420         XFreePixmap(dpy, brush.drawable);
421         XFreeGC(dpy, brush.gc);
422         XDestroyWindow(dpy, win);
423         XCloseDisplay(dpy);
424
425         return ret;
426 }