]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
Fix unaligned memory access on sparc (Thanks David Coppa)
[i3/i3] / src / xinerama.c
1 /*
2  * vim:ts=8: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 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <xcb/xcb.h>
20 #include <xcb/xinerama.h>
21
22 #include "queue.h"
23 #include "data.h"
24 #include "util.h"
25 #include "xinerama.h"
26 #include "workspace.h"
27 #include "log.h"
28 #include "randr.h"
29
30 static int num_screens;
31
32 /*
33  * Looks in outputs for the Output whose start coordinates are x, y
34  *
35  */
36 static Output *get_screen_at(int x, int y) {
37         Output *output;
38         TAILQ_FOREACH(output, &outputs, outputs)
39                 if (output->rect.x == x && output->rect.y == y)
40                         return output;
41
42         return NULL;
43 }
44
45 /*
46  * Gets the Xinerama screens and converts them to virtual Outputs (only one screen for two
47  * Xinerama screen which are configured in clone mode) in the given screenlist
48  *
49  */
50 static void query_screens(xcb_connection_t *conn) {
51         xcb_xinerama_query_screens_reply_t *reply;
52         xcb_xinerama_screen_info_t *screen_info;
53
54         reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
55         if (!reply) {
56                 ELOG("Couldn't get Xinerama screens\n");
57                 return;
58         }
59         screen_info = xcb_xinerama_query_screens_screen_info(reply);
60         int screens = xcb_xinerama_query_screens_screen_info_length(reply);
61
62         for (int screen = 0; screen < screens; screen++) {
63                 Output *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org);
64                 if (s != NULL) {
65                         DLOG("Re-used old Xinerama screen %p\n", s);
66                         /* This screen already exists. We use the littlest screen so that the user
67                            can always see the complete workspace */
68                         s->rect.width = min(s->rect.width, screen_info[screen].width);
69                         s->rect.height = min(s->rect.height, screen_info[screen].height);
70                 } else {
71                         s = scalloc(sizeof(Output));
72                         asprintf(&(s->name), "xinerama-%d", num_screens);
73                         DLOG("Created new Xinerama screen %s (%p)\n", s->name, s);
74                         s->active = true;
75                         s->rect.x = screen_info[screen].x_org;
76                         s->rect.y = screen_info[screen].y_org;
77                         s->rect.width = screen_info[screen].width;
78                         s->rect.height = screen_info[screen].height;
79                         /* We always treat the screen at 0x0 as the primary screen */
80                         if (s->rect.x == 0 && s->rect.y == 0)
81                                 TAILQ_INSERT_HEAD(&outputs, s, outputs);
82                         else TAILQ_INSERT_TAIL(&outputs, s, outputs);
83                         num_screens++;
84                 }
85
86                 DLOG("found Xinerama screen: %d x %d at %d x %d\n",
87                                 screen_info[screen].width, screen_info[screen].height,
88                                 screen_info[screen].x_org, screen_info[screen].y_org);
89         }
90
91         free(reply);
92
93         if (num_screens == 0) {
94                 ELOG("No screens found. Please fix your setup. i3 will exit now.\n");
95                 exit(0);
96         }
97 }
98
99 /*
100  * We have just established a connection to the X server and need the initial Xinerama
101  * information to setup workspaces for each screen.
102  *
103  */
104 void initialize_xinerama(xcb_connection_t *conn) {
105         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
106                 DLOG("Xinerama extension not found, disabling.\n");
107                 disable_randr(conn);
108         } else {
109                 xcb_xinerama_is_active_reply_t *reply;
110                 reply = xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL);
111
112                 if (reply == NULL || !reply->state) {
113                         DLOG("Xinerama is not active (in your X-Server), disabling.\n");
114                         disable_randr(conn);
115                 } else
116                         query_screens(conn);
117
118                 FREE(reply);
119         }
120
121         Output *output;
122         Workspace *ws;
123         /* Just go through each active output and associate one workspace */
124         TAILQ_FOREACH(output, &outputs, outputs) {
125                 ws = get_first_workspace_for_output(output);
126                 initialize_output(conn, output, ws);
127         }
128 }