2009-02-14 19:58:09 -05:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=8:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
|
|
|
|
*
|
2009-02-14 21:07:29 -05:00
|
|
|
|
* © 2009 Michael Stapelberg and contributors
|
2009-02-14 19:58:09 -05:00
|
|
|
|
*
|
|
|
|
|
* See file LICENSE for license information.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2009-03-03 17:51:02 -05:00
|
|
|
|
#include <stdbool.h>
|
2009-02-14 19:58:09 -05:00
|
|
|
|
|
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
|
#include <xcb/xinerama.h>
|
|
|
|
|
|
|
|
|
|
#include "queue.h"
|
|
|
|
|
#include "i3.h"
|
|
|
|
|
#include "data.h"
|
|
|
|
|
#include "table.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "xinerama.h"
|
2009-03-03 17:51:02 -05:00
|
|
|
|
#include "layout.h"
|
2009-03-04 06:09:43 -05:00
|
|
|
|
#include "xcb.h"
|
|
|
|
|
#include "config.h"
|
2009-02-14 19:58:09 -05:00
|
|
|
|
|
2009-02-14 20:12:14 -05:00
|
|
|
|
/* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
|
2009-02-14 19:58:09 -05:00
|
|
|
|
* (xrandr --same-as) */
|
2009-03-03 17:51:02 -05:00
|
|
|
|
struct screens_head *virtual_screens;
|
|
|
|
|
|
|
|
|
|
static bool xinerama_enabled = true;
|
2009-02-14 19:58:09 -05:00
|
|
|
|
|
2009-02-14 20:12:14 -05:00
|
|
|
|
/*
|
|
|
|
|
* Looks in virtual_screens for the i3Screen whose start coordinates are x, y
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-03 17:51:02 -05:00
|
|
|
|
i3Screen *get_screen_at(int x, int y, struct screens_head *screenlist) {
|
2009-02-14 19:58:09 -05:00
|
|
|
|
i3Screen *screen;
|
2009-03-03 17:51:02 -05:00
|
|
|
|
TAILQ_FOREACH(screen, screenlist, screens)
|
2009-02-14 19:58:09 -05:00
|
|
|
|
if (screen->rect.x == x && screen->rect.y == y)
|
|
|
|
|
return screen;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-14 20:12:14 -05:00
|
|
|
|
/*
|
|
|
|
|
* Looks in virtual_screens for the i3Screen which contains coordinates x, y
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-02-14 19:58:09 -05:00
|
|
|
|
i3Screen *get_screen_containing(int x, int y) {
|
|
|
|
|
i3Screen *screen;
|
2009-03-03 17:51:02 -05:00
|
|
|
|
TAILQ_FOREACH(screen, virtual_screens, screens) {
|
|
|
|
|
printf("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
|
|
|
|
|
x, y, screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
|
|
|
|
|
if (x >= screen->rect.x && x < (screen->rect.x + screen->rect.width) &&
|
|
|
|
|
y >= screen->rect.y && y < (screen->rect.y + screen->rect.height))
|
2009-02-14 19:58:09 -05:00
|
|
|
|
return screen;
|
2009-03-03 17:51:02 -05:00
|
|
|
|
}
|
2009-02-14 19:58:09 -05:00
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-14 20:12:14 -05:00
|
|
|
|
/*
|
|
|
|
|
* Fills virtual_screens with exactly one screen with width/height of the whole X server.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 09:45:12 -05:00
|
|
|
|
static void disable_xinerama(xcb_connection_t *conn) {
|
|
|
|
|
xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
|
2009-02-14 20:12:14 -05:00
|
|
|
|
|
|
|
|
|
i3Screen *s = calloc(sizeof(i3Screen), 1);
|
|
|
|
|
|
|
|
|
|
s->rect.x = 0;
|
|
|
|
|
s->rect.y = 0;
|
|
|
|
|
s->rect.width = root_screen->width_in_pixels;
|
|
|
|
|
s->rect.height = root_screen->height_in_pixels;
|
|
|
|
|
|
2009-03-03 17:51:02 -05:00
|
|
|
|
TAILQ_INSERT_TAIL(virtual_screens, s, screens);
|
|
|
|
|
|
|
|
|
|
xinerama_enabled = false;
|
2009-02-14 20:12:14 -05:00
|
|
|
|
}
|
2009-02-14 19:58:09 -05:00
|
|
|
|
|
|
|
|
|
/*
|
2009-03-03 17:51:02 -05:00
|
|
|
|
* Gets the Xinerama screens and converts them to virtual i3Screens (only one screen for two
|
|
|
|
|
* Xinerama screen which are configured in clone mode) in the given screenlist
|
2009-02-14 19:58:09 -05:00
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 09:45:12 -05:00
|
|
|
|
static void query_screens(xcb_connection_t *conn, struct screens_head *screenlist) {
|
2009-02-14 19:58:09 -05:00
|
|
|
|
xcb_xinerama_query_screens_reply_t *reply;
|
|
|
|
|
xcb_xinerama_screen_info_t *screen_info;
|
|
|
|
|
|
2009-03-04 09:45:12 -05:00
|
|
|
|
reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
|
2009-02-14 19:58:09 -05:00
|
|
|
|
if (!reply) {
|
2009-03-03 17:51:02 -05:00
|
|
|
|
printf("Couldn't get Xinerama screens\n");
|
2009-02-14 19:58:09 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
screen_info = xcb_xinerama_query_screens_screen_info(reply);
|
2009-03-03 17:51:02 -05:00
|
|
|
|
int screens = xcb_xinerama_query_screens_screen_info_length(reply);
|
|
|
|
|
num_screens = 0;
|
2009-02-14 19:58:09 -05:00
|
|
|
|
|
2009-03-03 17:51:02 -05:00
|
|
|
|
for (int screen = 0; screen < screens; screen++) {
|
|
|
|
|
i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org, screenlist);
|
2009-02-14 19:58:09 -05:00
|
|
|
|
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 {
|
2009-02-14 20:12:14 -05:00
|
|
|
|
s = calloc(sizeof(i3Screen), 1);
|
2009-02-14 19:58:09 -05:00
|
|
|
|
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;
|
2009-03-03 17:51:02 -05:00
|
|
|
|
/* 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++;
|
2009-02-14 19:58:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("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);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 17:51:02 -05:00
|
|
|
|
free(reply);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 09:45:12 -05:00
|
|
|
|
static void initialize_screen(xcb_connection_t *conn, i3Screen *screen, Workspace *workspace) {
|
|
|
|
|
i3Font *font = load_font(conn, config.font);
|
2009-03-04 06:09:43 -05:00
|
|
|
|
|
|
|
|
|
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};
|
2009-03-04 09:45:12 -05:00
|
|
|
|
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);
|
2009-03-04 06:09:43 -05:00
|
|
|
|
|
|
|
|
|
/* Copy dimensions */
|
|
|
|
|
memcpy(&(workspace->rect), &(screen->rect), sizeof(Rect));
|
|
|
|
|
printf("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);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 17:51:02 -05:00
|
|
|
|
/*
|
|
|
|
|
* We have just established a connection to the X server and need the initial Xinerama
|
|
|
|
|
* information to setup workspaces for each screen.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 09:45:12 -05:00
|
|
|
|
void initialize_xinerama(xcb_connection_t *conn) {
|
2009-03-03 17:51:02 -05:00
|
|
|
|
virtual_screens = scalloc(sizeof(struct screens_head));
|
|
|
|
|
TAILQ_INIT(virtual_screens);
|
|
|
|
|
|
2009-03-04 09:45:12 -05:00
|
|
|
|
if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
|
2009-03-03 17:51:02 -05:00
|
|
|
|
printf("Xinerama extension not found, disabling.\n");
|
2009-03-04 09:45:12 -05:00
|
|
|
|
disable_xinerama(conn);
|
2009-03-03 17:51:02 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 09:45:12 -05:00
|
|
|
|
if (!xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL)->state) {
|
2009-03-03 17:51:02 -05:00
|
|
|
|
printf("Xinerama is not active (in your X-Server), disabling.\n");
|
2009-03-04 09:45:12 -05:00
|
|
|
|
disable_xinerama(conn);
|
2009-03-03 17:51:02 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-04 09:45:12 -05:00
|
|
|
|
query_screens(conn, virtual_screens);
|
2009-03-03 17:51:02 -05:00
|
|
|
|
|
2009-02-14 19:58:09 -05:00
|
|
|
|
i3Screen *s;
|
|
|
|
|
num_screens = 0;
|
2009-03-03 17:51:02 -05:00
|
|
|
|
/* Just go through each workspace and associate as many screens as we can. */
|
|
|
|
|
TAILQ_FOREACH(s, virtual_screens, screens) {
|
2009-02-14 19:58:09 -05:00
|
|
|
|
s->num = num_screens;
|
2009-03-04 09:45:12 -05:00
|
|
|
|
initialize_screen(conn, s, &(workspaces[num_screens]));
|
2009-03-04 06:09:43 -05:00
|
|
|
|
num_screens++;
|
2009-02-14 19:58:09 -05:00
|
|
|
|
}
|
2009-03-03 17:51:02 -05:00
|
|
|
|
}
|
2009-02-14 19:58:09 -05:00
|
|
|
|
|
2009-03-03 17:51:02 -05:00
|
|
|
|
/*
|
|
|
|
|
* This is called when the rootwindow receives a configure_notify event and therefore the
|
|
|
|
|
* number/position of the Xinerama screens could have changed.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-03-04 09:45:12 -05:00
|
|
|
|
void xinerama_requery_screens(xcb_connection_t *conn) {
|
2009-03-03 17:51:02 -05:00
|
|
|
|
/* 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? */
|
|
|
|
|
|
|
|
|
|
if (!xinerama_enabled) {
|
|
|
|
|
printf("Xinerama is disabled\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We use a separate copy to diff with the previous set of screens */
|
|
|
|
|
struct screens_head *new_screens = scalloc(sizeof(struct screens_head));
|
|
|
|
|
TAILQ_INIT(new_screens);
|
|
|
|
|
|
2009-03-04 09:45:12 -05:00
|
|
|
|
query_screens(conn, new_screens);
|
2009-03-03 17:51:02 -05:00
|
|
|
|
|
|
|
|
|
i3Screen *first = TAILQ_FIRST(new_screens),
|
|
|
|
|
*screen;
|
|
|
|
|
int screen_count = 0;
|
|
|
|
|
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)) {
|
|
|
|
|
printf("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;
|
|
|
|
|
|
2009-03-04 06:09:43 -05:00
|
|
|
|
/* Re-use the old bar window */
|
|
|
|
|
screen->bar = workspaces[c].screen->bar;
|
|
|
|
|
screen->bargc = workspaces[c].screen->bargc;
|
|
|
|
|
|
2009-03-03 17:51:02 -05:00
|
|
|
|
/* Update the dimensions */
|
|
|
|
|
memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect));
|
|
|
|
|
workspaces[c].screen = screen;
|
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
printf("fix: initializing new workspace, setting num to %d\n", c);
|
2009-03-04 09:45:12 -05:00
|
|
|
|
initialize_screen(conn, screen, &(workspaces[c]));
|
2009-03-03 17:51:02 -05:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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)) {
|
2009-03-04 06:09:43 -05:00
|
|
|
|
printf("Closing bar window\n");
|
2009-03-04 09:45:12 -05:00
|
|
|
|
xcb_destroy_window(conn, workspaces[c].screen->bar);
|
2009-03-04 06:09:43 -05:00
|
|
|
|
|
2009-03-03 17:51:02 -05:00
|
|
|
|
printf("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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free the old list */
|
|
|
|
|
TAILQ_FOREACH(screen, virtual_screens, screens) {
|
|
|
|
|
TAILQ_REMOVE(virtual_screens, screen, screens);
|
|
|
|
|
free(screen);
|
|
|
|
|
}
|
|
|
|
|
free(virtual_screens);
|
|
|
|
|
|
|
|
|
|
virtual_screens = new_screens;
|
|
|
|
|
|
|
|
|
|
printf("Current workspace is now: %d\n", first->current_workspace);
|
|
|
|
|
|
2009-03-04 09:45:12 -05:00
|
|
|
|
render_layout(conn);
|
2009-02-14 19:58:09 -05:00
|
|
|
|
}
|