2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7 * i3-config-wizard: Program to convert configs using keycodes to configs using
11 #if defined(__FreeBSD__)
12 #include <sys/param.h>
15 /* For systems without getline, fall back to fgetln */
16 #if defined(__APPLE__)
18 #elif defined(__FreeBSD__)
19 /* Defining this macro before including stdio.h is necessary in order to have
20 * a prototype for getline in FreeBSD. */
25 #include <sys/types.h>
42 #include <xcb/xcb_aux.h>
43 #include <xcb/xcb_event.h>
44 #include <xcb/xcb_keysyms.h>
46 #include <xkbcommon/xkbcommon.h>
47 #include <xkbcommon/xkbcommon-x11.h>
50 #include <X11/keysym.h>
51 #include <X11/XKBlib.h>
53 /* We need SYSCONFDIR for the path to the keycode config template, so raise an
54 * error if it’s not defined for whatever reason */
56 #error "SYSCONFDIR not defined"
59 #define FREE(pointer) \
61 if (pointer != NULL) { \
71 (((row)-1) * font.height + logical_px(4))
72 #define window_height() \
73 (row_y(15) + font.height)
76 STEP_GENERATE } current_step = STEP_WELCOME;
78 MOD_Mod4 } modifier = MOD_Mod4;
80 static char *config_path;
81 static uint32_t xcb_numlock_mask;
82 xcb_connection_t *conn;
83 static xcb_key_symbols_t *keysyms;
84 xcb_screen_t *root_screen;
85 static xcb_get_modifier_mapping_reply_t *modmap_reply;
87 static i3Font bold_font;
88 static int char_width;
89 static char *socket_path;
90 static xcb_window_t win;
91 static xcb_pixmap_t pixmap;
92 static xcb_gcontext_t pixmap_gc;
93 static xcb_key_symbols_t *symbols;
95 static struct xkb_keymap *xkb_keymap;
96 static uint8_t xkb_base_event;
97 static uint8_t xkb_base_error;
101 #include "GENERATED_config_enums.h"
103 typedef struct token {
106 /* This might be __CALL */
107 cmdp_state next_state;
109 uint16_t call_identifier;
113 typedef struct tokenptr {
118 #include "GENERATED_config_tokens.h"
120 static cmdp_state state;
121 /* A list which contains the states that lead to the current state, e.g.
122 * INITIAL, WORKSPACE_LAYOUT.
123 * When jumping back to INITIAL, statelist_idx will simply be set to 1
124 * (likewise for other states, e.g. MODE or BAR).
125 * This list is used to process the nearest error token. */
126 static cmdp_state statelist[10] = {INITIAL};
127 /* NB: statelist_idx points to where the next entry will be inserted */
128 static int statelist_idx = 1;
131 /* Just a pointer, not dynamically allocated. */
132 const char *identifier;
143 /* 10 entries should be enough for everybody. */
144 static struct stack_entry stack[10];
147 * Pushes a string (identified by 'identifier') on the stack. We simply use a
148 * single array, since the number of entries we have to store is very small.
151 static void push_string(const char *identifier, const char *str) {
152 for (int c = 0; c < 10; c++) {
153 if (stack[c].identifier != NULL &&
154 strcmp(stack[c].identifier, identifier) != 0)
156 if (stack[c].identifier == NULL) {
157 /* Found a free slot, let’s store it here. */
158 stack[c].identifier = identifier;
159 stack[c].val.str = sstrdup(str);
160 stack[c].type = STACK_STR;
162 /* Append the value. */
163 char *prev = stack[c].val.str;
164 sasprintf(&(stack[c].val.str), "%s,%s", prev, str);
170 /* When we arrive here, the stack is full. This should not happen and
171 * means there’s either a bug in this parser or the specification
172 * contains a command with more than 10 identified tokens. */
173 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
174 "in the code, or a new command which contains more than "
175 "10 identified tokens.\n");
179 static void push_long(const char *identifier, long num) {
180 for (int c = 0; c < 10; c++) {
181 if (stack[c].identifier != NULL)
183 /* Found a free slot, let’s store it here. */
184 stack[c].identifier = identifier;
185 stack[c].val.num = num;
186 stack[c].type = STACK_LONG;
190 /* When we arrive here, the stack is full. This should not happen and
191 * means there’s either a bug in this parser or the specification
192 * contains a command with more than 10 identified tokens. */
193 fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
194 "in the code, or a new command which contains more than "
195 "10 identified tokens.\n");
199 static const char *get_string(const char *identifier) {
200 for (int c = 0; c < 10; c++) {
201 if (stack[c].identifier == NULL)
203 if (strcmp(identifier, stack[c].identifier) == 0)
204 return stack[c].val.str;
209 static void clear_stack(void) {
210 for (int c = 0; c < 10; c++) {
211 if (stack[c].type == STACK_STR && stack[c].val.str != NULL)
212 free(stack[c].val.str);
213 stack[c].identifier = NULL;
214 stack[c].val.str = NULL;
215 stack[c].val.num = 0;
220 * Returns true if sym is bound to any key except for 'except_keycode' on the
221 * first four layers (normal, shift, mode_switch, mode_switch + shift).
224 static bool keysym_used_on_other_key(KeySym sym, xcb_keycode_t except_keycode) {
226 min_keycode = xcb_get_setup(conn)->min_keycode,
227 max_keycode = xcb_get_setup(conn)->max_keycode;
229 for (i = min_keycode; i && i <= max_keycode; i++) {
230 if (i == except_keycode)
232 for (int level = 0; level < 4; level++) {
233 if (xcb_key_symbols_get_keysym(keysyms, i, level) != sym)
241 static char *next_state(const cmdp_token *token) {
242 cmdp_state _next_state = token->next_state;
244 if (token->next_state == __CALL) {
245 const char *modifiers = get_string("modifiers");
246 int keycode = atoi(get_string("key"));
248 if (modifiers != NULL &&
249 strstr(modifiers, "Shift") != NULL) {
250 /* When shift is included, we really need to use the second-level
251 * symbol (upper-case). The lower-case symbol could be on a
252 * different key than the upper-case one (unlikely for letters, but
253 * more likely for special characters). */
256 /* Try to use the keysym on the first level (lower-case). In case
257 * this doesn’t make it ambiguous (think of a keyboard layout
258 * having '1' on two different keys, but '!' only on keycode 10),
259 * we’ll stick with the keysym of the first level.
261 * This reduces a lot of confusion for users who switch keyboard
262 * layouts from qwerty to qwertz or other slight variations of
263 * qwerty (yes, that happens quite often). */
264 const xkb_keysym_t *syms;
265 int num = xkb_keymap_key_get_syms_by_level(xkb_keymap, keycode, 0, 0, &syms);
267 errx(1, "xkb_keymap_key_get_syms_by_level returned no symbols for keycode %d", keycode);
268 if (!keysym_used_on_other_key(syms[0], keycode))
272 const xkb_keysym_t *syms;
273 int num = xkb_keymap_key_get_syms_by_level(xkb_keymap, keycode, 0, level, &syms);
275 errx(1, "xkb_keymap_key_get_syms_by_level returned no symbols for keycode %d", keycode);
277 printf("xkb_keymap_key_get_syms_by_level (keycode = %d) returned %d symbolsinstead of 1, using only the first one.\n", keycode, num);
280 if (xkb_keysym_get_name(syms[0], str, sizeof(str)) == -1)
281 errx(EXIT_FAILURE, "xkb_keysym_get_name(%u) failed", syms[0]);
282 const char *release = get_string("release");
284 char *modrep = (modifiers == NULL ? sstrdup("") : sstrdup(modifiers));
286 while ((comma = strchr(modrep, ',')) != NULL) {
289 sasprintf(&res, "bindsym %s%s%s %s%s\n", (modifiers == NULL ? "" : modrep), (modifiers == NULL ? "" : "+"), str, (release == NULL ? "" : release), get_string("command"));
296 /* See if we are jumping back to a state in which we were in previously
297 * (statelist contains INITIAL) and just move statelist_idx accordingly. */
298 for (int i = 0; i < statelist_idx; i++) {
299 if (statelist[i] != _next_state)
301 statelist_idx = i + 1;
305 /* Otherwise, the state is new and we add it to the list */
306 statelist[statelist_idx++] = _next_state;
310 static char *rewrite_binding(const char *input) {
314 const char *walk = input;
315 const size_t len = strlen(input);
317 const cmdp_token *token;
320 /* The "<=" operator is intentional: We also handle the terminating 0-byte
321 * explicitly by looking for an 'end' token. */
322 while ((size_t)(walk - input) <= len) {
323 /* Skip whitespace before every token, newlines are relevant since they
324 * separate configuration directives. */
325 while ((*walk == ' ' || *walk == '\t') && *walk != '\0')
328 //printf("remaining input: %s\n", walk);
330 cmdp_token_ptr *ptr = &(tokens[state]);
331 for (c = 0; c < ptr->n; c++) {
332 token = &(ptr->array[c]);
335 if (token->name[0] == '\'') {
336 if (strncasecmp(walk, token->name + 1, strlen(token->name) - 1) == 0) {
337 if (token->identifier != NULL)
338 push_string(token->identifier, token->name + 1);
339 walk += strlen(token->name) - 1;
340 if ((result = next_state(token)) != NULL)
347 if (strcmp(token->name, "number") == 0) {
348 /* Handle numbers. We only accept decimal numbers for now. */
351 long int num = strtol(walk, &end, 10);
352 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
353 (errno != 0 && num == 0))
356 /* No valid numbers found */
360 if (token->identifier != NULL)
361 push_long(token->identifier, num);
363 /* Set walk to the first non-number character */
365 if ((result = next_state(token)) != NULL)
370 if (strcmp(token->name, "string") == 0 ||
371 strcmp(token->name, "word") == 0) {
372 const char *beginning = walk;
373 /* Handle quoted strings (or words). */
377 while (*walk != '\0' && (*walk != '"' || *(walk - 1) == '\\'))
380 if (token->name[0] == 's') {
381 while (*walk != '\0' && *walk != '\r' && *walk != '\n')
384 /* For a word, the delimiters are white space (' ' or
385 * '\t'), closing square bracket (]), comma (,) and
387 while (*walk != ' ' && *walk != '\t' &&
388 *walk != ']' && *walk != ',' &&
389 *walk != ';' && *walk != '\r' &&
390 *walk != '\n' && *walk != '\0')
394 if (walk != beginning) {
395 char *str = scalloc(walk - beginning + 1);
396 /* We copy manually to handle escaping of characters. */
398 for (inpos = 0, outpos = 0;
399 inpos < (walk - beginning);
401 /* We only handle escaped double quotes to not break
402 * backwards compatibility with people using \w in
403 * regular expressions etc. */
404 if (beginning[inpos] == '\\' && beginning[inpos + 1] == '"')
406 str[outpos] = beginning[inpos];
408 if (token->identifier)
409 push_string(token->identifier, str);
411 /* If we are at the end of a quoted string, skip the ending
415 if ((result = next_state(token)) != NULL)
421 if (strcmp(token->name, "end") == 0) {
422 //printf("checking for end: *%s*\n", walk);
423 if (*walk == '\0' || *walk == '\n' || *walk == '\r') {
424 if ((result = next_state(token)) != NULL)
426 /* To make sure we start with an appropriate matching
427 * datastructure for commands which do *not* specify any
428 * criteria, we re-initialize the criteria system after
430 // TODO: make this testable
442 * Having verboselog(), errorlog() and debuglog() is necessary when using libi3.
445 void verboselog(char *fmt, ...) {
449 vfprintf(stdout, fmt, args);
453 void errorlog(char *fmt, ...) {
457 vfprintf(stderr, fmt, args);
461 void debuglog(char *fmt, ...) {
465 * This function resolves ~ in pathnames.
466 * It may resolve wildcards in the first part of the path, but if no match
467 * or multiple matches are found, it just returns a copy of path as given.
470 static char *resolve_tilde(const char *path) {
471 static glob_t globbuf;
472 char *head, *tail, *result;
474 tail = strchr(path, '/');
475 head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
477 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
479 /* no match, or many wildcard matches are bad */
480 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
481 result = strdup(path);
483 err(1, "glob() failed");
485 head = globbuf.gl_pathv[0];
486 result = calloc(1, strlen(head) + (tail ? strlen(tail) : 0) + 1);
487 strncpy(result, head, strlen(head));
489 strncat(result, tail, strlen(tail));
497 * Handles expose events, that is, draws the window contents.
500 static int handle_expose() {
501 /* re-draw the background */
502 xcb_rectangle_t border = {0, 0, logical_px(300), window_height()};
503 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){get_colorpixel("#000000")});
504 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
508 #define txt(x, row, text) \
509 draw_text_ascii(text, pixmap, pixmap_gc, \
510 x, row_y(row), logical_px(500) - x * 2)
512 if (current_step == STEP_WELCOME) {
513 /* restore font color */
514 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
516 txt(logical_px(10), 2, "You have not configured i3 yet.");
517 txt(logical_px(10), 3, "Do you want me to generate ~/.i3/config?");
518 txt(logical_px(85), 5, "Yes, generate ~/.i3/config");
519 txt(logical_px(85), 7, "No, I will use the defaults");
522 set_font_colors(pixmap_gc, get_colorpixel("#00FF00"), get_colorpixel("#000000"));
523 txt(logical_px(25), 5, "<Enter>");
526 set_font_colors(pixmap_gc, get_colorpixel("#FF0000"), get_colorpixel("#000000"));
527 txt(logical_px(31), 7, "<ESC>");
530 if (current_step == STEP_GENERATE) {
531 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
533 txt(logical_px(10), 2, "Please choose either:");
534 txt(logical_px(85), 4, "Win as default modifier");
535 txt(logical_px(85), 5, "Alt as default modifier");
536 txt(logical_px(10), 7, "Afterwards, press");
537 txt(logical_px(85), 9, "to write ~/.i3/config");
538 txt(logical_px(85), 10, "to abort");
540 /* the not-selected modifier */
541 if (modifier == MOD_Mod4)
542 txt(logical_px(31), 5, "<Alt>");
544 txt(logical_px(31), 4, "<Win>");
546 /* the selected modifier */
547 set_font(&bold_font);
548 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
549 if (modifier == MOD_Mod4)
550 txt(logical_px(10), 4, "-> <Win>");
552 txt(logical_px(10), 5, "-> <Alt>");
556 set_font_colors(pixmap_gc, get_colorpixel("#00FF00"), get_colorpixel("#000000"));
557 txt(logical_px(25), 9, "<Enter>");
560 set_font_colors(pixmap_gc, get_colorpixel("#FF0000"), get_colorpixel("#000000"));
561 txt(logical_px(31), 10, "<ESC>");
564 /* Copy the contents of the pixmap to the real window */
565 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, logical_px(500), logical_px(500));
571 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
572 printf("Keypress %d, state raw = %d\n", event->detail, event->state);
574 /* Remove the numlock bit, all other bits are modifiers we can bind to */
575 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
576 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
577 * button masks are filtered out */
578 state_filtered &= 0xFF;
580 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, state_filtered);
582 printf("sym = %c (%d)\n", sym, sym);
584 if (sym == XK_Return || sym == XK_KP_Enter) {
585 if (current_step == STEP_WELCOME) {
586 current_step = STEP_GENERATE;
587 /* Set window title */
588 xcb_change_property(conn,
589 XCB_PROP_MODE_REPLACE,
594 strlen("i3: generate config"),
595 "i3: generate config");
601 /* Swap between modifiers when up or down is pressed. */
602 if (sym == XK_Up || sym == XK_Down) {
603 modifier = (modifier == MOD_Mod1) ? MOD_Mod4 : MOD_Mod1;
607 /* cancel any time */
608 if (sym == XK_Escape)
611 /* Check if this is Mod1 or Mod4. The modmap contains Shift, Lock, Control,
612 * Mod1, Mod2, Mod3, Mod4, Mod5 (in that order) */
613 xcb_keycode_t *modmap = xcb_get_modifier_mapping_keycodes(modmap_reply);
616 for (int i = 0; i < modmap_reply->keycodes_per_modifier; i++) {
617 xcb_keycode_t code = modmap[(mask * modmap_reply->keycodes_per_modifier) + i];
618 if (code == XCB_NONE)
620 printf("Modifier keycode for Mod1: 0x%02x\n", code);
621 if (code == event->detail) {
623 printf("This is Mod1!\n");
629 for (int i = 0; i < modmap_reply->keycodes_per_modifier; i++) {
630 xcb_keycode_t code = modmap[(mask * modmap_reply->keycodes_per_modifier) + i];
631 if (code == XCB_NONE)
633 printf("Modifier keycode for Mod4: 0x%02x\n", code);
634 if (code == event->detail) {
636 printf("This is Mod4!\n");
645 * Handle button presses to make clicking on "<win>" and "<alt>" work
648 static void handle_button_press(xcb_button_press_event_t *event) {
649 if (current_step != STEP_GENERATE)
652 if (event->event_x < logical_px(32) ||
653 event->event_x > (logical_px(32) + char_width * 5))
656 if (event->event_y >= row_y(4) && event->event_y <= (row_y(4) + font.height)) {
661 if (event->event_y >= row_y(5) && event->event_y <= (row_y(5) + font.height)) {
670 * Creates the config file and tells i3 to reload.
673 static void finish() {
674 printf("creating \"%s\"...\n", config_path);
676 struct xkb_context *xkb_context;
678 if ((xkb_context = xkb_context_new(0)) == NULL)
679 errx(1, "could not create xkbcommon context");
681 int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn);
682 if ((xkb_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL)
683 errx(1, "xkb_x11_keymap_new_from_device failed");
685 FILE *kc_config = fopen(SYSCONFDIR "/i3/config.keycodes", "r");
686 if (kc_config == NULL)
687 err(1, "Could not open input file \"%s\"", SYSCONFDIR "/i3/config.keycodes");
689 FILE *ks_config = fopen(config_path, "w");
690 if (ks_config == NULL)
691 err(1, "Could not open output config file \"%s\"", config_path);
699 bool head_of_file = true;
701 /* write a header about auto-generation to the output file */
702 fputs("# This file has been auto-generated by i3-config-wizard(1).\n", ks_config);
703 fputs("# It will not be overwritten, so edit it as you like.\n", ks_config);
704 fputs("#\n", ks_config);
705 fputs("# Should you change your keyboard layout some time, delete\n", ks_config);
706 fputs("# this file and re-run i3-config-wizard(1).\n", ks_config);
707 fputs("#\n", ks_config);
711 while ((buf = fgetln(kc_config, &len)) != NULL) {
712 /* fgetln does not return null-terminated strings */
714 sasprintf(&line, "%.*s", len, buf);
717 while ((read = getline(&line, &linecap, kc_config)) != -1) {
720 /* skip the warning block at the beginning of the input file */
722 strncmp("# WARNING", line, strlen("# WARNING")) == 0)
725 head_of_file = false;
727 /* Skip leading whitespace */
729 while (isspace(*walk) && walk < (line + len)) {
730 /* Pre-output the skipped whitespaces to keep proper indentation */
731 fputc(*walk, ks_config);
735 /* Set the modifier the user chose */
736 if (strncmp(walk, "set $mod ", strlen("set $mod ")) == 0) {
737 if (modifier == MOD_Mod1)
738 fputs("set $mod Mod1\n", ks_config);
740 fputs("set $mod Mod4\n", ks_config);
744 /* Check for 'bindcode'. If it’s not a bindcode line, we
745 * just copy it to the output file */
746 if (strncmp(walk, "bindcode", strlen("bindcode")) != 0) {
747 fputs(walk, ks_config);
750 char *result = rewrite_binding(walk);
751 fputs(result, ks_config);
755 /* sync to do our best in order to have the file really stored on disk */
757 fsync(fileno(ks_config));
766 /* tell i3 to reload the config file */
767 int sockfd = ipc_connect(socket_path);
768 ipc_send_message(sockfd, strlen("reload"), 0, (uint8_t *)"reload");
774 int main(int argc, char *argv[]) {
775 config_path = resolve_tilde("~/.i3/config");
776 socket_path = getenv("I3SOCK");
777 char *pattern = "pango:monospace 8";
778 char *patternbold = "pango:monospace bold 8";
779 int o, option_index = 0;
781 static struct option long_options[] = {
782 {"socket", required_argument, 0, 's'},
783 {"version", no_argument, 0, 'v'},
784 {"limit", required_argument, 0, 'l'},
785 {"prompt", required_argument, 0, 'P'},
786 {"prefix", required_argument, 0, 'p'},
787 {"font", required_argument, 0, 'f'},
788 {"help", no_argument, 0, 'h'},
791 char *options_string = "s:vh";
793 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
797 socket_path = strdup(optarg);
800 printf("i3-config-wizard " I3_VERSION "\n");
803 printf("i3-config-wizard " I3_VERSION "\n");
804 printf("i3-config-wizard [-s <socket>] [-v]\n");
809 /* Check if the destination config file does not exist but the path is
810 * writable. If not, exit now, this program is not useful in that case. */
812 if (stat(config_path, &stbuf) == 0) {
813 printf("The config file \"%s\" already exists. Exiting.\n", config_path);
817 /* Create ~/.i3 if it does not yet exist */
818 char *config_dir = resolve_tilde("~/.i3");
819 if (stat(config_dir, &stbuf) != 0)
820 if (mkdir(config_dir, 0755) == -1)
821 err(1, "mkdir(%s) failed", config_dir);
825 if ((fd = open(config_path, O_CREAT | O_RDWR, 0644)) == -1) {
826 printf("Cannot open file \"%s\" for writing: %s. Exiting.\n", config_path, strerror(errno));
833 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
834 xcb_connection_has_error(conn))
835 errx(1, "Cannot open display\n");
837 if (xkb_x11_setup_xkb_extension(conn,
838 XKB_X11_MIN_MAJOR_XKB_VERSION,
839 XKB_X11_MIN_MINOR_XKB_VERSION,
844 &xkb_base_error) != 1)
845 errx(EXIT_FAILURE, "Could not setup XKB extension.");
847 if (socket_path == NULL)
848 socket_path = root_atom_contents("I3_SOCKET_PATH", conn, screen);
850 if (socket_path == NULL)
851 socket_path = "/tmp/i3-ipc.sock";
853 keysyms = xcb_key_symbols_alloc(conn);
854 xcb_get_modifier_mapping_cookie_t modmap_cookie;
855 modmap_cookie = xcb_get_modifier_mapping(conn);
856 symbols = xcb_key_symbols_alloc(conn);
858 /* Place requests for the atoms we need as soon as possible */
859 #define xmacro(atom) \
860 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
861 #include "atoms.xmacro"
864 root_screen = xcb_aux_get_screen(conn, screen);
865 root = root_screen->root;
867 if (!(modmap_reply = xcb_get_modifier_mapping_reply(conn, modmap_cookie, NULL)))
868 errx(EXIT_FAILURE, "Could not get modifier mapping\n");
870 xcb_numlock_mask = get_mod_mask_for(XCB_NUM_LOCK, symbols, modmap_reply);
872 font = load_font(pattern, true);
873 bold_font = load_font(patternbold, true);
875 /* Determine character width in the default font. */
877 char_width = predict_text_width(i3string_from_utf8("a"));
879 /* Open an input window */
880 win = xcb_generate_id(conn);
883 XCB_COPY_FROM_PARENT,
884 win, /* the window id */
885 root, /* parent == root */
886 logical_px(490), logical_px(297), logical_px(300), window_height(), /* dimensions */
887 0, /* X11 border = 0, we draw our own */
888 XCB_WINDOW_CLASS_INPUT_OUTPUT,
889 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
890 XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
892 0, /* back pixel: black */
893 XCB_EVENT_MASK_EXPOSURE |
894 XCB_EVENT_MASK_BUTTON_PRESS});
896 /* Map the window (make it visible) */
897 xcb_map_window(conn, win);
899 /* Setup NetWM atoms */
900 #define xmacro(name) \
902 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
904 errx(EXIT_FAILURE, "Could not get atom " #name "\n"); \
906 A_##name = reply->atom; \
909 #include "atoms.xmacro"
913 xcb_change_property(conn,
914 XCB_PROP_MODE_REPLACE,
916 A__NET_WM_WINDOW_TYPE,
920 (unsigned char *)&A__NET_WM_WINDOW_TYPE_DIALOG);
922 /* Set window title */
923 xcb_change_property(conn,
924 XCB_PROP_MODE_REPLACE,
929 strlen("i3: first configuration"),
930 "i3: first configuration");
933 pixmap = xcb_generate_id(conn);
934 pixmap_gc = xcb_generate_id(conn);
935 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, logical_px(500), logical_px(500));
936 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
938 /* Grab the keyboard to get all input */
941 /* Try (repeatedly, if necessary) to grab the keyboard. We might not
942 * get the keyboard at the first attempt because of the keybinding
943 * still being active when started via a wm’s keybinding. */
944 xcb_grab_keyboard_cookie_t cookie;
945 xcb_grab_keyboard_reply_t *reply = NULL;
948 while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
949 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
950 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
954 if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
955 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
961 xcb_generic_event_t *event;
962 while ((event = xcb_wait_for_event(conn)) != NULL) {
963 if (event->response_type == 0) {
964 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
968 /* Strip off the highest bit (set if the event is generated) */
969 int type = (event->response_type & 0x7F);
973 handle_key_press(NULL, conn, (xcb_key_press_event_t *)event);
976 /* TODO: handle mappingnotify */
978 case XCB_BUTTON_PRESS:
979 handle_button_press((xcb_button_press_event_t *)event);