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 bool modeswitch_active = false;
41 static xcb_window_t win;
42 static xcb_pixmap_t pixmap;
43 static xcb_gcontext_t pixmap_gc;
44 static char *glyphs_ucs[512];
45 static char *glyphs_utf8[512];
46 static int input_position;
47 static int font_height;
48 static char *command_prefix;
50 static int prompt_len;
54 * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
55 * rendering it (UCS-2) or sending it to i3 (UTF-8).
58 static uint8_t *concat_strings(char **glyphs, int max) {
59 uint8_t *output = calloc(max+1, 4);
60 uint8_t *walk = output;
61 for (int c = 0; c < max; c++) {
62 printf("at %c\n", glyphs[c][0]);
63 /* if the first byte is 0, this has to be UCS2 */
64 if (glyphs[c][0] == '\0') {
65 memcpy(walk, glyphs[c], 2);
68 strcpy((char*)walk, glyphs[c]);
69 walk += strlen(glyphs[c]);
72 printf("output = %s\n", output);
77 * Handles expose events (redraws of the window) and rendering in general. Will
78 * be called from the code with event == NULL or from X with event != NULL.
81 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
84 /* re-draw the background */
85 xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4};
86 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
87 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
88 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
89 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
91 /* restore font color */
92 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
93 uint8_t *con = concat_strings(glyphs_ucs, input_position);
94 char *full_text = (char*)con;
96 full_text = malloc((prompt_len + input_position) * 2 + 1);
97 if (full_text == NULL)
98 err(EXIT_FAILURE, "malloc() failed\n");
99 memcpy(full_text, prompt, prompt_len * 2);
100 memcpy(full_text + (prompt_len * 2), con, input_position * 2);
102 xcb_image_text_16(conn, input_position + prompt_len, pixmap, pixmap_gc, 4 /* X */,
103 font_height + 2 /* Y = baseline of font */, (xcb_char2b_t*)full_text);
105 /* Copy the contents of the pixmap to the real window */
106 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, font_height + 8);
116 * Deactivates the Mode_switch bit upon release of the Mode_switch key.
119 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
120 printf("releasing %d, state raw = %d\n", event->detail, event->state);
122 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
123 if (sym == XK_Mode_switch) {
124 printf("Mode switch disabled\n");
125 modeswitch_active = false;
131 static void finish_input() {
132 uint8_t *command = concat_strings(glyphs_utf8, input_position);
133 char *full_command = (char*)command;
134 /* prefix the command if a prefix was specified on commandline */
135 if (command_prefix != NULL) {
136 if (asprintf(&full_command, "%s%s", command_prefix, command) == -1)
137 err(EXIT_FAILURE, "asprintf() failed\n");
139 printf("command = %s\n", full_command);
141 ipc_send_message(sockfd, strlen(full_command), 0, (uint8_t*)full_command);
151 * Handles keypresses by converting the keycodes to keysymbols, then the
152 * keysymbols to UCS-2. If the conversion succeeded, the glyph is saved in the
153 * internal buffers and displayed in the input window.
155 * Also handles backspace (deleting one character) and return (sending the
159 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
160 printf("Keypress %d, state raw = %d\n", event->detail, event->state);
163 if (modeswitch_active)
164 event->state |= modeswitchmask;
166 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
167 if (sym == XK_Mode_switch) {
168 printf("Mode switch enabled\n");
169 modeswitch_active = true;
173 if (sym == XK_Return)
176 if (sym == XK_BackSpace) {
177 if (input_position == 0)
181 free(glyphs_ucs[input_position]);
182 free(glyphs_utf8[input_position]);
184 handle_expose(NULL, conn, NULL);
187 if (sym == XK_Escape) {
191 /* TODO: handle all of these? */
192 printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
193 printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
194 printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
195 printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
196 printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
197 printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
198 printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
200 if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
203 printf("sym = %c (%d)\n", sym, sym);
205 /* convert the keysym to UCS */
206 uint16_t ucs = keysym2ucs(sym);
207 if ((int16_t)ucs == -1) {
208 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
212 /* store the UCS into a string */
213 uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
215 printf("inp[0] = %02x, inp[1] = %02x, inp[2] = %02x\n", inp[0], inp[1], inp[2]);
216 /* convert it to UTF-8 */
217 char *out = convert_ucs_to_utf8((char*)inp);
218 printf("converted to %s\n", out);
220 glyphs_ucs[input_position] = malloc(3 * sizeof(uint8_t));
221 if (glyphs_ucs[input_position] == NULL)
222 err(EXIT_FAILURE, "malloc() failed\n");
223 memcpy(glyphs_ucs[input_position], inp, 3);
224 glyphs_utf8[input_position] = strdup(out);
227 if (input_position == limit)
230 handle_expose(NULL, conn, NULL);
234 int main(int argc, char *argv[]) {
235 char *socket_path = "/tmp/i3-ipc.sock";
236 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
237 int o, option_index = 0;
239 static struct option long_options[] = {
240 {"socket", required_argument, 0, 's'},
241 {"version", no_argument, 0, 'v'},
242 {"limit", required_argument, 0, 'l'},
243 {"prompt", required_argument, 0, 'P'},
244 {"prefix", required_argument, 0, 'p'},
245 {"help", no_argument, 0, 'h'},
249 char *options_string = "s:p:P:l:vh";
251 while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
254 socket_path = strdup(optarg);
257 printf("i3-input " I3_VERSION);
260 command_prefix = strdup(optarg);
263 limit = atoi(optarg);
266 prompt = strdup(optarg);
269 printf("i3-input " I3_VERSION);
270 printf("i3-input [-s <socket>] [-p <prefix>] [-l <limit>] [-P <prompt>] [-v]\n");
275 sockfd = connect_ipc(socket_path);
278 prompt = convert_utf8_to_ucs2(prompt, &prompt_len);
281 xcb_connection_t *conn = xcb_connect(NULL, &screens);
282 if (xcb_connection_has_error(conn))
283 die("Cannot open display\n");
285 /* Set up event handlers for key press and key release */
286 xcb_event_handlers_t evenths;
287 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
288 xcb_event_handlers_init(conn, &evenths);
289 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
290 xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
291 xcb_event_set_expose_handler(&evenths, handle_expose, NULL);
293 modeswitchmask = get_mode_switch_mask(conn);
294 symbols = xcb_key_symbols_alloc(conn);
296 uint32_t font_id = get_font_id(conn, pattern, &font_height);
298 /* Open an input window */
299 win = open_input_window(conn, 500, font_height + 8);
302 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
304 pixmap = xcb_generate_id(conn);
305 pixmap_gc = xcb_generate_id(conn);
306 xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
307 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
309 /* Create graphics context */
310 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
312 /* Grab the keyboard to get all input */
313 xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
317 xcb_event_wait_for_event_loop(&evenths);