]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
8e7f7918ec5a42548c0943764f80e9944b51b34e
[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
16 #include <xcb/xcb.h>
17 #include <xcb/xinerama.h>
18
19 #include "queue.h"
20 #include "i3.h"
21 #include "data.h"
22 #include "table.h"
23 #include "util.h"
24 #include "xinerama.h"
25 #include "layout.h"
26
27 /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
28  * (xrandr --same-as) */
29 struct screens_head *virtual_screens;
30
31 static bool xinerama_enabled = true;
32
33 /*
34  * Looks in virtual_screens for the i3Screen whose start coordinates are x, y
35  *
36  */
37 i3Screen *get_screen_at(int x, int y, struct screens_head *screenlist) {
38         i3Screen *screen;
39         TAILQ_FOREACH(screen, screenlist, screens)
40                 if (screen->rect.x == x && screen->rect.y == y)
41                         return screen;
42
43         return NULL;
44 }
45
46 /*
47  * Looks in virtual_screens for the i3Screen which contains coordinates x, y
48  *
49  */
50 i3Screen *get_screen_containing(int x, int y) {
51         i3Screen *screen;
52         TAILQ_FOREACH(screen, virtual_screens, screens) {
53                 printf("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
54                                 x, y, screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
55                 if (x >= screen->rect.x && x < (screen->rect.x + screen->rect.width) &&
56                     y >= screen->rect.y && y < (screen->rect.y + screen->rect.height))
57                         return screen;
58         }
59
60         return NULL;
61 }
62
63 /*
64  * Fills virtual_screens with exactly one screen with width/height of the whole X server.
65  *
66  */
67 static void disable_xinerama(xcb_connection_t *connection) {
68         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
69
70         i3Screen *s = calloc(sizeof(i3Screen), 1);
71
72         s->rect.x = 0;
73         s->rect.y = 0;
74         s->rect.width = root_screen->width_in_pixels;
75         s->rect.height = root_screen->height_in_pixels;
76
77         TAILQ_INSERT_TAIL(virtual_screens, s, screens);
78
79         xinerama_enabled = false;
80 }
81
82 /*
83  * Gets the Xinerama screens and converts them to virtual i3Screens (only one screen for two
84  * Xinerama screen which are configured in clone mode) in the given screenlist
85  *
86  */
87 static void query_screens(xcb_connection_t *connection, struct screens_head *screenlist) {
88         xcb_xinerama_query_screens_reply_t *reply;
89         xcb_xinerama_screen_info_t *screen_info;
90
91         reply = xcb_xinerama_query_screens_reply(connection, xcb_xinerama_query_screens_unchecked(connection), NULL);
92         if (!reply) {
93                 printf("Couldn't get Xinerama screens\n");
94                 return;
95         }
96         screen_info = xcb_xinerama_query_screens_screen_info(reply);
97         int screens = xcb_xinerama_query_screens_screen_info_length(reply);
98         num_screens = 0;
99
100         for (int screen = 0; screen < screens; screen++) {
101                 i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
102                 if (s!= NULL) {
103                         /* This screen already exists. We use the littlest screen so that the user
104                            can always see the complete workspace */
105                         s->rect.width = min(s->rect.width, screen_info[screen].width);
106                         s->rect.height = min(s->rect.height, screen_info[screen].height);
107                 } else {
108                         s = calloc(sizeof(i3Screen), 1);
109                         s->rect.x = screen_info[screen].x_org;
110                         s->rect.y = screen_info[screen].y_org;
111                         s->rect.width = screen_info[screen].width;
112                         s->rect.height = screen_info[screen].height;
113                         /* We always treat the screen at 0x0 as the primary screen */
114                         if (s->rect.x == 0 && s->rect.y == 0)
115                                 TAILQ_INSERT_HEAD(screenlist, s, screens);
116                         else TAILQ_INSERT_TAIL(screenlist, s, screens);
117                         num_screens++;
118                 }
119
120                 printf("found Xinerama screen: %d x %d at %d x %d\n",
121                                 screen_info[screen].width, screen_info[screen].height,
122                                 screen_info[screen].x_org, screen_info[screen].y_org);
123         }
124
125         free(reply);
126 }
127
128 /*
129  * We have just established a connection to the X server and need the initial Xinerama
130  * information to setup workspaces for each screen.
131  *
132  */
133 void initialize_xinerama(xcb_connection_t *connection) {
134         virtual_screens = scalloc(sizeof(struct screens_head));
135         TAILQ_INIT(virtual_screens);
136
137         if (!xcb_get_extension_data(connection, &xcb_xinerama_id)->present) {
138                 printf("Xinerama extension not found, disabling.\n");
139                 disable_xinerama(connection);
140                 return;
141         }
142
143         if (!xcb_xinerama_is_active_reply(connection, xcb_xinerama_is_active(connection), NULL)->state) {
144                 printf("Xinerama is not active (in your X-Server), disabling.\n");
145                 disable_xinerama(connection);
146                 return;
147         }
148
149         query_screens(connection, virtual_screens);
150
151         i3Screen *s;
152         num_screens = 0;
153         /* Just go through each workspace and associate as many screens as we can. */
154         TAILQ_FOREACH(s, virtual_screens, screens) {
155                 s->num = num_screens;
156                 s->current_workspace = num_screens;
157                 workspaces[num_screens].screen = s;
158                 memcpy(&(workspaces[num_screens++].rect), &(s->rect), sizeof(Rect));
159                 printf("that is virtual screen at %d x %d with %d x %d\n",
160                                 s->rect.x, s->rect.y, s->rect.width, s->rect.height);
161         }
162 }
163
164 /*
165  * This is called when the rootwindow receives a configure_notify event and therefore the
166  * number/position of the Xinerama screens could have changed.
167  *
168  */
169 void xinerama_requery_screens(xcb_connection_t *connection) {
170         /* POSSIBLE PROBLEM: Is the order of the Xinerama screens always constant? That is, can
171            it change when I move the --right-of video projector to --left-of? */
172
173         if (!xinerama_enabled) {
174                 printf("Xinerama is disabled\n");
175                 return;
176         }
177
178         /* We use a separate copy to diff with the previous set of screens */
179         struct screens_head *new_screens = scalloc(sizeof(struct screens_head));
180         TAILQ_INIT(new_screens);
181
182         query_screens(connection, new_screens);
183
184         i3Screen *first = TAILQ_FIRST(new_screens),
185                  *screen;
186         int screen_count = 0;
187         TAILQ_FOREACH(screen, new_screens, screens) {
188                 screen->num = screen_count;
189                 screen->current_workspace = -1;
190                 for (int c = 0; c < 10; c++)
191                         if ((workspaces[c].screen != NULL) &&
192                             (workspaces[c].screen->num == screen_count)) {
193                                 printf("Found a matching screen\n");
194                                 /* Try to use the same workspace, if it’s available */
195                                 if (workspaces[c].screen->current_workspace)
196                                         screen->current_workspace = workspaces[c].screen->current_workspace;
197
198                                 if (screen->current_workspace == -1)
199                                         screen->current_workspace = c;
200
201                                 /* Update the dimensions */
202                                 memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect));
203                                 workspaces[c].screen = screen;
204                         }
205                 if (screen->current_workspace == -1) {
206                         /* Create a new workspace for this screen, it’s new */
207                         for (int c = 0; c < 10; c++)
208                                 if (workspaces[c].screen == NULL) {
209                                         printf("fix: initializing new workspace, setting num to %d\n", c);
210                                         workspaces[c].screen = screen;
211                                         screen->current_workspace = c;
212                                         /* Copy the dimensions from the virtual screen */
213                                         memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect));
214                                         break;
215                                 }
216                 }
217                 screen_count++;
218         }
219
220         /* Check for workspaces which are out of bounds */
221         for (int c = 0; c < 10; c++)
222                 if ((workspaces[c].screen != NULL) &&
223                     (workspaces[c].screen->num >= num_screens)) {
224                         printf("Workspace %d's screen out of bounds, assigning to first screen\n", c+1);
225                         workspaces[c].screen = first;
226                         memcpy(&(workspaces[c].rect), &(first->rect), sizeof(Rect));
227                 }
228
229         /* Free the old list */
230         TAILQ_FOREACH(screen, virtual_screens, screens) {
231                 TAILQ_REMOVE(virtual_screens, screen, screens);
232                 free(screen);
233         }
234         free(virtual_screens);
235
236         virtual_screens = new_screens;
237
238         printf("Current workspace is now: %d\n", first->current_workspace);
239
240         render_layout(connection);
241 }