7 #include <xcb/xcb_wm.h>
8 #include <X11/XKBlib.h>
18 static void set_focus(xcb_connection_t *conn, Client *client) {
19 /* Update container */
20 Client *old_client = client->container->currently_focused;
21 client->container->currently_focused = client;
23 current_col = client->container->col;
24 current_row = client->container->row;
26 /* Set focus to the entered window, and flush xcb buffer immediately */
27 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_NONE, client->child, XCB_CURRENT_TIME);
28 /* Update last/current client’s titlebar */
29 if (old_client != NULL)
30 decorate_window(conn, old_client);
31 decorate_window(conn, client);
37 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
38 * Therefore, we just replay all key presses.
41 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
42 printf("got key release, just passing\n");
43 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
49 * There was a key press. We lookup the key symbol and see if there are any bindings
50 * on that. This allows to do things like binding special characters (think of ä) to
51 * functions to get one more modifier while not losing AltGr :-)
52 * TODO: this description needs to be more understandable
55 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
56 printf("Keypress %d\n", event->detail);
58 /* We need to get the keysym group (There are group 1 to group 4, each holding
59 two keysyms (without shift and with shift) using Xkb because X fails to
60 provide them reliably (it works in Xephyr, it does not in real X) */
62 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
65 printf("state %d\n", event->state);
67 /* Find the binding */
68 /* TODO: event->state durch eine bitmask filtern und dann direkt vergleichen */
69 Binding *bind, *best_match = TAILQ_END(&bindings);
70 TAILQ_FOREACH(bind, &bindings, bindings) {
71 if (bind->keycode == event->detail &&
72 (bind->mods & event->state) == bind->mods) {
73 if (best_match == TAILQ_END(&bindings) ||
74 bind->mods > best_match->mods)
79 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
80 the user did not press Mode_switch, so just pass it… */
81 if (best_match == TAILQ_END(&bindings)) {
82 xcb_allow_events(conn, ReplayKeyboard, event->time);
87 if (event->state & 0x2) {
88 printf("that's mode_switch\n");
89 parse_command(conn, best_match->command);
90 printf("ok, hiding this event.\n");
91 xcb_allow_events(conn, SyncKeyboard, event->time);
96 parse_command(conn, best_match->command);
102 * When the user moves the mouse pointer onto a window, this callback gets called.
105 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
106 printf("enter_notify\n");
108 /* This was either a focus for a client’s parent (= titlebar)… */
109 Client *client = table_get(byParent, event->event);
110 /* …or the client itself */
112 client = table_get(byChild, event->event);
114 /* If not, then this event is not interesting. This should not happen */
115 if (client == NULL) {
116 printf("DEBUG: Uninteresting enter_notify-event?\n");
120 set_focus(conn, client);
125 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
126 printf("button press!\n");
127 /* This was either a focus for a client’s parent (= titlebar)… */
128 Client *client = table_get(byChild, event->event);
130 client = table_get(byParent, event->event);
134 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
135 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
137 /* Set focus in any case */
138 set_focus(conn, client);
140 /* Let’s see if this was on the borders (= resize). If not, we’re done */
141 i3Font *font = load_font(conn, pattern);
142 printf("press button on x=%d, y=%d\n", event->event_x, event->event_y);
143 if (event->event_y <= (font->height + 2))
146 printf("that was resize\n");
148 /* Open a new window, the resizebar. Grab the pointer and move the window around
149 as the user moves the pointer. */
152 /* TODO: the whole logic is missing. this is just a proof of concept */
153 xcb_window_t grabwin = xcb_generate_id(conn);
158 xcb_create_window(conn,
164 root_screen->width_in_pixels, /* width */
165 root_screen->height_in_pixels, /* height */
166 /* border_width */ 0,
167 XCB_WINDOW_CLASS_INPUT_ONLY,
168 root_screen->root_visual,
172 /* Map the window on the screen (= make it visible) */
173 xcb_map_window(conn, grabwin);
175 xcb_window_t helpwin = xcb_generate_id(conn);
177 mask = XCB_CW_BACK_PIXEL;
178 values[0] = root_screen->white_pixel;
179 xcb_create_window(conn, root_screen->root_depth, helpwin, root,
183 root_screen->height_in_pixels,
185 XCB_WINDOW_CLASS_INPUT_OUTPUT,
186 root_screen->root_visual,
190 xcb_map_window(conn, helpwin);
191 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
193 xcb_grab_pointer(conn, false, root, XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
194 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, grabwin, XCB_NONE, XCB_CURRENT_TIME);
198 xcb_generic_event_t *inside_event;
199 /* I’ve always wanted to have my own eventhandler… */
200 while ((inside_event = xcb_wait_for_event(conn))) {
201 /* Same as get_event_handler in xcb */
202 int nr = inside_event->response_type;
204 handle_event(NULL, conn, inside_event);
208 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
211 /* Check if we need to escape this loop… */
212 if (nr == XCB_BUTTON_RELEASE)
216 case XCB_MOTION_NOTIFY:
217 values[0] = ((xcb_motion_notify_event_t*)inside_event)->root_x;
218 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
222 /* Use original handler */
223 xcb_event_handle(&evenths, inside_event);
226 printf("Ignoring event of type %d\n", nr);
233 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
234 xcb_destroy_window(conn, helpwin);
235 xcb_destroy_window(conn, grabwin);
241 int handle_map_notify_event(void *prophs, xcb_connection_t *conn, xcb_map_notify_event_t *event) {
242 window_attributes_t wa = { TAG_VALUE };
243 wa.u.override_redirect = event->override_redirect;
244 printf("MapNotify for 0x%08x.\n", event->window);
245 manage_window(prophs, conn, event->window, wa);
250 * Our window decorations were unmapped. That means, the window will be killed now,
251 * so we better clean up before.
254 int handle_unmap_notify_event(void *data, xcb_connection_t *c, xcb_unmap_notify_event_t *e) {
255 Client *client = table_remove(byChild, e->event);
257 printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
260 printf("not a managed window. Ignoring.\n");
266 /* TODO: clear this up */
267 for (cols = 0; cols < c_ws->cols; cols++)
268 for (rows = 0; rows < c_ws->rows; rows++)
269 CIRCLEQ_FOREACH(con_client, &(CUR_TABLE[cols][rows]->clients), clients)
270 if (con_client == client) {
271 printf("removing from container\n");
272 if (client->container->currently_focused == client)
273 client->container->currently_focused = NULL;
274 CIRCLEQ_REMOVE(&(CUR_TABLE[cols][rows]->clients), con_client, clients);
280 root = xcb_setup_roots_iterator(xcb_get_setup(c)).data->root;
281 printf("child of 0x%08x.\n", client->frame);
282 xcb_reparent_window(c, client->child, root, 0, 0);
283 xcb_destroy_window(c, client->frame);
285 table_remove(byParent, client->frame);
294 * Called when a window changes its title
297 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
298 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
299 printf("window's name changed.\n");
300 Client *client = table_get(byChild, window);
304 client->name_len = xcb_get_property_value_length(prop);
305 client->name = malloc(client->name_len);
306 strncpy(client->name, xcb_get_property_value(prop), client->name_len);
307 printf("rename to \"%.*s\".\n", client->name_len, client->name);
309 decorate_window(conn, client);
316 * Expose event means we should redraw our windows (= title bar)
319 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *e) {
320 printf("handle_expose_event()\n");
321 Client *client = table_get(byParent, e->window);
322 if(!client || e->count != 0)
324 decorate_window(conn, client);