4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * i3-input/main.c: Utility which lets the user input commands and sends them
16 #include <sys/types.h>
28 #include <xcb/xcb_aux.h>
29 #include <xcb/xcb_event.h>
30 #include <xcb/xcb_keysyms.h>
32 #include <X11/keysym.h>
34 #include "keysym2ucs.h"
39 static xcb_key_symbols_t *symbols;
40 static int modeswitchmask;
41 static int numlockmask;
42 static bool modeswitch_active = false;
43 static xcb_window_t win;
44 static xcb_pixmap_t pixmap;
45 static xcb_gcontext_t pixmap_gc;
46 static char *glyphs_ucs[512];
47 static char *glyphs_utf8[512];
48 static int input_position;
49 static int font_height;
50 static char *command_prefix;
52 static int prompt_len;
56 * This function resolves ~ in pathnames (and more, see glob(3)).
59 static char *glob_path(const char *path) {
60 static glob_t globbuf;
61 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
62 errx(EXIT_FAILURE, "glob() failed");
63 char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
65 err(EXIT_FAILURE, "malloc() failed");
71 * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
72 * rendering it (UCS-2) or sending it to i3 (UTF-8).
75 static uint8_t *concat_strings(char **glyphs, int max) {
76 uint8_t *output = calloc(max+1, 4);
77 uint8_t *walk = output;
78 for (int c = 0; c < max; c++) {
79 printf("at %c\n", glyphs[c][0]);
80 /* if the first byte is 0, this has to be UCS2 */
81 if (glyphs[c][0] == '\0') {
82 memcpy(walk, glyphs[c], 2);
85 strcpy((char*)walk, glyphs[c]);
86 walk += strlen(glyphs[c]);
89 printf("output = %s\n", output);
94 * Handles expose events (redraws of the window) and rendering in general. Will
95 * be called from the code with event == NULL or from X with event != NULL.
98 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
101 /* re-draw the background */
102 xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4};
103 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
104 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
105 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
106 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
108 /* restore font color */
109 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
110 uint8_t *con = concat_strings(glyphs_ucs, input_position);
111 char *full_text = (char*)con;
112 if (prompt != NULL) {
113 full_text = malloc((prompt_len + input_position) * 2 + 1);
114 if (full_text == NULL)
115 err(EXIT_FAILURE, "malloc() failed\n");
116 memcpy(full_text, prompt, prompt_len * 2);
117 memcpy(full_text + (prompt_len * 2), con, input_position * 2);
119 xcb_image_text_16(conn, input_position + prompt_len, pixmap, pixmap_gc, 4 /* X */,
120 font_height + 2 /* Y = baseline of font */, (xcb_char2b_t*)full_text);
122 /* Copy the contents of the pixmap to the real window */
123 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, font_height + 8);
133 * Deactivates the Mode_switch bit upon release of the Mode_switch key.
136 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
137 printf("releasing %d, state raw = %d\n", event->detail, event->state);
140 event->state &= ~numlockmask;
142 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
143 if (sym == XK_Mode_switch) {
144 printf("Mode switch disabled\n");
145 modeswitch_active = false;
151 static void finish_input() {
152 uint8_t *command = concat_strings(glyphs_utf8, input_position);
153 char *full_command = (char*)command;
154 /* prefix the command if a prefix was specified on commandline */
155 if (command_prefix != NULL) {
156 if (asprintf(&full_command, "%s%s", command_prefix, command) == -1)
157 err(EXIT_FAILURE, "asprintf() failed\n");
159 printf("command = %s\n", full_command);
161 ipc_send_message(sockfd, strlen(full_command), 0, (uint8_t*)full_command);
171 * Handles keypresses by converting the keycodes to keysymbols, then the
172 * keysymbols to UCS-2. If the conversion succeeded, the glyph is saved in the
173 * internal buffers and displayed in the input window.
175 * Also handles backspace (deleting one character) and return (sending the
179 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
180 printf("Keypress %d, state raw = %d\n", event->detail, event->state);
183 if (modeswitch_active)
184 event->state |= modeswitchmask;
186 /* Apparantly, after activating numlock once, the numlock modifier
187 * stays turned on (use xev(1) to verify). So, to resolve useful
188 * keysyms, we remove the numlock flag from the event state */
189 event->state &= ~numlockmask;
191 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
192 if (sym == XK_Mode_switch) {
193 printf("Mode switch enabled\n");
194 modeswitch_active = true;
198 if (sym == XK_Return)
201 if (sym == XK_BackSpace) {
202 if (input_position == 0)
206 free(glyphs_ucs[input_position]);
207 free(glyphs_utf8[input_position]);
209 handle_expose(NULL, conn, NULL);
212 if (sym == XK_Escape) {
216 /* TODO: handle all of these? */
217 printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
218 printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
219 printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
220 printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
221 printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
222 printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
223 printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
225 if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
228 printf("sym = %c (%d)\n", sym, sym);
230 /* convert the keysym to UCS */
231 uint16_t ucs = keysym2ucs(sym);
232 if ((int16_t)ucs == -1) {
233 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
237 /* store the UCS into a string */
238 uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
240 printf("inp[0] = %02x, inp[1] = %02x, inp[2] = %02x\n", inp[0], inp[1], inp[2]);
241 /* convert it to UTF-8 */
242 char *out = convert_ucs_to_utf8((char*)inp);
243 printf("converted to %s\n", out);
245 glyphs_ucs[input_position] = malloc(3 * sizeof(uint8_t));
246 if (glyphs_ucs[input_position] == NULL)
247 err(EXIT_FAILURE, "malloc() failed\n");
248 memcpy(glyphs_ucs[input_position], inp, 3);
249 glyphs_utf8[input_position] = strdup(out);
252 if (input_position == limit)
255 handle_expose(NULL, conn, NULL);
259 int main(int argc, char *argv[]) {
260 char *socket_path = glob_path("~/.i3/ipc.sock");
261 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
262 int o, option_index = 0;
264 static struct option long_options[] = {
265 {"socket", required_argument, 0, 's'},
266 {"version", no_argument, 0, 'v'},
267 {"limit", required_argument, 0, 'l'},
268 {"prompt", required_argument, 0, 'P'},
269 {"prefix", required_argument, 0, 'p'},
270 {"font", required_argument, 0, 'f'},
271 {"help", no_argument, 0, 'h'},
275 char *options_string = "s:p:P:f:l:vh";
277 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
280 socket_path = glob_path(optarg);
283 printf("i3-input " I3_VERSION);
286 command_prefix = strdup(optarg);
289 limit = atoi(optarg);
292 prompt = strdup(optarg);
295 pattern = strdup(optarg);
298 printf("i3-input " I3_VERSION);
299 printf("i3-input [-s <socket>] [-p <prefix>] [-l <limit>] [-P <prompt>] [-f <font>] [-v]\n");
304 sockfd = connect_ipc(socket_path);
307 prompt = convert_utf8_to_ucs2(prompt, &prompt_len);
310 xcb_connection_t *conn = xcb_connect(NULL, &screens);
311 if (xcb_connection_has_error(conn))
312 die("Cannot open display\n");
314 /* Set up event handlers for key press and key release */
315 xcb_event_handlers_t evenths;
316 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
317 xcb_event_handlers_init(conn, &evenths);
318 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
319 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
320 xcb_event_set_expose_handler(&evenths, handle_expose, NULL);
322 modeswitchmask = get_mod_mask(conn, XK_Mode_switch);
323 numlockmask = get_mod_mask(conn, XK_Num_Lock);
324 symbols = xcb_key_symbols_alloc(conn);
326 uint32_t font_id = get_font_id(conn, pattern, &font_height);
328 /* Open an input window */
329 win = open_input_window(conn, 500, font_height + 8);
332 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
334 pixmap = xcb_generate_id(conn);
335 pixmap_gc = xcb_generate_id(conn);
336 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
337 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
339 /* Set input focus (we have override_redirect=1, so the wm will not do
341 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
343 /* Create graphics context */
344 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
346 /* Grab the keyboard to get all input */
349 /* Try (repeatedly, if necessary) to grab the keyboard. We might not
350 * get the keyboard at the first attempt because of the keybinding
351 * still being active when started via a wm’s keybinding. */
352 xcb_grab_keyboard_cookie_t cookie;
353 xcb_grab_keyboard_reply_t *reply = NULL;
356 while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
357 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
358 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
362 if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
363 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
369 xcb_event_wait_for_event_loop(&evenths);