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.
14 #include <sys/types.h>
30 #include <xcb/xcb_aux.h>
31 #include <xcb/xcb_event.h>
32 #include <xcb/randr.h>
33 #include <xcb/xcb_cursor.h>
35 #include "i3-nagbar.h"
37 /** This is the equivalent of XC_left_ptr. I’m not sure why xcb doesn’t have a
38 * constant for that. */
39 #define XCB_CURSOR_LEFT_PTR 68
41 static char *argv0 = NULL;
50 static xcb_window_t win;
51 static xcb_pixmap_t pixmap;
52 static xcb_gcontext_t pixmap_gc;
53 static xcb_rectangle_t rect = {0, 0, 600, 20};
55 static i3String *prompt;
56 static button_t *buttons;
59 /* Result of get_colorpixel() for the various colors. */
60 static color_t color_background; /* background of the bar */
61 static color_t color_button_background; /* background for buttons */
62 static color_t color_border; /* color of the button border */
63 static color_t color_border_bottom; /* color of the bottom border */
64 static color_t color_text; /* color of the text */
67 xcb_connection_t *conn;
68 xcb_screen_t *root_screen;
71 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
74 void verboselog(char *fmt, ...) {
78 vfprintf(stdout, fmt, args);
82 void errorlog(char *fmt, ...) {
86 vfprintf(stderr, fmt, args);
90 void debuglog(char *fmt, ...) {
94 * Starts the given application by passing it through a shell. We use double fork
95 * to avoid zombie processes. As the started application’s parent exits (immediately),
96 * the application is reparented to init (process-id 1), which correctly handles
97 * childs, so we don’t have to do it :-).
99 * The shell is determined by looking for the SHELL environment variable. If it
100 * does not exist, /bin/sh is used.
103 static void start_application(const char *command) {
104 printf("executing: %s\n", command);
109 /* This is the child */
110 execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
118 static button_t *get_button_at(int16_t x, int16_t y) {
119 for (int c = 0; c < buttoncnt; c++)
120 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
126 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
127 printf("button pressed on x = %d, y = %d\n",
128 event->event_x, event->event_y);
129 /* TODO: set a flag for the button, re-render */
133 * Called when the user releases the mouse button. Checks whether the
134 * coordinates are over a button and executes the appropriate action.
137 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
138 printf("button released on x = %d, y = %d\n",
139 event->event_x, event->event_y);
140 /* If the user hits the close button, we exit(0) */
141 if (event->event_x >= (rect.width - logical_px(32)))
143 button_t *button = get_button_at(event->event_x, event->event_y);
147 /* We need to create a custom script containing our actual command
148 * since not every terminal emulator which is contained in
149 * i3-sensible-terminal supports -e with multiple arguments (and not
150 * all of them support -e with one quoted argument either).
152 * NB: The paths need to be unique, that is, don’t assume users close
153 * their nagbars at any point in time (and they still need to work).
155 char *script_path = get_process_filename("nagbar-cmd");
157 int fd = open(script_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
159 warn("Could not create temporary script to store the nagbar command");
162 FILE *script = fdopen(fd, "w");
163 if (script == NULL) {
164 warn("Could not fdopen() temporary script to store the nagbar command");
167 fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, button->action);
172 char *exe_path = get_exe_path(argv0);
173 sasprintf(&link_path, "%s.nagbar_cmd", script_path);
174 if (symlink(exe_path, link_path) == -1) {
175 err(EXIT_FAILURE, "Failed to symlink %s to %s", link_path, exe_path);
179 sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
180 printf("argv0 = %s\n", argv0);
181 printf("terminal_cmd = %s\n", terminal_cmd);
183 start_application(terminal_cmd);
190 /* TODO: unset flag, re-render */
194 * Handles expose events (redraws of the window) and rendering in general. Will
195 * be called from the code with event == NULL or from X with event != NULL.
198 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
199 /* re-draw the background */
200 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_background.colorpixel});
201 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
203 /* restore font color */
204 set_font_colors(pixmap_gc, color_text, color_background);
205 draw_text(prompt, pixmap, pixmap_gc, NULL,
206 logical_px(4) + logical_px(4),
207 logical_px(4) + logical_px(4),
208 rect.width - logical_px(4) - logical_px(4));
210 /* render close button */
211 const char *close_button_label = "X";
212 int line_width = logical_px(4);
213 /* set width to the width of the label */
214 int w = predict_text_width(i3string_from_utf8(close_button_label));
215 /* account for left/right padding, which seems to be set to 8px (total) below */
219 values[0] = color_button_background.colorpixel;
220 values[1] = line_width;
221 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
223 xcb_rectangle_t close = {y - w - (2 * line_width), 0, w + (2 * line_width), rect.height};
224 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
226 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border.colorpixel});
227 xcb_point_t points[] = {
228 {y - w - (2 * line_width), line_width / 2},
229 {y - (line_width / 2), line_width / 2},
230 {y - (line_width / 2), (rect.height - (line_width / 2)) - logical_px(2)},
231 {y - w - (2 * line_width), (rect.height - (line_width / 2)) - logical_px(2)},
232 {y - w - (2 * line_width), line_width / 2}};
233 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
236 set_font_colors(pixmap_gc, color_text, color_button_background);
237 /* the x term here seems to set left/right padding */
238 draw_text_ascii(close_button_label, pixmap, pixmap_gc,
239 y - w - line_width + w / 2 - logical_px(4),
240 logical_px(4) + logical_px(3),
241 rect.width - y + w + line_width - w / 2 + logical_px(4));
246 /* render custom buttons */
248 for (int c = 0; c < buttoncnt; c++) {
249 /* set w to the width of the label */
250 w = predict_text_width(buttons[c].label);
251 /* account for left/right padding, which seems to be set to 12px (total) below */
254 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_button_background.colorpixel});
255 close = (xcb_rectangle_t){y - w - (2 * line_width), logical_px(2), w + (2 * line_width), rect.height - logical_px(6)};
256 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
258 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border.colorpixel});
259 buttons[c].x = y - w - (2 * line_width);
260 buttons[c].width = w;
261 xcb_point_t points2[] = {
262 {y - w - (2 * line_width), (line_width / 2) + logical_px(2)},
263 {y - (line_width / 2), (line_width / 2) + logical_px(2)},
264 {y - (line_width / 2), (rect.height - logical_px(4) - (line_width / 2))},
265 {y - w - (2 * line_width), (rect.height - logical_px(4) - (line_width / 2))},
266 {y - w - (2 * line_width), (line_width / 2) + logical_px(2)}};
267 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
269 values[0] = color_text.colorpixel;
270 values[1] = color_button_background.colorpixel;
271 set_font_colors(pixmap_gc, color_text, color_button_background);
272 /* the x term seems to set left/right padding */
273 draw_text(buttons[c].label, pixmap, pixmap_gc, NULL,
274 y - w - line_width + logical_px(6),
275 logical_px(4) + logical_px(3),
276 rect.width - y + w + line_width - logical_px(6));
281 /* border line at the bottom */
282 line_width = logical_px(2);
283 values[0] = color_border_bottom.colorpixel;
284 values[1] = line_width;
285 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
286 xcb_point_t bottom[] = {
287 {0, rect.height - 0},
288 {rect.width, rect.height - 0}};
289 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
291 /* Copy the contents of the pixmap to the real window */
292 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
299 * Return the position and size the i3-nagbar window should use.
300 * This will be the primary output or a fallback if it cannot be determined.
302 static xcb_rectangle_t get_window_position(void) {
303 /* Default values if we cannot determine the primary output or its CRTC info. */
304 xcb_rectangle_t result = (xcb_rectangle_t){50, 50, 500, font.height + logical_px(8) + logical_px(8)};
306 xcb_randr_get_screen_resources_current_cookie_t rcookie = xcb_randr_get_screen_resources_current(conn, root);
307 xcb_randr_get_output_primary_cookie_t pcookie = xcb_randr_get_output_primary(conn, root);
309 xcb_randr_get_output_primary_reply_t *primary = NULL;
310 xcb_randr_get_screen_resources_current_reply_t *res = NULL;
312 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL) {
313 DLOG("Could not determine the primary output.\n");
317 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
321 xcb_randr_get_output_info_reply_t *output =
322 xcb_randr_get_output_info_reply(conn,
323 xcb_randr_get_output_info(conn, primary->output, res->config_timestamp),
325 if (output == NULL || output->crtc == XCB_NONE)
328 xcb_randr_get_crtc_info_reply_t *crtc =
329 xcb_randr_get_crtc_info_reply(conn,
330 xcb_randr_get_crtc_info(conn, output->crtc, res->config_timestamp),
335 DLOG("Found primary output on position x = %i / y = %i / w = %i / h = %i.\n",
336 crtc->x, crtc->y, crtc->width, crtc->height);
337 if (crtc->width == 0 || crtc->height == 0) {
338 DLOG("Primary output is not active, ignoring it.\n");
352 int main(int argc, char *argv[]) {
353 /* The following lines are a terribly horrible kludge. Because terminal
354 * emulators have different ways of interpreting the -e command line
355 * argument (some need -e "less /etc/fstab", others need -e less
356 * /etc/fstab), we need to write commands to a script and then just run
357 * that script. However, since on some machines, $XDG_RUNTIME_DIR and
358 * $TMPDIR are mounted with noexec, we cannot directly execute the script
361 * Initially, we tried to pass the command via the environment variable
362 * _I3_NAGBAR_CMD. But turns out that some terminal emulators such as
363 * xfce4-terminal run all windows from a single master process and only
364 * pass on the command (not the environment) to that master process.
366 * Therefore, we symlink i3-nagbar (which MUST reside on an executable
367 * filesystem) with a special name and run that symlink. When i3-nagbar
368 * recognizes it’s started as a binary ending in .nagbar_cmd, it strips off
369 * the .nagbar_cmd suffix and runs /bin/sh on argv[0]. That way, we can run
370 * a shell script on a noexec filesystem.
372 * From a security point of view, i3-nagbar is just an alias to /bin/sh in
373 * certain circumstances. This should not open any new security issues, I
376 const size_t argv0_len = strlen(argv[0]);
377 if (argv0_len > strlen(".nagbar_cmd") &&
378 strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) {
380 cmd = sstrdup(argv[0]);
381 *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0';
382 execl("/bin/sh", "/bin/sh", cmd, NULL);
383 err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd);
388 char *pattern = sstrdup("pango:monospace 8");
389 int o, option_index = 0;
390 enum { TYPE_ERROR = 0,
391 TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
393 static struct option long_options[] = {
394 {"version", no_argument, 0, 'v'},
395 {"font", required_argument, 0, 'f'},
396 {"button", required_argument, 0, 'b'},
397 {"help", no_argument, 0, 'h'},
398 {"message", required_argument, 0, 'm'},
399 {"type", required_argument, 0, 't'},
402 char *options_string = "b:f:m:t:vh";
404 prompt = i3string_from_utf8("Please do not run this program.");
406 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
409 printf("i3-nagbar " I3_VERSION "\n");
413 pattern = sstrdup(optarg);
416 i3string_free(prompt);
417 prompt = i3string_from_utf8(optarg);
420 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
423 printf("i3-nagbar " I3_VERSION "\n");
424 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
427 buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
428 buttons[buttoncnt].label = i3string_from_utf8(optarg);
429 buttons[buttoncnt].action = argv[optind];
430 printf("button with label *%s* and action *%s*\n",
431 i3string_as_utf8(buttons[buttoncnt].label),
432 buttons[buttoncnt].action);
434 printf("now %d buttons\n", buttoncnt);
442 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
443 xcb_connection_has_error(conn))
444 die("Cannot open display\n");
446 /* Place requests for the atoms we need as soon as possible */
447 #define xmacro(atom) \
448 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
449 #include "atoms.xmacro"
452 root_screen = xcb_aux_get_screen(conn, screens);
453 root = root_screen->root;
455 if (bar_type == TYPE_ERROR) {
456 /* Red theme for error messages */
457 color_button_background = draw_util_hex_to_color("#680a0a");
458 color_background = draw_util_hex_to_color("#900000");
459 color_text = draw_util_hex_to_color("#ffffff");
460 color_border = draw_util_hex_to_color("#d92424");
461 color_border_bottom = draw_util_hex_to_color("#470909");
463 /* Yellowish theme for warnings */
464 color_button_background = draw_util_hex_to_color("#ffc100");
465 color_background = draw_util_hex_to_color("#ffa8000");
466 color_text = draw_util_hex_to_color("#000000");
467 color_border = draw_util_hex_to_color("#ab7100");
468 color_border_bottom = draw_util_hex_to_color("#ab7100");
472 font = load_font(pattern, true);
475 #if defined(__OpenBSD__)
476 if (pledge("stdio rpath wpath cpath getpw proc exec", NULL) == -1)
477 err(EXIT_FAILURE, "pledge");
480 xcb_rectangle_t win_pos = get_window_position();
483 xcb_cursor_context_t *cursor_ctx;
484 if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
485 cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
486 xcb_cursor_context_free(cursor_ctx);
488 cursor = xcb_generate_id(conn);
489 i3Font cursor_font = load_font("cursor", false);
490 xcb_create_glyph_cursor(
493 cursor_font.specific.xcb.id,
494 cursor_font.specific.xcb.id,
496 XCB_CURSOR_LEFT_PTR + 1,
498 65535, 65535, 65535);
501 /* Open an input window */
502 win = xcb_generate_id(conn);
506 XCB_COPY_FROM_PARENT,
507 win, /* the window id */
508 root, /* parent == root */
509 win_pos.x, win_pos.y, win_pos.width, win_pos.height, /* dimensions */
510 0, /* x11 border = 0, we draw our own */
511 XCB_WINDOW_CLASS_INPUT_OUTPUT,
512 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
513 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
515 0, /* back pixel: black */
516 XCB_EVENT_MASK_EXPOSURE |
517 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
518 XCB_EVENT_MASK_BUTTON_PRESS |
519 XCB_EVENT_MASK_BUTTON_RELEASE,
522 /* Map the window (make it visible) */
523 xcb_map_window(conn, win);
525 /* Setup NetWM atoms */
526 #define xmacro(name) \
528 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
530 die("Could not get atom " #name "\n"); \
532 A_##name = reply->atom; \
535 #include "atoms.xmacro"
539 xcb_change_property(conn,
540 XCB_PROP_MODE_REPLACE,
542 A__NET_WM_WINDOW_TYPE,
546 (unsigned char *)&A__NET_WM_WINDOW_TYPE_DOCK);
548 /* Reserve some space at the top of the screen */
554 uint32_t left_start_y;
556 uint32_t right_start_y;
557 uint32_t right_end_y;
558 uint32_t top_start_x;
560 uint32_t bottom_start_x;
561 uint32_t bottom_end_x;
562 } __attribute__((__packed__)) strut_partial;
563 memset(&strut_partial, 0, sizeof(strut_partial));
565 strut_partial.top = font.height + logical_px(6);
566 strut_partial.top_start_x = 0;
567 strut_partial.top_end_x = 800;
569 xcb_change_property(conn,
570 XCB_PROP_MODE_REPLACE,
572 A__NET_WM_STRUT_PARTIAL,
579 pixmap = xcb_generate_id(conn);
580 pixmap_gc = xcb_generate_id(conn);
581 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + logical_px(8));
582 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
584 /* Grab the keyboard to get all input */
587 xcb_generic_event_t *event;
588 while ((event = xcb_wait_for_event(conn)) != NULL) {
589 if (event->response_type == 0) {
590 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
594 /* Strip off the highest bit (set if the event is generated) */
595 int type = (event->response_type & 0x7F);
599 if (((xcb_expose_event_t *)event)->count == 0) {
600 handle_expose(conn, (xcb_expose_event_t *)event);
605 case XCB_BUTTON_PRESS:
606 handle_button_press(conn, (xcb_button_press_event_t *)event);
609 case XCB_BUTTON_RELEASE:
610 handle_button_release(conn, (xcb_button_release_event_t *)event);
613 case XCB_CONFIGURE_NOTIFY: {
614 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t *)event;
615 rect = (xcb_rectangle_t){
618 configure_notify->width,
619 configure_notify->height};
621 /* Recreate the pixmap / gc */
622 xcb_free_pixmap(conn, pixmap);
623 xcb_free_gc(conn, pixmap_gc);
625 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
626 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);