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;
55 * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
56 * rendering it (UCS-2) or sending it to i3 (UTF-8).
59 static uint8_t *concat_strings(char **glyphs, int max) {
60 uint8_t *output = calloc(max+1, 4);
61 uint8_t *walk = output;
62 for (int c = 0; c < max; c++) {
63 printf("at %c\n", glyphs[c][0]);
64 /* if the first byte is 0, this has to be UCS2 */
65 if (glyphs[c][0] == '\0') {
66 memcpy(walk, glyphs[c], 2);
69 strcpy((char*)walk, glyphs[c]);
70 walk += strlen(glyphs[c]);
73 printf("output = %s\n", output);
78 * Handles expose events (redraws of the window) and rendering in general. Will
79 * be called from the code with event == NULL or from X with event != NULL.
82 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
85 /* re-draw the background */
86 xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4};
87 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
88 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
89 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
90 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
92 /* restore font color */
93 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
94 uint8_t *con = concat_strings(glyphs_ucs, input_position);
95 char *full_text = (char*)con;
97 full_text = malloc((prompt_len + input_position) * 2 + 1);
98 if (full_text == NULL)
99 err(EXIT_FAILURE, "malloc() failed\n");
100 memcpy(full_text, prompt, prompt_len * 2);
101 memcpy(full_text + (prompt_len * 2), con, input_position * 2);
103 xcb_image_text_16(conn, input_position + prompt_len, pixmap, pixmap_gc, 4 /* X */,
104 font_height + 2 /* Y = baseline of font */, (xcb_char2b_t*)full_text);
106 /* Copy the contents of the pixmap to the real window */
107 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, font_height + 8);
117 * Deactivates the Mode_switch bit upon release of the Mode_switch key.
120 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
121 printf("releasing %d, state raw = %d\n", event->detail, event->state);
124 event->state &= ~numlockmask;
126 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
127 if (sym == XK_Mode_switch) {
128 printf("Mode switch disabled\n");
129 modeswitch_active = false;
135 static void finish_input() {
136 uint8_t *command = concat_strings(glyphs_utf8, input_position);
137 char *full_command = (char*)command;
138 /* prefix the command if a prefix was specified on commandline */
139 if (command_prefix != NULL) {
140 if (asprintf(&full_command, "%s%s", command_prefix, command) == -1)
141 err(EXIT_FAILURE, "asprintf() failed\n");
143 printf("command = %s\n", full_command);
145 ipc_send_message(sockfd, strlen(full_command), 0, (uint8_t*)full_command);
155 * Handles keypresses by converting the keycodes to keysymbols, then the
156 * keysymbols to UCS-2. If the conversion succeeded, the glyph is saved in the
157 * internal buffers and displayed in the input window.
159 * Also handles backspace (deleting one character) and return (sending the
163 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
164 printf("Keypress %d, state raw = %d\n", event->detail, event->state);
167 if (modeswitch_active)
168 event->state |= modeswitchmask;
170 /* Apparantly, after activating numlock once, the numlock modifier
171 * stays turned on (use xev(1) to verify). So, to resolve useful
172 * keysyms, we remove the numlock flag from the event state */
173 event->state &= ~numlockmask;
175 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
176 if (sym == XK_Mode_switch) {
177 printf("Mode switch enabled\n");
178 modeswitch_active = true;
182 if (sym == XK_Return)
185 if (sym == XK_BackSpace) {
186 if (input_position == 0)
190 free(glyphs_ucs[input_position]);
191 free(glyphs_utf8[input_position]);
193 handle_expose(NULL, conn, NULL);
196 if (sym == XK_Escape) {
200 /* TODO: handle all of these? */
201 printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
202 printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
203 printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
204 printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
205 printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
206 printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
207 printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
209 if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
212 printf("sym = %c (%d)\n", sym, sym);
214 /* convert the keysym to UCS */
215 uint16_t ucs = keysym2ucs(sym);
216 if ((int16_t)ucs == -1) {
217 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
221 /* store the UCS into a string */
222 uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
224 printf("inp[0] = %02x, inp[1] = %02x, inp[2] = %02x\n", inp[0], inp[1], inp[2]);
225 /* convert it to UTF-8 */
226 char *out = convert_ucs_to_utf8((char*)inp);
227 printf("converted to %s\n", out);
229 glyphs_ucs[input_position] = malloc(3 * sizeof(uint8_t));
230 if (glyphs_ucs[input_position] == NULL)
231 err(EXIT_FAILURE, "malloc() failed\n");
232 memcpy(glyphs_ucs[input_position], inp, 3);
233 glyphs_utf8[input_position] = strdup(out);
236 if (input_position == limit)
239 handle_expose(NULL, conn, NULL);
243 int main(int argc, char *argv[]) {
245 if ((socket_path = getenv("I3SOCK")) == NULL) {
246 socket_path = "/tmp/i3-ipc.sock";
248 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
249 int o, option_index = 0;
251 static struct option long_options[] = {
252 {"socket", required_argument, 0, 's'},
253 {"version", no_argument, 0, 'v'},
254 {"limit", required_argument, 0, 'l'},
255 {"prompt", required_argument, 0, 'P'},
256 {"prefix", required_argument, 0, 'p'},
257 {"font", required_argument, 0, 'f'},
258 {"help", no_argument, 0, 'h'},
262 char *options_string = "s:p:P:f:l:vh";
264 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
268 socket_path = strdup(optarg);
271 printf("i3-input " I3_VERSION);
274 FREE(command_prefix);
275 command_prefix = strdup(optarg);
278 limit = atoi(optarg);
282 prompt = strdup(optarg);
286 pattern = strdup(optarg);
289 printf("i3-input " I3_VERSION);
290 printf("i3-input [-s <socket>] [-p <prefix>] [-l <limit>] [-P <prompt>] [-f <font>] [-v]\n");
295 sockfd = connect_ipc(socket_path);
298 prompt = convert_utf8_to_ucs2(prompt, &prompt_len);
301 xcb_connection_t *conn = xcb_connect(NULL, &screens);
302 if (xcb_connection_has_error(conn))
303 die("Cannot open display\n");
305 /* Set up event handlers for key press and key release */
306 xcb_event_handlers_t evenths;
307 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
308 xcb_event_handlers_init(conn, &evenths);
309 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
310 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
311 xcb_event_set_expose_handler(&evenths, handle_expose, NULL);
313 modeswitchmask = get_mod_mask(conn, XK_Mode_switch);
314 numlockmask = get_mod_mask(conn, XK_Num_Lock);
315 symbols = xcb_key_symbols_alloc(conn);
317 uint32_t font_id = get_font_id(conn, pattern, &font_height);
319 /* Open an input window */
320 win = open_input_window(conn, 500, font_height + 8);
323 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
325 pixmap = xcb_generate_id(conn);
326 pixmap_gc = xcb_generate_id(conn);
327 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
328 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
330 /* Set input focus (we have override_redirect=1, so the wm will not do
332 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
334 /* Create graphics context */
335 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
337 /* Grab the keyboard to get all input */
340 /* Try (repeatedly, if necessary) to grab the keyboard. We might not
341 * get the keyboard at the first attempt because of the keybinding
342 * still being active when started via a wm’s keybinding. */
343 xcb_grab_keyboard_cookie_t cookie;
344 xcb_grab_keyboard_reply_t *reply = NULL;
347 while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
348 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
349 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
353 if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
354 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
360 xcb_event_wait_for_event_loop(&evenths);