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