4 * i3 - an improved dynamic tiling window manager
6 * © 2009 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>
27 #include <xcb/xcb_aux.h>
28 #include <xcb/xcb_event.h>
29 #include <xcb/xcb_keysyms.h>
31 #include <X11/keysym.h>
33 #include "keysym2ucs.h"
38 static xcb_key_symbols_t *symbols;
39 static int modeswitchmask;
40 static int numlockmask;
41 static bool modeswitch_active = false;
42 static xcb_window_t win;
43 static xcb_pixmap_t pixmap;
44 static xcb_gcontext_t pixmap_gc;
45 static char *glyphs_ucs[512];
46 static char *glyphs_utf8[512];
47 static int input_position;
48 static int font_height;
49 static char *command_prefix;
51 static int prompt_len;
56 * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
57 * rendering it (UCS-2) or sending it to i3 (UTF-8).
60 static uint8_t *concat_strings(char **glyphs, int max) {
61 uint8_t *output = calloc(max+1, 4);
62 uint8_t *walk = output;
63 for (int c = 0; c < max; c++) {
64 printf("at %c\n", glyphs[c][0]);
65 /* if the first byte is 0, this has to be UCS2 */
66 if (glyphs[c][0] == '\0') {
67 memcpy(walk, glyphs[c], 2);
70 strcpy((char*)walk, glyphs[c]);
71 walk += strlen(glyphs[c]);
74 printf("output = %s\n", output);
79 * Handles expose events (redraws of the window) and rendering in general. Will
80 * be called from the code with event == NULL or from X with event != NULL.
83 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
86 /* re-draw the background */
87 xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4};
88 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
89 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
90 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
91 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
93 /* restore font color */
94 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
95 uint8_t *con = concat_strings(glyphs_ucs, input_position);
96 char *full_text = (char*)con;
98 full_text = malloc((prompt_len + input_position) * 2 + 1);
99 if (full_text == NULL)
100 err(EXIT_FAILURE, "malloc() failed\n");
101 memcpy(full_text, prompt, prompt_len * 2);
102 memcpy(full_text + (prompt_len * 2), con, input_position * 2);
104 xcb_image_text_16(conn, input_position + prompt_len, pixmap, pixmap_gc, 4 /* X */,
105 font_height + 2 /* Y = baseline of font */, (xcb_char2b_t*)full_text);
107 /* Copy the contents of the pixmap to the real window */
108 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, font_height + 8);
118 * Deactivates the Mode_switch bit upon release of the Mode_switch key.
121 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
122 printf("releasing %d, state raw = %d\n", event->detail, event->state);
125 event->state &= ~numlockmask;
127 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
128 if (sym == XK_Mode_switch) {
129 printf("Mode switch disabled\n");
130 modeswitch_active = false;
136 static void finish_input() {
137 uint8_t *command = concat_strings(glyphs_utf8, input_position);
138 char *full_command = (char*)command;
139 /* prefix the command if a prefix was specified on commandline */
140 if (command_prefix != NULL) {
141 if (asprintf(&full_command, "%s%s", command_prefix, command) == -1)
142 err(EXIT_FAILURE, "asprintf() failed\n");
144 printf("command = %s\n", full_command);
146 ipc_send_message(sockfd, strlen(full_command), 0, (uint8_t*)full_command);
156 * Handles keypresses by converting the keycodes to keysymbols, then the
157 * keysymbols to UCS-2. If the conversion succeeded, the glyph is saved in the
158 * internal buffers and displayed in the input window.
160 * Also handles backspace (deleting one character) and return (sending the
164 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
165 printf("Keypress %d, state raw = %d\n", event->detail, event->state);
168 if (modeswitch_active)
169 event->state |= modeswitchmask;
171 /* Apparantly, after activating numlock once, the numlock modifier
172 * stays turned on (use xev(1) to verify). So, to resolve useful
173 * keysyms, we remove the numlock flag from the event state */
174 event->state &= ~numlockmask;
176 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
177 if (sym == XK_Mode_switch) {
178 printf("Mode switch enabled\n");
179 modeswitch_active = true;
183 if (sym == XK_Return)
186 if (sym == XK_BackSpace) {
187 if (input_position == 0)
191 free(glyphs_ucs[input_position]);
192 free(glyphs_utf8[input_position]);
194 handle_expose(NULL, conn, NULL);
197 if (sym == XK_Escape) {
201 /* TODO: handle all of these? */
202 printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
203 printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
204 printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
205 printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
206 printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
207 printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
208 printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
210 if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
213 printf("sym = %c (%d)\n", sym, sym);
215 /* convert the keysym to UCS */
216 uint16_t ucs = keysym2ucs(sym);
217 if ((int16_t)ucs == -1) {
218 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
222 /* store the UCS into a string */
223 uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
225 printf("inp[0] = %02x, inp[1] = %02x, inp[2] = %02x\n", inp[0], inp[1], inp[2]);
226 /* convert it to UTF-8 */
227 char *out = convert_ucs_to_utf8((char*)inp);
228 printf("converted to %s\n", out);
230 glyphs_ucs[input_position] = malloc(3 * sizeof(uint8_t));
231 if (glyphs_ucs[input_position] == NULL)
232 err(EXIT_FAILURE, "malloc() failed\n");
233 memcpy(glyphs_ucs[input_position], inp, 3);
234 glyphs_utf8[input_position] = strdup(out);
237 if (input_position == limit)
240 handle_expose(NULL, conn, NULL);
244 int main(int argc, char *argv[]) {
246 if ((socket_path = getenv("I3SOCK")) == NULL) {
247 socket_path = "/tmp/i3-ipc.sock";
249 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
250 int o, option_index = 0;
252 static struct option long_options[] = {
253 {"socket", required_argument, 0, 's'},
254 {"version", no_argument, 0, 'v'},
255 {"limit", required_argument, 0, 'l'},
256 {"prompt", required_argument, 0, 'P'},
257 {"prefix", required_argument, 0, 'p'},
258 {"font", required_argument, 0, 'f'},
259 {"help", no_argument, 0, 'h'},
263 char *options_string = "s:p:P:f:l:vh";
265 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
269 socket_path = strdup(optarg);
272 printf("i3-input " I3_VERSION);
275 FREE(command_prefix);
276 command_prefix = strdup(optarg);
279 limit = atoi(optarg);
283 prompt = strdup(optarg);
287 pattern = strdup(optarg);
290 printf("i3-input " I3_VERSION);
291 printf("i3-input [-s <socket>] [-p <prefix>] [-l <limit>] [-P <prompt>] [-f <font>] [-v]\n");
296 sockfd = connect_ipc(socket_path);
299 prompt = convert_utf8_to_ucs2(prompt, &prompt_len);
302 xcb_connection_t *conn = xcb_connect(NULL, &screens);
303 if (xcb_connection_has_error(conn))
304 die("Cannot open display\n");
306 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
307 root = root_screen->root;
309 /* Set up event handlers for key press and key release */
310 xcb_event_handlers_t evenths;
311 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
312 xcb_event_handlers_init(conn, &evenths);
313 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
314 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
315 xcb_event_set_expose_handler(&evenths, handle_expose, NULL);
317 modeswitchmask = get_mod_mask(conn, XK_Mode_switch);
318 numlockmask = get_mod_mask(conn, XK_Num_Lock);
319 symbols = xcb_key_symbols_alloc(conn);
321 uint32_t font_id = get_font_id(conn, pattern, &font_height);
323 /* Open an input window */
324 win = open_input_window(conn, 500, font_height + 8);
327 pixmap = xcb_generate_id(conn);
328 pixmap_gc = xcb_generate_id(conn);
329 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
330 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
332 /* Set input focus (we have override_redirect=1, so the wm will not do
334 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
336 /* Create graphics context */
337 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
339 /* Grab the keyboard to get all input */
342 /* Try (repeatedly, if necessary) to grab the keyboard. We might not
343 * get the keyboard at the first attempt because of the keybinding
344 * still being active when started via a wm’s keybinding. */
345 xcb_grab_keyboard_cookie_t cookie;
346 xcb_grab_keyboard_reply_t *reply = NULL;
349 while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
350 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
351 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
355 if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
356 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
362 xcb_event_wait_for_event_loop(&evenths);