Remove xcl and CVSROOT.
[free-sw/xcb/demo] / main.c
1 /*
2  * Copyright (C) 2001-2002 Bart Massey and Jamey Sharp.
3  * All Rights Reserved.  See the file COPYING in this directory
4  * for licensing information.
5  */
6
7 #define TEST_GET_WINDOW_ATTRIBUTES
8 #define TEST_GET_GEOMETRY
9 #define TEST_QUERY_TREE
10 #undef TEST_THREADS
11 #define VERBOSE
12 #undef SUPERVERBOSE
13 #define TEST_ICCCM
14
15 #ifdef TEST_THREADS
16 #include <pthread.h>
17 #endif
18
19 #ifdef TEST_ICCCM
20 #include <string.h>
21 #endif
22
23 #include <stdlib.h>
24
25 #include <X11/XCB/xcb.h>
26 #include <X11/XCB/xcb_aux.h>
27 #include "reply_formats.h"
28
29 #ifdef VERBOSE
30 #include <stdio.h>
31 #endif
32
33 void try_events(XCBConnection *c);
34 void wait_events(XCBConnection *c);
35
36 static XCBConnection *c;
37 static XCBWINDOW window;
38
39 int main(int argc, char **argv)
40 {
41     CARD32 mask = 0;
42     CARD32 values[6];
43     XCBDRAWABLE d;
44     XCBSCREEN *root;
45 #ifdef TEST_GET_GEOMETRY
46     XCBGetGeometryCookie geom[3];
47     XCBGetGeometryRep *geomrep[3];
48 #endif
49 #ifdef TEST_QUERY_TREE
50     XCBQueryTreeCookie tree[3];
51     XCBQueryTreeRep *treerep[3];
52 #endif
53 #ifdef TEST_GET_WINDOW_ATTRIBUTES
54     XCBGetWindowAttributesCookie attr[1];
55     XCBGetWindowAttributesRep *attrrep[1];
56 #endif
57 #ifdef TEST_ICCCM
58     XCBInternAtomCookie atom[2];
59     XCBInternAtomRep *atomrep[2];
60 #endif
61 #ifdef TEST_THREADS
62     pthread_t event_thread;
63 #endif
64     int screen_num;
65
66     c = XCBConnect(0, &screen_num);
67     root = XCBAuxGetScreen(c, screen_num);
68
69 #ifdef TEST_THREADS
70 # ifdef VERBOSE
71     printf("main() thread ID: %ld\n", pthread_self());
72 # endif
73     /* don't do this cast. */
74     pthread_create(&event_thread, 0, (void *(*)(void *))wait_events, c);
75 #endif
76
77 #if 1
78     window = XCBWINDOWNew(c);
79 #else
80     window = 0; /* should be an invalid ID */
81 #endif
82
83     mask |= XCBCWBackPixel;
84     values[0] = root->white_pixel;
85
86     mask |= XCBCWBorderPixel;
87     values[1] = root->black_pixel;
88
89     mask |= XCBCWBackingStore;
90     values[2] = Always;
91
92     mask |= XCBCWOverrideRedirect;
93     values[3] = 0;
94
95     mask |= XCBCWEventMask;
96     values[4] = ButtonReleaseMask | ExposureMask | StructureNotifyMask
97         | EnterWindowMask | LeaveWindowMask;
98
99     mask |= XCBCWDontPropagate;
100     values[5] = ButtonPressMask;
101
102     XCBCreateWindow(c, /* depth */ 0,
103         window, root->root,
104         /* x */ 20, /* y */ 200, /* width */ 150, /* height */ 150,
105         /* border_width */ 10, /* class */ InputOutput,
106         /* visual */ root->root_visual, mask, values);
107 #ifdef TEST_ICCCM
108     atom[0] = XCBInternAtom(c, 0, sizeof("WM_PROTOCOLS")-1, "WM_PROTOCOLS");
109     atom[1] = XCBInternAtom(c, 0, sizeof("WM_DELETE_WINDOW")-1, "WM_DELETE_WINDOW");
110     atomrep[1] = XCBInternAtomReply(c, atom[1], 0);
111     atomrep[0] = XCBInternAtomReply(c, atom[0], 0);
112     {
113         XCBATOM XA_WM_NAME = { 39 };
114         XCBATOM XA_STRING = { 31 };
115         XCBChangeProperty(c, PropModeReplace, window, XA_WM_NAME, XA_STRING, 8, strlen(argv[0]), argv[0]);
116     }
117     if(atomrep[0] && atomrep[1])
118     {
119         XCBATOM WM_PROTOCOLS = atomrep[0]->atom;
120         XCBATOM WM_DELETE_WINDOW = atomrep[1]->atom;
121         XCBATOM XA_ATOM = { 4 };
122         XCBChangeProperty(c, PropModeReplace, window, WM_PROTOCOLS, XA_ATOM, 32, 1, &WM_DELETE_WINDOW);
123     }
124     free(atomrep[0]);
125     free(atomrep[1]);
126 #endif
127     try_events(c);
128
129     XCBMapWindow(c, window);
130     XCBFlush(c);
131
132     /* Send off a collection of requests */
133 #ifdef TEST_GET_WINDOW_ATTRIBUTES
134     attr[0] = XCBGetWindowAttributes(c, window);
135 #endif
136 #ifdef TEST_GET_GEOMETRY
137     d.window = root->root;
138     geom[0] = XCBGetGeometry(c, d);
139     d.window = window;
140     geom[1] = XCBGetGeometry(c, d);
141 #endif
142 #ifdef TEST_QUERY_TREE
143 # ifdef SUPERVERBOSE /* this produces a lot of output :) */
144     tree[0] = XCBQueryTree(c, root->root);
145 # endif
146     tree[1] = XCBQueryTree(c, window);
147 #endif
148
149     /* Start reading replies and possibly events */
150 #ifdef TEST_GET_GEOMETRY
151     geomrep[0] = XCBGetGeometryReply(c, geom[0], 0);
152     formatGetGeometryReply(root->root, geomrep[0]);
153     free(geomrep[0]);
154 #endif
155
156 #ifdef TEST_QUERY_TREE
157 # ifdef SUPERVERBOSE /* this produces a lot of output :) */
158     treerep[0] = XCBQueryTreeReply(c, tree[0], 0);
159     formatQueryTreeReply(root->root, treerep[0]);
160     free(treerep[0]);
161 # endif
162 #endif
163
164 #ifdef TEST_GET_GEOMETRY
165     geomrep[1] = XCBGetGeometryReply(c, geom[1], 0);
166     formatGetGeometryReply(window, geomrep[1]);
167     free(geomrep[1]);
168 #endif
169
170     try_events(c);
171
172     /* Mix in some more requests */
173 #ifdef TEST_QUERY_TREE
174     treerep[1] = XCBQueryTreeReply(c, tree[1], 0);
175     formatQueryTreeReply(window, treerep[1]);
176
177     if(treerep[1] && treerep[1]->parent.xid && treerep[1]->parent.xid != root->root.xid)
178     {
179         tree[2] = XCBQueryTree(c, treerep[1]->parent);
180
181 # ifdef TEST_GET_GEOMETRY
182         d.window = treerep[1]->parent;
183         geom[2] = XCBGetGeometry(c, d);
184         geomrep[2] = XCBGetGeometryReply(c, geom[2], 0);
185         formatGetGeometryReply(treerep[1]->parent, geomrep[2]);
186         free(geomrep[2]);
187 # endif
188
189         treerep[2] = XCBQueryTreeReply(c, tree[2], 0);
190         formatQueryTreeReply(treerep[1]->parent, treerep[2]);
191         free(treerep[2]);
192     }
193
194     free(treerep[1]);
195 #endif
196
197     try_events(c);
198
199     /* Get the last reply of the first batch */
200 #if 1 /* if 0, leaves a reply in the reply queue */
201 #ifdef TEST_GET_WINDOW_ATTRIBUTES
202     attrrep[0] = XCBGetWindowAttributesReply(c, attr[0], 0);
203     formatGetWindowAttributesReply(window, attrrep[0]);
204     free(attrrep[0]);
205 #endif
206 #endif
207
208 #ifdef TEST_THREADS
209     pthread_join(event_thread, 0);
210 #else
211     wait_events(c);
212 #endif
213
214     exit(0);
215     /*NOTREACHED*/
216 }
217
218 int show_event(XCBGenericEvent *e)
219 {
220     int ret = 1;
221     if(!formatEvent(e))
222         return 0;
223
224     if(e->response_type == XCBButtonRelease)
225         ret = 0; /* They clicked, therefore, we're done. */
226     free(e);
227     return ret;
228 }
229
230 void try_events(XCBConnection *c)
231 {
232     XCBGenericEvent *e;
233     while((e = XCBPollForEvent(c, 0)) && show_event(e))
234         /* empty statement */ ;
235 }
236
237 void wait_events(XCBConnection *c)
238 {
239     XCBGenericEvent *e;
240 #ifdef TEST_THREADS
241 # ifdef VERBOSE
242     printf("wait_events() thread ID: %ld\n", pthread_self());
243 # endif
244 #endif
245     while((e = XCBWaitForEvent(c)) && show_event(e))
246         /* empty statement */ ;
247 }