[progressbar] check_settings should be static
[awesome.git] / awesome-menu.c
blob37b637792e0190daa75960ff3143b5a07769ef5f
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 /** awesome-menu run status */
53 typedef enum
55 /** Stop awesome-menu */
56 STOP = 0,
57 /** Run awesome-menu */
58 RUN = 1,
59 /** Stop awesome-menu and cancel any operation */
60 CANCEL = 2
61 } status_t;
63 /** Is awesome-menu running ? */
64 static status_t status = RUN;
66 /** Import awesome config file format */
67 extern cfg_opt_t awesome_opts[];
69 /** Item_t typedef */
70 typedef struct item_t item_t;
71 /** Item_t structure */
72 struct item_t
74 /** Data */
75 char *data;
76 /** Previous and next elems in item_t list */
77 item_t *prev, *next;
78 /** True if the item currently matches */
79 Bool match;
82 /** Destructor for item structure
83 * \param item item pointer
85 static void
86 item_delete(item_t **item)
88 p_delete(&(*item)->data);
89 p_delete(item);
92 DO_SLIST(item_t, item, item_delete)
94 /** awesome-run global configuration structure */
95 typedef struct
97 /** Display ref */
98 Display *display;
99 /** The window */
100 SimpleWindow *sw;
101 /** The draw contet */
102 DrawCtx *ctx;
103 /** Colors */
104 struct
106 style_t normal;
107 style_t focus;
108 } styles;
109 /** Numlock mask */
110 unsigned int numlockmask;
111 /** The text */
112 char *text;
113 /** The text when we asked to complete */
114 char *text_complete;
115 /** The text length */
116 size_t text_size;
117 /** Item list */
118 item_t *items;
119 /** Selected item */
120 item_t *item_selected;
121 /** What to do with the result text */
122 char *exec;
123 /** Prompt */
124 char *prompt;
125 } AwesomeMenuConf;
127 static AwesomeMenuConf globalconf;
129 /** Exit with given exit code
130 * \param exit_code exit code
131 * \return never returns
133 static void __attribute__ ((noreturn))
134 exit_help(int exit_code)
136 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
137 fprintf(outfile, "Usage: %s [-c config] [-e command] <message>\n",
138 PROGNAME);
139 exit(exit_code);
142 /** Parse configuration file and fill up AwesomeMenuConf
143 * data structures with configuration directives.
144 * \param screen screen number
145 * \param confpatharg configuration file pathname, or NULL if auto
146 * \param menu_title menu title
147 * \param geometry geometry to fill up with supplied information from
148 * configuration file
149 * \return cfg_parse status
151 static int
152 config_parse(int screen, const char *confpatharg,
153 const char *menu_title, area_t *geometry)
155 int ret, i;
156 char *confpath;
157 cfg_t *cfg, *cfg_menu = NULL, *cfg_screen = NULL,
158 *cfg_styles, *cfg_menu_styles = NULL;
160 if(!confpatharg)
161 confpath = config_file();
162 else
163 confpath = a_strdup(confpatharg);
165 cfg = cfg_new();
167 switch((ret = cfg_parse(cfg, confpath)))
169 case CFG_FILE_ERROR:
170 warn("parsing configuration file failed: %s\n", strerror(errno));
171 break;
172 case CFG_PARSE_ERROR:
173 cfg_error(cfg, "W: awesome: parsing configuration file %s failed.\n", confpath);
174 break;
177 if(ret)
178 return ret;
180 if(menu_title && !(cfg_menu = cfg_gettsec(cfg, "menu", menu_title)))
181 warn("no definition for menu %s in configuration file: using default\n",
182 menu_title);
184 /* get global screen section */
185 if(!(cfg_screen = cfg_getnsec(cfg, "screen", screen)))
186 cfg_screen = cfg_getsec(cfg, "screen");
188 if(cfg_menu)
190 cfg_menu_styles = cfg_getsec(cfg_menu, "styles");
191 if((i = cfg_getint(cfg_menu, "x")) != (int) 0xffffffff)
192 geometry->x = i;
193 if((i = cfg_getint(cfg_menu, "y")) != (int) 0xffffffff)
194 geometry->y = i;
195 if((i = cfg_getint(cfg_menu, "width")) > 0)
196 geometry->width = i;
197 if((i = cfg_getint(cfg_menu, "height")) > 0)
198 geometry->height = i;
201 if(cfg_screen
202 && (cfg_styles = cfg_getsec(cfg_screen, "styles")))
204 /* Grab default styles */
205 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
206 cfg_getsec(cfg_styles, "normal"),
207 &globalconf.styles.normal, NULL);
209 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
210 cfg_getsec(cfg_styles, "focus"),
211 &globalconf.styles.focus, &globalconf.styles.normal);
214 /* Now grab menu styles if any */
215 if(cfg_menu_styles)
217 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
218 cfg_getsec(cfg_menu_styles, "normal"),
219 &globalconf.styles.normal, NULL);
221 draw_style_init(globalconf.display, DefaultScreen(globalconf.display),
222 cfg_getsec(cfg_menu_styles, "focus"),
223 &globalconf.styles.focus, &globalconf.styles.normal);
226 if(!globalconf.styles.normal.font)
227 eprint("no default font available\n");
229 p_delete(&confpath);
231 return ret;
234 /** Return the last word for a text.
235 * \param text the text to look into
236 * \return a pointer to the last word position in text
238 static char *
239 get_last_word(char *text)
241 char *last_word;
243 if((last_word = strrchr(text, ' ')))
244 last_word++;
245 else
246 last_word = text;
248 return last_word;
251 /** Fill the completion list for awesome-menu with file list.
252 * \param directory directory to look into
253 * \return always true
255 static Bool
256 item_list_fill_file(const char *directory)
258 char *cwd, *home, *user, *filename;
259 const char *file;
260 DIR *dir;
261 struct dirent *dirinfo;
262 item_t *item;
263 ssize_t len, lenfile;
264 struct passwd *passwd = NULL;
265 struct stat st;
267 item_list_wipe(&globalconf.items);
269 if(!directory)
270 cwd = a_strdup("./");
271 else if(a_strlen(directory) > 1 && directory[0] == '~')
273 if(directory[1] == '/')
275 home = getenv("HOME");
276 asprintf(&cwd, "%s%s", (home ? home : ""), directory + 1);
278 else
280 if(!(file = strchr(directory, '/')))
281 file = directory + a_strlen(directory);
282 len = (file - directory) + 1;
283 user = p_new(char, len);
284 a_strncpy(user, len, directory + 1, (file - directory) - 1);
285 if((passwd = getpwnam(user)))
287 asprintf(&cwd, "%s%s", passwd->pw_dir, file);
288 p_delete(&user);
290 else
292 p_delete(&user);
293 return False;
297 else
298 cwd = a_strdup(directory);
300 if(!(dir = opendir(cwd)))
302 p_delete(&cwd);
303 return False;
306 while((dirinfo = readdir(dir)))
308 item = p_new(item_t, 1);
310 /* + 1 for \0 + 1 for / if directory */
311 len = a_strlen(directory) + a_strlen(dirinfo->d_name) + 2;
313 item->data = p_new(char, len);
314 if(a_strlen(directory))
315 a_strcpy(item->data, len, directory);
316 a_strcat(item->data, len, dirinfo->d_name);
318 lenfile = a_strlen(cwd) + a_strlen(dirinfo->d_name) + 2;
320 filename = p_new(char, lenfile);
321 a_strcpy(filename, lenfile, cwd);
322 a_strcat(filename, lenfile, dirinfo->d_name);
324 if(!stat(filename, &st) && S_ISDIR(st.st_mode))
325 a_strcat(item->data, len, "/");
327 p_delete(&filename);
329 item_list_push(&globalconf.items, item);
332 closedir(dir);
333 p_delete(&cwd);
335 return True;
338 static void
339 complete(Bool reverse)
341 int loop = 2;
342 item_t *item = NULL;
343 item_t *(*item_iter)(item_t **, item_t *) = item_list_next_cycle;
345 if(reverse)
346 item_iter = item_list_prev_cycle;
348 if(globalconf.item_selected)
349 item = item_iter(&globalconf.items, globalconf.item_selected);
350 else
351 item = globalconf.items;
353 for(; item && loop; item = item_iter(&globalconf.items, item))
355 if(item->match)
357 a_strcpy(globalconf.text_complete,
358 globalconf.text_size - (globalconf.text_complete - globalconf.text),
359 item->data);
360 globalconf.item_selected = item;
361 return;
363 /* Since loop is 2, it will be 1 at first iter, and then 0 if we
364 * get back before matching an item (i.e. no items match) to the
365 * first elem: so it will break the loop, otherwise it loops for
366 * ever
368 if(item == globalconf.items)
369 loop--;
373 /** Compute a match from completion list for word.
374 * \param word the word to match
376 static void
377 compute_match(const char *word)
379 ssize_t len = a_strlen(word);
380 item_t *item;
382 /* reset the selected item to NULL */
383 globalconf.item_selected = NULL;
385 if(len)
387 if(word[len - 1] == '/'
388 || word[len - 1] == ' ')
389 item_list_fill_file(word);
391 for(item = globalconf.items; item; item = item->next)
392 if(!a_strncmp(word, item->data, a_strlen(word)))
393 item->match = True;
394 else
395 item->match = False;
397 else
399 if(a_strlen(globalconf.text))
400 item_list_fill_file(NULL);
401 for(item = globalconf.items; item; item = item->next)
402 item->match = True;
406 /* Why not? */
407 #define MARGIN 10
409 /** Redraw the menu. */
410 static void
411 redraw(void)
413 item_t *item;
414 area_t geometry = { 0, 0, 0, 0, NULL, NULL };
415 Bool selected_item_is_drawn = False;
416 int len, prompt_len, x_of_previous_item;
417 style_t style;
419 geometry.width = globalconf.sw->geometry.width;
420 geometry.height = globalconf.sw->geometry.height;
422 if(a_strlen(globalconf.prompt))
424 draw_text(globalconf.ctx, geometry, AlignLeft,
425 MARGIN, globalconf.prompt, globalconf.styles.focus);
427 len = MARGIN * 2 + draw_textwidth(globalconf.display, globalconf.styles.focus.font, globalconf.prompt);
428 geometry.x += len;
429 geometry.width -= len;
432 draw_text(globalconf.ctx, geometry, AlignLeft,
433 MARGIN, globalconf.text, globalconf.styles.normal);
435 len = MARGIN * 2 + MAX(draw_textwidth(globalconf.display, globalconf.styles.normal.font, globalconf.text),
436 geometry.width / 20);
437 geometry.x += len;
438 geometry.width -= len;
439 prompt_len = geometry.x;
441 for(item = globalconf.items; item && geometry.width > 0; item = item->next)
442 if(item->match)
444 style = item == globalconf.item_selected ? globalconf.styles.focus : globalconf.styles.normal;
445 len = MARGIN + draw_textwidth(globalconf.display, style.font, item->data);
446 if(item == globalconf.item_selected)
448 if(len > geometry.width)
449 break;
450 else
451 selected_item_is_drawn = True;
453 draw_text(globalconf.ctx, geometry, AlignLeft,
454 MARGIN / 2, item->data, style);
455 geometry.x += len;
456 geometry.width -= len;
459 /* we have an item selected but not drawn, so redraw in the other side */
460 if(globalconf.item_selected && !selected_item_is_drawn)
462 geometry.x = globalconf.sw->geometry.width;
464 for(item = globalconf.item_selected; item; item = item_list_prev(&globalconf.items, item))
465 if(item->match)
467 style = item == globalconf.item_selected ? globalconf.styles.focus : globalconf.styles.normal;
468 x_of_previous_item = geometry.x;
469 geometry.width = MARGIN + draw_textwidth(globalconf.display, style.font, item->data);
470 geometry.x -= geometry.width;
472 if(geometry.x < prompt_len)
473 break;
475 draw_text(globalconf.ctx, geometry, AlignLeft,
476 MARGIN / 2, item->data, style);
479 if(item)
481 geometry.x = prompt_len;
482 geometry.width = x_of_previous_item - prompt_len;
483 draw_rectangle(globalconf.ctx, geometry, 1.0, True, globalconf.styles.normal.bg);
486 else if(geometry.width)
487 draw_rectangle(globalconf.ctx, geometry, 1.0, True, globalconf.styles.normal.bg);
489 simplewindow_refresh_drawable(globalconf.sw, DefaultScreen(globalconf.display));
490 XSync(globalconf.display, False);
493 /** Handle keypress event in awesome-menu.
494 * \param e received XKeyEvent
496 static void
497 handle_kpress(XKeyEvent *e)
499 char buf[32];
500 KeySym ksym;
501 int num;
502 ssize_t len;
503 size_t text_dst_len;
505 len = a_strlen(globalconf.text);
506 num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
507 if(e->state & ControlMask)
508 switch(ksym)
510 default:
511 return;
512 case XK_bracketleft:
513 ksym = XK_Escape;
514 break;
515 case XK_h:
516 case XK_H:
517 ksym = XK_BackSpace;
518 break;
519 case XK_i:
520 case XK_I:
521 ksym = XK_Tab;
522 break;
523 case XK_j:
524 case XK_J:
525 ksym = XK_Return;
526 break;
527 case XK_c:
528 case XK_C:
529 status = CANCEL;
530 break;
531 case XK_w:
532 case XK_W:
533 /* erase word */
534 if(len)
536 int i = len - 1;
537 while(i >= 0 && globalconf.text[i] == ' ')
538 globalconf.text[i--] = '\0';
539 while(i >= 0 && globalconf.text[i] != ' ')
540 globalconf.text[i--] = '\0';
541 compute_match(get_last_word(globalconf.text));
542 redraw();
544 return;
546 else if(CLEANMASK(e->state) & Mod1Mask)
547 switch(ksym)
549 default:
550 return;
551 case XK_h:
552 ksym = XK_Left;
553 break;
554 case XK_l:
555 ksym = XK_Right;
556 break;
559 switch(ksym)
561 case XK_space:
562 globalconf.text_complete = globalconf.text + a_strlen(globalconf.text) + 1;
563 default:
564 if(num && !iscntrl((int) buf[0]))
566 if(buf[0] != '/' || globalconf.text[len - 1] != '/')
568 buf[num] = '\0';
570 /* Reallocate text string if needed to hold
571 * concatenation of text and buf */
572 if((text_dst_len = (a_strlen(globalconf.text) + num - 1)) > globalconf.text_size)
574 globalconf.text_size += ((int) (text_dst_len / globalconf.text_size)) * CHUNK_SIZE;
575 p_realloc(&globalconf.text, globalconf.text_size);
577 a_strncat(globalconf.text, globalconf.text_size, buf, num);
579 compute_match(get_last_word(globalconf.text));
580 redraw();
582 break;
583 case XK_BackSpace:
584 if(len)
586 globalconf.text[--len] = '\0';
587 compute_match(get_last_word(globalconf.text));
588 redraw();
590 break;
591 case XK_ISO_Left_Tab:
592 case XK_Left:
593 complete(True);
594 redraw();
595 break;
596 case XK_Right:
597 case XK_Tab:
598 complete(False);
599 redraw();
600 break;
601 case XK_Escape:
602 status= CANCEL;
603 break;
604 case XK_Return:
605 status = STOP;
606 break;
610 /** Fill the completion by reading on stdin.
611 * \return true if something has been filled up, false otherwise.
613 static Bool
614 item_list_fill_stdin(void)
616 char *buf = NULL;
617 size_t len = 0;
618 ssize_t line_len;
620 item_t *newitem;
621 Bool has_entry = False;
623 item_list_init(&globalconf.items);
625 if((line_len = getline(&buf, &len, stdin)) != -1)
626 has_entry = True;
628 if(has_entry)
631 buf[line_len - 1] = '\0';
632 newitem = p_new(item_t, 1);
633 newitem->data = a_strdup(buf);
634 newitem->match = True;
635 item_list_append(&globalconf.items, newitem);
637 while((line_len = getline(&buf, &len, stdin)) != -1);
639 if(buf)
640 p_delete(&buf);
642 return has_entry;
645 /** Grab the keyboard.
647 static void
648 keyboard_grab(void)
650 int i;
651 for(i = 1000; i; i--)
653 if(XGrabKeyboard(globalconf.display, DefaultRootWindow(globalconf.display), True,
654 GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
655 break;
656 usleep(1000);
658 if(!i)
659 eprint("cannot grab keyboard");
662 /** Main function of awesome-menu.
663 * \param argc number of elements in argv
664 * \param argv arguments array
665 * \return EXIT_SUCCESS
668 main(int argc, char **argv)
670 XEvent ev;
671 int opt, ret, x, y, i, screen = 0;
672 char *configfile = NULL, *cmd;
673 ssize_t len;
674 const char *shell = getenv("SHELL");
675 area_t geometry = { 0, 0, 0, 0, NULL, NULL };
676 ScreensInfo *si;
677 unsigned int ui;
678 Window dummy;
679 static struct option long_options[] =
681 {"help", 0, NULL, 'h'},
682 {"version", 0, NULL, 'v'},
683 {"exec", 0, NULL, 'e'},
684 {NULL, 0, NULL, 0}
687 while((opt = getopt_long(argc, argv, "vhf:b:x:y:n:c:e:",
688 long_options, NULL)) != -1)
689 switch(opt)
691 case 'v':
692 eprint_version(PROGNAME);
693 break;
694 case 'h':
695 exit_help(EXIT_SUCCESS);
696 break;
697 case 'c':
698 configfile = a_strdup(optarg);
699 break;
700 case 'e':
701 globalconf.exec = a_strdup(optarg);
702 break;
705 if(argc - optind >= 1)
706 globalconf.prompt = a_strdup(argv[optind]);
708 if(!(globalconf.display = XOpenDisplay(NULL)))
709 eprint("unable to open display");
711 /* Get the numlock mask */
712 globalconf.numlockmask = xgetnumlockmask(globalconf.display);
714 si = screensinfo_new(globalconf.display);
715 if(si->xinerama_is_active)
717 if(XQueryPointer(globalconf.display, RootWindow(globalconf.display, DefaultScreen(globalconf.display)),
718 &dummy, &dummy, &x, &y, &i, &i, &ui))
720 screen = screen_get_bycoord(si, 0, x, y);
722 geometry.x = si->geometry[screen].x;
723 geometry.y = si->geometry[screen].y;
724 geometry.width = si->geometry[screen].width;
727 else
729 screen = DefaultScreen(globalconf.display);
730 if(!geometry.width)
731 geometry.width = DisplayWidth(globalconf.display, DefaultScreen(globalconf.display));
734 if((ret = config_parse(screen, configfile, globalconf.prompt, &geometry)))
735 return ret;
737 /* Init the geometry */
738 if(!geometry.height)
739 geometry.height = 1.5 * MAX(globalconf.styles.normal.font->height,
740 globalconf.styles.focus.font->height);
742 screensinfo_delete(&si);
744 /* Create the window */
745 globalconf.sw = simplewindow_new(globalconf.display, DefaultScreen(globalconf.display),
746 geometry.x, geometry.y, geometry.width, geometry.height, 0);
748 XStoreName(globalconf.display, globalconf.sw->window, PROGNAME);
750 /* Create the drawing context */
751 globalconf.ctx = draw_context_new(globalconf.display, DefaultScreen(globalconf.display),
752 geometry.width, geometry.height,
753 globalconf.sw->drawable);
755 /* Allocate a default size for the text on the heap instead of
756 * using stack allocation with PATH_MAX (may not been defined
757 * according to POSIX). This string size may be increased if
758 * needed */
759 globalconf.text_complete = globalconf.text = p_new(char, CHUNK_SIZE);
760 globalconf.text_size = CHUNK_SIZE;
762 if(isatty(STDIN_FILENO))
764 if(!item_list_fill_stdin())
765 item_list_fill_file(NULL);
766 keyboard_grab();
768 else
770 keyboard_grab();
771 if(!item_list_fill_stdin())
772 item_list_fill_file(NULL);
775 compute_match(NULL);
777 redraw();
779 XMapRaised(globalconf.display, globalconf.sw->window);
781 while(status == RUN)
783 XNextEvent(globalconf.display, &ev);
784 switch(ev.type)
786 case ButtonPress:
787 status = CANCEL;
788 break;
789 case KeyPress:
790 handle_kpress(&ev.xkey);
791 break;
792 case Expose:
793 if(!ev.xexpose.count)
794 simplewindow_refresh_drawable(globalconf.sw, DefaultScreen(globalconf.display));
795 break;
796 default:
797 break;
801 if(status != CANCEL)
803 if(!globalconf.exec)
804 printf("%s\n", globalconf.text);
805 else
807 if(!shell)
808 shell = a_strdup("/bin/sh");
809 len = a_strlen(globalconf.exec) + a_strlen(globalconf.text) + 1;
810 cmd = p_new(char, len);
811 a_strcpy(cmd, len, globalconf.exec);
812 a_strcat(cmd, len, globalconf.text);
813 execl(shell, shell, "-c", cmd, NULL);
817 p_delete(&globalconf.text);
818 draw_context_delete(&globalconf.ctx);
819 simplewindow_delete(&globalconf.sw);
820 XCloseDisplay(globalconf.display);
822 return EXIT_SUCCESS;
825 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80