]> git.sur5r.net Git - i3/i3/blob - i3-input/main.c
i3-input: remove dependency on xcb-event
[i3/i3] / i3-input / main.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * i3-input/main.c: Utility which lets the user input commands and sends them
11  * to i3.
12  *
13  */
14 #include <ev.h>
15 #include <stdio.h>
16 #include <sys/types.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <err.h>
23 #include <stdint.h>
24 #include <getopt.h>
25
26 #include <xcb/xcb.h>
27 #include <xcb/xcb_aux.h>
28 #include <xcb/xcb_event.h>
29 #include <xcb/xcb_keysyms.h>
30
31 #include <X11/keysym.h>
32
33 #include "keysym2ucs.h"
34
35 #include "i3-input.h"
36
37 static int sockfd;
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;
50 static char *prompt;
51 static int prompt_len;
52 static int limit;
53 xcb_window_t root;
54
55 /*
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).
58  *
59  */
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);
68                         walk += 2;
69                 } else {
70                         strcpy((char*)walk, glyphs[c]);
71                         walk += strlen(glyphs[c]);
72                 }
73         }
74         printf("output = %s\n", output);
75         return output;
76 }
77
78 /*
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.
81  *
82  */
83 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
84         printf("expose!\n");
85
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);
92
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;
97         if (prompt != NULL) {
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);
103         }
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);
106
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);
109         xcb_flush(conn);
110         free(con);
111         if (prompt != NULL)
112                 free(full_text);
113
114         return 1;
115 }
116
117 /*
118  * Deactivates the Mode_switch bit upon release of the Mode_switch key.
119  *
120  */
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);
123
124         /* fix state */
125         event->state &= ~numlockmask;
126
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;
131         }
132
133         return 1;
134 }
135
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");
143         }
144         printf("command = %s\n", full_command);
145
146         ipc_send_message(sockfd, strlen(full_command), 0, (uint8_t*)full_command);
147
148 #if 0
149         free(command);
150         return 1;
151 #endif
152         exit(0);
153 }
154
155 /*
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.
159  *
160  * Also handles backspace (deleting one character) and return (sending the
161  * command to i3).
162  *
163  */
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);
166
167         /* fix state */
168         if (modeswitch_active)
169                 event->state |= modeswitchmask;
170
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;
175
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;
180                 return 1;
181         }
182
183         if (sym == XK_Return)
184                 finish_input();
185
186         if (sym == XK_BackSpace) {
187                 if (input_position == 0)
188                         return 1;
189
190                 input_position--;
191                 free(glyphs_ucs[input_position]);
192                 free(glyphs_utf8[input_position]);
193
194                 handle_expose(NULL, conn, NULL);
195                 return 1;
196         }
197         if (sym == XK_Escape) {
198                 exit(0);
199         }
200
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));
209
210         if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
211                 return 1;
212
213         printf("sym = %c (%d)\n", sym, sym);
214
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");
219                 return 1;
220         }
221
222         /* store the UCS into a string */
223         uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
224
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);
229
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);
235         input_position++;
236
237         if (input_position == limit)
238                 finish_input();
239
240         handle_expose(NULL, conn, NULL);
241         return 1;
242 }
243
244 int main(int argc, char *argv[]) {
245         char *socket_path;
246         if ((socket_path = getenv("I3SOCK")) == NULL) {
247                 socket_path = "/tmp/i3-ipc.sock";
248         }
249         char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
250         int o, option_index = 0;
251
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'},
260                 {0, 0, 0, 0}
261         };
262
263         char *options_string = "s:p:P:f:l:vh";
264
265         while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
266                 switch (o) {
267                         case 's':
268                                 FREE(socket_path);
269                                 socket_path = strdup(optarg);
270                                 break;
271                         case 'v':
272                                 printf("i3-input " I3_VERSION);
273                                 return 0;
274                         case 'p':
275                                 FREE(command_prefix);
276                                 command_prefix = strdup(optarg);
277                                 break;
278                         case 'l':
279                                 limit = atoi(optarg);
280                                 break;
281                         case 'P':
282                                 FREE(prompt);
283                                 prompt = strdup(optarg);
284                                 break;
285                         case 'f':
286                                 FREE(pattern);
287                                 pattern = strdup(optarg);
288                                 break;
289                         case 'h':
290                                 printf("i3-input " I3_VERSION);
291                                 printf("i3-input [-s <socket>] [-p <prefix>] [-l <limit>] [-P <prompt>] [-f <font>] [-v]\n");
292                                 return 0;
293                 }
294         }
295
296         sockfd = connect_ipc(socket_path);
297
298         if (prompt != NULL)
299                 prompt = convert_utf8_to_ucs2(prompt, &prompt_len);
300
301         int screens;
302         xcb_connection_t *conn = xcb_connect(NULL, &screens);
303         if (xcb_connection_has_error(conn))
304                 die("Cannot open display\n");
305
306         xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
307         root = root_screen->root;
308
309         modeswitchmask = get_mod_mask(conn, XK_Mode_switch);
310         numlockmask = get_mod_mask(conn, XK_Num_Lock);
311         symbols = xcb_key_symbols_alloc(conn);
312
313         uint32_t font_id = get_font_id(conn, pattern, &font_height);
314
315         /* Open an input window */
316         win = open_input_window(conn, 500, font_height + 8);
317
318         /* Create pixmap */
319         pixmap = xcb_generate_id(conn);
320         pixmap_gc = xcb_generate_id(conn);
321         xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
322         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
323
324         /* Set input focus (we have override_redirect=1, so the wm will not do
325          * this for us) */
326         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
327
328         /* Create graphics context */
329         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
330
331         /* Grab the keyboard to get all input */
332         xcb_flush(conn);
333
334         /* Try (repeatedly, if necessary) to grab the keyboard. We might not
335          * get the keyboard at the first attempt because of the keybinding
336          * still being active when started via a wm’s keybinding. */
337         xcb_grab_keyboard_cookie_t cookie;
338         xcb_grab_keyboard_reply_t *reply = NULL;
339
340         int count = 0;
341         while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
342                 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
343                 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
344                 usleep(1000);
345         }
346
347         if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
348                 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
349                 exit(-1);
350         }
351
352         xcb_flush(conn);
353
354         xcb_generic_event_t *event;
355         while ((event = xcb_wait_for_event(conn)) != NULL) {
356                 if (event->response_type == 0) {
357                         fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
358                         continue;
359                 }
360
361                 /* Strip off the highest bit (set if the event is generated) */
362                 int type = (event->response_type & 0x7F);
363
364                 switch (type) {
365                         case XCB_KEY_PRESS:
366                                 handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
367                                 break;
368
369                         case XCB_KEY_RELEASE:
370                                 handle_key_release(NULL, conn, (xcb_key_release_event_t*)event);
371                                 break;
372
373                         case XCB_EXPOSE:
374                                 handle_expose(NULL, conn, (xcb_expose_event_t*)event);
375                                 break;
376                 }
377
378                 free(event);
379         }
380
381         return 0;
382 }