add the complete cursor example. Make the html code valid
[free-sw/xcb/libxcb] / doc / tutorial / index.html
1 <!DOCTYPE html public "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
3
4 <html>
5
6 <head>
7   <title>Basic Graphics Programming With The XCB Library</title>
8   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
9   <link href="xcb.css" rel="stylesheet" type="text/css">
10 </head>
11
12 <body>
13   <div class="title">
14     Basic Graphics Programming With The XCB Library
15   </div>
16   <div class="toc">
17   <ol>
18     <li><a class="section" href="#intro">Introduction</a>
19     <li><a class="section" href="#Xmodel">The client and server model of the X window system</a>
20     <li><a class="section" href="#asynch">GUI programming: the asynchronous model</a>
21     <li><a class="section" href="#notions">Basic XCB notions</a>
22       <ol>
23         <li><a class="subsection" href="#conn">The X Connection</a>
24         <li><a class="subsection" href="#requestsreplies">Requests and replies: the Xlib killers</a>
25         <li><a class="subsection" href="#gc">The Graphics Context</a>
26         <li>Object handles
27         <li>Memory allocation for XCB structures
28         <li><a class="subsection" href="#events">Events</a>
29       </ol>
30     <li><a class="section" href="#use">Using XCB-based programs</a>
31       <ol>
32         <li><a class="subsection" href="#inst">Installation of XCB</a>
33         <li><a class="subsection" href="#comp">Compiling XCB-based programs</a>
34       </ol>
35     <li><a class="section" href="#openconn">Opening and closing the connection to an X server</a>
36     <li><a class="section" href="#screen">Checking basic information about a connection</a>
37     <li><a class="section" href="#helloworld">Creating a basic window - the "hello world" program</a>
38     <li><a class="section" href="#drawing">Drawing in a window</a>
39       <ol>
40         <li><a class="subsection" href="#allocgc">Allocating a Graphics Context</a>
41         <li><a class="subsection" href="#changegc">Changing the attributes of a Graphics Context</a>
42         <li><a class="subsection" href="#drawingprim">Drawing primitives: point, line, box, circle,...</a>
43       </ol>
44     <li><a class="section" href="#xevents">X Events</a>
45       <ol>
46         <li><a class="subsection" href="#register">Registering for event types using event masks</a>
47         <li><a class="subsection" href="#loop">Receiving events: writing the events loop</a>
48         <li><a class="subsection" href="#expose">Expose events</a>
49         <li><a class="subsection" href="#userinput">Getting user input</a>
50           <ol>
51             <li><a class="subsubsection" href="#mousepressrelease">Mouse button press and release events</a>
52             <li><a class="subsubsection" href="#mousemvnt">Mouse movement events</a>
53             <li><a class="subsubsection" href="#mouseenter">Mouse pointer enter and leave events</a>
54             <li><a class="subsubsection" href="#focus">The keyboard focus</a>
55             <li><a class="subsubsection" href="#keypress">Keyboard press and release events</a>
56           </ol>
57         <li><a class="subsection" href="#eventex">X events: a complete example</a>
58       </ol>
59     <li><a class="section" href="#font">Handling text and fonts</a>
60       <ol>
61         <li><a class="subsection" href="#fontstruct">The Font structure</a>
62         <li>Loading a Font
63         <li>Assigning a Font to a Graphic Context
64         <li>Drawing text in a window
65       </ol>
66     <li>Windows hierarchy
67       <ol>
68         <li>Root, parent and child windows
69         <li>Events propagation
70       </ol>
71     <li><a class="section" href="#wm">Interacting with the window manager</a>
72       <ol>
73         <li><a class="subsection" href="#wmprop">Window properties</a>
74         <li><a class="subsection" href="#wmname">Setting the window name and icon name</a>
75         <li>Setting preferred window size(s)
76         <li>Setting miscellaneous window manager hints
77         <li>Setting an application's icon
78         <li>Obeying the delete-window protocol
79       </ol>
80     <li><a class="section" href="#winop">Simple window operations</a>
81       <ol>
82         <li><a class="subsection" href="#winmap">Mapping and unmapping a window</a>
83         <li><a class="subsection" href="#winconf">Configuring a window</a>
84         <li><a class="subsection" href="#winmove">Moving a window around the screen</a>
85         <li><a class="subsection" href="#winsize">Resizing a window</a>
86         <li><a class="subsection" href="#winstack">Changing windows stacking order: raise and lower</a>
87         <li>Iconifying and de-iconifying a window
88         <li><a class="subsection" href="#wingetinfo">Getting informations about a window</a>
89       </ol>
90     <li><a class="section" href="#usecolor">Using colors to paint the rainbow</a>
91       <ol>
92         <li><a class="subsection" href="#colormap">Color maps</a>
93         <li><a class="subsection" href="#colormapalloc">Allocating and freeing Color Maps</a>
94         <li><a class="subsection" href="#alloccolor">Allocating and freeing a color entry</a>
95         <li>Drawing with a color
96       </ol>
97     <li><a class="section" href="#pixmaps">X Bitmaps and Pixmaps</a>
98       <ol>
99         <li><a class="subsection" href="#pixmapswhat">What is a X Bitmap ? An X Pixmap ?</a>
100         <li>Loading a bitmap from a file
101         <li>Drawing a bitmap in a window
102         <li><a class="subsection" href="#pixmapscreate">Creating a pixmap</a>
103         <li><a class="subsection" href="#pixmapsdraw">Drawing a pixmap in a window</a>
104         <li><a class="subsection" href="#pixmapsfree">Freeing a pixmap</a>
105       </ol>
106     <li><a class="subsection" href="#mousecursor">Messing with the mouse cursor</a>
107       <ol>
108         <li><a class="subsection" href="#mousecursorcreate">Creating and destroying a mouse cursor</a>
109         <li><a class="subsection" href="#mousecursorset">Setting a window's mouse cursor</a>
110         <li><a class="subsection" href="#mousecursorexample">Complete example</a>
111       </ol>
112     <li><a class="subsection" href="#translation">Translation of basic Xlib functions and macros</a>
113       <ol>
114         <li><a class="subsection" href="#displaystructure">Members of the Display structure</a>
115           <ol>
116             <li><a class="subsection" href="#ConnectionNumber">ConnectionNumber</a>
117             <li><a class="subsection" href="#DefaultScreen">DefaultScreen</a>
118             <li><a class="subsection" href="#QLength">QLength</a>
119             <li><a class="subsection" href="#ScreenCount">ScreenCount</a>
120             <li><a class="subsection" href="#ServerVendor">ServerVendor</a>
121             <li><a class="subsection" href="#ProtocolVersion">ProtocolVersion</a>
122             <li><a class="subsection" href="#ProtocolRevision">ProtocolRevision</a>
123             <li><a class="subsection" href="#VendorRelease">VendorRelease</a>
124             <li><a class="subsection" href="#DisplayString">DisplayString</a>
125             <li><a class="subsection" href="#BitmapUnit">BitmapUnit</a>
126             <li><a class="subsection" href="#BitmapBitOrder">BitmapBitOrder</a>
127             <li><a class="subsection" href="#BitmapPad">BitmapPad</a>
128             <li><a class="subsection" href="#ImageByteOrder">ImageByteOrder</a>
129           </ol>
130         <li><a class="subsection" href="#screenofdisplay">ScreenOfDisplay related functions</a>
131           <ol>
132             <li><a class="subsection" href="#ScreenOfDisplay">ScreenOfDisplay</a>
133             <li><a class="subsection" href="#DefaultScreenOfDisplay">DefaultScreenOfDisplay</a>
134             <li><a class="subsection" href="#RootWindow">RootWindow / RootWindowOfScreen</a>
135             <li><a class="subsection" href="#DefaultRootWindow">DefaultRootWindow</a>
136             <li><a class="subsection" href="#DefaultVisual">DefaultVisual / DefaultVisualOfScreen</a>
137             <li><a class="subsection" href="#DefaultGC">DefaultGC / DefaultGCOfScreen</a>
138             <li><a class="subsection" href="#BlackPixel">BlackPixel / BlackPixelOfScreen</a>
139             <li><a class="subsection" href="#WhitePixel">WhitePixel / WhitePixelOfScreen</a>
140             <li><a class="subsection" href="#DisplayWidth">DisplayWidth / WidthOfScreen</a>
141             <li><a class="subsection" href="#DisplayHeight">DisplayHeight / HeightOfScreen</a>
142             <li><a class="subsection" href="#DisplayWidthMM">DisplayWidthMM / WidthMMOfScreen</a>
143             <li><a class="subsection" href="#DisplayHeightMM">DisplayHeightMM / HeightMMOfScreen</a>
144             <li><a class="subsection" href="#DisplayPlanes">DisplayPlanes / DefaultDepth / DefaultDepthOfScreen / PlanesOfScreen</a>
145             <li><a class="subsection" href="#DefaultColormap">DefaultColormap / DefaultColormapOfScreen</a>
146             <li><a class="subsection" href="#MinCmapsOfScreen">MinCmapsOfScreen</a>
147             <li><a class="subsection" href="#MaxCmapsOfScreen">MaxCmapsOfScreen</a>
148             <li><a class="subsection" href="#DoesSaveUnders">DoesSaveUnders</a>
149             <li><a class="subsection" href="#DoesBackingStore">DoesBackingStore</a>
150             <li><a class="subsection" href="#EventMaskOfScreen">EventMaskOfScreen</a>
151           </ol>
152         <li><a class="subsection" href="#misc">Miscellaneaous macros</a>
153           <ol>
154             <li><a class="subsection" href="#DisplayOfScreen">DisplayOfScreen</a>
155             <li><a class="subsection" href="#DisplayCells">DisplayCells / CellsOfScreen</a>
156           </ol>
157       </ol>
158   </ol>
159   </div>
160   <div class="section">
161     <ol>
162       <li class="title"><a name="intro">Introduction</a>
163       <p>
164       This tutorial is based on the
165       <a href="http://users.actcom.co.il/~choo/lupg/tutorials/xlib-programming/xlib-programming.html">Xlib Tutorial</a>
166       written by <a href="mailto:choor at atcom dot co dot il">Guy Keren</a>. The
167       author allowed me to take some parts of his text, mainly the text which
168       deals with the X Windows generality.
169       </p>
170       <p>
171       This tutorial is intended for people who want to start to program
172       with the <a href="http://xcb.freedesktop.org">XCB</a>
173       library. keep in mind that XCB, like the
174       <a href="http://tronche.com/gui/x/xlib/introduction">Xlib</a>
175       library, isn't what post programmers wanting to write X
176       applications are looking for. They should use a much higher
177       level GUI toolkit like Motif,
178       <a href="http://www.lesstif.org">LessTiff</a>,
179       <a href="http://www.gtk.org">GTK</a>,
180       <a href="http://www.trolltech.com">QT</a> or
181       <a href="http://www.enlightenment.org">EWL</a>, or use
182       <a href="http://cairographics.org">Cairo</a>.
183       However,
184       we need to start somewhere. More than this, knowing how things
185       work down below is never a bad idea.
186       </p>
187       <p>
188       After reading this tutorial, one should be able to write very
189       simple graphical programs, but not programs with decent user
190       interfaces. For such programs, one of the previously mentioned
191       libraries should be used.
192       </p>
193       <p>
194       But what is XCB? Xlib has been
195       the standard C binding for the <a href="http://www.x.org">X
196       Window System</a> protocol for many years now. It is an
197       excellent piece of work, but there are applications for which it
198       is not ideal, for example:
199       </p>
200       <ul>
201         <li><b>Small platforms</b>: Xlib is a large piece of code, and
202         it's difficult to make it smaller
203         <li><b>Latency hiding</b>: Xlib requests requiring a reply are
204         effectively synchronous: they block until the reply appears,
205         whether the result is needed immediately or not.
206         <li><b>Direct access to the protocol</b>: Xlib does quite a
207         bit of caching, layering, and similar optimizations. While this
208         is normally a feature, it makes it difficult to simply emit
209         specified X protocol requests and process specific
210         responses.
211         <li><b>Threaded applications</b>: While Xlib does attempt to
212         support multithreading, the API makes this difficult and
213         error-prone.
214         <li><b>New extensions</b>: The Xlib infrastructure provides
215         limited support for the new creation of X extension client side
216         code.
217       </ul>
218       <p>
219       For these reasons, among others, XCB, an X C binding, has been
220       designed to solve the above problems and thus provide a base for
221       </p>
222       <ul>
223         <li>Toolkit implementation.
224         <li>Direct protocol-level programming.
225         <li>Lightweight emulation of commonly used portions of the
226         Xlib API.
227       </ul>
228       <br>
229       <li class="title"><a name="Xmodel">The client and server model of the X window system</a>
230       <p>
231       The X Window System was developed with one major goal:
232       flexibility. The idea was that the way things look is one thing,
233       but the way things work is another matter. Thus, the lower
234       levels provide the tools required to draw windows, handle user
235       input, allow drawing graphics using colors (or black and white
236       screens), etc. To this point, a decision was made to separate
237       the system into two parts. A client that decides what to do, and
238       a server that actually draws on the screen and reads user input
239       in order to send it to the client for processing.
240       </p>
241       <p>
242       This model is the complete opposite of what is used to when
243       dealing with clients and servers. In our case, the user sits
244       near the machine controlled by the server, while the client
245       might be running on a remote machine. The server controls the
246       screens, mouse and keyboard. A client may connect to the server,
247       request that it draws a window (or several windows), and ask the
248       server to send it any input the user sends to these
249       windows. Thus, several clients may connect to a single X server
250       (one might be running mail software, one running a WWW
251       browser, etc). When input is sent by the user to some window,
252       the server sends a message to the client controlling this window
253       for processing. The client decides what to do with this input,
254       and sends the server requests for drawing in the window.
255       </p>
256       <p>
257       The whole session is carried out using the X message
258       protocol. This protocol was originally carried over the TCP/IP
259       protocol suite, allowing the client to run on any machine
260       connected to the same network that the server is. Later on, the
261       X servers were extended to allow clients running on the local
262       machine with more optimized access to the server (note that an X
263       protocol message may be several hundreds of KB in size), such as
264       using shared memory, or using Unix domain sockets (a method for
265       creating a logical channel on a Unix system between two processes).
266       </p>
267       <li class="title"><a name="asynch">GUI programming: the asynchronous model</a>
268       <p>
269       Unlike conventional computer programs, that carry some serial
270       nature, a GUI program usually uses an asynchronous programming
271       model, also known as "event-driven programming". This means that
272       that program mostly sits idle, waiting for events sent by the X
273       server, and then acts upon these events. An event may say "The
274       user pressed the 1st button mouse in spot (x,y)", or "The window
275       you control needs to be redrawn". In order for the program to be
276       responsive to the user input, as well as to refresh requests, it
277       needs to handle each event in a rather short period of time
278       (e.g. less that 200 milliseconds, as a rule of thumb).
279       </p>
280       <p>
281       This also implies that the program may not perform operations
282       that might take a long time while handling an event (such as
283       opening a network connection to some remote server, or
284       connecting to a database server, or even performing a long file
285       copy operation). Instead, it needs to perform all these
286       operations in an asynchronous manner. This may be done by using
287       various asynchronous models to perform the longish operations,
288       or by performing them in a different process or thread.
289       </p>
290       <p>
291       So the way a GUI program looks is something like that:
292       </p>
293       <ol>
294         <li>Perform initialization routines.
295         <li>Connect to the X server.
296         <li>Perform X-related initialization.
297         <li>While not finished:
298           <ol>
299             <li>Receive the next event from the X server.
300             <li>Handle the event, possibly sending various drawing
301             requests to the X server.
302             <li>If the event was a quit message, exit the loop.
303           </ol>
304         <li>Close down the connection to the X server.
305         <li>Perform cleanup operations.
306       </ol>
307       <br>
308       <li class="title"><a name="notions">Basic XCB notions</a>
309       <p>
310       XCB has been created to eliminate the need for
311       programs to actually implement the X protocol layer. This
312       library gives a program a very low-level access to any X
313       server. Since the protocol is standardized, a client using any
314       implementation of XCB may talk with any X server (the same
315       occurs for Xlib, of course). We now give a brief description of
316       the basic XCB notions. They will be detailed later.
317       </p>
318       <ol>
319         <li class="subtitle"><a name="conn">The X Connection</a>
320         <p>
321         The major notion of using XCB is the X Connection. This is a
322         structure representing the connection we have open with a
323         given X server. It hides a queue of messages coming from the
324         server, and a queue of pending requests that our client
325         intends to send to the server. In XCB, this structure is named
326         'xcb_connection_t'. It is analogous to the Xlib Display.
327         When we open a connection to an X server, the
328         library returns a pointer to such a structure. Later, we
329         supply this pointer to any XCB function that should send
330         messages to the X server or receive messages from this server.
331         </p>
332         <li class="subtitle"><a name="requestsreplies">Requests and
333         replies: the Xlib killers</a>
334         <p>
335         To ask for information from the X server, we have to make a request
336         and ask for a reply. With Xlib, these two tasks are
337         automatically done: Xlib locks the system, sends a request,
338         waits for a reply from the X server and unlocks. This is
339         annoying, especially if one makes a lot of requests to the X
340         server. Indeed, Xlib has to wait for the end of a reply
341         before asking for the next request (because of the locks that
342         Xlib sends). For example, here is a time-line of N=4
343         requests/replies with Xlib, with a round-trip latency
344         <b>T_round_trip</b> that is 5 times long as the time required
345         to write or read a request/reply (<b>T_write/T_read</b>):
346         </p>
347         <pre class="text">
348   W-----RW-----RW-----RW-----R
349 </pre>
350         <ul>
351           <li>W: Writing request
352           <li>-: Stalled, waiting for data
353           <li>R: Reading reply
354         </ul>
355         <p>
356         The total time is N * (T_write + T_round_trip + T_read).
357         </p>
358         <p>
359         With XCB, we can suppress most of the round-trips as the
360         requests and the replies are not locked. We usually send a
361         request, then XCB returns to us a <b>cookie</b>, which is an
362         identifier. Then, later, we ask for a reply using this
363         <b>cookie</b> and XCB returns a
364         pointer to that reply. Hence, with XCB, we can send a lot of
365         requests, and later in the program, ask for all the replies
366         when we need them. Here is the time-line for 4
367         requests/replies when we use this property of XCB:
368         </p>
369         <pre class="text">
370   WWWW--RRRR
371 </pre>
372         <p>
373         The total time is N * T_write + max (0, T_round_trip - (N-1) *
374         T_write) + N * T_read. Which can be considerably faster than
375         all those Xlib round-trips.
376         </p>
377         <p>
378         Here is a program that computes the time to create 500 atoms
379         with Xlib and XCB. It shows the Xlib way, the bad XCB way
380         (which is similar to Xlib) and the good XCB way. On my
381         computer, XCB is 25 times faster than Xlib.
382         </p>
383       <pre class="code">
384 #include &lt;stdlib.h&gt;
385 #include &lt;stdio.h&gt;
386 #include &lt;string.h&gt;
387 #include &lt;sys/time.h&gt;
388
389 #include &lt;xcb/xcb.h&gt;
390
391 #include &lt;X11/Xlib.h&gt;
392
393 double
394 get_time(void)
395 {
396   struct timeval timev;
397
398   gettimeofday(&amp;timev, NULL);
399
400   return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
401 }
402
403 int
404 main ()
405 {
406   xcb_connection_t         *c;
407   xcb_atom_t               *atoms;
408   xcb_intern_atom_cookie_t *cs;
409   char                    **names;
410   int                       count;
411   int                       i;
412   double                    start;
413   double                    end;
414   double                    diff;
415
416   /* Xlib */
417   Display *disp;
418   Atom    *atoms_x;
419   double   diff_x;
420
421   c = xcb_connect (NULL, NULL);
422
423   count = 500;
424   atoms = (xcb_atom_t *)malloc (count * sizeof (atoms));
425   names = (char **)malloc (count * sizeof (char *));
426
427   /* init names */
428   for (i = 0; i &lt; count; ++i) {
429     char buf[100];
430
431     sprintf (buf, "NAME%d", i);
432     names[i] = strdup (buf);
433   }
434
435   /* bad use */
436   start = get_time ();
437
438   for (i = 0; i &lt; count; ++i)
439     atoms[i] = xcb_intern_atom_reply (c,
440                                       xcb_intern_atom (c,
441                                                        0,
442                                                        strlen(names[i]),
443                                                        names[i]),
444                                       NULL)->atom;
445
446   end = get_time ();
447   diff = end - start;
448   printf ("bad use time  : %f\n", diff);
449
450   /* good use */
451   start = get_time ();
452
453   cs = (xcb_intern_atom_cookie_t *) malloc (count * sizeof(xcb_intern_atom_cookie_t));
454   for(i = 0; i &lt; count; ++i)
455     cs[i] = xcb_intern_atom (c, 0, strlen(names[i]), names[i]);
456
457   for(i = 0; i &lt; count; ++i) {
458     xcb_intern_atom_reply_t *r;
459
460     r = xcb_intern_atom_reply(c, cs[i], 0);
461     if(r)
462       atoms[i] = r->atom;
463     free(r);
464   }
465
466   end = get_time ();
467   printf ("good use time : %f\n", end - start);
468   printf ("ratio         : %f\n", diff / (end - start));
469   diff = end - start;
470
471   /* free var */
472   for (i = 0; i &lt; count; ++i)
473     free (names[i]);
474   free (atoms);
475   free (cs);
476
477   xcb_disconnect (c);
478
479   /* Xlib */
480   disp = XOpenDisplay (getenv("DISPLAY"));
481
482   atoms_x = (Atom *)malloc (count * sizeof (atoms_x));
483
484   start = get_time ();
485
486   for (i = 0; i &lt; count; ++i)
487     atoms_x[i] = XInternAtom(disp, names[i], 0);
488
489   end = get_time ();
490   diff_x = end - start;
491   printf ("Xlib use time : %f\n", diff_x);
492   printf ("ratio         : %f\n", diff_x / diff);
493
494   free (atoms_x);
495   free (names);
496
497   XCloseDisplay (disp);
498
499   return 0;
500 }
501 </pre>
502         <li class="subtitle"><a name="gc">The Graphic Context</a>
503         <p>
504         When we perform various drawing operations (graphics, text,
505         etc), we may specify various options for controlling how the
506         data will be drawn (what foreground and background colors to
507         use, how line edges will be connected, what font to use when
508         drawing some text, etc). In order to avoid the need to supply
509         hundreds of parameters to each drawing function, a graphical
510         context structure is used. We set the various drawing options
511         in this structure, and then we pass a pointer to this
512         structure to any drawing routines. This is rather handy, as we
513         often need to perform several drawing requests with the same
514         options. Thus, we would initialize a graphical context, set
515         the desired options, and pass this structure to all drawing
516         functions.
517         </p>
518         <p>
519         Note that graphic contexts have no client-side structure in
520         XCB, they're just XIDs. Xlib has a client-side structure
521         because it caches the GC contents so it can avoid making
522         redundant requests, but of course XCB doesn't do that.
523         </p>
524         <li class="subtitle"><a name="events">Events</a>
525         <p>
526         A structure is used to pass events received from the X
527         server. XCB supports exactly the events specified in the
528         protocol (33 events). This structure contains the type
529         of event received (including a bit for whether it came
530         from the server or another client), as well as the data associated with the
531         event (e.g. position on the screen where the event was
532         generated, mouse button associated with the event, region of
533         the screen associated with a "redraw" event, etc). The way to
534         read the event's data depends on the event type.
535         </p>
536       </ol>
537       <br>
538       <li class="title"><a name="use">Using XCB-based programs</a>
539       <br>
540       <ol>
541         <li class="subtitle"><a name="inst">Installation of XCB</a>
542         <p>
543         <b>TODO:</b> These instructions are out of date.
544         Just reference the <a href="http://xcb.freedesktop.org/">main XCB page</a>
545         so we don't have to maintain these instructions in more than
546         one place.
547         </p>
548         <p>
549         To build XCB from source, you need to have installed at
550         least:
551         </p>
552         <ul>
553           <li>pkgconfig 0.15.0
554           <li><a href="http://www.gnu.org/software/automake/">automake 1.7</a>
555           <li><a href="http://www.gnu.org/software/autoconf/">autoconf 2.50</a>
556           <li><a href="http://www.check.org">check</a>
557           <li><a href="http://xmlsoft.org/XSLT/">xsltproc</a>
558           <li><a href="http://www.gnu.org/software/gperf/">gperf 3.0.1</a>
559         </ul>
560         <p>
561         You have to checkout in the git repository the following modules:
562         </p>
563         <ul>
564           <li>Xau from xlibs
565           <li>xcb-proto
566           <li>xcb
567         </ul>
568         <p>
569         Note that xcb-proto exists only to install header
570         files, so typing 'make' or 'make all' will produce the message
571         "Nothing to be done for 'all'". That's normal.
572         </p>
573         <li class="subtitle"><a name="comp">Compiling XCB-based programs</a>
574         <p>
575         Compiling XCB-based programs requires linking them with the XCB
576         library. This is easily done thanks to pkgconfig:
577         </p>
578         <pre class="text">
579 gcc -Wall prog.c -o prog `pkg-config --cflags --libs xcb`
580 </pre>
581       </ol>
582       <li class="title"><a name="openconn">Opening and closing the connection to an X server</a>
583       <p>
584       An X program first needs to open the connection to the X
585       server. There is a function that opens a connection. It requires
586       the display name, or NULL. In the latter case, the display name
587       will be the one in the environment variable DISPLAY.
588       </p>
589       <pre class="code">
590 <span class="type">xcb_connection_t</span> *xcb_connect (<span class="keyword">const</span> <span class="type">char</span> *displayname,
591                                <span class="type">int</span>        *screenp);
592 </pre>
593       <p>
594       The second parameter returns the screen number used for the
595       connection. The returned structure describes an XCB connection
596       and is opaque. Here is how the connection can be opened:
597       </p>
598       <pre class="code">
599 #<span class="include">include</span> <span class="string">&lt;xcb/xcb.h&gt;</span>
600
601 <span class="type">int</span>
602 <span class="function">main</span> ()
603 {
604   <span class="type">xcb_connection_t</span> *c;
605
606   /* Open the connection to the X server. Use the DISPLAY environment variable as the default display name */
607   c = xcb_connect (NULL, NULL);
608
609   <span class="keyword">return</span> 0;
610 }
611 </pre>
612       <p>
613       To close a connection, it suffices to use:
614       </p>
615       <pre class="code">
616 <span class="type">void</span> xcb_disconnect (<span class="type">xcb_connection_t</span> *c);
617 </pre>
618       <div class="comp">
619         <div class="title">
620         Comparison Xlib/XCB
621         </div>
622         <div class="xlib">
623         <ul>
624           <li>XOpenDisplay ()
625         </ul>
626         </div>
627         <div class="xcb">
628         <ul>
629           <li>xcb_connect ()
630         </ul>
631         </div>
632         <div class="xlib">
633         <ul>
634           <li>XCloseDisplay ()
635         </ul>
636         </div>
637         <div class="xcb">
638         <ul>
639           <li>xcb_disconnect ()
640         </ul>
641         </div>
642       </div>
643       <br>
644       <li class="title"><a name="screen">Checking basic information about a connection</a>
645       <p>
646       Once we have opened a connection to an X server, we should check some
647       basic information about it: what screens it has, what is the
648       size (width and height) of the screen, how many colors it
649       supports (black and white ? grey scale ?, 256 colors ? more ?),
650       and so on. We get such information from the xcb_screen_t
651       structure:
652       </p>
653       <pre class="code">
654 typedef struct {
655     xcb_window_t   root;
656     xcb_colormap_t default_colormap;
657     uint32_t       white_pixel;
658     uint32_t       black_pixel;
659     uint32_t       current_input_masks;
660     uint16_t       width_in_pixels;
661     uint16_t       height_in_pixels;
662     uint16_t       width_in_millimeters;
663     uint16_t       height_in_millimeters;
664     uint16_t       min_installed_maps;
665     uint16_t       max_installed_maps;
666     xcb_visualid_t root_visual;
667     uint8_t        backing_stores;
668     uint8_t        save_unders;
669     uint8_t        root_depth;
670     uint8_t        allowed_depths_len;
671 } xcb_screen_t;
672 </pre>
673       <p>
674       We could retrieve the first screen of the connection by using the
675       following function:
676       </p>
677       <pre class="code">
678 xcb_screen_iterator_t xcb_setup_roots_iterator (xcb_setup_t *R);
679 </pre>
680       <p>
681       Here is a small program that shows how to use this function:
682       </p>
683       <pre class="code">
684 #include &lt;stdio.h&gt;
685
686 #include &lt;xcb/xcb.h&gt;
687
688 int
689 main ()
690 {
691   xcb_connection_t     *c;
692   xcb_screen_t         *screen;
693   int                   screen_nbr;
694   xcb_screen_iterator_t iter;
695
696   /* Open the connection to the X server. Use the DISPLAY environment variable */
697   c = xcb_connect (NULL, &amp;screen_nbr);
698
699   /* Get the screen #screen_nbr */
700   iter = xcb_setup_roots_iterator (xcb_get_setup (c));
701   for (; iter.rem; --screen_nbr, xcb_screen_next (&amp;iter))
702     if (screen_nbr == 0) {
703       screen = iter.data;
704       break;
705     }
706
707   printf ("\n");
708   printf ("Informations of screen %ld:\n", screen-&gt;root.xid);
709   printf ("  width.........: %d\n", screen-&gt;width_in_pixels);
710   printf ("  height........: %d\n", screen-&gt;height_in_pixels);
711   printf ("  white pixel...: %ld\n", screen-&gt;white_pixel);
712   printf ("  black pixel...: %ld\n", screen-&gt;black_pixel);
713   printf ("\n");
714
715   return 0;
716 }
717 </pre>
718       <li class="title"><a name="helloworld">Creating a basic window - the "hello world" program</a>
719       <p>
720       After we got some basic information about our screen, we can
721       create our first window. In the X Window System, a window is
722       characterized by an Id. So, in XCB, a window is of type:
723       </p>
724       <pre class="code">
725 typedef struct {
726     uint32_t xid;
727 } xcb_window_t;
728 </pre>
729       <p>
730       We first ask for a new Id for our window, with this function:
731       </p>
732       <pre class="code">
733 xcb_window_t xcb_generate_id(xcb_connection_t *c);
734 </pre>
735       <p>
736       Then, XCB supplies the following function to create new windows:
737       </p>
738       <pre class="code">
739 xcb_void_cookie_t xcb_create_window (xcb_connection_t *c,             /* Pointer to the xcb_connection_t structure */
740                                      uint8_t           depth,         /* Depth of the screen */
741                                      xcb_window_t      wid,           /* Id of the window */
742                                      xcb_window_t      parent,        /* Id of an existing window that should be the parent of the new window */
743                                      int16_t           x,             /* X position of the top-left corner of the window (in pixels) */
744                                      int16_t           y,             /* Y position of the top-left corner of the window (in pixels) */
745                                      uint16_t          width,         /* Width of the window (in pixels) */
746                                      uint16_t          height,        /* Height of the window (in pixels) */
747                                      uint16_t          border_width,  /* Width of the window's border (in pixels) */
748                                      uint16_t          _class,
749                                      xcb_visualid_t    visual,
750                                      uint32_t          value_mask,
751                                      const uint32_t   *value_list);
752 </pre>
753       <p>
754       The fact that we created the window does not mean that it will
755       be drawn on screen. By default, newly created windows are not
756       mapped on the screen (they are invisible). In order to make our
757       window visible, we use the function <span class="code">xcb_map_window()</span>, whose
758       prototype is
759       </p>
760       <pre class="code">
761 xcb_void_cookie_t xcb_map_window (xcb_connection_t *c,
762                                   xcb_window_t      window);
763 </pre>
764       <p>
765       Finally, here is a small program to create a window of size
766       150x150 pixels, positioned at the top-left corner of the screen:
767       </p>
768       <pre class="code">
769 #include &lt;unistd.h&gt;      /* pause() */
770
771 #include &lt;xcb/xcb.h&gt;
772
773 int
774 main ()
775 {
776   xcb_connection_t *c;
777   xcb_screen_t     *screen;
778   xcb_window_t      win;
779
780   /* Open the connection to the X server */
781   c = xcb_connect (NULL, NULL);
782
783   /* Get the first screen */
784   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
785
786   /* Ask for our window's Id */
787   win = xcb_generate_id(c);
788
789   /* Create the window */
790   xcb_create_window (c,                             /* Connection          */
791                      XCB_COPY_FROM_PARENT,          /* depth (same as root)*/
792                      win,                           /* window Id           */
793                      screen-&gt;root,                  /* parent window       */
794                      0, 0,                          /* x, y                */
795                      150, 150,                      /* width, height       */
796                      10,                            /* border_width        */
797                      XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
798                      screen-&gt;root_visual,           /* visual              */
799                      0, NULL);                      /* masks, not used yet */
800
801   /* Map the window on the screen */
802   xcb_map_window (c, win);
803
804   /* Make sure commands are sent before we pause, so window is shown */
805   xcb_flush (c);
806
807   pause ();    /* hold client until Ctrl-C */
808
809   return 0;
810 }
811 </pre>
812       <p>
813       In this code, you see one more function - <span class="code">xcb_flush()</span>, not explained
814       yet. It is used to flush all the pending requests. More
815       precisely, there are 2 functions that do such things. The first
816       one is <span class="code">xcb_flush()</span>:
817       </p>
818       <pre class="code">
819 int xcb_flush (xcb_connection_t *c);
820 </pre>
821       <p>
822       This function flushes all pending requests to the X server (much
823       like the <span class="code">fflush()</span> function is used to
824       flush standard output). The second function is
825       <span class="code">xcb_aux_sync()</span>:
826       </p>
827       <pre class="code">
828 int xcb_aux_sync (xcb_connection_t *c);
829 </pre>
830       <p>
831       This functions also flushes all pending requests to the X
832       server, and then waits until the X server finishing processing
833       these requests. In a normal program, this will not be necessary
834       (we'll see why when we get to write a normal X program), but for
835       now, we put it there.
836       </p>
837       <p>
838       The window that is created by the above code has a non defined
839       background. This one can be set to a specific color,
840       thanks to the two last parameters of
841       <span class="code">xcb_create_window()</span>, which are not
842       described yet. See the subsections
843       <a href="#winconf">Configuring a window</a> or
844       <a href="#winconf">Registering for event types using event masks</a>
845       for examples on how to use these parameters. In addition, as no
846       events are handled, you have to make a Ctrl-C to interrupt the
847       program.
848       </p>
849       <p>
850       <b>TODO</b>: one should tell what these functions return and
851       about the generic error
852       </p>
853       <div class="comp">
854         <div class="title">
855         Comparison Xlib/XCB
856         </div>
857         <div class="xlib">
858         <ul>
859           <li>XCreateWindow ()
860         </ul>
861         </div>
862         <div class="xcb">
863         <ul>
864           <li>xcb_generate_id ()
865           <li>xcb_create_window ()
866         </ul>
867         </div>
868       </div>
869       <br>
870       <li class="title"><a name="drawing">Drawing in a window</a>
871       <p>
872       Drawing in a window can be done using various graphical
873       functions (drawing pixels, lines, rectangles, etc). In order to
874       draw in a window, we first need to define various general
875       drawing parameters (what line width to use, which color to draw
876       with, etc). This is done using a graphical context.
877       </p>
878       <ol>
879         <li class="subtitle"><a name="allocgc">Allocating a Graphics Context</a>
880         <p>
881         As we said, a graphical context defines several attributes to
882         be used with the various drawing functions. For this, we
883         define a graphical context. We can use more than one graphical
884         context with a single window, in order to draw in multiple
885         styles (different colors, different line widths, etc). In XCB,
886         a Graphics Context is, as a window, characterized by an Id:
887         </p>
888         <pre class="code">
889 typedef struct {
890     uint32_t xid;
891 } xcb_gcontext_t;
892 </pre>
893         <p>
894         We first ask the X server to attribute an Id to our graphic
895         context with this function:
896         </p>
897         <pre class="code">
898 xcb_gcontext_t xcb_generate_id (xcb_connection_t *c);
899 </pre>
900         <p>
901         Then, we set the attributes of the graphic context with this function:
902         </p>
903         <pre class="code">
904 xcb_void_cookie_t xcb_create_gc (xcb_connection_t *c,
905                                  xcb_gcontext_t    cid,
906                                  xcb_drawable_t    drawable,
907                                  uint32_t          value_mask,
908                                  const uint32_t   *value_list);
909 </pre>
910         <p>
911         We give now an example on how to allocate a graphic context
912         that specifies that each drawing function that uses it will
913         draw in foreground with a black color.
914         </p>
915         <pre class="code">
916 #include &lt;xcb/xcb.h&gt;
917
918 int
919 main ()
920 {
921   xcb_connection_t *c;
922   xcb_screen_t     *screen;
923   xcb_drawable_t    win;
924   xcb_gcontext_t    black;
925   uint32_t          mask;
926   uint32_t          value[1];
927
928   /* Open the connection to the X server and get the first screen */
929   c = xcb_connect (NULL, NULL);
930   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
931
932   /* Create a black graphic context for drawing in the foreground */
933   win = screen-&gt;root;
934   black = xcb_generate_id (c);
935   mask = XCB_GC_FOREGROUND;
936   value[0] = screen-&gt;black_pixel;
937   xcb_create_gc (c, black, win, mask, value);
938
939   return 0;
940 }
941 </pre>
942         <p>
943         Note should be taken regarding the role of "value_mask" and
944         "value_list" in the prototype of <span class="code">xcb_create_gc()</span>. Since a
945         graphic context has many attributes, and since we often just
946         want to define a few of them, we need to be able to tell the
947         <span class="code">xcb_create_gc()</span> which attributes we
948         want to set. This is what the "value_mask" parameter is
949         for. We then use the "value_list" parameter to specify actual
950         values for the attribute we defined in "value_mask". Thus, for
951         each constant used in "value_list", we will use the matching
952         constant in "value_mask". In this case, we define a graphic
953         context with one attribute: when drawing (a point, a line,
954         etc), the foreground color will be black. The rest of the
955         attributes of this graphic context will be set to their
956         default values.
957         </p>
958         <p>
959         See the next Subsection for more details.
960         </p>
961         <div class="comp">
962           <div class="title">
963           Comparison Xlib/XCB
964           </div>
965           <div class="xlib">
966           <ul>
967             <li>XCreateGC ()
968           </ul>
969           </div>
970           <div class="xcb">
971           <ul>
972             <li>xcb_generate_id ()
973             <li>xcb_create_gc ()
974           </ul>
975           </div>
976         </div>
977         <br>
978         <li class="subtitle"><a name="changegc">Changing the attributes of a Graphics Context</a>
979         <p>
980         Once we have allocated a Graphic Context, we may need to
981         change its attributes (for example, changing the foreground
982         color we use to draw a line, or changing the attributes of the
983         font we use to display strings. See Subsections Drawing with a
984         color and Assigning a Font to a Graphic Context). This is done
985         by using this function:
986         </p>
987         <pre class="code">
988 xcb_void_cookie_t xcb_change_gc (xcb_connection_t *c,           /* The XCB Connection */
989                                  xcb_gcontext_t    gc,          /* The Graphic Context */
990                                  uint32_t          value_mask,  /* Components of the Graphic Context that have to be set */
991                                  const uint32_t   *value_list); /* Value as specified by value_mask */
992 </pre>
993         <p>
994         The <span class="code">value_mask</span> parameter could take
995         any combination of these masks from the xcb_gc_t enumeration:
996         </p>
997         <ul>
998           <li>XCB_GC_FUNCTION
999           <li>XCB_GC_PLANE_MASK
1000           <li>XCB_GC_FOREGROUND
1001           <li>XCB_GC_BACKGROUND
1002           <li>XCB_GC_LINE_WIDTH
1003           <li>XCB_GC_LINE_STYLE
1004           <li>XCB_GC_CAP_STYLE
1005           <li>XCB_GC_JOIN_STYLE
1006           <li>XCB_GC_FILL_STYLE
1007           <li>XCB_GC_FILL_RULE
1008           <li>XCB_GC_TILE
1009           <li>XCB_GC_STIPPLE
1010           <li>XCB_GC_TILE_STIPPLE_ORIGIN_X
1011           <li>XCB_GC_TILE_STIPPLE_ORIGIN_Y
1012           <li>XCB_GC_FONT
1013           <li>XCB_GC_SUBWINDOW_MODE
1014           <li>XCB_GC_GRAPHICS_EXPOSURES
1015           <li>XCB_GC_CLIP_ORIGIN_X
1016           <li>XCB_GC_CLIP_ORIGIN_Y
1017           <li>XCB_GC_CLIP_MASK
1018           <li>XCB_GC_DASH_OFFSET
1019           <li>XCB_GC_DASH_LIST
1020           <li>XCB_GC_ARC_MODE
1021         </ul>
1022         <p>
1023         It is possible to set several attributes at the same
1024         time (for example setting the attributes of a font and the
1025         color which will be used to display a string), by OR'ing these
1026         values in <span class="code">value_mask</span>. Then
1027         <span class="code">value_list</span> has to be an array which
1028         lists the value for the respective attributes.  <b>These values
1029         must be in the same order as masks listed above.</b> See Subsection
1030         Drawing with a color to have an example.
1031         </p>
1032         <p>
1033         <b>TODO</b>: set the links of the 3 subsections, once they will
1034         be written :)
1035         </p>
1036         <p>
1037         <b>TODO</b>: give an example which sets several attributes.
1038         </p>
1039         <li class="subtitle"><a name="drawingprim">Drawing primitives: point, line, box, circle,...</a>
1040         <p>
1041         After we have created a Graphic Context, we can draw on a
1042         window using this Graphic Context, with a set of XCB
1043         functions, collectively called "drawing primitives". Let see
1044         how they are used.
1045         </p>
1046         <p>
1047         To draw a point, or several points, we use
1048         </p>
1049         <pre class="code">
1050 xcb_void_cookie_t xcb_poly_point (xcb_connection_t  *c,               /* The connection to the X server */
1051                                   uint8_t            coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */
1052                                   xcb_drawable_t     drawable,        /* The drawable on which we want to draw the point(s) */
1053                                   xcb_gcontext_t     gc,              /* The Graphic Context we use to draw the point(s) */
1054                                   uint32_t           points_len,      /* The number of points */
1055                                   const xcb_point_t *points);         /* An array of points */
1056 </pre>
1057         <p>
1058         The <span class="code">coordinate_mode</span> parameter
1059         specifies the coordinate mode.  Available values are
1060         </p>
1061         <ul>
1062           <li><span class="code">XCB_COORD_MODE_ORIGIN</span>
1063           <li><span class="code">XCB_COORD_MODE_PREVIOUS</span>
1064         </ul>
1065         <p>
1066         If XCB_COORD_MODE_PREVIOUS is used, then all points but the first one
1067         are relative to the immediately previous point.
1068         </p>
1069         <p>
1070         The <span class="code">xcb_point_t</span> type is just a
1071         structure with two fields (the coordinates of the point):
1072         </p>
1073         <pre class="code">
1074 typedef struct {
1075     int16_t x;
1076     int16_t y;
1077 } xcb_point_t;
1078 </pre>
1079         <p>
1080         You could see an example in xpoints.c. <b>TODO</b> Set the link.
1081         </p>
1082         <p>
1083         To draw a line, or a polygonal line, we use
1084         </p>
1085         <pre class="code">
1086 xcb_void_cookie_t xcb_poly_line (xcb_connection_t  *c,               /* The connection to the X server */
1087                                  uint8_t            coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */
1088                                  xcb_drawable_t     drawable,        /* The drawable on which we want to draw the line(s) */
1089                                  xcb_gcontext_t     gc,              /* The Graphic Context we use to draw the line(s) */
1090                                  uint32_t           points_len,      /* The number of points in the polygonal line */
1091                                  const xcb_point_t *points);         /* An array of points */
1092 </pre>
1093         <p>
1094         This function will draw the line between the first and the
1095         second points, then the line between the second and the third
1096         points, and so on.
1097         </p>
1098         <p>
1099         To draw a segment, or several segments, we use
1100         </p>
1101         <pre class="code">
1102 xcb_void_cookie_t xcb_poly_segment (xcb_connection_t    *c,              /* The connection to the X server */
1103                                     xcb_drawable_t       drawable,       /* The drawable on which we want to draw the segment(s) */
1104                                     xcb_gcontext_t       gc,             /* The Graphic Context we use to draw the segment(s) */
1105                                     uint32_t             segments_len,   /* The number of segments */
1106                                     const xcb_segment_t *segments);      /* An array of segments */
1107 </pre>
1108         <p>
1109         The <span class="code">xcb_segment_t</span> type is just a
1110         structure with four fields (the coordinates of the two points
1111         that define the segment):
1112         </p>
1113         <pre class="code">
1114 typedef struct {
1115     int16_t x1;
1116     int16_t y1;
1117     int16_t x2;
1118     int16_t y2;
1119 } xcb_segment_t;
1120 </pre>
1121         <p>
1122         To draw a rectangle, or several rectangles, we use
1123         </p>
1124         <pre class="code">
1125 xcb_void_cookie_t xcb_poly_rectangle (xcb_connection_t      *c,              /* The connection to the X server */
1126                                       xcb_drawable_t         drawable,       /* The drawable on which we want to draw the rectangle(s) */
1127                                       xcb_gcontext_t         gc,             /* The Graphic Context we use to draw the rectangle(s) */
1128                                       uint32_t               rectangles_len, /* The number of rectangles */
1129                                       const xcb_rectangle_t *rectangles);    /* An array of rectangles */
1130 </pre>
1131         <p>
1132         The <span class="code">xcb_rectangle_t</span> type is just a
1133         structure with four fields (the coordinates of the top-left
1134         corner of the rectangle, and its width and height):
1135         </p>
1136         <pre class="code">
1137 typedef struct {
1138     int16_t  x;
1139     int16_t  y;
1140     uint16_t width;
1141     uint16_t height;
1142 } xcb_rectangle_t;
1143 </pre>
1144         <!-- There's no coordinate_mode. Is it normal? -->
1145         <!-- [iano] Yes, it's not in the protocol. -->
1146         <p>
1147         To draw an elliptical arc, or several elliptical arcs, we use
1148         </p>
1149         <pre class="code">
1150 xcb_void_cookie_t xcb_poly_arc (xcb_connection_t *c,          /* The connection to the X server */
1151                                 xcb_drawable_t    drawable,   /* The drawable on which we want to draw the arc(s) */
1152                                 xcb_gcontext_t    gc,         /* The Graphic Context we use to draw the arc(s) */
1153                                 uint32_t          arcs_len,   /* The number of arcs */
1154                                 const xcb_arc_t  *arcs);      /* An array of arcs */
1155 </pre>
1156         <p>
1157         The <span class="code">xcb_arc_t</span> type is a structure with
1158         six fields:
1159         </p>
1160         <pre class="code">
1161 typedef struct {
1162     int16_t  x;       /* Top left x coordinate of the rectangle surrounding the ellipse */
1163     int16_t  y;       /* Top left y coordinate of the rectangle surrounding the ellipse */
1164     uint16_t width;   /* Width of the rectangle surrounding the ellipse */
1165     uint16_t height;  /* Height of the rectangle surrounding the ellipse */
1166     int16_t  angle1;  /* Angle at which the arc begins */
1167     int16_t  angle2;  /* Angle at which the arc ends */
1168 } xcb_arc_t;
1169 </pre>
1170         <div class="emph">
1171         <p>
1172         Note: the angles are expressed in units of 1/64 of a degree,
1173         so to have an angle of 90 degrees, starting at 0,
1174         <span class="code">angle1 = 0</span> and
1175         <span class="code">angle2 = 90 &lt;&lt; 6</span>. Positive angles
1176         indicate counterclockwise motion, while  negative angles
1177         indicate clockwise motion.
1178         </p>
1179         </div>
1180         <!-- I think that (x,y) should be the center of the
1181         ellipse, and (width, height) the radius. It's more logical. -->
1182         <!-- iano: Yes, and I bet some toolkits do that.
1183          But the protocol (and many other graphics APIs) define arcs
1184          by bounding rectangles. -->
1185         <p>
1186         The corresponding function which fill inside the geometrical
1187         object are listed below, without  further explanation, as they
1188         are used as the above functions.
1189         </p>
1190         <p>
1191         To Fill a polygon defined by the points given as arguments ,
1192         we use
1193         </p>
1194         <pre class="code">
1195 xcb_void_cookie_t xcb_fill_poly (xcb_connection_t  *c,
1196                                  xcb_drawable_t     drawable,
1197                                  xcb_gcontext_t     gc,
1198                                  uint8_t            shape,
1199                                  uint8_t            coordinate_mode,
1200                                  uint32_t           points_len,
1201                                  const xcb_point_t *points);
1202 </pre>
1203         <p>
1204         The <span class="code">shape</span> parameter specifies a
1205         shape that helps the server to improve performance. Available
1206         values are
1207         </p>
1208         <ul>
1209           <li><span class="code">XCB_POLY_SHAPE_COMPLEX</span>
1210           <li><span class="code">XCB_POLY_SHAPE_NONCONVEX</span>
1211           <li><span class="code">XCB_POLY_SHAPE_CONVEX</span>
1212         </ul>
1213         <p>
1214         To fill one or several rectangles, we use
1215         </p>
1216         <pre class="code">
1217 xcb_void_cookie_t xcb_poly_fill_rectangle (xcb_connection_t      *c,
1218                                            xcb_drawable_t         drawable,
1219                                            xcb_gcontext_t         gc,
1220                                            uint32_t               rectangles_len,
1221                                            const xcb_rectangle_t *rectangles);
1222 </pre>
1223         <p>
1224         To fill one or several arcs, we use
1225         </p>
1226         <pre class="code">
1227 xcb_void_cookie_t xcb_poly_fill_arc (xcb_connection_t *c,
1228                                      xcb_drawable_t    drawable,
1229                                      xcb_gcontext_t    gc,
1230                                      uint32_t          arcs_len,
1231                                      const xcb_arc_t  *arcs);
1232 </pre>
1233         <br>
1234         <a name="points.c"></a>
1235         <p>
1236         To illustrate these functions, here is an example that draws
1237         four points, a polygonal line, two segments, two rectangles
1238         and two arcs. Remark that we use events for the first time, as
1239         an introduction to the next section.
1240         </p>
1241         <p>
1242         <b>TODO:</b> Use screen-&gt;root_depth for depth parameter.
1243         </p>
1244         <pre class="code">
1245 #include &lt;stdlib.h&gt;
1246 #include &lt;stdio.h&gt;
1247
1248 #include &lt;xcb/xcb.h&gt;
1249
1250 int
1251 main ()
1252 {
1253   xcb_connection_t    *c;
1254   xcb_screen_t        *screen;
1255   xcb_drawable_t       win;
1256   xcb_gcontext_t       foreground;
1257   xcb_generic_event_t *e;
1258   uint32_t             mask = 0;
1259   uint32_t             values[2];
1260
1261   /* geometric objects */
1262   xcb_point_t          points[] = {
1263     {10, 10},
1264     {10, 20},
1265     {20, 10},
1266     {20, 20}};
1267
1268   xcb_point_t          polyline[] = {
1269     {50, 10},
1270     { 5, 20},     /* rest of points are relative */
1271     {25,-20},
1272     {10, 10}};
1273
1274   xcb_segment_t        segments[] = {
1275     {100, 10, 140, 30},
1276     {110, 25, 130, 60}};
1277
1278   xcb_rectangle_t      rectangles[] = {
1279     { 10, 50, 40, 20},
1280     { 80, 50, 10, 40}};
1281
1282   xcb_arc_t            arcs[] = {
1283     {10, 100, 60, 40, 0, 90 &lt;&lt; 6},
1284     {90, 100, 55, 40, 0, 270 &lt;&lt; 6}};
1285
1286   /* Open the connection to the X server */
1287   c = xcb_connect (NULL, NULL);
1288
1289   /* Get the first screen */
1290   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
1291
1292   /* Create black (foreground) graphic context */
1293   win = screen-&gt;root;
1294
1295   foreground = xcb_generate_id (c);
1296   mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
1297   values[0] = screen-&gt;black_pixel;
1298   values[1] = 0;
1299   xcb_create_gc (c, foreground, win, mask, values);
1300
1301   /* Ask for our window's Id */
1302   win = xcb_generate_id(c);
1303
1304   /* Create the window */
1305   mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1306   values[0] = screen-&gt;white_pixel;
1307   values[1] = XCB_EVENT_MASK_EXPOSURE;
1308   xcb_create_window (c,                             /* Connection          */
1309                      XCB_COPY_FROM_PARENT,          /* depth               */
1310                      win,                           /* window Id           */
1311                      screen-&gt;root,                  /* parent window       */
1312                      0, 0,                          /* x, y                */
1313                      150, 150,                      /* width, height       */
1314                      10,                            /* border_width        */
1315                      XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
1316                      screen-&gt;root_visual,           /* visual              */
1317                      mask, values);                 /* masks */
1318
1319   /* Map the window on the screen */
1320   xcb_map_window (c, win);
1321
1322
1323   /* We flush the request */
1324   xcb_flush (c);
1325
1326   while ((e = xcb_wait_for_event (c))) {
1327     switch (e-&gt;response_type &amp; ~0x80) {
1328     case XCB_EXPOSE: {
1329       /* We draw the points */
1330       xcb_poly_point (c, XCB_COORD_MODE_ORIGIN, win, foreground, 4, points);
1331
1332       /* We draw the polygonal line */
1333       xcb_poly_line (c, XCB_COORD_MODE_PREVIOUS, win, foreground, 4, polyline);
1334
1335       /* We draw the segements */
1336       xcb_poly_segment (c, win, foreground, 2, segments);
1337
1338       /* We draw the rectangles */
1339       xcb_poly_rectangle (c, win, foreground, 2, rectangles);
1340
1341       /* We draw the arcs */
1342       xcb_poly_arc (c, win, foreground, 2, arcs);
1343
1344       /* We flush the request */
1345       xcb_flush (c);
1346
1347       break;
1348     }
1349     default: {
1350       /* Unknown event type, ignore it */
1351       break;
1352     }
1353     }
1354     /* Free the Generic Event */
1355     free (e);
1356   }
1357
1358   return 0;
1359 }
1360 </pre>
1361       </ol>
1362       <li class="title"><a name="xevents">X Events</a>
1363       <p>
1364       In an X program, everything is driven by events. Event painting
1365       on the screen is sometimes done as a response to an event (an
1366       <span class="code">Expose</span> event). If part of a program's
1367       window that was hidden, gets exposed (e.g. the window was raised
1368       above other widows), the X server will send an "expose" event to
1369       let the program know it should repaint that part of the
1370       window. User input (key presses, mouse movement, etc) is also
1371       received as a set of events.
1372       </p>
1373       <ol>
1374         <li class="subtitle"><a name="register">Registering for event types using event masks</a>
1375         <p>
1376         During the creation of a window, you should give it what kind
1377         of events it wishes to receive. Thus, you may register for
1378         various mouse (also called pointer) events, keyboard events,
1379         expose events, and so on. This is done for optimizing the
1380         server-to-client connection (i.e. why send a program (that
1381         might even be running at the other side of the globe) an event
1382         it is not interested in ?)
1383         </p>
1384         <p>
1385         In XCB, you use the "value_mask" and "value_list" data in the
1386         <span class="code">xcb_create_window()</span> function to
1387         register for events. Here is how we register for
1388         <span class="code">Expose</span> event when creating a window:
1389         </p>
1390         <pre class="code">
1391   mask = XCB_CW_EVENT_MASK;
1392   valwin[0] = XCB_EVENT_MASK_EXPOSURE;
1393   win = xcb_generate_id (c);
1394   xcb_create_window (c, depth, win, root-&gt;root,
1395                      0, 0, 150, 150, 10,
1396                      XCB_WINDOW_CLASS_INPUT_OUTPUT, root-&gt;root_visual,
1397                      mask, valwin);
1398 </pre>
1399         <p>
1400         <span class="code">XCB_EVENT_MASK_EXPOSURE</span> is a constant defined
1401         in the xcb_event_mask_t enumeration in the "xproto.h" header file. If we wanted to register for several
1402         event types, we can logically "or" them, as follows:
1403         </p>
1404         <pre class="code">
1405   mask = XCB_CW_EVENT_MASK;
1406   valwin[0] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS;
1407   win = xcb_generate_id (c);
1408   xcb_create_window (c, depth, win, root-&gt;root,
1409                      0, 0, 150, 150, 10,
1410                      XCB_WINDOW_CLASS_INPUT_OUTPUT, root-&gt;root_visual,
1411                      mask, valwin);
1412 </pre>
1413         <p>
1414         This registers for <span class="code">Expose</span> events as
1415         well as for mouse button presses inside the created
1416         window. You should note that a mask may represent several
1417         event sub-types.
1418         </p>
1419         <p>
1420         The values that a mask could take are given
1421         by the <span class="code">xcb_cw_t</span> enumeration:
1422         </p>
1423         <pre class="code">
1424 typedef enum {
1425     XCB_CW_BACK_PIXMAP       = 1L<<0,
1426     XCB_CW_BACK_PIXEL        = 1L<<1,
1427     XCB_CW_BORDER_PIXMAP     = 1L<<2,
1428     XCB_CW_BORDER_PIXEL      = 1L<<3,
1429     XCB_CW_BIT_GRAVITY       = 1L<<4,
1430     XCB_CW_WIN_GRAVITY       = 1L<<5,
1431     XCB_CW_BACKING_STORE     = 1L<<6,
1432     XCB_CW_BACKING_PLANES    = 1L<<7,
1433     XCB_CW_BACKING_PIXEL     = 1L<<8,
1434     XCB_CW_OVERRIDE_REDIRECT = 1L<<9,
1435     XCB_CW_SAVE_UNDER        = 1L<<10,
1436     XCB_CW_EVENT_MASK        = 1L<<11,
1437     XCB_CW_DONT_PROPAGATE    = 1L<<12,
1438     XCB_CW_COLORMAP          = 1L<<13,
1439     XCB_CW_CURSOR            = 1L<<14
1440 } xcb_cw_t;
1441 </pre>
1442         <div class="emph">
1443         <p>Note: we must be careful when setting the values of the valwin
1444         parameter, as they have to follow the order the
1445         <span class="code">xcb_cw_t</span> enumeration. Here is an
1446         example:
1447         </p>
1448         </div>
1449         <pre class="code">
1450   mask = XCB_CW_EVENT_MASK | XCB_CW_BACK_PIXMAP;
1451   valwin[0] = XCB_NONE;                                              /* for XCB_CW_BACK_PIXMAP (whose value is 1)     */
1452   valwin[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS; /* for XCB_CW_EVENT_MASK, whose value (2048)     */
1453                                                                      /* is greater than the one of XCB_CW_BACK_PIXMAP */
1454 </pre>
1455         <p>
1456         If the window has already been created, we can use the
1457         <span class="code">xcb_configure_window()</span> function to set
1458         the events that the window will receive. The subsection
1459         <a href="#winconf">Configuring a window</a> shows its
1460         prototype. As an example, here is a piece of code that
1461         configures the window to receive the
1462         <span class="code">Expose</span> and
1463         <span class="code">ButtonPress</span> events:
1464         </p>
1465         <pre class="code">
1466 const static uint32_t values[] = { XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS };
1467
1468 /* The connection c and the window win are supposed to be defined */
1469
1470 xcb_configure_window (c, win, XCB_CW_EVENT_MASK, values);
1471 </pre>
1472         <div class="emph">
1473         <p>
1474         Note: A common bug programmers do is adding code to handle new
1475         event types in their program, while forgetting to add the
1476         masks for these events in the creation of the window. Such a
1477         programmer then should sit down for hours debugging his
1478         program, wondering "Why doesn't my program notice that I
1479         released the button?", only to find that they registered for
1480         button press events but not for button release events.
1481         </p>
1482         </div>
1483         <li class="subtitle"><a name="loop">Receiving events: writing the events loop</a>
1484         <p>
1485         After we have registered for the event types we are interested
1486         in, we need to enter a loop of receiving events and handling
1487         them. There are two ways to receive events: a blocking way and
1488         a non-blocking way:
1489         </p>
1490         <ul>
1491           <li>
1492           <span class="code">xcb_wait_for_event (xcb_connection_t *c)</span>
1493           is the blocking way. It waits (so blocks...) until an event is
1494           queued in the X server. Then it retrieves it into a newly
1495           allocated structure (it dequeues it from the queue) and returns
1496           it. This structure has to be freed. The function returns
1497           <span class="code">NULL</span> if an error occurs.
1498
1499           <br>
1500           <li>
1501           <span class="code">xcb_poll_for_event (xcb_connection_t *c, int
1502           *error)</span> is the non-blocking way. It looks at the event
1503           queue and returns (and dequeues too) an existing event into
1504           a newly allocated structure. This structure has to be
1505           freed. It returns <span class="code">NULL</span> if there is
1506           no event. If an error occurs, the parameter <span
1507           class="code">error</span> will be filled with the error
1508           status.
1509         </ul>
1510         <p>
1511         There are various ways to write such a loop. We present two
1512         ways to write such a loop, with the two functions above. The
1513         first one uses <span class="code">xcb_wait_for_event_t</span>, which
1514         is similar to an event Xlib loop using only <span
1515         class="code">XNextEvent</span>:
1516         </p>
1517         <pre class="code">
1518   xcb_generic_event_t *e;
1519
1520   while ((e = xcb_wait_for_event (c))) {
1521     switch (e-&gt;response_type &amp; ~0x80) {
1522     case XCB_EXPOSE: {
1523       /* Handle the Expose event type */
1524       xcb_expose_event_t *ev = (xcb_expose_event_t *)e;
1525
1526       /* ... */
1527
1528       break;
1529     }
1530     case XCB_BUTTON_PRESS: {
1531       /* Handle the ButtonPress event type */
1532       xcb_button_press_event_t *ev = (xcb_button_press_event_t *)e;
1533
1534       /* ... */
1535
1536       break;
1537     }
1538     default: {
1539       /* Unknown event type, ignore it */
1540       break;
1541     }
1542     }
1543     /* Free the Generic Event */
1544     free (e);
1545   }
1546 </pre>
1547         <p>
1548         You will certainly want to use <span
1549         class="code">xcb_poll_for_event(xcb_connection_t *c, int
1550         *error)</span> if, in Xlib, you use <span
1551         class="code">XPending</span> or
1552         <span class="code">XCheckMaskEvent</span>:
1553         </p>
1554         <pre class="code">
1555   while (XPending (display)) {
1556     XEvent ev;
1557
1558     XNextEvent(d, &amp;ev);
1559
1560     /* Manage your event */
1561   }
1562 </pre>
1563         <p>
1564         Such a loop in XCB looks like:
1565         </p>
1566         <pre class="code">
1567   xcb_generic_event_t *ev;
1568
1569   while ((ev = xcb_poll_for_event (conn, 0))) {
1570     /* Manage your event */
1571   }
1572 </pre>
1573         <p>
1574         The events are managed in the same way as with <span
1575         class="code">xcb_wait_for_event_t</span>.
1576         Obviously, we will need to give the user some way of
1577         terminating the program. This is usually done by handling a
1578         special "quit" event, as we will soon see.
1579         </p>
1580         <div class="comp">
1581           <div class="title">
1582             Comparison Xlib/XCB
1583           </div>
1584           <div class="xlib">
1585             <ul>
1586               <li>XNextEvent ()
1587               </ul>
1588           </div>
1589           <div class="xcb">
1590             <ul>
1591               <li>xcb_wait_for_event ()
1592             </ul>
1593           </div>
1594           <div class="xlib">
1595             <ul>
1596               <li>XPending ()
1597               <li>XCheckMaskEvent ()
1598             </ul>
1599           </div>
1600           <div class="xcb">
1601             <ul>
1602               <li>xcb_poll_for_event ()
1603             </ul>
1604           </div>
1605         </div>
1606         <br>
1607         <li class="subtitle"><a name="expose">Expose events</a>
1608         <p>
1609         The <span class="code">Expose</span> event is one of the most
1610         basic (and most used) events an application may receive. It
1611         will be sent to us in one of several cases:
1612         </p>
1613         <ul>
1614           <li>A window that covered part of our window has moved
1615               away, exposing part (or all) of our window.
1616           <li>Our window was raised above other windows.
1617           <li>Our window mapped for the first time.
1618           <li>Our window was de-iconified.
1619         </ul>
1620         <p>
1621         You should note the implicit assumption hidden here: the
1622         contents of our window is lost when it is being obscured
1623         (covered) by either windows. One may wonder why the X server
1624         does not save this contents. The answer is: to save
1625         memory. After all, the number of windows on a display at a
1626         given time may be very large, and storing the contents of all
1627         of them might require a lot of memory. Actually, there is a
1628         way to tell the X server to store  the contents of a window in
1629         special cases, as we will see later.
1630         </p>
1631         <p>
1632         When we get an <span class="code">Expose</span> event, we
1633         should take the event's data from the members of the following
1634         structure:
1635         </p>
1636         <pre class="code">
1637 typedef struct {
1638     uint8_t      response_type; /* The type of the event, here it is XCB_EXPOSE */
1639     uint8_t      pad0;
1640     uint16_t     sequence;
1641     xcb_window_t window;        /* The Id of the window that receives the event (in case */
1642                                 /* our application registered for events on several windows */
1643     uint16_t     x;             /* The x coordinate of the top-left part of the window that needs to be redrawn */
1644     uint16_t     y;             /* The y coordinate of the top-left part of the window that needs to be redrawn */
1645     uint16_t     width;         /* The width of the part of the window that needs to be redrawn */
1646     uint16_t     height;        /* The height of the part of the window that needs to be redrawn */
1647     uint16_t     count;
1648 } xcb_expose_event_t;
1649 </pre>
1650         <li class="subtitle"><a name="userinput">Getting user input</a>
1651         <p>
1652         User input traditionally comes from two sources: the mouse
1653         and the keyboard. Various event types exist to notify us of
1654         user input (a key being presses on the keyboard, a key being
1655         released on the keyboard, the mouse moving over our window,
1656         the mouse entering (or leaving) our window, and so on.
1657         </p>
1658         <ol>
1659             <li class="subsubtitle"><a name="mousepressrelease">Mouse button press and release events</a>
1660           <p>
1661           The first event type we will deal with is a mouse
1662           button-press (or button-release) event in our window. In
1663           order to register to such an event type, we should add one
1664           (or more) of the following masks when we create our window:
1665           </p>
1666           <ul>
1667             <li><span class="code">XCB_EVENT_MASK_BUTTON_PRESS</span>: notify us
1668             of any button that was pressed in one of our windows.
1669             <li><span class="code">XCB_EVENT_MASK_BUTTON_RELEASE</span>: notify us
1670             of any button that was released in one of our windows.
1671           </ul>
1672           <p>
1673           The structure to be checked for in our events loop is the
1674           same for these two events, and is the following:
1675           </p>
1676           <pre class="code">
1677 typedef struct {
1678     uint8_t         response_type; /* The type of the event, here it is xcb_button_press_event_t or xcb_button_release_event_t */
1679     xcb_button_t    detail;
1680     uint16_t        sequence;
1681     xcb_timestamp_t time;          /* Time, in milliseconds the event took place in */
1682     xcb_window_t    root;
1683     xcb_window_t    event;
1684     xcb_window_t    child;
1685     int16_t         root_x;
1686     int16_t         root_y;
1687     int16_t         event_x;       /* The x coordinate where the mouse has been pressed in the window */
1688     int16_t         event_y;       /* The y coordinate where the mouse has been pressed in the window */
1689     uint16_t        state;         /* A mask of the buttons (or keys) during the event */
1690     uint8_t         same_screen;
1691 } xcb_button_press_event_t;
1692
1693 typedef xcb_button_press_event_t xcb_button_release_event_t;
1694 </pre>
1695           <p>
1696           The <span class="code">time</span> field may be used to calculate "double-click"
1697           situations by an application (e.g. if the mouse button was
1698           clicked two times in a duration shorter than a given amount
1699           of time, assume this was a double click).
1700           </p>
1701           <p>
1702           The <span class="code">state</span> field is a mask of the buttons held down during
1703           the event. It is a bitwise OR of any of the following (from the xcb_button_mask_t and
1704           xcb_mod_mask_t enumerations):
1705           </p>
1706           <ul>
1707             <li><span class="code">XCB_BUTTON_MASK_1</span>
1708             <li><span class="code">XCB_BUTTON_MASK_2</span>
1709             <li><span class="code">XCB_BUTTON_MASK_3</span>
1710             <li><span class="code">XCB_BUTTON_MASK_4</span>
1711             <li><span class="code">XCB_BUTTON_MASK_5</span>
1712             <li><span class="code">XCB_MOD_MASK_SHIFT</span>
1713             <li><span class="code">XCB_MOD_MASK_LOCK</span>
1714             <li><span class="code">XCB_MOD_MASK_CONTROL</span>
1715             <li><span class="code">XCB_MOD_MASK_1</span>
1716             <li><span class="code">XCB_MOD_MASK_2</span>
1717             <li><span class="code">XCB_MOD_MASK_3</span>
1718             <li><span class="code">XCB_MOD_MASK_4</span>
1719             <li><span class="code">XCB_MOD_MASK_5</span>
1720           </ul>
1721           <p>
1722           Their names are self explanatory, where the first 5 refer to
1723           the mouse buttons that are being pressed, while the rest
1724           refer to various "special keys" that are being pressed (Mod1
1725           is usually the 'Alt' key or the 'Meta' key).
1726           </p>
1727           <p>
1728           <b>TODO:</b> Problem: it seems that the state does not
1729           change when clicking with various buttons.
1730           </p>
1731             <li class="subsubtitle"><a name="mousemvnt">Mouse movement events</a>
1732           <p>
1733           Similar to mouse button press and release events, we also
1734           can be notified of various mouse movement events. These can
1735           be split into two families. One is of mouse pointer
1736           movement while no buttons are pressed, and the second is a
1737           mouse pointer motion while one (or more) of the buttons are
1738           pressed (this is sometimes called "a mouse drag operation",
1739           or just "dragging"). The following event masks may be added
1740           during the creation of our window:
1741           </p>
1742           <ul>
1743             <li><span class="code">XCB_EVENT_MASK_POINTER_MOTION</span>: events of
1744             the pointer moving in one of the windows controlled by our
1745             application, while no mouse button is held pressed.
1746             <li><span class="code">XCB_EVENT_MASK_BUTTON_MOTION</span>: Events of
1747             the pointer moving while one or more of the mouse buttons
1748             is held pressed.
1749             <li><span class="code">XCB_EVENT_MASK_BUTTON_1_MOTION</span>: same as
1750             <span class="code">XCB_EVENT_MASK_BUTTON_MOTION</span>, but only when
1751             the 1st mouse button is held pressed.
1752             <li><span class="code">XCB_EVENT_MASK_BUTTON_2_MOTION</span>,
1753             <span class="code">XCB_EVENT_MASK_BUTTON_3_MOTION</span>,
1754             <span class="code">XCB_EVENT_MASK_BUTTON_4_MOTION</span>,
1755             <span class="code">XCB_EVENT_MASK_BUTTON_5_MOTION</span>: same as
1756             <span class="code">XCB_EVENT_MASK_BUTTON_1_MOTION</span>, but
1757             respectively for 2nd, 3rd, 4th and 5th mouse button.
1758           </ul>
1759           <p>
1760           The structure to be checked for in our events loop is the
1761           same for these events, and is the following:
1762           </p>
1763           <pre class="code">
1764 typedef struct {
1765     uint8_t         response_type; /* The type of the event */
1766     uint8_t         detail;
1767     uint16_t        sequence;
1768     xcb_timestamp_t time;          /* Time, in milliseconds the event took place in */
1769     xcb_window_t    root;
1770     xcb_window_t    event;
1771     xcb_window_t    child;
1772     int16_t         root_x;
1773     int16_t         root_y;
1774     int16_t         event_x;       /* The x coordinate of the mouse when the  event was generated */
1775     int16_t         event_y;       /* The y coordinate of the mouse when the  event was generated */
1776     uint16_t        state;         /* A mask of the buttons (or keys) during the event */
1777     uint8_t         same_screen;
1778 } xcb_motion_notify_event_t;
1779 </pre>
1780             <li class="subsubtitle"><a name="mouseenter">Mouse pointer enter and leave events</a>
1781           <p>
1782           Another type of event that applications might be interested
1783           in, is a mouse pointer entering a window the program
1784           controls, or leaving such a window. Some programs use these
1785           events to show the user that the application is now in
1786           focus. In order to register for such an event type, we
1787           should add one (or more) of the following masks when we
1788           create our window:
1789           </p>
1790           <ul>
1791             <li><span class="code">xcb_event_enter_window_t</span>: notify us
1792             when the mouse pointer enters any of our controlled
1793             windows.
1794             <li><span class="code">xcb_event_leave_window_t</span>: notify us
1795             when the mouse pointer leaves any of our controlled
1796             windows.
1797           </ul>
1798           <p>
1799           The structure to be checked for in our events loop is the
1800           same for these two events, and is the following:
1801           </p>
1802           <pre class="code">
1803 typedef struct {
1804     uint8_t         response_type; /* The type of the event */
1805     uint8_t         detail;
1806     uint16_t        sequence;
1807     xcb_timestamp_t time;          /* Time, in milliseconds the event took place in */
1808     xcb_window_t    root;
1809     xcb_window_t    event;
1810     xcb_window_t    child;
1811     int16_t         root_x;
1812     int16_t         root_y;
1813     int16_t         event_x;       /* The x coordinate of the mouse when the  event was generated */
1814     int16_t         event_y;       /* The y coordinate of the mouse when the  event was generated */
1815     uint16_t        state;         /* A mask of the buttons (or keys) during the event */
1816     uint8_t         mode;          /* The number of mouse button that was clicked */
1817     uint8_t         same_screen_focus;
1818 } xcb_enter_notify_event_t;
1819
1820 typedef xcb_enter_notify_event_t xcb_leave_notify_event_t;
1821 </pre>
1822           <li class="subsubtitle"><a name="focus">The keyboard focus</a>
1823           <p>
1824           There may be many windows on a screen, but only a single
1825           keyboard attached to them. How does the X server then know
1826           which window should be sent a given keyboard input ? This is
1827           done using the keyboard focus. Only a single window on the
1828           screen may have the keyboard focus at a given time. There
1829           is a XCB function that allows a program to set the keyboard
1830           focus to a given window. The user can usually set the
1831           keyboard focus using the window manager (often by clicking
1832           on the title bar of the desired window). Once our window
1833           has the keyboard focus, every key press or key release will
1834           cause an event to be sent to our program (if it regsitered
1835           for these event types...).
1836           </p>
1837           <li class="subsubtitle"><a name="keypress">Keyboard press and release events</a>
1838           <p>
1839           If a window controlled by our program currently holds the
1840           keyboard focus, it can receive key press and key release
1841           events. So, we should add one (or more) of the following
1842           masks when we create our window:
1843           </p>
1844           <ul>
1845             <li><span class="code">XCB_EVENT_MASK_KEY_PRESS</span>: notify us when
1846             a key was pressed while any of our controlled windows had
1847             the keyboard focus.
1848             <li><span class="code">XCB_EVENT_MASK_KEY_RELEASE</span>: notify us
1849             when a key was released while any of our controlled
1850             windows had the keyboard focus.
1851           </ul>
1852           <p>
1853           The structure to be checked for in our events loop is the
1854           same for these two events, and is the following:
1855           </p>
1856           <pre class="code">
1857 typedef struct {
1858     uint8_t         response_type; /* The type of the event */
1859     xcb_keycode_t   detail;
1860     uint16_t        sequence;
1861     xcb_timestamp_t time;          /* Time, in milliseconds the event took place in */
1862     xcb_window_t    root;
1863     xcb_window_t    event;
1864     xcb_window_t    child;
1865     int16_t         root_x;
1866     int16_t         root_y;
1867     int16_t         event_x;
1868     int16_t         event_y;
1869     uint16_t        state;
1870     uint8_t         same_screen;
1871 } xcb_key_press_event_t;
1872
1873 typedef xcb_key_press_event_t xcb_key_release_event_t;
1874 </pre>
1875         <p>
1876         The <span class="code">detail</span> field refers to the
1877         physical key on the keyboard.
1878         </p>
1879         <p>
1880         <b>TODO:</b> Talk about getting the ASCII code from the key code.
1881         </p>
1882         </ol>
1883         <li class="subtitle"><a name="eventex">X events: a complete example</a>
1884         <p>
1885         As an example for handling events, we show a program that
1886         creates a window, enters an events loop and checks for all the
1887         events described above, and writes on the terminal the relevant
1888         characteristics of the event. With this code, it should be
1889         easy to add drawing operations, like those which have been
1890         described above.
1891         </p>
1892         <pre class="code">
1893 #include &lt;stdlib.h&gt;
1894 #include &lt;stdio.h&gt;
1895
1896 #include &lt;xcb/xcb.h&gt;
1897
1898 void
1899 print_modifiers (uint32_t mask)
1900 {
1901   const char **mod, *mods[] = {
1902     "Shift", "Lock", "Ctrl", "Alt",
1903     "Mod2", "Mod3", "Mod4", "Mod5",
1904     "Button1", "Button2", "Button3", "Button4", "Button5"
1905   };
1906   printf ("Modifier mask: ");
1907   for (mod = mods ; mask; mask &gt;&gt;= 1, mod++)
1908     if (mask &amp; 1)
1909       printf(*mod);
1910   putchar ('\n');
1911 }
1912
1913 int
1914 main ()
1915 {
1916   xcb_connection_t    *c;
1917   xcb_screen_t        *screen;
1918   xcb_window_t         win;
1919   xcb_generic_event_t *e;
1920   uint32_t             mask = 0;
1921   uint32_t             values[2];
1922
1923   /* Open the connection to the X server */
1924   c = xcb_connect (NULL, NULL);
1925
1926   /* Get the first screen */
1927   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
1928
1929   /* Ask for our window's Id */
1930   win = xcb_generate_id (c);
1931
1932   /* Create the window */
1933   mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1934   values[0] = screen-&gt;white_pixel;
1935   values[1] = XCB_EVENT_MASK_EXPOSURE       | XCB_EVENT_MASK_BUTTON_PRESS   |
1936               XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION |
1937               XCB_EVENT_MASK_ENTER_WINDOW   | XCB_EVENT_MASK_LEAVE_WINDOW   |
1938               XCB_EVENT_MASK_KEY_PRESS      | XCB_EVENT_MASK_KEY_RELEASE;
1939   xcb_create_window (c,                             /* Connection          */
1940                      0,                             /* depth               */
1941                      win,                           /* window Id           */
1942                      screen-&gt;root,                  /* parent window       */
1943                      0, 0,                          /* x, y                */
1944                      150, 150,                      /* width, height       */
1945                      10,                            /* border_width        */
1946                      XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
1947                      screen-&gt;root_visual,           /* visual              */
1948                      mask, values);                 /* masks */
1949
1950   /* Map the window on the screen */
1951   xcb_map_window (c, win);
1952
1953   xcb_flush (c);
1954
1955   while ((e = xcb_wait_for_event (c))) {
1956     switch (e-&gt;response_type &amp; ~0x80) {
1957     case XCB_EXPOSE: {
1958       xcb_expose_event_t *ev = (xcb_expose_event_t *)e;
1959
1960       printf ("Window %ld exposed. Region to be redrawn at location (%d,%d), with dimension (%d,%d)\n",
1961               ev-&gt;window.xid, ev-&gt;x, ev-&gt;y, ev-&gt;width, ev-&gt;height);
1962       break;
1963     }
1964     case XCB_BUTTON_PRESS: {
1965       xcb_button_press_event_t *ev = (xcb_button_press_event_t *)e;
1966       print_modifiers(ev-&gt;state);
1967
1968       switch (ev-&gt;detail.id) {
1969       case 4:
1970         printf ("Wheel Button up in window %ld, at coordinates (%d,%d)\n",
1971                 ev-&gt;event.xid, ev-&gt;event_x, ev-&gt;event_y);
1972         break;
1973       case 5:
1974         printf ("Wheel Button down in window %ld, at coordinates (%d,%d)\n",
1975                 ev-&gt;event.xid, ev-&gt;event_x, ev-&gt;event_y);
1976         break;
1977       default:
1978         printf ("Button %d pressed in window %ld, at coordinates (%d,%d)\n",
1979                 ev-&gt;detail.id, ev-&gt;event.xid, ev-&gt;event_x, ev-&gt;event_y);
1980       }
1981       break;
1982     }
1983     case XCB_BUTTON_RELEASE: {
1984       xcb_button_release_event_t *ev = (xcb_button_release_event_t *)e;
1985       print_modifiers(ev-&gt;state);
1986
1987       printf ("Button %d released in window %ld, at coordinates (%d,%d)\n",
1988               ev-&gt;detail.id, ev-&gt;event.xid, ev-&gt;event_x, ev-&gt;event_y);
1989       break;
1990     }
1991     case XCB_MOTION_NOTIFY: {
1992       xcb_motion_notify_event_t *ev = (xcb_motion_notify_event_t *)e;
1993
1994       printf ("Mouse moved in window %ld, at coordinates (%d,%d)\n",
1995               ev-&gt;event.xid, ev-&gt;event_x, ev-&gt;event_y);
1996       break;
1997     }
1998     case XCB_ENTER_NOTIFY: {
1999       xcb_enter_notify_event_t *ev = (xcb_enter_notify_event_t *)e;
2000
2001       printf ("Mouse entered window %ld, at coordinates (%d,%d)\n",
2002               ev-&gt;event.xid, ev-&gt;event_x, ev-&gt;event_y);
2003       break;
2004     }
2005     case XCB_LEAVE_NOTIFY: {
2006       xcb_leave_notify_event_t *ev = (xcb_leave_notify_event_t *)e;
2007
2008       printf ("Mouse left window %ld, at coordinates (%d,%d)\n",
2009               ev-&gt;event.xid, ev-&gt;event_x, ev-&gt;event_y);
2010       break;
2011     }
2012     case XCB_KEY_PRESS: {
2013       xcb_key_press_event_t *ev = (xcb_key_press_event_t *)e;
2014       print_modifiers(ev-&gt;state);
2015
2016       printf ("Key pressed in window %ld\n",
2017               ev-&gt;event.xid);
2018       break;
2019     }
2020     case XCB_KEY_RELEASE: {
2021       xcb_key_release_event_t *ev = (xcb_key_release_event_t *)e;
2022       print_modifiers(ev-&gt;state);
2023
2024       printf ("Key released in window %ld\n",
2025               ev-&gt;event.xid);
2026       break;
2027     }
2028     default:
2029       /* Unknown event type, ignore it */
2030       printf("Unknown event: %d\n", e-&gt;response_type);
2031       break;
2032     }
2033     /* Free the Generic Event */
2034     free (e);
2035   }
2036
2037   return 0;
2038 }
2039 </pre>
2040       </ol>
2041       <li class="title"><a name="font">Handling text and fonts</a>
2042       <p>
2043       Besides drawing graphics on a window, we often want to draw
2044       text. Text strings have two major properties: the characters to
2045       be drawn and the font with which they are drawn. In order to
2046       draw text, we need to first request the X server to load a
2047       font. We then assign a font to a Graphic Context, and finally, we
2048       draw the text in a window, using the Graphic Context.
2049       </p>
2050       <ol>
2051         <li class="subtitle"><a name="fontstruct">The Font structure</a>
2052         <p>
2053         In order to support flexible fonts, a font structure is
2054         defined. You know what ? It's an Id:
2055         </p>
2056         <pre class="code">
2057 typedef struct {
2058     uint32_t xid;
2059 } xcb_font_t;
2060 </pre>
2061         <p>
2062         It is used to contain information about a font, and is passed
2063         to several functions that handle fonts selection and text drawing.
2064         </p>
2065         <p>
2066         <b>TODO:</b> example for picking a font and displaying some text.
2067         Even better, also demonstrate translating keypresses to text.
2068         </p>
2069       </ol>
2070       <li class="title"><a name="wm">Interacting with the window manager</a>
2071       <p>
2072       After we have seen how to create windows and draw on them, we
2073       take one step back, and look at how our windows are interacting
2074       with their environment (the full screen and the other
2075       windows). First of all, our application needs to interact with
2076       the window manager. The window manager is responsible to
2077       decorating drawn windows (i.e. adding a frame, an iconify
2078       button, a system menu, a title bar, etc), as well as handling
2079       icons shown when windows are being iconified. It also handles
2080       ordering of windows on the screen, and other administrative
2081       tasks. We need to give it various hints as to how we want it to
2082       treat our application's windows.
2083       </p>
2084       <ol>
2085         <li class="subtitle"><a name="wmprop">Window properties</a>
2086         <p>
2087         Many of the parameters communicated to the window manager are
2088         passed using data called "properties". These properties are
2089         attached by the X server to different windows, and are stored
2090         in a format that makes it possible to read them from different
2091         machines that may use different architectures (remember that
2092         an X client program may run on a remote machine).
2093         </p>
2094         <p>
2095         The property and its type (a string, an integer, etc) are
2096         Id. Their type are <span class="code">xcb_atom_t</span>:
2097         </p>
2098         <pre class="code">
2099 typedef struct {
2100     uint32_t xid;
2101 } xcb_atom_t;
2102 </pre>
2103         <p>
2104         To change the property of a window, we use the following
2105         function:
2106         </p>
2107         <pre class="code">
2108 xcb_void_cookie_t xcb_change_property (xcb_connection_t *c,       /* Connection to the X server */
2109                                        uint8_t          mode,     /* Property mode */
2110                                        xcb_window_t     window,   /* Window */
2111                                        xcb_atom_t       property, /* Property to change */
2112                                        xcb_atom_t       type,     /* Type of the property */
2113                                        uint8_t          format,   /* Format of the property (8, 16, 32) */
2114                                        uint32_t         data_len, /* Length of the data parameter */
2115                                        const void      *data);    /* Data */
2116 </pre>
2117         <p>
2118         The <span class="code">mode</span> parameter coud be one of
2119         the following values (defined in enumeration xcb_prop_mode_t in
2120         the xproto.h header file):
2121         </p>
2122         <ul>
2123           <li>XCB_PROP_MODE_REPLACE
2124           <li>XCB_PROP_MODE_PREPEND
2125           <li>XCB_PROP_MODE_APPEND
2126         </ul>
2127         <br>
2128         <li class="subtitle"><a name="wmname">Setting the window name and icon name</a>
2129         <p>
2130         The first thing we want to do would be to set the name for our
2131         window. This is done using the
2132         <span class="code">xcb_change_property()</span> function. This
2133         name may be used by the window manager as the title of the
2134         window (in the title bar), in a task list, etc. The property
2135         atom to use to set the name of a window is
2136         <span class="code">WM_NAME</span> (and
2137         <span class="code">WM_ICON_NAME</span> for the iconified
2138         window) and its type is <span class="code">STRING</span>. Here
2139         is an example of utilization:
2140         </p>
2141         <pre class="code">
2142 #include &lt;string.h&gt;
2143
2144 #include &lt;xcb/xcb.h&gt;
2145 #include &lt;xcb/xcb_atom.h&gt;
2146
2147 int
2148 main ()
2149 {
2150   xcb_connection_t *c;
2151   xcb_screen_t     *screen;
2152   xcb_window_t      win;
2153   char             *title = "Hello World !";
2154   char             *title_icon = "Hello World ! (iconified)";
2155
2156
2157
2158   /* Open the connection to the X server */
2159   c = xcb_connect (NULL, NULL);
2160
2161   /* Get the first screen */
2162   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2163
2164   /* Ask for our window's Id */
2165   win = xcb_generate_id (c);
2166
2167   /* Create the window */
2168   xcb_create_window (c,                             /* Connection          */
2169                      0,                             /* depth               */
2170                      win,                           /* window Id           */
2171                      screen-&gt;root,                  /* parent window       */
2172                      0, 0,                          /* x, y                */
2173                      250, 150,                      /* width, height       */
2174                      10,                            /* border_width        */
2175                      XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
2176                      screen-&gt;root_visual,           /* visual              */
2177                      0, NULL);                      /* masks, not used     */
2178
2179   /* Set the title of the window */
2180   xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
2181                        WM_NAME, STRING, 8,
2182                        strlen (title), title);
2183
2184   /* Set the title of the window icon */
2185   xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
2186                        WM_ICON_NAME, STRING, 8,
2187                        strlen(title_icon), title_icon);
2188
2189   /* Map the window on the screen */
2190   xcb_map_window (c, win);
2191
2192   xcb_flush (c);
2193
2194   while (1) {}
2195
2196   return 0;
2197 }
2198 </pre>
2199         <div class="emph">
2200         <p>Note: the use of the atoms needs our program to be compiled
2201         and linked against xcb_atom, so that we have to use
2202         </p>
2203         </div>
2204         <pre class="text">
2205 gcc prog.c -o prog `pkg-config --cflags --libs xcb_atom`
2206 </pre>
2207         <div class="emph">
2208         <p>
2209         for the program to compile fine.
2210         </p>
2211         </div>
2212       </ol>
2213       <li class="title"><a name="winop">Simple window operations</a>
2214       <p>
2215       One more thing we can do to our window is manipulate them on the
2216       screen (resize them, move them, raise or lower them, iconify
2217       them, and so on). Some window operations functions are supplied
2218       by XCB for this purpose.
2219       </p>
2220       <ol>
2221         <li class="subtitle"><a name="winmap">Mapping and un-mapping a window</a>
2222         <p>
2223         The first pair of operations we can apply on a window is
2224         mapping it, or un-mapping it. Mapping a window causes the
2225         window to appear on the screen, as we have seen in our simple
2226         window program example. Un-mapping it causes it to be removed
2227         from the screen (although the window as a logical entity still
2228         exists). This gives the effect of making a window hidden
2229         (unmapped) and shown again (mapped). For example, if we have a
2230         dialog box window in our program, instead of creating it every
2231         time the user asks to open it, we can create the window once,
2232         in an un-mapped mode, and when the user asks to open it, we
2233         simply map the window on the screen. When the user clicked the
2234         'OK' or 'Cancel' button, we simply un-map the window. This is
2235         much faster than creating and destroying the window, however,
2236         the cost is wasted resources, both on the client side, and on
2237         the X server side.
2238         </p>
2239         <p>
2240         To map a window, you use the following function:
2241         </p>
2242         <pre class="code">
2243 xcb_void_cookie_t xcb_map_window (xcb_connection_t *c,
2244                                   xcb_window_t      window);
2245 </pre>
2246         <p>
2247         To have a simple example, see the <a href="#helloworld">example</a>
2248         above. The mapping operation will cause an
2249         <span class="code">Expose</span> event to be sent to our
2250         application, unless the window is completely covered by other
2251         windows.
2252         </p>
2253         <p>
2254         Un-mapping a window is also simple. You use the function
2255         </p>
2256         <pre class="code">
2257 xcb_void_cookie_t xcb_unmap_window (xcb_connection_t *c,
2258                                     xcb_window_t      window);
2259 </pre>
2260         <p>
2261         The utilization of this function is the same as
2262         <span class="code">xcb_map_window()</span>.
2263         </p>
2264         <li class="subtitle"><a name="winconf">Configuring a window</a>
2265         <p>
2266         As we have seen when we have created our first window, in the
2267         X Events subsection, we can set some attributes for the window
2268         (that is, the position, the size, the events the window will
2269         receive, etc). If we want to modify them, but the window is
2270         already created, we can change them by using the following
2271         function:
2272         </p>
2273         <pre class="code">
2274 xcb_void_cookie_t xcb_configure_window (xcb_connection_t *c,            /* The connection to the X server*/
2275                                         xcb_window_t      window,       /* The window to configure */
2276                                         uint16_t          value_mask,   /* The mask */
2277                                         const uint32_t   *value_list);  /* The values to set */
2278 </pre>
2279         <p>
2280         We set the <span class="code">value_mask</span> to one or
2281         several mask values that are in the xcb_config_window_t enumeration in the xproto.h header:
2282         </p>
2283         <ul>
2284           <li><span class="code">XCB_CONFIG_WINDOW_X</span>: new x coordinate of the window's top left corner
2285           <li><span class="code">XCB_CONFIG_WINDOW_Y</span>: new y coordinate of the window's top left corner
2286           <li><span class="code">XCB_CONFIG_WINDOW_WIDTH</span>: new width of the window
2287           <li><span class="code">XCB_CONFIG_WINDOW_HEIGHT</span>: new height of the window
2288           <li><span class="code">XCB_CONFIG_WINDOW_BORDER_WIDTH</span>: new width of the border of the window
2289           <li><span class="code">XCB_CONFIG_WINDOW_SIBLING</span>
2290           <li><span class="code">XCB_CONFIG_WINDOW_STACK_MODE</span>: the new stacking order
2291         </ul>
2292         <p>
2293         We then give to <span class="code">value_mask</span> the new
2294         value. We now describe how to use
2295         <span class="code">xcb_configure_window_t</span> in some useful
2296         situations.
2297         </p>
2298         <li class="subtitle"><a name="winmove">Moving a window around the screen</a>
2299         <p>
2300         An operation we might want to do with windows is to move them
2301         to a different location. This can be done like this:
2302         </p>
2303         <pre class="code">
2304 const static uint32_t values[] = { 10, 20 };
2305
2306 /* The connection c and the window win are supposed to be defined */
2307
2308 /* Move the window to coordinates x = 10 and y = 20 */
2309 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values);
2310 </pre>
2311         <p>
2312         Note that when the window is moved, it might get partially
2313         exposed or partially hidden by other windows, and thus we
2314         might get <span class="code">Expose</span> events due to this
2315         operation.
2316         </p>
2317         <li class="subtitle"><a name="winsize">Resizing a window</a>
2318         <p>
2319         Yet another operation we can do is to change the size of a
2320         window. This is done using the following code:
2321         </p>
2322         <pre class="code">
2323 const static uint32_t values[] = { 200, 300 };
2324
2325 /* The connection c and the window win are supposed to be defined */
2326
2327 /* Resize the window to width = 10 and height = 20 */
2328 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, values);
2329 </pre>
2330         <p>
2331         We can also combine the move and resize operations using one
2332         single call to <span class="code">xcb_configure_window_t</span>:
2333         </p>
2334         <pre class="code">
2335 const static uint32_t values[] = { 10, 20, 200, 300 };
2336
2337 /* The connection c and the window win are supposed to be defined */
2338
2339 /* Move the window to coordinates x = 10 and y = 20 */
2340 /* and resize the window to width = 10 and height = 20 */
2341 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, values);
2342 </pre>
2343         <li class="subtitle"><a name="winstack">Changing windows stacking order: raise and lower</a>
2344         <p>
2345         Until now, we changed properties of a single window. We'll see
2346         that there are properties that relate to the window and other
2347         windows. One of them is the stacking order. That is, the order
2348         in which the windows are layered on top of each other. The
2349         front-most window is said to be on the top of the stack, while
2350         the back-most window is at the bottom of the stack. Here is
2351         how to manipulate our windows stack order:
2352         </p>
2353         <pre class="code">
2354 const static uint32_t values[] = { XCB_STACK_MODE_ABOVE };
2355
2356 /* The connection c and the window win are supposed to be defined */
2357
2358 /* Move the window on the top of the stack */
2359 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
2360 </pre>
2361         <pre class="code">
2362 const static uint32_t values[] = { XCB_STACK_MODE_BELOW };
2363
2364 /* The connection c and the window win are supposed to be defined */
2365
2366 /* Move the window on the bottom of the stack */
2367 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
2368 </pre>
2369         <li class="subtitle"><a name="wingetinfo">Getting information about a window</a>
2370         <p>
2371         Just like we can set various attributes of our windows, we can
2372         also ask the X server supply the current values of these
2373         attributes. For example, we can check where a window is
2374         located on the screen, what is its current size, whether it is
2375         mapped or not, etc. The structure that contains some of this
2376         information is
2377         </p>
2378         <pre class="code">
2379 typedef struct {
2380     uint8_t      response_type;
2381     uint8_t      depth;         /* depth of the window */
2382     uint16_t     sequence;
2383     uint32_t     length;
2384     xcb_window_t root;          /* Id of the root window *>
2385     int16_t      x;             /* X coordinate of the window's location */
2386     int16_t      y;             /* Y coordinate of the window's location */
2387     uint16_t     width;         /* Width of the window */
2388     uint16_t     height;        /* Height of the window */
2389     uint16_t     border_width;  /* Width of the window's border */
2390 } xcb_get_geometry_reply_t;
2391 </pre>
2392         <p>
2393         XCB fill this structure with two functions:
2394         </p>
2395         <pre class="code">
2396 xcb_get_geometry_cookie_t  xcb_get_geometry       (xcb_connection_t         *c,
2397                                                    xcb_drawable_t            drawable);
2398 xcb_get_geometry_reply_t  *xcb_get_geometry_reply (xcb_connection_t         *c,
2399                                                    xcb_get_geometry_cookie_t cookie,
2400                                                    xcb_generic_error_t     **e);
2401 </pre>
2402         <p>
2403         You use them as follows:
2404         </p>
2405         <pre class="code">
2406   xcb_connection_t         *c;
2407   xcb_drawable_t            win;
2408   xcb_get_geometry_reply_t *geom;
2409
2410   /* You initialize c and win */
2411
2412   geom = xcb_get_geometry_reply (c, xcb_get_geometry (c, win), NULL);
2413
2414   /* Do something with the fields of geom */
2415
2416   free (geom);
2417 </pre>
2418         <p>
2419         Remark that you have to free the structure, as
2420         <span class="code">xcb_get_geometry_reply_t</span> allocates a
2421         newly one.
2422         </p>
2423         <p>
2424         One problem is that the returned location of the window is
2425         relative to its parent window. This makes these coordinates
2426         rather useless for any window manipulation functions, like
2427         moving it on the screen. In order to overcome this problem, we
2428         need to take a two-step operation. First, we find out the Id
2429         of the parent window of our window. We then translate the
2430         above relative coordinates to the screen coordinates.
2431         </p>
2432         <p>
2433         To get the Id of the parent window, we need this structure:
2434         </p>
2435         <pre class="code">
2436 typedef struct {
2437     uint8_t      response_type;
2438     uint8_t      pad0;
2439     uint16_t     sequence;
2440     uint32_t     length;
2441     xcb_window_t root;
2442     xcb_window_t parent;       /* Id of the parent window */
2443     uint16_t     children_len;
2444     uint8_t      pad1[14];
2445 } xcb_query_tree_reply_t;
2446 </pre>
2447         <p>
2448         To fill this structure, we use these two functions:
2449         </p>
2450         <pre class="code">
2451 xcb_query_tree_cookie_t xcb_query_tree       (xcb_connection_t        *c,
2452                                               xcb_window_t             window);
2453 xcb_query_tree_reply_t *xcb_query_tree_reply (xcb_connection_t        *c,
2454                                               xcb_query_tree_cookie_t  cookie,
2455                                               xcb_generic_error_t    **e);
2456 </pre>
2457         <p>
2458         The translated coordinates will be found in this structure:
2459         </p>
2460         <pre class="code">
2461 typedef struct {
2462     uint8_t      response_type;
2463     uint8_t      same_screen;
2464     uint16_t     sequence;
2465     uint32_t     length;
2466     xcb_window_t child;
2467     uint16_t     dst_x;        /* Translated x coordinate */
2468     uint16_t     dst_y;        /* Translated y coordinate */
2469 } xcb_translate_coordinates_reply_t;
2470 </pre>
2471         <p>
2472         As usual, we need two functions to fill this structure:
2473         </p>
2474         <pre class="code">
2475 xcb_translate_coordinates_cookie_t xcb_translate_coordinates       (xcb_connection_t                  *c,
2476                                                                     xcb_window_t                       src_window,
2477                                                                     xcb_window_t                       dst_window,
2478                                                                     int16_t                            src_x,
2479                                                                     int16_t                            src_y);
2480 xcb_translate_coordinates_reply_t *xcb_translate_coordinates_reply (xcb_connection_t                  *c,
2481                                                                     xcb_translate_coordinates_cookie_t cookie,
2482                                                                     xcb_generic_error_t              **e);
2483 </pre>
2484         <p>
2485         We use them as follows:
2486         </p>
2487         <pre class="code">
2488   xcb_connection_t                  *c;
2489   xcb_drawable_t                     win;
2490   xcb_get_geometry_reply_t          *geom;
2491   xcb_query_tree_reply_t            *tree;
2492   xcb_translate_coordinates_reply_t *trans;
2493
2494   /* You initialize c and win */
2495
2496   geom  = xcb_get_geometry_reply (c, xcb_get_geometry (c, win), NULL);
2497   if (!geom)
2498     return 0;
2499
2500   tree  = xcb_query_tree_reply (c, xcb_query_tree (c, win), NULL);
2501   if (!tree)
2502     return 0;
2503
2504   trans = xcb_translate_coordinates_reply (c,
2505                                            xcb_translate_coordinates (c,
2506                                                                       win,
2507                                                                       tree-&gt;parent,
2508                                                                       geom-&gt;x, geom-&gt;y),
2509                                            NULL);
2510   if (!trans)
2511     return 0;
2512
2513   /* the translated coordinates are in trans-&gt;dst_x and trans-&gt;dst_y */
2514
2515   free (trans);
2516   free (tree);
2517   free (geom);
2518 </pre>
2519         <p>
2520         Of course, as for <span class="code">geom</span>,
2521         <span class="code">tree</span> and
2522         <span class="code">trans</span> have to be freed.
2523         </p>
2524         <p>
2525         The work is a bit hard, but XCB is a very low-level library.
2526         </p>
2527         <p>
2528         <b>TODO:</b> the utilization of these functions should be a
2529         prog, which displays the coordinates of the window.
2530         </p>
2531         <p>
2532         There is another structure that gives informations about our window:
2533         </p>
2534         <pre class="code">
2535 typedef struct {
2536     uint8_t        response_type;
2537     uint8_t        backing_store;
2538     uint16_t       sequence;
2539     uint32_t       length;
2540     xcb_visualid_t visual;                /* Visual of the window */
2541     uint16_t       _class;
2542     uint8_t        bit_gravity;
2543     uint8_t        win_gravity;
2544     uint32_t       backing_planes;
2545     uint32_t       backing_pixel;
2546     uint8_t        save_under;
2547     uint8_t        map_is_installed;
2548     uint8_t        map_state;             /* Map state of the window */
2549     uint8_t        override_redirect;
2550     xcb_colormap_t colormap;              /* Colormap of the window */
2551     uint32_t       all_event_masks;
2552     uint32_t       your_event_mask;
2553     uint16_t       do_not_propagate_mask;
2554 } xcb_get_window_attributes_reply_t;
2555 </pre>
2556         <p>
2557         XCB supplies these two functions to fill it:
2558         </p>
2559         <pre class="code">
2560 xcb_get_window_attributes_cookie_t xcb_get_window_attributes       (xcb_connection_t                  *c,
2561                                                                     xcb_window_t                       window);
2562 xcb_get_window_attributes_reply_t *xcb_get_window_attributes_reply (xcb_connection_t                  *c,
2563                                                                     xcb_get_window_attributes_cookie_t cookie,
2564                                                                     xcb_generic_error_t              **e);
2565 </pre>
2566         <p>
2567         You use them as follows:
2568         </p>
2569         <pre class="code">
2570   xcb_connection_t                  *c;
2571   xcb_drawable_t                     win;
2572   xcb_get_window_attributes_reply_t *attr;
2573
2574   /* You initialize c and win */
2575
2576   attr = xcb_get_window_attributes_reply (c, xcb_get_window_attributes (c, win), NULL);
2577
2578   if (!attr)
2579     return 0;
2580
2581   /* Do something with the fields of attr */
2582
2583   free (attr);
2584 </pre>
2585         <p>
2586         As for <span class="code">geom</span>,
2587         <span class="code">attr</span> has to be freed.
2588         </p>
2589       </ol>
2590       <li class="title"><a name="usecolor">Using colors to paint the rainbow</a>
2591       <p>
2592       Up until now, all our painting operation were done using black
2593       and white. We will (finally) see now how to draw using colors.
2594       </p>
2595       <ol>
2596         <li class="subtitle"><a name="colormap">Color maps</a>
2597         <p>
2598         In the beginning, there were not enough colors. Screen
2599         controllers could only support a limited number of colors
2600         simultaneously (initially 2, then 4, 16 and 256). Because of
2601         this, an application could not just ask to draw in a "light
2602         purple-red" color, and expect that color to be available. Each
2603         application allocated the colors it needed, and when all the
2604         color entries (4, 16, 256 colors) were in use, the next color
2605         allocation would fail.
2606         </p>
2607         <p>
2608         Thus, the notion of "a color map" was introduced. A color map
2609         is a table whose size is the same as the number of
2610         simultaneous colors a given screen controller. Each entry
2611         contained the RGB (Red, Green and Blue) values of a different
2612         color (all colors can be drawn using some combination of red,
2613         green and blue). When an application wants to draw on the
2614         screen, it does not specify which color to use. Rather, it
2615         specifies which color entry of some color map to be used
2616         during this drawing. Change the value in this color map entry
2617         and the drawing will use a different color.
2618         </p>
2619         <p>
2620         In order to be able to draw using colors that got something to
2621         do with what the programmer intended, color map allocation
2622         functions are supplied. You could ask to allocate entry for a
2623         color with a set of RGB values. If one already existed, you
2624         would get its index in the table. If none existed, and the
2625         table was not full, a new cell would be allocated to contain
2626         the given RGB values, and its index returned. If the table was
2627         full, the procedure would fail. You could then ask to get a
2628         color map entry with a color that is closest to the one you
2629         were asking for. This would mean that the actual drawing on
2630         the screen would be done using colors similar to what you
2631         wanted, but not the same.
2632         </p>
2633         <p>
2634         On today's more modern screens where one runs an X server with
2635         support for 16 million colors, this limitation looks a little
2636         silly, but remember that there are still older computers with
2637         older graphics cards out there. Using color map, support for
2638         these screen becomes transparent to you. On a display
2639         supporting 16 million colors, any color entry allocation
2640         request would succeed. On a display supporting a limited
2641         number of colors, some color allocation requests would return
2642         similar colors. It won't look as good, but your application
2643         would still work.
2644         </p>
2645         <li class="subtitle"><a name="colormapalloc">Allocating and freeing Color Maps</a>
2646         <p>
2647         When you draw using XCB, you can choose to use the standard
2648         color map of the screen your window is displayed on, or you
2649         can allocate a new color map and apply it to a window. In the
2650         latter case, each time the mouse moves onto your window, the
2651         screen color map will be replaced by your window's color map,
2652         and you'll see all the other windows on screen change their
2653         colors into something quite bizzare. In fact, this is the
2654         effect you get with X applications that use the "-install"
2655         command line option.
2656         </p>
2657         <p>
2658         In XCB, a color map is (as often in X) an Id:
2659         </p>
2660         <pre class="code">
2661 typedef struct {
2662     uint32_t xid;
2663 } xcb_colormap_t;
2664 </pre>
2665         <p>
2666         In order to access the screen's default color map, you just
2667         have to retrieve the <span class="code">default_colormap</span>
2668         field of the <span class="code">xcb_screen_t</span> structure
2669         (see Section
2670         <a href="#screen">Checking basic information about a connection</a>):
2671         </p>
2672         <pre class="code">
2673 #include &lt;stdio.h&gt;
2674
2675 #include &lt;xcb/xcb.h&gt;
2676
2677 int
2678 main ()
2679 {
2680   xcb_connection_t *c;
2681   xcb_screen_t     *screen;
2682   xcb_colormap_t    colormap;
2683
2684   /* Open the connection to the X server and get the first screen */
2685   c = xcb_connect (NULL, NULL);
2686   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2687
2688   colormap = screen-&gt;default_colormap;
2689
2690   return 0;
2691 }
2692 </pre>
2693         <p>
2694         This will return the color map used by default on the first
2695         screen (again, remember that an X server may support several
2696         different screens, each of which might have its own resources).
2697         </p>
2698         <p>
2699         The other option, that of allocating a new colormap, works as
2700         follows.  We first ask the X server to give an Id to our color
2701         map, with this function:
2702         </p>
2703         <pre class="code">
2704 xcb_colormap_t xcb_generate_id (xcb_connection_t *c);
2705 </pre>
2706         <p>
2707         Then, we create the color map with
2708         </p>
2709         <pre class="code">
2710 xcb_void_cookie_t xcb_create_colormap (xcb_connection_t *c,       /* Pointer to the xcb_connection_t structure */
2711                                        uint8_t           alloc,   /* Colormap entries to be allocated (AllocNone or AllocAll) */
2712                                        xcb_colormap_t    mid,     /* Id of the color map */
2713                                        xcb_window_t      window,  /* Window on whose screen the colormap will be created */
2714                                        xcb_visualid_t    visual); /* Id of the visual supported by the screen */
2715 </pre>
2716         <p>
2717         Here is an example of creation of a new color map:
2718         </p>
2719         <pre class="code">
2720 #include &lt;xcb/xcb.h&gt;
2721
2722 int
2723 main ()
2724 {
2725   xcb_connection_t *c;
2726   xcb_screen_t     *screen;
2727   xcb_window_t      win;
2728   xcb_colormap_t    cmap
2729
2730   /* Open the connection to the X server and get the first screen */
2731   c = xcb_connect (NULL, NULL);
2732   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2733
2734   /* We create the window win here*/
2735
2736   cmap = xcb_generate_id (c);
2737   xcb_create_colormap (c, XCB_COLORMAP_ALLOC_NONE, cmap, win, screen-&gt;root_visual);
2738
2739   return 0;
2740 }
2741 </pre>
2742         <p>
2743         Note that the window parameter is only used to allow the X
2744         server to create the color map for the given screen. We can
2745         then use this color map for any window drawn on the same screen.
2746         </p>
2747         <p>
2748         To free  a color map, it suffices to use this function:
2749         </p>
2750         <pre class="code">
2751 xcb_void_cookie_t xcb_free_colormap (xcb_connection_t *c,   /* The connection */
2752                                      xcb_colormap_t cmap);  /* The color map */
2753 </pre>
2754         <div class="comp">
2755           <div class="title">
2756             Comparison Xlib/XCB
2757           </div>
2758           <div class="xlib">
2759             <ul>
2760               <li>XCreateColormap ()
2761             </ul>
2762           </div>
2763           <div class="xcb">
2764             <ul>
2765               <li>xcb_generate_id ()
2766               <li>xcb_create_colormap ()
2767             </ul>
2768           </div>
2769           <div class="xlib">
2770             <ul>
2771               <li>XFreeColormap ()
2772             </ul>
2773           </div>
2774           <div class="xcb">
2775             <ul>
2776               <li>xcb_free_colormap ()
2777             </ul>
2778           </div>
2779         </div>
2780         <br>
2781         <li class="subtitle"><a name="alloccolor">Allocating and freeing a color entry</a>
2782         <p>
2783         Once we got access to some color map, we can start allocating
2784         colors. The informations related to a color are stored in the
2785         following structure:
2786         </p>
2787         <pre class="code">
2788 typedef struct {
2789     uint8_t  response_type;
2790     uint8_t  pad0;
2791     uint16_t sequence;
2792     uint32_t length;
2793     uint16_t red;          /* The red component   */
2794     uint16_t green;        /* The green component */
2795     uint16_t blue;         /* The blue component  */
2796     uint8_t  pad1[2];
2797     uint32_t pixel;        /* The entry in the color map, supplied by the X server */
2798 } xcb_alloc_color_reply_t;
2799 </pre>
2800       <p>
2801       XCB supplies these two functions to fill it:
2802       </p>
2803       <pre class="code">
2804 xcb_alloc_color_cookie_t xcb_alloc_color       (xcb_connection_t        *c,
2805                                                 xcb_colormap_t           cmap,
2806                                                 uint16_t                 red,
2807                                                 uint16_t                 green,
2808                                                 uint16_t                 blue);
2809 xcb_alloc_color_reply_t *xcb_alloc_color_reply (xcb_connection_t        *c,
2810                                                 xcb_alloc_color_cookie_t cookie,
2811                                                 xcb_generic_error_t    **e);
2812 </pre>
2813       <p>
2814       The fuction <span class="code">xcb_alloc_color()</span> takes the
2815       3 RGB components as parameters (red, green and blue). Here is an
2816       example of using these functions:
2817       </p>
2818       <pre class="code">
2819 #include &lt;malloc.h&gt;
2820
2821 #include &lt;xcb/xcb.h&gt;
2822
2823 int
2824 main ()
2825 {
2826   xcb_connection_t        *c;
2827   xcb_screen_t            *screen;
2828   xcb_window_t             win;
2829   xcb_colormap_t           cmap;
2830   xcb_alloc_color_reply_t *rep;
2831
2832   /* Open the connection to the X server and get the first screen */
2833   c = xcb_connect (NULL, NULL);
2834   screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2835
2836   /* We create the window win here*/
2837
2838   cmap = xcb_generate_id (c);
2839   xcb_create_colormap (c, XCB_COLORMAP_ALLOC_NONE, cmap, win, screen-&gt;root_visual);
2840
2841   rep = xcb_alloc_color_reply (c, xcb_alloc_color (c, cmap, 65535, 0, 0), NULL);
2842
2843   if (!rep)
2844     return 0;
2845
2846   /* Do something with r-&gt;pixel or the components */
2847
2848   free (rep);
2849
2850   return 0;
2851 }
2852 </pre>
2853       <p>
2854       As <span class="code">xcb_alloc_color_reply()</span> allocates
2855       memory, you have to free <span class="code">rep</span>.
2856       </p>
2857       <p>
2858       <b>TODO</b>: Talk about freeing colors.
2859       </p>
2860       </ol>
2861       <li class="title"><a name="pixmaps">X Bitmaps and Pixmaps</a>
2862       <p>
2863       One thing many so-called "Multi-Media" applications need to do,
2864       is display images. In the X world, this is done using bitmaps
2865       and pixmaps. We have already seen some usage of them when
2866       setting an icon for our application. Lets study them further,
2867       and see how to draw these images inside a window, along side the
2868       simple graphics and text we have seen so far.
2869       </p>
2870       <p>
2871       One thing to note before delving further, is that XCB (nor Xlib)
2872       supplies no means of manipulating popular image formats, such as
2873       gif, png, jpeg or tiff. It is up to the programmer (or to higher
2874       level graphics libraries) to translate these image formats into
2875       formats that the X server is familiar with (x bitmaps and x
2876       pixmaps).
2877       </p>
2878       <ol>
2879         <li class="subtitle"><a name="pixmapswhat">What is a X Bitmap? An X Pixmap?</a>
2880         <p>
2881         An X bitmap is a two-color image stored in a format specific
2882         to the X window system. When stored in a file, the bitmap data
2883         looks like a C source file. It contains variables defining the
2884         width and the height of the bitmap, an array containing the
2885         bit values of the bitmap (the size of the array is
2886         (width+7)/8*height and the bit and byte order are LSB), and
2887         an optional hot-spot location (that will
2888         be explained later, when discussing mouse cursors).
2889         </p>
2890         <p>
2891         An X pixmap is a format used to stored images in the memory of
2892         an X server. This format can store both black and white images
2893         (such as x bitmaps) as well as color images. It is the only
2894         image format supported by the X protocol, and any image to be
2895         drawn on screen, should be first translated into this format.
2896         </p>
2897         <p>
2898         In actuality, an X pixmap can be thought of as a window that
2899         does not appear on the screen. Many graphics operations that
2900         work on windows, will also work on pixmaps. Indeed, the type
2901         of X pixmap in XCB is an Id like a window:
2902         </p>
2903         <pre class="code">
2904 typedef struct {
2905     uint32_t xid;
2906 } xcb_pixmap_t;
2907 </pre>
2908         <p>
2909         In order to make the difference between a window and a pixmap,
2910         XCB introduces a drawable type, which is a <b>union</b>
2911         </p>
2912         <pre class="code">
2913 typedef union {
2914     xcb_window_t window;
2915     xcb_pixmap_t pixmap;
2916 } xcb_drawable_t;
2917 </pre>
2918         <p>
2919         in order to avoid confusion between a window and a pixmap. The
2920         operations that will work the same on a window or a pixmap
2921         will require a <span class="code">xcb_drawable_t</span>
2922         </p>
2923         <div class="emph">
2924         <p>
2925         Remark: In Xlib, there is no specific difference between a
2926         <span class="code">Drawable</span>, a
2927         <span class="code">Pixmap</span> or a
2928         <span class="code">Window</span>: all are 32 bit long
2929         integer.  XCB wraps all these different IDs in structures to
2930         provide some measure of type-safety.
2931         </p>
2932         </div>
2933         <li class="subtitle"><a name="pixmapscreate">Creating a pixmap</a>
2934         <p>
2935         Sometimes we want to create an un-initialized pixmap, so we
2936         can later draw into it. This is useful for image drawing
2937         programs (creating a new empty canvas will cause the creation
2938         of a new pixmap on which the drawing can be stored). It is
2939         also useful when reading various image formats: we load the
2940         image data into memory, create a pixmap on the server, and
2941         then draw the decoded image data onto that pixmap.
2942         </p>
2943         <p>
2944         To create a new pixmap, we first ask the X server to give an
2945         Id to our pixmap, with this function:
2946         </p>
2947         <pre class="code">
2948 xcb_pixmap_t xcb_generate_id (xcb_connection_t *c);
2949 </pre>
2950         <p>
2951          Then, XCB supplies the following function to create new pixmaps:
2952         </p>
2953         <pre class="code">
2954 xcb_void_cookie_t xcb_create_pixmap (xcb_connection_t *c,         /* Pointer to the xcb_connection_t structure */
2955                                      uint8_t           depth,     /* Depth of the screen */
2956                                      xcb_pixmap_t      pid,       /* Id of the pixmap */
2957                                      xcb_drawable_t    drawable,
2958                                      uint16_t          width,     /* Width of the window (in pixels) */
2959                                      uint16_t          height);   /* Height of the window (in pixels) */
2960 </pre>
2961         <p>
2962         <b>TODO</b>: Explain the drawable parameter, and give an
2963         example (like <a href="xpoints.c">xpoints.c</a>)
2964         </p>
2965         <li class="subtitle"><a name="pixmapsdraw"></a>Drawing a pixmap in a window
2966         <p>
2967         Once we got a handle to a pixmap, we can draw it on some
2968         window, using the following function:
2969         </p>
2970         <pre class="code">
2971 xcb_void_cookie_t xcb_copy_area (xcb_connection_t *c,             /* Pointer to the xcb_connection_t structure */
2972                                  xcb_drawable_t    src_drawable,  /* The Drawable we want to paste */
2973                                  xcb_drawable_t    dst_drawable,  /* The Drawable on which we copy the previous Drawable */
2974                                  xcb_gcontext_t    gc,            /* A Graphic Context */
2975                                  int16_t           src_x,         /* Top left x coordinate of the region we want to copy */
2976                                  int16_t           src_y,         /* Top left y coordinate of the region we want to copy */
2977                                  int16_t           dst_x,         /* Top left x coordinate of the region where we want to copy */
2978                                  int16_t           dst_y,         /* Top left y coordinate of the region where we want to copy */
2979                                  uint16_t          width,         /* Width of the region we want to copy */
2980                                  uint16_t          height);       /* Height of the region we want to copy */
2981 </pre>
2982         <p>
2983         As you can see, we could copy the whole pixmap, as well as
2984         only a given rectangle of the pixmap. This is useful to
2985         optimize the drawing speed: we could copy only what we have
2986         modified in the pixmap.
2987         </p>
2988         <p>
2989         <b>One important note should be made</b>: it is possible to
2990         create pixmaps with different depths on the same screen. When
2991         we perform copy operations (a pixmap onto a window, etc), we
2992         should make sure that both source and target have the same
2993         depth. If they have a different depth, the operation would
2994         fail. The exception to this is if we copy a specific bit plane
2995         of the source pixmap using the
2996         <span class="code">xcb_copy_plane_t</span> function. In such an
2997         event, we can copy a specific plane to the target window (in
2998         actuality, setting a specific bit in the color of each pixel
2999         copied). This can be used to generate strange graphic effects
3000         in a window, but that is beyond the scope of this tutorial.
3001         </p>
3002         <li class="subtitle"><a name="pixmapsfree"></a>Freeing a pixmap
3003         <p>
3004         Finally, when we are done using a given pixmap, we should free
3005         it, in order to free resources of the X server. This is done
3006         using this function:
3007         </p>
3008         <pre class="code">
3009 xcb_void_cookie_t xcb_free_pixmap (xcb_connection_t *c,        /* Pointer to the xcb_connection_t structure */
3010                                    xcb_pixmap_t      pixmap);  /* A given pixmap */
3011 </pre>
3012         <p>
3013         Of course, after having freed it, we must not try accessing
3014         the pixmap again.
3015         </p>
3016         <p>
3017         <b>TODO</b>: Give an example, or a link to xpoints.c
3018         </p>
3019       </ol>
3020       <li class="title"><a name="mousecursor">Messing with the mouse cursor</a>
3021       <p>
3022       It it possible to modify the shape of the mouse pointer (also
3023       called the X pointer) when in certain states, as we otfen see in
3024       programs. For example, a busy application would often display
3025       the sand clock over its main window, to give the user a visual
3026       hint that he should wait. Let's see how we can change the mouse
3027       cursor of our windows.
3028       </p>
3029       <ol>
3030         <li class="subtitle"><a name="mousecursorcreate">Creating and destroying a mouse cursor</a>
3031         <p>
3032         There are two methods for creating cursors. One of them is by
3033         using a set of predefined cursors, that are supplied by the X
3034         server, the other is by using a user-supplied bitmap.
3035         </p>
3036         <p>
3037         In the first method, we use a special font named "cursor", and
3038         the function <span class="code">xcb_create_glyph_cursor</span>:
3039         </p>
3040         <pre class="code">
3041 xcb_void_cookie_t xcb_create_glyph_cursor (xcb_connection_t *c,
3042                                            xcb_cursor_t      cid,
3043                                            xcb_font_t        source_font, /* font for the source glyph */
3044                                            xcb_font_t        mask_font,   /* font for the mask glyph or XCB_NONE */
3045                                            uint16_t          source_char, /* character glyph for the source */
3046                                            uint16_t          mask_char,   /* character glyph for the mask */
3047                                            uint16_t          fore_red,    /* red value for the foreground of the source */
3048                                            uint16_t          fore_green,  /* green value for the foreground of the source */
3049                                            uint16_t          fore_blue,   /* blue value for the foreground of the source */
3050                                            uint16_t          back_red,    /* red value for the background of the source */
3051                                            uint16_t          back_green,  /* green value for the background of the source */
3052                                            uint16_t          back_blue)   /* blue value for the background of the source */
3053 </pre>
3054         <p>
3055         <b>TODO</b>: Describe <span class="code">source_char</span>
3056         and <span class="code">mask_char</span>, for example by giving
3057         an example on how to get the values. There is a list there:
3058         <a href="http://tronche.com/gui/x/xlib/appendix/b/">X Font Cursors</a>
3059         </p>
3060         <p>
3061         So we first open that font (see <a href="#loadfont">Loading a Font</a>)
3062         and create the new cursor. As for every X ressource, we have to
3063         ask for an X id with <span class="code">xcb_generate_id</span>
3064         first:
3065         </p>
3066         <pre class="code">
3067 xcb_font_t           font;
3068 xcb_cursor_t         cursor;
3069
3070 /* The connection is set */
3071
3072 font = xcb_generate_id (conn);
3073 xcb_open_font (conn, font, strlen ("cursor"), "cursor");
3074
3075 cursor = xcb_generate_id (conn);
3076 xcb_create_glyph_cursor (conn, cursor, font, font,
3077                          58, 58 + 1,
3078                          0, 0, 0,
3079                          0, 0, 0);
3080 </pre>
3081         <p>
3082         We have created the cursor "right hand" by specifying 58 to
3083         the <span class="code">source_fon</span>t argument and 58 + 1
3084         to the <span class="code">mask_font</span>.
3085         </p>
3086         <p>
3087         The cursor is destroyed by using the function
3088         </p>
3089         <pre class="code">
3090 xcb_void_cookie_t xcb_free_cursor (xcb_connection_t *c,
3091                                    xcb_cursor_t      cursor);
3092 </pre>
3093         <p>
3094         In the second method, we create a new cursor by using a pair
3095         of pixmaps, with depth of one (that is, two colors
3096         pixmaps). One pixmap defines the shape of the cursor, while
3097         the other works as a mask, specifying which pixels of the
3098         cursor will be actually drawn. The rest of the pixels will be
3099         transparent.
3100         </p>
3101         <p>
3102         <b>TODO</b>: give an example.
3103         </p>
3104         <li class="subtitle"><a name="mousecursorset">Setting a window's mouse cursor</a>
3105         <p>
3106         Once the cursor is created, we can modify the cursor of our
3107         window by using <span class="code">xcb_change_window_attributes</span>
3108         and using the <span class="code">XCB_CWCURSOR</span> attribute:
3109         </p>
3110         <pre class="code">
3111 uint32_t mask;
3112 uint32_t value_list;
3113
3114 /* The connection and window are set */
3115 /* The cursor is already created */
3116
3117 mask = XCB_CWCURSOR;
3118 value_list = cursor.xid;
3119 xcb_change_window_attributes (conn, window, mask, &amp;value_list);
3120 </pre>
3121         <p>
3122         Of course, the cursor and the font must be freed.
3123         </p>
3124         <li class="subtitle"><a name="mousecursorexample">Complete example</a>
3125         <p>
3126         The following example displays a window with a
3127         button. When entering the window, the window cursor is changed
3128         to an arrow. When clicking once on the button, the cursor is
3129         changed to a hand. When clicking again on the button, the
3130         cursor window gets back to the arrow. The Esc key exits the
3131         application.
3132         </p>
3133         <pre class="code">
3134 #include &lt;stdlib.h&gt;
3135 #include &lt;stdio.h&gt;
3136 #include &lt;string.h&gt;
3137
3138 #include &lt;xcb/xcb.h&gt;
3139
3140 #define WIDTH 300
3141 #define HEIGHT 150
3142
3143
3144
3145 static xcb_gc_t gc_font_get (xcb_connection_t *c,
3146                              xcb_screen_t     *screen,
3147                              xcb_window_t      window,
3148                              const char       *font_name);
3149
3150 static void button_draw (xcb_connection_t *c,
3151                          xcb_screen_t     *screen,
3152                          xcb_window_t      window,
3153                          int16_t           x1,
3154                          int16_t           y1,
3155                          const char       *label);
3156
3157 static void text_draw (xcb_connection_t *c,
3158                        xcb_screen_t     *screen,
3159                        xcb_window_t      window,
3160                        int16_t           x1,
3161                        int16_t           y1,
3162                        const char       *label);
3163
3164 static void cursor_set (xcb_connection_t *c,
3165                         xcb_screen_t     *screen,
3166                         xcb_window_t      window,
3167                         int               cursor_id);
3168
3169
3170 static void
3171 button_draw (xcb_connection_t *c,
3172              xcb_screen_t     *screen,
3173              xcb_window_t      window,
3174              int16_t           x1,
3175              int16_t           y1,
3176              const char       *label)
3177 {
3178   xcb_point_t          points[5];
3179   xcb_void_cookie_t    cookie_gc;
3180   xcb_void_cookie_t    cookie_line;
3181   xcb_void_cookie_t    cookie_text;
3182   xcb_generic_error_t *error;
3183   xcb_gcontext_t       gc;
3184   int16_t              width;
3185   int16_t              height;
3186   uint8_t              length;
3187   int16_t              inset;
3188
3189   length = strlen (label);
3190   inset = 2;
3191
3192   gc = gc_font_get(c, screen, window, "7x13");
3193
3194   width = 7 * length + 2 * (inset + 1);
3195   height = 13 + 2 * (inset + 1);
3196   points[0].x = x1;
3197   points[0].y = y1;
3198   points[1].x = x1 + width;
3199   points[1].y = y1;
3200   points[2].x = x1 + width;
3201   points[2].y = y1 - height;
3202   points[3].x = x1;
3203   points[3].y = y1 - height;
3204   points[4].x = x1;
3205   points[4].y = y1;
3206   cookie_line = xcb_poly_line_checked (c, XCB_COORD_MODE_ORIGIN,
3207                                        window, gc, 5, points);
3208
3209   error = xcb_request_check (c, cookie_line);
3210   if (error) {
3211     fprintf (stderr, "ERROR: can't draw lines : %d\n", error->error_code);
3212     xcb_disconnect (c);
3213     exit (-1);
3214   }
3215
3216   cookie_text = xcb_image_text_8_checked (c, length, window, gc,
3217                                           x1 + inset + 1,
3218                                           y1 - inset - 1, label);
3219   error = xcb_request_check (c, cookie_text);
3220   if (error) {
3221     fprintf (stderr, "ERROR: can't paste text : %d\n", error->error_code);
3222     xcb_disconnect (c);
3223     exit (-1);
3224   }
3225
3226   cookie_gc = xcb_free_gc (c, gc);
3227   error = xcb_request_check (c, cookie_gc);
3228   if (error) {
3229     fprintf (stderr, "ERROR: can't free gc : %d\n", error->error_code);
3230     xcb_disconnect (c);
3231     exit (-1);
3232   }
3233 }
3234
3235 static void
3236 text_draw (xcb_connection_t *c,
3237            xcb_screen_t     *screen,
3238            xcb_window_t      window,
3239            int16_t           x1,
3240            int16_t           y1,
3241            const char       *label)
3242 {
3243   xcb_void_cookie_t    cookie_gc;
3244   xcb_void_cookie_t    cookie_text;
3245   xcb_generic_error_t *error;
3246   xcb_gcontext_t       gc;
3247   uint8_t              length;
3248
3249   length = strlen (label);
3250
3251   gc = gc_font_get(c, screen, window, "7x13");
3252
3253   cookie_text = xcb_image_text_8_checked (c, length, window, gc,
3254                                           x1,
3255                                           y1, label);
3256   error = xcb_request_check (c, cookie_text);
3257   if (error) {
3258     fprintf (stderr, "ERROR: can't paste text : %d\n", error->error_code);
3259     xcb_disconnect (c);
3260     exit (-1);
3261   }
3262
3263   cookie_gc = xcb_free_gc (c, gc);
3264   error = xcb_request_check (c, cookie_gc);
3265   if (error) {
3266     fprintf (stderr, "ERROR: can't free gc : %d\n", error->error_code);
3267     xcb_disconnect (c);
3268     exit (-1);
3269   }
3270 }
3271
3272 static xcb_gc_t
3273 gc_font_get (xcb_connection_t *c,
3274              xcb_screen_t     *screen,
3275              xcb_window_t      window,
3276              const char       *font_name)
3277 {
3278   uint32_t             value_list[3];
3279   xcb_void_cookie_t    cookie_font;
3280   xcb_void_cookie_t    cookie_gc;
3281   xcb_generic_error_t *error;
3282   xcb_font_t           font;
3283   xcb_gcontext_t       gc;
3284   uint32_t             mask;
3285
3286   font = xcb_generate_id (c);
3287   cookie_font = xcb_open_font_checked (c, font,
3288                                        strlen (font_name),
3289                                        font_name);
3290
3291   error = xcb_request_check (c, cookie_font);
3292   if (error) {
3293     fprintf (stderr, "ERROR: can't open font : %d\n", error->error_code);
3294     xcb_disconnect (c);
3295     return -1;
3296   }
3297
3298   gc = xcb_generate_id (c);
3299   mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
3300   value_list[0] = screen->black_pixel;
3301   value_list[1] = screen->white_pixel;
3302   value_list[2] = font;
3303   cookie_gc = xcb_create_gc_checked (c, gc, window, mask, value_list);
3304   error = xcb_request_check (c, cookie_gc);
3305   if (error) {
3306     fprintf (stderr, "ERROR: can't create gc : %d\n", error->error_code);
3307     xcb_disconnect (c);
3308     exit (-1);
3309   }
3310
3311   cookie_font = xcb_close_font_checked (c, font);
3312   error = xcb_request_check (c, cookie_font);
3313   if (error) {
3314     fprintf (stderr, "ERROR: can't close font : %d\n", error->error_code);
3315     xcb_disconnect (c);
3316     exit (-1);
3317   }
3318
3319   return gc;
3320 }
3321
3322 static void
3323 cursor_set (xcb_connection_t *c,
3324             xcb_screen_t     *screen,
3325             xcb_window_t      window,
3326             int               cursor_id)
3327 {
3328   uint32_t             values_list[3];
3329   xcb_void_cookie_t    cookie_font;
3330   xcb_void_cookie_t    cookie_gc;
3331   xcb_generic_error_t *error;
3332   xcb_font_t           font;
3333   xcb_cursor_t         cursor;
3334   xcb_gcontext_t       gc;
3335   uint32_t             mask;
3336   uint32_t             value_list;
3337
3338   font = xcb_generate_id (c);
3339   cookie_font = xcb_open_font_checked (c, font,
3340                                        strlen ("cursor"),
3341                                        "cursor");
3342   error = xcb_request_check (c, cookie_font);
3343   if (error) {
3344     fprintf (stderr, "ERROR: can't open font : %d\n", error->error_code);
3345     xcb_disconnect (c);
3346     exit (-1);
3347   }
3348
3349   cursor = xcb_generate_id (c);
3350   xcb_create_glyph_cursor (c, cursor, font, font,
3351                            cursor_id, cursor_id + 1,
3352                            0, 0, 0,
3353                            0, 0, 0);
3354
3355   gc = xcb_generate_id (c);
3356   mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
3357   values_list[0] = screen->black_pixel;
3358   values_list[1] = screen->white_pixel;
3359   values_list[2] = font;
3360   cookie_gc = xcb_create_gc_checked (c, gc, window, mask, values_list);
3361   error = xcb_request_check (c, cookie_gc);
3362   if (error) {
3363     fprintf (stderr, "ERROR: can't create gc : %d\n", error->error_code);
3364     xcb_disconnect (c);
3365     exit (-1);
3366   }
3367
3368   mask = XCB_CW_CURSOR;
3369   value_list = cursor;
3370   xcb_change_window_attributes (c, window, mask, &amp;value_list);
3371
3372   xcb_free_cursor (c, cursor);
3373
3374   cookie_font = xcb_close_font_checked (c, font);
3375   error = xcb_request_check (c, cookie_font);
3376   if (error) {
3377     fprintf (stderr, "ERROR: can't close font : %d\n", error->error_code);
3378     xcb_disconnect (c);
3379     exit (-1);
3380   }
3381 }
3382
3383 int main ()
3384 {
3385   xcb_screen_iterator_t screen_iter;
3386   xcb_connection_t     *c;
3387   const xcb_setup_t    *setup;
3388   xcb_screen_t         *screen;
3389   xcb_generic_event_t  *e;
3390   xcb_generic_error_t  *error;
3391   xcb_void_cookie_t     cookie_window;
3392   xcb_void_cookie_t     cookie_map;
3393   xcb_window_t          window;
3394   uint32_t              mask;
3395   uint32_t              values[2];
3396   int                   screen_number;
3397   uint8_t               is_hand = 0;
3398
3399   /* getting the connection */
3400   c = xcb_connect (NULL, &amp;screen_number);
3401   if (!c) {
3402     fprintf (stderr, "ERROR: can't connect to an X server\n");
3403     return -1;
3404   }
3405
3406   /* getting the current screen */
3407   setup = xcb_get_setup (c);
3408
3409   screen = NULL;
3410   screen_iter = xcb_setup_roots_iterator (setup);
3411   for (; screen_iter.rem != 0; --screen_number, xcb_screen_next (&amp;screen_iter))
3412     if (screen_number == 0)
3413       {
3414         screen = screen_iter.data;
3415         break;
3416       }
3417   if (!screen) {
3418     fprintf (stderr, "ERROR: can't get the current screen\n");
3419     xcb_disconnect (c);
3420     return -1;
3421   }
3422
3423   /* creating the window */
3424   window = xcb_generate_id (c);
3425   mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
3426   values[0] = screen->white_pixel;
3427   values[1] =
3428     XCB_EVENT_MASK_KEY_RELEASE |
3429     XCB_EVENT_MASK_BUTTON_PRESS |
3430     XCB_EVENT_MASK_EXPOSURE |
3431     XCB_EVENT_MASK_POINTER_MOTION;
3432   cookie_window = xcb_create_window_checked (c,
3433                                              screen->root_depth,
3434                                              window, screen->root,
3435                                              20, 200, WIDTH, HEIGHT,
3436                                              0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
3437                                              screen->root_visual,
3438                                              mask, values);
3439   cookie_map = xcb_map_window_checked (c, window);
3440
3441   /* error managing */
3442   error = xcb_request_check (c, cookie_window);
3443   if (error) {
3444     fprintf (stderr, "ERROR: can't create window : %d\n", error->error_code);
3445     xcb_disconnect (c);
3446     return -1;
3447   }
3448   error = xcb_request_check (c, cookie_map);
3449   if (error) {
3450     fprintf (stderr, "ERROR: can't map window : %d\n", error->error_code);
3451     xcb_disconnect (c);
3452     return -1;
3453   }
3454
3455   cursor_set (c, screen, window, 68);
3456
3457   xcb_flush(c);
3458
3459   while (1) {
3460     e = xcb_poll_for_event(c);
3461     if (e) {
3462       switch (e->response_type) {
3463       case XCB_EXPOSE: {
3464         char *text;
3465
3466         text = "click here to change cursor";
3467         button_draw (c, screen, window,
3468                      (WIDTH - 7 * strlen(text)) / 2,
3469                      (HEIGHT - 16) / 2, text);
3470
3471         text = "Press ESC key to exit...";
3472         text_draw (c, screen, window, 10, HEIGHT - 10, text);
3473         break;
3474       }
3475       case XCB_BUTTON_PRESS: {
3476         xcb_button_press_event_t *ev;
3477         int                       length;
3478
3479         ev = (xcb_button_press_event_t *)e;
3480         length = strlen ("click here to change cursor");
3481
3482         if ((ev->event_x &gt;= (WIDTH - 7 * length) / 2) &amp;&amp;
3483             (ev->event_x &lt;= ((WIDTH - 7 * length) / 2 + 7 * length + 6)) &amp;&amp;
3484             (ev->event_y &gt;= (HEIGHT - 16) / 2 - 19) &amp;&amp;
3485             (ev->event_y &lt;= ((HEIGHT - 16) / 2)))
3486           is_hand = 1 - is_hand;
3487
3488         is_hand ? cursor_set (c, screen, window, 58) : cursor_set (c, screen, window, 68);
3489       }
3490       case XCB_KEY_RELEASE: {
3491         xcb_key_release_event_t *ev;
3492
3493         ev = (xcb_key_release_event_t *)e;
3494
3495         switch (ev->detail) {
3496           /* ESC */
3497         case 9:
3498           free (e);
3499           xcb_disconnect (c);
3500           return 0;
3501         }
3502       }
3503       }
3504       free (e);
3505     }
3506   }
3507
3508   return 0;
3509 }
3510 </pre>
3511       </ol>
3512       <li class="title"><a name="translation">Translation of basic Xlib functions and macros</a>
3513       <p>
3514       The problem when you want to port an Xlib program to XCB is that
3515       you don't know if the Xlib function that you want to "translate"
3516       is a X Window one or an Xlib macro. In that section, we describe
3517       a way to translate the usual functions or macros that Xlib
3518       provides. It's usually just a member of a structure.
3519       </p>
3520       <ol>
3521         <li class="subtitle"><a name="displaystructure">Members of the Display structure</a>
3522         <p>
3523         In this section, we look at how to translate the macros that
3524         return some members of the <span class="code">Display</span>
3525         structure. They are obtained by using a function that requires a
3526         <span class="code">xcb_connection_t *</span> or a member of the
3527         <span class="code">xcb_setup_t</span> structure
3528         (via the function <span class="code">xcb_get_setup</span>), or
3529         a function that requires that structure.
3530         </p>
3531         <ol>
3532           <li class="subtitle"><a name="ConnectionNumber">ConnectionNumber</a>
3533           <p>
3534           This number is the file descriptor that connects the client
3535           to the server. You just have to use that function:
3536           </p>
3537           <pre class="code">
3538 int xcb_get_file_descriptor (xcb_connection_t *c);
3539 </pre>
3540           <li class="subtitle"><a name="DefaultScreen"></a>DefaultScreen
3541           <p>
3542           That number is not stored by XCB. It is returned in the
3543           second parameter of the function <span class="code"><a href="#openconn">xcb_connect</a></span>.
3544           Hence, you have to store it yourself if you want to use
3545           it. Then, to get the <span class="code">xcb_screen_t</span>
3546           structure, you have to iterate on the screens.
3547           The equivalent function of the Xlib's
3548           <span class="code">ScreenOfDisplay</span> function can be
3549           found <a href="#ScreenOfDisplay">below</a>. This is also provided in the
3550           xcb_aux_t library as <span class="code">xcb_aux_get_screen()</span>. OK, here is the
3551           small piece of code to get that number:
3552           </p>
3553           <pre class="code">
3554 xcb_connection_t *c;
3555 int               screen_default_nbr;
3556
3557 /* you pass the name of the display you want to xcb_connect_t */
3558
3559 c = xcb_connect (display_name, &amp;screen_default_nbr);
3560
3561 /* screen_default_nbr contains now the number of the default screen */
3562 </pre>
3563           <li class="subtitle"><a name="QLength"></a>QLength
3564           <p>
3565           Not documented yet.
3566           </p>
3567           <p>
3568           However, this points out a basic difference in philosophy between
3569           Xlib and XCB.  Xlib has several functions for filtering and
3570           manipulating the incoming and outgoing X message queues.  XCB
3571           wishes to hide this as much as possible from the user, which
3572           allows for more freedom in implementation strategies.
3573           </p>
3574           <li class="subtitle"><a name="ScreenCount"></a>ScreenCount
3575           <p>
3576           You get the count of screens with the functions
3577           <span class="code">xcb_get_setup</span>
3578           and
3579           <span class="code">xcb_setup_roots_iterator</span>
3580           (if you need to iterate):
3581           </p>
3582           <pre class="code">
3583 xcb_connection_t *c;
3584 int               screen_count;
3585
3586 /* you init the connection */
3587
3588 screen_count = xcb_setup_roots_iterator (xcb_get_setup (c)).rem;
3589
3590 /* screen_count contains now the count of screens */
3591 </pre>
3592           <p>
3593           If you don't want to iterate over the screens, a better way
3594           to get that number is to use
3595           <span class="code">xcb_setup_roots_length_t</span>:
3596           </p>
3597           <pre class="code">
3598 xcb_connection_t *c;
3599 int               screen_count;
3600
3601 /* you init the connection */
3602
3603 screen_count = xcb_setup_roots_length (xcb_get_setup (c));
3604
3605 /* screen_count contains now the count of screens */
3606 </pre>
3607           <li class="subtitle"><a name="ServerVendor"></a>ServerVendor
3608           <p>
3609           You get the name of the vendor of the server hardware with
3610           the functions <span class="code">xcb_get_setup</span>
3611           and
3612           <span
3613           class="code">xcb_setup_vendor</span>. Beware
3614           that, unlike Xlib, the string returned by XCB is not
3615           necessarily null-terminaled:
3616           </p>
3617           <pre class="code">
3618 xcb_connection_t *c;
3619 char             *vendor = NULL;
3620 int               length;
3621
3622 /* you init the connection */
3623 length = xcb_setup_vendor_length (xcb_get_setup (c));
3624 vendor = (char *)malloc (length + 1);
3625 if (vendor)
3626 memcpy (vendor, xcb_setup_vendor (xcb_get_setup (c)), length);
3627 vendor[length] = '\0';
3628
3629 /* vendor contains now the name of the vendor. Must be freed when not used anymore */
3630 </pre>
3631           <li class="subtitle"><a name="ProtocolVersion"></a>ProtocolVersion
3632           <p>
3633           You get the major version of the protocol in the
3634           <span class="code">xcb_setup_t</span>
3635           structure, with the function <span class="code">xcb_get_setup</span>:
3636           </p>
3637           <pre class="code">
3638 xcb_connection_t *c;
3639 uint16_t          protocol_major_version;
3640
3641 /* you init the connection */
3642
3643 protocol_major_version = xcb_get_setup (c)-&gt;protocol_major_version;
3644
3645 /* protocol_major_version contains now the major version of the protocol */
3646 </pre>
3647           <li class="subtitle"><a name="ProtocolRevision"></a>ProtocolRevision
3648           <p>
3649           You get the minor version of the protocol in the
3650           <span class="code">xcb_setup_t</span>
3651           structure, with the function <span class="code">xcb_get_setup</span>:
3652           </p>
3653           <pre class="code">
3654 xcb_connection_t *c;
3655 uint16_t          protocol_minor_version;
3656
3657 /* you init the connection */
3658
3659 protocol_minor_version = xcb_get_setup (c)-&gt;protocol_minor_version;
3660
3661 /* protocol_minor_version contains now the minor version of the protocol */
3662 </pre>
3663           <li class="subtitle"><a name="VendorRelease"></a>VendorRelease
3664           <p>
3665           You get the number of the release of the server hardware in the
3666           <span class="code">xcb_setup_t</span>
3667           structure, with the function <span class="code">xcb_get_setup</span>:
3668           </p>
3669           <pre class="code">
3670 xcb_connection_t *c;
3671 uint32_t          release_number;
3672
3673 /* you init the connection */
3674
3675 release_number = xcb_get_setup (c)-&gt;release_number;
3676
3677 /* release_number contains now the number of the release of the server hardware */
3678 </pre>
3679           <li class="subtitle"><a name="DisplayString"></a>DisplayString
3680           <p>
3681           The name of the display is not stored in XCB. You have to
3682           store it by yourself.
3683           </p>
3684           <li class="subtitle"><a name="BitmapUnit"></a>BitmapUnit
3685           <p>
3686           You get the bitmap scanline unit in the
3687           <span class="code">xcb_setup_t</span>
3688           structure, with the function <span class="code">xcb_get_setup</span>:
3689           </p>
3690           <pre class="code">
3691 xcb_connection_t *c;
3692 uint8_t           bitmap_format_scanline_unit;
3693
3694 /* you init the connection */
3695
3696 bitmap_format_scanline_unit = xcb_get_setup (c)-&gt;bitmap_format_scanline_unit;
3697
3698 /* bitmap_format_scanline_unit contains now the bitmap scanline unit */
3699 </pre>
3700           <li class="subtitle"><a name="BitmapBitOrder"></a>BitmapBitOrder
3701           <p>
3702           You get the bitmap bit order in the
3703           <span class="code">xcb_setup_t</span>
3704           structure, with the function <span class="code">xcb_get_setup</span>:
3705           </p>
3706           <pre class="code">
3707 xcb_connection_t *c;
3708 uint8_t           bitmap_format_bit_order;
3709
3710 /* you init the connection */
3711
3712 bitmap_format_bit_order = xcb_get_setup (c)-&gt;bitmap_format_bit_order;
3713
3714 /* bitmap_format_bit_order contains now the bitmap bit order */
3715 </pre>
3716           <li class="subtitle"><a name="BitmapPad"></a>BitmapPad
3717           <p>
3718           You get the bitmap scanline pad in the
3719           <span class="code">xcb_setup_t</span>
3720           structure, with the function <span class="code">xcb_get_setup</span>:
3721           </p>
3722           <pre class="code">
3723 xcb_connection_t *c;
3724 uint8_t           bitmap_format_scanline_pad;
3725
3726 /* you init the connection */
3727
3728 bitmap_format_scanline_pad = xcb_get_setup (c)-&gt;bitmap_format_scanline_pad;
3729
3730 /* bitmap_format_scanline_pad contains now the bitmap scanline pad */
3731 </pre>
3732           <li class="subtitle"><a name="ImageByteOrder"></a>ImageByteOrder
3733           <p>
3734           You get the image byte order in the
3735           <span class="code">xcb_setup_t</span>
3736           structure, with the function <span class="code">xcb_get_setup</span>:
3737           </p>
3738           <pre class="code">
3739 xcb_connection_t *c;
3740 uint8_t           image_byte_order;
3741
3742 /* you init the connection */
3743
3744 image_byte_order = xcb_get_setup (c)-&gt;image_byte_order;
3745
3746 /* image_byte_order contains now the image byte order */
3747 </pre>
3748         </ol>
3749       <li class="subtitle"><a name="screenofdisplay">ScreenOfDisplay related functions</a>
3750       <p>
3751       in Xlib, <span class="code">ScreenOfDisplay</span> returns a
3752       <span class="code">Screen</span> structure that contains
3753       several characteristics of your screen. XCB has a similar
3754       structure (<span class="code">xcb_screen_t</span>),
3755       but the way to obtain it is a bit different. With
3756       Xlib, you just provide the number of the screen and you grab it
3757       from an array. With XCB, you iterate over all the screens to
3758       obtain the one you want. The complexity of this operation is
3759       O(n). So the best is to store this structure if you use
3760       it often. See <a href="#ScreenOfDisplay">screen_of_display</a> just below.
3761       </p>
3762       <p>
3763       Xlib provides generally two functions to obtain the characteristics
3764       related to the screen. One with the display and the number of
3765       the screen, which calls <span class="code">ScreenOfDisplay</span>,
3766       and the other that uses the <span class="code">Screen</span> structure.
3767       This might be a bit confusing. As mentioned above, with XCB, it
3768       is better to store the <span class="code">xcb_screen_t</span>
3769       structure. Then, you have to read the members of this
3770       structure. That's why the Xlib functions are put by pairs (or
3771       more) as, with XCB, you will use the same code.
3772       </p>
3773         <ol>
3774           <li class="subtitle"><a name="ScreenOfDisplay">ScreenOfDisplay</a>
3775           <p>
3776           This function returns the Xlib <span class="code">Screen</span>
3777           structure. With XCB, you iterate over all the screens and
3778           once you get the one you want, you return it:
3779           </p>
3780           <pre class="code"><a name="ScreenOfDisplay"></a>
3781 xcb_screen_t *screen_of_display (xcb_connection_t *c,
3782                                  int               screen)
3783 {
3784   xcb_screen_iterator_t iter;
3785
3786   iter = xcb_setup_roots_iterator (xcb_get_setup (c));
3787   for (; iter.rem; --screen, xcb_screen_next (&amp;iter))
3788     if (screen == 0)
3789       return iter.data;
3790
3791   return NULL;
3792 }
3793 </pre>
3794           <p>
3795           As mentioned above, you might want to store the value
3796           returned by this function.
3797           </p>
3798           <p>
3799           All the functions below will use the result of that
3800           function, as they just grab a specific member of the
3801           <span class="code">xcb_screen_t</span> structure.
3802           </p>
3803           <li class="subtitle"><a name="DefaultScreenOfDisplay"></a>DefaultScreenOfDisplay
3804           <p>
3805           It is the default screen that you obtain when you connect to
3806           the X server. It suffices to call the <a href="#ScreenOfDisplay">screen_of_display</a>
3807           function above with the connection and the number of the
3808           default screen.
3809           </p>
3810           <pre class="code">
3811 xcb_connection_t *c;
3812 int               screen_default_nbr;
3813 xcb_screen_t     *default_screen;  /* the returned default screen */
3814
3815 /* you pass the name of the display you want to xcb_connect_t */
3816
3817 c = xcb_connect (display_name, &amp;screen_default_nbr);
3818 default_screen = screen_of_display (c, screen_default_nbr);
3819
3820 /* default_screen contains now the default root window, or a NULL window if no screen is found */
3821 </pre>
3822           <li class="subtitle"><a name="RootWindow">RootWindow / RootWindowOfScreen</a>
3823           <br>
3824           <pre class="code">
3825 xcb_connection_t *c;
3826 xcb_screen_t     *screen;
3827 int               screen_nbr;
3828 xcb_window_t      root_window = { 0 };  /* the returned window */
3829
3830 /* you init the connection and screen_nbr */
3831
3832 screen = screen_of_display (c, screen_nbr);
3833 if (screen)
3834   root_window = screen-&gt;root;
3835
3836 /* root_window contains now the root window, or a NULL window if no screen is found */
3837 </pre>
3838           <li class="subtitle"><a name="DefaultRootWindow">DefaultRootWindow</a>
3839           <p>
3840           It is the root window of the default screen. So, you call
3841           <a name="ScreenOfDisplay">ScreenOfDisplay</a> with the
3842           default screen number and you get the
3843           <a href="#RootWindow">root window</a> as above:
3844           </p>
3845           <pre class="code">
3846 xcb_connection_t *c;
3847 xcb_screen_t     *screen;
3848 int               screen_default_nbr;
3849 xcb_window_t      root_window = { 0 };  /* the returned root window */
3850
3851 /* you pass the name of the display you want to xcb_connect_t */
3852
3853 c = xcb_connect (display_name, &amp;screen_default_nbr);
3854 screen = screen_of_display (c, screen_default_nbr);
3855 if (screen)
3856   root_window = screen-&gt;root;
3857
3858 /* root_window contains now the default root window, or a NULL window if no screen is found */
3859 </pre>
3860           <li class="subtitle"><a name="DefaultVisual">DefaultVisual / DefaultVisualOfScreen</a>
3861           <p>
3862           While a Visual is, in Xlib, a structure, in XCB, there are
3863           two types: <span class="code">xcb_visualid_t</span>, which is
3864           the Id of the visual, and <span class="code">xcb_visualtype_t</span>,
3865           which corresponds to the Xlib Visual. To get the Id of the
3866           visual of a screen, just get the
3867            <span class="code">root_visual</span>
3868            member of a <span class="code">xcb_screen_t</span>:
3869           </p>
3870           <pre class="code">
3871 xcb_connection_t *c;
3872 xcb_screen_t     *screen;
3873 int               screen_nbr;
3874 xcb_visualid_t    root_visual = { 0 };    /* the returned visual Id */
3875
3876 /* you init the connection and screen_nbr */
3877
3878 screen = screen_of_display (c, screen_nbr);
3879 if (screen)
3880   root_visual = screen-&gt;root_visual;
3881
3882 /* root_visual contains now the value of the Id of the visual, or a NULL visual if no screen is found */
3883 </pre>
3884           <p>
3885           To get the <span class="code">xcb_visualtype_t</span>
3886           structure, it's a bit less easy. You have to get the
3887           <span class="code">xcb_screen_t</span> structure that you want,
3888           get its <span class="code">root_visual</span> member,
3889           then iterate over the <span class="code">xcb_depth_t</span>s
3890           and the <span class="code">xcb_visualtype_t</span>s, and compare
3891           the <span class="code">xcb_visualid_t</span> of these <span class="code">xcb_visualtype_t</span>s:
3892           with <span class="code">root_visual</span>:
3893           </p>
3894           <pre class="code">
3895 xcb_connection_t *c;
3896 xcb_screen_t     *screen;
3897 int               screen_nbr;
3898 xcb_visualid_t    root_visual = { 0 };
3899 xcb_visualtype_t  *visual_type = NULL;    /* the returned visual type */
3900
3901 /* you init the connection and screen_nbr */
3902
3903 screen = screen_of_display (c, screen_nbr);
3904 if (screen) {
3905   xcb_depth_iterator_t depth_iter;
3906
3907   depth_iter = xcb_screen_allowed_depths_iterator (screen);
3908   for (; depth_iter.rem; xcb_depth_next (&amp;depth_iter)) {
3909     xcb_visualtype_iterator_t visual_iter;
3910
3911     visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
3912     for (; visual_iter.rem; xcb_visualtype_next (&amp;visual_iter)) {
3913       if (screen-&gt;root_visual.id == visual_iter.data-&gt;visual_id.id) {
3914         visual_type = visual_iter.data;
3915         break;
3916       }
3917     }
3918   }
3919 }
3920
3921 /* visual_type contains now the visual structure, or a NULL visual structure if no screen is found */
3922 </pre>
3923           <li class="subtitle"><a name="DefaultGC">DefaultGC / DefaultGCOfScreen</a>
3924           <p>
3925           This default Graphic Context is just a newly created Graphic
3926           Context, associated to the root window of a
3927           <span class="code">xcb_screen_t</span>,
3928           using the black white pixels of that screen:
3929           </p>
3930           <pre class="code">
3931 xcb_connection_t *c;
3932 xcb_screen_t     *screen;
3933 int               screen_nbr;
3934 xcb_gcontext_t    gc = { 0 };    /* the returned default graphic context */
3935
3936 /* you init the connection and screen_nbr */
3937
3938 screen = screen_of_display (c, screen_nbr);
3939 if (screen) {
3940   xcb_drawable_t draw;
3941   uint32_t       mask;
3942   uint32_t       values[2];
3943
3944   gc = xcb_generate_id (c);
3945   draw = screen-&gt;root;
3946   mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
3947   values[0] = screen-&gt;black_pixel;
3948   values[1] = screen-&gt;white_pixel;
3949   xcb_create_gc (c, gc, draw, mask, values);
3950 }
3951
3952 /* gc contains now the default graphic context */
3953 </pre>
3954           <li class="subtitle"><a name="BlackPixel">BlackPixel / BlackPixelOfScreen</a>
3955           <p>
3956           It is the Id of the black pixel, which is in the structure
3957           of an <span class="code">xcb_screen_t</span>.
3958           </p>
3959           <pre class="code">
3960 xcb_connection_t *c;
3961 xcb_screen_t     *screen;
3962 int               screen_nbr;
3963 uint32_t          black_pixel = 0;    /* the returned black pixel */
3964
3965 /* you init the connection and screen_nbr */
3966
3967 screen = screen_of_display (c, screen_nbr);
3968 if (screen)
3969   black_pixel = screen-&gt;black_pixel;
3970
3971 /* black_pixel contains now the value of the black pixel, or 0 if no screen is found */
3972 </pre>
3973           <li class="subtitle"><a name="WhitePixel">WhitePixel / WhitePixelOfScreen</a>
3974           <p>
3975           It is the Id of the white pixel, which is in the structure
3976           of an <span class="code">xcb_screen_t</span>.
3977           </p>
3978           <pre class="code">
3979 xcb_connection_t *c;
3980 xcb_screen_t     *screen;
3981 int               screen_nbr;
3982 uint32_t          white_pixel = 0;    /* the returned white pixel */
3983
3984 /* you init the connection and screen_nbr */
3985
3986 screen = screen_of_display (c, screen_nbr);
3987 if (screen)
3988   white_pixel = screen-&gt;white_pixel;
3989
3990 /* white_pixel contains now the value of the white pixel, or 0 if no screen is found */
3991 </pre>
3992           <li class="subtitle"><a name="DisplayWidth">DisplayWidth / WidthOfScreen</a>
3993           <p>
3994           It is the width in pixels of the screen that you want, and
3995           which is in the structure of the corresponding
3996           <span class="code">xcb_screen_t</span>.
3997           </p>
3998           <pre class="code">
3999 xcb_connection_t *c;
4000 xcb_screen_t     *screen;
4001 int               screen_nbr;
4002 uint32_t          width_in_pixels = 0;    /* the returned width in pixels */
4003
4004 /* you init the connection and screen_nbr */
4005
4006 screen = screen_of_display (c, screen_nbr);
4007 if (screen)
4008   width_in_pixels = screen-&gt;width_in_pixels;
4009
4010 /* width_in_pixels contains now the width in pixels, or 0 if no screen is found */
4011 </pre>
4012           <li class="subtitle"><a name="DisplayHeight">DisplayHeight / HeightOfScreen</a>
4013           <p>
4014           It is the height in pixels of the screen that you want, and
4015           which is in the structure of the corresponding
4016           <span class="code">xcb_screen_t</span>.
4017           </p>
4018           <pre class="code">
4019 xcb_connection_t *c;
4020 xcb_screen_t     *screen;
4021 int               screen_nbr;
4022 uint32_t          height_in_pixels = 0;    /* the returned height in pixels */
4023
4024 /* you init the connection and screen_nbr */
4025
4026 screen = screen_of_display (c, screen_nbr);
4027 if (screen)
4028   height_in_pixels = screen-&gt;height_in_pixels;
4029
4030 /* height_in_pixels contains now the height in pixels, or 0 if no screen is found */
4031 </pre>
4032           <li class="subtitle"><a name="DisplayWidthMM">DisplayWidthMM / WidthMMOfScreen</a>
4033           <p>
4034           It is the width in millimeters of the screen that you want, and
4035           which is in the structure of the corresponding
4036           <span class="code">xcb_screen_t</span>.
4037           </p>
4038           <pre class="code">
4039 xcb_connection_t *c;
4040 xcb_screen_t     *screen;
4041 int               screen_nbr;
4042 uint32_t          width_in_millimeters = 0;    /* the returned width in millimeters */
4043
4044 /* you init the connection and screen_nbr */
4045
4046 screen = screen_of_display (c, screen_nbr);
4047 if (screen)
4048   width_in_millimeters = screen-&gt;width_in_millimeters;
4049
4050 /* width_in_millimeters contains now the width in millimeters, or 0 if no screen is found */
4051 </pre>
4052           <li class="subtitle"><a name="DisplayHeightMM">DisplayHeightMM / HeightMMOfScreen</a>
4053           <p>
4054           It is the height in millimeters of the screen that you want, and
4055           which is in the structure of the corresponding
4056           <span class="code">xcb_screen_t</span>.
4057           </p>
4058           <pre class="code">
4059 xcb_connection_t *c;
4060 xcb_screen_t     *screen;
4061 int               screen_nbr;
4062 uint32_t          height_in_millimeters = 0;    /* the returned height in millimeters */
4063
4064 /* you init the connection and screen_nbr */
4065
4066 screen = screen_of_display (c, screen_nbr);
4067 if (screen)
4068   height_in_millimeters = screen-&gt;height_in_millimeters;
4069
4070 /* height_in_millimeters contains now the height in millimeters, or 0 if no screen is found */
4071 </pre>
4072           <li class="subtitle"><a name="DisplayPlanes">DisplayPlanes / DefaultDepth / DefaultDepthOfScreen / PlanesOfScreen</a>
4073           <p>
4074           It is the depth (in bits) of the root window of the
4075           screen. You get it from the <span class="code">xcb_screen_t</span> structure.
4076           </p>
4077           <pre class="code">
4078 xcb_connection_t *c;
4079 xcb_screen_t     *screen;
4080 int               screen_nbr;
4081 uint8_t           root_depth = 0;  /* the returned depth of the root window */
4082
4083 /* you init the connection and screen_nbr */
4084
4085 screen = screen_of_display (c, screen_nbr);
4086 if (screen)
4087   root_depth = screen-&gt;root_depth;
4088
4089 /* root_depth contains now the depth of the root window, or 0 if no screen is found */
4090 </pre>
4091           <li class="subtitle"><a name="DefaultColormap">DefaultColormap / DefaultColormapOfScreen</a>
4092           <p>
4093           This is the default colormap of the screen (and not the
4094           (default) colormap of the default screen !). As usual, you
4095           get it from the <span class="code">xcb_screen_t</span> structure:
4096           </p>
4097           <pre class="code">
4098 xcb_connection_t *c;
4099 xcb_screen_t     *screen;
4100 int               screen_nbr;
4101 xcb_colormap_t    default_colormap = { 0 };  /* the returned default colormap */
4102
4103 /* you init the connection and screen_nbr */
4104
4105 screen = screen_of_display (c, screen_nbr);
4106 if (screen)
4107   default_colormap = screen-&gt;default_colormap;
4108
4109 /* default_colormap contains now the default colormap, or a NULL colormap if no screen is found */
4110 </pre>
4111           <li class="subtitle"><a name="MinCmapsOfScreen"></a>MinCmapsOfScreen
4112           <p>
4113           You get the minimum installed colormaps in the <span class="code">xcb_screen_t</span> structure:
4114           </p>
4115           <pre class="code">
4116 xcb_connection_t *c;
4117 xcb_screen_t     *screen;
4118 int               screen_nbr;
4119 uint16_t          min_installed_maps = 0;  /* the returned minimum installed colormaps */
4120
4121 /* you init the connection and screen_nbr */
4122
4123 screen = screen_of_display (c, screen_nbr);
4124 if (screen)
4125   min_installed_maps = screen-&gt;min_installed_maps;
4126
4127 /* min_installed_maps contains now the minimum installed colormaps, or 0 if no screen is found */
4128 </pre>
4129           <li class="subtitle"><a name="MaxCmapsOfScreen"></a>MaxCmapsOfScreen
4130           <p>
4131           You get the maximum installed colormaps in the <span class="code">xcb_screen_t</span> structure:
4132           </p>
4133           <pre class="code">
4134 xcb_connection_t *c;
4135 xcb_screen_t     *screen;
4136 int               screen_nbr;
4137 uint16_t          max_installed_maps = 0;  /* the returned maximum installed colormaps */
4138
4139 /* you init the connection and screen_nbr */
4140
4141 screen = screen_of_display (c, screen_nbr);
4142 if (screen)
4143   max_installed_maps = screen-&gt;max_installed_maps;
4144
4145 /* max_installed_maps contains now the maximum installed colormaps, or 0 if no screen is found */
4146 </pre>
4147           <li class="subtitle"><a name="DoesSaveUnders"></a>DoesSaveUnders
4148           <p>
4149           You know if <span class="code">save_unders</span> is set,
4150           by looking in the <span class="code">xcb_screen_t</span> structure:
4151           </p>
4152           <pre class="code">
4153 xcb_connection_t *c;
4154 xcb_screen_t     *screen;
4155 int               screen_nbr;
4156 uint8_t           save_unders = 0;  /* the returned value of save_unders */
4157
4158 /* you init the connection and screen_nbr */
4159
4160 screen = screen_of_display (c, screen_nbr);
4161 if (screen)
4162   save_unders = screen-&gt;save_unders;
4163
4164 /* save_unders contains now the value of save_unders, or FALSE if no screen is found */
4165 </pre>
4166           <li class="subtitle"><a name="DoesBackingStore"></a>DoesBackingStore
4167           <p>
4168           You know the value of <span class="code">backing_stores</span>,
4169           by looking in the <span class="code">xcb_screen_t</span> structure:
4170           </p>
4171           <pre class="code">
4172 xcb_connection_t *c;
4173 xcb_screen_t     *screen;
4174 int               screen_nbr;
4175 uint8_t           backing_stores = 0;  /* the returned value of backing_stores */
4176
4177 /* you init the connection and screen_nbr */
4178
4179 screen = screen_of_display (c, screen_nbr);
4180 if (screen)
4181   backing_stores = screen-&gt;backing_stores;
4182
4183 /* backing_stores contains now the value of backing_stores, or FALSE if no screen is found */
4184 </pre>
4185           <li class="subtitle"><a name="EventMaskOfScreen"></a>EventMaskOfScreen
4186           <p>
4187           To get the current input masks,
4188           you look in the <span class="code">xcb_screen_t</span> structure:
4189           </p>
4190           <pre class="code">
4191 xcb_connection_t *c;
4192 xcb_screen_t     *screen;
4193 int               screen_nbr;
4194 uint32_t          current_input_masks = 0;  /* the returned value of current input masks */
4195
4196 /* you init the connection and screen_nbr */
4197
4198 screen = screen_of_display (c, screen_nbr);
4199 if (screen)
4200   current_input_masks = screen-&gt;current_input_masks;
4201
4202 /* current_input_masks contains now the value of the current input masks, or FALSE if no screen is found */
4203 </pre>
4204         </ol>
4205       <li class="subtitle"><a name="misc">Miscellaneous macros</a>
4206         <ol>
4207           <li class="subtitle"><a name="DisplayOfScreen"></a>DisplayOfScreen
4208           <p>
4209           in Xlib, the <span class="code">Screen</span> structure
4210           stores its associated <span class="code">Display</span>
4211           structure. This is not the case in the X Window protocol,
4212           hence, it's also not the case in XCB. So you have to store
4213           it by yourself.
4214           </p>
4215           <li class="subtitle"><a name="DisplayCells"></a>DisplayCells / CellsOfScreen
4216           <p>
4217           To get the colormap entries,
4218           you look in the <span class="code">xcb_visualtype_t</span>
4219           structure, that you grab like <a class="subsection" href="#DefaultVisual">here</a>:
4220           </p>
4221           <pre class="code">
4222 xcb_connection_t *c;
4223 xcb_visualtype_t *visual_type;
4224 uint16_t          colormap_entries = 0;  /* the returned value of the colormap entries */
4225
4226 /* you init the connection and visual_type */
4227
4228 if (visual_type)
4229   colormap_entries = visual_type-&gt;colormap_entries;
4230
4231 /* colormap_entries contains now the value of the colormap entries, or FALSE if no screen is found */
4232 </pre>
4233         </ol>
4234       </ol>
4235     </ol>
4236   </div>
4237 </body>
4238
4239 </html>