4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
17 #include <xcb/xinerama.h>
27 /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
28 * (xrandr --same-as) */
29 struct screens_head *virtual_screens;
31 static bool xinerama_enabled = true;
34 * Looks in virtual_screens for the i3Screen whose start coordinates are x, y
37 i3Screen *get_screen_at(int x, int y, struct screens_head *screenlist) {
39 TAILQ_FOREACH(screen, screenlist, screens)
40 if (screen->rect.x == x && screen->rect.y == y)
47 * Looks in virtual_screens for the i3Screen which contains coordinates x, y
50 i3Screen *get_screen_containing(int x, int y) {
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))
64 * Fills virtual_screens with exactly one screen with width/height of the whole X server.
67 static void disable_xinerama(xcb_connection_t *connection) {
68 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
70 i3Screen *s = calloc(sizeof(i3Screen), 1);
74 s->rect.width = root_screen->width_in_pixels;
75 s->rect.height = root_screen->height_in_pixels;
77 TAILQ_INSERT_TAIL(virtual_screens, s, screens);
79 xinerama_enabled = false;
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
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;
91 reply = xcb_xinerama_query_screens_reply(connection, xcb_xinerama_query_screens_unchecked(connection), NULL);
93 printf("Couldn't get Xinerama screens\n");
96 screen_info = xcb_xinerama_query_screens_screen_info(reply);
97 int screens = xcb_xinerama_query_screens_screen_info_length(reply);
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);
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);
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);
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);
129 * We have just established a connection to the X server and need the initial Xinerama
130 * information to setup workspaces for each screen.
133 void initialize_xinerama(xcb_connection_t *connection) {
134 virtual_screens = scalloc(sizeof(struct screens_head));
135 TAILQ_INIT(virtual_screens);
137 if (!xcb_get_extension_data(connection, &xcb_xinerama_id)->present) {
138 printf("Xinerama extension not found, disabling.\n");
139 disable_xinerama(connection);
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);
149 query_screens(connection, virtual_screens);
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);
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.
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? */
173 if (!xinerama_enabled) {
174 printf("Xinerama is disabled\n");
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);
182 query_screens(connection, new_screens);
184 i3Screen *first = TAILQ_FIRST(new_screens),
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;
198 if (screen->current_workspace == -1)
199 screen->current_workspace = c;
201 /* Update the dimensions */
202 memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect));
203 workspaces[c].screen = screen;
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));
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));
229 /* Free the old list */
230 TAILQ_FOREACH(screen, virtual_screens, screens) {
231 TAILQ_REMOVE(virtual_screens, screen, screens);
234 free(virtual_screens);
236 virtual_screens = new_screens;
238 printf("Current workspace is now: %d\n", first->current_workspace);
240 render_layout(connection);