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;
57 static xcb_window_t win;
61 static i3String *prompt;
63 static button_t btn_close;
64 static button_t *buttons;
67 /* Result of get_colorpixel() for the various colors. */
68 static color_t color_background; /* background of the bar */
69 static color_t color_button_background; /* background for buttons */
70 static color_t color_border; /* color of the button border */
71 static color_t color_border_bottom; /* color of the bottom border */
72 static color_t color_text; /* color of the text */
75 xcb_connection_t *conn;
76 xcb_screen_t *root_screen;
79 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
82 void verboselog(char *fmt, ...) {
86 vfprintf(stdout, fmt, args);
90 void errorlog(char *fmt, ...) {
94 vfprintf(stderr, fmt, args);
98 void debuglog(char *fmt, ...) {
102 * Starts the given application by passing it through a shell. We use double fork
103 * to avoid zombie processes. As the started application’s parent exits (immediately),
104 * the application is reparented to init (process-id 1), which correctly handles
105 * childs, so we don’t have to do it :-).
107 * The shell is determined by looking for the SHELL environment variable. If it
108 * does not exist, /bin/sh is used.
111 static void start_application(const char *command) {
112 printf("executing: %s\n", command);
117 /* This is the child */
118 execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
126 static button_t *get_button_at(int16_t x, int16_t y) {
127 for (int c = 0; c < buttoncnt; c++)
128 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
134 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
135 printf("button pressed on x = %d, y = %d\n",
136 event->event_x, event->event_y);
137 /* TODO: set a flag for the button, re-render */
141 * Called when the user releases the mouse button. Checks whether the
142 * coordinates are over a button and executes the appropriate action.
145 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
146 printf("button released on x = %d, y = %d\n",
147 event->event_x, event->event_y);
148 /* If the user hits the close button, we exit(0) */
149 if (event->event_x >= btn_close.x && event->event_x < btn_close.x + btn_close.width)
151 button_t *button = get_button_at(event->event_x, event->event_y);
155 /* We need to create a custom script containing our actual command
156 * since not every terminal emulator which is contained in
157 * i3-sensible-terminal supports -e with multiple arguments (and not
158 * all of them support -e with one quoted argument either).
160 * NB: The paths need to be unique, that is, don’t assume users close
161 * their nagbars at any point in time (and they still need to work).
163 char *script_path = get_process_filename("nagbar-cmd");
165 int fd = open(script_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
167 warn("Could not create temporary script to store the nagbar command");
170 FILE *script = fdopen(fd, "w");
171 if (script == NULL) {
172 warn("Could not fdopen() temporary script to store the nagbar command");
175 fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, button->action);
180 char *exe_path = get_exe_path(argv0);
181 sasprintf(&link_path, "%s.nagbar_cmd", script_path);
182 if (symlink(exe_path, link_path) == -1) {
183 err(EXIT_FAILURE, "Failed to symlink %s to %s", link_path, exe_path);
187 sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
188 printf("argv0 = %s\n", argv0);
189 printf("terminal_cmd = %s\n", terminal_cmd);
191 start_application(terminal_cmd);
198 /* TODO: unset flag, re-render */
202 * Draws a button and returns its width
205 static int button_draw(button_t *button, int position) {
206 int text_width = predict_text_width(button->label);
207 button->width = text_width + 2 * BTN_PADDING + 2 * BTN_BORDER;
208 button->x = position - button->width;
211 draw_util_rectangle(&bar, color_border,
212 position - button->width,
213 MSG_PADDING - BTN_PADDING - BTN_BORDER,
215 font.height + 2 * BTN_PADDING + 2 * BTN_BORDER);
216 /* draw background */
217 draw_util_rectangle(&bar, color_button_background,
218 position - button->width + BTN_BORDER,
219 MSG_PADDING - BTN_PADDING,
220 text_width + 2 * BTN_PADDING,
221 font.height + 2 * BTN_PADDING);
223 draw_util_text(button->label, &bar, color_text, color_button_background,
224 position - button->width + BTN_BORDER + BTN_PADDING,
227 return button->width;
231 * Handles expose events (redraws of the window) and rendering in general. Will
232 * be called from the code with event == NULL or from X with event != NULL.
235 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
236 /* draw background */
237 draw_util_clear_surface(&bar, color_background);
239 draw_util_text(prompt, &bar, color_text, color_background,
240 MSG_PADDING, MSG_PADDING,
241 bar.width - 2 * MSG_PADDING);
243 int position = bar.width - (MSG_PADDING - BTN_BORDER - BTN_PADDING);
245 /* render close button */
246 position -= button_draw(&btn_close, position);
247 position -= CLOSE_BTN_GAP;
249 /* render custom buttons */
250 for (int i = 0; i < buttoncnt; i++) {
252 position -= button_draw(&buttons[i], position);
255 /* border line at the bottom */
256 draw_util_rectangle(&bar, color_border_bottom, 0, bar.height - BAR_BORDER, bar.width, BAR_BORDER);
263 * Return the position and size the i3-nagbar window should use.
264 * This will be the primary output or a fallback if it cannot be determined.
266 static xcb_rectangle_t get_window_position(void) {
267 /* Default values if we cannot determine the primary output or its CRTC info. */
268 xcb_rectangle_t result = (xcb_rectangle_t){50, 50, 500, font.height + 2 * MSG_PADDING + BAR_BORDER};
270 xcb_randr_get_screen_resources_current_cookie_t rcookie = xcb_randr_get_screen_resources_current(conn, root);
271 xcb_randr_get_output_primary_cookie_t pcookie = xcb_randr_get_output_primary(conn, root);
273 xcb_randr_get_output_primary_reply_t *primary = NULL;
274 xcb_randr_get_screen_resources_current_reply_t *res = NULL;
276 if ((primary = xcb_randr_get_output_primary_reply(conn, pcookie, NULL)) == NULL) {
277 DLOG("Could not determine the primary output.\n");
281 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
285 xcb_randr_get_output_info_reply_t *output =
286 xcb_randr_get_output_info_reply(conn,
287 xcb_randr_get_output_info(conn, primary->output, res->config_timestamp),
289 if (output == NULL || output->crtc == XCB_NONE)
292 xcb_randr_get_crtc_info_reply_t *crtc =
293 xcb_randr_get_crtc_info_reply(conn,
294 xcb_randr_get_crtc_info(conn, output->crtc, res->config_timestamp),
299 DLOG("Found primary output on position x = %i / y = %i / w = %i / h = %i.\n",
300 crtc->x, crtc->y, crtc->width, crtc->height);
301 if (crtc->width == 0 || crtc->height == 0) {
302 DLOG("Primary output is not active, ignoring it.\n");
316 int main(int argc, char *argv[]) {
317 /* The following lines are a terribly horrible kludge. Because terminal
318 * emulators have different ways of interpreting the -e command line
319 * argument (some need -e "less /etc/fstab", others need -e less
320 * /etc/fstab), we need to write commands to a script and then just run
321 * that script. However, since on some machines, $XDG_RUNTIME_DIR and
322 * $TMPDIR are mounted with noexec, we cannot directly execute the script
325 * Initially, we tried to pass the command via the environment variable
326 * _I3_NAGBAR_CMD. But turns out that some terminal emulators such as
327 * xfce4-terminal run all windows from a single master process and only
328 * pass on the command (not the environment) to that master process.
330 * Therefore, we symlink i3-nagbar (which MUST reside on an executable
331 * filesystem) with a special name and run that symlink. When i3-nagbar
332 * recognizes it’s started as a binary ending in .nagbar_cmd, it strips off
333 * the .nagbar_cmd suffix and runs /bin/sh on argv[0]. That way, we can run
334 * a shell script on a noexec filesystem.
336 * From a security point of view, i3-nagbar is just an alias to /bin/sh in
337 * certain circumstances. This should not open any new security issues, I
340 const size_t argv0_len = strlen(argv[0]);
341 if (argv0_len > strlen(".nagbar_cmd") &&
342 strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) {
344 cmd = sstrdup(argv[0]);
345 *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0';
346 execl("/bin/sh", "/bin/sh", cmd, NULL);
347 err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd);
352 char *pattern = sstrdup("pango:monospace 8");
353 int o, option_index = 0;
354 enum { TYPE_ERROR = 0,
355 TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
357 static struct option long_options[] = {
358 {"version", no_argument, 0, 'v'},
359 {"font", required_argument, 0, 'f'},
360 {"button", required_argument, 0, 'b'},
361 {"help", no_argument, 0, 'h'},
362 {"message", required_argument, 0, 'm'},
363 {"type", required_argument, 0, 't'},
366 char *options_string = "b:f:m:t:vh";
368 prompt = i3string_from_utf8("Please do not run this program.");
370 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
373 printf("i3-nagbar " I3_VERSION "\n");
377 pattern = sstrdup(optarg);
380 i3string_free(prompt);
381 prompt = i3string_from_utf8(optarg);
384 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
387 printf("i3-nagbar " I3_VERSION "\n");
388 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
391 buttons = srealloc(buttons, sizeof(button_t) * (buttoncnt + 1));
392 buttons[buttoncnt].label = i3string_from_utf8(optarg);
393 buttons[buttoncnt].action = argv[optind];
394 printf("button with label *%s* and action *%s*\n",
395 i3string_as_utf8(buttons[buttoncnt].label),
396 buttons[buttoncnt].action);
398 printf("now %d buttons\n", buttoncnt);
405 btn_close.label = i3string_from_utf8("X");
408 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
409 xcb_connection_has_error(conn))
410 die("Cannot open display\n");
412 /* Place requests for the atoms we need as soon as possible */
413 #define xmacro(atom) \
414 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
415 #include "atoms.xmacro"
418 root_screen = xcb_aux_get_screen(conn, screens);
419 root = root_screen->root;
421 if (bar_type == TYPE_ERROR) {
422 /* Red theme for error messages */
423 color_button_background = draw_util_hex_to_color("#680a0a");
424 color_background = draw_util_hex_to_color("#900000");
425 color_text = draw_util_hex_to_color("#ffffff");
426 color_border = draw_util_hex_to_color("#d92424");
427 color_border_bottom = draw_util_hex_to_color("#470909");
429 /* Yellowish theme for warnings */
430 color_button_background = draw_util_hex_to_color("#ffc100");
431 color_background = draw_util_hex_to_color("#ffa8000");
432 color_text = draw_util_hex_to_color("#000000");
433 color_border = draw_util_hex_to_color("#ab7100");
434 color_border_bottom = draw_util_hex_to_color("#ab7100");
438 font = load_font(pattern, true);
441 #if defined(__OpenBSD__)
442 if (pledge("stdio rpath wpath cpath getpw proc exec", NULL) == -1)
443 err(EXIT_FAILURE, "pledge");
446 xcb_rectangle_t win_pos = get_window_position();
449 xcb_cursor_context_t *cursor_ctx;
450 if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) == 0) {
451 cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr");
452 xcb_cursor_context_free(cursor_ctx);
454 cursor = xcb_generate_id(conn);
455 i3Font cursor_font = load_font("cursor", false);
456 xcb_create_glyph_cursor(
459 cursor_font.specific.xcb.id,
460 cursor_font.specific.xcb.id,
462 XCB_CURSOR_LEFT_PTR + 1,
464 65535, 65535, 65535);
467 /* Open an input window */
468 win = xcb_generate_id(conn);
472 XCB_COPY_FROM_PARENT,
473 win, /* the window id */
474 root, /* parent == root */
475 win_pos.x, win_pos.y, win_pos.width, win_pos.height, /* dimensions */
476 0, /* x11 border = 0, we draw our own */
477 XCB_WINDOW_CLASS_INPUT_OUTPUT,
478 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
479 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
481 0, /* back pixel: black */
482 XCB_EVENT_MASK_EXPOSURE |
483 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
484 XCB_EVENT_MASK_BUTTON_PRESS |
485 XCB_EVENT_MASK_BUTTON_RELEASE,
488 /* Map the window (make it visible) */
489 xcb_map_window(conn, win);
491 /* Setup NetWM atoms */
492 #define xmacro(name) \
494 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
496 die("Could not get atom " #name "\n"); \
498 A_##name = reply->atom; \
501 #include "atoms.xmacro"
505 xcb_change_property(conn,
506 XCB_PROP_MODE_REPLACE,
508 A__NET_WM_WINDOW_TYPE,
512 (unsigned char *)&A__NET_WM_WINDOW_TYPE_DOCK);
514 /* Reserve some space at the top of the screen */
520 uint32_t left_start_y;
522 uint32_t right_start_y;
523 uint32_t right_end_y;
524 uint32_t top_start_x;
526 uint32_t bottom_start_x;
527 uint32_t bottom_end_x;
528 } __attribute__((__packed__)) strut_partial;
529 memset(&strut_partial, 0, sizeof(strut_partial));
531 strut_partial.top = font.height + logical_px(6);
532 strut_partial.top_start_x = 0;
533 strut_partial.top_end_x = 800;
535 xcb_change_property(conn,
536 XCB_PROP_MODE_REPLACE,
538 A__NET_WM_STRUT_PARTIAL,
544 /* Initialize the drawable bar */
545 draw_util_surface_init(conn, &bar, win, get_visualtype(root_screen), win_pos.width, win_pos.height);
547 /* Grab the keyboard to get all input */
550 xcb_generic_event_t *event;
551 while ((event = xcb_wait_for_event(conn)) != NULL) {
552 if (event->response_type == 0) {
553 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
557 /* Strip off the highest bit (set if the event is generated) */
558 int type = (event->response_type & 0x7F);
562 if (((xcb_expose_event_t *)event)->count == 0) {
563 handle_expose(conn, (xcb_expose_event_t *)event);
568 case XCB_BUTTON_PRESS:
569 handle_button_press(conn, (xcb_button_press_event_t *)event);
572 case XCB_BUTTON_RELEASE:
573 handle_button_release(conn, (xcb_button_release_event_t *)event);
576 case XCB_CONFIGURE_NOTIFY: {
577 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t *)event;
578 draw_util_surface_set_size(&bar, configure_notify->width, configure_notify->height);
587 draw_util_surface_free(conn, &bar);