4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
20 #include <xcb/xinerama.h>
31 #include "workspace.h"
34 /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
35 * (xrandr --same-as) */
36 struct screens_head *virtual_screens;
38 static bool xinerama_enabled = true;
41 * Returns true if both screen objects describe the same screen (checks their
45 bool screens_are_equal(i3Screen *screen1, i3Screen *screen2) {
46 /* If one of both objects (or both) are NULL, we cannot compare them */
47 if (screen1 == NULL || screen2 == NULL)
50 /* If the pointers are equal, take the short-circuit */
51 if (screen1 == screen2)
54 /* Compare their size - other properties are not relevant to determine
55 * if a screen is equal to another one */
56 return (memcmp(&(screen1->rect), &(screen2->rect), sizeof(Rect)) == 0);
60 * Looks in virtual_screens for the i3Screen whose start coordinates are x, y
63 i3Screen *get_screen_at(int x, int y, struct screens_head *screenlist) {
65 TAILQ_FOREACH(screen, screenlist, screens)
66 if (screen->rect.x == x && screen->rect.y == y)
73 * Looks in virtual_screens for the i3Screen which contains coordinates x, y
76 i3Screen *get_screen_containing(int x, int y) {
78 TAILQ_FOREACH(screen, virtual_screens, screens) {
79 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
80 x, y, screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
81 if (x >= screen->rect.x && x < (screen->rect.x + screen->rect.width) &&
82 y >= screen->rect.y && y < (screen->rect.y + screen->rect.height))
90 * Gets the screen which is the last one in the given direction, for example the screen
91 * on the most bottom when direction == D_DOWN, the screen most right when direction == D_RIGHT
94 * This function always returns a screen.
97 i3Screen *get_screen_most(direction_t direction, i3Screen *current) {
98 i3Screen *screen, *candidate = NULL;
100 TAILQ_FOREACH(screen, virtual_screens, screens) {
101 /* Repeated calls of WIN determine the winner of the comparison */
102 #define WIN(variable, condition) \
103 if (variable condition) { \
104 candidate = screen; \
105 position = variable; \
109 if (((direction == D_UP) || (direction == D_DOWN)) &&
110 (current->rect.x != screen->rect.x))
113 if (((direction == D_LEFT) || (direction == D_RIGHT)) &&
114 (current->rect.y != screen->rect.y))
119 WIN(screen->rect.y, <= position);
121 WIN(screen->rect.y, >= position);
123 WIN(screen->rect.x, <= position);
125 WIN(screen->rect.x, >= position);
129 assert(candidate != NULL);
134 static void initialize_screen(xcb_connection_t *conn, i3Screen *screen, Workspace *workspace) {
135 i3Font *font = load_font(conn, config.font);
137 workspace->screen = screen;
138 screen->current_workspace = workspace;
140 /* Create a bar for each screen */
141 Rect bar_rect = {screen->rect.x,
142 screen->rect.y + screen->rect.height - (font->height + 6),
143 screen->rect.x + screen->rect.width,
145 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
146 uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS};
147 screen->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, true, mask, values);
148 screen->bargc = xcb_generate_id(conn);
149 xcb_create_gc(conn, screen->bargc, screen->bar, 0, 0);
151 SLIST_INIT(&(screen->dock_clients));
153 DLOG("that is virtual screen at %d x %d with %d x %d\n",
154 screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
158 * Fills virtual_screens with exactly one screen with width/height of the whole X server.
161 static void disable_xinerama(xcb_connection_t *conn) {
162 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
164 i3Screen *s = calloc(sizeof(i3Screen), 1);
168 s->rect.width = root_screen->width_in_pixels;
169 s->rect.height = root_screen->height_in_pixels;
174 TAILQ_INSERT_TAIL(virtual_screens, s, screens);
176 xinerama_enabled = false;
180 * Gets the Xinerama screens and converts them to virtual i3Screens (only one screen for two
181 * Xinerama screen which are configured in clone mode) in the given screenlist
184 static void query_screens(xcb_connection_t *conn, struct screens_head *screenlist) {
185 xcb_xinerama_query_screens_reply_t *reply;
186 xcb_xinerama_screen_info_t *screen_info;
187 time_t before_trying = time(NULL);
189 /* Try repeatedly to find screens (there might be short timeframes in
190 * which the X server does not return any screens, such as when rotating
191 * screens), but not longer than 5 seconds (strictly speaking, only four
192 * seconds of trying are guaranteed due to the 1-second-resolution) */
193 while ((time(NULL) - before_trying) < 10) {
194 reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
196 ELOG("Couldn't get Xinerama screens\n");
199 screen_info = xcb_xinerama_query_screens_screen_info(reply);
200 int screens = xcb_xinerama_query_screens_screen_info_length(reply);
203 for (int screen = 0; screen < screens; screen++) {
204 i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
206 DLOG("Re-used old Xinerama screen %p\n", s);
207 /* This screen already exists. We use the littlest screen so that the user
208 can always see the complete workspace */
209 s->rect.width = min(s->rect.width, screen_info[screen].width);
210 s->rect.height = min(s->rect.height, screen_info[screen].height);
212 s = calloc(sizeof(i3Screen), 1);
213 DLOG("Created new Xinerama screen %p\n", s);
214 s->rect.x = screen_info[screen].x_org;
215 s->rect.y = screen_info[screen].y_org;
216 s->rect.width = screen_info[screen].width;
217 s->rect.height = screen_info[screen].height;
218 /* We always treat the screen at 0x0 as the primary screen */
219 if (s->rect.x == 0 && s->rect.y == 0)
220 TAILQ_INSERT_HEAD(screenlist, s, screens);
221 else TAILQ_INSERT_TAIL(screenlist, s, screens);
225 DLOG("found Xinerama screen: %d x %d at %d x %d\n",
226 screen_info[screen].width, screen_info[screen].height,
227 screen_info[screen].x_org, screen_info[screen].y_org);
232 if (num_screens == 0) {
233 ELOG("No screens found. This is weird. Trying again...\n");
234 /* Give the scheduler a chance to do something else
235 * and don’t hog the CPU */
243 if (num_screens == 0) {
244 ELOG("No screens found for 10 seconds. Please fix your setup. i3 will exit now.\n");
250 * We have just established a connection to the X server and need the initial Xinerama
251 * information to setup workspaces for each screen.
254 void initialize_xinerama(xcb_connection_t *conn) {
255 virtual_screens = scalloc(sizeof(struct screens_head));
256 TAILQ_INIT(virtual_screens);
258 if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
259 DLOG("Xinerama extension not found, disabling.\n");
260 disable_xinerama(conn);
262 xcb_xinerama_is_active_reply_t *reply;
263 reply = xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL);
265 if (reply == NULL || !reply->state) {
266 DLOG("Xinerama is not active (in your X-Server), disabling.\n");
267 disable_xinerama(conn);
269 query_screens(conn, virtual_screens);
276 /* Just go through each workspace and associate as many screens as we can. */
277 TAILQ_FOREACH(screen, virtual_screens, screens) {
278 screen->num = num_screens;
280 Workspace *ws = get_first_workspace_for_screen(virtual_screens, screen);
281 initialize_screen(conn, screen, ws);
286 * This is called when the rootwindow receives a configure_notify event and therefore the
287 * number/position of the Xinerama screens could have changed.
290 void xinerama_requery_screens(xcb_connection_t *conn) {
291 i3Font *font = load_font(conn, config.font);
293 /* POSSIBLE PROBLEM: Is the order of the Xinerama screens always constant? That is, can
294 it change when I move the --right-of video projector to --left-of? */
296 if (!xinerama_enabled) {
297 DLOG("Xinerama is disabled\n");
301 /* We use a separate copy to diff with the previous set of screens */
302 struct screens_head *new_screens = scalloc(sizeof(struct screens_head));
303 TAILQ_INIT(new_screens);
305 query_screens(conn, new_screens);
307 i3Screen *first = TAILQ_FIRST(new_screens),
310 int screen_count = 0;
311 /* Mark each workspace which currently is assigned to a screen, so we
312 * can garbage-collect afterwards */
314 TAILQ_FOREACH(ws, workspaces, workspaces)
315 ws->reassigned = (ws->screen == NULL);
317 TAILQ_FOREACH(screen, new_screens, screens) {
318 screen->num = screen_count;
319 screen->current_workspace = NULL;
321 TAILQ_FOREACH(old_screen, virtual_screens, screens) {
322 if (old_screen->num != screen_count)
325 DLOG("Found a matching screen\n");
326 /* Use the same workspace */
327 screen->current_workspace = old_screen->current_workspace;
329 /* Re-use the old bar window */
330 screen->bar = old_screen->bar;
331 screen->bargc = old_screen->bargc;
332 DLOG("old_screen->bar = %p\n", old_screen->bar);
334 Rect bar_rect = {screen->rect.x,
335 screen->rect.y + screen->rect.height - (font->height + 6),
339 DLOG("configuring bar to be at %d x %d with %d x %d\n",
340 bar_rect.x, bar_rect.y, bar_rect.height, bar_rect.width);
341 xcb_configure_window(conn, screen->bar, XCB_CONFIG_WINDOW_X |
342 XCB_CONFIG_WINDOW_Y |
343 XCB_CONFIG_WINDOW_WIDTH |
344 XCB_CONFIG_WINDOW_HEIGHT, &(bar_rect.x));
346 /* Copy the list head for the dock clients */
347 screen->dock_clients = old_screen->dock_clients;
348 SLIST_INIT(&(old_screen->dock_clients));
350 /* Update the dimensions */
352 TAILQ_FOREACH(ws, workspaces, workspaces) {
353 if (ws->screen != old_screen)
356 DLOG("re-assigning ws %d\n", ws->num);
357 memcpy(&(ws->rect), &(screen->rect), sizeof(Rect));
359 ws->reassigned = true;
364 if (screen->current_workspace == NULL) {
365 /* Find the first unused workspace, preferring the ones
366 * which are assigned to this screen and initialize
367 * the screen with it. */
368 DLOG("getting first ws for screen %p\n", screen);
369 Workspace *ws = get_first_workspace_for_screen(new_screens, screen);
370 initialize_screen(conn, screen, ws);
371 ws->reassigned = true;
373 /* As this workspace just got visible (we got a new screen
374 * without workspace), we need to map its clients */
375 workspace_map_clients(conn, ws);
380 /* check for dock_clients which are out of bounds */
381 TAILQ_FOREACH(old_screen, virtual_screens, screens) {
382 if (SLIST_EMPTY(&(old_screen->dock_clients)))
385 DLOG("dock_clients out of bounds at screen %p, reassigning\n", old_screen);
386 if (SLIST_EMPTY(&(first->dock_clients))) {
387 first->dock_clients = old_screen->dock_clients;
391 /* We need to merge the lists */
394 while (!SLIST_EMPTY(&(old_screen->dock_clients))) {
395 dock_client = SLIST_FIRST(&(old_screen->dock_clients));
396 SLIST_INSERT_HEAD(&(first->dock_clients), dock_client, dock_clients);
397 SLIST_REMOVE_HEAD(&(old_screen->dock_clients), dock_clients);
401 /* Check for workspaces which are out of bounds */
402 TAILQ_FOREACH(ws, workspaces, workspaces) {
406 DLOG("Closing bar window (%p)\n", ws->screen->bar);
407 xcb_destroy_window(conn, ws->screen->bar);
409 DLOG("Workspace %d's screen out of bounds, assigning to first screen\n", ws->num + 1);
410 workspace_assign_to(ws, first);
415 /* Free the old list */
416 while (!TAILQ_EMPTY(virtual_screens)) {
417 screen = TAILQ_FIRST(virtual_screens);
418 TAILQ_REMOVE(virtual_screens, screen, screens);
421 free(virtual_screens);
423 virtual_screens = new_screens;
425 /* Check for workspaces which need to be assigned to specific screens
426 * which may now be available */
427 TAILQ_FOREACH(ws, workspaces, workspaces) {
428 if (ws->preferred_screen == NULL)
431 workspace_initialize(ws, ws->screen, true);
434 DLOG("Current workspace is now: %d\n", first->current_workspace);