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 #define MSG_PADDING logical_px(8)
42 #define BTN_PADDING logical_px(3)
43 #define BTN_BORDER logical_px(3)
44 #define BTN_GAP logical_px(20)
45 #define CLOSE_BTN_GAP logical_px(15)
46 #define BAR_BORDER logical_px(2)
48 static char *argv0 = NULL;
53 bool exec_in_terminal;
58 static xcb_window_t win;
62 static i3String *prompt;
64 static button_t btn_close;
65 static button_t *buttons;
68 /* Result of get_colorpixel() for the various colors. */
69 static color_t color_background; /* background of the bar */
70 static color_t color_button_background; /* background for buttons */
71 static color_t color_border; /* color of the button border */
72 static color_t color_border_bottom; /* color of the bottom border */
73 static color_t color_text; /* color of the text */
76 xcb_connection_t *conn;
77 xcb_screen_t *root_screen;
80 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
83 void verboselog(char *fmt, ...) {
87 vfprintf(stdout, fmt, args);
91 void errorlog(char *fmt, ...) {
95 vfprintf(stderr, fmt, args);
99 void debuglog(char *fmt, ...) {
103 * Starts the given application by passing it through a shell. We use double fork
104 * to avoid zombie processes. As the started application’s parent exits (immediately),
105 * the application is reparented to init (process-id 1), which correctly handles
106 * childs, so we don’t have to do it :-).
108 * The shell is determined by looking for the SHELL environment variable. If it
109 * does not exist, /bin/sh is used.
112 static void start_application(const char *command) {
113 printf("executing: %s\n", command);
118 /* This is the child */
119 execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
127 static void execute_in_terminal(const char *command) {
128 /* We need to create a custom script containing our actual command
129 * since not every terminal emulator which is contained in
130 * i3-sensible-terminal supports -e with multiple arguments (and not
131 * all of them support -e with one quoted argument either).
133 * NB: The paths need to be unique, that is, don’t assume users close
134 * their nagbars at any point in time (and they still need to work).
136 char *script_path = get_process_filename("nagbar-cmd");
138 int fd = open(script_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
140 warn("Could not create temporary script to store the nagbar command");
143 FILE *script = fdopen(fd, "w");
144 if (script == NULL) {
145 warn("Could not fdopen() temporary script to store the nagbar command");
148 fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, command);
153 char *exe_path = get_exe_path(argv0);
154 sasprintf(&link_path, "%s.nagbar_cmd", script_path);
155 if (symlink(exe_path, link_path) == -1) {
156 err(EXIT_FAILURE, "Failed to symlink %s to %s", link_path, exe_path);
160 sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
161 printf("argv0 = %s\n", argv0);
162 printf("terminal_cmd = %s\n", terminal_cmd);
164 start_application(terminal_cmd);
172 static button_t *get_button_at(int16_t x, int16_t y) {
173 for (int c = 0; c < buttoncnt; c++)
174 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
180 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
181 printf("button pressed on x = %d, y = %d\n",
182 event->event_x, event->event_y);
183 /* TODO: set a flag for the button, re-render */
187 * Called when the user releases the mouse button. Checks whether the
188 * coordinates are over a button and executes the appropriate action.
191 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
192 printf("button released on x = %d, y = %d\n",
193 event->event_x, event->event_y);
194 /* If the user hits the close button, we exit(0) */
195 if (event->event_x >= btn_close.x && event->event_x < btn_close.x + btn_close.width)
197 button_t *button = get_button_at(event->event_x, event->event_y);
202 if (button->exec_in_terminal) {
203 execute_in_terminal(button->action);
205 start_application(button->action);
208 /* TODO: unset flag, re-render */
212 * Draws a button and returns its width
215 static int button_draw(button_t *button, int position) {
216 int text_width = predict_text_width(button->label);
217 button->width = text_width + 2 * BTN_PADDING + 2 * BTN_BORDER;
218 button->x = position - button->width;
221 draw_util_rectangle(&bar, color_border,
222 position - button->width,
223 MSG_PADDING - BTN_PADDING - BTN_BORDER,
225 font.height + 2 * BTN_PADDING + 2 * BTN_BORDER);
226 /* draw background */
227 draw_util_rectangle(&bar, color_button_background,
228 position - button->width + BTN_BORDER,
229 MSG_PADDING - BTN_PADDING,
230 text_width + 2 * BTN_PADDING,
231 font.height + 2 * BTN_PADDING);
233 draw_util_text(button->label, &bar, color_text, color_button_background,
234 position - button->width + BTN_BORDER + BTN_PADDING,
237 return button->width;
241 * Handles expose events (redraws of the window) and rendering in general. Will
242 * be called from the code with event == NULL or from X with event != NULL.
245 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
246 /* draw background */
247 draw_util_clear_surface(&bar, color_background);
249 draw_util_text(prompt, &bar, color_text, color_background,
250 MSG_PADDING, MSG_PADDING,
251 bar.width - 2 * MSG_PADDING);
253 int position = bar.width - (MSG_PADDING - BTN_BORDER - BTN_PADDING);
255 /* render close button */
256 position -= button_draw(&btn_close, position);
257 position -= CLOSE_BTN_GAP;
259 /* render custom buttons */
260 for (int i = 0; i < buttoncnt; i++) {
262 position -= button_draw(&buttons[i], position);
265 /* border line at the bottom */
266 draw_util_rectangle(&bar, color_border_bottom, 0, bar.height - BAR_BORDER, bar.width, BAR_BORDER);
273 * Return the position and size the i3-nagbar window should use.
274 * This will be the primary output or a fallback if it cannot be determined.
276 static xcb_rectangle_t get_window_position(void) {
277 /* Default values if we cannot determine the primary output or its CRTC info. */
278 xcb_rectangle_t result = (xcb_rectangle_t){50, 50, 500, font.height + 2 * MSG_PADDING + BAR_BORDER};
280 xcb_randr_get_screen_resources_current_cookie_t rcookie = xcb_randr_get_screen_resources_current(conn, root);
281 xcb_randr_get_output_primary_cookie_t pcookie = xcb_randr_get_output_primary(conn, root);
283 xcb_randr_get_output_primary_reply_t *primary = NULL;
284 xcb_randr_get_screen_resources_current_reply_t *res = NULL;
286 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL) {
287 DLOG("Could not determine the primary output.\n");
291 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
295 xcb_randr_get_output_info_reply_t *output =
296 xcb_randr_get_output_info_reply(conn,
297 xcb_randr_get_output_info(conn, primary->output, res->config_timestamp),
299 if (output == NULL || output->crtc == XCB_NONE)
302 xcb_randr_get_crtc_info_reply_t *crtc =
303 xcb_randr_get_crtc_info_reply(conn,
304 xcb_randr_get_crtc_info(conn, output->crtc, res->config_timestamp),
309 DLOG("Found primary output on position x = %i / y = %i / w = %i / h = %i.\n",
310 crtc->x, crtc->y, crtc->width, crtc->height);
311 if (crtc->width == 0 || crtc->height == 0) {
312 DLOG("Primary output is not active, ignoring it.\n");
326 int main(int argc, char *argv[]) {
327 /* The following lines are a terribly horrible kludge. Because terminal
328 * emulators have different ways of interpreting the -e command line
329 * argument (some need -e "less /etc/fstab", others need -e less
330 * /etc/fstab), we need to write commands to a script and then just run
331 * that script. However, since on some machines, $XDG_RUNTIME_DIR and
332 * $TMPDIR are mounted with noexec, we cannot directly execute the script
335 * Initially, we tried to pass the command via the environment variable
336 * _I3_NAGBAR_CMD. But turns out that some terminal emulators such as
337 * xfce4-terminal run all windows from a single master process and only
338 * pass on the command (not the environment) to that master process.
340 * Therefore, we symlink i3-nagbar (which MUST reside on an executable
341 * filesystem) with a special name and run that symlink. When i3-nagbar
342 * recognizes it’s started as a binary ending in .nagbar_cmd, it strips off
343 * the .nagbar_cmd suffix and runs /bin/sh on argv[0]. That way, we can run
344 * a shell script on a noexec filesystem.
346 * From a security point of view, i3-nagbar is just an alias to /bin/sh in
347 * certain circumstances. This should not open any new security issues, I
350 const size_t argv0_len = strlen(argv[0]);
351 if (argv0_len > strlen(".nagbar_cmd") &&
352 strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) {
354 cmd = sstrdup(argv[0]);
355 *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0';
356 execl("/bin/sh", "/bin/sh", cmd, NULL);
357 err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd);
362 char *pattern = sstrdup("pango:monospace 8");
363 int o, option_index = 0;
364 enum { TYPE_ERROR = 0,
365 TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
367 static struct option long_options[] = {
368 {"version", no_argument, 0, 'v'},
369 {"font", required_argument, 0, 'f'},
370 {"button", required_argument, 0, 'b'},
371 {"button-sh", required_argument, 0, 'B'},
372 {"help", no_argument, 0, 'h'},
373 {"message", required_argument, 0, 'm'},
374 {"type", required_argument, 0, 't'},
377 char *options_string = "B:b:f:m:t:vh";
379 prompt = i3string_from_utf8("Please do not run this program.");
381 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
384 printf("i3-nagbar " I3_VERSION "\n");
388 pattern = sstrdup(optarg);
391 i3string_free(prompt);
392 prompt = i3string_from_utf8(optarg);
395 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
398 printf("i3-nagbar " I3_VERSION "\n");
399 printf("i3-nagbar [-m <message>] [-b <button> <terminal-action>] "
400 "[-B <button> <shell-action> [-t warning|error] [-f <font>] [-v]\n");
405 buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
406 buttons[buttoncnt].label = i3string_from_utf8(optarg);
407 buttons[buttoncnt].action = argv[optind];
409 buttons[buttoncnt].exec_in_terminal = true;
410 printf("button with label *%s* and terminal action *%s*\n",
411 i3string_as_utf8(buttons[buttoncnt].label),
412 buttons[buttoncnt].action);
414 buttons[buttoncnt].exec_in_terminal = false;
415 printf("button with label *%s* and shell action *%s*\n",
416 i3string_as_utf8(buttons[buttoncnt].label),
417 buttons[buttoncnt].action);
420 printf("now %d buttons\n", buttoncnt);
427 btn_close.label = i3string_from_utf8("X");
430 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
431 xcb_connection_has_error(conn))
432 die("Cannot open display\n");
434 /* Place requests for the atoms we need as soon as possible */
435 #define xmacro(atom) \
436 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
437 #include "atoms.xmacro"
440 root_screen = xcb_aux_get_screen(conn, screens);
441 root = root_screen->root;
443 if (bar_type == TYPE_ERROR) {
444 /* Red theme for error messages */
445 color_button_background = draw_util_hex_to_color("#680a0a");
446 color_background = draw_util_hex_to_color("#900000");
447 color_text = draw_util_hex_to_color("#ffffff");
448 color_border = draw_util_hex_to_color("#d92424");
449 color_border_bottom = draw_util_hex_to_color("#470909");
451 /* Yellowish theme for warnings */
452 color_button_background = draw_util_hex_to_color("#ffc100");
453 color_background = draw_util_hex_to_color("#ffa8000");
454 color_text = draw_util_hex_to_color("#000000");
455 color_border = draw_util_hex_to_color("#ab7100");
456 color_border_bottom = draw_util_hex_to_color("#ab7100");
460 font = load_font(pattern, true);
463 #if defined(__OpenBSD__)
464 if (pledge("stdio rpath wpath cpath getpw proc exec", NULL) == -1)
465 err(EXIT_FAILURE, "pledge");
468 xcb_rectangle_t win_pos = get_window_position();
471 xcb_cursor_context_t *cursor_ctx;
472 if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
473 cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
474 xcb_cursor_context_free(cursor_ctx);
476 cursor = xcb_generate_id(conn);
477 i3Font cursor_font = load_font("cursor", false);
478 xcb_create_glyph_cursor(
481 cursor_font.specific.xcb.id,
482 cursor_font.specific.xcb.id,
484 XCB_CURSOR_LEFT_PTR + 1,
486 65535, 65535, 65535);
489 /* Open an input window */
490 win = xcb_generate_id(conn);
494 XCB_COPY_FROM_PARENT,
495 win, /* the window id */
496 root, /* parent == root */
497 win_pos.x, win_pos.y, win_pos.width, win_pos.height, /* dimensions */
498 0, /* x11 border = 0, we draw our own */
499 XCB_WINDOW_CLASS_INPUT_OUTPUT,
500 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
501 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
503 0, /* back pixel: black */
504 XCB_EVENT_MASK_EXPOSURE |
505 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
506 XCB_EVENT_MASK_BUTTON_PRESS |
507 XCB_EVENT_MASK_BUTTON_RELEASE,
510 /* Map the window (make it visible) */
511 xcb_map_window(conn, win);
513 /* Setup NetWM atoms */
514 #define xmacro(name) \
516 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
518 die("Could not get atom " #name "\n"); \
520 A_##name = reply->atom; \
523 #include "atoms.xmacro"
527 xcb_change_property(conn,
528 XCB_PROP_MODE_REPLACE,
530 A__NET_WM_WINDOW_TYPE,
534 (unsigned char *)&A__NET_WM_WINDOW_TYPE_DOCK);
536 /* Reserve some space at the top of the screen */
542 uint32_t left_start_y;
544 uint32_t right_start_y;
545 uint32_t right_end_y;
546 uint32_t top_start_x;
548 uint32_t bottom_start_x;
549 uint32_t bottom_end_x;
550 } __attribute__((__packed__)) strut_partial;
551 memset(&strut_partial, 0, sizeof(strut_partial));
553 strut_partial.top = font.height + logical_px(6);
554 strut_partial.top_start_x = 0;
555 strut_partial.top_end_x = 800;
557 xcb_change_property(conn,
558 XCB_PROP_MODE_REPLACE,
560 A__NET_WM_STRUT_PARTIAL,
566 /* Initialize the drawable bar */
567 draw_util_surface_init(conn, &bar, win, get_visualtype(root_screen), win_pos.width, win_pos.height);
569 /* Grab the keyboard to get all input */
572 xcb_generic_event_t *event;
573 while ((event = xcb_wait_for_event(conn)) != NULL) {
574 if (event->response_type == 0) {
575 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
579 /* Strip off the highest bit (set if the event is generated) */
580 int type = (event->response_type & 0x7F);
584 if (((xcb_expose_event_t *)event)->count == 0) {
585 handle_expose(conn, (xcb_expose_event_t *)event);
590 case XCB_BUTTON_PRESS:
591 handle_button_press(conn, (xcb_button_press_event_t *)event);
594 case XCB_BUTTON_RELEASE:
595 handle_button_release(conn, (xcb_button_release_event_t *)event);
598 case XCB_CONFIGURE_NOTIFY: {
599 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t *)event;
600 draw_util_surface_set_size(&bar, configure_notify->width, configure_notify->height);
609 draw_util_surface_free(conn, &bar);