1 <!DOCTYPE html public "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
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">
14 Basic Graphics Programming With The XCB Library
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>
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>
27 <li>Memory allocation for XCB structures
28 <li><a class="subsection" href="#events">Events</a>
30 <li><a class="section" href="#use">Using XCB-based programs</a>
32 <li><a class="subsection" href="#inst">Installation of XCB</a>
33 <li><a class="subsection" href="#comp">Compiling XCB-based programs</a>
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>
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>
44 <li><a class="section" href="#xevents">X Events</a>
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>
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>
57 <li><a class="subsection" href="#eventex">X events: a complete example</a>
59 <li><a class="section" href="#font">Handling text and fonts</a>
61 <li><a class="subsection" href="#fontstruct">The Font structure</a>
63 <li>Assigning a Font to a Graphic Context
64 <li>Drawing text in a window
68 <li>Root, parent and child windows
69 <li>Events propagation
71 <li><a class="section" href="#wm">Interacting with the window manager</a>
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
80 <li><a class="section" href="#winop">Simple window operations</a>
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>
90 <li><a class="section" href="#usecolor">Using colors to paint the rainbow</a>
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
97 <li><a class="section" href="#pixmaps">X Bitmaps and Pixmaps</a>
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>
106 <li><a class="subsection" href="#mousecursor">Messing with the mouse cursor</a>
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>
112 <li><a class="subsection" href="#translation">Translation of basic Xlib functions and macros</a>
114 <li><a class="subsection" href="#displaystructure">Members of the Display structure</a>
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>
130 <li><a class="subsection" href="#screenofdisplay">ScreenOfDisplay related functions</a>
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>
152 <li><a class="subsection" href="#misc">Miscellaneaous macros</a>
154 <li><a class="subsection" href="#DisplayOfScreen">DisplayOfScreen</a>
155 <li><a class="subsection" href="#DisplayCells">DisplayCells / CellsOfScreen</a>
160 <div class="section">
162 <li class="title"><a name="intro">Introduction</a>
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.
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>.
184 we need to start somewhere. More than this, knowing how things
185 work down below is never a bad idea.
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.
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:
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
211 <li><b>Threaded applications</b>: While Xlib does attempt to
212 support multithreading, the API makes this difficult and
214 <li><b>New extensions</b>: The Xlib infrastructure provides
215 limited support for the new creation of X extension client side
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
223 <li>Toolkit implementation.
224 <li>Direct protocol-level programming.
225 <li>Lightweight emulation of commonly used portions of the
229 <li class="title"><a name="Xmodel">The client and server model of the X window system</a>
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.
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.
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).
267 <li class="title"><a name="asynch">GUI programming: the asynchronous model</a>
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).
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.
291 So the way a GUI program looks is something like that:
294 <li>Perform initialization routines.
295 <li>Connect to the X server.
296 <li>Perform X-related initialization.
297 <li>While not finished:
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.
304 <li>Close down the connection to the X server.
305 <li>Perform cleanup operations.
308 <li class="title"><a name="notions">Basic XCB notions</a>
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.
319 <li class="subtitle"><a name="conn">The X Connection</a>
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.
332 <li class="subtitle"><a name="requestsreplies">Requests and
333 replies: the Xlib killers</a>
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>):
348 W-----RW-----RW-----RW-----R
351 <li>W: Writing request
352 <li>-: Stalled, waiting for data
356 The total time is N * (T_write + T_round_trip + T_read).
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:
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.
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.
384 #include <stdlib.h>
385 #include <stdio.h>
386 #include <string.h>
387 #include <sys/time.h>
389 #include <X11/XCB/xcb.h>
391 #include <X11/Xlib.h>
396 struct timeval timev;
398 gettimeofday(&timev, NULL);
400 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
408 xcb_intern_atom_cookie_t *cs;
421 c = xcb_connect (NULL, NULL);
424 atoms = (xcb_atom_t *)malloc (count * sizeof (atoms));
425 names = (char **)malloc (count * sizeof (char *));
428 for (i = 0; i < count; ++i) {
431 sprintf (buf, "NAME%d", i);
432 names[i] = strdup (buf);
438 for (i = 0; i < count; ++i)
439 atoms[i] = xcb_intern_atom_reply (c,
448 printf ("bad use time : %f\n", diff);
453 cs = (xcb_intern_atom_cookie_t *) malloc (count * sizeof(xcb_intern_atom_cookie_t));
454 for(i = 0; i < count; ++i)
455 cs[i] = xcb_intern_atom (c, 0, strlen(names[i]), names[i]);
457 for(i = 0; i < count; ++i) {
458 xcb_intern_atom_reply_t *r;
460 r = xcb_intern_atom_reply(c, cs[i], 0);
467 printf ("good use time : %f\n", end - start);
468 printf ("ratio : %f\n", diff / (end - start));
472 for (i = 0; i < count; ++i)
480 disp = XOpenDisplay (getenv("DISPLAY"));
482 atoms_x = (Atom *)malloc (count * sizeof (atoms_x));
486 for (i = 0; i < count; ++i)
487 atoms_x[i] = XInternAtom(disp, names[i], 0);
490 diff_x = end - start;
491 printf ("Xlib use time : %f\n", diff_x);
492 printf ("ratio : %f\n", diff_x / diff);
497 XCloseDisplay (disp);
502 <li class="subtitle"><a name="gc">The Graphic Context</a>
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
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.
524 <li class="subtitle"><a name="events">Events</a>
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.
538 <li class="title"><a name="use">Using XCB-based programs</a>
541 <li class="subtitle"><a name="inst">Installation of XCB</a>
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
549 To build XCB from source, you need to have installed at
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>
561 You have to checkout in the git repository the following modules:
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.
573 <li class="subtitle"><a name="comp">Compiling XCB-based programs</a>
575 Compiling XCB-based programs requires linking them with the XCB
576 library. This is easily done thanks to pkgconfig:
579 gcc -Wall prog.c -o prog `pkg-config --cflags --libs xcb`
582 <li class="title"><a name="openconn">Opening and closing the connection to an X server</a>
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.
590 <type>xcb_connection_t</type> *xcb_connect (<keyword>const</keyword> <type>char</type> *displayname,
591 <type>int</type> *screenp);
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:
599 #<include>include</include> <string><X11/XCB/xcb.h></string>
602 <function>main</function> ()
604 <type>xcb_connection_t</type> *c;
606 /* Open the connection to the X server. Use the DISPLAY environment variable as the default display name */
607 c = xcb_connect (NULL, NULL);
609 <keyword>return</keyword> 0;
613 To close a connection, it suffices to use:
616 <type>void</type> xcb_disconnect (<type>xcb_connection_t</type> *c);
639 <li>xcb_disconnect ()
645 <li class="title"><a name="screen">Checking basic information about a connection</a>
647 Once we have opened a connection to an X server, we should check some
648 basic information about it: what screens it has, what is the
649 size (width and height) of the screen, how many colors it
650 supports (black and white ? grey scale ?, 256 colors ? more ?),
651 and so on. We get such information from the xcb_screen_t
657 xcb_colormap_t default_colormap;
658 uint32_t white_pixel;
659 uint32_t black_pixel;
660 uint32_t current_input_masks;
661 uint16_t width_in_pixels;
662 uint16_t height_in_pixels;
663 uint16_t width_in_millimeters;
664 uint16_t height_in_millimeters;
665 uint16_t min_installed_maps;
666 uint16_t max_installed_maps;
667 xcb_visualid_t root_visual;
668 uint8_t backing_stores;
671 uint8_t allowed_depths_len;
675 We could retrieve the first screen of the connection by using the
679 xcb_screen_iterator_t xcb_setup_roots_iterator (xcb_setup_t *R);
682 Here is a small program that shows how to use this function:
685 #include <stdio.h>
687 #include <X11/XCB/xcb.h>
693 xcb_screen_t *screen;
695 xcb_screen_iterator_t iter;
697 /* Open the connection to the X server. Use the DISPLAY environment variable */
698 c = xcb_connect (NULL, &screen_nbr);
700 /* Get the screen #screen_nbr */
701 iter = xcb_setup_roots_iterator (xcb_get_setup (c));
702 for (; iter.rem; --screen_nbr, xcb_screen_next (&iter))
703 if (screen_nbr == 0) {
709 printf ("Informations of screen %ld:\n", screen->root.xid);
710 printf (" width.........: %d\n", screen->width_in_pixels);
711 printf (" height........: %d\n", screen->height_in_pixels);
712 printf (" white pixel...: %ld\n", screen->white_pixel);
713 printf (" black pixel...: %ld\n", screen->black_pixel);
719 <li class="title"><a name="helloworld">Creating a basic window - the "hello world" program</a>
721 After we got some basic information about our screen, we can
722 create our first window. In the X Window System, a window is
723 characterized by an Id. So, in XCB, a window is of type:
731 We first ask for a new Id for our window, with this function:
734 xcb_window_t xcb_window_new(xcb_connection_t *c);
737 Then, XCB supplies the following function to create new windows:
740 xcb_void_cookie_t xcb_create_window (xcb_connection_t *c, /* Pointer to the xcb_connection_t structure */
741 uint8_t depth, /* Depth of the screen */
742 xcb_window_t wid, /* Id of the window */
743 xcb_window_t parent, /* Id of an existing window that should be the parent of the new window */
744 int16_t x, /* X position of the top-left corner of the window (in pixels) */
745 int16_t y, /* Y position of the top-left corner of the window (in pixels) */
746 uint16_t width, /* Width of the window (in pixels) */
747 uint16_t height, /* Height of the window (in pixels) */
748 uint16_t border_width, /* Width of the window's border (in pixels) */
750 xcb_visualid_t visual,
752 const uint32_t *value_list);
755 The fact that we created the window does not mean that it will
756 be drawn on screen. By default, newly created windows are not
757 mapped on the screen (they are invisible). In order to make our
758 window visible, we use the function <span class="code">xcb_map_window()</span>, whose
762 xcb_void_cookie_t xcb_map_window (xcb_connection_t *c,
763 xcb_window_t window);
766 Finally, here is a small program to create a window of size
767 150x150 pixels, positioned at the top-left corner of the screen:
770 #include <unistd.h> /* pause() */
772 #include <X11/XCB/xcb.h>
778 xcb_screen_t *screen;
781 /* Open the connection to the X server */
782 c = xcb_connect (NULL, NULL);
784 /* Get the first screen */
785 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
787 /* Ask for our window's Id */
788 win.window = xcb_window_new(c);
790 /* Create the window */
791 xcb_create_window (c, /* Connection */
792 XCB_COPY_FROM_PARENT, /* depth (same as root)*/
793 win.window, /* window Id */
794 screen->root, /* parent window */
796 150, 150, /* width, height */
797 10, /* border_width */
798 XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
799 screen->root_visual, /* visual */
800 0, NULL); /* masks, not used yet */
802 /* Map the window on the screen */
803 xcb_map_window (c, win.window);
805 /* Make sure commands are sent before we pause, so window is shown */
808 pause (); /* hold client until Ctrl-C */
814 In this code, you see one more function - <span class="code">xcb_flush()</span>, not explained
815 yet. It is used to flush all the pending requests. More
816 precisely, there are 2 functions that do such things. The first
817 one is <span class="code">xcb_flush()</span>:
820 int xcb_flush (xcb_connection_t *c);
823 This function flushes all pending requests to the X server (much
824 like the <span class="code">fflush()</span> function is used to
825 flush standard output). The second function is
826 <span class="code">xcb_aux_sync()</span>:
829 int xcb_aux_sync (xcb_connection_t *c);
832 This functions also flushes all pending requests to the X
833 server, and then waits until the X server finishing processing
834 these requests. In a normal program, this will not be necessary
835 (we'll see why when we get to write a normal X program), but for
836 now, we put it there.
839 The window that is created by the above code has a default
840 background (gray). This one can be set to a specific color,
841 thanks to the two last parameters of
842 <span class="code">xcb_create_window()</span>, which are not
843 described yet. See the subsections
844 <a href="#winconf">Configuring a window</a> or
845 <a href="#winconf">Registering for event types using event masks</a>
846 for examples on how to use these parameters. In addition, as no
847 events are handled, you have to make a Ctrl-C to interrupt the
851 <b>TODO</b>: one should tell what these functions return and
852 about the generic error
865 <li>xcb_window_new ()
866 <li>xcb_create_window ()
871 <li class="title"><a name="drawing">Drawing in a window</a>
873 Drawing in a window can be done using various graphical
874 functions (drawing pixels, lines, rectangles, etc). In order to
875 draw in a window, we first need to define various general
876 drawing parameters (what line width to use, which color to draw
877 with, etc). This is done using a graphical context.
880 <li class="subtitle"><a name="allocgc">Allocating a Graphics Context</a>
882 As we said, a graphical context defines several attributes to
883 be used with the various drawing functions. For this, we
884 define a graphical context. We can use more than one graphical
885 context with a single window, in order to draw in multiple
886 styles (different colors, different line widths, etc). In XCB,
887 a Graphics Context is, as a window, characterized by an Id:
895 We first ask the X server to attribute an Id to our graphic
896 context with this function:
899 xcb_gcontext_t xcb_gcontext_new (xcb_connection_t *c);
902 Then, we set the attributes of the graphic context with this function:
905 xcb_void_cookie_t xcb_create_gc (xcb_connection_t *c,
907 xcb_drawable_t drawable,
909 const uint32_t *value_list);
912 We give now an example on how to allocate a graphic context
913 that specifies that each drawing function that uses it will
914 draw in foreground with a black color.
917 #include <X11/XCB/xcb.h>
923 xcb_screen_t *screen;
925 xcb_gcontext_t black;
929 /* Open the connection to the X server and get the first screen */
930 c = xcb_connect (NULL, NULL);
931 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
933 /* Create a black graphic context for drawing in the foreground */
934 win.window = screen->root;
935 black = xcb_gcontext_new (c);
936 mask = XCB_GC_FOREGROUND;
937 value[0] = screen->black_pixel;
938 xcb_create_gc (c, black, win, mask, value);
944 Note should be taken regarding the role of "value_mask" and
945 "value_list" in the prototype of <span class="code">xcb_create_gc()</span>. Since a
946 graphic context has many attributes, and since we often just
947 want to define a few of them, we need to be able to tell the
948 <span class="code">xcb_create_gc()</span> which attributes we
949 want to set. This is what the "value_mask" parameter is
950 for. We then use the "value_list" parameter to specify actual
951 values for the attribute we defined in "value_mask". Thus, for
952 each constant used in "value_list", we will use the matching
953 constant in "value_mask". In this case, we define a graphic
954 context with one attribute: when drawing (a point, a line,
955 etc), the foreground color will be black. The rest of the
956 attributes of this graphic context will be set to their
960 See the next Subsection for more details.
973 <li>xcb_gcontext_new ()
979 <li class="subtitle"><a name="changegc">Changing the attributes of a Graphics Context</a>
981 Once we have allocated a Graphic Context, we may need to
982 change its attributes (for example, changing the foreground
983 color we use to draw a line, or changing the attributes of the
984 font we use to display strings. See Subsections Drawing with a
985 color and Assigning a Font to a Graphic Context). This is done
986 by using this function:
989 xcb_void_cookie_t xcb_change_gc (xcb_connection_t *c, /* The XCB Connection */
990 xcb_gcontext_t gc, /* The Graphic Context */
991 uint32_t value_mask, /* Components of the Graphic Context that have to be set */
992 const uint32_t *value_list); /* Value as specified by value_mask */
995 The <span class="code">value_mask</span> parameter could take
996 any combination of these masks from the xcb_gc_t enumeration:
1000 <li>XCB_GC_PLANE_MASK
1001 <li>XCB_GC_FOREGROUND
1002 <li>XCB_GC_BACKGROUND
1003 <li>XCB_GC_LINE_WIDTH
1004 <li>XCB_GC_LINE_STYLE
1005 <li>XCB_GC_CAP_STYLE
1006 <li>XCB_GC_JOIN_STYLE
1007 <li>XCB_GC_FILL_STYLE
1008 <li>XCB_GC_FILL_RULE
1011 <li>XCB_GC_TILE_STIPPLE_ORIGIN_X
1012 <li>XCB_GC_TILE_STIPPLE_ORIGIN_Y
1014 <li>XCB_GC_SUBWINDOW_MODE
1015 <li>XCB_GC_GRAPHICS_EXPOSURES
1016 <li>XCB_GC_CLIP_ORIGIN_X
1017 <li>XCB_GC_CLIP_ORIGIN_Y
1018 <li>XCB_GC_CLIP_MASK
1019 <li>XCB_GC_DASH_OFFSET
1020 <li>XCB_GC_DASH_LIST
1024 It is possible to set several attributes at the same
1025 time (for example setting the attributes of a font and the
1026 color which will be used to display a string), by OR'ing these
1027 values in <span class="code">value_mask</span>. Then
1028 <span class="code">value_list</span> has to be an array which
1029 lists the value for the respective attributes. <b>These values
1030 must be in the same order as masks listed above.</b> See Subsection
1031 Drawing with a color to have an example.
1034 <b>TODO</b>: set the links of the 3 subsections, once they will
1038 <b>TODO</b>: give an example which sets several attributes.
1040 <li class="subtitle"><a name="drawingprim">Drawing primitives: point, line, box, circle,...</a>
1042 After we have created a Graphic Context, we can draw on a
1043 window using this Graphic Context, with a set of XCB
1044 functions, collectively called "drawing primitives". Let see
1048 To draw a point, or several points, we use
1051 xcb_void_cookie_t xcb_poly_point (xcb_connection_t *c, /* The connection to the X server */
1052 uint8_t coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */
1053 xcb_drawable_t drawable, /* The drawable on which we want to draw the point(s) */
1054 xcb_gcontext_t gc, /* The Graphic Context we use to draw the point(s) */
1055 uint32_t points_len, /* The number of points */
1056 const xcb_point_t *points); /* An array of points */
1059 The <span class="code">coordinate_mode</span> parameter
1060 specifies the coordinate mode. Available values are
1063 <li><span class="code">XCB_COORD_MODE_ORIGIN</span>
1064 <li><span class="code">XCB_COORD_MODE_PREVIOUS</span>
1067 If XCB_COORD_MODE_PREVIOUS is used, then all points but the first one
1068 are relative to the immediately previous point.
1071 The <span class="code">xcb_point_t</span> type is just a
1072 structure with two fields (the coordinates of the point):
1081 You could see an example in xpoints.c. <b>TODO</b> Set the link.
1084 To draw a line, or a polygonal line, we use
1087 xcb_void_cookie_t xcb_poly_line (xcb_connection_t *c, /* The connection to the X server */
1088 uint8_t coordinate_mode, /* Coordinate mode, usually set to XCB_COORD_MODE_ORIGIN */
1089 xcb_drawable_t drawable, /* The drawable on which we want to draw the line(s) */
1090 xcb_gcontext_t gc, /* The Graphic Context we use to draw the line(s) */
1091 uint32_t points_len, /* The number of points in the polygonal line */
1092 const xcb_point_t *points); /* An array of points */
1095 This function will draw the line between the first and the
1096 second points, then the line between the second and the third
1100 To draw a segment, or several segments, we use
1103 xcb_void_cookie_t xcb_poly_segment (xcb_connection_t *c, /* The connection to the X server */
1104 xcb_drawable_t drawable, /* The drawable on which we want to draw the segment(s) */
1105 xcb_gcontext_t gc, /* The Graphic Context we use to draw the segment(s) */
1106 uint32_t segments_len, /* The number of segments */
1107 const xcb_segment_t *segments); /* An array of segments */
1110 The <span class="code">xcb_segment_t</span> type is just a
1111 structure with four fields (the coordinates of the two points
1112 that define the segment):
1123 To draw a rectangle, or several rectangles, we use
1126 xcb_void_cookie_t xcb_poly_rectangle (xcb_connection_t *c, /* The connection to the X server */
1127 xcb_drawable_t drawable, /* The drawable on which we want to draw the rectangle(s) */
1128 xcb_gcontext_t gc, /* The Graphic Context we use to draw the rectangle(s) */
1129 uint32_t rectangles_len, /* The number of rectangles */
1130 const xcb_rectangle_t *rectangles); /* An array of rectangles */
1133 The <span class="code">xcb_rectangle_t</span> type is just a
1134 structure with four fields (the coordinates of the top-left
1135 corner of the rectangle, and its width and height):
1145 <!-- There's no coordinate_mode. Is it normal? -->
1146 <!-- [iano] Yes, it's not in the protocol. -->
1148 To draw an elliptical arc, or several elliptical arcs, we use
1151 xcb_void_cookie_t xcb_poly_arc (xcb_connection_t *c, /* The connection to the X server */
1152 xcb_drawable_t drawable, /* The drawable on which we want to draw the arc(s) */
1153 xcb_gcontext_t gc, /* The Graphic Context we use to draw the arc(s) */
1154 uint32_t arcs_len, /* The number of arcs */
1155 const xcb_arc_t *arcs); /* An array of arcs */
1158 The <span class="code">xcb_arc_t</span> type is a structure with
1163 int16_t x; /* Top left x coordinate of the rectangle surrounding the ellipse */
1164 int16_t y; /* Top left y coordinate of the rectangle surrounding the ellipse */
1165 uint16_t width; /* Width of the rectangle surrounding the ellipse */
1166 uint16_t height; /* Height of the rectangle surrounding the ellipse */
1167 int16_t angle1; /* Angle at which the arc begins */
1168 int16_t angle2; /* Angle at which the arc ends */
1173 Note: the angles are expressed in units of 1/64 of a degree,
1174 so to have an angle of 90 degrees, starting at 0,
1175 <span class="code">angle1 = 0</span> and
1176 <span class="code">angle2 = 90 << 6</span>. Positive angles
1177 indicate counterclockwise motion, while negative angles
1178 indicate clockwise motion.
1181 <!-- I think that (x,y) should be the center of the
1182 ellipse, and (width, height) the radius. It's more logical. -->
1183 <!-- iano: Yes, and I bet some toolkits do that.
1184 But the protocol (and many other graphics APIs) define arcs
1185 by bounding rectangles. -->
1187 The corresponding function which fill inside the geometrical
1188 object are listed below, without further explanation, as they
1189 are used as the above functions.
1192 To Fill a polygon defined by the points given as arguments ,
1196 xcb_void_cookie_t xcb_fill_poly (xcb_connection_t *c,
1197 xcb_drawable_t drawable,
1200 uint8_t coordinate_mode,
1201 uint32_t points_len,
1202 const xcb_point_t *points);
1205 The <span class="code">shape</span> parameter specifies a
1206 shape that helps the server to improve performance. Available
1210 <li><span class="code">XCB_POLY_SHAPE_COMPLEX</span>
1211 <li><span class="code">XCB_POLY_SHAPE_NONCONVEX</span>
1212 <li><span class="code">XCB_POLY_SHAPE_CONVEX</span>
1215 To fill one or several rectangles, we use
1218 xcb_void_cookie_t xcb_poly_fill_rectangle (xcb_connection_t *c,
1219 xcb_drawable_t drawable,
1221 uint32_t rectangles_len,
1222 const xcb_rectangle_t *rectangles);
1225 To fill one or several arcs, we use
1228 xcb_void_cookie_t xcb_poly_fill_arc (xcb_connection_t *c,
1229 xcb_drawable_t drawable,
1232 const xcb_arc_t *arcs);
1235 <a name="points.c"></a>
1237 To illustrate these functions, here is an example that draws
1238 four points, a polygonal line, two segments, two rectangles
1239 and two arcs. Remark that we use events for the first time, as
1240 an introduction to the next section.
1243 <b>TODO:</b> Use screen->root_depth for depth parameter.
1246 #include <stdlib.h>
1247 #include <stdio.h>
1249 #include <X11/XCB/xcb.h>
1254 xcb_connection_t *c;
1255 xcb_screen_t *screen;
1257 xcb_gcontext_t foreground;
1258 xcb_generic_event_t *e;
1262 /* geometric objects */
1263 xcb_point_t points[] = {
1269 xcb_point_t polyline[] = {
1271 { 5, 20}, /* rest of points are relative */
1275 xcb_segment_t segments[] = {
1277 {110, 25, 130, 60}};
1279 xcb_rectangle_t rectangles[] = {
1283 xcb_arc_t arcs[] = {
1284 {10, 100, 60, 40, 0, 90 << 6},
1285 {90, 100, 55, 40, 0, 270 << 6}};
1287 /* Open the connection to the X server */
1288 c = xcb_connect (NULL, NULL);
1290 /* Get the first screen */
1291 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
1293 /* Create black (foreground) graphic context */
1294 win.window = screen->root;
1296 foreground = xcb_gcontext_new (c);
1297 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
1298 values[0] = screen->black_pixel;
1300 xcb_create_gc (c, foreground, win, mask, values);
1302 /* Ask for our window's Id */
1303 win.window = xcb_window_new(c);
1305 /* Create the window */
1306 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1307 values[0] = screen->white_pixel;
1308 values[1] = XCB_EVENT_MASK_EXPOSURE;
1309 xcb_create_window (c, /* Connection */
1310 XCB_COPY_FROM_PARENT, /* depth */
1311 win.window, /* window Id */
1312 screen->root, /* parent window */
1314 150, 150, /* width, height */
1315 10, /* border_width */
1316 XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
1317 screen->root_visual, /* visual */
1318 mask, values); /* masks */
1320 /* Map the window on the screen */
1321 xcb_map_window (c, win.window);
1324 /* We flush the request */
1327 while ((e = xcb_wait_for_event (c))) {
1328 switch (e->response_type & ~0x80) {
1330 /* We draw the points */
1331 xcb_poly_point (c, XCB_COORD_MODE_ORIGIN, win, foreground, 4, points);
1333 /* We draw the polygonal line */
1334 xcb_poly_line (c, XCB_COORD_MODE_PREVIOUS, win, foreground, 4, polyline);
1336 /* We draw the segements */
1337 xcb_poly_segment (c, win, foreground, 2, segments);
1339 /* We draw the rectangles */
1340 xcb_poly_rectangle (c, win, foreground, 2, rectangles);
1342 /* We draw the arcs */
1343 xcb_poly_arc (c, win, foreground, 2, arcs);
1345 /* We flush the request */
1351 /* Unknown event type, ignore it */
1355 /* Free the Generic Event */
1363 <li class="title"><a name="xevents">X Events</a>
1365 In an X program, everything is driven by events. Event painting
1366 on the screen is sometimes done as a response to an event (an
1367 <span class="code">Expose</span> event). If part of a program's
1368 window that was hidden, gets exposed (e.g. the window was raised
1369 above other widows), the X server will send an "expose" event to
1370 let the program know it should repaint that part of the
1371 window. User input (key presses, mouse movement, etc) is also
1372 received as a set of events.
1375 <li class="subtitle"><a name="register">Registering for event types using event masks</a>
1377 During the creation of a window, you should give it what kind
1378 of events it wishes to receive. Thus, you may register for
1379 various mouse (also called pointer) events, keyboard events,
1380 expose events, and so on. This is done for optimizing the
1381 server-to-client connection (i.e. why send a program (that
1382 might even be running at the other side of the globe) an event
1383 it is not interested in ?)
1386 In XCB, you use the "value_mask" and "value_list" data in the
1387 <span class="code">xcb_create_window()</span> function to
1388 register for events. Here is how we register for
1389 <span class="code">Expose</span> event when creating a window:
1392 mask = XCB_CW_EVENT_MASK;
1393 valwin[0] = XCB_EVENT_MASK_EXPOSURE;
1394 win.window = xcb_window_new (c);
1395 xcb_create_window (c, depth, win.window, root->root,
1397 XCB_WINDOW_CLASS_INPUT_OUTPUT, root->root_visual,
1401 <span class="code">XCB_EVENT_MASK_EXPOSURE</span> is a constant defined
1402 in the xcb_event_mask_t enumeration in the "xproto.h" header file. If we wanted to register for several
1403 event types, we can logically "or" them, as follows:
1406 mask = XCB_CW_EVENT_MASK;
1407 valwin[0] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS;
1408 win.window = xcb_window_new (c);
1409 xcb_create_window (c, depth, win.window, root->root,
1411 XCB_WINDOW_CLASS_INPUT_OUTPUT, root->root_visual,
1415 This registers for <span class="code">Expose</span> events as
1416 well as for mouse button presses inside the created
1417 window. You should note that a mask may represent several
1421 The values that a mask could take are given
1422 by the <span class="code">xcb_cw_t</span> enumeration:
1426 XCB_CW_BACK_PIXMAP = 1L<<0,
1427 XCB_CW_BACK_PIXEL = 1L<<1,
1428 XCB_CW_BORDER_PIXMAP = 1L<<2,
1429 XCB_CW_BORDER_PIXEL = 1L<<3,
1430 XCB_CW_BIT_GRAVITY = 1L<<4,
1431 XCB_CW_WIN_GRAVITY = 1L<<5,
1432 XCB_CW_BACKING_STORE = 1L<<6,
1433 XCB_CW_BACKING_PLANES = 1L<<7,
1434 XCB_CW_BACKING_PIXEL = 1L<<8,
1435 XCB_CW_OVERRIDE_REDIRECT = 1L<<9,
1436 XCB_CW_SAVE_UNDER = 1L<<10,
1437 XCB_CW_EVENT_MASK = 1L<<11,
1438 XCB_CW_DONT_PROPAGATE = 1L<<12,
1439 XCB_CW_COLORMAP = 1L<<13,
1440 XCB_CW_CURSOR = 1L<<14
1444 <p>Note: we must be careful when setting the values of the valwin
1445 parameter, as they have to follow the order the
1446 <span class="code">xcb_cw_t</span> enumeration. Here is an
1451 mask = XCB_CW_EVENT_MASK | XCB_CW_BACK_PIXMAP;
1452 valwin[0] = XCB_NONE; /* for XCB_CW_BACK_PIXMAP (whose value is 1) */
1453 valwin[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS; /* for XCB_CW_EVENT_MASK, whose value (2048) */
1454 /* is greater than the one of XCB_CW_BACK_PIXMAP */
1457 If the window has already been created, we can use the
1458 <span class="code">xcb_configure_window()</span> function to set
1459 the events that the window will receive. The subsection
1460 <a href="#winconf">Configuring a window</a> shows its
1461 prototype. As an example, here is a piece of code that
1462 configures the window to receive the
1463 <span class="code">Expose</span> and
1464 <span class="code">ButtonPress</span> events:
1467 const static uint32_t values[] = { XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS };
1469 /* The connection c and the window win are supposed to be defined */
1471 xcb_configure_window (c, win, XCB_CW_EVENT_MASK, values);
1475 Note: A common bug programmers do is adding code to handle new
1476 event types in their program, while forgetting to add the
1477 masks for these events in the creation of the window. Such a
1478 programmer then should sit down for hours debugging his
1479 program, wondering "Why doesn't my program notice that I
1480 released the button?", only to find that they registered for
1481 button press events but not for button release events.
1484 <li class="subtitle"><a name="loop">Receiving events: writing the events loop</a>
1486 After we have registered for the event types we are interested
1487 in, we need to enter a loop of receiving events and handling
1488 them. There are two ways to receive events: a blocking way and
1493 <span class="code">xcb_wait_for_event (xcb_connection_t *c)</span>
1494 is the blocking way. It waits (so blocks...) until an event is
1495 queued in the X server. Then it retrieves it into a newly
1496 allocated structure (it dequeues it from the queue) and returns
1497 it. This structure has to be freed. The function returns
1498 <span class="code">NULL</span> if an error occurs.
1502 <span class="code">xcb_poll_for_event (xcb_connection_t *c, int
1503 *error)</span> is the non-blocking way. It looks at the event
1504 queue and returns (and dequeues too) an existing event into
1505 a newly allocated structure. This structure has to be
1506 freed. It returns <span class="code">NULL</span> if there is
1507 no event. If an error occurs, the parameter <span
1508 class="code">error</span> will be filled with the error
1512 There are various ways to write such a loop. We present two
1513 ways to write such a loop, with the two functions above. The
1514 first one uses <span class="code">xcb_wait_for_event_t</span>, which
1515 is similar to an event Xlib loop using only <span
1516 class="code">XNextEvent</span>:
1519 xcb_generic_event_t *e;
1521 while ((e = xcb_wait_for_event (c))) {
1522 switch (e->response_type & ~0x80) {
1524 /* Handle the Expose event type */
1525 xcb_expose_event_t *ev = (xcb_expose_event_t *)e;
1531 case XCB_BUTTON_PRESS: {
1532 /* Handle the ButtonPress event type */
1533 xcb_button_press_event_t *ev = (xcb_button_press_event_t *)e;
1540 /* Unknown event type, ignore it */
1544 /* Free the Generic Event */
1549 You will certainly want to use <span
1550 class="code">xcb_poll_for_event(xcb_connection_t *c, int
1551 *error)</span> if, in Xlib, you use <span
1552 class="code">XPending</span> or
1553 <span class="code">XCheckMaskEvent</span>:
1556 while (XPending (display)) {
1559 XNextEvent(d, &ev);
1561 /* Manage your event */
1565 Such a loop in XCB looks like:
1568 xcb_generic_event_t *ev;
1570 while ((ev = xcb_poll_for_event (conn, 0))) {
1571 /* Manage your event */
1575 The events are managed in the same way as with <span
1576 class="code">xcb_wait_for_event_t</span>.
1577 Obviously, we will need to give the user some way of
1578 terminating the program. This is usually done by handling a
1579 special "quit" event, as we will soon see.
1592 <li>xcb_wait_for_event ()
1598 <li>XCheckMaskEvent ()
1603 <li>xcb_poll_for_event ()
1608 <li class="subtitle"><a name="expose">Expose events</a>
1610 The <span class="code">Expose</span> event is one of the most
1611 basic (and most used) events an application may receive. It
1612 will be sent to us in one of several cases:
1615 <li>A window that covered part of our window has moved
1616 away, exposing part (or all) of our window.
1617 <li>Our window was raised above other windows.
1618 <li>Our window mapped for the first time.
1619 <li>Our window was de-iconified.
1622 You should note the implicit assumption hidden here: the
1623 contents of our window is lost when it is being obscured
1624 (covered) by either windows. One may wonder why the X server
1625 does not save this contents. The answer is: to save
1626 memory. After all, the number of windows on a display at a
1627 given time may be very large, and storing the contents of all
1628 of them might require a lot of memory. Actually, there is a
1629 way to tell the X server to store the contents of a window in
1630 special cases, as we will see later.
1633 When we get an <span class="code">Expose</span> event, we
1634 should take the event's data from the members of the following
1639 uint8_t response_type; /* The type of the event, here it is XCB_EXPOSE */
1642 xcb_window_t window; /* The Id of the window that receives the event (in case */
1643 /* our application registered for events on several windows */
1644 uint16_t x; /* The x coordinate of the top-left part of the window that needs to be redrawn */
1645 uint16_t y; /* The y coordinate of the top-left part of the window that needs to be redrawn */
1646 uint16_t width; /* The width of the part of the window that needs to be redrawn */
1647 uint16_t height; /* The height of the part of the window that needs to be redrawn */
1649 } xcb_expose_event_t;
1651 <li class="subtitle"><a name="userinput">Getting user input</a>
1653 User input traditionally comes from two sources: the mouse
1654 and the keyboard. Various event types exist to notify us of
1655 user input (a key being presses on the keyboard, a key being
1656 released on the keyboard, the mouse moving over our window,
1657 the mouse entering (or leaving) our window, and so on.
1660 <li class="subsubtitle"><a name="mousepressrelease">Mouse button press and release events</a>
1662 The first event type we will deal with is a mouse
1663 button-press (or button-release) event in our window. In
1664 order to register to such an event type, we should add one
1665 (or more) of the following masks when we create our window:
1668 <li><span class="code">XCB_EVENT_MASK_BUTTON_PRESS</span>: notify us
1669 of any button that was pressed in one of our windows.
1670 <li><span class="code">XCB_EVENT_MASK_BUTTON_RELEASE</span>: notify us
1671 of any button that was released in one of our windows.
1674 The structure to be checked for in our events loop is the
1675 same for these two events, and is the following:
1679 uint8_t response_type; /* The type of the event, here it is xcb_button_press_event_t or xcb_button_release_event_t */
1680 xcb_button_t detail;
1682 xcb_timestamp_t time; /* Time, in milliseconds the event took place in */
1688 int16_t event_x; /* The x coordinate where the mouse has been pressed in the window */
1689 int16_t event_y; /* The y coordinate where the mouse has been pressed in the window */
1690 uint16_t state; /* A mask of the buttons (or keys) during the event */
1691 uint8_t same_screen;
1692 } xcb_button_press_event_t;
1694 typedef xcb_button_press_event_t xcb_button_release_event_t;
1697 The <span class="code">time</span> field may be used to calculate "double-click"
1698 situations by an application (e.g. if the mouse button was
1699 clicked two times in a duration shorter than a given amount
1700 of time, assume this was a double click).
1703 The <span class="code">state</span> field is a mask of the buttons held down during
1704 the event. It is a bitwise OR of any of the following (from the xcb_button_mask_t and
1705 xcb_mod_mask_t enumerations):
1708 <li><span class="code">XCB_BUTTON_MASK_1</span>
1709 <li><span class="code">XCB_BUTTON_MASK_2</span>
1710 <li><span class="code">XCB_BUTTON_MASK_3</span>
1711 <li><span class="code">XCB_BUTTON_MASK_4</span>
1712 <li><span class="code">XCB_BUTTON_MASK_5</span>
1713 <li><span class="code">XCB_MOD_MASK_SHIFT</span>
1714 <li><span class="code">XCB_MOD_MASK_LOCK</span>
1715 <li><span class="code">XCB_MOD_MASK_CONTROL</span>
1716 <li><span class="code">XCB_MOD_MASK_1</span>
1717 <li><span class="code">XCB_MOD_MASK_2</span>
1718 <li><span class="code">XCB_MOD_MASK_3</span>
1719 <li><span class="code">XCB_MOD_MASK_4</span>
1720 <li><span class="code">XCB_MOD_MASK_5</span>
1723 Their names are self explanatory, where the first 5 refer to
1724 the mouse buttons that are being pressed, while the rest
1725 refer to various "special keys" that are being pressed (Mod1
1726 is usually the 'Alt' key or the 'Meta' key).
1729 <b>TODO:</b> Problem: it seems that the state does not
1730 change when clicking with various buttons.
1732 <li class="subsubtitle"><a name="mousemvnt">Mouse movement events</a>
1734 Similar to mouse button press and release events, we also
1735 can be notified of various mouse movement events. These can
1736 be split into two families. One is of mouse pointer
1737 movement while no buttons are pressed, and the second is a
1738 mouse pointer motion while one (or more) of the buttons are
1739 pressed (this is sometimes called "a mouse drag operation",
1740 or just "dragging"). The following event masks may be added
1741 during the creation of our window:
1744 <li><span class="code">XCB_EVENT_MASK_POINTER_MOTION</span>: events of
1745 the pointer moving in one of the windows controlled by our
1746 application, while no mouse button is held pressed.
1747 <li><span class="code">XCB_EVENT_MASK_BUTTON_MOTION</span>: Events of
1748 the pointer moving while one or more of the mouse buttons
1750 <li><span class="code">XCB_EVENT_MASK_BUTTON_1_MOTION</span>: same as
1751 <span class="code">XCB_EVENT_MASK_BUTTON_MOTION</span>, but only when
1752 the 1st mouse button is held pressed.
1753 <li><span class="code">XCB_EVENT_MASK_BUTTON_2_MOTION</span>,
1754 <span class="code">XCB_EVENT_MASK_BUTTON_3_MOTION</span>,
1755 <span class="code">XCB_EVENT_MASK_BUTTON_4_MOTION</span>,
1756 <span class="code">XCB_EVENT_MASK_BUTTON_5_MOTION</span>: same as
1757 <span class="code">XCB_EVENT_MASK_BUTTON_1_MOTION</span>, but
1758 respectively for 2nd, 3rd, 4th and 5th mouse button.
1761 The structure to be checked for in our events loop is the
1762 same for these events, and is the following:
1766 uint8_t response_type; /* The type of the event */
1769 xcb_timestamp_t time; /* Time, in milliseconds the event took place in */
1775 int16_t event_x; /* The x coordinate of the mouse when the event was generated */
1776 int16_t event_y; /* The y coordinate of the mouse when the event was generated */
1777 uint16_t state; /* A mask of the buttons (or keys) during the event */
1778 uint8_t same_screen;
1779 } xcb_motion_notify_event_t;
1781 <li class="subsubtitle"><a name="mouseenter">Mouse pointer enter and leave events</a>
1783 Another type of event that applications might be interested
1784 in, is a mouse pointer entering a window the program
1785 controls, or leaving such a window. Some programs use these
1786 events to show the user that the application is now in
1787 focus. In order to register for such an event type, we
1788 should add one (or more) of the following masks when we
1792 <li><span class="code">xcb_event_enter_window_t</span>: notify us
1793 when the mouse pointer enters any of our controlled
1795 <li><span class="code">xcb_event_leave_window_t</span>: notify us
1796 when the mouse pointer leaves any of our controlled
1800 The structure to be checked for in our events loop is the
1801 same for these two events, and is the following:
1805 uint8_t response_type; /* The type of the event */
1808 xcb_timestamp_t time; /* Time, in milliseconds the event took place in */
1814 int16_t event_x; /* The x coordinate of the mouse when the event was generated */
1815 int16_t event_y; /* The y coordinate of the mouse when the event was generated */
1816 uint16_t state; /* A mask of the buttons (or keys) during the event */
1817 uint8_t mode; /* The number of mouse button that was clicked */
1818 uint8_t same_screen_focus;
1819 } xcb_enter_notify_event_t;
1821 typedef xcb_enter_notify_event_t xcb_leave_notify_event_t;
1823 <li class="subsubtitle"><a name="focus">The keyboard focus</a>
1825 There may be many windows on a screen, but only a single
1826 keyboard attached to them. How does the X server then know
1827 which window should be sent a given keyboard input ? This is
1828 done using the keyboard focus. Only a single window on the
1829 screen may have the keyboard focus at a given time. There
1830 is a XCB function that allows a program to set the keyboard
1831 focus to a given window. The user can usually set the
1832 keyboard focus using the window manager (often by clicking
1833 on the title bar of the desired window). Once our window
1834 has the keyboard focus, every key press or key release will
1835 cause an event to be sent to our program (if it regsitered
1836 for these event types...).
1838 <li class="subsubtitle"><a name="keypress">Keyboard press and release events</a>
1840 If a window controlled by our program currently holds the
1841 keyboard focus, it can receive key press and key release
1842 events. So, we should add one (or more) of the following
1843 masks when we create our window:
1846 <li><span class="code">XCB_EVENT_MASK_KEY_PRESS</span>: notify us when
1847 a key was pressed while any of our controlled windows had
1849 <li><span class="code">XCB_EVENT_MASK_KEY_RELEASE</span>: notify us
1850 when a key was released while any of our controlled
1851 windows had the keyboard focus.
1854 The structure to be checked for in our events loop is the
1855 same for these two events, and is the following:
1859 uint8_t response_type; /* The type of the event */
1860 xcb_keycode_t detail;
1862 xcb_timestamp_t time; /* Time, in milliseconds the event took place in */
1871 uint8_t same_screen;
1872 } xcb_key_press_event_t;
1874 typedef xcb_key_press_event_t xcb_key_release_event_t;
1877 The <span class="code">detail</span> field refers to the
1878 physical key on the keyboard.
1881 <b>TODO:</b> Talk about getting the ASCII code from the key code.
1884 <li class="subtitle"><a name="eventex">X events: a complete example</a>
1886 As an example for handling events, we show a program that
1887 creates a window, enters an events loop and checks for all the
1888 events described above, and writes on the terminal the relevant
1889 characteristics of the event. With this code, it should be
1890 easy to add drawing operations, like those which have been
1894 #include <stdlib.h>
1895 #include <stdio.h>
1897 #include <X11/XCB/xcb.h>
1900 print_modifiers (uint32_t mask)
1902 const char **mod, *mods[] = {
1903 "Shift", "Lock", "Ctrl", "Alt",
1904 "Mod2", "Mod3", "Mod4", "Mod5",
1905 "Button1", "Button2", "Button3", "Button4", "Button5"
1907 printf ("Modifier mask: ");
1908 for (mod = mods ; mask; mask >>= 1, mod++)
1917 xcb_connection_t *c;
1918 xcb_screen_t *screen;
1920 xcb_generic_event_t *e;
1924 /* Open the connection to the X server */
1925 c = xcb_connect (NULL, NULL);
1927 /* Get the first screen */
1928 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
1930 /* Ask for our window's Id */
1931 win.window = xcb_window_new (c);
1933 /* Create the window */
1934 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1935 values[0] = screen->white_pixel;
1936 values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS |
1937 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION |
1938 XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
1939 XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE;
1940 xcb_create_window (c, /* Connection */
1942 win.window, /* window Id */
1943 screen->root, /* parent window */
1945 150, 150, /* width, height */
1946 10, /* border_width */
1947 XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
1948 screen->root_visual, /* visual */
1949 mask, values); /* masks */
1951 /* Map the window on the screen */
1952 xcb_map_window (c, win.window);
1956 while ((e = xcb_wait_for_event (c))) {
1957 switch (e->response_type & ~0x80) {
1959 xcb_expose_event_t *ev = (xcb_expose_event_t *)e;
1961 printf ("Window %ld exposed. Region to be redrawn at location (%d,%d), with dimension (%d,%d)\n",
1962 ev->window.xid, ev->x, ev->y, ev->width, ev->height);
1965 case XCB_BUTTON_PRESS: {
1966 xcb_button_press_event_t *ev = (xcb_button_press_event_t *)e;
1967 print_modifiers(ev->state);
1969 switch (ev->detail.id) {
1971 printf ("Wheel Button up in window %ld, at coordinates (%d,%d)\n",
1972 ev->event.xid, ev->event_x, ev->event_y);
1975 printf ("Wheel Button down in window %ld, at coordinates (%d,%d)\n",
1976 ev->event.xid, ev->event_x, ev->event_y);
1979 printf ("Button %d pressed in window %ld, at coordinates (%d,%d)\n",
1980 ev->detail.id, ev->event.xid, ev->event_x, ev->event_y);
1984 case XCB_BUTTON_RELEASE: {
1985 xcb_button_release_event_t *ev = (xcb_button_release_event_t *)e;
1986 print_modifiers(ev->state);
1988 printf ("Button %d released in window %ld, at coordinates (%d,%d)\n",
1989 ev->detail.id, ev->event.xid, ev->event_x, ev->event_y);
1992 case XCB_MOTION_NOTIFY: {
1993 xcb_motion_notify_event_t *ev = (xcb_motion_notify_event_t *)e;
1995 printf ("Mouse moved in window %ld, at coordinates (%d,%d)\n",
1996 ev->event.xid, ev->event_x, ev->event_y);
1999 case XCB_ENTER_NOTIFY: {
2000 xcb_enter_notify_event_t *ev = (xcb_enter_notify_event_t *)e;
2002 printf ("Mouse entered window %ld, at coordinates (%d,%d)\n",
2003 ev->event.xid, ev->event_x, ev->event_y);
2006 case XCB_LEAVE_NOTIFY: {
2007 xcb_leave_notify_event_t *ev = (xcb_leave_notify_event_t *)e;
2009 printf ("Mouse left window %ld, at coordinates (%d,%d)\n",
2010 ev->event.xid, ev->event_x, ev->event_y);
2013 case XCB_KEY_PRESS: {
2014 xcb_key_press_event_t *ev = (xcb_key_press_event_t *)e;
2015 print_modifiers(ev->state);
2017 printf ("Key pressed in window %ld\n",
2021 case XCB_KEY_RELEASE: {
2022 xcb_key_release_event_t *ev = (xcb_key_release_event_t *)e;
2023 print_modifiers(ev->state);
2025 printf ("Key released in window %ld\n",
2030 /* Unknown event type, ignore it */
2031 printf("Unknown event: %d\n", e->response_type);
2034 /* Free the Generic Event */
2042 <li class="title"><a name="font">Handling text and fonts</a>
2044 Besides drawing graphics on a window, we often want to draw
2045 text. Text strings have two major properties: the characters to
2046 be drawn and the font with which they are drawn. In order to
2047 draw text, we need to first request the X server to load a
2048 font. We then assign a font to a Graphic Context, and finally, we
2049 draw the text in a window, using the Graphic Context.
2052 <li class="subtitle"><a name="fontstruct">The Font structure</a>
2054 In order to support flexible fonts, a font structure is
2055 defined. You know what ? It's an Id:
2063 It is used to contain information about a font, and is passed
2064 to several functions that handle fonts selection and text drawing.
2067 <b>TODO:</b> example for picking a font and displaying some text.
2068 Even better, also demonstrate translating keypresses to text.
2071 <li class="title"><a name="wm">Interacting with the window manager</a>
2073 After we have seen how to create windows and draw on them, we
2074 take one step back, and look at how our windows are interacting
2075 with their environment (the full screen and the other
2076 windows). First of all, our application needs to interact with
2077 the window manager. The window manager is responsible to
2078 decorating drawn windows (i.e. adding a frame, an iconify
2079 button, a system menu, a title bar, etc), as well as handling
2080 icons shown when windows are being iconified. It also handles
2081 ordering of windows on the screen, and other administrative
2082 tasks. We need to give it various hints as to how we want it to
2083 treat our application's windows.
2086 <li class="subtitle"><a name="wmprop">Window properties</a>
2088 Many of the parameters communicated to the window manager are
2089 passed using data called "properties". These properties are
2090 attached by the X server to different windows, and are stored
2091 in a format that makes it possible to read them from different
2092 machines that may use different architectures (remember that
2093 an X client program may run on a remote machine).
2096 The property and its type (a string, an integer, etc) are
2097 Id. Their type are <span class="code">xcb_atom_t</span>:
2105 To change the property of a window, we use the following
2109 xcb_void_cookie_t xcb_change_property (xcb_connection_t *c, /* Connection to the X server */
2110 uint8_t mode, /* Property mode */
2111 xcb_window_t window, /* Window */
2112 xcb_atom_t property, /* Property to change */
2113 xcb_atom_t type, /* Type of the property */
2114 uint8_t format, /* Format of the property (8, 16, 32) */
2115 uint32_t data_len, /* Length of the data parameter */
2116 const void *data); /* Data */
2119 The <span class="code">mode</span> parameter coud be one of
2120 the following values (defined in enumeration xcb_prop_mode_t in
2121 the xproto.h header file):
2124 <li>XCB_PROP_MODE_REPLACE
2125 <li>XCB_PROP_MODE_PREPEND
2126 <li>XCB_PROP_MODE_APPEND
2129 <li class="subtitle"><a name="wmname">Setting the window name and icon name</a>
2131 The first thing we want to do would be to set the name for our
2132 window. This is done using the
2133 <span class="code">xcb_change_property()</span> function. This
2134 name may be used by the window manager as the title of the
2135 window (in the title bar), in a task list, etc. The property
2136 atom to use to set the name of a window is
2137 <span class="code">WM_NAME</span> (and
2138 <span class="code">WM_ICON_NAME</span> for the iconified
2139 window) and its type is <span class="code">STRING</span>. Here
2140 is an example of utilization:
2143 #include <string.h>
2145 #include <X11/XCB/xcb.h>
2146 #include <X11/XCB/xcb_atom.h>
2151 xcb_connection_t *c;
2152 xcb_screen_t *screen;
2154 char *title = "Hello World !";
2155 char *title_icon = "Hello World ! (iconified)";
2159 /* Open the connection to the X server */
2160 c = xcb_connect (NULL, NULL);
2162 /* Get the first screen */
2163 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2165 /* Ask for our window's Id */
2166 win = xcb_window_new (c);
2168 /* Create the window */
2169 xcb_create_window (c, /* Connection */
2171 win, /* window Id */
2172 screen->root, /* parent window */
2174 250, 150, /* width, height */
2175 10, /* border_width */
2176 XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
2177 screen->root_visual, /* visual */
2178 0, NULL); /* masks, not used */
2180 /* Set the title of the window */
2181 xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
2183 strlen (title), title);
2185 /* Set the title of the window icon */
2186 xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
2187 WM_ICON_NAME, STRING, 8,
2188 strlen(title_icon), title_icon);
2190 /* Map the window on the screen */
2191 xcb_map_window (c, win);
2201 <p>Note: the use of the atoms needs our program to be compiled
2202 and linked against xcb_atom, so that we have to use
2206 gcc prog.c -o prog `pkg-config --cflags --libs xcb_atom`
2210 for the program to compile fine.
2214 <li class="title"><a name="winop">Simple window operations</a>
2216 One more thing we can do to our window is manipulate them on the
2217 screen (resize them, move them, raise or lower them, iconify
2218 them, and so on). Some window operations functions are supplied
2219 by XCB for this purpose.
2222 <li class="subtitle"><a name="winmap">Mapping and un-mapping a window</a>
2224 The first pair of operations we can apply on a window is
2225 mapping it, or un-mapping it. Mapping a window causes the
2226 window to appear on the screen, as we have seen in our simple
2227 window program example. Un-mapping it causes it to be removed
2228 from the screen (although the window as a logical entity still
2229 exists). This gives the effect of making a window hidden
2230 (unmapped) and shown again (mapped). For example, if we have a
2231 dialog box window in our program, instead of creating it every
2232 time the user asks to open it, we can create the window once,
2233 in an un-mapped mode, and when the user asks to open it, we
2234 simply map the window on the screen. When the user clicked the
2235 'OK' or 'Cancel' button, we simply un-map the window. This is
2236 much faster than creating and destroying the window, however,
2237 the cost is wasted resources, both on the client side, and on
2241 To map a window, you use the following function:
2244 xcb_void_cookie_t xcb_map_window (xcb_connection_t *c,
2245 xcb_window_t window);
2248 To have a simple example, see the <a href="#helloworld">example</a>
2249 above. The mapping operation will cause an
2250 <span class="code">Expose</span> event to be sent to our
2251 application, unless the window is completely covered by other
2255 Un-mapping a window is also simple. You use the function
2258 xcb_void_cookie_t xcb_unmap_window (xcb_connection_t *c,
2259 xcb_window_t window);
2262 The utilization of this function is the same as
2263 <span class="code">xcb_map_window()</span>.
2265 <li class="subtitle"><a name="winconf">Configuring a window</a>
2267 As we have seen when we have created our first window, in the
2268 X Events subsection, we can set some attributes for the window
2269 (that is, the position, the size, the events the window will
2270 receive, etc). If we want to modify them, but the window is
2271 already created, we can change them by using the following
2275 xcb_void_cookie_t xcb_configure_window (xcb_connection_t *c, /* The connection to the X server*/
2276 xcb_window_t window, /* The window to configure */
2277 uint16_t value_mask, /* The mask */
2278 const uint32_t *value_list); /* The values to set */
2281 We set the <span class="code">value_mask</span> to one or
2282 several mask values that are in the xcb_config_window_t enumeration in the xproto.h header:
2285 <li><span class="code">XCB_CONFIG_WINDOW_X</span>: new x coordinate of the window's top left corner
2286 <li><span class="code">XCB_CONFIG_WINDOW_Y</span>: new y coordinate of the window's top left corner
2287 <li><span class="code">XCB_CONFIG_WINDOW_WIDTH</span>: new width of the window
2288 <li><span class="code">XCB_CONFIG_WINDOW_HEIGHT</span>: new height of the window
2289 <li><span class="code">XCB_CONFIG_WINDOW_BORDER_WIDTH</span>: new width of the border of the window
2290 <li><span class="code">XCB_CONFIG_WINDOW_SIBLING</span>
2291 <li><span class="code">XCB_CONFIG_WINDOW_STACK_MODE</span>: the new stacking order
2294 We then give to <span class="code">value_mask</span> the new
2295 value. We now describe how to use
2296 <span class="code">xcb_configure_window_t</span> in some useful
2299 <li class="subtitle"><a name="winmove">Moving a window around the screen</a>
2301 An operation we might want to do with windows is to move them
2302 to a different location. This can be done like this:
2305 const static uint32_t values[] = { 10, 20 };
2307 /* The connection c and the window win are supposed to be defined */
2309 /* Move the window to coordinates x = 10 and y = 20 */
2310 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values);
2313 Note that when the window is moved, it might get partially
2314 exposed or partially hidden by other windows, and thus we
2315 might get <span class="code">Expose</span> events due to this
2318 <li class="subtitle"><a name="winsize">Resizing a window</a>
2320 Yet another operation we can do is to change the size of a
2321 window. This is done using the following code:
2324 const static uint32_t values[] = { 200, 300 };
2326 /* The connection c and the window win are supposed to be defined */
2328 /* Resize the window to width = 10 and height = 20 */
2329 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, values);
2332 We can also combine the move and resize operations using one
2333 single call to <span class="code">xcb_configure_window_t</span>:
2336 const static uint32_t values[] = { 10, 20, 200, 300 };
2338 /* The connection c and the window win are supposed to be defined */
2340 /* Move the window to coordinates x = 10 and y = 20 */
2341 /* and resize the window to width = 10 and height = 20 */
2342 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, values);
2344 <li class="subtitle"><a name="winstack">Changing windows stacking order: raise and lower</a>
2346 Until now, we changed properties of a single window. We'll see
2347 that there are properties that relate to the window and other
2348 windows. One of them is the stacking order. That is, the order
2349 in which the windows are layered on top of each other. The
2350 front-most window is said to be on the top of the stack, while
2351 the back-most window is at the bottom of the stack. Here is
2352 how to manipulate our windows stack order:
2355 const static uint32_t values[] = { XCB_STACK_MODE_ABOVE };
2357 /* The connection c and the window win are supposed to be defined */
2359 /* Move the window on the top of the stack */
2360 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
2363 const static uint32_t values[] = { XCB_STACK_MODE_BELOW };
2365 /* The connection c and the window win are supposed to be defined */
2367 /* Move the window on the bottom of the stack */
2368 xcb_configure_window (c, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
2370 <li class="subtitle"><a name="wingetinfo">Getting information about a window</a>
2372 Just like we can set various attributes of our windows, we can
2373 also ask the X server supply the current values of these
2374 attributes. For example, we can check where a window is
2375 located on the screen, what is its current size, whether it is
2376 mapped or not, etc. The structure that contains some of this
2381 uint8_t response_type;
2382 uint8_t depth; /* depth of the window */
2385 xcb_window_t root; /* Id of the root window *>
2386 int16_t x; /* X coordinate of the window's location */
2387 int16_t y; /* Y coordinate of the window's location */
2388 uint16_t width; /* Width of the window */
2389 uint16_t height; /* Height of the window */
2390 uint16_t border_width; /* Width of the window's border */
2391 } xcb_get_geometry_reply_t;
2394 XCB fill this structure with two functions:
2397 xcb_get_geometry_cookie_t xcb_get_geometry (xcb_connection_t *c,
2398 xcb_drawable_t drawable);
2399 xcb_get_geometry_reply_t *xcb_get_geometry_reply (xcb_connection_t *c,
2400 xcb_get_geometry_cookie_t cookie,
2401 xcb_generic_error_t **e);
2404 You use them as follows:
2407 xcb_connection_t *c;
2409 xcb_get_geometry_reply_t *geom;
2411 /* You initialize c and win */
2413 geom = xcb_get_geometry_reply (c, xcb_get_geometry (c, win), NULL);
2415 /* Do something with the fields of geom */
2420 Remark that you have to free the structure, as
2421 <span class="code">xcb_get_geometry_reply_t</span> allocates a
2425 One problem is that the returned location of the window is
2426 relative to its parent window. This makes these coordinates
2427 rather useless for any window manipulation functions, like
2428 moving it on the screen. In order to overcome this problem, we
2429 need to take a two-step operation. First, we find out the Id
2430 of the parent window of our window. We then translate the
2431 above relative coordinates to the screen coordinates.
2434 To get the Id of the parent window, we need this structure:
2438 uint8_t response_type;
2443 xcb_window_t parent; /* Id of the parent window */
2444 uint16_t children_len;
2446 } xcb_query_tree_reply_t;
2449 To fill this structure, we use these two functions:
2452 xcb_query_tree_cookie_t xcb_query_tree (xcb_connection_t *c,
2453 xcb_window_t window);
2454 xcb_query_tree_reply_t *xcb_query_tree_reply (xcb_connection_t *c,
2455 xcb_query_tree_cookie_t cookie,
2456 xcb_generic_error_t **e);
2459 The translated coordinates will be found in this structure:
2463 uint8_t response_type;
2464 uint8_t same_screen;
2468 uint16_t dst_x; /* Translated x coordinate */
2469 uint16_t dst_y; /* Translated y coordinate */
2470 } xcb_translate_coordinates_reply_t;
2473 As usual, we need two functions to fill this structure:
2476 xcb_translate_coordinates_cookie_t xcb_translate_coordinates (xcb_connection_t *c,
2477 xcb_window_t src_window,
2478 xcb_window_t dst_window,
2481 xcb_translate_coordinates_reply_t *xcb_translate_coordinates_reply (xcb_connection_t *c,
2482 xcb_translate_coordinates_cookie_t cookie,
2483 xcb_generic_error_t **e);
2486 We use them as follows:
2489 xcb_connection_t *c;
2491 xcb_get_geometry_reply_t *geom;
2492 xcb_query_tree_reply_t *tree;
2493 xcb_translate_coordinates_reply_t *trans;
2495 /* You initialize c and win */
2497 geom = xcb_get_geometry_reply (c, xcb_get_geometry (c, win), NULL);
2501 tree = xcb_query_tree_reply (c, xcb_query_tree (c, win), NULL);
2505 trans = xcb_translate_coordinates_reply (c,
2506 xcb_translate_coordinates (c,
2509 geom->x, geom->y),
2514 /* the translated coordinates are in trans->dst_x and trans->dst_y */
2521 Of course, as for <span class="code">geom</span>,
2522 <span class="code">tree</span> and
2523 <span class="code">trans</span> have to be freed.
2526 The work is a bit hard, but XCB is a very low-level library.
2529 <b>TODO:</b> the utilization of these functions should be a
2530 prog, which displays the coordinates of the window.
2533 There is another structure that gives informations about our window:
2537 uint8_t response_type;
2538 uint8_t backing_store;
2541 xcb_visualid_t visual; /* Visual of the window */
2543 uint8_t bit_gravity;
2544 uint8_t win_gravity;
2545 uint32_t backing_planes;
2546 uint32_t backing_pixel;
2548 uint8_t map_is_installed;
2549 uint8_t map_state; /* Map state of the window */
2550 uint8_t override_redirect;
2551 xcb_colormap_t colormap; /* Colormap of the window */
2552 uint32_t all_event_masks;
2553 uint32_t your_event_mask;
2554 uint16_t do_not_propagate_mask;
2555 } xcb_get_window_attributes_reply_t;
2558 XCB supplies these two functions to fill it:
2561 xcb_get_window_attributes_cookie_t xcb_get_window_attributes (xcb_connection_t *c,
2562 xcb_window_t window);
2563 xcb_get_window_attributes_reply_t *xcb_get_window_attributes_reply (xcb_connection_t *c,
2564 xcb_get_window_attributes_cookie_t cookie,
2565 xcb_generic_error_t **e);
2568 You use them as follows:
2571 xcb_connection_t *c;
2573 xcb_get_window_attributes_reply_t *attr;
2575 /* You initialize c and win */
2577 attr = xcb_get_window_attributes_reply (c, xcb_get_window_attributes (c, win), NULL);
2582 /* Do something with the fields of attr */
2587 As for <span class="code">geom</span>,
2588 <span class="code">attr</span> has to be freed.
2591 <li class="title"><a name="usecolor">Using colors to paint the rainbow</a>
2593 Up until now, all our painting operation were done using black
2594 and white. We will (finally) see now how to draw using colors.
2597 <li class="subtitle"><a name="colormap">Color maps</a>
2599 In the beginning, there were not enough colors. Screen
2600 controllers could only support a limited number of colors
2601 simultaneously (initially 2, then 4, 16 and 256). Because of
2602 this, an application could not just ask to draw in a "light
2603 purple-red" color, and expect that color to be available. Each
2604 application allocated the colors it needed, and when all the
2605 color entries (4, 16, 256 colors) were in use, the next color
2606 allocation would fail.
2609 Thus, the notion of "a color map" was introduced. A color map
2610 is a table whose size is the same as the number of
2611 simultaneous colors a given screen controller. Each entry
2612 contained the RGB (Red, Green and Blue) values of a different
2613 color (all colors can be drawn using some combination of red,
2614 green and blue). When an application wants to draw on the
2615 screen, it does not specify which color to use. Rather, it
2616 specifies which color entry of some color map to be used
2617 during this drawing. Change the value in this color map entry
2618 and the drawing will use a different color.
2621 In order to be able to draw using colors that got something to
2622 do with what the programmer intended, color map allocation
2623 functions are supplied. You could ask to allocate entry for a
2624 color with a set of RGB values. If one already existed, you
2625 would get its index in the table. If none existed, and the
2626 table was not full, a new cell would be allocated to contain
2627 the given RGB values, and its index returned. If the table was
2628 full, the procedure would fail. You could then ask to get a
2629 color map entry with a color that is closest to the one you
2630 were asking for. This would mean that the actual drawing on
2631 the screen would be done using colors similar to what you
2632 wanted, but not the same.
2635 On today's more modern screens where one runs an X server with
2636 support for 16 million colors, this limitation looks a little
2637 silly, but remember that there are still older computers with
2638 older graphics cards out there. Using color map, support for
2639 these screen becomes transparent to you. On a display
2640 supporting 16 million colors, any color entry allocation
2641 request would succeed. On a display supporting a limited
2642 number of colors, some color allocation requests would return
2643 similar colors. It won't look as good, but your application
2646 <li class="subtitle"><a name="colormapalloc">Allocating and freeing Color Maps</a>
2648 When you draw using XCB, you can choose to use the standard
2649 color map of the screen your window is displayed on, or you
2650 can allocate a new color map and apply it to a window. In the
2651 latter case, each time the mouse moves onto your window, the
2652 screen color map will be replaced by your window's color map,
2653 and you'll see all the other windows on screen change their
2654 colors into something quite bizzare. In fact, this is the
2655 effect you get with X applications that use the "-install"
2656 command line option.
2659 In XCB, a color map is (as often in X) an Id:
2667 In order to access the screen's default color map, you just
2668 have to retrieve the <span class="code">default_colormap</span>
2669 field of the <span class="code">xcb_screen_t</span> structure
2671 <a href="#screen">Checking basic information about a connection</a>):
2674 #include <stdio.h>
2676 #include <X11/XCB/xcb.h>
2681 xcb_connection_t *c;
2682 xcb_screen_t *screen;
2683 xcb_colormap_t colormap;
2685 /* Open the connection to the X server and get the first screen */
2686 c = xcb_connect (NULL, NULL);
2687 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2689 colormap = screen->default_colormap;
2695 This will return the color map used by default on the first
2696 screen (again, remember that an X server may support several
2697 different screens, each of which might have its own resources).
2700 The other option, that of allocating a new colormap, works as
2701 follows. We first ask the X server to give an Id to our color
2702 map, with this function:
2705 xcb_colormap_t xcb_colormap_new (xcb_connection_t *c);
2708 Then, we create the color map with
2711 xcb_void_cookie_t xcb_create_colormap (xcb_connection_t *c, /* Pointer to the xcb_connection_t structure */
2712 uint8_t alloc, /* Colormap entries to be allocated (AllocNone or AllocAll) */
2713 xcb_colormap_t mid, /* Id of the color map */
2714 xcb_window_t window, /* Window on whose screen the colormap will be created */
2715 xcb_visualid_t visual); /* Id of the visual supported by the screen */
2718 Here is an example of creation of a new color map:
2721 #include <X11/XCB/xcb.h>
2726 xcb_connection_t *c;
2727 xcb_screen_t *screen;
2731 /* Open the connection to the X server and get the first screen */
2732 c = xcb_connect (NULL, NULL);
2733 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2735 /* We create the window win here*/
2737 cmap = xcb_colormap_new (c);
2738 xcb_create_colormap (c, XCB_COLORMAP_ALLOC_NONE, cmap, win, screen->root_visual);
2744 Note that the window parameter is only used to allow the X
2745 server to create the color map for the given screen. We can
2746 then use this color map for any window drawn on the same screen.
2749 To free a color map, it suffices to use this function:
2752 xcb_void_cookie_t xcb_free_colormap (xcb_connection_t *c, /* The connection */
2753 xcb_colormap_t cmap); /* The color map */
2761 <li>XCreateColormap ()
2766 <li>xcb_colormap_new ()
2767 <li>xcb_create_colormap ()
2772 <li>XFreeColormap ()
2777 <li>xcb_free_colormap ()
2782 <li class="subtitle"><a name="alloccolor">Allocating and freeing a color entry</a>
2784 Once we got access to some color map, we can start allocating
2785 colors. The informations related to a color are stored in the
2786 following structure:
2790 uint8_t response_type;
2794 uint16_t red; /* The red component */
2795 uint16_t green; /* The green component */
2796 uint16_t blue; /* The blue component */
2798 uint32_t pixel; /* The entry in the color map, supplied by the X server */
2799 } xcb_alloc_color_reply_t;
2802 XCB supplies these two functions to fill it:
2805 xcb_alloc_color_cookie_t xcb_alloc_color (xcb_connection_t *c,
2806 xcb_colormap_t cmap,
2810 xcb_alloc_color_reply_t *xcb_alloc_color_reply (xcb_connection_t *c,
2811 xcb_alloc_color_cookie_t cookie,
2812 xcb_generic_error_t **e);
2815 The fuction <span class="code">xcb_alloc_color()</span> takes the
2816 3 RGB components as parameters (red, green and blue). Here is an
2817 example of using these functions:
2820 #include <malloc.h>
2822 #include <X11/XCB/xcb.h>
2827 xcb_connection_t *c;
2828 xcb_screen_t *screen;
2830 xcb_colormap_t cmap;
2831 xcb_alloc_color_reply_t *rep;
2833 /* Open the connection to the X server and get the first screen */
2834 c = xcb_connect (NULL, NULL);
2835 screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2837 /* We create the window win here*/
2839 cmap = xcb_colormap_new (c);
2840 xcb_create_colormap (c, XCB_COLORMAP_ALLOC_NONE, cmap, win, screen->root_visual);
2842 rep = xcb_alloc_color_reply (c, xcb_alloc_color (c, cmap, 65535, 0, 0), NULL);
2847 /* Do something with r->pixel or the components */
2855 As <span class="code">xcb_alloc_color_reply()</span> allocates
2856 memory, you have to free <span class="code">rep</span>.
2859 <b>TODO</b>: Talk about freeing colors.
2862 <li class="title"><a name="pixmaps">X Bitmaps and Pixmaps</a>
2864 One thing many so-called "Multi-Media" applications need to do,
2865 is display images. In the X world, this is done using bitmaps
2866 and pixmaps. We have already seen some usage of them when
2867 setting an icon for our application. Lets study them further,
2868 and see how to draw these images inside a window, along side the
2869 simple graphics and text we have seen so far.
2872 One thing to note before delving further, is that XCB (nor Xlib)
2873 supplies no means of manipulating popular image formats, such as
2874 gif, png, jpeg or tiff. It is up to the programmer (or to higher
2875 level graphics libraries) to translate these image formats into
2876 formats that the X server is familiar with (x bitmaps and x
2880 <li class="subtitle"><a name="pixmapswhat">What is a X Bitmap? An X Pixmap?</a>
2882 An X bitmap is a two-color image stored in a format specific
2883 to the X window system. When stored in a file, the bitmap data
2884 looks like a C source file. It contains variables defining the
2885 width and the height of the bitmap, an array containing the
2886 bit values of the bitmap (the size of the array is
2887 (width+7)/8*height and the bit and byte order are LSB), and
2888 an optional hot-spot location (that will
2889 be explained later, when discussing mouse cursors).
2892 An X pixmap is a format used to stored images in the memory of
2893 an X server. This format can store both black and white images
2894 (such as x bitmaps) as well as color images. It is the only
2895 image format supported by the X protocol, and any image to be
2896 drawn on screen, should be first translated into this format.
2899 In actuality, an X pixmap can be thought of as a window that
2900 does not appear on the screen. Many graphics operations that
2901 work on windows, will also work on pixmaps. Indeed, the type
2902 of X pixmap in XCB is an Id like a window:
2910 In order to make the difference between a window and a pixmap,
2911 XCB introduces a drawable type, which is a <b>union</b>
2915 xcb_window_t window;
2916 xcb_pixmap_t pixmap;
2920 in order to avoid confusion between a window and a pixmap. The
2921 operations that will work the same on a window or a pixmap
2922 will require a <span class="code">xcb_drawable_t</span>
2926 Remark: In Xlib, there is no specific difference between a
2927 <span class="code">Drawable</span>, a
2928 <span class="code">Pixmap</span> or a
2929 <span class="code">Window</span>: all are 32 bit long
2930 integer. XCB wraps all these different IDs in structures to
2931 provide some measure of type-safety.
2934 <li class="subtitle"><a name="pixmapscreate">Creating a pixmap</a>
2936 Sometimes we want to create an un-initialized pixmap, so we
2937 can later draw into it. This is useful for image drawing
2938 programs (creating a new empty canvas will cause the creation
2939 of a new pixmap on which the drawing can be stored). It is
2940 also useful when reading various image formats: we load the
2941 image data into memory, create a pixmap on the server, and
2942 then draw the decoded image data onto that pixmap.
2945 To create a new pixmap, we first ask the X server to give an
2946 Id to our pixmap, with this function:
2949 xcb_pixmap_t xcb_pixmap_new (xcb_connection_t *c);
2952 Then, XCB supplies the following function to create new pixmaps:
2955 xcb_void_cookie_t xcb_create_pixmap (xcb_connection_t *c, /* Pointer to the xcb_connection_t structure */
2956 uint8_t depth, /* Depth of the screen */
2957 xcb_pixmap_t pid, /* Id of the pixmap */
2958 xcb_drawable_t drawable,
2959 uint16_t width, /* Width of the window (in pixels) */
2960 uint16_t height); /* Height of the window (in pixels) */
2963 <b>TODO</b>: Explain the drawable parameter, and give an
2964 example (like <a href="xpoints.c">xpoints.c</a>)
2966 <li class="subtitle"><a name="pixmapsdraw"></a>Drawing a pixmap in a window
2968 Once we got a handle to a pixmap, we can draw it on some
2969 window, using the following function:
2972 xcb_void_cookie_t xcb_copy_area (xcb_connection_t *c, /* Pointer to the xcb_connection_t structure */
2973 xcb_drawable_t src_drawable, /* The Drawable we want to paste */
2974 xcb_drawable_t dst_drawable, /* The Drawable on which we copy the previous Drawable */
2975 xcb_gcontext_t gc, /* A Graphic Context */
2976 int16_t src_x, /* Top left x coordinate of the region we want to copy */
2977 int16_t src_y, /* Top left y coordinate of the region we want to copy */
2978 int16_t dst_x, /* Top left x coordinate of the region where we want to copy */
2979 int16_t dst_y, /* Top left y coordinate of the region where we want to copy */
2980 uint16_t width, /* Width of the region we want to copy */
2981 uint16_t height); /* Height of the region we want to copy */
2984 As you can see, we could copy the whole pixmap, as well as
2985 only a given rectangle of the pixmap. This is useful to
2986 optimize the drawing speed: we could copy only what we have
2987 modified in the pixmap.
2990 <b>One important note should be made</b>: it is possible to
2991 create pixmaps with different depths on the same screen. When
2992 we perform copy operations (a pixmap onto a window, etc), we
2993 should make sure that both source and target have the same
2994 depth. If they have a different depth, the operation would
2995 fail. The exception to this is if we copy a specific bit plane
2996 of the source pixmap using the
2997 <span class="code">xcb_copy_plane_t</span> function. In such an
2998 event, we can copy a specific plane to the target window (in
2999 actuality, setting a specific bit in the color of each pixel
3000 copied). This can be used to generate strange graphic effects
3001 in a window, but that is beyond the scope of this tutorial.
3003 <li class="subtitle"><a name="pixmapsfree"></a>Freeing a pixmap
3005 Finally, when we are done using a given pixmap, we should free
3006 it, in order to free resources of the X server. This is done
3007 using this function:
3010 xcb_void_cookie_t xcb_free_pixmap (xcb_connection_t *c, /* Pointer to the xcb_connection_t structure */
3011 xcb_pixmap_t pixmap); /* A given pixmap */
3014 Of course, after having freed it, we must not try accessing
3018 <b>TODO</b>: Give an example, or a link to xpoints.c
3021 <li class="title"><a name="mousecursor">Messing with the mouse cursor</a>
3023 It it possible to modify the shape of the mouse pointer (also
3024 called the X pointer) when in certain states, as we otfen see in
3025 programs. For example, a busy application would often display
3026 the sand clock over its main window, to give the user a visual
3027 hint that he should wait. Let's see how we can change the mouse
3028 cursor of our windows.
3031 <li class="subtitle"><a name="mousecursorcreate">Creating and destroying a mouse cursor</a>
3033 There are two methods for creating cursors. One of them is by
3034 using a set of predefined cursors, that are supplied by the X
3035 server, the other is by using a user-supplied bitmap.
3038 In the first method, we use a special font named "cursor", and
3039 the function <span class="code">xcb_create_glyph_cursor</span>:
3042 xcb_void_cookie_t xcb_create_glyph_cursor_checked (xcb_connection_t *c,
3044 xcb_font_t source_font, /* font for the source glyph */
3045 xcb_font_t mask_font, /* font for the mask glyph or XCB_NONE */
3046 uint16_t source_char, /* character glyph for the source */
3047 uint16_t mask_char, /* character glyph for the mask */
3048 uint16_t fore_red, /* red value for the foreground of the source */
3049 uint16_t fore_green, /* green value for the foreground of the source */
3050 uint16_t fore_blue, /* blue value for the foreground of the source */
3051 uint16_t back_red, /* red value for the background of the source */
3052 uint16_t back_green, /* green value for the background of the source */
3053 uint16_t back_blue) /* blue value for the background of the source */
3056 <b>TODO</b>: Describe <span class="code">source_char</span>
3057 and <span class="code">mask_char</span>, for example by giving
3058 an example on how to get the values. There is a list there:
3059 <a href="http://tronche.com/gui/x/xlib/appendix/b/">X Font Cursors</a>
3062 So we first open that font (see <a href="#loadfont">Loading a Font</a>)
3063 and create the new cursor. As for every X ressource, we have to
3064 ask for an X id with <span class="code">xcb_cursor_new</span>
3069 xcb_cursor_t cursor;
3071 /* The connection is set */
3073 font = xcb_font_new (conn);
3074 xcb_open_font (conn, font, strlen ("cursor"), "cursor");
3076 cursor = xcb_cursor_new (conn);
3077 xcb_create_glyph_cursor (conn, cursor, font, font,
3083 We have created the cursor "right hand" by specifying 58 to
3084 the <span class="code">source_fon</span>t argument and 58 + 1
3085 to the <span class="code">mask_font</span>.
3088 The cursor is destroyed by using the function
3091 xcb_void_cookie_t xcb_free_cursor (xcb_connection_t *c,
3092 xcb_cursor_t cursor);
3095 In the second method, we create a new cursor by using a pair
3096 of pixmaps, with depth of one (that is, two colors
3097 pixmaps). One pixmap defines the shape of the cursor, while
3098 the other works as a mask, specifying which pixels of the
3099 cursor will be actually drawn. The rest of the pixels will be
3103 <b>TODO</b>: give an example.
3105 <li class="subtitle"><a name="mousecursorset">Setting a window's mouse cursor</a>
3107 Once the cursor is created, we can modify the cursor of our
3108 window by using <span class="code">xcb_change_window_attributes</span>
3109 and using the <span class="code">XCB_CWCURSOR</span> attribute:
3113 uint32_t value_list;
3115 /* The connection and window are set */
3116 /* The cursor is already created */
3118 mask = XCB_CWCURSOR;
3119 value_list = cursor.xid;
3120 xcb_change_window_attributes (conn, window, mask, &value_list);
3123 Of course, the cursor and the font must be freed.
3125 <li class="subtitle"><a name="mousecursorexample">Complete example</a>
3127 <b>TODO</b>: to do...
3130 <li class="title"><a name="translation">Translation of basic Xlib functions and macros</a>
3132 The problem when you want to port an Xlib program to XCB is that
3133 you don't know if the Xlib function that you want to "translate"
3134 is a X Window one or an Xlib macro. In that section, we describe
3135 a way to translate the usual functions or macros that Xlib
3136 provides. It's usually just a member of a structure.
3139 <li class="subtitle"><a name="displaystructure">Members of the Display structure</a>
3141 In this section, we look at how to translate the macros that
3142 return some members of the <span class="code">Display</span>
3143 structure. They are obtained by using a function that requires a
3144 <span class="code">xcb_connection_t *</span> or a member of the
3145 <span class="code">xcb_setup_t</span> structure
3146 (via the function <span class="code">xcb_get_setup</span>), or
3147 a function that requires that structure.
3150 <li class="subtitle"><a name="ConnectionNumber">ConnectionNumber</a>
3152 This number is the file descriptor that connects the client
3153 to the server. You just have to use that function:
3156 int xcb_get_file_descriptor (xcb_connection_t *c);
3158 <li class="subtitle"><a name="DefaultScreen"></a>DefaultScreen
3160 That number is not stored by XCB. It is returned in the
3161 second parameter of the function <span class="code"><a href="#openconn">xcb_connect</a></span>.
3162 Hence, you have to store it yourself if you want to use
3163 it. Then, to get the <span class="code">xcb_screen_t</span>
3164 structure, you have to iterate on the screens.
3165 The equivalent function of the Xlib's
3166 <span class="code">ScreenOfDisplay</span> function can be
3167 found <a href="#ScreenOfDisplay">below</a>. This is also provided in the
3168 xcb_aux_t library as <span class="code">xcb_aux_get_screen()</span>. OK, here is the
3169 small piece of code to get that number:
3172 xcb_connection_t *c;
3173 int screen_default_nbr;
3175 /* you pass the name of the display you want to xcb_connect_t */
3177 c = xcb_connect (display_name, &screen_default_nbr);
3179 /* screen_default_nbr contains now the number of the default screen */
3181 <li class="subtitle"><a name="QLength"></a>QLength
3186 However, this points out a basic difference in philosophy between
3187 Xlib and XCB. Xlib has several functions for filtering and
3188 manipulating the incoming and outgoing X message queues. XCB
3189 wishes to hide this as much as possible from the user, which
3190 allows for more freedom in implementation strategies.
3192 <li class="subtitle"><a name="ScreenCount"></a>ScreenCount
3194 You get the count of screens with the functions
3195 <span class="code">xcb_get_setup</span>
3197 <span class="code">xcb_setup_roots_iterator</span>
3198 (if you need to iterate):
3201 xcb_connection_t *c;
3204 /* you init the connection */
3206 screen_count = xcb_setup_roots_iterator (xcb_get_setup (c)).rem;
3208 /* screen_count contains now the count of screens */
3211 If you don't want to iterate over the screens, a better way
3212 to get that number is to use
3213 <span class="code">xcb_setup_roots_length_t</span>:
3216 xcb_connection_t *c;
3219 /* you init the connection */
3221 screen_count = xcb_setup_roots_length (xcb_get_setup (c));
3223 /* screen_count contains now the count of screens */
3225 <li class="subtitle"><a name="ServerVendor"></a>ServerVendor
3227 You get the name of the vendor of the server hardware with
3228 the functions <span class="code">xcb_get_setup</span>
3231 class="code">xcb_setup_vendor</span>. Beware
3232 that, unlike Xlib, the string returned by XCB is not
3233 necessarily null-terminaled:
3236 xcb_connection_t *c;
3237 char *vendor = NULL;
3240 /* you init the connection */
3241 length = xcb_setup_vendor_length (xcb_get_setup (c));
3242 vendor = (char *)malloc (length + 1);
3244 memcpy (vendor, xcb_setup_vendor (xcb_get_setup (c)), length);
3245 vendor[length] = '\0';
3247 /* vendor contains now the name of the vendor. Must be freed when not used anymore */
3249 <li class="subtitle"><a name="ProtocolVersion"></a>ProtocolVersion
3251 You get the major version of the protocol in the
3252 <span class="code">xcb_setup_t</span>
3253 structure, with the function <span class="code">xcb_get_setup</span>:
3256 xcb_connection_t *c;
3257 uint16_t protocol_major_version;
3259 /* you init the connection */
3261 protocol_major_version = xcb_get_setup (c)->protocol_major_version;
3263 /* protocol_major_version contains now the major version of the protocol */
3265 <li class="subtitle"><a name="ProtocolRevision"></a>ProtocolRevision
3267 You get the minor version of the protocol in the
3268 <span class="code">xcb_setup_t</span>
3269 structure, with the function <span class="code">xcb_get_setup</span>:
3272 xcb_connection_t *c;
3273 uint16_t protocol_minor_version;
3275 /* you init the connection */
3277 protocol_minor_version = xcb_get_setup (c)->protocol_minor_version;
3279 /* protocol_minor_version contains now the minor version of the protocol */
3281 <li class="subtitle"><a name="VendorRelease"></a>VendorRelease
3283 You get the number of the release of the server hardware in the
3284 <span class="code">xcb_setup_t</span>
3285 structure, with the function <span class="code">xcb_get_setup</span>:
3288 xcb_connection_t *c;
3289 uint32_t release_number;
3291 /* you init the connection */
3293 release_number = xcb_get_setup (c)->release_number;
3295 /* release_number contains now the number of the release of the server hardware */
3297 <li class="subtitle"><a name="DisplayString"></a>DisplayString
3299 The name of the display is not stored in XCB. You have to
3300 store it by yourself.
3302 <li class="subtitle"><a name="BitmapUnit"></a>BitmapUnit
3304 You get the bitmap scanline unit in the
3305 <span class="code">xcb_setup_t</span>
3306 structure, with the function <span class="code">xcb_get_setup</span>:
3309 xcb_connection_t *c;
3310 uint8_t bitmap_format_scanline_unit;
3312 /* you init the connection */
3314 bitmap_format_scanline_unit = xcb_get_setup (c)->bitmap_format_scanline_unit;
3316 /* bitmap_format_scanline_unit contains now the bitmap scanline unit */
3318 <li class="subtitle"><a name="BitmapBitOrder"></a>BitmapBitOrder
3320 You get the bitmap bit order in the
3321 <span class="code">xcb_setup_t</span>
3322 structure, with the function <span class="code">xcb_get_setup</span>:
3325 xcb_connection_t *c;
3326 uint8_t bitmap_format_bit_order;
3328 /* you init the connection */
3330 bitmap_format_bit_order = xcb_get_setup (c)->bitmap_format_bit_order;
3332 /* bitmap_format_bit_order contains now the bitmap bit order */
3334 <li class="subtitle"><a name="BitmapPad"></a>BitmapPad
3336 You get the bitmap scanline pad in the
3337 <span class="code">xcb_setup_t</span>
3338 structure, with the function <span class="code">xcb_get_setup</span>:
3341 xcb_connection_t *c;
3342 uint8_t bitmap_format_scanline_pad;
3344 /* you init the connection */
3346 bitmap_format_scanline_pad = xcb_get_setup (c)->bitmap_format_scanline_pad;
3348 /* bitmap_format_scanline_pad contains now the bitmap scanline pad */
3350 <li class="subtitle"><a name="ImageByteOrder"></a>ImageByteOrder
3352 You get the image byte order in the
3353 <span class="code">xcb_setup_t</span>
3354 structure, with the function <span class="code">xcb_get_setup</span>:
3357 xcb_connection_t *c;
3358 uint8_t image_byte_order;
3360 /* you init the connection */
3362 image_byte_order = xcb_get_setup (c)->image_byte_order;
3364 /* image_byte_order contains now the image byte order */
3367 <li class="subtitle"><a name="screenofdisplay">ScreenOfDisplay related functions</a>
3369 in Xlib, <span class="code">ScreenOfDisplay</span> returns a
3370 <span class="code">Screen</span> structure that contains
3371 several characteristics of your screen. XCB has a similar
3372 structure (<span class="code">xcb_screen_t</span>),
3373 but the way to obtain it is a bit different. With
3374 Xlib, you just provide the number of the screen and you grab it
3375 from an array. With XCB, you iterate over all the screens to
3376 obtain the one you want. The complexity of this operation is
3377 O(n). So the best is to store this structure if you use
3378 it often. See <a href="#ScreenOfDisplay">screen_of_display</a> just below.
3381 Xlib provides generally two functions to obtain the characteristics
3382 related to the screen. One with the display and the number of
3383 the screen, which calls <span class="code">ScreenOfDisplay</span>,
3384 and the other that uses the <span class="code">Screen</span> structure.
3385 This might be a bit confusing. As mentioned above, with XCB, it
3386 is better to store the <span class="code">xcb_screen_t</span>
3387 structure. Then, you have to read the members of this
3388 structure. That's why the Xlib functions are put by pairs (or
3389 more) as, with XCB, you will use the same code.
3392 <li class="subtitle"><a name="ScreenOfDisplay">ScreenOfDisplay</a>
3394 This function returns the Xlib <span class="code">Screen</span>
3395 structure. With XCB, you iterate over all the screens and
3396 once you get the one you want, you return it:
3398 <pre class="code"><a name="ScreenOfDisplay"></a>
3399 xcb_screen_t *screen_of_display (xcb_connection_t *c,
3402 xcb_screen_iterator_t iter;
3404 iter = xcb_setup_roots_iterator (xcb_get_setup (c));
3405 for (; iter.rem; --screen, xcb_screen_next (&iter))
3413 As mentioned above, you might want to store the value
3414 returned by this function.
3417 All the functions below will use the result of that
3418 function, as they just grab a specific member of the
3419 <span class="code">xcb_screen_t</span> structure.
3421 <li class="subtitle"><a name="DefaultScreenOfDisplay"></a>DefaultScreenOfDisplay
3423 It is the default screen that you obtain when you connect to
3424 the X server. It suffices to call the <a href="#ScreenOfDisplay">screen_of_display</a>
3425 function above with the connection and the number of the
3429 xcb_connection_t *c;
3430 int screen_default_nbr;
3431 xcb_screen_t *default_screen; /* the returned default screen */
3433 /* you pass the name of the display you want to xcb_connect_t */
3435 c = xcb_connect (display_name, &screen_default_nbr);
3436 default_screen = screen_of_display (c, screen_default_nbr);
3438 /* default_screen contains now the default root window, or a NULL window if no screen is found */
3440 <li class="subtitle"><a name="RootWindow">RootWindow / RootWindowOfScreen</a>
3444 xcb_connection_t *c;
3445 xcb_screen_t *screen;
3447 xcb_window_t root_window = { 0 }; /* the returned window */
3449 /* you init the connection and screen_nbr */
3451 screen = screen_of_display (c, screen_nbr);
3453 root_window = screen->root;
3455 /* root_window contains now the root window, or a NULL window if no screen is found */
3457 <li class="subtitle"><a name="DefaultRootWindow">DefaultRootWindow</a>
3459 It is the root window of the default screen. So, you call
3460 <a name="ScreenOfDisplay">ScreenOfDisplay</a> with the
3461 default screen number and you get the
3462 <a href="#RootWindow">root window</a> as above:
3465 xcb_connection_t *c;
3466 xcb_screen_t *screen;
3467 int screen_default_nbr;
3468 xcb_window_t root_window = { 0 }; /* the returned root window */
3470 /* you pass the name of the display you want to xcb_connect_t */
3472 c = xcb_connect (display_name, &screen_default_nbr);
3473 screen = screen_of_display (c, screen_default_nbr);
3475 root_window = screen->root;
3477 /* root_window contains now the default root window, or a NULL window if no screen is found */
3479 <li class="subtitle"><a name="DefaultVisual">DefaultVisual / DefaultVisualOfScreen</a>
3481 While a Visual is, in Xlib, a structure, in XCB, there are
3482 two types: <span class="code">xcb_visualid_t</span>, which is
3483 the Id of the visual, and <span class="code">xcb_visualtype_t</span>,
3484 which corresponds to the Xlib Visual. To get the Id of the
3485 visual of a screen, just get the
3486 <span class="code">root_visual</span>
3487 member of a <span class="code">xcb_screen_t</span>:
3490 xcb_connection_t *c;
3491 xcb_screen_t *screen;
3493 xcb_visualid_t root_visual = { 0 }; /* the returned visual Id */
3495 /* you init the connection and screen_nbr */
3497 screen = screen_of_display (c, screen_nbr);
3499 root_visual = screen->root_visual;
3501 /* root_visual contains now the value of the Id of the visual, or a NULL visual if no screen is found */
3504 To get the <span class="code">xcb_visualtype_t</span>
3505 structure, it's a bit less easy. You have to get the
3506 <span class="code">xcb_screen_t</span> structure that you want,
3507 get its <span class="code">root_visual</span> member,
3508 then iterate over the <span class="code">xcb_depth_t</span>s
3509 and the <span class="code">xcb_visualtype_t</span>s, and compare
3510 the <span class="code">xcb_visualid_t</span> of these <span class="code">xcb_visualtype_t</span>s:
3511 with <span class="code">root_visual</span>:
3514 xcb_connection_t *c;
3515 xcb_screen_t *screen;
3517 xcb_visualid_t root_visual = { 0 };
3518 xcb_visualtype_t *visual_type = NULL; /* the returned visual type */
3520 /* you init the connection and screen_nbr */
3522 screen = screen_of_display (c, screen_nbr);
3524 xcb_depth_iterator_t depth_iter;
3526 depth_iter = xcb_screen_allowed_depths_iterator (screen);
3527 for (; depth_iter.rem; xcb_depth_next (&depth_iter)) {
3528 xcb_visualtype_iterator_t visual_iter;
3530 visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
3531 for (; visual_iter.rem; xcb_visualtype_next (&visual_iter)) {
3532 if (screen->root_visual.id == visual_iter.data->visual_id.id) {
3533 visual_type = visual_iter.data;
3540 /* visual_type contains now the visual structure, or a NULL visual structure if no screen is found */
3542 <li class="subtitle"><a name="DefaultGC">DefaultGC / DefaultGCOfScreen</a>
3544 This default Graphic Context is just a newly created Graphic
3545 Context, associated to the root window of a
3546 <span class="code">xcb_screen_t</span>,
3547 using the black white pixels of that screen:
3550 xcb_connection_t *c;
3551 xcb_screen_t *screen;
3553 xcb_gcontext_t gc = { 0 }; /* the returned default graphic context */
3555 /* you init the connection and screen_nbr */
3557 screen = screen_of_display (c, screen_nbr);
3559 xcb_drawable_t draw;
3563 gc = xcb_gcontext_new (c);
3564 draw.window = screen->root;
3565 mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
3566 values[0] = screen->black_pixel;
3567 values[1] = screen->white_pixel;
3568 xcb_create_gc (c, gc, draw, mask, values);
3571 /* gc contains now the default graphic context */
3573 <li class="subtitle"><a name="BlackPixel">BlackPixel / BlackPixelOfScreen</a>
3575 It is the Id of the black pixel, which is in the structure
3576 of an <span class="code">xcb_screen_t</span>.
3579 xcb_connection_t *c;
3580 xcb_screen_t *screen;
3582 uint32_t black_pixel = 0; /* the returned black pixel */
3584 /* you init the connection and screen_nbr */
3586 screen = screen_of_display (c, screen_nbr);
3588 black_pixel = screen->black_pixel;
3590 /* black_pixel contains now the value of the black pixel, or 0 if no screen is found */
3592 <li class="subtitle"><a name="WhitePixel">WhitePixel / WhitePixelOfScreen</a>
3594 It is the Id of the white pixel, which is in the structure
3595 of an <span class="code">xcb_screen_t</span>.
3598 xcb_connection_t *c;
3599 xcb_screen_t *screen;
3601 uint32_t white_pixel = 0; /* the returned white pixel */
3603 /* you init the connection and screen_nbr */
3605 screen = screen_of_display (c, screen_nbr);
3607 white_pixel = screen->white_pixel;
3609 /* white_pixel contains now the value of the white pixel, or 0 if no screen is found */
3611 <li class="subtitle"><a name="DisplayWidth">DisplayWidth / WidthOfScreen</a>
3613 It is the width in pixels of the screen that you want, and
3614 which is in the structure of the corresponding
3615 <span class="code">xcb_screen_t</span>.
3618 xcb_connection_t *c;
3619 xcb_screen_t *screen;
3621 uint32_t width_in_pixels = 0; /* the returned width in pixels */
3623 /* you init the connection and screen_nbr */
3625 screen = screen_of_display (c, screen_nbr);
3627 width_in_pixels = screen->width_in_pixels;
3629 /* width_in_pixels contains now the width in pixels, or 0 if no screen is found */
3631 <li class="subtitle"><a name="DisplayHeight">DisplayHeight / HeightOfScreen</a>
3633 It is the height in pixels of the screen that you want, and
3634 which is in the structure of the corresponding
3635 <span class="code">xcb_screen_t</span>.
3638 xcb_connection_t *c;
3639 xcb_screen_t *screen;
3641 uint32_t height_in_pixels = 0; /* the returned height in pixels */
3643 /* you init the connection and screen_nbr */
3645 screen = screen_of_display (c, screen_nbr);
3647 height_in_pixels = screen->height_in_pixels;
3649 /* height_in_pixels contains now the height in pixels, or 0 if no screen is found */
3651 <li class="subtitle"><a name="DisplayWidthMM">DisplayWidthMM / WidthMMOfScreen</a>
3653 It is the width in millimeters of the screen that you want, and
3654 which is in the structure of the corresponding
3655 <span class="code">xcb_screen_t</span>.
3658 xcb_connection_t *c;
3659 xcb_screen_t *screen;
3661 uint32_t width_in_millimeters = 0; /* the returned width in millimeters */
3663 /* you init the connection and screen_nbr */
3665 screen = screen_of_display (c, screen_nbr);
3667 width_in_millimeters = screen->width_in_millimeters;
3669 /* width_in_millimeters contains now the width in millimeters, or 0 if no screen is found */
3671 <li class="subtitle"><a name="DisplayHeightMM">DisplayHeightMM / HeightMMOfScreen</a>
3673 It is the height in millimeters of the screen that you want, and
3674 which is in the structure of the corresponding
3675 <span class="code">xcb_screen_t</span>.
3678 xcb_connection_t *c;
3679 xcb_screen_t *screen;
3681 uint32_t height_in_millimeters = 0; /* the returned height in millimeters */
3683 /* you init the connection and screen_nbr */
3685 screen = screen_of_display (c, screen_nbr);
3687 height_in_millimeters = screen->height_in_millimeters;
3689 /* height_in_millimeters contains now the height in millimeters, or 0 if no screen is found */
3691 <li class="subtitle"><a name="DisplayPlanes">DisplayPlanes / DefaultDepth / DefaultDepthOfScreen / PlanesOfScreen</a>
3693 It is the depth (in bits) of the root window of the
3694 screen. You get it from the <span class="code">xcb_screen_t</span> structure.
3697 xcb_connection_t *c;
3698 xcb_screen_t *screen;
3700 uint8_t root_depth = 0; /* the returned depth of the root window */
3702 /* you init the connection and screen_nbr */
3704 screen = screen_of_display (c, screen_nbr);
3706 root_depth = screen->root_depth;
3708 /* root_depth contains now the depth of the root window, or 0 if no screen is found */
3710 <li class="subtitle"><a name="DefaultColormap">DefaultColormap / DefaultColormapOfScreen</a>
3712 This is the default colormap of the screen (and not the
3713 (default) colormap of the default screen !). As usual, you
3714 get it from the <span class="code">xcb_screen_t</span> structure:
3717 xcb_connection_t *c;
3718 xcb_screen_t *screen;
3720 xcb_colormap_t default_colormap = { 0 }; /* the returned default colormap */
3722 /* you init the connection and screen_nbr */
3724 screen = screen_of_display (c, screen_nbr);
3726 default_colormap = screen->default_colormap;
3728 /* default_colormap contains now the default colormap, or a NULL colormap if no screen is found */
3730 <li class="subtitle"><a name="MinCmapsOfScreen"></a>MinCmapsOfScreen
3732 You get the minimum installed colormaps in the <span class="code">xcb_screen_t</span> structure:
3735 xcb_connection_t *c;
3736 xcb_screen_t *screen;
3738 uint16_t min_installed_maps = 0; /* the returned minimum installed colormaps */
3740 /* you init the connection and screen_nbr */
3742 screen = screen_of_display (c, screen_nbr);
3744 min_installed_maps = screen->min_installed_maps;
3746 /* min_installed_maps contains now the minimum installed colormaps, or 0 if no screen is found */
3748 <li class="subtitle"><a name="MaxCmapsOfScreen"></a>MaxCmapsOfScreen
3750 You get the maximum installed colormaps in the <span class="code">xcb_screen_t</span> structure:
3753 xcb_connection_t *c;
3754 xcb_screen_t *screen;
3756 uint16_t max_installed_maps = 0; /* the returned maximum installed colormaps */
3758 /* you init the connection and screen_nbr */
3760 screen = screen_of_display (c, screen_nbr);
3762 max_installed_maps = screen->max_installed_maps;
3764 /* max_installed_maps contains now the maximum installed colormaps, or 0 if no screen is found */
3766 <li class="subtitle"><a name="DoesSaveUnders"></a>DoesSaveUnders
3768 You know if <span class="code">save_unders</span> is set,
3769 by looking in the <span class="code">xcb_screen_t</span> structure:
3772 xcb_connection_t *c;
3773 xcb_screen_t *screen;
3775 uint8_t save_unders = 0; /* the returned value of save_unders */
3777 /* you init the connection and screen_nbr */
3779 screen = screen_of_display (c, screen_nbr);
3781 save_unders = screen->save_unders;
3783 /* save_unders contains now the value of save_unders, or FALSE if no screen is found */
3785 <li class="subtitle"><a name="DoesBackingStore"></a>DoesBackingStore
3787 You know the value of <span class="code">backing_stores</span>,
3788 by looking in the <span class="code">xcb_screen_t</span> structure:
3791 xcb_connection_t *c;
3792 xcb_screen_t *screen;
3794 uint8_t backing_stores = 0; /* the returned value of backing_stores */
3796 /* you init the connection and screen_nbr */
3798 screen = screen_of_display (c, screen_nbr);
3800 backing_stores = screen->backing_stores;
3802 /* backing_stores contains now the value of backing_stores, or FALSE if no screen is found */
3804 <li class="subtitle"><a name="EventMaskOfScreen"></a>EventMaskOfScreen
3806 To get the current input masks,
3807 you look in the <span class="code">xcb_screen_t</span> structure:
3810 xcb_connection_t *c;
3811 xcb_screen_t *screen;
3813 uint32_t current_input_masks = 0; /* the returned value of current input masks */
3815 /* you init the connection and screen_nbr */
3817 screen = screen_of_display (c, screen_nbr);
3819 current_input_masks = screen->current_input_masks;
3821 /* current_input_masks contains now the value of the current input masks, or FALSE if no screen is found */
3824 <li class="subtitle"><a name="misc">Miscellaneous macros</a>
3826 <li class="subtitle"><a name="DisplayOfScreen"></a>DisplayOfScreen
3828 in Xlib, the <span class="code">Screen</span> structure
3829 stores its associated <span class="code">Display</span>
3830 structure. This is not the case in the X Window protocol,
3831 hence, it's also not the case in XCB. So you have to store
3834 <li class="subtitle"><a name="DisplayCells"></a>DisplayCells / CellsOfScreen
3836 To get the colormap entries,
3837 you look in the <span class="code">xcb_visualtype_t</span>
3838 structure, that you grab like <a class="subsection" href="#DefaultVisual">here</a>:
3841 xcb_connection_t *c;
3842 xcb_visualtype_t *visual_type;
3843 uint16_t colormap_entries = 0; /* the returned value of the colormap entries */
3845 /* you init the connection and visual_type */
3848 colormap_entries = visual_type->colormap_entries;
3850 /* colormap_entries contains now the value of the colormap entries, or FALSE if no screen is found */