]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
9e412e0330db13a788068c1bcb3c34534047fb40
[i3/i3] / src / xinerama.c
1 #undef I3__FILE__
2 #define I3__FILE__ "xinerama.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * This is LEGACY code (we support RandR, which can do much more than
10  * Xinerama), but necessary for the poor users of the nVidia binary
11  * driver which does not support RandR in 2011 *sigh*.
12  *
13  */
14 #include "all.h"
15
16 #include <xcb/xinerama.h>
17
18 static int num_screens;
19
20 /*
21  * Looks in outputs for the Output whose start coordinates are x, y
22  *
23  */
24 static Output *get_screen_at(unsigned int x, unsigned int y) {
25     Output *output;
26     TAILQ_FOREACH(output, &outputs, outputs)
27     if (output->rect.x == x && output->rect.y == y)
28         return output;
29
30     return NULL;
31 }
32
33 /*
34  * Gets the Xinerama screens and converts them to virtual Outputs (only one screen for two
35  * Xinerama screen which are configured in clone mode) in the given screenlist
36  *
37  */
38 static void query_screens(xcb_connection_t *conn) {
39     xcb_xinerama_query_screens_reply_t *reply;
40     xcb_xinerama_screen_info_t *screen_info;
41
42     reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
43     if (!reply) {
44         ELOG("Couldn't get Xinerama screens\n");
45         return;
46     }
47     screen_info = xcb_xinerama_query_screens_screen_info(reply);
48     int screens = xcb_xinerama_query_screens_screen_info_length(reply);
49
50     for (int screen = 0; screen < screens; screen++) {
51         Output *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org);
52         if (s != NULL) {
53             DLOG("Re-used old Xinerama screen %p\n", s);
54             /* This screen already exists. We use the littlest screen so that the user
55                can always see the complete workspace */
56             s->rect.width = min(s->rect.width, screen_info[screen].width);
57             s->rect.height = min(s->rect.height, screen_info[screen].height);
58         } else {
59             s = scalloc(sizeof(Output));
60             sasprintf(&(s->name), "xinerama-%d", num_screens);
61             DLOG("Created new Xinerama screen %s (%p)\n", s->name, s);
62             s->active = true;
63             s->rect.x = screen_info[screen].x_org;
64             s->rect.y = screen_info[screen].y_org;
65             s->rect.width = screen_info[screen].width;
66             s->rect.height = screen_info[screen].height;
67             /* We always treat the screen at 0x0 as the primary screen */
68             if (s->rect.x == 0 && s->rect.y == 0)
69                 TAILQ_INSERT_HEAD(&outputs, s, outputs);
70             else
71                 TAILQ_INSERT_TAIL(&outputs, s, outputs);
72             output_init_con(s);
73             init_ws_for_output(s, output_get_content(s->con));
74             num_screens++;
75         }
76
77         DLOG("found Xinerama screen: %d x %d at %d x %d\n",
78              screen_info[screen].width, screen_info[screen].height,
79              screen_info[screen].x_org, screen_info[screen].y_org);
80     }
81
82     free(reply);
83
84     if (num_screens == 0) {
85         ELOG("No screens found. Please fix your setup. i3 will exit now.\n");
86         exit(0);
87     }
88 }
89
90 /*
91  * We have just established a connection to the X server and need the initial Xinerama
92  * information to setup workspaces for each screen.
93  *
94  */
95 void xinerama_init(void) {
96     if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
97         DLOG("Xinerama extension not found, disabling.\n");
98         disable_randr(conn);
99     } else {
100         xcb_xinerama_is_active_reply_t *reply;
101         reply = xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL);
102
103         if (reply == NULL || !reply->state) {
104             DLOG("Xinerama is not active (in your X-Server), disabling.\n");
105             disable_randr(conn);
106         } else
107             query_screens(conn);
108
109         FREE(reply);
110     }
111
112 #if 0
113     Output *output;
114     Workspace *ws;
115     /* Just go through each active output and associate one workspace */
116     TAILQ_FOREACH(output, &outputs, outputs) {
117         ws = get_first_workspace_for_output(output);
118         initialize_output(conn, output, ws);
119     }
120 #endif
121 }