2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * i3-config-wizard: Program to convert configs using keycodes to configs using
13 #if defined(__FreeBSD__)
14 #include <sys/param.h>
17 /* For systems without getline, fall back to fgetln */
18 #if defined(__APPLE__)
20 #elif defined(__FreeBSD__)
21 /* Defining this macro before including stdio.h is necessary in order to have
22 * a prototype for getline in FreeBSD. */
27 #include <sys/types.h>
44 #include <xcb/xcb_aux.h>
45 #include <xcb/xcb_event.h>
46 #include <xcb/xcb_keysyms.h>
48 #include <xkbcommon/xkbcommon.h>
49 #include <xkbcommon/xkbcommon-x11.h>
52 #include <X11/keysym.h>
53 #include <X11/XKBlib.h>
55 /* We need SYSCONFDIR for the path to the keycode config template, so raise an
56 * error if it’s not defined for whatever reason */
58 #error "SYSCONFDIR not defined"
61 #define FREE(pointer) \
63 if (pointer != NULL) { \
73 (((row)-1) * font.height + logical_px(4))
74 #define window_height() \
75 (row_y(15) + font.height)
78 STEP_GENERATE } current_step = STEP_WELCOME;
80 MOD_Mod4 } modifier = MOD_Mod4;
82 static char *config_path;
83 static uint32_t xcb_numlock_mask;
84 xcb_connection_t *conn;
85 static xcb_key_symbols_t *keysyms;
86 xcb_screen_t *root_screen;
87 static xcb_get_modifier_mapping_reply_t *modmap_reply;
89 static i3Font bold_font;
90 static int char_width;
91 static char *socket_path;
92 static xcb_window_t win;
93 static xcb_pixmap_t pixmap;
94 static xcb_gcontext_t pixmap_gc;
95 static xcb_key_symbols_t *symbols;
97 static struct xkb_keymap *xkb_keymap;
98 static uint8_t xkb_base_event;
99 static uint8_t xkb_base_error;
101 static void finish();
103 #include "GENERATED_config_enums.h"
105 typedef struct token {
108 /* This might be __CALL */
109 cmdp_state next_state;
111 uint16_t call_identifier;
115 typedef struct tokenptr {
120 #include "GENERATED_config_tokens.h"
122 static cmdp_state state;
123 /* A list which contains the states that lead to the current state, e.g.
124 * INITIAL, WORKSPACE_LAYOUT.
125 * When jumping back to INITIAL, statelist_idx will simply be set to 1
126 * (likewise for other states, e.g. MODE or BAR).
127 * This list is used to process the nearest error token. */
128 static cmdp_state statelist[10] = {INITIAL};
129 /* NB: statelist_idx points to where the next entry will be inserted */
130 static int statelist_idx = 1;
133 /* Just a pointer, not dynamically allocated. */
134 const char *identifier;
145 /* 10 entries should be enough for everybody. */
146 static struct stack_entry stack[10];
149 * Pushes a string (identified by 'identifier') on the stack. We simply use a
150 * single array, since the number of entries we have to store is very small.
153 static void push_string(const char *identifier, const char *str) {
154 for (int c = 0; c < 10; c++) {
155 if (stack[c].identifier != NULL &&
156 strcmp(stack[c].identifier, identifier) != 0)
158 if (stack[c].identifier == NULL) {
159 /* Found a free slot, let’s store it here. */
160 stack[c].identifier = identifier;
161 stack[c].val.str = sstrdup(str);
162 stack[c].type = STACK_STR;
164 /* Append the value. */
165 char *prev = stack[c].val.str;
166 sasprintf(&(stack[c].val.str), "%s,%s", prev, str);
172 /* When we arrive here, the stack is full. This should not happen and
173 * means there’s either a bug in this parser or the specification
174 * contains a command with more than 10 identified tokens. */
175 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
176 "in the code, or a new command which contains more than "
177 "10 identified tokens.\n");
181 static void push_long(const char *identifier, long num) {
182 for (int c = 0; c < 10; c++) {
183 if (stack[c].identifier != NULL)
185 /* Found a free slot, let’s store it here. */
186 stack[c].identifier = identifier;
187 stack[c].val.num = num;
188 stack[c].type = STACK_LONG;
192 /* When we arrive here, the stack is full. This should not happen and
193 * means there’s either a bug in this parser or the specification
194 * contains a command with more than 10 identified tokens. */
195 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
196 "in the code, or a new command which contains more than "
197 "10 identified tokens.\n");
201 static const char *get_string(const char *identifier) {
202 for (int c = 0; c < 10; c++) {
203 if (stack[c].identifier == NULL)
205 if (strcmp(identifier, stack[c].identifier) == 0)
206 return stack[c].val.str;
211 static void clear_stack(void) {
212 for (int c = 0; c < 10; c++) {
213 if (stack[c].type == STACK_STR && stack[c].val.str != NULL)
214 free(stack[c].val.str);
215 stack[c].identifier = NULL;
216 stack[c].val.str = NULL;
217 stack[c].val.num = 0;
222 * Returns true if sym is bound to any key except for 'except_keycode' on the
223 * first four layers (normal, shift, mode_switch, mode_switch + shift).
226 static bool keysym_used_on_other_key(KeySym sym, xcb_keycode_t except_keycode) {
228 min_keycode = xcb_get_setup(conn)->min_keycode,
229 max_keycode = xcb_get_setup(conn)->max_keycode;
231 for (i = min_keycode; i && i <= max_keycode; i++) {
232 if (i == except_keycode)
234 for (int level = 0; level < 4; level++) {
235 if (xcb_key_symbols_get_keysym(keysyms, i, level) != sym)
243 static char *next_state(const cmdp_token *token) {
244 cmdp_state _next_state = token->next_state;
246 if (token->next_state == __CALL) {
247 const char *modifiers = get_string("modifiers");
248 int keycode = atoi(get_string("key"));
250 if (modifiers != NULL &&
251 strstr(modifiers, "Shift") != NULL) {
252 /* When shift is included, we really need to use the second-level
253 * symbol (upper-case). The lower-case symbol could be on a
254 * different key than the upper-case one (unlikely for letters, but
255 * more likely for special characters). */
258 /* Try to use the keysym on the first level (lower-case). In case
259 * this doesn’t make it ambiguous (think of a keyboard layout
260 * having '1' on two different keys, but '!' only on keycode 10),
261 * we’ll stick with the keysym of the first level.
263 * This reduces a lot of confusion for users who switch keyboard
264 * layouts from qwerty to qwertz or other slight variations of
265 * qwerty (yes, that happens quite often). */
266 const xkb_keysym_t *syms;
267 int num = xkb_keymap_key_get_syms_by_level(xkb_keymap, keycode, 0, 0, &syms);
269 errx(1, "xkb_keymap_key_get_syms_by_level returned no symbols for keycode %d", keycode);
270 if (!keysym_used_on_other_key(syms[0], keycode))
274 const xkb_keysym_t *syms;
275 int num = xkb_keymap_key_get_syms_by_level(xkb_keymap, keycode, 0, level, &syms);
277 errx(1, "xkb_keymap_key_get_syms_by_level returned no symbols for keycode %d", keycode);
279 printf("xkb_keymap_key_get_syms_by_level (keycode = %d) returned %d symbolsinstead of 1, using only the first one.\n", keycode, num);
282 if (xkb_keysym_get_name(syms[0], str, sizeof(str)) == -1)
283 errx(EXIT_FAILURE, "xkb_keysym_get_name(%u) failed", syms[0]);
284 const char *release = get_string("release");
286 char *modrep = (modifiers == NULL ? sstrdup("") : sstrdup(modifiers));
288 while ((comma = strchr(modrep, ',')) != NULL) {
291 sasprintf(&res, "bindsym %s%s%s %s%s\n", (modifiers == NULL ? "" : modrep), (modifiers == NULL ? "" : "+"), str, (release == NULL ? "" : release), get_string("command"));
298 /* See if we are jumping back to a state in which we were in previously
299 * (statelist contains INITIAL) and just move statelist_idx accordingly. */
300 for (int i = 0; i < statelist_idx; i++) {
301 if (statelist[i] != _next_state)
303 statelist_idx = i + 1;
307 /* Otherwise, the state is new and we add it to the list */
308 statelist[statelist_idx++] = _next_state;
312 static char *rewrite_binding(const char *input) {
316 const char *walk = input;
317 const size_t len = strlen(input);
319 const cmdp_token *token;
322 /* The "<=" operator is intentional: We also handle the terminating 0-byte
323 * explicitly by looking for an 'end' token. */
324 while ((size_t)(walk - input) <= len) {
325 /* Skip whitespace before every token, newlines are relevant since they
326 * separate configuration directives. */
327 while ((*walk == ' ' || *walk == '\t') && *walk != '\0')
330 //printf("remaining input: %s\n", walk);
332 cmdp_token_ptr *ptr = &(tokens[state]);
333 for (c = 0; c < ptr->n; c++) {
334 token = &(ptr->array[c]);
337 if (token->name[0] == '\'') {
338 if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) {
339 if (token->identifier != NULL)
340 push_string(token->identifier, token->name + 1);
341 walk += strlen(token->name) - 1;
342 if ((result = next_state(token)) != NULL)
349 if (strcmp(token->name, "number") == 0) {
350 /* Handle numbers. We only accept decimal numbers for now. */
353 long int num = strtol(walk, &end, 10);
354 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
355 (errno != 0 && num == 0))
358 /* No valid numbers found */
362 if (token->identifier != NULL)
363 push_long(token->identifier, num);
365 /* Set walk to the first non-number character */
367 if ((result = next_state(token)) != NULL)
372 if (strcmp(token->name, "string") == 0 ||
373 strcmp(token->name, "word") == 0) {
374 const char *beginning = walk;
375 /* Handle quoted strings (or words). */
379 while (*walk != '\0' && (*walk != '"' || *(walk - 1) == '\\'))
382 if (token->name[0] == 's') {
383 while (*walk != '\0' && *walk != '\r' && *walk != '\n')
386 /* For a word, the delimiters are white space (' ' or
387 * '\t'), closing square bracket (]), comma (,) and
389 while (*walk != ' ' && *walk != '\t' &&
390 *walk != ']' && *walk != ',' &&
391 *walk != ';' && *walk != '\r' &&
392 *walk != '\n' && *walk != '\0')
396 if (walk != beginning) {
397 char *str = scalloc(walk - beginning + 1, 1);
398 /* We copy manually to handle escaping of characters. */
400 for (inpos = 0, outpos = 0;
401 inpos < (walk - beginning);
403 /* We only handle escaped double quotes to not break
404 * backwards compatibility with people using \w in
405 * regular expressions etc. */
406 if (beginning[inpos] == '\\' && beginning[inpos + 1] == '"')
408 str[outpos] = beginning[inpos];
410 if (token->identifier)
411 push_string(token->identifier, str);
413 /* If we are at the end of a quoted string, skip the ending
417 if ((result = next_state(token)) != NULL)
423 if (strcmp(token->name, "end") == 0) {
424 //printf("checking for end: *%s*\n", walk);
425 if (*walk == '\0' || *walk == '\n' || *walk == '\r') {
426 if ((result = next_state(token)) != NULL)
428 /* To make sure we start with an appropriate matching
429 * datastructure for commands which do *not* specify any
430 * criteria, we re-initialize the criteria system after
432 // TODO: make this testable
444 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
447 void verboselog(char *fmt, ...) {
451 vfprintf(stdout, fmt, args);
455 void errorlog(char *fmt, ...) {
459 vfprintf(stderr, fmt, args);
463 void debuglog(char *fmt, ...) {
467 * Handles expose events, that is, draws the window contents.
470 static int handle_expose() {
471 /* re-draw the background */
472 xcb_rectangle_t border = {0, 0, logical_px(300), window_height()};
473 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){get_colorpixel("#000000")});
474 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
478 #define txt(x, row, text) \
479 draw_text_ascii(text, pixmap, pixmap_gc, \
480 x, row_y(row), logical_px(500) - x * 2)
482 if (current_step == STEP_WELCOME) {
483 /* restore font color */
484 set_font_colors(pixmap_gc, draw_util_hex_to_color("#FFFFFF"), draw_util_hex_to_color("#000000"));
486 txt(logical_px(10), 2, "You have not configured i3 yet.");
487 txt(logical_px(10), 3, "Do you want me to generate a config at");
490 sasprintf(&msg, "%s?", config_path);
491 txt(logical_px(10), 4, msg);
494 txt(logical_px(85), 6, "Yes, generate the config");
495 txt(logical_px(85), 8, "No, I will use the defaults");
498 set_font_colors(pixmap_gc, draw_util_hex_to_color("#00FF00"), draw_util_hex_to_color("#000000"));
499 txt(logical_px(25), 6, "<Enter>");
502 set_font_colors(pixmap_gc, draw_util_hex_to_color("#FF0000"), draw_util_hex_to_color("#000000"));
503 txt(logical_px(31), 8, "<ESC>");
506 if (current_step == STEP_GENERATE) {
507 set_font_colors(pixmap_gc, draw_util_hex_to_color("#FFFFFF"), draw_util_hex_to_color("#000000"));
509 txt(logical_px(10), 2, "Please choose either:");
510 txt(logical_px(85), 4, "Win as default modifier");
511 txt(logical_px(85), 5, "Alt as default modifier");
512 txt(logical_px(10), 7, "Afterwards, press");
513 txt(logical_px(85), 9, "to write the config");
514 txt(logical_px(85), 10, "to abort");
516 /* the not-selected modifier */
517 if (modifier == MOD_Mod4)
518 txt(logical_px(31), 5, "<Alt>");
520 txt(logical_px(31), 4, "<Win>");
522 /* the selected modifier */
523 set_font(&bold_font);
524 set_font_colors(pixmap_gc, draw_util_hex_to_color("#FFFFFF"), draw_util_hex_to_color("#000000"));
525 if (modifier == MOD_Mod4)
526 txt(logical_px(10), 4, "-> <Win>");
528 txt(logical_px(10), 5, "-> <Alt>");
532 set_font_colors(pixmap_gc, draw_util_hex_to_color("#00FF00"), draw_util_hex_to_color("#000000"));
533 txt(logical_px(25), 9, "<Enter>");
536 set_font_colors(pixmap_gc, draw_util_hex_to_color("#FF0000"), draw_util_hex_to_color("#000000"));
537 txt(logical_px(31), 10, "<ESC>");
540 /* Copy the contents of the pixmap to the real window */
541 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, logical_px(500), logical_px(500));
547 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
548 printf("Keypress %d, state raw = %d\n", event->detail, event->state);
550 /* Remove the numlock bit, all other bits are modifiers we can bind to */
551 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
552 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
553 * button masks are filtered out */
554 state_filtered &= 0xFF;
556 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, state_filtered);
558 printf("sym = %c (%d)\n", sym, sym);
560 if (sym == XK_Return || sym == XK_KP_Enter) {
561 if (current_step == STEP_WELCOME) {
562 current_step = STEP_GENERATE;
563 /* Set window title */
564 xcb_change_property(conn,
565 XCB_PROP_MODE_REPLACE,
570 strlen("i3: generate config"),
571 "i3: generate config");
577 /* Swap between modifiers when up or down is pressed. */
578 if (sym == XK_Up || sym == XK_Down) {
579 modifier = (modifier == MOD_Mod1) ? MOD_Mod4 : MOD_Mod1;
583 /* cancel any time */
584 if (sym == XK_Escape)
587 /* Check if this is Mod1 or Mod4. The modmap contains Shift, Lock, Control,
588 * Mod1, Mod2, Mod3, Mod4, Mod5 (in that order) */
589 xcb_keycode_t *modmap = xcb_get_modifier_mapping_keycodes(modmap_reply);
592 for (int i = 0; i < modmap_reply->keycodes_per_modifier; i++) {
593 xcb_keycode_t code = modmap[(mask * modmap_reply->keycodes_per_modifier) + i];
594 if (code == XCB_NONE)
596 printf("Modifier keycode for Mod1: 0x%02x\n", code);
597 if (code == event->detail) {
599 printf("This is Mod1!\n");
605 for (int i = 0; i < modmap_reply->keycodes_per_modifier; i++) {
606 xcb_keycode_t code = modmap[(mask * modmap_reply->keycodes_per_modifier) + i];
607 if (code == XCB_NONE)
609 printf("Modifier keycode for Mod4: 0x%02x\n", code);
610 if (code == event->detail) {
612 printf("This is Mod4!\n");
621 * Handle button presses to make clicking on "<win>" and "<alt>" work
624 static void handle_button_press(xcb_button_press_event_t *event) {
625 if (current_step != STEP_GENERATE)
628 if (event->event_x < logical_px(32) ||
629 event->event_x > (logical_px(32) + char_width * 5))
632 if (event->event_y >= row_y(4) && event->event_y <= (row_y(4) + font.height)) {
637 if (event->event_y >= row_y(5) && event->event_y <= (row_y(5) + font.height)) {
646 * Creates the config file and tells i3 to reload.
649 static void finish() {
650 printf("creating \"%s\"...\n", config_path);
652 struct xkb_context *xkb_context;
654 if ((xkb_context = xkb_context_new(0)) == NULL)
655 errx(1, "could not create xkbcommon context");
657 int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn);
658 if ((xkb_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL)
659 errx(1, "xkb_x11_keymap_new_from_device failed");
661 FILE *kc_config = fopen(SYSCONFDIR "/i3/config.keycodes", "r");
662 if (kc_config == NULL)
663 err(1, "Could not open input file \"%s\"", SYSCONFDIR "/i3/config.keycodes");
665 FILE *ks_config = fopen(config_path, "w");
666 if (ks_config == NULL)
667 err(1, "Could not open output config file \"%s\"", config_path);
675 bool head_of_file = true;
677 /* write a header about auto-generation to the output file */
678 fputs("# This file has been auto-generated by i3-config-wizard(1).\n", ks_config);
679 fputs("# It will not be overwritten, so edit it as you like.\n", ks_config);
680 fputs("#\n", ks_config);
681 fputs("# Should you change your keyboard layout some time, delete\n", ks_config);
682 fputs("# this file and re-run i3-config-wizard(1).\n", ks_config);
683 fputs("#\n", ks_config);
687 while ((buf = fgetln(kc_config, &len)) != NULL) {
688 /* fgetln does not return null-terminated strings */
690 sasprintf(&line, "%.*s", len, buf);
693 while ((read = getline(&line, &linecap, kc_config)) != -1) {
696 /* skip the warning block at the beginning of the input file */
698 strncmp("# WARNING", line, strlen("# WARNING")) == 0)
701 head_of_file = false;
703 /* Skip leading whitespace */
705 while (isspace(*walk) && walk < (line + len)) {
706 /* Pre-output the skipped whitespaces to keep proper indentation */
707 fputc(*walk, ks_config);
711 /* Set the modifier the user chose */
712 if (strncmp(walk, "set $mod ", strlen("set $mod ")) == 0) {
713 if (modifier == MOD_Mod1)
714 fputs("set $mod Mod1\n", ks_config);
716 fputs("set $mod Mod4\n", ks_config);
720 /* Check for 'bindcode'. If it’s not a bindcode line, we
721 * just copy it to the output file */
722 if (strncmp(walk, "bindcode", strlen("bindcode")) != 0) {
723 fputs(walk, ks_config);
726 char *result = rewrite_binding(walk);
727 fputs(result, ks_config);
731 /* sync to do our best in order to have the file really stored on disk */
733 fsync(fileno(ks_config));
742 /* tell i3 to reload the config file */
743 int sockfd = ipc_connect(socket_path);
744 ipc_send_message(sockfd, strlen("reload"), 0, (uint8_t *)"reload");
750 int main(int argc, char *argv[]) {
751 char *xdg_config_home;
752 socket_path = getenv("I3SOCK");
753 char *pattern = "pango:monospace 8";
754 char *patternbold = "pango:monospace bold 8";
755 int o, option_index = 0;
757 static struct option long_options[] = {
758 {"socket", required_argument, 0, 's'},
759 {"version", no_argument, 0, 'v'},
760 {"limit", required_argument, 0, 'l'},
761 {"prompt", required_argument, 0, 'P'},
762 {"prefix", required_argument, 0, 'p'},
763 {"font", required_argument, 0, 'f'},
764 {"help", no_argument, 0, 'h'},
767 char *options_string = "s:vh";
769 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
773 socket_path = sstrdup(optarg);
776 printf("i3-config-wizard " I3_VERSION "\n");
779 printf("i3-config-wizard " I3_VERSION "\n");
780 printf("i3-config-wizard [-s <socket>] [-v]\n");
785 char *path = get_config_path(NULL, false);
787 printf("The config file \"%s\" already exists. Exiting.\n", path);
792 /* Always write to $XDG_CONFIG_HOME/i3/config by default. */
793 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
794 xdg_config_home = "~/.config";
796 xdg_config_home = resolve_tilde(xdg_config_home);
797 sasprintf(&config_path, "%s/i3/config", xdg_config_home);
799 /* Create $XDG_CONFIG_HOME/i3 if it does not yet exist */
802 sasprintf(&config_dir, "%s/i3", xdg_config_home);
803 if (stat(config_dir, &stbuf) != 0)
804 if (mkdirp(config_dir, DEFAULT_DIR_MODE) != 0)
805 err(EXIT_FAILURE, "mkdirp(%s) failed", config_dir);
807 free(xdg_config_home);
810 if ((fd = open(config_path, O_CREAT | O_RDWR, 0644)) == -1) {
811 printf("Cannot open file \"%s\" for writing: %s. Exiting.\n", config_path, strerror(errno));
818 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
819 xcb_connection_has_error(conn))
820 errx(1, "Cannot open display\n");
822 if (xkb_x11_setup_xkb_extension(conn,
823 XKB_X11_MIN_MAJOR_XKB_VERSION,
824 XKB_X11_MIN_MINOR_XKB_VERSION,
829 &xkb_base_error) != 1)
830 errx(EXIT_FAILURE, "Could not setup XKB extension.");
832 if (socket_path == NULL)
833 socket_path = root_atom_contents("I3_SOCKET_PATH", conn, screen);
835 if (socket_path == NULL)
836 socket_path = "/tmp/i3-ipc.sock";
838 keysyms = xcb_key_symbols_alloc(conn);
839 xcb_get_modifier_mapping_cookie_t modmap_cookie;
840 modmap_cookie = xcb_get_modifier_mapping(conn);
841 symbols = xcb_key_symbols_alloc(conn);
843 /* Place requests for the atoms we need as soon as possible */
844 #define xmacro(atom) \
845 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
846 #include "atoms.xmacro"
849 root_screen = xcb_aux_get_screen(conn, screen);
850 root = root_screen->root;
852 if (!(modmap_reply = xcb_get_modifier_mapping_reply(conn, modmap_cookie, NULL)))
853 errx(EXIT_FAILURE, "Could not get modifier mapping\n");
855 xcb_numlock_mask = get_mod_mask_for(XCB_NUM_LOCK, symbols, modmap_reply);
858 font = load_font(pattern, true);
859 bold_font = load_font(patternbold, true);
861 /* Determine character width in the default font. */
863 char_width = predict_text_width(i3string_from_utf8("a"));
865 /* Open an input window */
866 win = xcb_generate_id(conn);
869 XCB_COPY_FROM_PARENT,
870 win, /* the window id */
871 root, /* parent == root */
872 logical_px(490), logical_px(297), logical_px(300), window_height(), /* dimensions */
873 0, /* X11 border = 0, we draw our own */
874 XCB_WINDOW_CLASS_INPUT_OUTPUT,
875 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
876 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
878 0, /* back pixel: black */
879 XCB_EVENT_MASK_EXPOSURE |
880 XCB_EVENT_MASK_BUTTON_PRESS});
882 /* Map the window (make it visible) */
883 xcb_map_window(conn, win);
885 /* Setup NetWM atoms */
886 #define xmacro(name) \
888 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
890 errx(EXIT_FAILURE, "Could not get atom " #name "\n"); \
892 A_##name = reply->atom; \
895 #include "atoms.xmacro"
899 xcb_change_property(conn,
900 XCB_PROP_MODE_REPLACE,
902 A__NET_WM_WINDOW_TYPE,
906 (unsigned char *)&A__NET_WM_WINDOW_TYPE_DIALOG);
908 /* Set window title */
909 xcb_change_property(conn,
910 XCB_PROP_MODE_REPLACE,
915 strlen("i3: first configuration"),
916 "i3: first configuration");
919 pixmap = xcb_generate_id(conn);
920 pixmap_gc = xcb_generate_id(conn);
921 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, logical_px(500), logical_px(500));
922 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
924 /* Grab the keyboard to get all input */
927 /* Try (repeatedly, if necessary) to grab the keyboard. We might not
928 * get the keyboard at the first attempt because of the keybinding
929 * still being active when started via a wm’s keybinding. */
930 xcb_grab_keyboard_cookie_t cookie;
931 xcb_grab_keyboard_reply_t *reply = NULL;
934 while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
935 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
936 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
940 if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
941 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
947 xcb_generic_event_t *event;
948 while ((event = xcb_wait_for_event(conn)) != NULL) {
949 if (event->response_type == 0) {
950 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
954 /* Strip off the highest bit (set if the event is generated) */
955 int type = (event->response_type & 0x7F);
959 handle_key_press(NULL, conn, (xcb_key_press_event_t *)event);
962 /* TODO: handle mappingnotify */
964 case XCB_BUTTON_PRESS:
965 handle_button_press((xcb_button_press_event_t *)event);