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