2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 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 his configuration file.
12 #include <sys/types.h>
25 #include <xcb/xcb_aux.h>
26 #include <xcb/xcb_event.h>
29 #include "i3-nagbar.h"
38 static xcb_window_t win;
39 static xcb_pixmap_t pixmap;
40 static xcb_gcontext_t pixmap_gc;
41 static xcb_rectangle_t rect = { 0, 0, 600, 20 };
43 static i3String *prompt;
44 static button_t *buttons;
47 /* Result of get_colorpixel() for the various colors. */
48 static uint32_t color_background; /* background of the bar */
49 static uint32_t color_button_background; /* background for buttons */
50 static uint32_t color_border; /* color of the button border */
51 static uint32_t color_border_bottom; /* color of the bottom border */
52 static uint32_t color_text; /* color of the text */
55 xcb_connection_t *conn;
56 xcb_screen_t *root_screen;
59 * Having verboselog() and errorlog() is necessary when using libi3.
62 void verboselog(char *fmt, ...) {
66 vfprintf(stdout, fmt, args);
70 void errorlog(char *fmt, ...) {
74 vfprintf(stderr, fmt, args);
79 * Starts the given application by passing it through a shell. We use double fork
80 * to avoid zombie processes. As the started application’s parent exits (immediately),
81 * the application is reparented to init (process-id 1), which correctly handles
82 * childs, so we don’t have to do it :-).
84 * The shell is determined by looking for the SHELL environment variable. If it
85 * does not exist, /bin/sh is used.
88 static void start_application(const char *command) {
89 printf("executing: %s\n", command);
94 /* Stores the path of the shell */
95 static const char *shell = NULL;
98 if ((shell = getenv("SHELL")) == NULL)
101 /* This is the child */
102 execl(shell, shell, "-c", command, (void*)NULL);
110 static button_t *get_button_at(int16_t x, int16_t y) {
111 for (int c = 0; c < buttoncnt; c++)
112 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
118 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
119 printf("button pressed on x = %d, y = %d\n",
120 event->event_x, event->event_y);
121 /* TODO: set a flag for the button, re-render */
125 * Called when the user releases the mouse button. Checks whether the
126 * coordinates are over a button and executes the appropriate action.
129 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
130 printf("button released on x = %d, y = %d\n",
131 event->event_x, event->event_y);
132 /* If the user hits the close button, we exit(0) */
133 if (event->event_x >= (rect.width - 32))
135 button_t *button = get_button_at(event->event_x, event->event_y);
138 start_application(button->action);
140 /* TODO: unset flag, re-render */
144 * Handles expose events (redraws of the window) and rendering in general. Will
145 * be called from the code with event == NULL or from X with event != NULL.
148 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
149 /* re-draw the background */
150 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_background });
151 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
153 /* restore font color */
154 set_font_colors(pixmap_gc, color_text, color_background);
155 draw_text(prompt, pixmap, pixmap_gc,
156 4 + 4, 4 + 4, rect.width - 4 - 4);
158 /* render close button */
163 values[0] = color_button_background;
164 values[1] = line_width;
165 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
167 xcb_rectangle_t close = { y - w - (2 * line_width), 0, w + (2 * line_width), rect.height };
168 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
170 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
171 xcb_point_t points[] = {
172 { y - w - (2 * line_width), line_width / 2 },
173 { y - (line_width / 2), line_width / 2 },
174 { y - (line_width / 2), (rect.height - (line_width / 2)) - 2 },
175 { y - w - (2 * line_width), (rect.height - (line_width / 2)) - 2 },
176 { y - w - (2 * line_width), line_width / 2 }
178 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
181 set_font_colors(pixmap_gc, color_text, color_button_background);
182 draw_text_ascii("X", pixmap, pixmap_gc, y - w - line_width + w / 2 - 4,
183 4 + 4 - 1, rect.width - y + w + line_width - w / 2 + 4);
188 /* render custom buttons */
190 for (int c = 0; c < buttoncnt; c++) {
191 /* TODO: make w = text extents of the label */
194 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_button_background });
195 close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
196 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
198 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
199 buttons[c].x = y - w - (2 * line_width);
200 buttons[c].width = w;
201 xcb_point_t points2[] = {
202 { y - w - (2 * line_width), (line_width / 2) + 2 },
203 { y - (line_width / 2), (line_width / 2) + 2 },
204 { y - (line_width / 2), (rect.height - 4 - (line_width / 2)) },
205 { y - w - (2 * line_width), (rect.height - 4 - (line_width / 2)) },
206 { y - w - (2 * line_width), (line_width / 2) + 2 }
208 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
210 values[0] = color_text;
211 values[1] = color_button_background;
212 set_font_colors(pixmap_gc, color_text, color_button_background);
213 draw_text(buttons[c].label, pixmap, pixmap_gc,
214 y - w - line_width + 6, 4 + 3, rect.width - y + w + line_width - 6);
219 /* border line at the bottom */
221 values[0] = color_border_bottom;
222 values[1] = line_width;
223 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
224 xcb_point_t bottom[] = {
225 { 0, rect.height - 0 },
226 { rect.width, rect.height - 0 }
228 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
231 /* Copy the contents of the pixmap to the real window */
232 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
238 int main(int argc, char *argv[]) {
239 char *pattern = sstrdup("-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1");
240 int o, option_index = 0;
241 enum { TYPE_ERROR = 0, TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
243 static struct option long_options[] = {
244 {"version", no_argument, 0, 'v'},
245 {"font", required_argument, 0, 'f'},
246 {"button", required_argument, 0, 'b'},
247 {"help", no_argument, 0, 'h'},
248 {"message", no_argument, 0, 'm'},
249 {"type", required_argument, 0, 't'},
253 char *options_string = "b:f:m:t:vh";
255 prompt = i3string_from_utf8("Please do not run this program.");
257 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
260 printf("i3-nagbar " I3_VERSION);
264 pattern = sstrdup(optarg);
267 i3string_free(prompt);
268 prompt = i3string_from_utf8(optarg);
271 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
274 printf("i3-nagbar " I3_VERSION "\n");
275 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
278 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
279 buttons[buttoncnt].label = i3string_from_utf8(optarg);
280 buttons[buttoncnt].action = argv[optind];
281 printf("button with label *%s* and action *%s*\n",
282 i3string_as_utf8(buttons[buttoncnt].label),
283 buttons[buttoncnt].action);
285 printf("now %d buttons\n", buttoncnt);
293 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
294 xcb_connection_has_error(conn))
295 die("Cannot open display\n");
297 /* Place requests for the atoms we need as soon as possible */
298 #define xmacro(atom) \
299 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
300 #include "atoms.xmacro"
303 root_screen = xcb_aux_get_screen(conn, screens);
304 root = root_screen->root;
306 if (bar_type == TYPE_ERROR) {
307 /* Red theme for error messages */
308 color_button_background = get_colorpixel("#680a0a");
309 color_background = get_colorpixel("#900000");
310 color_text = get_colorpixel("#ffffff");
311 color_border = get_colorpixel("#d92424");
312 color_border_bottom = get_colorpixel("#470909");
314 /* Yellowish theme for warnings */
315 color_button_background = get_colorpixel("#ffc100");
316 color_background = get_colorpixel("#ffa8000");
317 color_text = get_colorpixel("#000000");
318 color_border = get_colorpixel("#ab7100");
319 color_border_bottom = get_colorpixel("#ab7100");
322 font = load_font(pattern, true);
325 /* Open an input window */
326 win = xcb_generate_id(conn);
330 XCB_COPY_FROM_PARENT,
331 win, /* the window id */
332 root, /* parent == root */
333 50, 50, 500, font.height + 8 + 8 /* 8 px padding */, /* dimensions */
334 0, /* x11 border = 0, we draw our own */
335 XCB_WINDOW_CLASS_INPUT_OUTPUT,
336 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
337 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
339 0, /* back pixel: black */
340 XCB_EVENT_MASK_EXPOSURE |
341 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
342 XCB_EVENT_MASK_BUTTON_PRESS |
343 XCB_EVENT_MASK_BUTTON_RELEASE
346 /* Map the window (make it visible) */
347 xcb_map_window(conn, win);
349 /* Setup NetWM atoms */
350 #define xmacro(name) \
352 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
354 die("Could not get atom " # name "\n"); \
356 A_ ## name = reply->atom; \
359 #include "atoms.xmacro"
363 xcb_change_property(conn,
364 XCB_PROP_MODE_REPLACE,
366 A__NET_WM_WINDOW_TYPE,
370 (unsigned char*) &A__NET_WM_WINDOW_TYPE_DOCK);
372 /* Reserve some space at the top of the screen */
378 uint32_t left_start_y;
380 uint32_t right_start_y;
381 uint32_t right_end_y;
382 uint32_t top_start_x;
384 uint32_t bottom_start_x;
385 uint32_t bottom_end_x;
386 } __attribute__((__packed__)) strut_partial = {0,};
388 strut_partial.top = font.height + 6;
389 strut_partial.top_start_x = 0;
390 strut_partial.top_end_x = 800;
392 xcb_change_property(conn,
393 XCB_PROP_MODE_REPLACE,
395 A__NET_WM_STRUT_PARTIAL,
402 pixmap = xcb_generate_id(conn);
403 pixmap_gc = xcb_generate_id(conn);
404 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + 8);
405 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
407 /* Grab the keyboard to get all input */
410 xcb_generic_event_t *event;
411 while ((event = xcb_wait_for_event(conn)) != NULL) {
412 if (event->response_type == 0) {
413 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
417 /* Strip off the highest bit (set if the event is generated) */
418 int type = (event->response_type & 0x7F);
422 handle_expose(conn, (xcb_expose_event_t*)event);
425 case XCB_BUTTON_PRESS:
426 handle_button_press(conn, (xcb_button_press_event_t*)event);
429 case XCB_BUTTON_RELEASE:
430 handle_button_release(conn, (xcb_button_release_event_t*)event);
433 case XCB_CONFIGURE_NOTIFY: {
434 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t*)event;
435 rect = (xcb_rectangle_t){
438 configure_notify->width,
439 configure_notify->height
442 /* Recreate the pixmap / gc */
443 xcb_free_pixmap(conn, pixmap);
444 xcb_free_gc(conn, pixmap_gc);
446 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
447 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);