4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
18 #include <xcb/xinerama.h>
30 /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
31 * (xrandr --same-as) */
32 struct screens_head *virtual_screens;
34 static bool xinerama_enabled = true;
37 * Looks in virtual_screens for the i3Screen whose start coordinates are x, y
40 i3Screen *get_screen_at(int x, int y, struct screens_head *screenlist) {
42 TAILQ_FOREACH(screen, screenlist, screens)
43 if (screen->rect.x == x && screen->rect.y == y)
50 * Looks in virtual_screens for the i3Screen which contains coordinates x, y
53 i3Screen *get_screen_containing(int x, int y) {
55 TAILQ_FOREACH(screen, virtual_screens, screens) {
56 LOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
57 x, y, screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
58 if (x >= screen->rect.x && x < (screen->rect.x + screen->rect.width) &&
59 y >= screen->rect.y && y < (screen->rect.y + screen->rect.height))
67 * Gets the screen which is the last one in the given direction, for example the screen
68 * on the most bottom when direction == D_DOWN, the screen most right when direction == D_RIGHT
71 * This function always returns a screen.
74 i3Screen *get_screen_most(direction_t direction) {
75 i3Screen *screen, *candidate = NULL;
77 TAILQ_FOREACH(screen, virtual_screens, screens) {
78 /* Repeated calls of WIN determine the winner of the comparison */
79 #define WIN(variable, condition) \
80 if (variable condition) { \
82 position = variable; \
88 WIN(screen->rect.y, <= position);
90 WIN(screen->rect.y, >= position);
92 WIN(screen->rect.x, <= position);
94 WIN(screen->rect.x, >= position);
98 assert(candidate != NULL);
104 * Fills virtual_screens with exactly one screen with width/height of the whole X server.
107 static void disable_xinerama(xcb_connection_t *conn) {
108 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
110 i3Screen *s = calloc(sizeof(i3Screen), 1);
114 s->rect.width = root_screen->width_in_pixels;
115 s->rect.height = root_screen->height_in_pixels;
117 TAILQ_INSERT_TAIL(virtual_screens, s, screens);
119 xinerama_enabled = false;
123 * Gets the Xinerama screens and converts them to virtual i3Screens (only one screen for two
124 * Xinerama screen which are configured in clone mode) in the given screenlist
127 static void query_screens(xcb_connection_t *conn, struct screens_head *screenlist) {
128 xcb_xinerama_query_screens_reply_t *reply;
129 xcb_xinerama_screen_info_t *screen_info;
131 reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
133 LOG("Couldn't get Xinerama screens\n");
136 screen_info = xcb_xinerama_query_screens_screen_info(reply);
137 int screens = xcb_xinerama_query_screens_screen_info_length(reply);
140 for (int screen = 0; screen < screens; screen++) {
141 i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
143 /* This screen already exists. We use the littlest screen so that the user
144 can always see the complete workspace */
145 s->rect.width = min(s->rect.width, screen_info[screen].width);
146 s->rect.height = min(s->rect.height, screen_info[screen].height);
148 s = calloc(sizeof(i3Screen), 1);
149 s->rect.x = screen_info[screen].x_org;
150 s->rect.y = screen_info[screen].y_org;
151 s->rect.width = screen_info[screen].width;
152 s->rect.height = screen_info[screen].height;
153 /* We always treat the screen at 0x0 as the primary screen */
154 if (s->rect.x == 0 && s->rect.y == 0)
155 TAILQ_INSERT_HEAD(screenlist, s, screens);
156 else TAILQ_INSERT_TAIL(screenlist, s, screens);
160 LOG("found Xinerama screen: %d x %d at %d x %d\n",
161 screen_info[screen].width, screen_info[screen].height,
162 screen_info[screen].x_org, screen_info[screen].y_org);
168 static void initialize_screen(xcb_connection_t *conn, i3Screen *screen, Workspace *workspace) {
169 i3Font *font = load_font(conn, config.font);
171 workspace->screen = screen;
172 screen->current_workspace = workspace->num;
174 /* Create a bar for each screen */
175 Rect bar_rect = {screen->rect.x,
176 screen->rect.height - (font->height + 6),
177 screen->rect.x + screen->rect.width,
179 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
180 uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS};
181 screen->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, mask, values);
182 screen->bargc = xcb_generate_id(conn);
183 xcb_create_gc(conn, screen->bargc, screen->bar, 0, 0);
185 /* Copy dimensions */
186 memcpy(&(workspace->rect), &(screen->rect), sizeof(Rect));
187 LOG("that is virtual screen at %d x %d with %d x %d\n",
188 screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
192 * We have just established a connection to the X server and need the initial Xinerama
193 * information to setup workspaces for each screen.
196 void initialize_xinerama(xcb_connection_t *conn) {
197 virtual_screens = scalloc(sizeof(struct screens_head));
198 TAILQ_INIT(virtual_screens);
200 if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
201 LOG("Xinerama extension not found, disabling.\n");
202 disable_xinerama(conn);
206 xcb_xinerama_is_active_reply_t *reply;
207 reply = xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL);
209 if (reply == NULL || !reply->state) {
210 LOG("Xinerama is not active (in your X-Server), disabling.\n");
212 disable_xinerama(conn);
218 query_screens(conn, virtual_screens);
222 /* Just go through each workspace and associate as many screens as we can. */
223 TAILQ_FOREACH(s, virtual_screens, screens) {
224 s->num = num_screens;
225 initialize_screen(conn, s, &(workspaces[num_screens]));
231 * This is called when the rootwindow receives a configure_notify event and therefore the
232 * number/position of the Xinerama screens could have changed.
235 void xinerama_requery_screens(xcb_connection_t *conn) {
236 /* POSSIBLE PROBLEM: Is the order of the Xinerama screens always constant? That is, can
237 it change when I move the --right-of video projector to --left-of? */
239 if (!xinerama_enabled) {
240 LOG("Xinerama is disabled\n");
244 /* We use a separate copy to diff with the previous set of screens */
245 struct screens_head *new_screens = scalloc(sizeof(struct screens_head));
246 TAILQ_INIT(new_screens);
248 query_screens(conn, new_screens);
250 i3Screen *first = TAILQ_FIRST(new_screens),
252 int screen_count = 0;
253 TAILQ_FOREACH(screen, new_screens, screens) {
254 screen->num = screen_count;
255 screen->current_workspace = -1;
256 for (int c = 0; c < 10; c++)
257 if ((workspaces[c].screen != NULL) &&
258 (workspaces[c].screen->num == screen_count)) {
259 LOG("Found a matching screen\n");
260 /* Try to use the same workspace, if it’s available */
261 if (workspaces[c].screen->current_workspace)
262 screen->current_workspace = workspaces[c].screen->current_workspace;
264 if (screen->current_workspace == -1)
265 screen->current_workspace = c;
267 /* Re-use the old bar window */
268 screen->bar = workspaces[c].screen->bar;
269 screen->bargc = workspaces[c].screen->bargc;
271 /* Update the dimensions */
272 memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect));
273 workspaces[c].screen = screen;
275 if (screen->current_workspace == -1) {
276 /* Create a new workspace for this screen, it’s new */
277 for (int c = 0; c < 10; c++)
278 if (workspaces[c].screen == NULL) {
279 LOG("fix: initializing new workspace, setting num to %d\n", c);
280 initialize_screen(conn, screen, &(workspaces[c]));
287 /* Check for workspaces which are out of bounds */
288 for (int c = 0; c < 10; c++)
289 if ((workspaces[c].screen != NULL) &&
290 (workspaces[c].screen->num >= num_screens)) {
291 LOG("Closing bar window\n");
292 xcb_destroy_window(conn, workspaces[c].screen->bar);
294 LOG("Workspace %d's screen out of bounds, assigning to first screen\n", c+1);
295 workspaces[c].screen = first;
296 memcpy(&(workspaces[c].rect), &(first->rect), sizeof(Rect));
299 /* Free the old list */
300 while (!TAILQ_EMPTY(virtual_screens)) {
301 screen = TAILQ_FIRST(virtual_screens);
302 TAILQ_REMOVE(virtual_screens, screen, screens);
305 free(virtual_screens);
307 virtual_screens = new_screens;
309 LOG("Current workspace is now: %d\n", first->current_workspace);