[client] Invalidate cache on maximize
[awesome.git] / awesome-menu.c
blob668d00c9b6d200c075f2d61bbee3441e3c80dc5f
1 /*
2 * awesome-menu.c - menu window for awesome
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /* getline(), asprintf() */
23 #define _GNU_SOURCE
25 #define CHUNK_SIZE 4096
27 #include <getopt.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <dirent.h>
33 #include <pwd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <string.h>
37 #include <errno.h>
39 #include <X11/Xutil.h>
41 #include "common/swindow.h"
42 #include "common/util.h"
43 #include "common/version.h"
44 #include "common/configopts.h"
45 #include "common/xutil.h"
46 #include "common/xscreen.c"
48 #define PROGNAME "awesome-menu"
50 #define CLEANMASK(mask) (mask & ~(globalconf.numlockmask | LockMask))
52 typedef enum
54 STOP = 0,
55 RUN = 1,
56 CANCEL = 2
57 } status_t;
59 static status_t status = RUN;
61 /** Import awesome config file format */
62 extern cfg_opt_t awesome_opts[];
64 typedef struct item_t item_t;
65 struct item_t
67 char *data;
68 item_t *prev, *next;
69 Bool match;
72 static void
73 item_delete(item_t **item)
75 p_delete(&(*item)->data);
76 p_delete(item);
79 DO_SLIST(item_t, item, item_delete)
81 /** awesome-run global configuration structure */
82 typedef struct
84 /** Display ref */
85 Display *display;
86 /** The window */
87 SimpleWindow *sw;
88 /** The draw contet */
89 DrawCtx *ctx;
90 /** Colors */
91 struct
93 style_t normal;
94 style_t focus;
95 } styles;
96 /** Numlock mask */
97 unsigned int numlockmask;
98 /** The text */
99 char *text;
100 /** The text length */
101 size_t text_size;
102 /** Item list */
103 item_t *items;
104 /** Selected item */
105 item_t *item_selected;
106 /** What to do with the result text */
107 char *exec;
108 /** Prompt */
109 char *prompt;
110 } AwesomeMenuConf;
112 static AwesomeMenuConf globalconf;
114 static void __attribute__ ((noreturn))
115 exit_help(int exit_code)
117 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
118 fprintf(outfile, "Usage: %s [-c config] [-e command] <message>\n",
119 PROGNAME);
120 exit(exit_code);
123 static int
124 config_parse(int screen, const char *confpatharg,
125 const char *menu_title, area_t *geometry)
127 int ret, i;
128 char *confpath;
129 cfg_t *cfg, *cfg_menu = NULL, *cfg_screen = NULL,
130 *cfg_styles, *cfg_menu_styles = NULL;
132 if(!confpatharg)
133 confpath = config_file();
134 else
135 confpath = a_strdup(confpatharg);
137 cfg = cfg_new();
139 switch((ret = cfg_parse(cfg, confpath)))
141 case CFG_FILE_ERROR:
142 warn("parsing configuration file failed: %s\n", strerror(errno));
143 break;
144 case CFG_PARSE_ERROR:
145 cfg_error(cfg, "W: awesome: parsing configuration file %s failed.\n", confpath);
146 break;
149 if(ret)
150 return ret;
152 if(menu_title && !(cfg_menu = cfg_gettsec(cfg, "menu", menu_title)))
153 warn("no definition for menu %s in configuration file: using default\n",
154 menu_title);
156 /* get global screen section */
157 if(!(cfg_screen = cfg_getnsec(cfg, "screen", screen)))
158 cfg_screen = cfg_getsec(cfg, "screen");
160 if(cfg_menu)
162 cfg_menu_styles = cfg_getsec(cfg_menu, "styles");
163 if((i = cfg_getint(cfg_menu, "x")) != (int) 0xffffffff)
164 geometry->x = i;
165 if((i = cfg_getint(cfg_menu, "y")) != (int) 0xffffffff)
166 geometry->y = i;
167 if((i = cfg_getint(cfg_menu, "width")) > 0)
168 geometry->width = i;
169 if((i = cfg_getint(cfg_menu, "height")) > 0)
170 geometry->height = i;
173 if(cfg_screen
174 && (cfg_styles = cfg_getsec(cfg_screen, "styles")))
176 /* Grab default styles */
177 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
178 cfg_getsec(cfg_styles, "normal"),
179 &globalconf.styles.normal, NULL);
181 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
182 cfg_getsec(cfg_styles, "focus"),
183 &globalconf.styles.focus, &globalconf.styles.normal);
186 /* Now grab menu styles if any */
187 if(cfg_menu_styles)
189 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
190 cfg_getsec(cfg_menu_styles, "normal"),
191 &globalconf.styles.normal, NULL);
193 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
194 cfg_getsec(cfg_menu_styles, "focus"),
195 &globalconf.styles.focus, &globalconf.styles.normal);
198 if(!globalconf.styles.normal.font)
199 eprint("no default font available\n");
201 p_delete(&confpath);
203 return ret;
206 static char *
207 get_last_word(char *text)
209 char *last_word;
211 if((last_word = strrchr(text, ' ')))
212 last_word++;
213 else
214 last_word = text;
216 return last_word;
219 static Bool
220 item_list_fill_file(const char *directory)
222 char *cwd, *home, *user, *filename;
223 const char *file;
224 DIR *dir;
225 struct dirent *dirinfo;
226 item_t *item;
227 ssize_t len, lenfile;
228 struct passwd *passwd = NULL;
229 struct stat st;
231 item_list_wipe(&globalconf.items);
233 if(!directory)
234 cwd = a_strdup("./");
235 else if(a_strlen(directory) > 1 && directory[0] == '~')
237 if(directory[1] == '/')
239 home = getenv("HOME");
240 asprintf(&cwd, "%s%s", (home ? home : ""), directory + 1);
242 else
244 if(!(file = strchr(directory, '/')))
245 file = directory + a_strlen(directory);
246 len = (file - directory) + 1;
247 user = p_new(char, len);
248 a_strncpy(user, len, directory + 1, (file - directory) - 1);
249 if((passwd = getpwnam(user)))
251 asprintf(&cwd, "%s%s", passwd->pw_dir, file);
252 p_delete(&user);
254 else
256 p_delete(&user);
257 return False;
261 else
262 cwd = a_strdup(directory);
264 if(!(dir = opendir(cwd)))
266 p_delete(&cwd);
267 return False;
270 while((dirinfo = readdir(dir)))
272 item = p_new(item_t, 1);
274 /* + 1 for \0 + 1 for / if directory */
275 len = a_strlen(directory) + a_strlen(dirinfo->d_name) + 2;
277 item->data = p_new(char, len);
278 if(a_strlen(directory))
279 a_strcpy(item->data, len, directory);
280 a_strcat(item->data, len, dirinfo->d_name);
282 lenfile = a_strlen(cwd) + a_strlen(dirinfo->d_name) + 2;
284 filename = p_new(char, lenfile);
285 a_strcpy(filename, lenfile, cwd);
286 a_strcat(filename, lenfile, dirinfo->d_name);
288 if(!stat(filename, &st) && S_ISDIR(st.st_mode))
289 a_strcat(item->data, len, "/");
291 p_delete(&filename);
293 item_list_push(&globalconf.items, item);
296 closedir(dir);
297 p_delete(&cwd);
299 return True;
302 static void
303 complete(Bool reverse)
305 char *word;
306 int loop = 2;
307 item_t *item = NULL;
308 item_t *(*item_iter)(item_t **, item_t *) = item_list_next_cycle;
310 if(reverse)
311 item_iter = item_list_prev_cycle;
313 if(globalconf.item_selected)
314 item = item_iter(&globalconf.items, globalconf.item_selected);
315 else
316 item = globalconf.items;
318 for(; item && loop; item = item_iter(&globalconf.items, item))
320 if(item->match)
322 word = get_last_word(globalconf.text);
323 a_strcpy(word, globalconf.text_size - (word - globalconf.text), item->data);
324 globalconf.item_selected = item;
325 return;
328 * Since loop is 2, it will be 1 at first iter, and then 0 if we
329 * get back before matching an item (i.e. no items match) to the
330 * first elem: so it will break the loop, otherwise it loops for
331 * ever
333 if(item == globalconf.items)
334 loop--;
338 static void
339 compute_match(const char *word)
341 ssize_t len = a_strlen(word);
342 item_t *item;
344 /* reset the selected item to NULL */
345 globalconf.item_selected = NULL;
347 if(len)
349 if(word[len - 1] == '/'
350 || word[len - 1] == ' ')
351 item_list_fill_file(word);
353 for(item = globalconf.items; item; item = item->next)
354 if(!a_strncmp(word, item->data, a_strlen(word)))
355 item->match = True;
356 else
357 item->match = False;
359 else
361 if(a_strlen(globalconf.text))
362 item_list_fill_file(NULL);
363 for(item = globalconf.items; item; item = item->next)
364 item->match = True;
368 /* Why not? */
369 #define MARGIN 10
371 static void
372 redraw(void)
374 item_t *item;
375 area_t geometry = { 0, 0, 0, 0, NULL, NULL };
376 Bool selected_item_is_drawn = False;
377 int len, prompt_len, x_of_previous_item;
378 style_t style;
380 geometry.width = globalconf.sw->geometry.width;
381 geometry.height = globalconf.sw->geometry.height;
383 if(a_strlen(globalconf.prompt))
385 draw_text(globalconf.ctx, geometry, AlignLeft,
386 MARGIN, globalconf.prompt, globalconf.styles.focus);
388 len = MARGIN * 2 + draw_textwidth(globalconf.display, globalconf.styles.focus.font, globalconf.prompt);
389 geometry.x += len;
390 geometry.width -= len;
393 draw_text(globalconf.ctx, geometry, AlignLeft,
394 MARGIN, globalconf.text, globalconf.styles.normal);
396 len = MARGIN * 2 + MAX(draw_textwidth(globalconf.display, globalconf.styles.normal.font, globalconf.text),
397 geometry.width / 20);
398 geometry.x += len;
399 geometry.width -= len;
400 prompt_len = geometry.x;
402 for(item = globalconf.items; item && geometry.width > 0; item = item->next)
403 if(item->match)
405 style = item == globalconf.item_selected ? globalconf.styles.focus : globalconf.styles.normal;
406 len = MARGIN + draw_textwidth(globalconf.display, style.font, item->data);
407 if(item == globalconf.item_selected)
409 if(len > geometry.width)
410 break;
411 else
412 selected_item_is_drawn = True;
414 draw_text(globalconf.ctx, geometry, AlignLeft,
415 MARGIN / 2, item->data, style);
416 geometry.x += len;
417 geometry.width -= len;
420 /* we have an item selected but not drawn, so redraw in the other side */
421 if(globalconf.item_selected && !selected_item_is_drawn)
423 geometry.x = globalconf.sw->geometry.width;
425 for(item = globalconf.item_selected; item; item = item_list_prev(&globalconf.items, item))
426 if(item->match)
428 style = item == globalconf.item_selected ? globalconf.styles.focus : globalconf.styles.normal;
429 x_of_previous_item = geometry.x;
430 geometry.width = MARGIN + draw_textwidth(globalconf.display, style.font, item->data);
431 geometry.x -= geometry.width;
433 if(geometry.x < prompt_len)
434 break;
436 draw_text(globalconf.ctx, geometry, AlignLeft,
437 MARGIN / 2, item->data, style);
440 if(item)
442 geometry.x = prompt_len;
443 geometry.width = x_of_previous_item - prompt_len;
444 draw_rectangle(globalconf.ctx, geometry, 1.0, True, globalconf.styles.normal.bg);
447 else if(geometry.width)
448 draw_rectangle(globalconf.ctx, geometry, 1.0, True, globalconf.styles.normal.bg);
450 simplewindow_refresh_drawable(globalconf.sw, DefaultScreen(globalconf.display));
451 XSync(globalconf.display, False);
454 static void
455 handle_kpress(XKeyEvent *e)
457 char buf[32];
458 KeySym ksym;
459 int num;
460 ssize_t len;
461 size_t text_dst_len;
463 len = a_strlen(globalconf.text);
464 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
465 if(e->state & ControlMask)
466 switch(ksym)
468 default:
469 return;
470 case XK_bracketleft:
471 ksym = XK_Escape;
472 break;
473 case XK_h:
474 case XK_H:
475 ksym = XK_BackSpace;
476 break;
477 case XK_i:
478 case XK_I:
479 ksym = XK_Tab;
480 break;
481 case XK_j:
482 case XK_J:
483 ksym = XK_Return;
484 break;
485 case XK_c:
486 case XK_C:
487 status = CANCEL;
488 break;
489 case XK_w:
490 case XK_W:
491 /* erase word */
492 if(len)
494 int i = len - 1;
495 while(i >= 0 && globalconf.text[i] == ' ')
496 globalconf.text[i--] = '\0';
497 while(i >= 0 && globalconf.text[i] != ' ')
498 globalconf.text[i--] = '\0';
499 compute_match(get_last_word(globalconf.text));
500 redraw();
502 return;
504 else if(CLEANMASK(e->state) & Mod1Mask)
505 switch(ksym)
507 default:
508 return;
509 case XK_h:
510 ksym = XK_Left;
511 break;
512 case XK_l:
513 ksym = XK_Right;
514 break;
517 switch(ksym)
519 default:
520 if(num && !iscntrl((int) buf[0]))
522 if(buf[0] != '/' || globalconf.text[len - 1] != '/')
524 buf[num] = '\0';
526 /* Reallocate text string if needed to hold
527 * concatenation of text and buf */
528 if((text_dst_len = (a_strlen(globalconf.text) + num - 1)) > globalconf.text_size)
530 globalconf.text_size += ((int) (text_dst_len / globalconf.text_size)) * CHUNK_SIZE;
531 p_realloc(&globalconf.text, globalconf.text_size);
533 a_strncat(globalconf.text, globalconf.text_size, buf, num);
535 compute_match(get_last_word(globalconf.text));
536 redraw();
538 break;
539 case XK_BackSpace:
540 if(len)
542 globalconf.text[--len] = '\0';
543 compute_match(get_last_word(globalconf.text));
544 redraw();
546 break;
547 case XK_ISO_Left_Tab:
548 case XK_Left:
549 complete(True);
550 redraw();
551 break;
552 case XK_Right:
553 case XK_Tab:
554 complete(False);
555 redraw();
556 break;
557 case XK_Escape:
558 status= CANCEL;
559 break;
560 case XK_Return:
561 status = STOP;
562 break;
566 static Bool
567 item_list_fill_stdin(void)
569 char *buf = NULL;
570 size_t len = 0;
571 ssize_t line_len;
573 item_t *newitem;
574 Bool has_entry = False;
576 item_list_init(&globalconf.items);
578 if((line_len = getline(&buf, &len, stdin)) != -1)
579 has_entry = True;
581 if(has_entry)
584 buf[line_len - 1] = '\0';
585 newitem = p_new(item_t, 1);
586 newitem->data = a_strdup(buf);
587 newitem->match = True;
588 item_list_append(&globalconf.items, newitem);
590 while((line_len = getline(&buf, &len, stdin)) != -1);
592 if(buf)
593 p_delete(&buf);
595 return has_entry;
599 main(int argc, char **argv)
601 XEvent ev;
602 int opt, ret, x, y, i, screen = 0;
603 char *configfile = NULL, *cmd;
604 ssize_t len;
605 const char *shell = getenv("SHELL");
606 area_t geometry = { 0, 0, 0, 0, NULL, NULL };
607 ScreensInfo *si;
608 unsigned int ui;
609 Window dummy;
610 static struct option long_options[] =
612 {"help", 0, NULL, 'h'},
613 {"version", 0, NULL, 'v'},
614 {"exec", 0, NULL, 'e'},
615 {NULL, 0, NULL, 0}
618 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:e:",
619 long_options, NULL)) != -1)
620 switch(opt)
622 case 'v':
623 eprint_version(PROGNAME);
624 break;
625 case 'h':
626 exit_help(EXIT_SUCCESS);
627 break;
628 case 'c':
629 configfile = a_strdup(optarg);
630 break;
631 case 'e':
632 globalconf.exec = a_strdup(optarg);
633 break;
636 if(argc - optind >= 1)
637 globalconf.prompt = a_strdup(argv[optind]);
639 if(!(globalconf.display = XOpenDisplay(NULL)))
640 eprint("unable to open display");
642 /* Get the numlock mask */
643 globalconf.numlockmask = xgetnumlockmask(globalconf.display);
645 si = screensinfo_new(globalconf.display);
646 if(si->xinerama_is_active)
648 if(XQueryPointer(globalconf.display, RootWindow(globalconf.display, DefaultScreen(globalconf.display)),
649 &dummy, &dummy, &x, &y, &i, &i, &ui))
651 screen = screen_get_bycoord(si, 0, x, y);
653 geometry.x = si->geometry[screen].x;
654 geometry.y = si->geometry[screen].y;
655 geometry.width = si->geometry[screen].width;
658 else
660 screen = DefaultScreen(globalconf.display);
661 if(!geometry.width)
662 geometry.width = DisplayWidth(globalconf.display, DefaultScreen(globalconf.display));
665 if((ret = config_parse(screen, configfile, globalconf.prompt, &geometry)))
666 return ret;
668 /* Init the geometry */
669 if(!geometry.height)
670 geometry.height = 1.5 * MAX(globalconf.styles.normal.font->height,
671 globalconf.styles.focus.font->height);
673 screensinfo_delete(&si);
675 /* Create the window */
676 globalconf.sw = simplewindow_new(globalconf.display, DefaultScreen(globalconf.display),
677 geometry.x, geometry.y, geometry.width, geometry.height, 0);
679 XStoreName(globalconf.display, globalconf.sw->window, PROGNAME);
680 XMapRaised(globalconf.display, globalconf.sw->window);
682 /* Create the drawing context */
683 globalconf.ctx = draw_context_new(globalconf.display, DefaultScreen(globalconf.display),
684 geometry.width, geometry.height,
685 globalconf.sw->drawable);
688 if(!item_list_fill_stdin())
689 item_list_fill_file(NULL);
691 /* Allocate a default size for the text on the heap instead of
692 * using stack allocation with PATH_MAX (may not been defined
693 * according to POSIX). This string size may be increased if
694 * needed */
695 globalconf.text = p_new(char, CHUNK_SIZE);
696 globalconf.text_size = CHUNK_SIZE;
697 compute_match(NULL);
699 for(opt = 1000; opt; opt--)
701 if(XGrabKeyboard(globalconf.display, DefaultRootWindow(globalconf.display), True,
702 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
703 break;
704 usleep(1000);
706 if(!opt)
707 eprint("cannot grab keyboard");
709 redraw();
711 while(status == RUN)
713 XNextEvent(globalconf.display, &ev);
714 switch(ev.type)
716 case ButtonPress:
717 status = CANCEL;
718 break;
719 case KeyPress:
720 handle_kpress(&ev.xkey);
721 break;
722 case Expose:
723 if(!ev.xexpose.count)
724 simplewindow_refresh_drawable(globalconf.sw, DefaultScreen(globalconf.display));
725 break;
726 default:
727 break;
731 if(status != CANCEL)
733 if(!globalconf.exec)
734 printf("%s\n", globalconf.text);
735 else
737 if(!shell)
738 shell = a_strdup("/bin/sh");
739 len = a_strlen(globalconf.exec) + a_strlen(globalconf.text) + 1;
740 cmd = p_new(char, len);
741 a_strcpy(cmd, len, globalconf.exec);
742 a_strcat(cmd, len, globalconf.text);
743 execl(shell, shell, "-c", cmd, NULL);
747 p_delete(&globalconf.text);
748 draw_context_delete(&globalconf.ctx);
749 simplewindow_delete(&globalconf.sw);
750 XCloseDisplay(globalconf.display);
752 return EXIT_SUCCESS;
755 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80