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