2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2013 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>
32 #include "i3-nagbar.h"
34 static char *argv0 = NULL;
43 static xcb_window_t win;
44 static xcb_pixmap_t pixmap;
45 static xcb_gcontext_t pixmap_gc;
46 static xcb_rectangle_t rect = {0, 0, 600, 20};
48 static i3String *prompt;
49 static button_t *buttons;
52 /* Result of get_colorpixel() for the various colors. */
53 static uint32_t color_background; /* background of the bar */
54 static uint32_t color_button_background; /* background for buttons */
55 static uint32_t color_border; /* color of the button border */
56 static uint32_t color_border_bottom; /* color of the bottom border */
57 static uint32_t color_text; /* color of the text */
60 xcb_connection_t *conn;
61 xcb_screen_t *root_screen;
64 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
67 void verboselog(char *fmt, ...) {
71 vfprintf(stdout, fmt, args);
75 void errorlog(char *fmt, ...) {
79 vfprintf(stderr, fmt, args);
83 void debuglog(char *fmt, ...) {
87 * Starts the given application by passing it through a shell. We use double fork
88 * to avoid zombie processes. As the started application’s parent exits (immediately),
89 * the application is reparented to init (process-id 1), which correctly handles
90 * childs, so we don’t have to do it :-).
92 * The shell is determined by looking for the SHELL environment variable. If it
93 * does not exist, /bin/sh is used.
96 static void start_application(const char *command) {
97 printf("executing: %s\n", command);
102 /* This is the child */
103 execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (void *)NULL);
111 static button_t *get_button_at(int16_t x, int16_t y) {
112 for (int c = 0; c < buttoncnt; c++)
113 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
119 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
120 printf("button pressed on x = %d, y = %d\n",
121 event->event_x, event->event_y);
122 /* TODO: set a flag for the button, re-render */
126 * Called when the user releases the mouse button. Checks whether the
127 * coordinates are over a button and executes the appropriate action.
130 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
131 printf("button released on x = %d, y = %d\n",
132 event->event_x, event->event_y);
133 /* If the user hits the close button, we exit(0) */
134 if (event->event_x >= (rect.width - logical_px(32)))
136 button_t *button = get_button_at(event->event_x, event->event_y);
140 /* We need to create a custom script containing our actual command
141 * since not every terminal emulator which is contained in
142 * i3-sensible-terminal supports -e with multiple arguments (and not
143 * all of them support -e with one quoted argument either).
145 * NB: The paths need to be unique, that is, don’t assume users close
146 * their nagbars at any point in time (and they still need to work).
148 char *script_path = get_process_filename("nagbar-cmd");
150 int fd = open(script_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
152 warn("Could not create temporary script to store the nagbar command");
155 FILE *script = fdopen(fd, "w");
156 if (script == NULL) {
157 warn("Could not fdopen() temporary script to store the nagbar command");
160 fprintf(script, "#!/bin/sh\nrm %s\n%s", script_path, button->action);
165 char *exe_path = get_exe_path(argv0);
166 sasprintf(&link_path, "%s.nagbar_cmd", script_path);
167 if (symlink(exe_path, link_path) == -1) {
168 err(EXIT_FAILURE, "Failed to symlink %s to %s", link_path, exe_path);
172 sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
173 printf("argv0 = %s\n", argv0);
174 printf("terminal_cmd = %s\n", terminal_cmd);
176 start_application(terminal_cmd);
183 /* TODO: unset flag, re-render */
187 * Handles expose events (redraws of the window) and rendering in general. Will
188 * be called from the code with event == NULL or from X with event != NULL.
191 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
192 /* re-draw the background */
193 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_background});
194 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
196 /* restore font color */
197 set_font_colors(pixmap_gc, color_text, color_background);
198 draw_text(prompt, pixmap, pixmap_gc,
199 logical_px(4) + logical_px(4),
200 logical_px(4) + logical_px(4),
201 rect.width - logical_px(4) - logical_px(4));
203 /* render close button */
204 const char *close_button_label = "X";
205 int line_width = logical_px(4);
206 /* set width to the width of the label */
207 int w = predict_text_width(i3string_from_utf8(close_button_label));
208 /* account for left/right padding, which seems to be set to 8px (total) below */
212 values[0] = color_button_background;
213 values[1] = line_width;
214 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
216 xcb_rectangle_t close = {y - w - (2 * line_width), 0, w + (2 * line_width), rect.height};
217 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
219 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border});
220 xcb_point_t points[] = {
221 {y - w - (2 * line_width), line_width / 2},
222 {y - (line_width / 2), line_width / 2},
223 {y - (line_width / 2), (rect.height - (line_width / 2)) - logical_px(2)},
224 {y - w - (2 * line_width), (rect.height - (line_width / 2)) - logical_px(2)},
225 {y - w - (2 * line_width), line_width / 2}};
226 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
229 set_font_colors(pixmap_gc, color_text, color_button_background);
230 /* the x term here seems to set left/right padding */
231 draw_text_ascii(close_button_label, pixmap, pixmap_gc,
232 y - w - line_width + w / 2 - logical_px(4),
233 logical_px(4) + logical_px(3),
234 rect.width - y + w + line_width - w / 2 + logical_px(4));
239 /* render custom buttons */
241 for (int c = 0; c < buttoncnt; c++) {
242 /* set w to the width of the label */
243 w = predict_text_width(buttons[c].label);
244 /* account for left/right padding, which seems to be set to 12px (total) below */
247 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_button_background});
248 close = (xcb_rectangle_t){y - w - (2 * line_width), logical_px(2), w + (2 * line_width), rect.height - logical_px(6)};
249 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
251 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){color_border});
252 buttons[c].x = y - w - (2 * line_width);
253 buttons[c].width = w;
254 xcb_point_t points2[] = {
255 {y - w - (2 * line_width), (line_width / 2) + logical_px(2)},
256 {y - (line_width / 2), (line_width / 2) + logical_px(2)},
257 {y - (line_width / 2), (rect.height - logical_px(4) - (line_width / 2))},
258 {y - w - (2 * line_width), (rect.height - logical_px(4) - (line_width / 2))},
259 {y - w - (2 * line_width), (line_width / 2) + logical_px(2)}};
260 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
262 values[0] = color_text;
263 values[1] = color_button_background;
264 set_font_colors(pixmap_gc, color_text, color_button_background);
265 /* the x term seems to set left/right padding */
266 draw_text(buttons[c].label, pixmap, pixmap_gc,
267 y - w - line_width + logical_px(6),
268 logical_px(4) + logical_px(3),
269 rect.width - y + w + line_width - logical_px(6));
274 /* border line at the bottom */
275 line_width = logical_px(2);
276 values[0] = color_border_bottom;
277 values[1] = line_width;
278 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
279 xcb_point_t bottom[] = {
280 {0, rect.height - 0},
281 {rect.width, rect.height - 0}};
282 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
284 /* Copy the contents of the pixmap to the real window */
285 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
291 int main(int argc, char *argv[]) {
292 /* The following lines are a terribly horrible kludge. Because terminal
293 * emulators have different ways of interpreting the -e command line
294 * argument (some need -e "less /etc/fstab", others need -e less
295 * /etc/fstab), we need to write commands to a script and then just run
296 * that script. However, since on some machines, $XDG_RUNTIME_DIR and
297 * $TMPDIR are mounted with noexec, we cannot directly execute the script
300 * Initially, we tried to pass the command via the environment variable
301 * _I3_NAGBAR_CMD. But turns out that some terminal emulators such as
302 * xfce4-terminal run all windows from a single master process and only
303 * pass on the command (not the environment) to that master process.
305 * Therefore, we symlink i3-nagbar (which MUST reside on an executable
306 * filesystem) with a special name and run that symlink. When i3-nagbar
307 * recognizes it’s started as a binary ending in .nagbar_cmd, it strips off
308 * the .nagbar_cmd suffix and runs /bin/sh on argv[0]. That way, we can run
309 * a shell script on a noexec filesystem.
311 * From a security point of view, i3-nagbar is just an alias to /bin/sh in
312 * certain circumstances. This should not open any new security issues, I
315 const size_t argv0_len = strlen(argv[0]);
316 if (argv0_len > strlen(".nagbar_cmd") &&
317 strcmp(argv[0] + argv0_len - strlen(".nagbar_cmd"), ".nagbar_cmd") == 0) {
319 cmd = strdup(argv[0]);
320 *(cmd + argv0_len - strlen(".nagbar_cmd")) = '\0';
321 execl("/bin/sh", "/bin/sh", cmd, NULL);
322 err(EXIT_FAILURE, "execv(/bin/sh, /bin/sh, %s)", cmd);
327 char *pattern = sstrdup("pango:monospace 8");
328 int o, option_index = 0;
329 enum { TYPE_ERROR = 0,
330 TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
332 static struct option long_options[] = {
333 {"version", no_argument, 0, 'v'},
334 {"font", required_argument, 0, 'f'},
335 {"button", required_argument, 0, 'b'},
336 {"help", no_argument, 0, 'h'},
337 {"message", required_argument, 0, 'm'},
338 {"type", required_argument, 0, 't'},
341 char *options_string = "b:f:m:t:vh";
343 prompt = i3string_from_utf8("Please do not run this program.");
345 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
348 printf("i3-nagbar " I3_VERSION "\n");
352 pattern = sstrdup(optarg);
355 i3string_free(prompt);
356 prompt = i3string_from_utf8(optarg);
359 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
362 printf("i3-nagbar " I3_VERSION "\n");
363 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
366 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
367 buttons[buttoncnt].label = i3string_from_utf8(optarg);
368 buttons[buttoncnt].action = argv[optind];
369 printf("button with label *%s* and action *%s*\n",
370 i3string_as_utf8(buttons[buttoncnt].label),
371 buttons[buttoncnt].action);
373 printf("now %d buttons\n", buttoncnt);
381 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
382 xcb_connection_has_error(conn))
383 die("Cannot open display\n");
385 /* Place requests for the atoms we need as soon as possible */
386 #define xmacro(atom) \
387 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
388 #include "atoms.xmacro"
391 root_screen = xcb_aux_get_screen(conn, screens);
392 root = root_screen->root;
394 if (bar_type == TYPE_ERROR) {
395 /* Red theme for error messages */
396 color_button_background = get_colorpixel("#680a0a");
397 color_background = get_colorpixel("#900000");
398 color_text = get_colorpixel("#ffffff");
399 color_border = get_colorpixel("#d92424");
400 color_border_bottom = get_colorpixel("#470909");
402 /* Yellowish theme for warnings */
403 color_button_background = get_colorpixel("#ffc100");
404 color_background = get_colorpixel("#ffa8000");
405 color_text = get_colorpixel("#000000");
406 color_border = get_colorpixel("#ab7100");
407 color_border_bottom = get_colorpixel("#ab7100");
410 font = load_font(pattern, true);
413 /* Open an input window */
414 win = xcb_generate_id(conn);
418 XCB_COPY_FROM_PARENT,
419 win, /* the window id */
420 root, /* parent == root */
421 50, 50, 500, font.height + logical_px(8) + logical_px(8) /* 8 px padding */, /* dimensions */
422 0, /* x11 border = 0, we draw our own */
423 XCB_WINDOW_CLASS_INPUT_OUTPUT,
424 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
425 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
427 0, /* back pixel: black */
428 XCB_EVENT_MASK_EXPOSURE |
429 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
430 XCB_EVENT_MASK_BUTTON_PRESS |
431 XCB_EVENT_MASK_BUTTON_RELEASE});
433 /* Map the window (make it visible) */
434 xcb_map_window(conn, win);
436 /* Setup NetWM atoms */
437 #define xmacro(name) \
439 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
441 die("Could not get atom " #name "\n"); \
443 A_##name = reply->atom; \
446 #include "atoms.xmacro"
450 xcb_change_property(conn,
451 XCB_PROP_MODE_REPLACE,
453 A__NET_WM_WINDOW_TYPE,
457 (unsigned char *)&A__NET_WM_WINDOW_TYPE_DOCK);
459 /* Reserve some space at the top of the screen */
465 uint32_t left_start_y;
467 uint32_t right_start_y;
468 uint32_t right_end_y;
469 uint32_t top_start_x;
471 uint32_t bottom_start_x;
472 uint32_t bottom_end_x;
473 } __attribute__((__packed__)) strut_partial;
474 memset(&strut_partial, 0, sizeof(strut_partial));
476 strut_partial.top = font.height + logical_px(6);
477 strut_partial.top_start_x = 0;
478 strut_partial.top_end_x = 800;
480 xcb_change_property(conn,
481 XCB_PROP_MODE_REPLACE,
483 A__NET_WM_STRUT_PARTIAL,
490 pixmap = xcb_generate_id(conn);
491 pixmap_gc = xcb_generate_id(conn);
492 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + logical_px(8));
493 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
495 /* Grab the keyboard to get all input */
498 xcb_generic_event_t *event;
499 while ((event = xcb_wait_for_event(conn)) != NULL) {
500 if (event->response_type == 0) {
501 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
505 /* Strip off the highest bit (set if the event is generated) */
506 int type = (event->response_type & 0x7F);
510 handle_expose(conn, (xcb_expose_event_t *)event);
513 case XCB_BUTTON_PRESS:
514 handle_button_press(conn, (xcb_button_press_event_t *)event);
517 case XCB_BUTTON_RELEASE:
518 handle_button_release(conn, (xcb_button_release_event_t *)event);
521 case XCB_CONFIGURE_NOTIFY: {
522 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t *)event;
523 rect = (xcb_rectangle_t){
526 configure_notify->width,
527 configure_notify->height};
529 /* Recreate the pixmap / gc */
530 xcb_free_pixmap(conn, pixmap);
531 xcb_free_gc(conn, pixmap_gc);
533 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
534 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);