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.
13 #include <sys/types.h>
26 #include <xcb/xcb_aux.h>
27 #include <xcb/xcb_event.h>
30 #include "i3-nagbar.h"
39 static xcb_window_t win;
40 static xcb_pixmap_t pixmap;
41 static xcb_gcontext_t pixmap_gc;
42 static xcb_rectangle_t rect = { 0, 0, 600, 20 };
44 static i3String *prompt;
45 static button_t *buttons;
48 /* Result of get_colorpixel() for the various colors. */
49 static uint32_t color_background; /* background of the bar */
50 static uint32_t color_button_background; /* background for buttons */
51 static uint32_t color_border; /* color of the button border */
52 static uint32_t color_border_bottom; /* color of the bottom border */
53 static uint32_t color_text; /* color of the text */
56 xcb_connection_t *conn;
57 xcb_screen_t *root_screen;
60 * Having verboselog() and errorlog() is necessary when using libi3.
63 void verboselog(char *fmt, ...) {
67 vfprintf(stdout, fmt, args);
71 void errorlog(char *fmt, ...) {
75 vfprintf(stderr, fmt, args);
80 * Starts the given application by passing it through a shell. We use double fork
81 * to avoid zombie processes. As the started application’s parent exits (immediately),
82 * the application is reparented to init (process-id 1), which correctly handles
83 * childs, so we don’t have to do it :-).
85 * The shell is determined by looking for the SHELL environment variable. If it
86 * does not exist, /bin/sh is used.
89 static void start_application(const char *command) {
90 printf("executing: %s\n", command);
95 /* Stores the path of the shell */
96 static const char *shell = NULL;
99 if ((shell = getenv("SHELL")) == NULL)
102 /* This is the child */
103 execl(shell, shell, "-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 - 32))
136 button_t *button = get_button_at(event->event_x, event->event_y);
139 start_application(button->action);
141 /* TODO: unset flag, re-render */
145 * Handles expose events (redraws of the window) and rendering in general. Will
146 * be called from the code with event == NULL or from X with event != NULL.
149 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
150 /* re-draw the background */
151 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_background });
152 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
154 /* restore font color */
155 set_font_colors(pixmap_gc, color_text, color_background);
156 draw_text(prompt, pixmap, pixmap_gc,
157 4 + 4, 4 + 4, rect.width - 4 - 4);
159 /* render close button */
164 values[0] = color_button_background;
165 values[1] = line_width;
166 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
168 xcb_rectangle_t close = { y - w - (2 * line_width), 0, w + (2 * line_width), rect.height };
169 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
171 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
172 xcb_point_t points[] = {
173 { y - w - (2 * line_width), line_width / 2 },
174 { y - (line_width / 2), line_width / 2 },
175 { y - (line_width / 2), (rect.height - (line_width / 2)) - 2 },
176 { y - w - (2 * line_width), (rect.height - (line_width / 2)) - 2 },
177 { y - w - (2 * line_width), line_width / 2 }
179 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
182 set_font_colors(pixmap_gc, color_text, color_button_background);
183 draw_text_ascii("X", pixmap, pixmap_gc, y - w - line_width + w / 2 - 4,
184 4 + 4 - 1, rect.width - y + w + line_width - w / 2 + 4);
189 /* render custom buttons */
191 for (int c = 0; c < buttoncnt; c++) {
192 /* TODO: make w = text extents of the label */
195 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_button_background });
196 close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
197 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
199 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
200 buttons[c].x = y - w - (2 * line_width);
201 buttons[c].width = w;
202 xcb_point_t points2[] = {
203 { y - w - (2 * line_width), (line_width / 2) + 2 },
204 { y - (line_width / 2), (line_width / 2) + 2 },
205 { y - (line_width / 2), (rect.height - 4 - (line_width / 2)) },
206 { y - w - (2 * line_width), (rect.height - 4 - (line_width / 2)) },
207 { y - w - (2 * line_width), (line_width / 2) + 2 }
209 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
211 values[0] = color_text;
212 values[1] = color_button_background;
213 set_font_colors(pixmap_gc, color_text, color_button_background);
214 draw_text(buttons[c].label, pixmap, pixmap_gc,
215 y - w - line_width + 6, 4 + 3, rect.width - y + w + line_width - 6);
220 /* border line at the bottom */
222 values[0] = color_border_bottom;
223 values[1] = line_width;
224 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
225 xcb_point_t bottom[] = {
226 { 0, rect.height - 0 },
227 { rect.width, rect.height - 0 }
229 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
232 /* Copy the contents of the pixmap to the real window */
233 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
239 int main(int argc, char *argv[]) {
240 char *pattern = sstrdup("-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1");
241 int o, option_index = 0;
242 enum { TYPE_ERROR = 0, TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
244 static struct option long_options[] = {
245 {"version", no_argument, 0, 'v'},
246 {"font", required_argument, 0, 'f'},
247 {"button", required_argument, 0, 'b'},
248 {"help", no_argument, 0, 'h'},
249 {"message", no_argument, 0, 'm'},
250 {"type", required_argument, 0, 't'},
254 char *options_string = "b:f:m:t:vh";
256 prompt = i3string_from_utf8("Please do not run this program.");
258 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
261 printf("i3-nagbar " I3_VERSION);
265 pattern = sstrdup(optarg);
268 i3string_free(prompt);
269 prompt = i3string_from_utf8(optarg);
272 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
275 printf("i3-nagbar " I3_VERSION "\n");
276 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-t warning|error] [-f <font>] [-v]\n");
279 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
280 buttons[buttoncnt].label = i3string_from_utf8(optarg);
281 buttons[buttoncnt].action = argv[optind];
282 printf("button with label *%s* and action *%s*\n",
283 i3string_as_utf8(buttons[buttoncnt].label),
284 buttons[buttoncnt].action);
286 printf("now %d buttons\n", buttoncnt);
294 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
295 xcb_connection_has_error(conn))
296 die("Cannot open display\n");
298 /* Place requests for the atoms we need as soon as possible */
299 #define xmacro(atom) \
300 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
301 #include "atoms.xmacro"
304 root_screen = xcb_aux_get_screen(conn, screens);
305 root = root_screen->root;
307 if (bar_type == TYPE_ERROR) {
308 /* Red theme for error messages */
309 color_button_background = get_colorpixel("#680a0a");
310 color_background = get_colorpixel("#900000");
311 color_text = get_colorpixel("#ffffff");
312 color_border = get_colorpixel("#d92424");
313 color_border_bottom = get_colorpixel("#470909");
315 /* Yellowish theme for warnings */
316 color_button_background = get_colorpixel("#ffc100");
317 color_background = get_colorpixel("#ffa8000");
318 color_text = get_colorpixel("#000000");
319 color_border = get_colorpixel("#ab7100");
320 color_border_bottom = get_colorpixel("#ab7100");
323 font = load_font(pattern, true);
326 /* Open an input window */
327 win = xcb_generate_id(conn);
331 XCB_COPY_FROM_PARENT,
332 win, /* the window id */
333 root, /* parent == root */
334 50, 50, 500, font.height + 8 + 8 /* 8 px padding */, /* dimensions */
335 0, /* x11 border = 0, we draw our own */
336 XCB_WINDOW_CLASS_INPUT_OUTPUT,
337 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
338 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
340 0, /* back pixel: black */
341 XCB_EVENT_MASK_EXPOSURE |
342 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
343 XCB_EVENT_MASK_BUTTON_PRESS |
344 XCB_EVENT_MASK_BUTTON_RELEASE
347 /* Map the window (make it visible) */
348 xcb_map_window(conn, win);
350 /* Setup NetWM atoms */
351 #define xmacro(name) \
353 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
355 die("Could not get atom " # name "\n"); \
357 A_ ## name = reply->atom; \
360 #include "atoms.xmacro"
364 xcb_change_property(conn,
365 XCB_PROP_MODE_REPLACE,
367 A__NET_WM_WINDOW_TYPE,
371 (unsigned char*) &A__NET_WM_WINDOW_TYPE_DOCK);
373 /* Reserve some space at the top of the screen */
379 uint32_t left_start_y;
381 uint32_t right_start_y;
382 uint32_t right_end_y;
383 uint32_t top_start_x;
385 uint32_t bottom_start_x;
386 uint32_t bottom_end_x;
387 } __attribute__((__packed__)) strut_partial = {0,};
389 strut_partial.top = font.height + 6;
390 strut_partial.top_start_x = 0;
391 strut_partial.top_end_x = 800;
393 xcb_change_property(conn,
394 XCB_PROP_MODE_REPLACE,
396 A__NET_WM_STRUT_PARTIAL,
403 pixmap = xcb_generate_id(conn);
404 pixmap_gc = xcb_generate_id(conn);
405 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + 8);
406 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
408 /* Grab the keyboard to get all input */
411 xcb_generic_event_t *event;
412 while ((event = xcb_wait_for_event(conn)) != NULL) {
413 if (event->response_type == 0) {
414 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
418 /* Strip off the highest bit (set if the event is generated) */
419 int type = (event->response_type & 0x7F);
423 handle_expose(conn, (xcb_expose_event_t*)event);
426 case XCB_BUTTON_PRESS:
427 handle_button_press(conn, (xcb_button_press_event_t*)event);
430 case XCB_BUTTON_RELEASE:
431 handle_button_release(conn, (xcb_button_release_event_t*)event);
434 case XCB_CONFIGURE_NOTIFY: {
435 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t*)event;
436 rect = (xcb_rectangle_t){
439 configure_notify->width,
440 configure_notify->height
443 /* Recreate the pixmap / gc */
444 xcb_free_pixmap(conn, pixmap);
445 xcb_free_gc(conn, pixmap_gc);
447 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
448 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);