]> git.sur5r.net Git - i3/i3/blobdiff - src/xinerama.c
Implement assignments of workspaces to screens, big cleanup of workspace code
[i3/i3] / src / xinerama.c
index ac820bd8d14bb999b365e2ba8964257918f0bdf2..d7ac5f29cf3b8c2cf52f613244896718e570fe26 100644 (file)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <stdbool.h>
 #include <assert.h>
+#include <time.h>
 
 #include <xcb/xcb.h>
 #include <xcb/xinerama.h>
@@ -26,6 +27,7 @@
 #include "layout.h"
 #include "xcb.h"
 #include "config.h"
+#include "workspace.h"
 
 /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
  * (xrandr --same-as) */
@@ -33,6 +35,25 @@ struct screens_head *virtual_screens;
 
 static bool xinerama_enabled = true;
 
+/*
+ * Returns true if both screen objects describe the same screen (checks their
+ * size and position).
+ *
+ */
+bool screens_are_equal(i3Screen *screen1, i3Screen *screen2) {
+        /* If one of both objects (or both) are NULL, we cannot compare them */
+        if (screen1 == NULL || screen2 == NULL)
+                return false;
+
+        /* If the pointers are equal, take the short-circuit */
+        if (screen1 == screen2)
+                return true;
+
+        /* Compare their size - other properties are not relevant to determine
+         * if a screen is equal to another one */
+        return (memcmp(&(screen1->rect), &(screen2->rect), sizeof(Rect)) == 0);
+}
+
 /*
  * Looks in virtual_screens for the i3Screen whose start coordinates are x, y
  *
@@ -100,6 +121,29 @@ i3Screen *get_screen_most(direction_t direction) {
         return candidate;
 }
 
+static void initialize_screen(xcb_connection_t *conn, i3Screen *screen, Workspace *workspace) {
+        i3Font *font = load_font(conn, config.font);
+
+        workspace->screen = screen;
+        screen->current_workspace = workspace->num;
+
+        /* Create a bar for each screen */
+        Rect bar_rect = {screen->rect.x,
+                         screen->rect.height - (font->height + 6),
+                         screen->rect.x + screen->rect.width,
+                         font->height + 6};
+        uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
+        uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS};
+        screen->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, true, mask, values);
+        screen->bargc = xcb_generate_id(conn);
+        xcb_create_gc(conn, screen->bargc, screen->bar, 0, 0);
+
+        SLIST_INIT(&(screen->dock_clients));
+
+        LOG("that is virtual screen at %d x %d with %d x %d\n",
+                        screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
+}
+
 /*
  * Fills virtual_screens with exactly one screen with width/height of the whole X server.
  *
@@ -114,6 +158,10 @@ static void disable_xinerama(xcb_connection_t *conn) {
         s->rect.width = root_screen->width_in_pixels;
         s->rect.height = root_screen->height_in_pixels;
 
+        num_screens = 1;
+        s->num = 0;
+        initialize_screen(conn, s, &(workspaces[0]));
+
         TAILQ_INSERT_TAIL(virtual_screens, s, screens);
 
         xinerama_enabled = false;
@@ -127,65 +175,56 @@ static void disable_xinerama(xcb_connection_t *conn) {
 static void query_screens(xcb_connection_t *conn, struct screens_head *screenlist) {
         xcb_xinerama_query_screens_reply_t *reply;
         xcb_xinerama_screen_info_t *screen_info;
-
-        reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
-        if (!reply) {
-                LOG("Couldn't get Xinerama screens\n");
-                return;
-        }
-        screen_info = xcb_xinerama_query_screens_screen_info(reply);
-        int screens = xcb_xinerama_query_screens_screen_info_length(reply);
-        num_screens = 0;
-
-        for (int screen = 0; screen < screens; screen++) {
-                i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
-                if (s!= NULL) {
-                        /* This screen already exists. We use the littlest screen so that the user
-                           can always see the complete workspace */
-                        s->rect.width = min(s->rect.width, screen_info[screen].width);
-                        s->rect.height = min(s->rect.height, screen_info[screen].height);
-                } else {
-                        s = calloc(sizeof(i3Screen), 1);
-                        s->rect.x = screen_info[screen].x_org;
-                        s->rect.y = screen_info[screen].y_org;
-                        s->rect.width = screen_info[screen].width;
-                        s->rect.height = screen_info[screen].height;
-                        /* We always treat the screen at 0x0 as the primary screen */
-                        if (s->rect.x == 0 && s->rect.y == 0)
-                                TAILQ_INSERT_HEAD(screenlist, s, screens);
-                        else TAILQ_INSERT_TAIL(screenlist, s, screens);
-                        num_screens++;
+        time_t before_trying = time(NULL);
+
+        /* Try repeatedly to find screens (there might be short timeframes in
+         * which the X server does not return any screens, such as when rotating
+         * screens), but not longer than 5 seconds (strictly speaking, only four
+         * seconds of trying are guaranteed due to the 1-second-resolution) */
+        while ((time(NULL) - before_trying) < 5) {
+                reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
+                if (!reply) {
+                        LOG("Couldn't get Xinerama screens\n");
+                        return;
                 }
+                screen_info = xcb_xinerama_query_screens_screen_info(reply);
+                int screens = xcb_xinerama_query_screens_screen_info_length(reply);
+                num_screens = 0;
+
+                for (int screen = 0; screen < screens; screen++) {
+                        i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
+                        if (s != NULL) {
+                                /* This screen already exists. We use the littlest screen so that the user
+                                   can always see the complete workspace */
+                                s->rect.width = min(s->rect.width, screen_info[screen].width);
+                                s->rect.height = min(s->rect.height, screen_info[screen].height);
+                        } else {
+                                s = calloc(sizeof(i3Screen), 1);
+                                s->rect.x = screen_info[screen].x_org;
+                                s->rect.y = screen_info[screen].y_org;
+                                s->rect.width = screen_info[screen].width;
+                                s->rect.height = screen_info[screen].height;
+                                /* We always treat the screen at 0x0 as the primary screen */
+                                if (s->rect.x == 0 && s->rect.y == 0)
+                                        TAILQ_INSERT_HEAD(screenlist, s, screens);
+                                else TAILQ_INSERT_TAIL(screenlist, s, screens);
+                                num_screens++;
+                        }
 
-                LOG("found Xinerama screen: %d x %d at %d x %d\n",
-                                screen_info[screen].width, screen_info[screen].height,
-                                screen_info[screen].x_org, screen_info[screen].y_org);
-        }
-
-        free(reply);
-}
-
-static void initialize_screen(xcb_connection_t *conn, i3Screen *screen, Workspace *workspace) {
-        i3Font *font = load_font(conn, config.font);
+                        LOG("found Xinerama screen: %d x %d at %d x %d\n",
+                                        screen_info[screen].width, screen_info[screen].height,
+                                        screen_info[screen].x_org, screen_info[screen].y_org);
+                }
 
-        workspace->screen = screen;
-        screen->current_workspace = workspace->num;
+                free(reply);
 
-        /* Create a bar for each screen */
-        Rect bar_rect = {screen->rect.x,
-                         screen->rect.height - (font->height + 6),
-                         screen->rect.x + screen->rect.width,
-                         font->height + 6};
-        uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
-        uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS};
-        screen->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, mask, values);
-        screen->bargc = xcb_generate_id(conn);
-        xcb_create_gc(conn, screen->bargc, screen->bar, 0, 0);
+                if (num_screens == 0) {
+                        LOG("No screens found. This is weird. Trying again...\n");
+                        continue;
+                }
 
