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