2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * i3-input/main.c: Utility which lets the user input commands and sends them
13 #include <sys/types.h>
25 #include <xcb/xcb_aux.h>
26 #include <xcb/xcb_event.h>
27 #include <xcb/xcb_keysyms.h>
29 #include <X11/keysym.h>
31 #include "keysym2ucs.h"
37 /* IPC format string. %s will be replaced with what the user entered, then
38 * the command will be sent to i3 */
41 static char *socket_path;
43 static xcb_key_symbols_t *symbols;
44 static bool modeswitch_active = false;
45 static xcb_window_t win;
46 static xcb_pixmap_t pixmap;
47 static xcb_gcontext_t pixmap_gc;
48 static char *glyphs_ucs[512];
49 static char *glyphs_utf8[512];
50 static int input_position;
53 static size_t prompt_len;
56 xcb_connection_t *conn;
59 * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
60 * rendering it (UCS-2) or sending it to i3 (UTF-8).
63 static uint8_t *concat_strings(char **glyphs, int max) {
64 uint8_t *output = calloc(max+1, 4);
65 uint8_t *walk = output;
66 for (int c = 0; c < max; c++) {
67 printf("at %c\n", glyphs[c][0]);
68 /* if the first byte is 0, this has to be UCS2 */
69 if (glyphs[c][0] == '\0') {
70 memcpy(walk, glyphs[c], 2);
73 strcpy((char*)walk, glyphs[c]);
74 walk += strlen(glyphs[c]);
77 printf("output = %s\n", output);
82 * Handles expose events (redraws of the window) and rendering in general. Will
83 * be called from the code with event == NULL or from X with event != NULL.
86 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
89 /* re-draw the background */
90 xcb_rectangle_t border = {0, 0, 500, font.height + 8}, inner = {2, 2, 496, font.height + 8 - 4};
91 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FF0000") });
92 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
93 xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") });
94 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
96 /* restore font color */
97 set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
100 uint8_t *con = concat_strings(glyphs_ucs, input_position);
101 char *full_text = (char*)con;
102 if (prompt != NULL) {
103 full_text = malloc((prompt_len + input_position) * 2 + 1);
104 if (full_text == NULL)
105 err(EXIT_FAILURE, "malloc() failed\n");
106 memcpy(full_text, prompt, prompt_len * 2);
107 memcpy(full_text + (prompt_len * 2), con, input_position * 2);
109 if (input_position + prompt_len != 0)
110 draw_text(full_text, input_position + prompt_len, true, pixmap, pixmap_gc, 4, 4, 492);
112 /* Copy the contents of the pixmap to the real window */
113 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, font.height + 8);
123 * Deactivates the Mode_switch bit upon release of the Mode_switch key.
126 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
127 printf("releasing %d, state raw = %d\n", event->detail, event->state);
129 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
130 if (sym == XK_Mode_switch) {
131 printf("Mode switch disabled\n");
132 modeswitch_active = false;
138 static void finish_input() {
139 char *command = (char*)concat_strings(glyphs_utf8, input_position);
141 /* count the occurences of %s in the string */
143 int len = strlen(format);
145 for (c = 0; c < (len-1); c++)
146 if (format[c] == '%' && format[c+1] == 's')
148 printf("occurences = %d\n", cnt);
150 /* allocate space for the output */
151 int inputlen = strlen(command);
152 char *full = calloc(1,
153 strlen(format) - (2 * cnt) /* format without all %s */
154 + (inputlen * cnt) /* replaced %s */
155 + 1); /* trailing NUL */
157 for (c = 0; c < len; c++) {
158 /* if this is not % or it is % but without a following 's',
159 * just copy the character */
160 if (format[c] != '%' || (c == (len-1)) || format[c+1] != 's')
161 *(dest++) = format[c];
163 strncat(dest, command, inputlen);
165 /* skip the following 's' of '%s' */
170 /* prefix the command if a prefix was specified on commandline */
171 printf("command = %s\n", full);
173 ipc_send_message(sockfd, strlen(full), 0, (uint8_t*)full);
183 * Handles keypresses by converting the keycodes to keysymbols, then the
184 * keysymbols to UCS-2. If the conversion succeeded, the glyph is saved in the
185 * internal buffers and displayed in the input window.
187 * Also handles backspace (deleting one character) and return (sending the
191 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
192 printf("Keypress %d, state raw = %d\n", event->detail, event->state);
194 /* See the documentation of xcb_key_symbols_get_keysym for this one.
195 * Basically: We get either col 0 or col 1, depending on whether shift is
197 int col = (event->state & XCB_MOD_MASK_SHIFT);
199 /* If modeswitch is currently active, we need to look in group 2 or 3,
201 if (modeswitch_active)
204 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, col);
205 if (sym == XK_Mode_switch) {
206 printf("Mode switch enabled\n");
207 modeswitch_active = true;
211 if (sym == XK_Return)
214 if (sym == XK_BackSpace) {
215 if (input_position == 0)
219 free(glyphs_ucs[input_position]);
220 free(glyphs_utf8[input_position]);
222 handle_expose(NULL, conn, NULL);
225 if (sym == XK_Escape) {
229 /* TODO: handle all of these? */
230 printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
231 printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
232 printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
233 printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
234 printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
235 printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
236 printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
238 if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
241 printf("sym = %c (%d)\n", sym, sym);
243 /* convert the keysym to UCS */
244 uint16_t ucs = keysym2ucs(sym);
245 if ((int16_t)ucs == -1) {
246 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
250 /* store the UCS into a string */
251 uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
253 printf("inp[0] = %02x, inp[1] = %02x, inp[2] = %02x\n", inp[0], inp[1], inp[2]);
254 /* convert it to UTF-8 */
255 char *out = convert_ucs2_to_utf8((xcb_char2b_t*)inp, 1);
256 printf("converted to %s\n", out);
258 glyphs_ucs[input_position] = malloc(3 * sizeof(uint8_t));
259 if (glyphs_ucs[input_position] == NULL)
260 err(EXIT_FAILURE, "malloc() failed\n");
261 memcpy(glyphs_ucs[input_position], inp, 3);
262 glyphs_utf8[input_position] = out;
265 if (input_position == limit)
268 handle_expose(NULL, conn, NULL);
272 int main(int argc, char *argv[]) {
273 format = strdup("%s");
274 socket_path = getenv("I3SOCK");
275 char *pattern = sstrdup("-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1");
276 int o, option_index = 0;
278 static struct option long_options[] = {
279 {"socket", required_argument, 0, 's'},
280 {"version", no_argument, 0, 'v'},
281 {"limit", required_argument, 0, 'l'},
282 {"prompt", required_argument, 0, 'P'},
283 {"prefix", required_argument, 0, 'p'},
284 {"format", required_argument, 0, 'F'},
285 {"font", required_argument, 0, 'f'},
286 {"help", no_argument, 0, 'h'},
290 char *options_string = "s:p:P:f:l:F:vh";
292 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
296 socket_path = strdup(optarg);
299 printf("i3-input " I3_VERSION);
302 /* This option is deprecated, but will still work in i3 v4.1, 4.2 and 4.3 */
303 fprintf(stderr, "i3-input: WARNING: the -p option is DEPRECATED in favor of the -F (format) option\n");
305 sasprintf(&format, "%s%%s", optarg);
308 limit = atoi(optarg);
312 prompt = strdup(optarg);
316 pattern = strdup(optarg);
320 format = strdup(optarg);
323 printf("i3-input " I3_VERSION "\n");
324 printf("i3-input [-s <socket>] [-F <format>] [-l <limit>] [-P <prompt>] [-f <font>] [-v]\n");
326 printf("Example:\n");
327 printf(" i3-input -F 'workspace \"%%s\"' -P 'Switch to workspace: '\n");
332 printf("using format \"%s\"\n", format);
334 if (socket_path == NULL)
335 socket_path = root_atom_contents("I3_SOCKET_PATH");
337 if (socket_path == NULL)
338 socket_path = "/tmp/i3-ipc.sock";
340 sockfd = ipc_connect(socket_path);
343 prompt = (char*)convert_utf8_to_ucs2(prompt, &prompt_len);
346 conn = xcb_connect(NULL, &screens);
347 if (!conn || xcb_connection_has_error(conn))
348 die("Cannot open display\n");
350 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
351 root = root_screen->root;
353 symbols = xcb_key_symbols_alloc(conn);
355 font = load_font(pattern, true);
358 /* Open an input window */
359 win = xcb_generate_id(conn);
362 XCB_COPY_FROM_PARENT,
363 win, /* the window id */
364 root, /* parent == root */
365 50, 50, 500, font.height + 8, /* dimensions */
366 0, /* X11 border = 0, we draw our own */
367 XCB_WINDOW_CLASS_INPUT_OUTPUT,
368 XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
369 XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK,
371 0, /* back pixel: black */
372 1, /* override redirect: don’t manage this window */
373 XCB_EVENT_MASK_EXPOSURE
376 /* Map the window (make it visible) */
377 xcb_map_window(conn, win);
380 pixmap = xcb_generate_id(conn);
381 pixmap_gc = xcb_generate_id(conn);
382 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font.height + 8);
383 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
385 /* Set input focus (we have override_redirect=1, so the wm will not do
387 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
389 /* Grab the keyboard to get all input */
392 /* Try (repeatedly, if necessary) to grab the keyboard. We might not
393 * get the keyboard at the first attempt because of the keybinding
394 * still being active when started via a wm’s keybinding. */
395 xcb_grab_keyboard_cookie_t cookie;
396 xcb_grab_keyboard_reply_t *reply = NULL;
399 while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
400 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
401 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
405 if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
406 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
412 xcb_generic_event_t *event;
413 while ((event = xcb_wait_for_event(conn)) != NULL) {
414 if (event->response_type == 0) {
415 fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
419 /* Strip off the highest bit (set if the event is generated) */
420 int type = (event->response_type & 0x7F);
424 handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
427 case XCB_KEY_RELEASE:
428 handle_key_release(NULL, conn, (xcb_key_release_event_t*)event);
432 handle_expose(NULL, conn, (xcb_expose_event_t*)event);