-        /* Copy dimensions */
-        memcpy(&(workspace->rect), &(screen->rect), sizeof(Rect));
-        LOG("that is virtual screen at %d x %d with %d x %d\n",
-                        screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
+                break;
+        }
 }
 
 /*
@@ -208,22 +247,20 @@ void initialize_xinerama(xcb_connection_t *conn) {
 
         if (reply == NULL || !reply->state) {
                 LOG("Xinerama is not active (in your X-Server), disabling.\n");
-                FREE(reply);
                 disable_xinerama(conn);
-                return;
-        }
+        } else
+                query_screens(conn, virtual_screens);
 
         FREE(reply);
 
-        query_screens(conn, virtual_screens);
-
-        i3Screen *s;
+        i3Screen *screen;
         num_screens = 0;
         /* Just go through each workspace and associate as many screens as we can. */
-        TAILQ_FOREACH(s, virtual_screens, screens) {
-                s->num = num_screens;
-                initialize_screen(conn, s, &(workspaces[num_screens]));
+        TAILQ_FOREACH(screen, virtual_screens, screens) {
+                screen->num = num_screens;
                 num_screens++;
+                Workspace *ws = get_first_workspace_for_screen(virtual_screens, screen);
+                initialize_screen(conn, screen, ws);
         }
 }
 
