]> git.sur5r.net Git - i3/i3/blob - src/mainx.c
Move stuff to include/ and src/
[i3/i3] / src / mainx.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <stdbool.h>
9 #include <assert.h>
10
11 #include <xcb/xcb.h>
12
13 #include <X11/XKBlib.h>
14 #include <X11/extensions/XKB.h>
15
16 #include <xcb/xcb_wm.h>
17 #include <xcb/xcb_aux.h>
18 #include <xcb/xcb_event.h>
19 #include <xcb/xcb_property.h>
20 #include <xcb/xcb_keysyms.h>
21 #include <xcb/xcb_icccm.h>
22 #include <xcb/xinerama.h>
23 #include "data.h"
24
25 #include "queue.h"
26 #include "table.h"
27 #include "font.h"
28 #include "layout.h"
29 #include "debug.h"
30 #include "handlers.h"
31 #include "util.h"
32
33 #define TERMINAL "/usr/pkg/bin/urxvt"
34
35 Display *xkbdpy;
36
37 TAILQ_HEAD(bindings_head, Binding) bindings;
38 xcb_event_handlers_t evenths;
39
40 static const int TOP = 20;
41 static const int LEFT = 5;
42 static const int BOTTOM = 5;
43 static const int RIGHT = 5;
44
45 /* This is the filtered environment which will be passed to opened applications.
46  * It contains DISPLAY (naturally) and locales stuff (LC_*, LANG) */
47 char **environment;
48
49 /* hm, xcb_wm wants us to implement this. */
50 table_t *byChild = 0;
51 table_t *byParent = 0;
52 xcb_window_t root_win;
53
54 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
55 int num_screens = 0;
56
57 /*
58  *
59  * TODO: what exactly does this, what happens if we leave stuff out?
60  *
61  */
62 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
63 {
64         printf("managing window.\n");
65         xcb_drawable_t d = { window };
66         xcb_get_geometry_cookie_t geomc;
67         xcb_get_geometry_reply_t *geom;
68         xcb_get_window_attributes_reply_t *attr = 0;
69         if(wa.tag == TAG_COOKIE)
70         {
71                 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
72                 if(!attr)
73                         return;
74                 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
75                 {
76                         printf("Window 0x%08x is not mapped. Ignoring.\n", window);
77                         free(attr);
78                         return;
79                 }
80                 wa.tag = TAG_VALUE;
81                 wa.u.override_redirect = attr->override_redirect;
82         }
83         if(!wa.u.override_redirect && table_get(byChild, window))
84         {
85                 printf("Window 0x%08x already managed. Ignoring.\n", window);
86                 free(attr);
87                 return;
88         }
89         if(wa.u.override_redirect)
90         {
91                 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
92                 free(attr);
93                 return;
94         }
95         geomc = xcb_get_geometry(c, d);
96         if(!attr)
97         {
98                 wa.tag = TAG_COOKIE;
99                 wa.u.cookie = xcb_get_window_attributes(c, window);
100                 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
101         }
102         geom = xcb_get_geometry_reply(c, geomc, 0);
103         if(attr && geom)
104         {
105                 reparent_window(c, window, attr->visual, geom->root, geom->depth, geom->x, geom->y, geom->width, geom->height);
106                 xcb_property_changed(prophs, XCB_PROPERTY_NEW_VALUE, window, WM_NAME);
107         }
108         free(attr);
109         free(geom);
110 }
111
112 /*
113  * Let’s own this window…
114  *
115  */
116 void reparent_window(xcb_connection_t *conn, xcb_window_t child,
117                 xcb_visualid_t visual, xcb_window_t root, uint8_t depth,
118                 int16_t x, int16_t y, uint16_t width, uint16_t height) {
119
120         Client *new = table_get(byChild, child);
121         if (new == NULL) {
122                 printf("oh, it's new\n");
123                 new = calloc(sizeof(Client), 1);
124                 new->x = -1;
125                 new->y = -1;
126         }
127         uint32_t mask = 0;
128         uint32_t values[3];
129
130         /* Insert into the currently active container */
131         CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
132
133         printf("currently_focused = %p\n", new);
134         CUR_CELL->currently_focused = new;
135         new->container = CUR_CELL;
136
137         new->frame = xcb_generate_id(conn);
138         new->child = child;
139         new->width = width;
140         new->height = height;
141
142         /* Don’t generate events for our new window, it should *not* be managed */
143         mask |= XCB_CW_OVERRIDE_REDIRECT;
144         values[0] = 1;
145
146         /* We want to know when… */
147         mask |= XCB_CW_EVENT_MASK;
148         values[1] =     XCB_EVENT_MASK_BUTTON_PRESS | /* …mouse is pressed/released */
149                         XCB_EVENT_MASK_BUTTON_RELEASE |
150                         XCB_EVENT_MASK_EXPOSURE | /* …our window needs to be redrawn */
151                         XCB_EVENT_MASK_ENTER_WINDOW /* …user moves cursor inside our window */;
152
153         printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
154
155         /* Yo dawg, I heard you like windows, so I create a window around your window… */
156         xcb_create_window(conn,
157                         depth,
158                         new->frame,
159                         root,
160                         x,
161                         y,
162                         width + LEFT + RIGHT,
163                         height + TOP + BOTTOM,
164                         /* border_width */ 0,
165                         XCB_WINDOW_CLASS_INPUT_OUTPUT,
166                         visual,
167                         mask,
168                         values);
169         xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
170
171         /* Map the window on the screen (= make it visible) */
172         xcb_map_window(conn, new->frame);
173
174         /* Generate a graphics context for the titlebar */
175         new->titlegc = xcb_generate_id(conn);
176         xcb_create_gc(conn, new->titlegc, new->frame, 0, 0);
177
178         /* Draw decorations */
179         decorate_window(conn, new);
180
181         /* Put our data structure (Client) into the table */
182         table_put(byParent, new->frame, new);
183         table_put(byChild, child, new);
184
185         /* Moves the original window into the new frame we've created for it */
186         i3Font *font = load_font(conn, pattern);
187         xcb_reparent_window(conn, child, new->frame, 0, font->height);
188
189         /* We are interested in property changes */
190         mask = XCB_CW_EVENT_MASK;
191         values[0] =     XCB_EVENT_MASK_PROPERTY_CHANGE |
192                         XCB_EVENT_MASK_STRUCTURE_NOTIFY |
193                         XCB_EVENT_MASK_ENTER_WINDOW |
194                         XCB_EVENT_MASK_BUTTON_PRESS;
195         xcb_change_window_attributes(conn, child, mask, values);
196
197         /* We need to grab the mouse buttons for click to focus */
198         xcb_grab_button(conn, false, child, XCB_EVENT_MASK_BUTTON_PRESS,
199                         XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 1 /* left mouse button */,
200                         XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */);
201
202         /* Focus the new window */
203         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->child, XCB_CURRENT_TIME);
204
205         render_layout(conn);
206 }
207
208 void manage_existing_windows(xcb_connection_t *c, xcb_property_handlers_t *prophs, xcb_window_t root) {
209         xcb_query_tree_cookie_t wintree;
210         xcb_query_tree_reply_t *rep;
211         int i, len;
212         xcb_window_t *children;
213         xcb_get_window_attributes_cookie_t *cookies;
214
215         wintree = xcb_query_tree(c, root);
216         rep = xcb_query_tree_reply(c, wintree, 0);
217         if(!rep)
218                 return;
219         len = xcb_query_tree_children_length(rep);
220         cookies = malloc(len * sizeof(*cookies));
221         if(!cookies)
222         {
223                 free(rep);
224                 return;
225         }
226         children = xcb_query_tree_children(rep);
227         for(i = 0; i < len; ++i)
228                 cookies[i] = xcb_get_window_attributes(c, children[i]);
229         for(i = 0; i < len; ++i)
230         {
231                 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
232                 manage_window(prophs, c, children[i], wa);
233         }
234         free(rep);
235 }
236
237 static void initialize_xinerama(xcb_connection_t *conn) {
238         xcb_xinerama_query_screens_reply_t *reply;
239         xcb_xinerama_screen_info_t *screen_info;
240         int screen;
241
242         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
243                 printf("Xinerama extension not found, disabling.\n");
244                 return;
245         }
246
247         if (!xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL)->state) {
248                 printf("Xinerama is not active (in your X-Server), disabling.\n");
249                 return;
250         }
251
252
253         reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
254         /* TODO: error check */
255         screen_info = xcb_xinerama_query_screens_screen_info(reply);
256
257         num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
258
259         /* Just go through each workspace and associate as many screens as we can. */
260         for (screen = 0; screen < num_screens; screen++) {
261                 workspaces[screen].x = screen_info[screen].x_org;
262                 workspaces[screen].y = screen_info[screen].y_org;
263                 workspaces[screen].width = screen_info[screen].width;
264                 workspaces[screen].height = screen_info[screen].height;
265                 workspaces[screen].screen_num = screen;
266
267                 printf("found Xinerama screen: %d x %d at %d x %d\n",
268                                 screen_info[screen].width, screen_info[screen].height,
269                                 screen_info[screen].x_org, screen_info[screen].y_org);
270         }
271
272         free(screen_info);
273 }
274
275 int main(int argc, char *argv[], char *env[]) {
276         int i, e = 0;
277
278         for (i = 0; (env[i] != NULL); i++)
279                 if (strncmp(env[i], "LC_", strlen("LC_")) == 0 ||
280                         strncmp(env[i], "LANG=", strlen("LANG=")) == 0 ||
281                         strncmp(env[i], "DISPLAY=", strlen("DISPLAY=")) == 0) {
282                         printf("Passing environment \"%s\"\n", env[i]);
283                         environment = realloc(environment, sizeof(char*) * ++e);
284                         environment[e-1] = env[i];
285                 }
286
287         /* environment has to be NULL-terminated */
288         environment = realloc(environment, sizeof(char*) * ++e);
289         environment[e-1] = NULL;
290
291         init_table();
292
293         xcb_connection_t *c;
294         xcb_property_handlers_t prophs;
295         xcb_window_t root;
296
297         int screens;
298
299         memset(&evenths, 0, sizeof(xcb_event_handlers_t));
300         memset(&prophs, 0, sizeof(xcb_property_handlers_t));
301
302         byChild = alloc_table();
303         byParent = alloc_table();
304
305         TAILQ_INIT(&bindings);
306
307         c = xcb_connect(NULL, &screens);
308
309         printf("x screen is %d\n", screens);
310
311         /* TODO: this has to be more beautiful somewhen */
312         int major, minor, error;
313
314         major = XkbMajorVersion;
315         minor = XkbMinorVersion;
316
317         int evBase, errBase;
318
319         if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
320                 fprintf(stderr, "XkbOpenDisplay() failed\n");
321                 return 1;
322         }
323
324         int i1;
325         if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
326                 fprintf(stderr, "XKB not supported by X-server\n");
327                 return 1;
328         }
329         /* end of ugliness */
330
331         xcb_event_handlers_init(c, &evenths);
332         for(i = 2; i < 128; ++i)
333                 xcb_event_set_handler(&evenths, i, handle_event, 0);
334
335         for(i = 0; i < 256; ++i)
336                 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
337
338         /* Expose = an Application should redraw itself. That is, we have to redraw our
339          * contents (= top/bottom bar, titlebars for each window) */
340         xcb_event_set_expose_handler(&evenths, handle_expose_event, 0);
341
342         /* Key presses/releases are pretty obvious, I think */
343         xcb_event_set_key_press_handler(&evenths, handle_key_press, 0);
344         xcb_event_set_key_release_handler(&evenths, handle_key_release, 0);
345
346         /* Enter window = user moved his mouse over the window */
347         xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
348
349         /* Button press = user pushed a mouse button over one of our windows */
350         xcb_event_set_button_press_handler(&evenths, handle_button_press, 0);
351
352         xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
353
354         xcb_property_handlers_init(&prophs, &evenths);
355         xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
356
357         xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
358
359         root = xcb_aux_get_screen(c, screens)->root;
360         root_win = root;
361
362         uint32_t mask = XCB_CW_EVENT_MASK;
363         uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE };
364         xcb_change_window_attributes(c, root, mask, values);
365
366         #define BIND(key, modifier, cmd) { \
367                 Binding *new = malloc(sizeof(Binding)); \
368                 new->keycode = key; \
369                 new->mods = modifier; \
370                 new->command = cmd; \
371                 TAILQ_INSERT_TAIL(&bindings, new, bindings); \
372         }
373
374         /* 38 = 'a' */
375         BIND(38, BIND_MODE_SWITCH, "foo");
376
377         BIND(30, 0, "exec /usr/pkg/bin/urxvt");
378
379         BIND(44, BIND_MOD_1, "h");
380         BIND(45, BIND_MOD_1, "j");
381         BIND(46, BIND_MOD_1, "k");
382         BIND(47, BIND_MOD_1, "l");
383
384         BIND(44, BIND_MOD_1 | BIND_CONTROL, "sh");
385         BIND(45, BIND_MOD_1 | BIND_CONTROL, "sj");
386         BIND(46, BIND_MOD_1 | BIND_CONTROL, "sk");
387         BIND(47, BIND_MOD_1 | BIND_CONTROL, "sl");
388
389         BIND(44, BIND_MOD_1 | BIND_SHIFT, "mh");
390         BIND(45, BIND_MOD_1 | BIND_SHIFT, "mj");
391         BIND(46, BIND_MOD_1 | BIND_SHIFT, "mk");
392         BIND(47, BIND_MOD_1 | BIND_SHIFT, "ml");
393
394         BIND(10, BIND_MOD_1 , "1");
395         BIND(11, BIND_MOD_1 , "2");
396         BIND(12, BIND_MOD_1 , "3");
397         BIND(13, BIND_MOD_1 , "4");
398         BIND(14, BIND_MOD_1 , "5");
399         BIND(15, BIND_MOD_1 , "6");
400         BIND(16, BIND_MOD_1 , "7");
401         BIND(17, BIND_MOD_1 , "8");
402         BIND(18, BIND_MOD_1 , "9");
403         BIND(19, BIND_MOD_1 , "0");
404
405         Binding *bind;
406         TAILQ_FOREACH(bind, &bindings, bindings) {
407                 printf("Grabbing %d\n", bind->keycode);
408                 if (bind->mods & BIND_MODE_SWITCH)
409                         xcb_grab_key(c, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
410                 else xcb_grab_key(c, 0, root, bind->mods, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
411         }
412
413         /* check for Xinerama */
414         printf("Checking for Xinerama...\n");
415         initialize_xinerama(c);
416
417         start_application(TERMINAL, NULL);
418
419         xcb_flush(c);
420
421         manage_existing_windows(c, &prophs, root);
422
423         xcb_event_wait_for_event_loop(&evenths);
424
425         /* not reached */
426         return 0;
427 }