]> git.sur5r.net Git - i3/i3/blob - src/xinerama.c
Implement Xinerama (workspaces have a specific screen)
[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 Rects stores the virtual screens, used for handling overlapping screens
26  * (xrandr --same-as) */
27 struct screens_head virtual_screens;
28
29 i3Screen *get_screen_at(int x, int y) {
30         i3Screen *screen;
31         TAILQ_FOREACH(screen, &virtual_screens, screens)
32                 if (screen->rect.x == x && screen->rect.y == y)
33                         return screen;
34
35         return NULL;
36 }
37
38 i3Screen *get_screen_containing(int x, int y) {
39         i3Screen *screen;
40         TAILQ_FOREACH(screen, &virtual_screens, screens)
41                 if (x > screen->rect.x && x < (screen->rect.x + screen->rect.width) &&
42                     y > screen->rect.y && y < (screen->rect.y + screen->rect.height))
43                         return screen;
44
45         return NULL;
46 }
47
48
49 /*
50  * We have just established a connection to the X server and need the initial Xinerama
51  * information to setup workspaces for each screen.
52  *
53  */
54 void initialize_xinerama(xcb_connection_t *conn) {
55         xcb_xinerama_query_screens_reply_t *reply;
56         xcb_xinerama_screen_info_t *screen_info;
57         int screen;
58
59         if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
60                 printf("Xinerama extension not found, disabling.\n");
61                 return;
62         }
63
64         if (!xcb_xinerama_is_active_reply(conn, xcb_xinerama_is_active(conn), NULL)->state) {
65                 printf("Xinerama is not active (in your X-Server), disabling.\n");
66                 return;
67         }
68
69         reply = xcb_xinerama_query_screens_reply(conn, xcb_xinerama_query_screens_unchecked(conn), NULL);
70         if (!reply) {
71                 printf("Couldn’t get active Xinerama screens\n");
72                 return;
73         }
74         screen_info = xcb_xinerama_query_screens_screen_info(reply);
75         num_screens = xcb_xinerama_query_screens_screen_info_length(reply);
76
77         TAILQ_INIT(&virtual_screens);
78
79         /* Just go through each workspace and associate as many screens as we can. */
80         for (screen = 0; screen < num_screens; screen++) {
81                 i3Screen *s = get_screen_at(screen_info[screen].x_org, screen_info[screen].y_org);
82                 if (s!= NULL) {
83                         /* This screen already exists. We use the littlest screen so that the user
84                            can always see the complete workspace */
85                         s->rect.width = min(s->rect.width, screen_info[screen].width);
86                         s->rect.height = min(s->rect.height, screen_info[screen].height);
87                 } else {
88                         s = calloc(sizeof(Screen), 1);
89                         s->rect.x = screen_info[screen].x_org;
90                         s->rect.y = screen_info[screen].y_org;
91                         s->rect.width = screen_info[screen].width;
92                         s->rect.height = screen_info[screen].height;
93                         TAILQ_INSERT_TAIL(&virtual_screens, s, screens);
94                 }
95
96                 printf("found Xinerama screen: %d x %d at %d x %d\n",
97                                 screen_info[screen].width, screen_info[screen].height,
98                                 screen_info[screen].x_org, screen_info[screen].y_org);
99         }
100
101         i3Screen *s;
102         num_screens = 0;
103         TAILQ_FOREACH(s, &virtual_screens, screens) {
104                 s->num = num_screens;
105                 s->current_workspace = num_screens;
106                 workspaces[num_screens].screen = s;
107                 memcpy(&(workspaces[num_screens++].rect), &(s->rect), sizeof(Rect));
108                 printf("that is virtual screen at %d x %d with %d x %d\n",
109                                 s->rect.x, s->rect.y, s->rect.width, s->rect.height);
110         }
111
112         free(screen_info);
113 }