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