]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
Bugfix: Correctly disable Xinerama, use TAILQ_HEAD_INITIALIZER where possible
[i3/i3] / src / xinerama.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * (c) 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <xcb/xcb.h>
16 #include <xcb/xinerama.h>
17
18 #include "queue.h"
19 #include "i3.h"
20 #include "data.h"
21 #include "table.h"
22 #include "util.h"
23 #include "xinerama.h"
24
25 /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens
26  * (xrandr --same-as) */
27 struct screens_head virtual_screens = TAILQ_HEAD_INITIALIZER(virtual_screens);
28
29 /*
30  * Looks in virtual_screens for the i3Screen whose start coordinates are x, y
31  *
32  */
33 i3Screen *get_screen_at(int x, int y) {
34         i3Screen *screen;
35         TAILQ_FOREACH(screen, &virtual_screens, screens)
36                 if (screen->rect.x == x && screen->rect.y == y)
37                         return screen;
38
39         return NULL;
40 }
41
42 /*
43  * Looks in virtual_screens for the i3Screen which contains coordinates x, y
44  *
45  */
46 i3Screen *get_screen_containing(int x, int y) {
47         i3Screen *screen;
48         TAILQ_FOREACH(screen, &virtual_screens, screens)
49                 if (x > screen->rect.x && x < (screen->rect.x + screen->rect.width) &&
50                     y > screen->rect.y && y < (screen->rect.y + screen->rect.height))
51                         return screen;
52
53         return NULL;
54 }
55
56 /*
57  * Fills virtual_screens with exactly one screen with width/height of the whole X server.
58  *
59  */
60 static void disable_xinerama(xcb_connection_t *connection) {
61         xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
62
63         i3Screen *s = calloc(sizeof(i3Screen), 1);
64
65         s->rect.x = 0;
66         s->rect.y = 0;
67         s->rect.width = root_screen->width_in_pixels;
68         s->rect.height = root_screen->height_in_pixels;
69
70         TAILQ_INSERT_TAIL(&virtual_screens, s, screens);
71 }
72
73 /*
74  * We have just established a connection to the X server and need the initial Xinerama
75  * information to setup workspaces for each screen.
76  *
77  */
78 void initialize_xinerama(xcb_connection_t *conn) {
79         xcb_xinerama_query_screens_reply_t *reply;
80         xcb_xinerama_screen_info_t *screen_info;
81         int screen;
82
83         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
84                 printf("Xinerama extension not found, disabling.\n");
85                 disable_xinerama(conn);
86                 return;
87         }
88
89         if (!xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL)->state) {
90                 printf("Xinerama is not active (in your X-Server), disabling.\n");
91                 disable_xinerama(conn);
92                 return;
93         }
94
95         reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
96         if (!reply) {
97                 printf("Couldn’t get active Xinerama screens\n");
98                 return;
99         }
100         screen_info = xcb_xinerama_query_screens_screen_info(reply);
101         num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
102
103         /* Just go through each workspace and associate as many screens as we can. */
104         for (screen = 0; screen < num_screens; screen++) {
105                 i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org);
106                 if (s!= NULL) {
107                         /* This screen already exists. We use the littlest screen so that the user
108                            can always see the complete workspace */
109                         s->rect.width = min(s->rect.width, screen_info[screen].width);
110                         s->rect.height = min(s->rect.height, screen_info[screen].height);
111                 } else {
112                         s = calloc(sizeof(i3Screen), 1);
113                         s->rect.x = screen_info[screen].x_org;
114                         s->rect.y = screen_info[screen].y_org;
115                         s->rect.width = screen_info[screen].width;
116                         s->rect.height = screen_info[screen].height;
117                         TAILQ_INSERT_TAIL(&virtual_screens, s, screens);
118                 }
119
120                 printf("found Xinerama screen: %d x %d at %d x %d\n",
121                                 screen_info[screen].width, screen_info[screen].height,
122                                 screen_info[screen].x_org, screen_info[screen].y_org);
123         }
124
125         i3Screen *s;
126         num_screens = 0;
127         TAILQ_FOREACH(s, &virtual_screens, screens) {
128                 s->num = num_screens;
129                 s->current_workspace = num_screens;
130                 workspaces[num_screens].screen = s;
131                 memcpy(&(workspaces[num_screens++].rect), &(s->rect), sizeof(Rect));
132                 printf("that is virtual screen at %d x %d with %d x %d\n",
133                                 s->rect.x, s->rect.y, s->rect.width, s->rect.height);
134         }
135
136         free(screen_info);
137 }