]> git.sur5r.net Git - i3/i3/blob - i3-config-wizard/main.c
wizard: check if the config file does not already exist and if we can create it
[i3/i3] / i3-config-wizard / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2011 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * i3-config-wizard: Program to convert configs using keycodes to configs using
11  * keysyms.
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 <limits.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28
29 #include <xcb/xcb.h>
30 #include <xcb/xcb_aux.h>
31 #include <xcb/xcb_event.h>
32 #include <xcb/xcb_keysyms.h>
33
34 #include <X11/Xlib.h>
35 #include <X11/keysym.h>
36
37 #define FREE(pointer) do { \
38     if (pointer != NULL) { \
39         free(pointer); \
40         pointer = NULL; \
41     } \
42 } \
43 while (0)
44
45 #include "xcb.h"
46
47 enum { STEP_WELCOME, STEP_GENERATE } current_step = STEP_WELCOME;
48 enum { MOD_ALT, MOD_SUPER } modifier = MOD_SUPER;
49
50 static char *config_path = "/tmp/wizout/i3.config";
51 static xcb_connection_t *conn;
52 static uint32_t font_id;
53 static uint32_t font_bold_id;
54 static char *socket_path;
55 static int sockfd;
56 static int font_height;
57 static int font_bold_height;
58 static xcb_window_t win;
59 static xcb_pixmap_t pixmap;
60 static xcb_gcontext_t pixmap_gc;
61 static xcb_key_symbols_t *symbols;
62 xcb_window_t root;
63 Display *dpy;
64
65 static void finish();
66
67 /*
68  * Try to get the socket path from X11 and return NULL if it doesn’t work.
69  * As i3-msg is a short-running tool, we don’t bother with cleaning up the
70  * connection and leave it up to the operating system on exit.
71  *
72  */
73 static char *socket_path_from_x11() {
74     xcb_connection_t *conn;
75     int screen;
76     if ((conn = xcb_connect(NULL, &screen)) == NULL ||
77         xcb_connection_has_error(conn))
78         return NULL;
79     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screen);
80     xcb_window_t root = root_screen->root;
81
82     xcb_intern_atom_cookie_t atom_cookie;
83     xcb_intern_atom_reply_t *atom_reply;
84
85     atom_cookie = xcb_intern_atom(conn, 0, strlen("I3_SOCKET_PATH"), "I3_SOCKET_PATH");
86     atom_reply = xcb_intern_atom_reply(conn, atom_cookie, NULL);
87     if (atom_reply == NULL)
88         return NULL;
89
90     xcb_get_property_cookie_t prop_cookie;
91     xcb_get_property_reply_t *prop_reply;
92     prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
93                                              XCB_GET_PROPERTY_TYPE_ANY, 0, PATH_MAX);
94     prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
95     if (prop_reply == NULL || xcb_get_property_value_length(prop_reply) == 0)
96         return NULL;
97     if (asprintf(&socket_path, "%.*s", xcb_get_property_value_length(prop_reply),
98                  (char*)xcb_get_property_value(prop_reply)) == -1)
99         return NULL;
100     return socket_path;
101 }
102
103 /*
104  * Handles expose events, that is, draws the window contents.
105  *
106  */
107 static int handle_expose() {
108     /* re-draw the background */
109     xcb_rectangle_t border = {0, 0, 300, (15*font_height) + 8},
110                     inner = {2, 2, 296, (15*font_height) + 8 - 4};
111     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#285577"));
112     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
113     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
114     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
115
116     xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id);
117
118 #define txt(x, row, text) xcb_image_text_8(conn, strlen(text), pixmap, pixmap_gc, x, (row * font_height) + 2, text)
119
120     if (current_step == STEP_WELCOME) {
121         /* restore font color */
122         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
123
124         txt(10, 1, "i3: first configuration");
125         txt(10, 4, "You have not configured i3 yet.");
126         txt(10, 5, "Do you want me to generate ~/.i3/config?");
127         txt(85, 8, "Yes, generate ~/.i3/config");
128         txt(85, 10, "No, I will use the defaults");
129
130         /* green */
131         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#00FF00"));
132         txt(25, 8, "<Enter>");
133
134         /* red */
135         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
136         txt(31, 10, "<ESC>");
137     }
138
139     if (current_step == STEP_GENERATE) {
140         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
141
142         txt(10, 1, "i3: generate config");
143
144         txt(10, 4, "Please choose either:");
145         txt(85, 6, "Win as default modifier");
146         txt(85, 7, "Alt as default modifier");
147         txt(10, 9, "Afterwards, press");
148         txt(85, 11, "to write ~/.i3/config");
149         txt(85, 12, "to abort");
150
151         /* the not-selected modifier */
152         if (modifier == MOD_SUPER)
153             txt(31, 7, "<Alt>");
154         else txt(31, 6, "<Win>");
155
156         /* the selected modifier */
157         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_bold_id);
158         if (modifier == MOD_SUPER)
159             txt(31, 6, "<Win>");
160         else txt(31, 7, "<Alt>");
161
162         /* green */
163         uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_FONT;
164         uint32_t values[] = { get_colorpixel(conn, "#00FF00"), font_id };
165         xcb_change_gc(conn, pixmap_gc, mask, values);
166
167         txt(25, 11, "<Enter>");
168
169         /* red */
170         xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
171         txt(31, 12, "<ESC>");
172     }
173
174     /* Copy the contents of the pixmap to the real window */
175     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, /* */ 500, 500);
176     xcb_flush(conn);
177
178     return 1;
179 }
180
181 static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
182     printf("Keypress %d, state raw = %d\n", event->detail, event->state);
183
184     xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
185
186     printf("sym = %c (%d)\n", sym, sym);
187
188     if (sym == XK_Return) {
189         if (current_step == STEP_WELCOME)
190             current_step = STEP_GENERATE;
191         else finish();
192     }
193
194     /* cancel any time */
195     if (sym == XK_Escape)
196         exit(0);
197
198     if (sym == XK_Alt_L)
199         modifier = MOD_ALT;
200
201     if (sym == XK_Super_L)
202         modifier = MOD_SUPER;
203
204     handle_expose();
205     return 1;
206 }
207
208 static void finish() {
209     printf("finishing the wizard\n");
210
211 #if 0
212     dpy = XOpenDisplay(NULL);
213
214     FILE *kc_config = fopen("../i3.config.kc", "r");
215     char *line = NULL;
216     size_t len = 0;
217     ssize_t read;
218     while ((read = getline(&line, &len, kc_config)) != -1) {
219         /* See if that line is interesting by skipping leading whitespaces,
220          * then checking for 'bindcode' */
221         char *walk = line;
222         while (isspace(*walk) && walk < (line + len))
223             walk++;
224         if (strncmp(walk, "bindcode", strlen("bindcode")) != 0)
225             continue;
226         char *result = rewrite_binding(walk);
227         printf("in:  %s", walk);
228         printf("out: %s", result);
229         free(result);
230
231     }
232
233     free(line);
234     fclose(kc_config);
235
236     exit(0);
237 #endif
238
239     exit(0);
240 }
241
242 int main(int argc, char *argv[]) {
243     socket_path = getenv("I3SOCK");
244     char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1";
245     char *patternbold = "-misc-fixed-bold-r-normal--13-120-75-75-C-70-iso10646-1";
246     int o, option_index = 0;
247
248     static struct option long_options[] = {
249         {"socket", required_argument, 0, 's'},
250         {"version", no_argument, 0, 'v'},
251         {"limit", required_argument, 0, 'l'},
252         {"prompt", required_argument, 0, 'P'},
253         {"prefix", required_argument, 0, 'p'},
254         {"font", required_argument, 0, 'f'},
255         {"help", no_argument, 0, 'h'},
256         {0, 0, 0, 0}
257     };
258
259     char *options_string = "s:vh";
260
261     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
262         switch (o) {
263             case 's':
264                 FREE(socket_path);
265                 socket_path = strdup(optarg);
266                 break;
267             case 'v':
268                 printf("i3-config-wizard " I3_VERSION);
269                 return 0;
270             case 'h':
271                 printf("i3-config-wizard " I3_VERSION);
272                 printf("i3-config-wizard [-s <socket>] [-v]\n");
273                 return 0;
274         }
275     }
276
277     /* Check if the destination config file does not exist but the path is
278      * writable. If not, exit now, this program is not useful in that case. */
279     struct stat stbuf;
280     if (stat(config_path, &stbuf) == 0) {
281         printf("The config file \"%s\" already exists. Exiting.\n", config_path);
282         return 0;
283     }
284
285     int fd;
286     if ((fd = open(config_path, O_CREAT | O_RDWR, 0644)) == -1) {
287         printf("Cannot open file \"%s\" for writing: %s. Exiting.\n", config_path, strerror(errno));
288         return 0;
289     }
290     close(fd);
291     unlink(config_path);
292
293     if (socket_path == NULL)
294         socket_path = socket_path_from_x11();
295
296     if (socket_path == NULL)
297         socket_path = "/tmp/i3-ipc.sock";
298
299     int screens;
300     conn = xcb_connect(NULL, &screens);
301     if (xcb_connection_has_error(conn))
302         errx(1, "Cannot open display\n");
303
304     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
305     root = root_screen->root;
306
307     symbols = xcb_key_symbols_alloc(conn);
308
309     font_id = get_font_id(conn, pattern, &font_height);
310     font_bold_id = get_font_id(conn, patternbold, &font_bold_height);
311
312     /* Open an input window */
313     win = open_input_window(conn, 300, 205);
314
315     /* Create pixmap */
316     pixmap = xcb_generate_id(conn);
317     pixmap_gc = xcb_generate_id(conn);
318     xcb_create_pixmap(conn, root_screen->root_depth, pixmap, win, 500, 500);
319     xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
320
321     /* Set input focus (we have override_redirect=1, so the wm will not do
322      * this for us) */
323     xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
324
325     /* Grab the keyboard to get all input */
326     xcb_flush(conn);
327
328     /* Try (repeatedly, if necessary) to grab the keyboard. We might not
329      * get the keyboard at the first attempt because of the keybinding
330      * still being active when started via a wm’s keybinding. */
331     xcb_grab_keyboard_cookie_t cookie;
332     xcb_grab_keyboard_reply_t *reply = NULL;
333
334     int count = 0;
335     while ((reply == NULL || reply->status != XCB_GRAB_STATUS_SUCCESS) && (count++ < 500)) {
336         cookie = xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
337         reply = xcb_grab_keyboard_reply(conn, cookie, NULL);
338         usleep(1000);
339     }
340
341     if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
342         fprintf(stderr, "Could not grab keyboard, status = %d\n", reply->status);
343         exit(-1);
344     }
345
346     xcb_flush(conn);
347
348     xcb_generic_event_t *event;
349     while ((event = xcb_wait_for_event(conn)) != NULL) {
350         if (event->response_type == 0) {
351             fprintf(stderr, "X11 Error received! sequence %x\n", event->sequence);
352             continue;
353         }
354
355         /* Strip off the highest bit (set if the event is generated) */
356         int type = (event->response_type & 0x7F);
357
358         switch (type) {
359             case XCB_KEY_PRESS:
360                 handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
361                 break;
362
363             /* TODO: handle mappingnotify */
364
365             case XCB_EXPOSE:
366                 handle_expose();
367                 break;
368         }
369
370         free(event);
371     }
372
373     return 0;
374 }