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