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