2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
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.
12 #include <sys/types.h>
27 #include <xcb/xcb_aux.h>
28 #include <xcb/xcb_event.h>
31 #include "i3-nagbar.h"
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 };
45 static i3String *prompt;
46 static button_t *buttons;
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 */
57 xcb_connection_t *conn;
58 xcb_screen_t *root_screen;
61 * Having verboselog() and errorlog() is necessary when using libi3.
64 void verboselog(char *fmt, ...) {
68 vfprintf(stdout, fmt, args);
72 void errorlog(char *fmt, ...) {
76 vfprintf(stderr, fmt, args);
81 * Starts the given application by passing it through a shell. We use double fork
82 * to avoid zombie processes. As the started application’s parent exits (immediately),
83 * the application is reparented to init (process-id 1), which correctly handles
84 * childs, so we don’t have to do it :-).
86 * The shell is determined by looking for the SHELL environment variable. If it
87 * does not exist, /bin/sh is used.
90 static void start_application(const char *command) {
91 printf("executing: %s\n", command);
96 /* Stores the path of the shell */
97 static const char *shell = NULL;
100 if ((shell = getenv("SHELL")) == NULL)
103 /* This is the child */
104 execl(shell, shell, "-c", command, (void*)NULL);
112 static button_t *get_button_at(int16_t x, int16_t y) {
113 for (int c = 0; c < buttoncnt; c++)
114 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
120 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
121 printf("button pressed on x = %d, y = %d\n",
122 event->event_x, event->event_y);
123 /* TODO: set a flag for the button, re-render */
127 * Called when the user releases the mouse button. Checks whether the
128 * coordinates are over a button and executes the appropriate action.
131 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
132 printf("button released on x = %d, y = %d\n",
133 event->event_x, event->event_y);
134 /* If the user hits the close button, we exit(0) */
135 if (event->event_x >= (rect.width - 32))
137 button_t *button = get_button_at(event->event_x, event->event_y);
141 /* We need to create a custom script containing our actual command
142 * since not every terminal emulator which is contained in
143 * i3-sensible-terminal supports -e with multiple arguments (and not
144 * all of them support -e with one quoted argument either).
146 * NB: The paths need to be unique, that is, don’t assume users close
147 * their nagbars at any point in time (and they still need to work).
149 char *script_path = get_process_filename("nagbar-cmd");
151 int fd = open(script_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
153 warn("Could not create temporary script to store the nagbar command");
156 FILE *script = fdopen(fd, "w");
157 if (script == NULL) {
158 warn("Could not fdopen() temporary script to store the nagbar command");
161 fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, button->action);
166 sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", script_path);
168 start_application(terminal_cmd);
173 /* TODO: unset flag, re-render */
177 * Handles expose events (redraws of the window) and rendering in general. Will
178 * be called from the code with event == NULL or from X with event != NULL.
181 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
182 /* re-draw the background */
183 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_background });
184 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
186 /* restore font color */
187 set_font_colors(pixmap_gc, color_text, color_background);
188 draw_text(prompt, pixmap, pixmap_gc,
189 4 + 4, 4 + 4, rect.width - 4 - 4);
191 /* render close button */
196 values[0] = color_button_background;
197 values[1] = line_width;
198 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
200 xcb_rectangle_t close = { y - w - (2 * line_width), 0, w + (2 * line_width), rect.height };
201 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
203 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
204 xcb_point_t points[] = {
205 { y - w - (2 * line_width), line_width / 2 },
206 { y - (line_width / 2), line_width / 2 },
207 { y - (line_width / 2), (rect.height - (line_width / 2)) - 2 },
208 { y - w - (2 * line_width), (rect.height - (line_width / 2)) - 2 },
209 { y - w - (2 * line_width), line_width / 2 }
211 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
214 set_font_colors(pixmap_gc, color_text, color_button_background);
215 draw_text_ascii("X", pixmap, pixmap_gc, y - w - line_width + w / 2 - 4,
216 4 + 4 - 1, rect.width - y + w + line_width - w / 2 + 4);
221 /* render custom buttons */
223 for (int c = 0; c < buttoncnt; c++) {
224 /* TODO: make w = text extents of the label */
227 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_button_background });
228 close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
229 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
231 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
232 buttons[c].x = y - w - (2 * line_width);
233 buttons[c].width = w;
234 xcb_point_t points2[] = {
235 { y - w - (2 * line_width), (line_width / 2) + 2 },
236 { y - (line_width / 2), (line_width / 2) + 2 },
237 { y - (line_width / 2), (rect.height - 4 - (line_width / 2)) },
238 { y - w - (2 * line_width), (rect.height - 4 - (line_width / 2)) },
239 { y - w - (2 * line_width), (line_width / 2) + 2 }
241 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
243 values[0] = color_text;
244 values[1] = color_button_background;
245 set_font_colors(pixmap_gc, color_text, color_button_background);
246 draw_text(buttons[c].label, pixmap, pixmap_gc,
247 y - w - line_width + 6, 4 + 3, rect.width - y + w + line_width - 6);
252 /* border line at the bottom */
254 values[0] = color_border_bottom;
255 values[1] = line_width;
256 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
257 xcb_point_t bottom[] = {
258 { 0, rect.height - 0 },
259 { rect.width, rect.height - 0 }
261 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
264 /* Copy the contents of the pixmap to the real window */
265 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
271 int main(int argc, char *argv[]) {
272 char *pattern = sstrdup("-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1");
273 int o, option_index = 0;
274 enum { TYPE_ERROR = 0, TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
276 static struct option long_options[] = {
277 {"version", no_argument, 0, 'v'},
278 {"font", required_argument, 0, 'f'},
279 {"button", required_argument, 0, 'b'},
280 {"help", no_argument, 0, 'h'},
281 {"message", no_argument, 0, 'm'},
282 {"type", required_argument, 0, 't'},
286 char *options_string = "b:f:m:t:vh";
288 prompt = i3string_from_utf8("Please do not run this program.");
290 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
293 printf("i3-nagbar " I3_VERSION);
297 pattern = sstrdup(optarg);
300 i3string_free(prompt);
301 prompt = i3string_from_utf8(optarg);
304 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
307 printf("i3-nagbar " I3_VERSION "\n");
308 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
311 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
312 buttons[buttoncnt].label = i3string_from_utf8(optarg);
313 buttons[buttoncnt].action = argv[optind];
314 printf("button with label *%s* and action *%s*\n",
315 i3string_as_utf8(buttons[buttoncnt].label),
316 buttons[buttoncnt].action);
318 printf("now %d buttons\n", buttoncnt);
326 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
327 xcb_connection_has_error(conn))
328 die("Cannot open display\n");
330 /* Place requests for the atoms we need as soon as possible */
331 #define xmacro(atom) \
332 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
333 #include "atoms.xmacro"
336 root_screen = xcb_aux_get_screen(conn, screens);
337 root = root_screen->root;
339 if (bar_type == TYPE_ERROR) {
340 /* Red theme for error messages */
341 color_button_background = get_colorpixel("#680a0a");
342 color_background = get_colorpixel("#900000");
343 color_text = get_colorpixel("#ffffff");
344 color_border = get_colorpixel("#d92424");
345 color_border_bottom = get_colorpixel("#470909");
347 /* Yellowish theme for warnings */
348 color_button_background = get_colorpixel("#ffc100");
349 color_background = get_colorpixel("#ffa8000");
350 color_text = get_colorpixel("#000000");
351 color_border = get_colorpixel("#ab7100");
352 color_border_bottom = get_colorpixel("#ab7100");
355 font = load_font(pattern, true);
358 /* Open an input window */
359 win = xcb_generate_id(conn);
363 XCB_COPY_FROM_PARENT,
364 win, /* the window id */
365 root, /* parent == root */
366 50, 50, 500, font.height + 8 + 8 /* 8 px padding */, /* dimensions */
367 0, /* x11 border = 0, we draw our own */
368 XCB_WINDOW_CLASS_INPUT_OUTPUT,
369 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
370 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
372 0, /* back pixel: black */
373 XCB_EVENT_MASK_EXPOSURE |
374 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
375 XCB_EVENT_MASK_BUTTON_PRESS |
376 XCB_EVENT_MASK_BUTTON_RELEASE
379 /* Map the window (make it visible) */
380 xcb_map_window(conn, win);
382 /* Setup NetWM atoms */
383 #define xmacro(name) \
385 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
387 die("Could not get atom " # name "\n"); \
389 A_ ## name = reply->atom; \
392 #include "atoms.xmacro"
396 xcb_change_property(conn,
397 XCB_PROP_MODE_REPLACE,
399 A__NET_WM_WINDOW_TYPE,
403 (unsigned char*) &A__NET_WM_WINDOW_TYPE_DOCK);
405 /* Reserve some space at the top of the screen */
411 uint32_t left_start_y;
413 uint32_t right_start_y;
414 uint32_t right_end_y;
415 uint32_t top_start_x;
417 uint32_t bottom_start_x;
418 uint32_t bottom_end_x;
419 } __attribute__((__packed__)) strut_partial = {0,};
421 strut_partial.top = font.height + 6;
422 strut_partial.top_start_x = 0;
423 strut_partial.top_end_x = 800;
425 xcb_change_property(conn,
426 XCB_PROP_MODE_REPLACE,
428 A__NET_WM_STRUT_PARTIAL,
435 pixmap = xcb_generate_id(conn);
436 pixmap_gc = xcb_generate_id(conn);
437 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + 8);
438 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
440 /* Grab the keyboard to get all input */
443 xcb_generic_event_t *event;
444 while ((event = xcb_wait_for_event(conn)) != NULL) {
445 if (event->response_type == 0) {
446 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
450 /* Strip off the highest bit (set if the event is generated) */
451 int type = (event->response_type & 0x7F);
455 handle_expose(conn, (xcb_expose_event_t*)event);
458 case XCB_BUTTON_PRESS:
459 handle_button_press(conn, (xcb_button_press_event_t*)event);
462 case XCB_BUTTON_RELEASE:
463 handle_button_release(conn, (xcb_button_release_event_t*)event);
466 case XCB_CONFIGURE_NOTIFY: {
467 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t*)event;
468 rect = (xcb_rectangle_t){
471 configure_notify->width,
472 configure_notify->height
475 /* Recreate the pixmap / gc */
476 xcb_free_pixmap(conn, pixmap);
477 xcb_free_gc(conn, pixmap_gc);
479 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
480 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);