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