]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
first step of the big refactoring ("tree" branch).
[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-2010 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             num_screens++;
74         }
75
76         DLOG("found Xinerama screen: %d x %d at %d x %d\n",
77                         screen_info[screen].width, screen_info[screen].height,
78                         screen_info[screen].x_org, screen_info[screen].y_org);
79     }
80
81     free(reply);
82
83     if (num_screens == 0) {
84         ELOG("No screens found. Please fix your setup. i3 will exit now.\n");
85         exit(0);
86     }
87 }
88
89 /*
90  * We have just established a connection to the X server and need the initial Xinerama
91  * information to setup workspaces for each screen.
92  *
93  */
94 void xinerama_init() {
95     if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
96         DLOG("Xinerama extension not found, disabling.\n");
97         disable_randr(conn);
98     } else {
99         xcb_xinerama_is_active_reply_t *reply;
100         reply = xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL);
101
102         if (reply == NULL || !reply->state) {
103                 DLOG("Xinerama is not active (in your X-Server), disabling.\n");
104                 disable_randr(conn);
105         } else
106                 query_screens(conn);
107
108         FREE(reply);
109     }
110
111 #if 0
112     Output *output;
113     Workspace *ws;
114     /* Just go through each active output and associate one workspace */
115     TAILQ_FOREACH(output, &outputs, outputs) {
116         ws = get_first_workspace_for_output(output);
117         initialize_output(conn, output, ws);
118     }
119 #endif
120 }