]> git.sur5r.net Git - i3/i3/blob - i3-nagbar/main.c
Merge branch 'bar-config' 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  *
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 "i3-nagbar.h"
32
33 typedef struct {
34     char *label;
35     char *action;
36     int16_t x;
37     uint16_t width;
38 } button_t;
39
40 static xcb_window_t win;
41 static xcb_pixmap_t pixmap;
42 static xcb_gcontext_t pixmap_gc;
43 static xcb_rectangle_t rect = { 0, 0, 600, 20 };
44 static int font_height;
45 static char *prompt = "Please do not run this program.";
46 static button_t *buttons;
47 static int buttoncnt;
48
49 /* Result of get_colorpixel() for the various colors. */
50 static uint32_t color_background;        /* background of the bar */
51 static uint32_t color_button_background; /* background for buttons */
52 static uint32_t color_border;            /* color of the button border */
53 static uint32_t color_border_bottom;     /* color of the bottom border */
54 static uint32_t color_text;              /* color of the text */
55
56 xcb_window_t root;
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_single(conn, pixmap_gc, XCB_GC_FOREGROUND, color_background);
131     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
132
133     /* restore font color */
134     uint32_t values[3];
135     values[0] = color_text;
136     values[1] = color_background;
137     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
138     xcb_image_text_8(conn, strlen(prompt), pixmap, pixmap_gc, 4 + 4/* X */,
139                       font_height + 2 + 4 /* Y = baseline of font */, prompt);
140
141     /* render close button */
142     int line_width = 4;
143     int w = 20;
144     int y = rect.width;
145     values[0] = color_button_background;
146     values[1] = line_width;
147     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
148
149     xcb_rectangle_t close = { y - w - (2 * line_width), 0, w + (2 * line_width), rect.height };
150     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
151
152     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, color_border);
153     xcb_point_t points[] = {
154         { y - w - (2 * line_width), line_width / 2 },
155         { y - (line_width / 2), line_width / 2 },
156         { y - (line_width / 2), (rect.height - (line_width / 2)) - 2 },
157         { y - w - (2 * line_width), (rect.height - (line_width / 2)) - 2 },
158         { y - w - (2 * line_width), line_width / 2 }
159     };
160     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
161
162     values[0] = color_text;
163     values[1] = color_button_background;
164     values[2] = 1;
165     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_LINE_WIDTH, values);
166     xcb_image_text_8(conn, strlen("x"), pixmap, pixmap_gc, y - w - line_width + (w / 2) - 4/* X */,
167                       font_height + 2 + 4 - 1/* Y = baseline of font */, "X");
168     y -= w;
169
170     y -= 20;
171
172     /* render custom buttons */
173     line_width = 1;
174     for (int c = 0; c < buttoncnt; c++) {
175         /* TODO: make w = text extents of the label */
176         w = 100;
177         y -= 30;
178         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, color_button_background);
179         close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
180         xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
181
182         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, color_border);
183         buttons[c].x = y - w - (2 * line_width);
184         buttons[c].width = w;
185         xcb_point_t points2[] = {
186             { y - w - (2 * line_width), (line_width / 2) + 2 },
187             { y - (line_width / 2), (line_width / 2) + 2 },
188             { y - (line_width / 2), (rect.height - 4 - (line_width / 2)) },
189             { y - w - (2 * line_width), (rect.height - 4 - (line_width / 2)) },
190             { y - w - (2 * line_width), (line_width / 2) + 2 }
191         };
192         xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
193
194         values[0] = color_text;
195         values[1] = color_button_background;
196         xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
197         xcb_image_text_8(conn, strlen(buttons[c].label), pixmap, pixmap_gc, y - w - line_width + 6/* X */,
198                           font_height + 2 + 3/* Y = baseline of font */, buttons[c].label);
199
200         y -= w;
201     }
202
203     /* border line at the bottom */
204     line_width = 2;
205     values[0] = color_border_bottom;
206     values[1] = line_width;
207     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
208     xcb_point_t bottom[] = {
209         { 0, rect.height - 0 },
210         { rect.width, rect.height - 0 }
211     };
212     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
213
214
215     /* Copy the contents of the pixmap to the real window */
216     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
217     xcb_flush(conn);
218
219     return 1;
220 }
221
222 int main(int argc, char *argv[]) {
223     char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
224     int o, option_index = 0;
225     enum { TYPE_ERROR = 0, TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
226
227     static struct option long_options[] = {
228         {"version", no_argument, 0, 'v'},
229         {"font", required_argument, 0, 'f'},
230         {"button", required_argument, 0, 'b'},
231         {"help", no_argument, 0, 'h'},
232         {"message", no_argument, 0, 'm'},
233         {"type", required_argument, 0, 't'},
234         {0, 0, 0, 0}
235     };
236
237     char *options_string = "b:f:m:t:vh";
238
239     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
240         switch (o) {
241             case 'v':
242                 printf("i3-nagbar " I3_VERSION);
243                 return 0;
244             case 'f':
245                 FREE(pattern);
246                 pattern = strdup(optarg);
247                 break;
248             case 'm':
249                 prompt = strdup(optarg);
250                 break;
251             case 't':
252                 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
253                 break;
254             case 'h':
255                 printf("i3-nagbar " I3_VERSION "\n");
256                 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-f <font>] [-v]\n");
257                 return 0;
258             case 'b':
259                 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
260                 buttons[buttoncnt].label = optarg;
261                 buttons[buttoncnt].action = argv[optind];
262                 printf("button with label *%s* and action *%s*\n",
263                         buttons[buttoncnt].label,
264                         buttons[buttoncnt].action);
265                 buttoncnt++;
266                 printf("now %d buttons\n", buttoncnt);
267                 if (optind < argc)
268                     optind++;
269                 break;
270         }
271     }
272
273     int screens;
274     xcb_connection_t *conn;
275     if ((conn = xcb_connect(NULL, &screens)) == NULL ||
276         xcb_connection_has_error(conn))
277         die("Cannot open display\n");
278
279     /* Place requests for the atoms we need as soon as possible */
280     #define xmacro(atom) \
281         xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
282     #include "atoms.xmacro"
283     #undef xmacro
284
285     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
286     root = root_screen->root;
287
288     if (bar_type == TYPE_ERROR) {
289         /* Red theme for error messages */
290         color_button_background = get_colorpixel(conn, "#680a0a");
291         color_background = get_colorpixel(conn, "#900000");
292         color_text = get_colorpixel(conn, "#ffffff");
293         color_border = get_colorpixel(conn, "#d92424");
294         color_border_bottom = get_colorpixel(conn, "#470909");
295     } else {
296         /* Yellowish theme for warnings */
297         color_button_background = get_colorpixel(conn, "#ffc100");
298         color_background = get_colorpixel(conn, "#ffa8000");
299         color_text = get_colorpixel(conn, "#000000");
300         color_border = get_colorpixel(conn, "#ab7100");
301         color_border_bottom = get_colorpixel(conn, "#ab7100");
302     }
303
304     uint32_t font_id = get_font_id(conn, pattern, &font_height);
305
306     /* Open an input window */
307     win = open_input_window(conn, 500, font_height + 8 + 8 /* 8px padding */);
308
309     /* Setup NetWM atoms */
310     #define xmacro(name) \
311         do { \
312             xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
313             if (!reply) \
314                 die("Could not get atom " # name "\n"); \
315             \
316             A_ ## name = reply->atom; \
317             free(reply); \
318         } while (0);
319     #include "atoms.xmacro"
320     #undef xmacro
321
322     /* Set dock mode */
323     xcb_change_property(conn,
324         XCB_PROP_MODE_REPLACE,
325         win,
326         A__NET_WM_WINDOW_TYPE,
327         A_ATOM,
328         32,
329         1,
330         (unsigned char*) &A__NET_WM_WINDOW_TYPE_DOCK);
331
332     /* Reserve some space at the top of the screen */
333     struct {
334         uint32_t left;
335         uint32_t right;
336         uint32_t top;
337         uint32_t bottom;
338         uint32_t left_start_y;
339         uint32_t left_end_y;
340         uint32_t right_start_y;
341         uint32_t right_end_y;
342         uint32_t top_start_x;
343         uint32_t top_end_x;
344         uint32_t bottom_start_x;
345         uint32_t bottom_end_x;
346     } __attribute__((__packed__)) strut_partial = {0,};
347
348     strut_partial.top = font_height + 6;
349     strut_partial.top_start_x = 0;
350     strut_partial.top_end_x = 800;
351
352     xcb_change_property(conn,
353         XCB_PROP_MODE_REPLACE,
354         win,
355         A__NET_WM_STRUT_PARTIAL,
356         A_CARDINAL,
357         32,
358         12,
359         &strut_partial);
360
361     /* Create pixmap */
362     pixmap = xcb_generate_id(conn);
363     pixmap_gc = xcb_generate_id(conn);
364     xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
365     xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
366
367     /* Create graphics context */
368     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
369
370     /* Grab the keyboard to get all input */
371     xcb_flush(conn);
372
373     xcb_generic_event_t *event;
374     while ((event = xcb_wait_for_event(conn)) != NULL) {
375         if (event->response_type == 0) {
376             fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
377             continue;
378         }
379
380         /* Strip off the highest bit (set if the event is generated) */
381         int type = (event->response_type & 0x7F);
382
383         switch (type) {
384             case XCB_EXPOSE:
385                 handle_expose(conn, (xcb_expose_event_t*)event);
386                 break;
387
388             case XCB_BUTTON_PRESS:
389                 handle_button_press(conn, (xcb_button_press_event_t*)event);
390                 break;
391
392             case XCB_BUTTON_RELEASE:
393                 handle_button_release(conn, (xcb_button_release_event_t*)event);
394                 break;
395
396             case XCB_CONFIGURE_NOTIFY: {
397                 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t*)event;
398                 rect = (xcb_rectangle_t){
399                     configure_notify->x,
400                     configure_notify->y,
401                     configure_notify->width,
402                     configure_notify->height
403                 };
404
405                 /* Recreate the pixmap / gc */
406                 xcb_free_pixmap(conn, pixmap);
407                 xcb_free_gc(conn, pixmap_gc);
408
409                 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
410                 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
411
412                 /* Create graphics context */
413                 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
414                 break;
415             }
416         }
417
418         free(event);
419     }
420
421     return 0;
422 }