]> git.sur5r.net Git - i3/i3/blob - handlers.c
Cleanups, first strike. Move stuff to separate files, eliminate warnings
[i3/i3] / handlers.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <xcb/xcb.h>
6
7 #include <xcb/xcb_wm.h>
8 #include <X11/XKBlib.h>
9
10 #include "i3.h"
11 #include "debug.h"
12 #include "table.h"
13 #include "layout.h"
14 #include "commands.h"
15 #include "data.h"
16 #include "font.h"
17
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;
22
23         current_col = client->container->col;
24         current_row = client->container->row;
25
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);
32         xcb_flush(conn);
33 }
34
35
36 /*
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.
39  *
40  */
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);
44         xcb_flush(conn);
45         return 1;
46 }
47
48 /*
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
53  *
54  */
55 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
56         printf("Keypress %d\n", event->detail);
57
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) */
61         XkbStateRec state;
62         if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
63                 event->state |= 0x2;
64
65         printf("state %d\n", event->state);
66
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)
75                                 best_match = bind;
76                 }
77         }
78
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);
83                 xcb_flush(conn);
84                 return 1;
85         }
86
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);
92                 xcb_flush(conn);
93                 return 1;
94         }
95
96         parse_command(conn, best_match->command);
97         return 1;
98 }
99
100
101 /*
102  * When the user moves the mouse pointer onto a window, this callback gets called.
103  *
104  */
105 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
106         printf("enter_notify\n");
107
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 */
111         if (client == NULL)
112                 client = table_get(byChild, event->event);
113
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");
117                 return 1;
118         }
119
120         set_focus(conn, client);
121
122         return 1;
123 }
124
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);
129         if (client == NULL)
130                 client = table_get(byParent, event->event);
131         if (client == NULL)
132                 return 1;
133
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;
136
137         /* Set focus in any case */
138         set_focus(conn, client);
139
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))
144                 return 1;
145
146         printf("that was resize\n");
147
148         /* Open a new window, the resizebar. Grab the pointer and move the window around
149            as the user moves the pointer. */
150
151
152         /* TODO: the whole logic is missing. this is just a proof of concept */
153         xcb_window_t grabwin = xcb_generate_id(conn);
154
155         uint32_t mask = 0;
156         uint32_t values[3];
157
158         xcb_create_window(conn,
159                         0,
160                         grabwin,
161                         root,
162                         0, /* x */
163                         0, /* y */
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,
169                         0,
170                         values);
171
172         /* Map the window on the screen (= make it visible) */
173         xcb_map_window(conn, grabwin);
174
175         xcb_window_t helpwin = xcb_generate_id(conn);
176
177         mask = XCB_CW_BACK_PIXEL;
178         values[0] = root_screen->white_pixel;
179         xcb_create_window(conn, root_screen->root_depth, helpwin, root,
180                         event->root_x,
181                         0,
182                         5,
183                         root_screen->height_in_pixels,
184                         /* bordor */ 0,
185                         XCB_WINDOW_CLASS_INPUT_OUTPUT,
186                         root_screen->root_visual,
187                         mask,
188                         values);
189
190         xcb_map_window(conn, helpwin);
191         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
192
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);
195
196         xcb_flush(conn);
197
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;
203                 if (nr == 0) {
204                         handle_event(NULL, conn, inside_event);
205                         continue;
206                 }
207                 assert(nr < 256);
208                 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
209                 assert(nr >= 2);
210
211                 /* Check if we need to escape this loop… */
212                 if (nr == XCB_BUTTON_RELEASE)
213                         break;
214
215                 switch (nr) {
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);
219                                 xcb_flush(conn);
220                                 break;
221                         case XCB_EXPOSE:
222                                 /* Use original handler */
223                                 xcb_event_handle(&evenths, inside_event);
224                                 break;
225                         default:
226                                 printf("Ignoring event of type %d\n", nr);
227                                 break;
228                 }
229                 printf("---\n");
230                 free(inside_event);
231         }
232
233         xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
234         xcb_destroy_window(conn, helpwin);
235         xcb_destroy_window(conn, grabwin);
236         xcb_flush(conn);
237
238         return 1;
239 }
240
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);
246         return 1;
247 }
248
249 /*
250  * Our window decorations were unmapped. That means, the window will be killed now,
251  * so we better clean up before.
252  *
253  */
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);
256         xcb_window_t root;
257         printf("UnmapNotify for 0x%08x (received from 0x%08x): ", e->window, e->event);
258         if(!client)
259         {
260                 printf("not a managed window. Ignoring.\n");
261                 return 0;
262         }
263
264         int rows, cols;
265         Client *con_client;
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);
275                                         break;
276                                 }
277
278
279
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);
284         xcb_flush(c);
285         table_remove(byParent, client->frame);
286         free(client);
287
288         render_layout(c);
289
290         return 1;
291 }
292
293 /*
294  * Called when a window changes its title
295  *
296  */
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);
301         if (client == NULL)
302                 return 1;
303
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);
308
309         decorate_window(conn, client);
310         xcb_flush(conn);
311
312         return 1;
313 }
314
315 /*
316  * Expose event means we should redraw our windows (= title bar)
317  *
318  */
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)
323                 return 1;
324         decorate_window(conn, client);
325         return 1;
326 }