From 2491a155ee9ddc6dc52c0d9904c05e67facb2bea Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 1 May 2011 00:26:10 +0200 Subject: [PATCH] initial commit of the i3-config-wizard (GUI working, functionality incomplete) --- .gitignore | 8 +- i3-config-wizard/Makefile | 39 ++++ i3-config-wizard/cfgparse.l | 104 +++++++++++ i3-config-wizard/cfgparse.y | 153 ++++++++++++++++ i3-config-wizard/main.c | 355 ++++++++++++++++++++++++++++++++++++ i3-config-wizard/xcb.c | 166 +++++++++++++++++ i3-config-wizard/xcb.h | 6 + 7 files changed, 827 insertions(+), 4 deletions(-) create mode 100644 i3-config-wizard/Makefile create mode 100644 i3-config-wizard/cfgparse.l create mode 100644 i3-config-wizard/cfgparse.y create mode 100644 i3-config-wizard/main.c create mode 100644 i3-config-wizard/xcb.c create mode 100644 i3-config-wizard/xcb.h diff --git a/.gitignore b/.gitignore index e3034ec3..d1555b0d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ loglevels.tmp *.swp testcases/testsuite-* testcases/latest -src/*.output -src/*.tab.c -src/*.tab.h -src/*.yy.c +*.output +*.tab.c +*.tab.h +*.yy.c diff --git a/i3-config-wizard/Makefile b/i3-config-wizard/Makefile new file mode 100644 index 00000000..44715766 --- /dev/null +++ b/i3-config-wizard/Makefile @@ -0,0 +1,39 @@ +# Default value so one can compile i3-input standalone +TOPDIR=.. + +include $(TOPDIR)/common.mk + +# Depend on the object files of all source-files in src/*.c and on all header files +FILES:=$(patsubst %.c,%.o,$(wildcard *.c)) +HEADERS:=$(wildcard *.h) + +# Depend on the specific file (.c for each .o) and on all headers +%.o: %.c ${HEADERS} + echo "CC $<" + $(CC) $(CFLAGS) -c -o $@ $< + +all: cfgparse.y.o cfgparse.yy.o ${FILES} + echo "LINK i3-config-wizard" + $(CC) -o i3-config-wizard ${FILES} $(LDFLAGS) + +cfgparse.yy.o: cfgparse.l cfgparse.y.o ${HEADERS} + echo "LEX $<" + flex -i -o$(@:.o=.c) $< + $(CC) $(CFLAGS) -c -o $@ $(@:.o=.c) + +cfgparse.y.o: cfgparse.y ${HEADERS} + echo "YACC $<" + bison --debug --verbose -b $(basename $< .y) -d $< + $(CC) $(CFLAGS) -c -o $@ $(<:.y=.tab.c) + + +install: all + echo "INSTALL" + $(INSTALL) -d -m 0755 $(DESTDIR)$(PREFIX)/bin + $(INSTALL) -m 0755 i3-config-wizard $(DESTDIR)$(PREFIX)/bin/ + +clean: + rm -f *.o + +distclean: clean + rm -f i3-config-wizard diff --git a/i3-config-wizard/cfgparse.l b/i3-config-wizard/cfgparse.l new file mode 100644 index 00000000..d94abe2a --- /dev/null +++ b/i3-config-wizard/cfgparse.l @@ -0,0 +1,104 @@ +%option nounput +%option noinput +%option noyy_top_state +%option stack + +%{ +/* + * vim:ts=8:expandtab + * + */ +#include +#include +#include +#include +#include "cfgparse.tab.h" + +int yycolumn = 1; + +struct context { + int line_number; + char *line_copy; + + char *compact_error; + + /* These are the same as in YYLTYPE */ + int first_column; + int last_column; +}; + + +#define YY_DECL int yylex (struct context *context) + +#define YY_USER_ACTION { \ + context->first_column = yycolumn; \ + context->last_column = yycolumn+yyleng-1; \ + yycolumn += yyleng; \ +} + +%} + +EOL (\r?\n) + +%s BINDCODE_COND +%s BIND_AWS_COND +%s BIND_A2WS_COND +%x BUFFER_LINE + +%% + + { + /* This is called when a new line is lexed. We only want the + * first line to match to go into state BUFFER_LINE */ + if (context->line_number == 0) { + context->line_number = 1; + BEGIN(INITIAL); + yy_push_state(BUFFER_LINE); + } + } + +^[^\r\n]*/{EOL}? { + /* save whole line */ + context->line_copy = strdup(yytext); + + yyless(0); + yy_pop_state(); + yy_set_bol(true); + yycolumn = 1; +} + + +[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR; } +[0-9]+ { yylval.number = atoi(yytext); return NUMBER; } +bind { BEGIN(BINDCODE_COND); return TOKBINDCODE; } +bindcode { BEGIN(BINDCODE_COND); return TOKBINDCODE; } +Mod1 { yylval.number = (1 << 3); return MODIFIER; } +Mod2 { yylval.number = (1 << 4); return MODIFIER; } +Mod3 { yylval.number = (1 << 5); return MODIFIER; } +Mod4 { yylval.number = (1 << 6); return MODIFIER; } +Mod5 { yylval.number = (1 << 7); return MODIFIER; } +Mode_switch { yylval.number = (1 << 8); return MODIFIER; } +control { return TOKCONTROL; } +ctrl { return TOKCONTROL; } +shift { return TOKSHIFT; } +{EOL} { + if (context->line_copy) { + free(context->line_copy); + context->line_copy = NULL; + } + context->line_number++; + BEGIN(INITIAL); + yy_push_state(BUFFER_LINE); + } +[ \t]+ { BEGIN(BIND_AWS_COND); return WHITESPACE; } +[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; } +[ \t]+ { return WHITESPACE; } +. { return (int)yytext[0]; } + +<> { + while (yy_start_stack_ptr > 0) + yy_pop_state(); + yyterminate(); +} + +%% diff --git a/i3-config-wizard/cfgparse.y b/i3-config-wizard/cfgparse.y new file mode 100644 index 00000000..85e57bfb --- /dev/null +++ b/i3-config-wizard/cfgparse.y @@ -0,0 +1,153 @@ +%{ +/* + * vim:ts=4:sw=4:expandtab + * + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern Display *dpy; + +struct context { + int line_number; + char *line_copy; + + char *compact_error; + + /* These are the same as in YYLTYPE */ + int first_column; + int last_column; + + char *result; +}; + +typedef struct yy_buffer_state *YY_BUFFER_STATE; +extern int yylex(struct context *context); +extern int yyparse(void); +extern FILE *yyin; +YY_BUFFER_STATE yy_scan_string(const char *); + +static struct context *context; + +/* We don’t need yydebug for now, as we got decent error messages using + * yyerror(). Should you ever want to extend the parser, it might be handy + * to just comment it in again, so it stays here. */ +//int yydebug = 1; + +void yyerror(const char *error_message) { + fprintf(stderr, "\n"); + fprintf(stderr, "CONFIG: %s\n", error_message); + fprintf(stderr, "CONFIG: line %d:\n", + context->line_number); + fprintf(stderr, "CONFIG: %s\n", context->line_copy); + fprintf(stderr, "CONFIG: "); + for (int c = 1; c <= context->last_column; c++) + if (c >= context->first_column) + fprintf(stderr, "^"); + else fprintf(stderr, " "); + fprintf(stderr, "\n"); + fprintf(stderr, "\n"); +} + +int yywrap() { + return 1; +} + +char *rewrite_binding(const char *bindingline) { + char *result = NULL; + + context = calloc(sizeof(struct context), 1); + + yy_scan_string(bindingline); + + if (yyparse() != 0) { + fprintf(stderr, "Could not parse configfile\n"); + exit(1); + } + + result = context->result; + + if (context->line_copy) + free(context->line_copy); + free(context); + + return result; +} + +/* XXX: does not work for combinations of modifiers yet */ +static char *modifier_to_string(int modifiers) { + //printf("should convert %d to string\n", modifiers); + if (modifiers == (1 << 3)) + return strdup("Mod1"); + else if (modifiers == ((1 << 3) | (1 << 0))) + return strdup("Mod1+shift"); + else return strdup("UNKNOWN"); +} + +%} + +%error-verbose +%lex-param { struct context *context } + +%union { + int number; + char *string; +} + +%token NUMBER "" +%token STR "" +%token TOKBINDCODE +%token MODIFIER "" +%token TOKCONTROL "control" +%token TOKSHIFT "shift" +%token WHITESPACE "" + +%% + +lines: /* empty */ + | lines WHITESPACE bindcode + | lines error + | lines bindcode + ; + +bindcode: + TOKBINDCODE WHITESPACE binding_modifiers NUMBER WHITESPACE STR + { + //printf("\tFound keycode binding mod%d with key %d and command %s\n", $3, $4, $6); + int level = 0; + if (($3 & (1 << 0))) { + /* When shift is included, we really need to use the second-level + * symbol (upper-case). The lower-case symbol could be on a + * different key than the upper-case one (unlikely for letters, but + * more likely for special characters). */ + level = 1; + } + KeySym sym = XKeycodeToKeysym(dpy, $4, level); + char *str = XKeysymToString(sym); + char *modifiers = modifier_to_string($3); + // TODO: modifier to string + asprintf(&(context->result), "bindsym %s+%s %s\n", modifiers, str, $6); + free(modifiers); + } + ; + +binding_modifiers: + /* NULL */ { $$ = 0; } + | binding_modifier + | binding_modifiers '+' binding_modifier { $$ = $1 | $3; } + | binding_modifiers '+' { $$ = $1; } + ; + +binding_modifier: + MODIFIER { $$ = $1; } + | TOKCONTROL { $$ = (1 << 2); } + | TOKSHIFT { $$ = (1 << 0); } + ; diff --git a/i3-config-wizard/main.c b/i3-config-wizard/main.c new file mode 100644 index 00000000..d50669ed --- /dev/null +++ b/i3-config-wizard/main.c @@ -0,0 +1,355 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2011 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + * i3-config-wizard: Program to convert configs using keycodes to configs using + * keysyms. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#define FREE(pointer) do { \ + if (pointer != NULL) { \ + free(pointer); \ + pointer = NULL; \ + } \ +} \ +while (0) + +#include "xcb.h" + +enum { STEP_WELCOME, STEP_GENERATE } current_step = STEP_WELCOME; +enum { MOD_ALT, MOD_SUPER } modifier = MOD_SUPER; + +static xcb_connection_t *conn; +static uint32_t font_id; +static uint32_t font_bold_id; +static char *socket_path; +static int sockfd; +static int font_height; +static int font_bold_height; +static xcb_window_t win; +static xcb_pixmap_t pixmap; +static xcb_gcontext_t pixmap_gc; +static xcb_key_symbols_t *symbols; +xcb_window_t root; +Display *dpy; + +static void finish(); + +/* + * Try to get the socket path from X11 and return NULL if it doesn’t work. + * As i3-msg is a short-running tool, we don’t bother with cleaning up the + * connection and leave it up to the operating system on exit. + * + */ +static char *socket_path_from_x11() { + xcb_connection_t *conn; + int screen; + if ((conn = xcb_connect(NULL, &screen)) == NULL || + xcb_connection_has_error(conn)) + return NULL; + xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screen); + xcb_window_t root = root_screen->root; + + xcb_intern_atom_cookie_t atom_cookie; + xcb_intern_atom_reply_t *atom_reply; + + atom_cookie = xcb_intern_atom(conn, 0, strlen("I3_SOCKET_PATH"), "I3_SOCKET_PATH"); + atom_reply = xcb_intern_atom_reply(conn, atom_cookie, NULL); + if (atom_reply == NULL) + return NULL; + + xcb_get_property_cookie_t prop_cookie; + xcb_get_property_reply_t *prop_reply; + prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom, + XCB_GET_PROPERTY_TYPE_ANY, 0, PATH_MAX); + prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL); + if (prop_reply == NULL || xcb_get_property_value_length(prop_reply) == 0) + return NULL; + if (asprintf(&socket_path, "%.*s", xcb_get_property_value_length(prop_reply), + (char*)xcb_get_property_value(prop_reply)) == -1) + return NULL; + return socket_path; +} + +/* + * Handles expose events, that is, draws the window contents. + * + */ +static int handle_expose() { + /* re-draw the background */ + xcb_rectangle_t border = {0, 0, 300, (15*font_height) + 8}, + inner = {2, 2, 296, (15*font_height) + 8 - 4}; + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#285577")); + xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border); + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000")); + xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner); + + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id); + +#define txt(x, row, text) xcb_image_text_8(conn, strlen(text), pixmap, pixmap_gc, x, (row * font_height) + 2, text) + + if (current_step == STEP_WELCOME) { + /* restore font color */ + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF")); + + txt(10, 1, "i3: first configuration"); + txt(10, 4, "You have not configured i3 yet."); + txt(10, 5, "Do you want me to generate ~/.i3/config?"); + txt(85, 8, "Yes, generate ~/.i3/config"); + txt(85, 10, "No, I will use the defaults"); + + /* green */ + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#00FF00")); + txt(25, 8, ""); + + /* red */ + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000")); + txt(31, 10, ""); + } + + if (current_step == STEP_GENERATE) { + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF")); + + txt(10, 1, "i3: generate config"); + + txt(10, 4, "Please choose either:"); + txt(85, 6, "Win as default modifier"); + txt(85, 7, "Alt as default modifier"); + txt(10, 9, "Afterwards, press"); + txt(85, 11, "to write ~/.i3/config"); + txt(85, 12, "to abort"); + + /* the not-selected modifier */ + if (modifier == MOD_SUPER) + txt(31, 7, ""); + else txt(31, 6, ""); + + /* the selected modifier */ + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_bold_id); + if (modifier == MOD_SUPER) + txt(31, 6, ""); + else txt(31, 7, ""); + + /* green */ + uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_FONT; + uint32_t values[] = { get_colorpixel(conn, "#00FF00"), font_id }; + xcb_change_gc(conn, pixmap_gc, mask, values); + + txt(25, 11, ""); + + /* red */ + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000")); + txt(31, 12, ""); + } + + /* Copy the contents of the pixmap to the real window */ + xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, 500); + xcb_flush(conn); + + return 1; +} + +static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) { + printf("Keypress %d, state raw = %d\n", event->detail, event->state); + + xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state); + + printf("sym = %c (%d)\n", sym, sym); + + if (sym == XK_Return) { + if (current_step == STEP_WELCOME) + current_step = STEP_GENERATE; + else finish(); + } + + /* cancel any time */ + if (sym == XK_Escape) + exit(0); + + if (sym == XK_Alt_L) + modifier = MOD_ALT; + + if (sym == XK_Super_L) + modifier = MOD_SUPER; + + handle_expose(); + return 1; +} + +static void finish() { + printf("finishing the wizard\n"); + +#if 0 + dpy = XOpenDisplay(NULL); + + FILE *kc_config = fopen("../i3.config.kc", "r"); + char *line = NULL; + size_t len = 0; + ssize_t read; + while ((read = getline(&line, &len, kc_config)) != -1) { + /* See if that line is interesting by skipping leading whitespaces, + * then checking for 'bindcode' */ + char *walk = line; + while (isspace(*walk) && walk < (line + len)) + walk++; + if (strncmp(walk, "bindcode", strlen("bindcode")) != 0) + continue; + char *result = rewrite_binding(walk); + printf("in: %s", walk); + printf("out: %s", result); + free(result); + + } + + free(line); + fclose(kc_config); + + exit(0); +#endif + + exit(0); +} + +int main(int argc, char *argv[]) { + socket_path = getenv("I3SOCK"); + char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1"; + char *patternbold = "-misc-fixed-bold-r-normal--13-120-75-75-C-70-iso10646-1"; + int o, option_index = 0; + + static struct option long_options[] = { + {"socket", required_argument, 0, 's'}, + {"version", no_argument, 0, 'v'}, + {"limit", required_argument, 0, 'l'}, + {"prompt", required_argument, 0, 'P'}, + {"prefix", required_argument, 0, 'p'}, + {"font", required_argument, 0, 'f'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0} + }; + + char *options_string = "s:vh"; + + while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) { + switch (o) { + case 's': + FREE(socket_path); + socket_path = strdup(optarg); + break; + case 'v': + printf("i3-config-wizard " I3_VERSION); + return 0; + case 'h': + printf("i3-config-wizard " I3_VERSION); + printf("i3-config-wizard [-s ] [-v]\n"); + return 0; + } + } + + if (socket_path == NULL) + socket_path = socket_path_from_x11(); + + if (socket_path == NULL) + socket_path = "/tmp/i3-ipc.sock"; + + int screens; + conn = xcb_connect(NULL, &screens); + if (xcb_connection_has_error(conn)) + errx(1, "Cannot open display\n"); + + xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens); + root = root_screen->root; + + symbols = xcb_key_symbols_alloc(conn); + + font_id = get_font_id(conn, pattern, &font_height); + font_bold_id = get_font_id(conn, patternbold, &font_bold_height); + + /* Open an input window */ + win = open_input_window(conn, 300, 205); + + /* Create pixmap */ + pixmap = xcb_generate_id(conn); + pixmap_gc = xcb_generate_id(conn); + xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, 500); + xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0); + + /* Set input focus (we have override_redirect=1, so the wm will not do + * this for us) */ + xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME); + + /* Grab the keyboard to get all input */ + xcb_flush(conn); + + /* Try (repeatedly, if necessary) to grab the keyboard. We might not + * get the keyboard at the first attempt because of the keybinding + * still being active when started via a wm’s keybinding. */ + xcb_grab_keyboard_cookie_t cookie; + xcb_grab_keyboard_reply_t *reply = NULL; + + int count = 0; + while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) { + cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC); + reply = xcb_grab_keyboard_reply(conn, cookie, NULL); + usleep(1000); + } + + if (reply->status != XCB_GRAB_STATUS_SUCCESS) { + fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status); + exit(-1); + } + + xcb_flush(conn); + + xcb_generic_event_t *event; + while ((event = xcb_wait_for_event(conn)) != NULL) { + if (event->response_type == 0) { + fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence); + continue; + } + + /* Strip off the highest bit (set if the event is generated) */ + int type = (event->response_type & 0x7F); + + switch (type) { + case XCB_KEY_PRESS: + handle_key_press(NULL, conn, (xcb_key_press_event_t*)event); + break; + + /* TODO: handle mappingnotify */ + + case XCB_EXPOSE: + handle_expose(); + break; + } + + free(event); + } + + return 0; +} diff --git a/i3-config-wizard/xcb.c b/i3-config-wizard/xcb.c new file mode 100644 index 00000000..9a9bf615 --- /dev/null +++ b/i3-config-wizard/xcb.c @@ -0,0 +1,166 @@ +/* + * vim:ts=8:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2009 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + */ +#include +#include +#include +#include +#include + +#include +#include + +#include + +extern xcb_window_t root; + +/* + * Convenience-wrapper around xcb_change_gc which saves us declaring a variable + * + */ +void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) { + xcb_change_gc(conn, gc, mask, &value); +} + +/* + * Returns the colorpixel to use for the given hex color (think of HTML). + * + * The hex_color has to start with #, for example #FF00FF. + * + * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. + * This has to be done by the caller. + * + */ +uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) { + char strgroups[3][3] = {{hex[1], hex[2], '\0'}, + {hex[3], hex[4], '\0'}, + {hex[5], hex[6], '\0'}}; + uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)), + (strtol(strgroups[1], NULL, 16)), + (strtol(strgroups[2], NULL, 16))}; + + return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2]; +} + +/* + * Returns the mask for Mode_switch (to be used for looking up keysymbols by + * keycode). + * + */ +uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode) { + xcb_key_symbols_t *symbols = xcb_key_symbols_alloc(conn); + + xcb_get_modifier_mapping_reply_t *modmap_r; + xcb_keycode_t *modmap, kc; + xcb_keycode_t *modeswitchcodes = xcb_key_symbols_get_keycode(symbols, keycode); + if (modeswitchcodes == NULL) + return 0; + + modmap_r = xcb_get_modifier_mapping_reply(conn, xcb_get_modifier_mapping(conn), NULL); + modmap = xcb_get_modifier_mapping_keycodes(modmap_r); + + for (int i = 0; i < 8; i++) + for (int j = 0; j < modmap_r->keycodes_per_modifier; j++) { + kc = modmap[i * modmap_r->keycodes_per_modifier + j]; + for (xcb_keycode_t *ktest = modeswitchcodes; *ktest; ktest++) { + if (*ktest != kc) + continue; + + free(modeswitchcodes); + free(modmap_r); + return (1 << i); + } + } + + return 0; +} + +/* + * Opens the window we use for input/output and maps it + * + */ +xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height) { + xcb_window_t win = xcb_generate_id(conn); + //xcb_cursor_t cursor_id = xcb_generate_id(conn); + +#if 0 + /* Use the default cursor (left pointer) */ + if (cursor > -1) { + i3Font *cursor_font = load_font(conn, "cursor"); + xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id, + XCB_CURSOR_LEFT_PTR, XCB_CURSOR_LEFT_PTR + 1, + 0, 0, 0, 65535, 65535, 65535); + } +#endif + + uint32_t mask = 0; + uint32_t values[3]; + + mask |= XCB_CW_BACK_PIXEL; + values[0] = 0; + + mask |= XCB_CW_OVERRIDE_REDIRECT; + values[1] = 1; + + mask |= XCB_CW_EVENT_MASK; + values[2] = XCB_EVENT_MASK_EXPOSURE; + + xcb_create_window(conn, + XCB_COPY_FROM_PARENT, + win, /* the window id */ + root, /* parent == root */ + 490, 297, width, height, /* dimensions */ + 0, /* border = 0, we draw our own */ + XCB_WINDOW_CLASS_INPUT_OUTPUT, + XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */ + mask, + values); + +#if 0 + if (cursor > -1) + xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id); +#endif + + /* Map the window (= make it visible) */ + xcb_map_window(conn, win); + + return win; +} + +/* + * Returns the ID of the font matching the given pattern and stores the height + * of the font (in pixels) in *font_height. die()s if no font matches. + * + */ +int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height) { + xcb_void_cookie_t font_cookie; + xcb_list_fonts_with_info_cookie_t info_cookie; + + /* Send all our requests first */ + int result; + result = xcb_generate_id(conn); + font_cookie = xcb_open_font_checked(conn, result, strlen(pattern), pattern); + info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern); + + xcb_generic_error_t *error = xcb_request_check(conn, font_cookie); + if (error != NULL) { + fprintf(stderr, "ERROR: Could not open font: %d\n", error->error_code); + exit(1); + } + + /* Get information (height/name) for this font */ + xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL); + if (reply == NULL) + errx(1, "Could not load font \"%s\"\n", pattern); + + *font_height = reply->font_ascent + reply->font_descent; + + return result; +} diff --git a/i3-config-wizard/xcb.h b/i3-config-wizard/xcb.h new file mode 100644 index 00000000..71280e1c --- /dev/null +++ b/i3-config-wizard/xcb.h @@ -0,0 +1,6 @@ + +void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value); +uint32_t get_colorpixel(xcb_connection_t *conn, char *hex); +uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode); +xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height); +int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height);