]> git.sur5r.net Git - i3/i3/blob - i3-input/main.c
Add font-option to i3-input
[i3/i3] / i3-input / main.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 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 #include <glob.h>
26
27 #include <xcb/xcb.h>
28 #include <xcb/xcb_aux.h>
29 #include <xcb/xcb_event.h>
30 #include <xcb/xcb_keysyms.h>
31
32 #include <X11/keysym.h>
33
34 #include "keysym2ucs.h"
35
36 #include "i3-input.h"
37
38 static int sockfd;
39 static xcb_key_symbols_t *symbols;
40 static int modeswitchmask;
41 static int numlockmask;
42 static bool modeswitch_active = false;
43 static xcb_window_t win;
44 static xcb_pixmap_t pixmap;
45 static xcb_gcontext_t pixmap_gc;
46 static char *glyphs_ucs[512];
47 static char *glyphs_utf8[512];
48 static int input_position;
49 static int font_height;
50 static char *command_prefix;
51 static char *prompt;
52 static int prompt_len;
53 static int limit;
54
55 /*
56  * This function resolves ~ in pathnames (and more, see glob(3)).
57  *
58  */
59 static char *glob_path(const char *path) {
60         static glob_t globbuf;
61         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
62                 errx(EXIT_FAILURE, "glob() failed");
63         char *result = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
64         if (result == NULL)
65                 err(EXIT_FAILURE, "malloc() failed");
66         globfree(&globbuf);
67         return result;
68 }
69
70 /*
71  * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
72  * rendering it (UCS-2) or sending it to i3 (UTF-8).
73  *
74  */
75 static uint8_t *concat_strings(char **glyphs, int max) {
76         uint8_t *output = calloc(max+1, 4);
77         uint8_t *walk = output;
78         for (int c = 0; c < max; c++) {
79                 printf("at %c\n", glyphs[c][0]);
80                 /* if the first byte is 0, this has to be UCS2 */
81                 if (glyphs[c][0] == '\0') {
82                         memcpy(walk, glyphs[c], 2);
83                         walk += 2;
84                 } else {
85                         strcpy((char*)walk, glyphs[c]);
86                         walk += strlen(glyphs[c]);
87                 }
88         }
89         printf("output = %s\n", output);
90         return output;
91 }
92
93 /*
94  * Handles expose events (redraws of the window) and rendering in general. Will
95  * be called from the code with event == NULL or from X with event != NULL.
96  *
97  */
98 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
99         printf("expose!\n");
100
101         /* re-draw the background */
102         xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4};
103         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
104         xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
105         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
106         xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
107
108         /* restore font color */
109         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
110         uint8_t *con = concat_strings(glyphs_ucs, input_position);
111         char *full_text = (char*)con;
112         if (prompt != NULL) {
113                 full_text = malloc((prompt_len + input_position) * 2 + 1);
114                 if (full_text == NULL)
115                         err(EXIT_FAILURE, "malloc() failed\n");
116                 memcpy(full_text, prompt, prompt_len * 2);
117                 memcpy(full_text + (prompt_len * 2), con, input_position * 2);
118         }
119         xcb_image_text_16(conn, input_position + prompt_len, pixmap, pixmap_gc, 4 /* X */,
120                           font_height + 2 /* Y = baseline of font */, (xcb_char2b_t*)full_text);
121
122         /* Copy the contents of the pixmap to the real window */
123         xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, font_height + 8);
124         xcb_flush(conn);
125         free(con);
126         if (prompt != NULL)
127                 free(full_text);
128
129         return 1;
130 }
131
132 /*
133  * Deactivates the Mode_switch bit upon release of the Mode_switch key.
134  *
135  */
136 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
137         printf("releasing %d, state raw = %d\n", event->detail, event->state);
138
139         /* fix state */
140         event->state &= ~numlockmask;
141
142         xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
143         if (sym == XK_Mode_switch) {
144                 printf("Mode switch disabled\n");
145                 modeswitch_active = false;
146         }
147
148         return 1;
149 }
150
151 static void finish_input() {
152         uint8_t *command = concat_strings(glyphs_utf8, input_position);
153         char *full_command = (char*)command;
154         /* prefix the command if a prefix was specified on commandline */
155         if (command_prefix != NULL) {
156                 if (asprintf(&full_command, "%s%s", command_prefix, command) == -1)
157                         err(EXIT_FAILURE, "asprintf() failed\n");
158         }
159         printf("command = %s\n", full_command);
160
161         ipc_send_message(sockfd, strlen(full_command), 0, (uint8_t*)full_command);
162
163 #if 0
164         free(command);
165         return 1;
166 #endif
167         exit(0);
168 }
169
170 /*
171  * Handles keypresses by converting the keycodes to keysymbols, then the
172  * keysymbols to UCS-2. If the conversion succeeded, the glyph is saved in the
173  * internal buffers and displayed in the input window.
174  *
175  * Also handles backspace (deleting one character) and return (sending the
176  * command to i3).
177  *
178  */
179 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
180         printf("Keypress %d, state raw = %d\n", event->detail, event->state);
181
182         /* fix state */
183         if (modeswitch_active)
184                 event->state |= modeswitchmask;
185
186         /* Apparantly, after activating numlock once, the numlock modifier
187          * stays turned on (use xev(1) to verify). So, to resolve useful
188          * keysyms, we remove the numlock flag from the event state */
189         event->state &= ~numlockmask;
190
191         xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
192         if (sym == XK_Mode_switch) {
193                 printf("Mode switch enabled\n");
194                 modeswitch_active = true;
195                 return 1;
196         }
197
198         if (sym == XK_Return)
199                 finish_input();
200
201         if (sym == XK_BackSpace) {
202                 if (input_position == 0)
203                         return 1;
204
205                 input_position--;
206                 free(glyphs_ucs[input_position]);
207                 free(glyphs_utf8[input_position]);
208
209                 handle_expose(NULL, conn, NULL);
210                 return 1;
211         }
212         if (sym == XK_Escape) {
213                 exit(0);
214         }
215
216         /* TODO: handle all of these? */
217         printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
218         printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
219         printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
220         printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
221         printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
222         printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
223         printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
224
225         if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
226                 return 1;
227
228         printf("sym = %c (%d)\n", sym, sym);
229
230         /* convert the keysym to UCS */
231         uint16_t ucs = keysym2ucs(sym);
232         if ((int16_t)ucs == -1) {
233                 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
234                 return 1;
235         }
236
237         /* store the UCS into a string */
238         uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
239
240         printf("inp[0] = %02x, inp[1] = %02x, inp[2] = %02x\n", inp[0], inp[1], inp[2]);
241         /* convert it to UTF-8 */
242         char *out = convert_ucs_to_utf8((char*)inp);
243         printf("converted to %s\n", out);
244
245         glyphs_ucs[input_position] = malloc(3 * sizeof(uint8_t));
246         if (glyphs_ucs[input_position] == NULL)
247                 err(EXIT_FAILURE, "malloc() failed\n");
248         memcpy(glyphs_ucs[input_position], inp, 3);
249         glyphs_utf8[input_position] = strdup(out);
250         input_position++;
251
252         if (input_position == limit)
253                 finish_input();
254
255         handle_expose(NULL, conn, NULL);
256         return 1;
257 }
258
259 int main(int argc, char *argv[]) {
260         char *socket_path = glob_path("~/.i3/ipc.sock");
261         char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
262         int o, option_index = 0;
263
264         static struct option long_options[] = {
265                 {"socket", required_argument, 0, 's'},
266                 {"version", no_argument, 0, 'v'},
267                 {"limit", required_argument, 0, 'l'},
268                 {"prompt", required_argument, 0, 'P'},
269                 {"prefix", required_argument, 0, 'p'},
270                 {"font", required_argument, 0, 'f'},
271                 {"help", no_argument, 0, 'h'},
272                 {0, 0, 0, 0}
273         };
274
275         char *options_string = "s:p:P:f:l:vh";
276
277         while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
278                 switch (o) {
279                         case 's':
280                                 socket_path = glob_path(optarg);
281                                 break;
282                         case 'v':
283                                 printf("i3-input " I3_VERSION);
284                                 return 0;
285                         case 'p':
286                                 command_prefix = strdup(optarg);
287                                 break;
288                         case 'l':
289                                 limit = atoi(optarg);
290                                 break;
291                         case 'P':
292                                 prompt = strdup(optarg);
293                                 break;
294                         case 'f':
295                                 pattern = strdup(optarg);
296                                 break;
297                         case 'h':
298                                 printf("i3-input " I3_VERSION);
299                                 printf("i3-input [-s <socket>] [-p <prefix>] [-l <limit>] [-P <prompt>] [-f <font>] [-v]\n");
300                                 return 0;
301                 }
302         }
303
304         sockfd = connect_ipc(socket_path);
305
306         if (prompt != NULL)
307                 prompt = convert_utf8_to_ucs2(prompt, &prompt_len);
308
309         int screens;
310         xcb_connection_t *conn = xcb_connect(NULL, &screens);
311         if (xcb_connection_has_error(conn))
312                 die("Cannot open display\n");
313
314         /* Set up event handlers for key press and key release */
315         xcb_event_handlers_t evenths;
316         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
317         xcb_event_handlers_init(conn, &evenths);
318         xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
319         xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
320         xcb_event_set_expose_handler(&evenths, handle_expose, NULL);
321
322         modeswitchmask = get_mod_mask(conn, XK_Mode_switch);
323         numlockmask = get_mod_mask(conn, XK_Num_Lock);
324         symbols = xcb_key_symbols_alloc(conn);
325
326         uint32_t font_id = get_font_id(conn, pattern, &font_height);
327
328         /* Open an input window */
329         win = open_input_window(conn, 500, font_height + 8);
330
331         /* Create pixmap */
332         xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
333
334         pixmap = xcb_generate_id(conn);
335         pixmap_gc = xcb_generate_id(conn);
336         xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
337         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
338
339         /* Set input focus (we have override_redirect=1, so the wm will not do
340          * this for us) */
341         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
342
343         /* Create graphics context */
344         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
345
346         /* Grab the keyboard to get all input */
347         xcb_flush(conn);
348
349         /* Try (repeatedly, if necessary) to grab the keyboard. We might not
350          * get the keyboard at the first attempt because of the keybinding
351          * still being active when started via a wm’s keybinding. */
352         xcb_grab_keyboard_cookie_t cookie;
353         xcb_grab_keyboard_reply_t *reply = NULL;
354
355         int count = 0;
356         while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
357                 cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
358                 reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
359                 usleep(1000);
360         }
361
362         if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
363                 fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
364                 exit(-1);
365         }
366
367         xcb_flush(conn);
368
369         xcb_event_wait_for_event_loop(&evenths);
370
371         return 0;
372 }