2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 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 their configuration file.
12 #include <sys/types.h>
28 #include <xcb/xcb_aux.h>
29 #include <xcb/xcb_event.h>
30 #include <xcb/randr.h>
31 #include <xcb/xcb_cursor.h>
34 #include "i3-nagbar.h"
36 /** This is the equivalent of XC_left_ptr. I’m not sure why xcb doesn’t have a
37 * constant for that. */
38 #define XCB_CURSOR_LEFT_PTR 68
40 static char *argv0 = NULL;
49 static xcb_window_t win;
50 static xcb_pixmap_t pixmap;
51 static xcb_gcontext_t pixmap_gc;
52 static xcb_rectangle_t rect = {0, 0, 600, 20};
54 static i3String *prompt;
55 static button_t *buttons;
58 /* Result of get_colorpixel() for the various colors. */
59 static color_t color_background; /* background of the bar */
60 static color_t color_button_background; /* background for buttons */
61 static color_t color_border; /* color of the button border */
62 static color_t color_border_bottom; /* color of the bottom border */
63 static color_t color_text; /* color of the text */
66 xcb_connection_t *conn;
67 xcb_screen_t *root_screen;
70 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
73 void verboselog(char *fmt, ...) {
77 vfprintf(stdout, fmt, args);
81 void errorlog(char *fmt, ...) {
85 vfprintf(stderr, fmt, args);
89 void debuglog(char *fmt, ...) {
93 * Starts the given application by passing it through a shell. We use double fork
94 * to avoid zombie processes. As the started application’s parent exits (immediately),
95 * the application is reparented to init (process-id 1), which correctly handles
96 * childs, so we don’t have to do it :-).
98 * The shell is determined by looking for the SHELL environment variable. If it
99 * does not exist, /bin/sh is used.
102 static void start_application(const char *command) {
103 printf("executing: %s\n", command);
108 /* This is the child */
109 execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
117 static button_t *get_button_at(int16_t x, int16_t y) {
118 for (int c = 0; c < buttoncnt; c++)
119 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
125 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
126 printf("button pressed on x = %d, y = %d\n",
127 event->event_x, event->event_y);
128 /* TODO: set a flag for the button, re-render */
132 * Called when the user releases the mouse button. Checks whether the
133 * coordinates are over a button and executes the appropriate action.
136 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
137 printf("button released on x = %d, y = %d\n",
138 event->event_x, event->event_y);
139 /* If the user hits the close button, we exit(0) */
140 if (event->event_x >= (rect.width - logical_px(32)))
142 button_t *button = get_button_at(event->event_x, event->event_y);
146 /* We need to create a custom script containing our actual command
147 * since not every terminal emulator which is contained in
148 * i3-sensible-terminal supports -e with multiple arguments (and not
149 * all of them support -e with one quoted argument either).
151 * NB: The paths need to be unique, that is, don’t assume users close
152 * their nagbars at any point in time (and they still need to work).
154 char *script_path = get_process_filename("nagbar-cmd");
156 int fd = open(script_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
158 warn("Could not create temporary script to store the nagbar command");
161 FILE *script = fdopen(fd, "w");
162 if (script == NULL) {
163 warn("Could not fdopen() temporary script to store the nagbar command");
166 fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, button->action);
171 char *exe_path = get_exe_path(argv0);
172 sasprintf(&link_path, "%s.nagbar_cmd", script_path);
173 if (symlink(exe_path, link_path) == -1) {
174 err(EXIT_FAILURE, "Failed to symlink %s to %s", link_path, exe_path);
178 sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
179 printf("argv0 = %s\n", argv0);
180 printf("terminal_cmd = %s\n", terminal_cmd);
182 start_application(terminal_cmd);
189 /* TODO: unset flag, re-render */
193 * Handles expose events (redraws of the window) and rendering in general. Will
194 * be called from the code with event == NULL or from X with event != NULL.
197 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
198 /* re-draw the background */
199 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_background.colorpixel});
200 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
202 /* restore font color */
203 set_font_colors(pixmap_gc, color_text, color_background);
204 draw_text(prompt, pixmap, pixmap_gc, NULL,
205 logical_px(4) + logical_px(4),
206 logical_px(4) + logical_px(4),
207 rect.width - logical_px(4) - logical_px(4));
209 /* render close button */
210 const char *close_button_label = "X";
211 int line_width = logical_px(4);
212 /* set width to the width of the label */
213 int w = predict_text_width(i3string_from_utf8(close_button_label));
214 /* account for left/right padding, which seems to be set to 8px (total) below */
218 values[0] = color_button_background.colorpixel;
219 values[1] = line_width;
220 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
222 xcb_rectangle_t close = {y - w - (2 * line_width), 0, w + (2 * line_width), rect.height};
223 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
225 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border.colorpixel});
226 xcb_point_t points[] = {
227 {y - w - (2 * line_width), line_width / 2},
228 {y - (line_width / 2), line_width / 2},
229 {y - (line_width / 2), (rect.height - (line_width / 2)) - logical_px(2)},
230 {y - w - (2 * line_width), (rect.height - (line_width / 2)) - logical_px(2)},
231 {y - w - (2 * line_width), line_width / 2}};
232 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
235 set_font_colors(pixmap_gc, color_text, color_button_background);
236 /* the x term here seems to set left/right padding */
237 draw_text_ascii(close_button_label, pixmap, pixmap_gc,
238 y - w - line_width + w / 2 - logical_px(4),
239 logical_px(4) + logical_px(3),
240 rect.width - y + w + line_width - w / 2 + logical_px(4));
245 /* render custom buttons */
247 for (int c = 0; c < buttoncnt; c++) {
248 /* set w to the width of the label */
249 w = predict_text_width(buttons[c].label);
250 /* account for left/right padding, which seems to be set to 12px (total) below */
253 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_button_background.colorpixel});
254 close = (xcb_rectangle_t){y - w - (2 * line_width), logical_px(2), w + (2 * line_width), rect.height - logical_px(6)};
255 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
257 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border.colorpixel});
258 buttons[c].x = y - w - (2 * line_width);
259 buttons[c].width = w;
260 xcb_point_t points2[] = {
261 {y - w - (2 * line_width), (line_width / 2) + logical_px(2)},
262 {y - (line_width / 2), (line_width / 2) + logical_px(2)},
263 {y - (line_width / 2), (rect.height - logical_px(4) - (line_width / 2))},
264 {y - w - (2 * line_width), (rect.height - logical_px(4) - (line_width / 2))},
265 {y - w - (2 * line_width), (line_width / 2) + logical_px(2)}};
266 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
268 values[0] = color_text.colorpixel;
269 values[1] = color_button_background.colorpixel;
270 set_font_colors(pixmap_gc, color_text, color_button_background);
271 /* the x term seems to set left/right padding */
272 draw_text(buttons[c].label, pixmap, pixmap_gc, NULL,
273 y - w - line_width + logical_px(6),
274 logical_px(4) + logical_px(3),
275 rect.width - y + w + line_width - logical_px(6));
280 /* border line at the bottom */
281 line_width = logical_px(2);
282 values[0] = color_border_bottom.colorpixel;
283 values[1] = line_width;
284 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
285 xcb_point_t bottom[] = {
286 {0, rect.height - 0},
287 {rect.width, rect.height - 0}};
288 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
290 /* Copy the contents of the pixmap to the real window */
291 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
298 * Return the position and size the i3-nagbar window should use.
299 * This will be the primary output or a fallback if it cannot be determined.
301 static xcb_rectangle_t get_window_position(void) {
302 /* Default values if we cannot determine the primary output or its CRTC info. */
303 xcb_rectangle_t result = (xcb_rectangle_t){50, 50, 500, font.height + logical_px(8) + logical_px(8)};
305 xcb_randr_get_screen_resources_current_cookie_t rcookie = xcb_randr_get_screen_resources_current(conn, root);
306 xcb_randr_get_output_primary_cookie_t pcookie = xcb_randr_get_output_primary(conn, root);
308 xcb_randr_get_output_primary_reply_t *primary = NULL;
309 xcb_randr_get_screen_resources_current_reply_t *res = NULL;
311 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL) {
312 DLOG("Could not determine the primary output.\n");
316 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
320 xcb_randr_get_output_info_reply_t *output =
321 xcb_randr_get_output_info_reply(conn,
322 xcb_randr_get_output_info(conn, primary->output, res->config_timestamp),
324 if (output == NULL || output->crtc == XCB_NONE)
327 xcb_randr_get_crtc_info_reply_t *crtc =
328 xcb_randr_get_crtc_info_reply(conn,
329 xcb_randr_get_crtc_info(conn, output->crtc, res->config_timestamp),
334 DLOG("Found primary output on position x = %i / y = %i / w = %i / h = %i.\n",
335 crtc->x, crtc->y, crtc->width, crtc->height);
336 if (crtc->width == 0 || crtc->height == 0) {
337 DLOG("Primary output is not active, ignoring it.\n");
351 int main(int argc, char *argv[]) {
352 /* The following lines are a terribly horrible kludge. Because terminal
353 * emulators have different ways of interpreting the -e command line
354 * argument (some need -e "less /etc/fstab", others need -e less
355 * /etc/fstab), we need to write commands to a script and then just run
356 * that script. However, since on some machines, $XDG_RUNTIME_DIR and
357 * $TMPDIR are mounted with noexec, we cannot directly execute the script
360 * Initially, we tried to pass the command via the environment variable
361 * _I3_NAGBAR_CMD. But turns out that some terminal emulators such as
362 * xfce4-terminal run all windows from a single master process and only
363 * pass on the command (not the environment) to that master process.
365 * Therefore, we symlink i3-nagbar (which MUST reside on an executable
366 * filesystem) with a special name and run that symlink. When i3-nagbar
367 * recognizes it’s started as a binary ending in .nagbar_cmd, it strips off
368 * the .nagbar_cmd suffix and runs /bin/sh on argv[0]. That way, we can run
369 * a shell script on a noexec filesystem.
371 * From a security point of view, i3-nagbar is just an alias to /bin/sh in
372 * certain circumstances. This should not open any new security issues, I
375 const size_t argv0_len = strlen(argv[0]);
376 if (argv0_len > strlen(".nagbar_cmd") &&
377 strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) {
379 cmd = sstrdup(argv[0]);
380 *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0';
381 execl("/bin/sh", "/bin/sh", cmd, NULL);
382 err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd);
387 char *pattern = sstrdup("pango:monospace 8");
388 int o, option_index = 0;
389 enum { TYPE_ERROR = 0,
390 TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
392 static struct option long_options[] = {
393 {"version", no_argument, 0, 'v'},
394 {"font", required_argument, 0, 'f'},
395 {"button", required_argument, 0, 'b'},
396 {"help", no_argument, 0, 'h'},
397 {"message", required_argument, 0, 'm'},
398 {"type", required_argument, 0, 't'},
401 char *options_string = "b:f:m:t:vh";
403 prompt = i3string_from_utf8("Please do not run this program.");
405 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
408 printf("i3-nagbar " I3_VERSION "\n");
412 pattern = sstrdup(optarg);
415 i3string_free(prompt);
416 prompt = i3string_from_utf8(optarg);
419 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
422 printf("i3-nagbar " I3_VERSION "\n");
423 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
426 buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
427 buttons[buttoncnt].label = i3string_from_utf8(optarg);
428 buttons[buttoncnt].action = argv[optind];
429 printf("button with label *%s* and action *%s*\n",
430 i3string_as_utf8(buttons[buttoncnt].label),
431 buttons[buttoncnt].action);
433 printf("now %d buttons\n", buttoncnt);
441 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
442 xcb_connection_has_error(conn))
443 die("Cannot open display\n");
445 /* Place requests for the atoms we need as soon as possible */
446 #define xmacro(atom) \
447 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
448 #include "atoms.xmacro"
451 root_screen = xcb_aux_get_screen(conn, screens);
452 root = root_screen->root;
454 if (bar_type == TYPE_ERROR) {
455 /* Red theme for error messages */
456 color_button_background = draw_util_hex_to_color("#680a0a");
457 color_background = draw_util_hex_to_color("#900000");
458 color_text = draw_util_hex_to_color("#ffffff");
459 color_border = draw_util_hex_to_color("#d92424");
460 color_border_bottom = draw_util_hex_to_color("#470909");
462 /* Yellowish theme for warnings */
463 color_button_background = draw_util_hex_to_color("#ffc100");
464 color_background = draw_util_hex_to_color("#ffa8000");
465 color_text = draw_util_hex_to_color("#000000");
466 color_border = draw_util_hex_to_color("#ab7100");
467 color_border_bottom = draw_util_hex_to_color("#ab7100");
470 font = load_font(pattern, true);
473 xcb_rectangle_t win_pos = get_window_position();
476 xcb_cursor_context_t *cursor_ctx;
477 if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
478 cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
479 xcb_cursor_context_free(cursor_ctx);
481 cursor = xcb_generate_id(conn);
482 i3Font cursor_font = load_font("cursor", false);
483 xcb_create_glyph_cursor(
486 cursor_font.specific.xcb.id,
487 cursor_font.specific.xcb.id,
489 XCB_CURSOR_LEFT_PTR + 1,
491 65535, 65535, 65535);
494 /* Open an input window */
495 win = xcb_generate_id(conn);
499 XCB_COPY_FROM_PARENT,
500 win, /* the window id */
501 root, /* parent == root */
502 win_pos.x, win_pos.y, win_pos.width, win_pos.height, /* dimensions */
503 0, /* x11 border = 0, we draw our own */
504 XCB_WINDOW_CLASS_INPUT_OUTPUT,
505 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
506 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
508 0, /* back pixel: black */
509 XCB_EVENT_MASK_EXPOSURE |
510 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
511 XCB_EVENT_MASK_BUTTON_PRESS |
512 XCB_EVENT_MASK_BUTTON_RELEASE,
515 /* Map the window (make it visible) */
516 xcb_map_window(conn, win);
518 /* Setup NetWM atoms */
519 #define xmacro(name) \
521 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
523 die("Could not get atom " #name "\n"); \
525 A_##name = reply->atom; \
528 #include "atoms.xmacro"
532 xcb_change_property(conn,
533 XCB_PROP_MODE_REPLACE,
535 A__NET_WM_WINDOW_TYPE,
539 (unsigned char *)&A__NET_WM_WINDOW_TYPE_DOCK);
541 /* Reserve some space at the top of the screen */
547 uint32_t left_start_y;
549 uint32_t right_start_y;
550 uint32_t right_end_y;
551 uint32_t top_start_x;
553 uint32_t bottom_start_x;
554 uint32_t bottom_end_x;
555 } __attribute__((__packed__)) strut_partial;
556 memset(&strut_partial, 0, sizeof(strut_partial));
558 strut_partial.top = font.height + logical_px(6);
559 strut_partial.top_start_x = 0;
560 strut_partial.top_end_x = 800;
562 xcb_change_property(conn,
563 XCB_PROP_MODE_REPLACE,
565 A__NET_WM_STRUT_PARTIAL,
572 pixmap = xcb_generate_id(conn);
573 pixmap_gc = xcb_generate_id(conn);
574 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + logical_px(8));
575 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
577 /* Grab the keyboard to get all input */
580 xcb_generic_event_t *event;
581 while ((event = xcb_wait_for_event(conn)) != NULL) {
582 if (event->response_type == 0) {
583 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
587 /* Strip off the highest bit (set if the event is generated) */
588 int type = (event->response_type & 0x7F);
592 handle_expose(conn, (xcb_expose_event_t *)event);
595 case XCB_BUTTON_PRESS:
596 handle_button_press(conn, (xcb_button_press_event_t *)event);
599 case XCB_BUTTON_RELEASE:
600 handle_button_release(conn, (xcb_button_release_event_t *)event);
603 case XCB_CONFIGURE_NOTIFY: {
604 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t *)event;
605 rect = (xcb_rectangle_t){
608 configure_notify->width,
609 configure_notify->height};
611 /* Recreate the pixmap / gc */
612 xcb_free_pixmap(conn, pixmap);
613 xcb_free_gc(conn, pixmap_gc);
615 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
616 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);