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 };
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;
59 * Starts the given application by passing it through a shell. We use double fork
60 * to avoid zombie processes. As the started application’s parent exits (immediately),
61 * the application is reparented to init (process-id 1), which correctly handles
62 * childs, so we don’t have to do it :-).
64 * The shell is determined by looking for the SHELL environment variable. If it
65 * does not exist, /bin/sh is used.
68 static void start_application(const char *command) {
69 printf("executing: %s\n", command);
74 /* Stores the path of the shell */
75 static const char *shell = NULL;
78 if ((shell = getenv("SHELL")) == NULL)
81 /* This is the child */
82 execl(shell, shell, "-c", command, (void*)NULL);
90 static button_t *get_button_at(int16_t x, int16_t y) {
91 for (int c = 0; c < buttoncnt; c++)
92 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
98 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
99 printf("button pressed on x = %d, y = %d\n",
100 event->event_x, event->event_y);
101 /* TODO: set a flag for the button, re-render */
105 * Called when the user releases the mouse button. Checks whether the
106 * coordinates are over a button and executes the appropriate action.
109 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
110 printf("button released on x = %d, y = %d\n",
111 event->event_x, event->event_y);
112 /* If the user hits the close button, we exit(0) */
113 if (event->event_x >= (rect.width - 32))
115 button_t *button = get_button_at(event->event_x, event->event_y);
118 start_application(button->action);
120 /* TODO: unset flag, re-render */
124 * Handles expose events (redraws of the window) and rendering in general. Will
125 * be called from the code with event == NULL or from X with event != NULL.
128 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
129 /* re-draw the background */
130 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_background });
131 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
133 /* restore font color */
135 values[0] = color_text;
136 values[1] = color_background;
137 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
138 draw_text(prompt, strlen(prompt), false, pixmap, pixmap_gc, 4 + 4, 4 + 4);
140 /* render close button */
144 values[0] = color_button_background;
145 values[1] = line_width;
146 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
148 xcb_rectangle_t close = { y - w - (2 * line_width), 0, w + (2 * line_width), rect.height };
149 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
151 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
152 xcb_point_t points[] = {
153 { y - w - (2 * line_width), line_width / 2 },
154 { y - (line_width / 2), line_width / 2 },
155 { y - (line_width / 2), (rect.height - (line_width / 2)) - 2 },
156 { y - w - (2 * line_width), (rect.height - (line_width / 2)) - 2 },
157 { y - w - (2 * line_width), line_width / 2 }
159 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
161 values[0] = color_text;
162 values[1] = color_button_background;
164 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_LINE_WIDTH, values);
165 draw_text("X", 1, false, pixmap, pixmap_gc, y - w - line_width + w / 2 - 4, 4 + 4 - 1);
170 /* render custom buttons */
172 for (int c = 0; c < buttoncnt; c++) {
173 /* TODO: make w = text extents of the label */
176 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_button_background });
177 close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
178 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
180 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ color_border });
181 buttons[c].x = y - w - (2 * line_width);
182 buttons[c].width = w;
183 xcb_point_t points2[] = {
184 { y - w - (2 * line_width), (line_width / 2) + 2 },
185 { y - (line_width / 2), (line_width / 2) + 2 },
186 { y - (line_width / 2), (rect.height - 4 - (line_width / 2)) },
187 { y - w - (2 * line_width), (rect.height - 4 - (line_width / 2)) },
188 { y - w - (2 * line_width), (line_width / 2) + 2 }
190 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
192 values[0] = color_text;
193 values[1] = color_button_background;
194 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
195 draw_text(buttons[c].label, strlen(buttons[c].label), false, pixmap, pixmap_gc,
196 y - w - line_width + 6, 4 + 3);
201 /* border line at the bottom */
203 values[0] = color_border_bottom;
204 values[1] = line_width;
205 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
206 xcb_point_t bottom[] = {
207 { 0, rect.height - 0 },
208 { rect.width, rect.height - 0 }
210 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
213 /* Copy the contents of the pixmap to the real window */
214 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
220 int main(int argc, char *argv[]) {
221 char *pattern = strdup("-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1");
222 int o, option_index = 0;
223 enum { TYPE_ERROR = 0, TYPE_WARNING = 1 } bar_type = TYPE_ERROR;
225 static struct option long_options[] = {
226 {"version", no_argument, 0, 'v'},
227 {"font", required_argument, 0, 'f'},
228 {"button", required_argument, 0, 'b'},
229 {"help", no_argument, 0, 'h'},
230 {"message", no_argument, 0, 'm'},
231 {"type", required_argument, 0, 't'},
235 char *options_string = "b:f:m:t:vh";
237 prompt = strdup("Please do not run this program.");
239 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
242 printf("i3-nagbar " I3_VERSION);
246 pattern = strdup(optarg);
250 prompt = strdup(optarg);
253 bar_type = (strcasecmp(optarg, "warning") == 0 ? TYPE_WARNING : TYPE_ERROR);
256 printf("i3-nagbar " I3_VERSION "\n");
257 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-f <font>] [-v]\n");
260 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
261 buttons[buttoncnt].label = optarg;
262 buttons[buttoncnt].action = argv[optind];
263 printf("button with label *%s* and action *%s*\n",
264 buttons[buttoncnt].label,
265 buttons[buttoncnt].action);
267 printf("now %d buttons\n", buttoncnt);
275 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
276 xcb_connection_has_error(conn))
277 die("Cannot open display\n");
279 /* Place requests for the atoms we need as soon as possible */
280 #define xmacro(atom) \
281 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
282 #include "atoms.xmacro"
285 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
286 root = root_screen->root;
288 if (bar_type == TYPE_ERROR) {
289 /* Red theme for error messages */
290 color_button_background = get_colorpixel("#680a0a");
291 color_background = get_colorpixel("#900000");
292 color_text = get_colorpixel("#ffffff");
293 color_border = get_colorpixel("#d92424");
294 color_border_bottom = get_colorpixel("#470909");
296 /* Yellowish theme for warnings */
297 color_button_background = get_colorpixel("#ffc100");
298 color_background = get_colorpixel("#ffa8000");
299 color_text = get_colorpixel("#000000");
300 color_border = get_colorpixel("#ab7100");
301 color_border_bottom = get_colorpixel("#ab7100");
304 font = load_font(pattern, true);
307 /* Open an input window */
308 win = xcb_generate_id(conn);
312 XCB_COPY_FROM_PARENT,
313 win, /* the window id */
314 root, /* parent == root */
315 50, 50, 500, font.height + 8 + 8 /* 8 px padding */, /* dimensions */
316 0, /* x11 border = 0, we draw our own */
317 XCB_WINDOW_CLASS_INPUT_OUTPUT,
318 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
319 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
321 0, /* back pixel: black */
322 XCB_EVENT_MASK_EXPOSURE |
323 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
324 XCB_EVENT_MASK_BUTTON_PRESS |
325 XCB_EVENT_MASK_BUTTON_RELEASE
328 /* Map the window (make it visible) */
329 xcb_map_window(conn, win);
331 /* Setup NetWM atoms */
332 #define xmacro(name) \
334 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
336 die("Could not get atom " # name "\n"); \
338 A_ ## name = reply->atom; \
341 #include "atoms.xmacro"
345 xcb_change_property(conn,
346 XCB_PROP_MODE_REPLACE,
348 A__NET_WM_WINDOW_TYPE,
352 (unsigned char*) &A__NET_WM_WINDOW_TYPE_DOCK);
354 /* Reserve some space at the top of the screen */
360 uint32_t left_start_y;
362 uint32_t right_start_y;
363 uint32_t right_end_y;
364 uint32_t top_start_x;
366 uint32_t bottom_start_x;
367 uint32_t bottom_end_x;
368 } __attribute__((__packed__)) strut_partial = {0,};
370 strut_partial.top = font.height + 6;
371 strut_partial.top_start_x = 0;
372 strut_partial.top_end_x = 800;
374 xcb_change_property(conn,
375 XCB_PROP_MODE_REPLACE,
377 A__NET_WM_STRUT_PARTIAL,
384 pixmap = xcb_generate_id(conn);
385 pixmap_gc = xcb_generate_id(conn);
386 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + 8);
387 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
389 /* Create graphics context */
390 xcb_change_gc(conn, pixmap_gc, XCB_GC_FONT, (uint32_t[]){ font.id });
392 /* Grab the keyboard to get all input */
395 xcb_generic_event_t *event;
396 while ((event = xcb_wait_for_event(conn)) != NULL) {
397 if (event->response_type == 0) {
398 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
402 /* Strip off the highest bit (set if the event is generated) */
403 int type = (event->response_type & 0x7F);
407 handle_expose(conn, (xcb_expose_event_t*)event);
410 case XCB_BUTTON_PRESS:
411 handle_button_press(conn, (xcb_button_press_event_t*)event);
414 case XCB_BUTTON_RELEASE:
415 handle_button_release(conn, (xcb_button_release_event_t*)event);
418 case XCB_CONFIGURE_NOTIFY: {
419 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t*)event;
420 rect = (xcb_rectangle_t){
423 configure_notify->width,
424 configure_notify->height
427 /* Recreate the pixmap / gc */
428 xcb_free_pixmap(conn, pixmap);
429 xcb_free_gc(conn, pixmap_gc);
431 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
432 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
434 /* Create graphics context */
435 xcb_change_gc(conn, pixmap_gc, XCB_GC_FONT, (uint32_t[]){ font.id });