]> git.sur5r.net Git - i3/i3/blob - i3-input/main.c
30588ffc888f822eee6857f7a1d218e326087e9b
[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
49
50 /*
51  * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
52  * rendering it (UCS-2) or sending it to i3 (UTF-8).
53  *
54  */
55 static uint8_t *concat_strings(char **glyphs, int max) {
56         uint8_t *output = calloc(max, 4);
57         uint8_t *walk = output;
58         for (int c = 0; c < max; c++) {
59                 printf("at %c\n", glyphs[c][0]);
60                 /* if the first byte is 0, this has to be UCS2 */
61                 if (glyphs[c][0] == '\0') {
62                         memcpy(walk, glyphs[c], 2);
63                         walk += 2;
64                 } else {
65                         strcpy((char*)walk, glyphs[c]);
66                         walk += strlen(glyphs[c]);
67                 }
68         }
69         printf("output = %s\n", output);
70         return output;
71 }
72
73 /*
74  * Handles expose events (redraws of the window) and rendering in general. Will
75  * be called from the code with event == NULL or from X with event != NULL.
76  *
77  */
78 static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
79         printf("expose!\n");
80
81         /* re-draw the background */
82         xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4};
83         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
84         xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
85         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
86         xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
87
88         /* restore font color */
89         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
90         uint8_t *con = concat_strings(glyphs_ucs, input_position);
91         xcb_image_text_16(conn, input_position, pixmap, pixmap_gc, 4 /* X */,
92                           font_height + 2 /* Y = baseline of font */, (xcb_char2b_t*)con);
93
94         /* Copy the contents of the pixmap to the real window */
95         xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, font_height + 8);
96         xcb_flush(conn);
97         free(con);
98
99         return 1;
100 }
101
102 /*
103  * Deactivates the Mode_switch bit upon release of the Mode_switch key.
104  *
105  */
106 static int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
107         printf("releasing %d, state raw = %d\n", event->detail, event->state);
108
109         xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
110         if (sym == XK_Mode_switch) {
111                 printf("Mode switch disabled\n");
112                 modeswitch_active = false;
113         }
114
115         return 1;
116 }
117
118 /*
119  * Handles keypresses by converting the keycodes to keysymbols, then the
120  * keysymbols to UCS-2. If the conversion succeeded, the glyph is saved in the
121  * internal buffers and displayed in the input window.
122  *
123  * Also handles backspace (deleting one character) and return (sending the
124  * command to i3).
125  *
126  */
127 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
128         printf("Keypress %d, state raw = %d\n", event->detail, event->state);
129
130         /* fix state */
131         if (modeswitch_active)
132                 event->state |= modeswitchmask;
133
134         xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
135         if (sym == XK_Mode_switch) {
136                 printf("Mode switch enabled\n");
137                 modeswitch_active = true;
138                 return 1;
139         }
140
141         if (sym == XK_Return) {
142                 uint8_t *command = concat_strings(glyphs_utf8, input_position);
143                 printf("command = %s\n", command);
144
145                 ipc_send_message(sockfd, strlen((char*)command), 0, command);
146
147 #if 0
148                 free(command);
149                 return 1;
150 #endif
151                 exit(0);
152         }
153
154         if (sym == XK_BackSpace) {
155                 if (input_position == 0)
156                         return 1;
157
158                 input_position--;
159                 free(glyphs_ucs[input_position]);
160                 free(glyphs_utf8[input_position]);
161
162                 handle_expose(NULL, conn, NULL);
163                 return 1;
164         }
165         if (sym == XK_Escape) {
166                 exit(0);
167         }
168
169         /* TODO: handle all of these? */
170         printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
171         printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
172         printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
173         printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
174         printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
175         printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
176         printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
177
178         if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
179                 return 1;
180
181         printf("sym = %c (%d)\n", sym, sym);
182
183         /* convert the keysym to UCS */
184         uint16_t ucs = keysym2ucs(sym);
185         if ((int16_t)ucs == -1) {
186                 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
187                 return 1;
188         }
189
190         /* store the UCS into a string */
191         uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
192
193         printf("inp[0] = %02x, inp[1] = %02x, inp[2] = %02x\n", inp[0], inp[1], inp[2]);
194         /* convert it to UTF-8 */
195         char *out = convert_ucs_to_utf8((char*)inp);
196         printf("converted to %s\n", out);
197
198         glyphs_ucs[input_position] = malloc(3 * sizeof(uint8_t));
199         if (glyphs_ucs[input_position] == NULL)
200                 err(EXIT_FAILURE, "malloc() failed\n");
201         memcpy(glyphs_ucs[input_position], inp, 3);
202         glyphs_utf8[input_position] = strdup(out);
203         input_position++;
204
205         handle_expose(NULL, conn, NULL);
206         return 1;
207 }
208
209 int main(int argc, char *argv[]) {
210         char *socket_path = "/tmp/i3-ipc.sock";
211         char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
212         int o, option_index = 0;
213
214         static struct option long_options[] = {
215                 {"socket", required_argument, 0, 's'},
216                 {"version", no_argument, 0, 'v'},
217                 {"help", no_argument, 0, 'h'},
218                 {0, 0, 0, 0}
219         };
220
221         char *options_string = "s:t:vh";
222
223         while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
224                 if (o == 's') {
225                         socket_path = strdup(optarg);
226                         break;
227                 } else if (o == 'v') {
228                         printf("i3-input " I3_VERSION);
229                         return 0;
230                 } else if (o == 'h') {
231                         printf("i3-input " I3_VERSION);
232                         printf("i3-input [-s <socket>] [-p <prefix>]\n");
233                         return 0;
234                 }
235         }
236
237         sockfd = connect_ipc(socket_path);
238
239         int screens;
240         xcb_connection_t *conn = xcb_connect(NULL, &screens);
241         if (xcb_connection_has_error(conn))
242                 die("Cannot open display\n");
243
244         /* Set up event handlers for key press and key release */
245         xcb_event_handlers_t evenths;
246         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
247         xcb_event_handlers_init(conn, &evenths);
248         xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
249         xcb_event_set_key_release_handler(&evenths, handle_key_release, NULL);
250         xcb_event_set_expose_handler(&evenths, handle_expose, NULL);
251
252         modeswitchmask = get_mode_switch_mask(conn);
253         symbols = xcb_key_symbols_alloc(conn);
254
255         uint32_t font_id = get_font_id(conn, pattern, &font_height);
256
257         /* Open an input window */
258         win = open_input_window(conn, 500, font_height + 8);
259
260         /* Create pixmap */
261         xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
262
263         pixmap = xcb_generate_id(conn);
264         pixmap_gc = xcb_generate_id(conn);
265         xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, font_height + 8);
266         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
267
268         /* Create graphics context */
269         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
270
271         /* Grab the keyboard to get all input */
272         xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
273
274         xcb_flush(conn);
275
276         xcb_event_wait_for_event_loop(&evenths);
277
278         return 0;
279 }