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