]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
Bugfix: Correctly re-assign dock_clients to the first screen when their screen disappears
[i3/i3] / src / xinerama.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdbool.h>
15 #include <assert.h>
16 #include <time.h>
17
18 #include <xcb/xcb.h>
19 #include <xcb/xinerama.h>
20
21 #include "queue.h"
22 #include "i3.h"
23 #include "data.h"
24 #include "table.h"
25 #include "util.h"
26 #include "xinerama.h"
27 #include "layout.h"
28 #include "xcb.h"
29 #include "config.h"
30 #include "workspace.h"
31
32 /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
33  * (xrandr --same-as) */
34 struct screens_head *virtual_screens;
35
36 static bool xinerama_enabled = true;
37
38 /*
39  * Returns true if both screen objects describe the same screen (checks their
40  * size and position).
41  *
42  */
43 bool screens_are_equal(i3Screen *screen1, i3Screen *screen2) {
44         /* If one of both objects (or both) are NULL, we cannot compare them */
45         if (screen1 == NULL || screen2 == NULL)
46                 return false;
47
48         /* If the pointers are equal, take the short-circuit */
49         if (screen1 == screen2)
50                 return true;
51
52         /* Compare their size - other properties are not relevant to determine
53          * if a screen is equal to another one */
54         return (memcmp(&(screen1->rect), &(screen2->rect), sizeof(Rect)) == 0);
55 }
56
57 /*
58  * Looks in virtual_screens for the i3Screen whose start coordinates are x, y
59  *
60  */
61 i3Screen *get_screen_at(int x, int y, struct screens_head *screenlist) {
62         i3Screen *screen;
63         TAILQ_FOREACH(screen, screenlist, screens)
64                 if (screen->rect.x == x && screen->rect.y == y)
65                         return screen;
66
67         return NULL;
68 }
69
70 /*
71  * Looks in virtual_screens for the i3Screen which contains coordinates x, y
72  *
73  */
74 i3Screen *get_screen_containing(int x, int y) {
75         i3Screen *screen;
76         TAILQ_FOREACH(screen, virtual_screens, screens) {
77                 LOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
78                                 x, y, screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
79                 if (x >= screen->rect.x && x < (screen->rect.x + screen->rect.width) &&
80                     y >= screen->rect.y && y < (screen->rect.y + screen->rect.height))
81                         return screen;
82         }
83
84         return NULL;
85 }
86
87 /*
88  * Gets the screen which is the last one in the given direction, for example the screen
89  * on the most bottom when direction == D_DOWN, the screen most right when direction == D_RIGHT
90  * and so on.
91  *
92  * This function always returns a screen.
93  *
94  */
95 i3Screen *get_screen_most(direction_t direction, i3Screen *current) {
96         i3Screen *screen, *candidate = NULL;
97         int position = 0;
98         TAILQ_FOREACH(screen, virtual_screens, screens) {
99                 /* Repeated calls of WIN determine the winner of the comparison */
100                 #define WIN(variable, condition) \
101                         if (variable condition) { \
102                                 candidate = screen; \
103                                 position = variable; \
104                         } \
105                         break;
106
107                 if (((direction == D_UP) || (direction == D_DOWN)) &&
108                     (current->rect.x != screen->rect.x))
109                         continue;
110
111                 if (((direction == D_LEFT) || (direction == D_RIGHT)) &&
112                     (current->rect.y != screen->rect.y))
113                         continue;
114
115                 switch (direction) {
116                         case D_UP:
117                                 WIN(screen->rect.y, <= position);
118                         case D_DOWN:
119                                 WIN(screen->rect.y, >= position);
120                         case D_LEFT:
121                                 WIN(screen->rect.x, <= position);
122                         case D_RIGHT:
123                                 WIN(screen->rect.x, >= position);
124                 }
125         }
126
127         assert(candidate != NULL);
128
129         return candidate;
130 }
131
132 static void initialize_screen(xcb_connection_t *conn, i3Screen *screen, Workspace *workspace) {
133         i3Font *font = load_font(conn, config.font);
134
135         workspace->screen = screen;
136         screen->current_workspace = workspace;
137
138         /* Create a bar for each screen */
139         Rect bar_rect = {screen->rect.x,
140                          screen->rect.y + screen->rect.height - (font->height + 6),
141                          screen->rect.x + screen->rect.width,
142                          font->height + 6};
143         uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
144         uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS};
145         screen->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, true, mask, values);
146         screen->bargc = xcb_generate_id(conn);
147         xcb_create_gc(conn, screen->bargc, screen->bar, 0, 0);
148
149         SLIST_INIT(&(screen->dock_clients));
150
151         LOG("that is virtual screen at %d x %d with %d x %d\n",
152                         screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
153 }
154
155 /*
156  * Fills virtual_screens with exactly one screen with width/height of the whole X server.
157  *
158  */
159 static void disable_xinerama(xcb_connection_t *conn) {
160         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
161
162         i3Screen *s = calloc(sizeof(i3Screen), 1);
163
164         s->rect.x = 0;
165         s->rect.y = 0;
166         s->rect.width = root_screen->width_in_pixels;
167         s->rect.height = root_screen->height_in_pixels;
168
169         num_screens = 1;
170         s->num = 0;
171
172         TAILQ_INSERT_TAIL(virtual_screens, s, screens);
173
174         xinerama_enabled = false;
175 }
176
177 /*
178  * Gets the Xinerama screens and converts them to virtual i3Screens (only one screen for two
179  * Xinerama screen which are configured in clone mode) in the given screenlist
180  *
181  */
182 static void query_screens(xcb_connection_t *conn, struct screens_head *screenlist) {
183         xcb_xinerama_query_screens_reply_t *reply;
184         xcb_xinerama_screen_info_t *screen_info;
185         time_t before_trying = time(NULL);
186
187         /* Try repeatedly to find screens (there might be short timeframes in
188          * which the X server does not return any screens, such as when rotating
189          * screens), but not longer than 5 seconds (strictly speaking, only four
190          * seconds of trying are guaranteed due to the 1-second-resolution) */
191         while ((time(NULL) - before_trying) < 5) {
192                 reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
193                 if (!reply) {
194                         LOG("Couldn't get Xinerama screens\n");
195                         return;
196                 }
197                 screen_info = xcb_xinerama_query_screens_screen_info(reply);
198                 int screens = xcb_xinerama_query_screens_screen_info_length(reply);
199                 num_screens = 0;
200
201                 for (int screen = 0; screen < screens; screen++) {
202                         i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
203                         if (s != NULL) {
204                                 /* This screen already exists. We use the littlest screen so that the user
205                                    can always see the complete workspace */
206                                 s->rect.width = min(s->rect.width, screen_info[screen].width);
207                                 s->rect.height = min(s->rect.height, screen_info[screen].height);
208                         } else {
209                                 s = calloc(sizeof(i3Screen), 1);
210                                 s->rect.x = screen_info[screen].x_org;
211                                 s->rect.y = screen_info[screen].y_org;
212                                 s->rect.width = screen_info[screen].width;
213                                 s->rect.height = screen_info[screen].height;
214                                 /* We always treat the screen at 0x0 as the primary screen */
215                                 if (s->rect.x == 0 && s->rect.y == 0)
216                                         TAILQ_INSERT_HEAD(screenlist, s, screens);
217                                 else TAILQ_INSERT_TAIL(screenlist, s, screens);
218                                 num_screens++;
219                         }
220
221                         LOG("found Xinerama screen: %d x %d at %d x %d\n",
222                                         screen_info[screen].width, screen_info[screen].height,
223                                         screen_info[screen].x_org, screen_info[screen].y_org);
224                 }
225
226                 free(reply);
227
228                 if (num_screens == 0) {
229                         LOG("No screens found. This is weird. Trying again...\n");
230                         continue;
231                 }
232
233                 break;
234         }
235 }
236
237 /*
238  * We have just established a connection to the X server and need the initial Xinerama
239  * information to setup workspaces for each screen.
240  *
241  */
242 void initialize_xinerama(xcb_connection_t *conn) {
243         virtual_screens = scalloc(sizeof(struct screens_head));
244         TAILQ_INIT(virtual_screens);
245
246         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
247                 LOG("Xinerama extension not found, disabling.\n");
248                 disable_xinerama(conn);
249         } else {
250                 xcb_xinerama_is_active_reply_t *reply;
251                 reply = xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL);
252
253                 if (reply == NULL || !reply->state) {
254                         LOG("Xinerama is not active (in your X-Server), disabling.\n");
255                         disable_xinerama(conn);
256                 } else
257                         query_screens(conn, virtual_screens);
258
259                 FREE(reply);
260         }
261
262         i3Screen *screen;
263         num_screens = 0;
264         /* Just go through each workspace and associate as many screens as we can. */
265         TAILQ_FOREACH(screen, virtual_screens, screens) {
266                 screen->num = num_screens;
267                 num_screens++;
268                 Workspace *ws = get_first_workspace_for_screen(virtual_screens, screen);
269                 initialize_screen(conn, screen, ws);
270         }
271 }
272
273 /*
274  * This is called when the rootwindow receives a configure_notify event and therefore the
275  * number/position of the Xinerama screens could have changed.
276  *
277  */
278 void xinerama_requery_screens(xcb_connection_t *conn) {
279         i3Font *font = load_font(conn, config.font);
280
281         /* POSSIBLE PROBLEM: Is the order of the Xinerama screens always constant? That is, can
282            it change when I move the --right-of video projector to --left-of? */
283
284         if (!xinerama_enabled) {
285                 LOG("Xinerama is disabled\n");
286                 return;
287         }
288
289         /* We use a separate copy to diff with the previous set of screens */
290         struct screens_head *new_screens = scalloc(sizeof(struct screens_head));
291         TAILQ_INIT(new_screens);
292
293         query_screens(conn, new_screens);
294
295         i3Screen *first = TAILQ_FIRST(new_screens),
296                  *screen,
297                  *old_screen;
298         int screen_count = 0;
299         /* Mark each workspace which currently is assigned to a screen, so we
300          * can garbage-collect afterwards */
301         Workspace *ws;
302         TAILQ_FOREACH(ws, workspaces, workspaces)
303                 ws->reassigned = (ws->screen == NULL);
304
305         TAILQ_FOREACH(screen, new_screens, screens) {
306                 screen->num = screen_count;
307                 screen->current_workspace = NULL;
308
309                 TAILQ_FOREACH(old_screen, virtual_screens, screens) {
310                         if (old_screen->num != screen_count)
311                                 continue;
312
313                         LOG("Found a matching screen\n");
314                         /* Use the same workspace */
315                         screen->current_workspace = old_screen->current_workspace;
316
317                         /* Re-use the old bar window */
318                         screen->bar = old_screen->bar;
319                         screen->bargc = old_screen->bargc;
320                         LOG("old_screen->bar = %p\n", old_screen->bar);
321
322                         Rect bar_rect = {screen->rect.x,
323                                          screen->rect.y + screen->rect.height - (font->height + 6),
324                                          screen->rect.x + screen->rect.width,
325                                          font->height + 6};
326
327                         LOG("configuring bar to be at %d x %d with %d x %d\n",
328                                         bar_rect.x, bar_rect.y, bar_rect.height, bar_rect.width);
329                         xcb_configure_window(conn, screen->bar, XCB_CONFIG_WINDOW_X |
330                                                                 XCB_CONFIG_WINDOW_Y |
331                                                                 XCB_CONFIG_WINDOW_WIDTH |
332                                                                 XCB_CONFIG_WINDOW_HEIGHT, &(bar_rect.x));
333
334                         /* Copy the list head for the dock clients */
335                         screen->dock_clients = old_screen->dock_clients;
336                         SLIST_INIT(&(old_screen->dock_clients));
337
338                         /* Update the dimensions */
339                         Workspace *ws;
340                         TAILQ_FOREACH(ws, workspaces, workspaces) {
341                                 if (ws->screen != old_screen)
342                                         continue;
343
344                                 LOG("re-assigning ws %d\n", ws->num);
345                                 memcpy(&(ws->rect), &(screen->rect), sizeof(Rect));
346                                 ws->screen = screen;
347                                 ws->reassigned = true;
348                         }
349
350                         break;
351                 }
352                 if (screen->current_workspace == NULL) {
353                         /* Find the first unused workspace, preferring the ones
354                          * which are assigned to this screen and initialize
355                          * the screen with it. */
356                         LOG("getting first ws for screen %p\n", screen);
357                         Workspace *ws = get_first_workspace_for_screen(new_screens, screen);
358                         initialize_screen(conn, screen, ws);
359                         ws->reassigned = true;
360
361                         /* As this workspace just got visible (we got a new screen
362                          * without workspace), we need to map its clients */
363                         workspace_map_clients(conn, ws);
364                 }
365                 screen_count++;
366         }
367
368         /* check for dock_clients which are out of bounds */
369         TAILQ_FOREACH(old_screen, virtual_screens, screens) {
370                 if (SLIST_EMPTY(&(old_screen->dock_clients)))
371                         continue;
372
373                 LOG("dock_clients out of bounds at screen %p, reassigning\n", old_screen);
374                 if (SLIST_EMPTY(&(first->dock_clients))) {
375                         first->dock_clients = old_screen->dock_clients;
376                         continue;
377                 }
378
379                 /* We need to merge the lists */
380                 Client *dock_client;
381
382                 while (!SLIST_EMPTY(&(old_screen->dock_clients))) {
383                         dock_client = SLIST_FIRST(&(old_screen->dock_clients));
384                         SLIST_INSERT_HEAD(&(first->dock_clients), dock_client, dock_clients);
385                         SLIST_REMOVE_HEAD(&(old_screen->dock_clients), dock_clients);
386                 }
387         }
388
389         /* Check for workspaces which are out of bounds */
390         TAILQ_FOREACH(ws, workspaces, workspaces) {
391                 if (ws->reassigned)
392                         continue;
393
394                 Client *client;
395
396                 LOG("Closing bar window (%p)\n", ws->screen->bar);
397                 xcb_destroy_window(conn, ws->screen->bar);
398
399                 LOG("Workspace %d's screen out of bounds, assigning to first screen\n", ws->num + 1);
400                 ws->screen = first;
401                 memcpy(&(ws->rect), &(first->rect), sizeof(Rect));
402
403                 /* Force reconfiguration for each client on that workspace */
404                 FOR_TABLE(ws)
405                         CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
406                                 client->force_reconfigure = true;
407
408                 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
409                 render_workspace(conn, first, ws);
410
411                 /* …unless we want to see them at the moment, we should hide that workspace */
412                 if (workspace_is_visible(ws))
413                         continue;
414
415                 workspace_unmap_clients(conn, ws);
416
417                 if (c_ws == ws) {
418                         LOG("Need to adjust c_ws...\n");
419                         c_ws = first->current_workspace;
420                 }
421         }
422         xcb_flush(conn);
423
424         /* Free the old list */
425         while (!TAILQ_EMPTY(virtual_screens)) {
426                 screen = TAILQ_FIRST(virtual_screens);
427                 TAILQ_REMOVE(virtual_screens, screen, screens);
428                 free(screen);
429         }
430         free(virtual_screens);
431
432         virtual_screens = new_screens;
433
434         LOG("Current workspace is now: %d\n", first->current_workspace);
435
436         render_layout(conn);
437 }