]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
Bugfix: Don’t initialize screen twice when not using Xinerama (Thanks badboy)
[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) {
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                 switch (direction) {
108                         case D_UP:
109                                 WIN(screen->rect.y, <= position);
110                         case D_DOWN:
111                                 WIN(screen->rect.y, >= position);
112                         case D_LEFT:
113                                 WIN(screen->rect.x, <= position);
114                         case D_RIGHT:
115                                 WIN(screen->rect.x, >= position);
116                 }
117         }
118
119         assert(candidate != NULL);
120
121         return candidate;
122 }
123
124 static void initialize_screen(xcb_connection_t *conn, i3Screen *screen, Workspace *workspace) {
125         i3Font *font = load_font(conn, config.font);
126
127         workspace->screen = screen;
128         screen->current_workspace = workspace->num;
129
130         /* Create a bar for each screen */
131         Rect bar_rect = {screen->rect.x,
132                          screen->rect.height - (font->height + 6),
133                          screen->rect.x + screen->rect.width,
134                          font->height + 6};
135         uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
136         uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS};
137         screen->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, true, mask, values);
138         screen->bargc = xcb_generate_id(conn);
139         xcb_create_gc(conn, screen->bargc, screen->bar, 0, 0);
140
141         SLIST_INIT(&(screen->dock_clients));
142
143         LOG("that is virtual screen at %d x %d with %d x %d\n",
144                         screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
145 }
146
147 /*
148  * Fills virtual_screens with exactly one screen with width/height of the whole X server.
149  *
150  */
151 static void disable_xinerama(xcb_connection_t *conn) {
152         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
153
154         i3Screen *s = calloc(sizeof(i3Screen), 1);
155
156         s->rect.x = 0;
157         s->rect.y = 0;
158         s->rect.width = root_screen->width_in_pixels;
159         s->rect.height = root_screen->height_in_pixels;
160
161         num_screens = 1;
162         s->num = 0;
163
164         TAILQ_INSERT_TAIL(virtual_screens, s, screens);
165
166         xinerama_enabled = false;
167 }
168
169 /*
170  * Gets the Xinerama screens and converts them to virtual i3Screens (only one screen for two
171  * Xinerama screen which are configured in clone mode) in the given screenlist
172  *
173  */
174 static void query_screens(xcb_connection_t *conn, struct screens_head *screenlist) {
175         xcb_xinerama_query_screens_reply_t *reply;
176         xcb_xinerama_screen_info_t *screen_info;
177         time_t before_trying = time(NULL);
178
179         /* Try repeatedly to find screens (there might be short timeframes in
180          * which the X server does not return any screens, such as when rotating
181          * screens), but not longer than 5 seconds (strictly speaking, only four
182          * seconds of trying are guaranteed due to the 1-second-resolution) */
183         while ((time(NULL) - before_trying) < 5) {
184                 reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
185                 if (!reply) {
186                         LOG("Couldn't get Xinerama screens\n");
187                         return;
188                 }
189                 screen_info = xcb_xinerama_query_screens_screen_info(reply);
190                 int screens = xcb_xinerama_query_screens_screen_info_length(reply);
191                 num_screens = 0;
192
193                 for (int screen = 0; screen < screens; screen++) {
194                         i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
195                         if (s != NULL) {
196                                 /* This screen already exists. We use the littlest screen so that the user
197                                    can always see the complete workspace */
198                                 s->rect.width = min(s->rect.width, screen_info[screen].width);
199                                 s->rect.height = min(s->rect.height, screen_info[screen].height);
200                         } else {
201                                 s = calloc(sizeof(i3Screen), 1);
202                                 s->rect.x = screen_info[screen].x_org;
203                                 s->rect.y = screen_info[screen].y_org;
204                                 s->rect.width = screen_info[screen].width;
205                                 s->rect.height = screen_info[screen].height;
206                                 /* We always treat the screen at 0x0 as the primary screen */
207                                 if (s->rect.x == 0 && s->rect.y == 0)
208                                         TAILQ_INSERT_HEAD(screenlist, s, screens);
209                                 else TAILQ_INSERT_TAIL(screenlist, s, screens);
210                                 num_screens++;
211                         }
212
213                         LOG("found Xinerama screen: %d x %d at %d x %d\n",
214                                         screen_info[screen].width, screen_info[screen].height,
215                                         screen_info[screen].x_org, screen_info[screen].y_org);
216                 }
217
218                 free(reply);
219
220                 if (num_screens == 0) {
221                         LOG("No screens found. This is weird. Trying again...\n");
222                         continue;
223                 }
224
225                 break;
226         }
227 }
228
229 /*
230  * We have just established a connection to the X server and need the initial Xinerama
231  * information to setup workspaces for each screen.
232  *
233  */
234 void initialize_xinerama(xcb_connection_t *conn) {
235         virtual_screens = scalloc(sizeof(struct screens_head));
236         TAILQ_INIT(virtual_screens);
237
238         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
239                 LOG("Xinerama extension not found, disabling.\n");
240                 disable_xinerama(conn);
241                 return;
242         }
243
244         xcb_xinerama_is_active_reply_t *reply;
245         reply = xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL);
246
247         if (reply == NULL || !reply->state) {
248                 LOG("Xinerama is not active (in your X-Server), disabling.\n");
249                 disable_xinerama(conn);
250         } else
251                 query_screens(conn, virtual_screens);
252
253         FREE(reply);
254
255         i3Screen *screen;
256         num_screens = 0;
257         /* Just go through each workspace and associate as many screens as we can. */
258         TAILQ_FOREACH(screen, virtual_screens, screens) {
259                 screen->num = num_screens;
260                 num_screens++;
261                 Workspace *ws = get_first_workspace_for_screen(virtual_screens, screen);
262                 initialize_screen(conn, screen, ws);
263         }
264 }
265
266 /*
267  * This is called when the rootwindow receives a configure_notify event and therefore the
268  * number/position of the Xinerama screens could have changed.
269  *
270  */
271 void xinerama_requery_screens(xcb_connection_t *conn) {
272         i3Font *font = load_font(conn, config.font);
273
274         /* POSSIBLE PROBLEM: Is the order of the Xinerama screens always constant? That is, can
275            it change when I move the --right-of video projector to --left-of? */
276
277         if (!xinerama_enabled) {
278                 LOG("Xinerama is disabled\n");
279                 return;
280         }
281
282         /* We use a separate copy to diff with the previous set of screens */
283         struct screens_head *new_screens = scalloc(sizeof(struct screens_head));
284         TAILQ_INIT(new_screens);
285
286         query_screens(conn, new_screens);
287
288         i3Screen *first = TAILQ_FIRST(new_screens),
289                  *screen,
290                  *old_screen;
291         int screen_count = 0;
292         /* Mark each workspace which currently is assigned to a screen, so we
293          * can garbage-collect afterwards */
294         for (int c = 0; c < 10; c++)
295                 workspaces[c].reassigned = (workspaces[c].screen == NULL);
296
297         TAILQ_FOREACH(screen, new_screens, screens) {
298                 screen->num = screen_count;
299                 screen->current_workspace = -1;
300
301                 TAILQ_FOREACH(old_screen, virtual_screens, screens) {
302                         if (old_screen->num != screen_count)
303                                 continue;
304
305                         LOG("Found a matching screen\n");
306                         /* Use the same workspace */
307                         screen->current_workspace = old_screen->current_workspace;
308
309                         /* Re-use the old bar window */
310                         screen->bar = old_screen->bar;
311                         screen->bargc = old_screen->bargc;
312                         LOG("old_screen->bar = %p\n", old_screen->bar);
313
314                         Rect bar_rect = {screen->rect.x,
315                                          screen->rect.height - (font->height + 6),
316                                          screen->rect.x + screen->rect.width,
317                                          font->height + 6};
318
319                         LOG("configuring bar to be at %d x %d with %d x %d\n",
320                                         bar_rect.x, bar_rect.y, bar_rect.height, bar_rect.width);
321                         xcb_configure_window(conn, screen->bar, XCB_CONFIG_WINDOW_X |
322                                                                 XCB_CONFIG_WINDOW_Y |
323                                                                 XCB_CONFIG_WINDOW_WIDTH |
324                                                                 XCB_CONFIG_WINDOW_HEIGHT, &(bar_rect.x));
325
326                         /* Copy the list head for the dock clients */
327                         screen->dock_clients = old_screen->dock_clients;
328
329                         /* Update the dimensions */
330                         for (int c = 0; c < 10; c++) {
331                                 Workspace *ws = &(workspaces[c]);
332                                 if (ws->screen != old_screen)
333                                         continue;
334
335                                 LOG("re-assigning ws %d\n", ws->num);
336                                 memcpy(&(ws->rect), &(screen->rect), sizeof(Rect));
337                                 ws->screen = screen;
338                                 ws->reassigned = true;
339                         }
340
341                         break;
342                 }
343                 if (screen->current_workspace == -1) {
344                         /* Find the first unused workspace, preferring the ones
345                          * which are assigned to this screen and initialize
346                          * the screen with it. */
347                         LOG("getting first ws for screen %p\n", screen);
348                         Workspace *ws = get_first_workspace_for_screen(new_screens, screen);
349                         initialize_screen(conn, screen, ws);
350
351                         /* As this workspace just got visible (we got a new screen
352                          * without workspace), we need to map its clients */
353                         workspace_map_clients(conn, ws);
354                 }
355                 screen_count++;
356         }
357
358         /* Check for workspaces which are out of bounds */
359         for (int c = 0; c < 10; c++) {
360                 if (workspaces[c].reassigned)
361                         continue;
362
363                 /* f_ws is a shortcut to the workspace to fix */
364                 Workspace *f_ws = &(workspaces[c]);
365                 Client *client;
366
367                 LOG("Closing bar window (%p)\n", f_ws->screen->bar);
368                 xcb_destroy_window(conn, f_ws->screen->bar);
369
370                 LOG("Workspace %d's screen out of bounds, assigning to first screen\n", c+1);
371                 f_ws->screen = first;
372                 memcpy(&(f_ws->rect), &(first->rect), sizeof(Rect));
373
374                 /* Force reconfiguration for each client on that workspace */
375                 FOR_TABLE(f_ws)
376                         CIRCLEQ_FOREACH(client, &(f_ws->table[cols][rows]->clients), clients)
377                                 client->force_reconfigure = true;
378
379                 /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
380                 render_workspace(conn, first, f_ws);
381
382                 /* …unless we want to see them at the moment, we should hide that workspace */
383                 if (workspace_is_visible(f_ws))
384                         continue;
385
386                 workspace_unmap_clients(conn, f_ws);
387
388                 if (c_ws == f_ws) {
389                         LOG("Need to adjust c_ws...\n");
390                         c_ws = &(workspaces[first->current_workspace]);
391                 }
392         }
393         xcb_flush(conn);
394
395         /* Free the old list */
396         while (!TAILQ_EMPTY(virtual_screens)) {
397                 screen = TAILQ_FIRST(virtual_screens);
398                 TAILQ_REMOVE(virtual_screens, screen, screens);
399                 free(screen);
400         }
401         free(virtual_screens);
402
403         virtual_screens = new_screens;
404
405         LOG("Current workspace is now: %d\n", first->current_workspace);
406
407         render_layout(conn);
408 }