13 #include <X11/XKBlib.h>
14 #include <X11/extensions/XKB.h>
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>
33 #define TERMINAL "/usr/pkg/bin/urxvt"
37 TAILQ_HEAD(bindings_head, Binding) bindings;
38 xcb_event_handlers_t evenths;
40 /* hm, xcb_wm wants us to implement this. */
42 table_t *byParent = 0;
43 xcb_window_t root_win;
45 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
50 * TODO: what exactly does this, what happens if we leave stuff out?
53 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
55 printf("managing window.\n");
56 xcb_drawable_t d = { window };
57 xcb_get_geometry_cookie_t geomc;
58 xcb_get_geometry_reply_t *geom;
59 xcb_get_window_attributes_reply_t *attr = 0;
60 if(wa.tag == TAG_COOKIE)
62 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
65 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
67 printf("Window 0x%08x is not mapped. Ignoring.\n", window);
72 wa.u.override_redirect = attr->override_redirect;
74 if(!wa.u.override_redirect && table_get(byChild, window))
76 printf("Window 0x%08x already managed. Ignoring.\n", window);
80 if(wa.u.override_redirect)
82 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
86 geomc = xcb_get_geometry(c, d);
90 wa.u.cookie = xcb_get_window_attributes(c, window);
91 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
93 geom = xcb_get_geometry_reply(c, geomc, 0);
96 reparent_window(c, window, attr->visual, geom->root, geom->depth, geom->x, geom->y, geom->width, geom->height);
97 xcb_property_changed(prophs, XCB_PROPERTY_NEW_VALUE, window, WM_NAME);
104 * reparent_window() gets called when a new window was opened and becomes a child of the root
105 * window, or it gets called by us when we manage the already existing windows at startup.
107 * Essentially, this is the point, where we take over control.
110 void reparent_window(xcb_connection_t *conn, xcb_window_t child,
111 xcb_visualid_t visual, xcb_window_t root, uint8_t depth,
112 int16_t x, int16_t y, uint16_t width, uint16_t height) {
114 Client *new = table_get(byChild, child);
116 /* TODO: When does this happen for existing clients? Is that a bug? */
117 printf("oh, it's new\n");
118 new = calloc(sizeof(Client), 1);
119 /* We initialize x and y with the invalid coordinates -1 so that they will
120 get updated at the next render_layout() at any case */
127 /* Insert into the currently active container */
128 CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
130 /* Update the data structures */
131 CUR_CELL->currently_focused = new;
132 new->container = CUR_CELL;
134 new->frame = xcb_generate_id(conn);
137 new->height = height;
139 /* Don’t generate events for our new window, it should *not* be managed */
140 mask |= XCB_CW_OVERRIDE_REDIRECT;
143 /* We want to know when… */
144 mask |= XCB_CW_EVENT_MASK;
145 values[1] = XCB_EVENT_MASK_BUTTON_PRESS | /* …mouse is pressed/released */
146 XCB_EVENT_MASK_BUTTON_RELEASE |
147 XCB_EVENT_MASK_EXPOSURE | /* …our window needs to be redrawn */
148 XCB_EVENT_MASK_ENTER_WINDOW; /* …user moves cursor inside our window */
150 printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
152 i3Font *font = load_font(conn, pattern);
154 /* Yo dawg, I heard you like windows, so I create a window around your window… */
155 xcb_create_window(conn,
161 width + 2 + 2, /* 2 px border at each side */
162 height + 2 + 2 + font->height, /* 2 px border plus font’s height */
163 0, /* border_width = 0, we draw our own borders */
164 XCB_WINDOW_CLASS_INPUT_OUTPUT,
168 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
170 /* Map the window on the screen (= make it visible) */
171 xcb_map_window(conn, new->frame);
173 /* Generate a graphics context for the titlebar */
174 new->titlegc = xcb_generate_id(conn);
175 xcb_create_gc(conn, new->titlegc, new->frame, 0, 0);
177 /* Put our data structure (Client) into the table */
178 table_put(byParent, new->frame, new);
179 table_put(byChild, child, new);
181 /* Moves the original window into the new frame we've created for it */
182 xcb_reparent_window(conn, child, new->frame, 0, font->height);
184 /* We are interested in property changes */
185 mask = XCB_CW_EVENT_MASK;
186 values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE |
187 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
188 XCB_EVENT_MASK_ENTER_WINDOW |
189 XCB_EVENT_MASK_BUTTON_PRESS;
190 xcb_change_window_attributes(conn, child, mask, values);
192 /* We need to grab the mouse buttons for click to focus */
193 xcb_grab_button(conn, false, child, XCB_EVENT_MASK_BUTTON_PRESS,
194 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
195 1 /* left mouse button */,
196 XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */);
198 /* Focus the new window */
199 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->child, XCB_CURRENT_TIME);
204 void manage_existing_windows(xcb_connection_t *c, xcb_property_handlers_t *prophs, xcb_window_t root) {
205 xcb_query_tree_cookie_t wintree;
206 xcb_query_tree_reply_t *rep;
208 xcb_window_t *children;
209 xcb_get_window_attributes_cookie_t *cookies;
211 wintree = xcb_query_tree(c, root);
212 rep = xcb_query_tree_reply(c, wintree, 0);
215 len = xcb_query_tree_children_length(rep);
216 cookies = malloc(len * sizeof(*cookies));
222 children = xcb_query_tree_children(rep);
223 for(i = 0; i < len; ++i)
224 cookies[i] = xcb_get_window_attributes(c, children[i]);
225 for(i = 0; i < len; ++i)
227 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
228 manage_window(prophs, c, children[i], wa);
233 static void initialize_xinerama(xcb_connection_t *conn) {
234 xcb_xinerama_query_screens_reply_t *reply;
235 xcb_xinerama_screen_info_t *screen_info;
238 if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
239 printf("Xinerama extension not found, disabling.\n");
243 if (!xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL)->state) {
244 printf("Xinerama is not active (in your X-Server), disabling.\n");
249 reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
250 /* TODO: error check */
251 screen_info = xcb_xinerama_query_screens_screen_info(reply);
253 num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
255 /* Just go through each workspace and associate as many screens as we can. */
256 for (screen = 0; screen < num_screens; screen++) {
257 workspaces[screen].x = screen_info[screen].x_org;
258 workspaces[screen].y = screen_info[screen].y_org;
259 workspaces[screen].width = screen_info[screen].width;
260 workspaces[screen].height = screen_info[screen].height;
261 workspaces[screen].screen_num = screen;
263 printf("found Xinerama screen: %d x %d at %d x %d\n",
264 screen_info[screen].width, screen_info[screen].height,
265 screen_info[screen].x_org, screen_info[screen].y_org);
271 int main(int argc, char *argv[], char *env[]) {
274 xcb_property_handlers_t prophs;
277 /* Initialize the table data structures for each workspace */
280 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
281 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
283 byChild = alloc_table();
284 byParent = alloc_table();
286 TAILQ_INIT(&bindings);
288 c = xcb_connect(NULL, &screens);
290 /* TODO: this has to be more beautiful somewhen */
291 int major, minor, error;
293 major = XkbMajorVersion;
294 minor = XkbMinorVersion;
298 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
299 fprintf(stderr, "XkbOpenDisplay() failed\n");
304 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
305 fprintf(stderr, "XKB not supported by X-server\n");
308 /* end of ugliness */
310 xcb_event_handlers_init(c, &evenths);
311 for(i = 2; i < 128; ++i)
312 xcb_event_set_handler(&evenths, i, handle_event, 0);
314 for(i = 0; i < 256; ++i)
315 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
317 /* Expose = an Application should redraw itself. That is, we have to redraw our
318 * contents (= top/bottom bar, titlebars for each window) */
319 xcb_event_set_expose_handler(&evenths, handle_expose_event, 0);
321 /* Key presses/releases are pretty obvious, I think */
322 xcb_event_set_key_press_handler(&evenths, handle_key_press, 0);
323 xcb_event_set_key_release_handler(&evenths, handle_key_release, 0);
325 /* Enter window = user moved his mouse over the window */
326 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
328 /* Button press = user pushed a mouse button over one of our windows */
329 xcb_event_set_button_press_handler(&evenths, handle_button_press, 0);
331 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
333 xcb_property_handlers_init(&prophs, &evenths);
334 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
336 xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
338 root = xcb_aux_get_screen(c, screens)->root;
341 uint32_t mask = XCB_CW_EVENT_MASK;
342 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE };
343 xcb_change_window_attributes(c, root, mask, values);
345 #define BIND(key, modifier, cmd) { \
346 Binding *new = malloc(sizeof(Binding)); \
347 new->keycode = key; \
348 new->mods = modifier; \
349 new->command = cmd; \
350 TAILQ_INSERT_TAIL(&bindings, new, bindings); \
354 BIND(38, BIND_MODE_SWITCH, "foo");
356 BIND(30, 0, "exec /usr/pkg/bin/urxvt");
358 BIND(44, BIND_MOD_1, "h");
359 BIND(45, BIND_MOD_1, "j");
360 BIND(46, BIND_MOD_1, "k");
361 BIND(47, BIND_MOD_1, "l");
363 BIND(44, BIND_MOD_1 | BIND_CONTROL, "sh");
364 BIND(45, BIND_MOD_1 | BIND_CONTROL, "sj");
365 BIND(46, BIND_MOD_1 | BIND_CONTROL, "sk");
366 BIND(47, BIND_MOD_1 | BIND_CONTROL, "sl");
368 BIND(44, BIND_MOD_1 | BIND_SHIFT, "mh");
369 BIND(45, BIND_MOD_1 | BIND_SHIFT, "mj");
370 BIND(46, BIND_MOD_1 | BIND_SHIFT, "mk");
371 BIND(47, BIND_MOD_1 | BIND_SHIFT, "ml");
373 BIND(10, BIND_MOD_1 , "1");
374 BIND(11, BIND_MOD_1 , "2");
375 BIND(12, BIND_MOD_1 , "3");
376 BIND(13, BIND_MOD_1 , "4");
377 BIND(14, BIND_MOD_1 , "5");
378 BIND(15, BIND_MOD_1 , "6");
379 BIND(16, BIND_MOD_1 , "7");
380 BIND(17, BIND_MOD_1 , "8");
381 BIND(18, BIND_MOD_1 , "9");
382 BIND(19, BIND_MOD_1 , "0");
385 TAILQ_FOREACH(bind, &bindings, bindings) {
386 printf("Grabbing %d\n", bind->keycode);
387 if (bind->mods & BIND_MODE_SWITCH)
388 xcb_grab_key(c, 0, root, 0, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
389 else xcb_grab_key(c, 0, root, bind->mods, bind->keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
392 /* check for Xinerama */
393 printf("Checking for Xinerama...\n");
394 initialize_xinerama(c);
396 start_application(TERMINAL);
400 manage_existing_windows(c, &prophs, root);
402 xcb_event_wait_for_event_loop(&evenths);