2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2011 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * i3-nagbar is a utility which displays a nag message.
15 #include <sys/types.h>
28 #include <xcb/xcb_aux.h>
29 #include <xcb/xcb_event.h>
31 #include "i3-nagbar.h"
40 static xcb_window_t win;
41 static xcb_pixmap_t pixmap;
42 static xcb_gcontext_t pixmap_gc;
43 static xcb_rectangle_t rect = { 0, 0, 600, 20 };
44 static int font_height;
45 static char *prompt = "Please do not run this program.";
46 static button_t *buttons;
51 * Starts the given application by passing it through a shell. We use double fork
52 * to avoid zombie processes. As the started application’s parent exits (immediately),
53 * the application is reparented to init (process-id 1), which correctly handles
54 * childs, so we don’t have to do it :-).
56 * The shell is determined by looking for the SHELL environment variable. If it
57 * does not exist, /bin/sh is used.
60 static void start_application(const char *command) {
61 printf("executing: %s\n", command);
66 /* Stores the path of the shell */
67 static const char *shell = NULL;
70 if ((shell = getenv("SHELL")) == NULL)
73 /* This is the child */
74 execl(shell, shell, "-c", command, (void*)NULL);
82 static button_t *get_button_at(int16_t x, int16_t y) {
83 for (int c = 0; c < buttoncnt; c++)
84 if (x >= (buttons[c].x) && x <= (buttons[c].x + buttons[c].width))
90 static void handle_button_press(xcb_connection_t *conn, xcb_button_press_event_t *event) {
91 printf("button pressed on x = %d, y = %d\n",
92 event->event_x, event->event_y);
93 /* TODO: set a flag for the button, re-render */
97 * Called when the user releases the mouse button. Checks whether the
98 * coordinates are over a button and executes the appropriate action.
101 static void handle_button_release(xcb_connection_t *conn, xcb_button_release_event_t *event) {
102 printf("button released on x = %d, y = %d\n",
103 event->event_x, event->event_y);
104 /* If the user hits the close button, we exit(0) */
105 if (event->event_x >= (rect.width - 32))
107 button_t *button = get_button_at(event->event_x, event->event_y);
110 start_application(button->action);
112 /* TODO: unset flag, re-render */
116 * Handles expose events (redraws of the window) and rendering in general. Will
117 * be called from the code with event == NULL or from X with event != NULL.
120 static int handle_expose(xcb_connection_t *conn, xcb_expose_event_t *event) {
123 /* re-draw the background */
124 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#900000"));
125 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &rect);
127 /* restore font color */
129 values[0] = get_colorpixel(conn, "#FFFFFF");
130 values[1] = get_colorpixel(conn, "#900000");
131 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
132 xcb_image_text_8(conn, strlen(prompt), pixmap, pixmap_gc, 4 + 4/* X */,
133 font_height + 2 + 4 /* Y = baseline of font */, prompt);
135 /* render close button */
139 values[0] = get_colorpixel(conn, "#680a0a");
140 values[1] = line_width;
141 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
143 xcb_rectangle_t close = { y - w - (2 * line_width), 0, w + (2 * line_width), rect.height };
144 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
146 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#d92424"));
147 xcb_point_t points[] = {
148 { y - w - (2 * line_width), line_width / 2 },
149 { y - (line_width / 2), line_width / 2 },
150 { y - (line_width / 2), (rect.height - (line_width / 2)) - 2 },
151 { y - w - (2 * line_width), (rect.height - (line_width / 2)) - 2 },
152 { y - w - (2 * line_width), line_width / 2 }
154 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points);
156 values[0] = get_colorpixel(conn, "#ffffff");
157 values[1] = get_colorpixel(conn, "#680a0a");
159 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_LINE_WIDTH, values);
160 xcb_image_text_8(conn, strlen("x"), pixmap, pixmap_gc, y - w - line_width + (w / 2) - 4/* X */,
161 font_height + 2 + 4 - 1/* Y = baseline of font */, "X");
166 /* render custom buttons */
168 for (int c = 0; c < buttoncnt; c++) {
169 /* TODO: make w = text extents of the label */
172 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#680a0a"));
173 close = (xcb_rectangle_t){ y - w - (2 * line_width), 2, w + (2 * line_width), rect.height - 6 };
174 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &close);
176 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#d92424"));
177 buttons[c].x = y - w - (2 * line_width);
178 buttons[c].width = w;
179 xcb_point_t points2[] = {
180 { y - w - (2 * line_width), (line_width / 2) + 2 },
181 { y - (line_width / 2), (line_width / 2) + 2 },
182 { y - (line_width / 2), (rect.height - 4 - (line_width / 2)) },
183 { y - w - (2 * line_width), (rect.height - 4 - (line_width / 2)) },
184 { y - w - (2 * line_width), (line_width / 2) + 2 }
186 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 5, points2);
188 values[0] = get_colorpixel(conn, "#ffffff");
189 values[1] = get_colorpixel(conn, "#680a0a");
190 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
191 xcb_image_text_8(conn, strlen(buttons[c].label), pixmap, pixmap_gc, y - w - line_width + 6/* X */,
192 font_height + 2 + 3/* Y = baseline of font */, buttons[c].label);
197 /* border line at the bottom */
199 values[0] = get_colorpixel(conn, "#470909");
200 values[1] = line_width;
201 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
202 xcb_point_t bottom[] = {
203 { 0, rect.height - 0 },
204 { rect.width, rect.height - 0 }
206 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, pixmap, pixmap_gc, 2, bottom);
209 /* Copy the contents of the pixmap to the real window */
210 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, rect.width, rect.height);
216 int main(int argc, char *argv[]) {
217 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
218 int o, option_index = 0;
220 static struct option long_options[] = {
221 {"version", no_argument, 0, 'v'},
222 {"font", required_argument, 0, 'f'},
223 {"button", required_argument, 0, 'b'},
224 {"help", no_argument, 0, 'h'},
225 {"message", no_argument, 0, 'm'},
229 char *options_string = "b:f:m:vh";
231 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
234 printf("i3-nagbar " I3_VERSION);
238 pattern = strdup(optarg);
241 prompt = strdup(optarg);
244 printf("i3-nagbar " I3_VERSION "\n");
245 printf("i3-nagbar [-m <message>] [-b <button> <action>] [-f <font>] [-v]\n");
248 buttons = realloc(buttons, sizeof(button_t) * (buttoncnt + 1));
249 buttons[buttoncnt].label = optarg;
250 buttons[buttoncnt].action = argv[optind];
251 printf("button with label *%s* and action *%s*\n",
252 buttons[buttoncnt].label,
253 buttons[buttoncnt].action);
255 printf("now %d buttons\n", buttoncnt);
263 xcb_connection_t *conn;
264 if ((conn = xcb_connect(NULL, &screens)) == NULL ||
265 xcb_connection_has_error(conn))
266 die("Cannot open display\n");
268 /* Place requests for the atoms we need as soon as possible */
269 #define xmacro(atom) \
270 xcb_intern_atom_cookie_t atom ## _cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
271 #include "atoms.xmacro"
274 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
275 root = root_screen->root;
277 uint32_t font_id = get_font_id(conn, pattern, &font_height);
279 /* Open an input window */
280 win = open_input_window(conn, 500, font_height + 8 + 8 /* 8px padding */);
282 /* Setup NetWM atoms */
283 #define xmacro(name) \
285 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name ## _cookie, NULL); \
287 die("Could not get atom " # name "\n"); \
289 A_ ## name = reply->atom; \
292 #include "atoms.xmacro"
296 xcb_change_property(conn,
297 XCB_PROP_MODE_REPLACE,
299 A__NET_WM_WINDOW_TYPE,
303 (unsigned char*) &A__NET_WM_WINDOW_TYPE_DOCK);
305 /* Reserve some space at the top of the screen */
311 uint32_t left_start_y;
313 uint32_t right_start_y;
314 uint32_t right_end_y;
315 uint32_t top_start_x;
317 uint32_t bottom_start_x;
318 uint32_t bottom_end_x;
319 } __attribute__((__packed__)) strut_partial = {0,};
321 strut_partial.top = font_height + 6;
322 strut_partial.top_start_x = 0;
323 strut_partial.top_end_x = 800;
325 xcb_change_property(conn,
326 XCB_PROP_MODE_REPLACE,
328 A__NET_WM_STRUT_PARTIAL,
335 pixmap = xcb_generate_id(conn);
336 pixmap_gc = xcb_generate_id(conn);
337 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
338 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
340 /* Create graphics context */
341 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
343 /* Grab the keyboard to get all input */
346 xcb_generic_event_t *event;
347 while ((event = xcb_wait_for_event(conn)) != NULL) {
348 if (event->response_type == 0) {
349 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
353 /* Strip off the highest bit (set if the event is generated) */
354 int type = (event->response_type & 0x7F);
358 handle_expose(conn, (xcb_expose_event_t*)event);
361 case XCB_BUTTON_PRESS:
362 handle_button_press(conn, (xcb_button_press_event_t*)event);
365 case XCB_BUTTON_RELEASE:
366 handle_button_release(conn, (xcb_button_release_event_t*)event);
369 case XCB_CONFIGURE_NOTIFY: {
370 xcb_configure_notify_event_t *configure_notify = (xcb_configure_notify_event_t*)event;
371 rect = (xcb_rectangle_t){
374 configure_notify->width,
375 configure_notify->height
378 /* Recreate the pixmap / gc */
379 xcb_free_pixmap(conn, pixmap);
380 xcb_free_gc(conn, pixmap_gc);
382 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, rect.width, rect.height);
383 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
385 /* Create graphics context */
386 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);