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