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 static const int TOP = 20;
41 static const int LEFT = 5;
42 static const int BOTTOM = 5;
43 static const int RIGHT = 5;
45 /* This is the filtered environment which will be passed to opened applications.
46 * It contains DISPLAY (naturally) and locales stuff (LC_*, LANG) */
49 /* hm, xcb_wm wants us to implement this. */
51 table_t *byParent = 0;
52 xcb_window_t root_win;
54 char *pattern = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso8859-1";
59 * TODO: what exactly does this, what happens if we leave stuff out?
62 void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *c, xcb_window_t window, window_attributes_t wa)
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)
71 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
74 if(attr->map_state != XCB_MAP_STATE_VIEWABLE)
76 printf("Window 0x%08x is not mapped. Ignoring.\n", window);
81 wa.u.override_redirect = attr->override_redirect;
83 if(!wa.u.override_redirect && table_get(byChild, window))
85 printf("Window 0x%08x already managed. Ignoring.\n", window);
89 if(wa.u.override_redirect)
91 printf("Window 0x%08x has override-redirect set. Ignoring.\n", window);
95 geomc = xcb_get_geometry(c, d);
99 wa.u.cookie = xcb_get_window_attributes(c, window);
100 attr = xcb_get_window_attributes_reply(c, wa.u.cookie, 0);
102 geom = xcb_get_geometry_reply(c, geomc, 0);
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);
113 * Let’s own this window…
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) {
120 Client *new = table_get(byChild, child);
122 printf("oh, it's new\n");
123 new = calloc(sizeof(Client), 1);
130 /* Insert into the currently active container */
131 CIRCLEQ_INSERT_TAIL(&(CUR_CELL->clients), new, clients);
133 printf("currently_focused = %p\n", new);
134 CUR_CELL->currently_focused = new;
135 new->container = CUR_CELL;
137 new->frame = xcb_generate_id(conn);
140 new->height = height;
142 /* Don’t generate events for our new window, it should *not* be managed */
143 mask |= XCB_CW_OVERRIDE_REDIRECT;
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 */;
153 printf("Reparenting 0x%08x under 0x%08x.\n", child, new->frame);
155 /* Yo dawg, I heard you like windows, so I create a window around your window… */
156 xcb_create_window(conn,
162 width + LEFT + RIGHT,
163 height + TOP + BOTTOM,
164 /* border_width */ 0,
165 XCB_WINDOW_CLASS_INPUT_OUTPUT,
169 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, child);
171 /* Map the window on the screen (= make it visible) */
172 xcb_map_window(conn, new->frame);
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);
178 /* Draw decorations */
179 decorate_window(conn, new);
181 /* Put our data structure (Client) into the table */
182 table_put(byParent, new->frame, new);
183 table_put(byChild, child, new);
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);
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);
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 */);
202 /* Focus the new window */
203 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, new->child, XCB_CURRENT_TIME);
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;
212 xcb_window_t *children;
213 xcb_get_window_attributes_cookie_t *cookies;
215 wintree = xcb_query_tree(c, root);
216 rep = xcb_query_tree_reply(c, wintree, 0);
219 len = xcb_query_tree_children_length(rep);
220 cookies = malloc(len * sizeof(*cookies));
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)
231 window_attributes_t wa = { TAG_COOKIE, { cookies[i] } };
232 manage_window(prophs, c, children[i], wa);
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;
242 if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
243 printf("Xinerama extension not found, disabling.\n");
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");
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);
257 num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
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;
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);
275 int main(int argc, char *argv[], char *env[]) {
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];
287 /* environment has to be NULL-terminated */
288 environment = realloc(environment, sizeof(char*) * ++e);
289 environment[e-1] = NULL;
294 xcb_property_handlers_t prophs;
299 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
300 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
302 byChild = alloc_table();
303 byParent = alloc_table();
305 TAILQ_INIT(&bindings);
307 c = xcb_connect(NULL, &screens);
309 printf("x screen is %d\n", screens);
311 /* TODO: this has to be more beautiful somewhen */
312 int major, minor, error;
314 major = XkbMajorVersion;
315 minor = XkbMinorVersion;
319 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &evBase, &errBase, &major, &minor, &error)) == NULL) {
320 fprintf(stderr, "XkbOpenDisplay() failed\n");
325 if (!XkbQueryExtension(xkbdpy,&i1,&evBase,&errBase,&major,&minor)) {
326 fprintf(stderr, "XKB not supported by X-server\n");
329 /* end of ugliness */
331 xcb_event_handlers_init(c, &evenths);
332 for(i = 2; i < 128; ++i)
333 xcb_event_set_handler(&evenths, i, handle_event, 0);
335 for(i = 0; i < 256; ++i)
336 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
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);
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);
346 /* Enter window = user moved his mouse over the window */
347 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, 0);
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);
352 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, 0);
354 xcb_property_handlers_init(&prophs, &evenths);
355 xcb_event_set_map_notify_handler(&evenths, handle_map_notify_event, &prophs);
357 xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
359 root = xcb_aux_get_screen(c, screens)->root;
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);
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); \
375 BIND(38, BIND_MODE_SWITCH, "foo");
377 BIND(30, 0, "exec /usr/pkg/bin/urxvt");
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");
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");
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");
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");
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);
413 /* check for Xinerama */
414 printf("Checking for Xinerama...\n");
415 initialize_xinerama(c);
417 start_application(TERMINAL, NULL);
421 manage_existing_windows(c, &prophs, root);
423 xcb_event_wait_for_event_loop(&evenths);