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