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