]> git.sur5r.net Git - i3/i3/blob - i3-nagbar/main.c
Merge branch 'master' into next
[i3/i3] / i3-nagbar / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * i3-nagbar is a utility which displays a nag message, for example in the case
8  * when the user has an error in his configuration file.
9  *
10  */
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <err.h>
20 #include <stdint.h>
21 #include <getopt.h>
22 #include <limits.h>
23
24 #include <xcb/xcb.h>
25 #include <xcb/xcb_aux.h>
26 #include <xcb/xcb_event.h>
27
28 #include "libi3.h"
29 #include "i3-nagbar.h"
30
31 typedef struct {
32     i3String *label;
33     char *action;
34     int16_t x;
35     uint16_t width;
36 } button_t;
37
38 static xcb_window_t win;
39 static xcb_pixmap_t pixmap;
40 static xcb_gcontext_t pixmap_gc;
41 static xcb_rectangle_t rect = { 0, 0, 600, 20 };
42 static i3Font font;
43 static i3String *prompt;
44 static button_t *buttons;
45 static int buttoncnt;
46
47 /* Result of get_colorpixel() for the various colors. */
48 static uint32_t color_background;        /* background of the bar */
49 static uint32_t color_button_background; /* background for buttons */
50 static uint32_t color_border;            /* color of the button border */
51 static uint32_t color_border_bottom;     /* color of the bottom border */
52 static uint32_t color_text;              /* color of the text */
53
54 xcb_window_t root;
55 xcb_connection_t *conn;
56 xcb_screen_t *root_screen;
57
58 /*
59  * Having verboselog() and errorlog() is necessary when using libi3.
60  *
61  */
62 void verboselog(char *fmt, ...) {
63     va_list args;
64
65     va_start(args, fmt);
66     vfprintf(stdout, fmt, args);
67     va_end(args);
68 }
69
70 void errorlog(char *fmt, ...) {
71     va_list args;
72
73     va_start(args, fmt);
74     vfprintf(stderr, fmt, args);
75     va_end(args);
76 }
77
78 /*
79  * Starts the given application by passing it through a shell. We use double fork
80  * to avoid zombie processes. As the started application’s parent exits (immediately),
81  * the application is reparented to init (process-id 1), which correctly handles
82  * childs, so we don’t have to do it :-).
83  *
84  * The shell is determined by looking for the SHELL environment variable. If it
85  * does not exist, /bin/sh is used.
86  *
87  */
88 static void start_application(const char *command) {
89     printf("executing: %s\n", command);
90     if (fork() == 0) {
91         /* Child process */
92         setsid();
93         if (fork() == 0) {
94             /* Stores the path of the shell */
95             static const char *shell = NULL;
96
97             if (shell == NULL)
98                 if ((shell = getenv("SHELL")) == NULL)
99                     shell = "/bin/sh";
100
101             /* This is the child */
102             execl(shell, shell, "-c", command, (void*)NULL);
103             /* not reached */
104         }
105         exit(0);
106     }
107     wait(0);
108 }
109
110 static button_t *get_button_at(int16_t x, int16_t y) {
111     for (int c = 0; c < buttoncnt; c++)
112         if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
113             return &buttons[c];
114
115     return NULL;
116 }
117
118 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
119     printf("button pressed on x = %d, y = %d\n",
120             event->event_x, event->event_y);
121     /* TODO: set a flag for the button, re-render */
122 }
123
124 /*
125  * Called when the user releases the mouse button. Checks whether the
126  * coordinates are over a button and executes the appropriate action.
127  *
128  */
129 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
130     printf("button released on x = %d, y = %d\n",
131             event->event_x, event->event_y);
132     /* If the user hits the close button, we exit(0) */
133     if (event->event_x >= (rect.width - 32))
134         exit(0);
135     button_t *button = get_button_at(event->event_x, event->event_y);
136     if (!button)
137         return;
138     start_application(button->action);
139
140     /* TODO: unset flag, re-render */
141 }
142
143 /*
144  * Handles expose events (redraws of the window) and rendering in general. Will
145  * be called from the code with event == NULL or from X with event != NULL.
146  *
147  */
148 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
149     /* re-draw the background */
150     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_background });
151     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
152
153     /* restore font color */
154     set_font_colors(pixmap_gc, color_text, color_background);
155     draw_text(prompt, pixmap, pixmap_gc,
156             4 + 4, 4 + 4, rect.width - 4 - 4);
157
158     /* render close button */
159     int line_width = 4;
160     int w = 20;
161     int y = rect.width;
162     uint32_t values[3];
163     values[0] = color_button_background;
164     values[1] = line_width;
165     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
166
167     xcb_rectangle_t close = { y - w - (2 * line_width), 0, w + (2 * line_width), rect.height };
168     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
169
170     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
171     xcb_point_t points[] = {
172         { y - w - (2 * line_width), line_width / 2 },
173         { y - (line_width / 2), line_width / 2 },
174         { y - (line_width / 2), (rect.height - (line_width / 2)) - 2 },
175         { y - w - (2 * line_width), (rect.height - (line_width / 2)) - 2 },
176         { y - w - (2 * line_width), line_width / 2 }
177     };
178     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
179
180     values[0] = 1;
181     set_font_colors(pixmap_gc, color_text, color_button_background);
182     draw_text_ascii("X", pixmap, pixmap_gc, y - w - line_width + w / 2 - 4,
183             4 + 4 - 1, rect.width - y + w + line_width - w / 2 + 4);
184     y -= w;
185
186     y -= 20;
187
188     /* render custom buttons */
189     line_width = 1;
190     for (int c = 0; c < buttoncnt; c++) {
191         /* TODO: make w = text extents of the label */
192         w = 100;
193         y -= 30;
194         xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_button_background });
195         close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
196         xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
197
198         xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
199         buttons[c].x = y - w - (2 * line_width);
200         buttons[c].width = w;
201         xcb_point_t points2[] = {
202             { y - w - (2 * line_width), (line_width / 2) + 2 },
203             { y - (line_width / 2), (line_width / 2) + 2 },
204             { y - (line_width / 2), (rect.height - 4 - (line_width / 2)) },
205             { y - w - (2 * line_width), (rect.height - 4 - (line_width / 2)) },
206             { y - w - (2 * line_width), (line_width / 2) + 2 }
207         };
208         xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
209
210         values[0] = color_text;
211         values[1] = color_button_background;
212         set_font_colors(pixmap_gc, color_text, color_button_background);
213         draw_text(buttons[c].label, pixmap, pixmap_gc,
214                 y - w - line_width + 6, 4 + 3, rect.width - y + w + line_width - 6);
215
216         y -= w;
217     }
218
219     /* border line at the bottom */
220     line_width = 2;
221     values[0] = color_border_bottom;
222     values[1] = line_width;
223     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
224     xcb_point_t bottom[] = {
225         { 0, rect.height - 0 },
226         { rect.width, rect.height - 0 }
227     };
228     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
229
230
231     /* Copy the contents of the pixmap to the real window */
232     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
233     xcb_flush(conn);
234
235     return 1;
236 }
237
238 int main(int argc, char *argv[]) {
239     char *pattern = sstrdup("-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1");
240     int o, option_index = 0;
241     enum { TYPE_ERROR = 0, TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
242
243     static struct option long_options[] = {
244         {"version", no_argument, 0, 'v'},
245         {"font", required_argument, 0, 'f'},
246         {"button", required_argument, 0, 'b'},
247         {"help", no_argument, 0, 'h'},
248         {"message", no_argument, 0, 'm'},
249         {"type", required_argument, 0, 't'},
250         {0, 0, 0, 0}
251     };
252
253     char *options_string = "b:f:m:t:vh";
254
255     prompt = i3string_from_utf8("Please do not run this program.");
256
257     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
258         switch (o) {
259             case 'v':
260                 printf("i3-nagbar " I3_VERSION);
261                 return 0;
262             case 'f':
263                 FREE(pattern);
264                 pattern = sstrdup(optarg);
265                 break;
266             case 'm':
267                 i3string_free(prompt);
268                 prompt = i3string_from_utf8(optarg);
269                 break;
270             case 't':
271                 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
272                 break;
273             case 'h':
274                 printf("i3-nagbar " I3_VERSION "\n");
275                 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
276                 return 0;
277             case 'b':
278                 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
279                 buttons[buttoncnt].label = i3string_from_utf8(optarg);
280                 buttons[buttoncnt].action = argv[optind];
281                 printf("button with label *%s* and action *%s*\n",
282                         i3string_as_utf8(buttons[buttoncnt].label),
283                         buttons[buttoncnt].action);
284                 buttoncnt++;
285                 printf("now %d buttons\n", buttoncnt);
286                 if (optind < argc)
287                     optind++;
288                 break;
289         }
290     }
291
292     int screens;
293     if ((conn = xcb_connect(NULL, &screens)) == NULL ||
294         xcb_connection_has_error(conn))
295         die("Cannot open display\n");
296
297     /* Place requests for the atoms we need as soon as possible */
298     #define xmacro(atom) \
299         xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
300     #include "atoms.xmacro"
301     #undef xmacro
302
303     root_screen = xcb_aux_get_screen(conn, screens);
304     root = root_screen->root;
305
306     if (bar_type == TYPE_ERROR) {
307         /* Red theme for error messages */
308         color_button_background = get_colorpixel("#680a0a");
309         color_background = get_colorpixel("#900000");
310         color_text = get_colorpixel("#ffffff");
311         color_border = get_colorpixel("#d92424");
312         color_border_bottom = get_colorpixel("#470909");
313     } else {
314         /* Yellowish theme for warnings */
315         color_button_background = get_colorpixel("#ffc100");
316         color_background = get_colorpixel("#ffa8000");
317         color_text = get_colorpixel("#000000");
318         color_border = get_colorpixel("#ab7100");
319         color_border_bottom = get_colorpixel("#ab7100");
320     }
321
322     font = load_font(pattern, true);
323     set_font(&font);
324
325     /* Open an input window */
326     win = xcb_generate_id(conn);
327
328     xcb_create_window(
329         conn,
330         XCB_COPY_FROM_PARENT,
331         win, /* the window id */
332         root, /* parent == root */
333         50, 50, 500, font.height + 8 + 8 /* 8 px padding */, /* dimensions */
334         0, /* x11 border = 0, we draw our own */
335         XCB_WINDOW_CLASS_INPUT_OUTPUT,
336         XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
337         XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
338         (uint32_t[]){
339             0, /* back pixel: black */
340             XCB_EVENT_MASK_EXPOSURE |
341             XCB_EVENT_MASK_STRUCTURE_NOTIFY |
342             XCB_EVENT_MASK_BUTTON_PRESS |
343             XCB_EVENT_MASK_BUTTON_RELEASE
344         });
345
346     /* Map the window (make it visible) */
347     xcb_map_window(conn, win);
348
349     /* Setup NetWM atoms */
350     #define xmacro(name) \
351         do { \
352             xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
353             if (!reply) \
354                 die("Could not get atom " # name "\n"); \
355             \
356             A_ ## name = reply->atom; \
357             free(reply); \
358         } while (0);
359     #include "atoms.xmacro"
360     #undef xmacro
361
362     /* Set dock mode */
363     xcb_change_property(conn,
364         XCB_PROP_MODE_REPLACE,
365         win,
366         A__NET_WM_WINDOW_TYPE,
367         A_ATOM,
368         32,
369         1,
370         (unsigned char*) &A__NET_WM_WINDOW_TYPE_DOCK);
371
372     /* Reserve some space at the top of the screen */
373     struct {
374         uint32_t left;
375         uint32_t right;
376         uint32_t top;
377         uint32_t bottom;
378         uint32_t left_start_y;
379         uint32_t left_end_y;
380         uint32_t right_start_y;
381         uint32_t right_end_y;
382         uint32_t top_start_x;
383         uint32_t top_end_x;
384         uint32_t bottom_start_x;
385         uint32_t bottom_end_x;
386     } __attribute__((__packed__)) strut_partial = {0,};
387
388     strut_partial.top = font.height + 6;
389     strut_partial.top_start_x = 0;
390     strut_partial.top_end_x = 800;
391
392     xcb_change_property(conn,
393         XCB_PROP_MODE_REPLACE,
394         win,
395         A__NET_WM_STRUT_PARTIAL,
396         A_CARDINAL,
397         32,
398         12,
399         &strut_partial);
400
401     /* Create pixmap */
402     pixmap = xcb_generate_id(conn);
403     pixmap_gc = xcb_generate_id(conn);
404     xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + 8);
405     xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
406
407     /* Grab the keyboard to get all input */
408     xcb_flush(conn);
409
410     xcb_generic_event_t *event;
411     while ((event = xcb_wait_for_event(conn)) != NULL) {
412         if (event->response_type == 0) {
413             fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
414             continue;
415         }
416
417         /* Strip off the highest bit (set if the event is generated) */
418         int type = (event->response_type & 0x7F);
419
420         switch (type) {
421             case XCB_EXPOSE:
422                 handle_expose(conn, (xcb_expose_event_t*)event);
423                 break;
424
425             case XCB_BUTTON_PRESS:
426                 handle_button_press(conn, (xcb_button_press_event_t*)event);
427                 break;
428
429             case XCB_BUTTON_RELEASE:
430                 handle_button_release(conn, (xcb_button_release_event_t*)event);
431                 break;
432
433             case XCB_CONFIGURE_NOTIFY: {
434                 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t*)event;
435                 rect = (xcb_rectangle_t){
436                     configure_notify->x,
437                     configure_notify->y,
438                     configure_notify->width,
439                     configure_notify->height
440                 };
441
442                 /* Recreate the pixmap / gc */
443                 xcb_free_pixmap(conn, pixmap);
444                 xcb_free_gc(conn, pixmap_gc);
445
446                 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
447                 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
448                 break;
449             }
450         }
451
452         free(event);
453     }
454
455     FREE(pattern);
456
457     return 0;
458 }