]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
4c8ffa88a48552cffd321cbe8d8847b0726f554b
[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
82         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
83                 printf("Xinerama extension not found, disabling.\n");
84                 disable_xinerama(conn);
85                 return;
86         }
87
88         if (!xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL)->state) {
89                 printf("Xinerama is not active (in your X-Server), disabling.\n");
90                 disable_xinerama(conn);
91                 return;
92         }
93
94         reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
95         if (!reply) {
96                 printf("Couldn’t get active Xinerama screens\n");
97                 return;
98         }
99         screen_info = xcb_xinerama_query_screens_screen_info(reply);
100         num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
101
102         /* Just go through each workspace and associate as many screens as we can. */
103         for (int screen = 0; screen < num_screens; screen++) {
104                 i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org);
105                 if (s!= NULL) {
106                         /* This screen already exists. We use the littlest screen so that the user
107                            can always see the complete workspace */
108                         s->rect.width = min(s->rect.width, screen_info[screen].width);
109                         s->rect.height = min(s->rect.height, screen_info[screen].height);
110                 } else {
111                         s = calloc(sizeof(i3Screen), 1);
112                         s->rect.x = screen_info[screen].x_org;
113                         s->rect.y = screen_info[screen].y_org;
114                         s->rect.width = screen_info[screen].width;
115                         s->rect.height = screen_info[screen].height;
116                         TAILQ_INSERT_TAIL(&virtual_screens, s, screens);
117                 }
118
119                 printf("found Xinerama screen: %d x %d at %d x %d\n",
120                                 screen_info[screen].width, screen_info[screen].height,
121                                 screen_info[screen].x_org, screen_info[screen].y_org);
122         }
123
124         i3Screen *s;
125         num_screens = 0;
126         TAILQ_FOREACH(s, &virtual_screens, screens) {
127                 s->num = num_screens;
128                 s->current_workspace = num_screens;
129                 workspaces[num_screens].screen = s;
130                 memcpy(&(workspaces[num_screens++].rect), &(s->rect), sizeof(Rect));
131                 printf("that is virtual screen at %d x %d with %d x %d\n",
132                                 s->rect.x, s->rect.y, s->rect.width, s->rect.height);
133         }
134
135         free(screen_info);
136 }