move more display code into the display file/struct
[dana/dcompmgr.git] / screen.c
1 #include "screen.h"
2 #include "display.h"
3 #include "window.h"
4 #include "gettext.h"
5 #include <string.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <xcb/composite.h>
9
10 #define ROOT_MASK      (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
11                         XCB_EVENT_MASK_STRUCTURE_NOTIFY)
12
13 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
14                         XCB_EVENT_MASK_PROPERTY_CHANGE)
15
16 static void screen_init(d_screen_t *sc);
17 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
18
19 gboolean
20 screen_register(struct d_display *dpy, int num, d_screen_t *sc)
21 {
22     char *name;
23     xcb_window_t w;
24     xcb_intern_atom_cookie_t ack;
25     xcb_intern_atom_reply_t *arep;
26     xcb_get_selection_owner_cookie_t sck;
27     xcb_get_selection_owner_reply_t *srep;
28     uint32_t event_mask;
29     gboolean taken, ret;
30
31     sc->dpy = dpy;
32     sc->num = num;
33     w = xcb_generate_id(sc->dpy->conn);
34     event_mask = SELECTION_MASK;
35     xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
36                       0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
37                       sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
38
39     name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
40     ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
41     arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
42     g_free(name);
43
44     sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
45     srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
46     taken = !!srep->owner;
47     free(srep);
48     if (taken) {
49         printf(_("screen %d already has a composite manager, skipping\n"),
50                sc->num);
51         ret = FALSE;
52     }
53     else {
54         xcb_timestamp_t time;
55
56         sc->selwin = w;
57         sc->selatom = arep->atom;
58
59         time = screen_timestamp(sc);
60
61         xcb_grab_server(sc->dpy->conn);
62         xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
63         sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
64         srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
65         taken = srep->owner == w;
66         if (taken) {
67             screen_init(sc);
68             ret = TRUE;
69         }
70         else {
71             xcb_destroy_window(sc->dpy->conn, w);
72             ret = FALSE;
73         }
74         xcb_ungrab_server(sc->dpy->conn);
75         xcb_flush(sc->dpy->conn);
76     }
77     return ret;
78 }
79
80 static guint
81 window_hash(xcb_window_t *w) { return *w; }
82
83 static gboolean
84 window_compare(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
85
86 static void
87 screen_init(d_screen_t *sc)
88 {
89     uint32_t mask;
90
91     sc->winhash = g_hash_table_new((GHashFunc)window_hash,
92                                    (GCompareFunc)window_compare);
93
94 //    xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
95 //                                      XCB_COMPOSITE_REDIRECT_MANUAL);
96
97     mask = SELECTION_MASK;
98     xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
99                                  XCB_CW_EVENT_MASK, &mask);
100     mask = ROOT_MASK;
101     xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
102                                  XCB_CW_EVENT_MASK, &mask);
103 }
104
105 void
106 screen_add_window(d_screen_t *sc, xcb_window_t wid)
107 {
108     d_window_t *w;
109
110     /* XXX xgetwindowattributes - size and tings */
111
112     w = window_new(wid, sc);
113     g_hash_table_insert(sc->winhash, &w->id, w);
114
115     printf("screen added window 0x%x\n", w->id);
116 }
117
118 void
119 screen_remove_window(d_screen_t *sc, struct d_window *w)
120 {
121     printf("screen removed window 0x%x\n", w->id);
122
123     g_hash_table_remove(sc->winhash, &w->id);
124     window_unref(w);
125 }
126
127 d_window_t*
128 screen_find_window(d_screen_t *sc, xcb_window_t id)
129 {
130     return g_hash_table_lookup(sc->winhash, &id);
131 }
132
133 static xcb_timestamp_t
134 screen_timestamp(d_screen_t *sc)
135 {
136     xcb_void_cookie_t ck;
137     xcb_timestamp_t   time;
138
139     ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
140                              sc->selatom, sc->selatom, 32, 0, NULL);
141     xcb_flush(sc->dpy->conn);
142     while (1) {
143         xcb_generic_event_t *ev;
144
145         ev = xcb_wait_for_event(sc->dpy->conn);
146         if (!ev) {
147             printf(_("IO error\n"));
148             exit(0);
149         }
150
151         /* expect errors.. */
152         if (!ev->response_type) {
153             display_error(sc->dpy, (xcb_generic_error_t*)ev);
154             free(ev);
155             continue;
156         }
157
158         if (ev->response_type == XCB_PROPERTY_NOTIFY &&
159             ev->full_sequence == ck.sequence)
160         {
161             time = ((xcb_property_notify_event_t*)ev)->time;
162             free(ev);
163             break;
164         }
165     }
166     printf("created timestamp %lu\n", (unsigned long) time);
167     return time;
168 }