]> git.sur5r.net Git - i3/i3/blob - i3-input/main.c
Bugfix: i3-input: Allocate one more character for null-termination
[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 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;
49 static char *prompt;
50 static int prompt_len;
51 static int limit;
52
53 /*
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).
56  *
57  */
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);
66                         walk += 2;
67                 } else {
68                         strcpy((char*)walk, glyphs[c]);
69                         walk += strlen(glyphs[c]);
70                 }
71         }
72         printf("output = %s\n", output);
73         return output;
74 }
75
76 /*
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.
79  *
80  */
81 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
82         printf("expose!\n");
83
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);
90
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;
95         if (prompt != NULL) {
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);
101         }
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);
104
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);
107         xcb_flush(conn);
108         free(con);
109         if (prompt != NULL)
110                 free(full_text);
111
112         return 1;
113 }
114
115 /*
116  * Deactivates the Mode_switch bit upon release of the Mode_switch key.
117  *
118  */
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);
121
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;
126         }
127
128         return 1;
129 }
130
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");
138         }
139         printf("command = %s\n", full_command);
140
141         ipc_send_message(sockfd, strlen(full_command), 0, (uint8_t*)full_command);
142
143 #if 0
144         free(command);
145         return 1;
146 #endif
147         exit(0);
148 }
149
150 /*
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.
154  *
155  * Also handles backspace (deleting one character) and return (sending the
156  * command to i3).
157  *
158  */
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);
161
162         /* fix state */
163         if (modeswitch_active)
164                 event->state |= modeswitchmask;
165
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;
170                 return 1;
171         }
172
173         if (sym == XK_Return)
174                 finish_input();
175
176         if (sym == XK_BackSpace) {
177                 if (input_position == 0)
178                         return 1;
179
180                 input_position--;
181                 free(glyphs_ucs[input_position]);
182                 free(glyphs_utf8[input_position]);
183
184                 handle_expose(NULL, conn, NULL);
185                 return 1;
186         }
187         if (sym == XK_Escape) {
188                 exit(0);
189         }
190
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));
199
200         if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
201                 return 1;
202
203         printf("sym = %c (%d)\n", sym, sym);
204
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");
209                 return 1;
210         }
211
212         /* store the UCS into a string */
213         uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
214
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);
219
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);
225         input_position++;
226
227         if (input_position == limit)
228                 finish_input();
229
230         handle_expose(NULL, conn, NULL);
231         return 1;
232 }
233
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;
238
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'},
246                 {0, 0, 0, 0}
247         };
248
249         char *options_string = "s:p:P:l:vh";
250
251         while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
252                 switch (o) {
253                         case 's':
254                                 socket_path = strdup(optarg);
255                                 break;
256                         case 'v':
257                                 printf("i3-input " I3_VERSION);
258                                 return 0;
259                         case 'p':
260                                 command_prefix = strdup(optarg);
261                                 break;
262                         case 'l':
263                                 limit = atoi(optarg);
264                                 break;
265                         case 'P':
266                                 prompt = strdup(optarg);
267                                 break;
268                         case 'h':
269                                 printf("i3-input " I3_VERSION);
270                                 printf("i3-input [-s <socket>] [-p <prefix>] [-l <limit>] [-P <prompt>] [-v]\n");
271                                 return 0;
272                 }
273         }
274
275         sockfd = connect_ipc(socket_path);
276
277         if (prompt != NULL)
278                 prompt = convert_utf8_to_ucs2(prompt, &prompt_len);
279
280         int screens;
281         xcb_connection_t *conn = xcb_connect(NULL, &screens);
282         if (xcb_connection_has_error(conn))
283                 die("Cannot open display\n");
284
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);
292
293         modeswitchmask = get_mode_switch_mask(conn);
294         symbols = xcb_key_symbols_alloc(conn);
295
296         uint32_t font_id = get_font_id(conn, pattern, &font_height);
297
298         /* Open an input window */
299         win = open_input_window(conn, 500, font_height + 8);
300
301         /* Create pixmap */
302         xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
303
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);
308
309         /* Create graphics context */
310         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
311
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);
314
315         xcb_flush(conn);
316
317         xcb_event_wait_for_event_loop(&evenths);
318
319         return 0;
320 }