@@ -233,6 +270,8 @@ void initialize_xinerama(xcb_connection_t *conn) {
  *
  */
 void xinerama_requery_screens(xcb_connection_t *conn) {
+        i3Font *font = load_font(conn, config.font);
+
         /* POSSIBLE PROBLEM: Is the order of the Xinerama screens always constant? That is, can
            it change when I move the --right-of video projector to --left-of? */
 
@@ -248,53 +287,111 @@ void xinerama_requery_screens(xcb_connection_t *conn) {
         query_screens(conn, new_screens);
 
         i3Screen *first = TAILQ_FIRST(new_screens),
-                 *screen;
+                 *screen,
+                 *old_screen;
         int screen_count = 0;
+        /* Mark each workspace which currently is assigned to a screen, so we
+         * can garbage-collect afterwards */
+        for (int c = 0; c < 10; c++)
+                workspaces[c].reassigned = (workspaces[c].screen == NULL);
+
         TAILQ_FOREACH(screen, new_screens, screens) {
                 screen->num = screen_count;
                 screen->current_workspace = -1;
-                for (int c = 0; c < 10; c++)
-                        if ((workspaces[c].screen != NULL) &&
-                            (workspaces[c].screen->num == screen_count)) {
-                                LOG("Found a matching screen\n");
-                                /* Try to use the same workspace, if it’s available */
-                                if (workspaces[c].screen->current_workspace)
-                                        screen->current_workspace = workspaces[c].screen->current_workspace;
-
-                                if (screen->current_workspace == -1)
-                                        screen->current_workspace = c;
-
-                                /* Re-use the old bar window */
-                                screen->bar = workspaces[c].screen->bar;
-                                screen->bargc = workspaces[c].screen->bargc;
-
-                                /* Update the dimensions */
-                                memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect));
-                                workspaces[c].screen = screen;
+
+                TAILQ_FOREACH(old_screen, virtual_screens, screens) {
+                        if (old_screen->num != screen_count)
+                                continue;
+
+                        LOG("Found a matching screen\n");
+                        /* Use the same workspace */
+                        screen->current_workspace = old_screen->current_workspace;
+
+                        /* Re-use the old bar window */
+                        screen->bar = old_screen->bar;
+                        screen->bargc = old_screen->bargc;
+                        LOG("old_screen->bar = %p\n", old_screen->bar);
+
+                        Rect bar_rect = {screen->rect.x,
+                                         screen->rect.height - (font->height + 6),
+                                         screen->rect.x + screen->rect.width,
+                                         font->height + 6};
+
+                        LOG("configuring bar to be at %d x %d with %d x %d\n",
+                                        bar_rect.x, bar_rect.y, bar_rect.height, bar_rect.width);
+                        xcb_configure_window(conn, screen->bar, XCB_CONFIG_WINDOW_X |
+                                                                XCB_CONFIG_WINDOW_Y |
+                                                                XCB_CONFIG_WINDOW_WIDTH |
+                                                                XCB_CONFIG_WINDOW_HEIGHT, &(bar_rect.x));
+
+                        /* Copy the list head for the dock clients */
+                        screen->dock_clients = old_screen->dock_clients;
+
+                        /* Update the dimensions */
+                        for (int c = 0; c < 10; c++) {
+                                Workspace *ws = &(workspaces[c]);
+                                if (ws->screen != old_screen)
+                                        continue;
+
+                                LOG("re-assigning ws %d\n", ws->num);
+                                memcpy(&(ws->rect), &(screen->rect), sizeof(Rect));
+                                ws->screen = screen;
+                                ws->reassigned = true;
                         }
+
+                        break;
+                }
                 if (screen->current_workspace == -1) {
-                        /* Create a new workspace for this screen, it’s new */
-                        for (int c = 0; c < 10; c++)
-                                if (workspaces[c].screen == NULL) {
-                                        LOG("fix: initializing new workspace, setting num to %d\n", c);
-                                        initialize_screen(conn, screen, &(workspaces[c]));
-                                        break;
-                                }
+                        /* Find the first unused workspace, preferring the ones
+                         * which are assigned to this screen and initialize
+                         * the screen with it. */
+                        LOG("getting first ws for screen %p\n", screen);
+                        Workspace *ws = get_first_workspace_for_screen(new_screens, screen);
+                        initialize_screen(conn, screen, ws);
+
+                        /* As this workspace just got visible (we got a new screen
+                         * without workspace), we need to map its clients */
+                        workspace_map_clients(conn, ws);
                 }
                 screen_count++;
         }
 
         /* Check for workspaces which are out of bounds */
-        for (int c = 0; c < 10; c++)
-                if ((workspaces[c].screen != NULL) &&
-                    (workspaces[c].screen->num >= num_screens)) {
-                        LOG("Closing bar window\n");
-                        xcb_destroy_window(conn, workspaces[c].screen->bar);
-
-                        LOG("Workspace %d's screen out of bounds, assigning to first screen\n", c+1);
-                        workspaces[c].screen = first;
-                        memcpy(&(workspaces[c].rect), &(first->rect), sizeof(Rect));
+        for (int c = 0; c < 10; c++) {
+                if (workspaces[c].reassigned)
+                        continue;
+
+                /* f_ws is a shortcut to the workspace to fix */
+                Workspace *f_ws = &(workspaces[c]);
+                Client *client;
+
+                LOG("Closing bar window (%p)\n", f_ws->screen->bar);
+                xcb_destroy_window(conn, f_ws->screen->bar);
+
+                LOG("Workspace %d's screen out of bounds, assigning to first screen\n", c+1);
+                f_ws->screen = first;
+                memcpy(&(f_ws->rect), &(first->rect), sizeof(Rect));
+
+                /* Force reconfiguration for each client on that workspace */
+                FOR_TABLE(f_ws)
+                        CIRCLEQ_FOREACH(client, &(f_ws->table[cols][rows]->clients), clients)
+                                client->force_reconfigure = true;
+
+                /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
+                render_workspace(conn, first, f_ws);
+
+                /* …unless we want to see them at the moment, we should hide that workspace */
+                if (workspace_is_visible(f_ws))
+                        continue;
+
+                workspace_unmap_clients(conn, f_ws);
+
+                if (c_ws == f_ws) {
+                        LOG("Need to adjust c_ws...\n");
+                        c_ws = &(workspaces[first->current_workspace]);
                 }
+        }
+        xcb_flush(conn);
 
         /* Free the old list */
         while (!TAILQ_EMPTY(virtual_screens)) {