<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>X11 on dev@iostream.pl</title><link>https://dev.iostream.pl/categories/x11/</link><description>Recent content in X11 on dev@iostream.pl</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 04 Dec 2026 23:00:00 +0100</lastBuildDate><atom:link href="https://dev.iostream.pl/categories/x11/index.xml" rel="self" type="application/rss+xml"/><item><title>Open a Window, It's Stuffy in Here</title><link>https://dev.iostream.pl/posts/2026-12-04/</link><pubDate>Fri, 04 Dec 2026 23:00:00 +0100</pubDate><guid>https://dev.iostream.pl/posts/2026-12-04/</guid><description>&lt;h1 id="by-way-of-introduction"&gt;By way of introduction
&lt;/h1&gt;&lt;p&gt;For this episode, you will need a compiler (&lt;code&gt;gcc&lt;/code&gt; will do nicely), an X server running on either a local or a remote machine (with permission to connect to it), and the &lt;code&gt;Xlib&lt;/code&gt; library along with its header files. And, of course, your favorite text editor (Emacs, right?), a terminal emulator, and a willingness to experiment.&lt;/p&gt;
&lt;p&gt;Ladies and gentlemen, today we are going to write, build, and run our first graphical application, which will:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;open a window,&lt;/li&gt;
&lt;li&gt;display some text using a specified foreground and background color,&lt;/li&gt;
&lt;li&gt;respond favorably to a request to close the window and clean up after itself.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;rsquo;s get to work!&lt;/p&gt;
&lt;h1 id="source-code-and-compilation"&gt;Source code and compilation
&lt;/h1&gt;&lt;p&gt;The source code can be found right over here: &lt;a class="link" href="hello-x11.c" &gt;hello-x11.c&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Compilation is remarkably simple:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gcc hello-x11.c -o x11-demo &lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;pkg-config --cflags --libs x11&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without &lt;code&gt;pkg-config&lt;/code&gt;, assuming the headers and library are in the compiler&amp;rsquo;s standard search paths:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gcc hello-x11.c -o x11-demo -lX11
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The program should compile in the blink of an eye, without so much as a single warning.&lt;/p&gt;
&lt;p&gt;After running it (via &lt;code&gt;./x11-demo&lt;/code&gt;), we should be greeted by the following window:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Hello, X11!" class="gallery-image" data-flex-basis="315px" data-flex-grow="131" height="240" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://dev.iostream.pl/posts/2026-12-04/hello-x11.png" width="315"&gt;&lt;/p&gt;
&lt;h1 id="where-is-the-server"&gt;Where is the server?
&lt;/h1&gt;&lt;p&gt;The X Window System is built around a client-server architecture. Communication often takes place through a local Unix domain socket, but it can also take place over a TCP/IP network.&lt;/p&gt;
&lt;p&gt;From a programmer&amp;rsquo;s perspective, a display is therefore not a physical monitor. A pointer to a &lt;code&gt;Display&lt;/code&gt; structure represents a connection to a particular instance of an X server. An application can even maintain several such connections if necessary.&lt;/p&gt;
&lt;p&gt;To make matters slightly more confusing, our particular server instance also provides access to input devices, such as keyboards and mice. For more information about the history, principles, and capabilities of the &lt;strong&gt;X Window System&lt;/strong&gt;, &lt;a class="link" href="https://en.wikipedia.org/wiki/X_Window_System" target="_blank" rel="noopener"
 &gt;this article&lt;/a&gt; is a good starting point.&lt;/p&gt;
&lt;p&gt;For graphical applications to know which X server they should connect to, there is an environment variable called &lt;code&gt;DISPLAY&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In line 22:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;display &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;XOpenDisplay&lt;/span&gt;(NULL);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;we attempt to establish a connection to the server specified by that variable.&lt;/p&gt;
&lt;p&gt;The function declaration looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Display &lt;span style="color:#f92672"&gt;*&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;XOpenDisplay&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;char&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;display_name);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can explicitly specify a display name if necessary. Passing &lt;code&gt;NULL&lt;/code&gt; means that the function should obtain the name from the &lt;code&gt;DISPLAY&lt;/code&gt; environment variable.&lt;/p&gt;
&lt;p&gt;Chances are that you have a computer running Linux or one of the BSD systems. Once an X session has been started, either through a display manager or by invoking &lt;code&gt;startx&lt;/code&gt;, the value of &lt;code&gt;DISPLAY&lt;/code&gt; will quite often be &lt;code&gt;:0&lt;/code&gt;, although other values are perfectly possible.&lt;/p&gt;
&lt;p&gt;Applications running on your computer may connect to the server through a Unix domain socket located in &lt;code&gt;/tmp/.X11-unix/&lt;/code&gt;. For display &lt;code&gt;:0&lt;/code&gt;, the corresponding socket is normally named &lt;code&gt;X0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You might want to try a little experiment: open a new terminal window, remove the &lt;code&gt;DISPLAY&lt;/code&gt; environment variable by typing:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;unset DISPLAY
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and try to launch a traditional X11 application.&lt;/p&gt;
&lt;p&gt;Unless the application has some alternative way of determining the display name, it will fail to connect. It simply has no idea which server to talk to.&lt;/p&gt;
&lt;p&gt;The usual syntax of the &lt;code&gt;DISPLAY&lt;/code&gt; variable is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[host]:display[.screen]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Apart from specifying a particular X server, we can also select an X screen. If the screen number is omitted, it defaults to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The server itself may be running on a completely different machine, hence the optional hostname.&lt;/p&gt;
&lt;p&gt;Traditionally, a remote display numbered &lt;code&gt;:0&lt;/code&gt; listens on TCP port &lt;em&gt;6000&lt;/em&gt;, display &lt;code&gt;:1&lt;/code&gt; on port &lt;em&gt;6001&lt;/em&gt;, and so on. However, modern X servers frequently disable TCP connections by default. SSH with X11 forwarding is generally a more convenient and secure way of running remote graphical applications.&lt;/p&gt;
&lt;p&gt;When writing our own programs, it is usually better to rely on the &lt;code&gt;DISPLAY&lt;/code&gt; environment variable and pass &lt;code&gt;NULL&lt;/code&gt; to &lt;strong&gt;XOpenDisplay&lt;/strong&gt; rather than hardcoding a particular value.&lt;/p&gt;
&lt;p&gt;Of course, one can imagine a scenario in which a specific display name is stored in a configuration file, or an application needs to communicate with several X servers.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;XOpenDisplay&lt;/strong&gt; function returns a pointer to a &lt;code&gt;Display&lt;/code&gt; structure, representing the connection and providing access to information about the server, its screens, supported capabilities, and various other details.&lt;/p&gt;
&lt;p&gt;Applications should treat the internal contents of this structure as private and use the appropriate Xlib functions and macros to access them.&lt;/p&gt;
&lt;p&gt;A valid &lt;code&gt;Display&lt;/code&gt; pointer is required by many Xlib functions. After all, most operations involving server-side resources need to know which server they are dealing with.&lt;/p&gt;
&lt;p&gt;If the function returns &lt;code&gt;NULL&lt;/code&gt;, the connection could not be established. There is little point in continuing at this stage, assuming that graphical operations are what we came here for.&lt;/p&gt;
&lt;p&gt;We can simply print an appropriate message to the standard error stream and terminate the program with a non-zero exit status.&lt;/p&gt;
&lt;h1 id="a-screen-is-not-a-monitor"&gt;A screen is not a monitor?
&lt;/h1&gt;&lt;p&gt;Every X screen has its own &lt;strong&gt;root window&lt;/strong&gt;. This window sits underneath the entire window hierarchy and is the ultimate ancestor of all other windows on that screen.&lt;/p&gt;
&lt;p&gt;It is a perfectly usable window, something that desktop wallpaper utilities and screenshot applications are more than happy to take advantage of.&lt;/p&gt;
&lt;p&gt;In line 27:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;screen &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;DefaultScreen&lt;/span&gt;(display);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;we obtain the default X screen number for our connection.&lt;/p&gt;
&lt;p&gt;Now, this is where things get interesting.&lt;/p&gt;
&lt;p&gt;Historically, multi-monitor workstations were often configured with separate X screens, sometimes with a separate graphics adapter for each monitor. Each screen had its own root window, coordinate system, and collection of windows.&lt;/p&gt;
&lt;p&gt;Although the mouse pointer could travel from one screen to another, moving an existing window between those screens was not normally possible.&lt;/p&gt;
&lt;p&gt;A window created on screen &lt;code&gt;:0.0&lt;/code&gt; belonged to that screen. Screen &lt;code&gt;:0.1&lt;/code&gt; was a different world altogether.&lt;/p&gt;
&lt;p&gt;Technologies such as &lt;strong&gt;Xinerama&lt;/strong&gt; and, later, the &lt;strong&gt;RandR&lt;/strong&gt; (Resize and Rotate) extension made it possible to present multiple physical monitors as parts of a larger, unified desktop.&lt;/p&gt;
&lt;p&gt;A typical modern X11 desktop therefore consists of a single X screen and a single root window spanning several physical monitors.&lt;/p&gt;
&lt;p&gt;For example, two monitors with a resolution of 1920 × 1080 pixels each, arranged side by side, may form an X screen measuring 3840 × 1080 pixels.&lt;/p&gt;
&lt;p&gt;As far as our application is concerned, there is one large coordinate space. The actual arrangement of the physical monitors can be obtained separately through the appropriate X11 extensions.&lt;/p&gt;
&lt;p&gt;Separate X screens are still possible, however, so we should not assume that there will always be exactly one.&lt;/p&gt;
&lt;p&gt;In short: a display, a screen, and a monitor are three different things.&lt;/p&gt;
&lt;p&gt;Nobody promised it would be easy.&lt;/p&gt;
&lt;h1 id="life-takes-on-some-color"&gt;Life takes on some color
&lt;/h1&gt;&lt;p&gt;Our window will need some colors obtained from the server.&lt;/p&gt;
&lt;p&gt;Yes, &lt;em&gt;obtained&lt;/em&gt;, because a typical conversation goes something like this:&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;— I&amp;rsquo;d like a color with these RGB components, please.&lt;/p&gt;
&lt;p&gt;— Certainly. Here you go. Happy painting.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;Or:&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;— I&amp;rsquo;d like a color with these RGB components, please.&lt;/p&gt;
&lt;p&gt;— I haven&amp;rsquo;t got that exact color, but I&amp;rsquo;ve got something close. Off you go.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;To use a particular color, whether specified by its RGB components or by a name, we need a pixel value appropriate for the visual and colormap associated with our window.&lt;/p&gt;
&lt;p&gt;One way of obtaining it is to ask the X server through the &lt;code&gt;XAllocNamedColor&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;This function accepts a textual color specification, such as &lt;code&gt;&amp;quot;red&amp;quot;&lt;/code&gt;, &lt;code&gt;&amp;quot;navy&amp;quot;&lt;/code&gt;, or &lt;code&gt;&amp;quot;#eaeeaf&amp;quot;&lt;/code&gt;, and attempts to allocate a corresponding read-only color entry in the specified colormap.&lt;/p&gt;
&lt;p&gt;On a modern TrueColor display, this does not necessarily mean reserving a scarce physical palette entry, as it might have done on older hardware.&lt;/p&gt;
&lt;p&gt;What matters to us is that we receive a usable pixel value which can then be passed to drawing functions.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;XAllocNamedColor&lt;/code&gt; function requires:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a pointer to a &lt;code&gt;Display&lt;/code&gt; structure,&lt;/li&gt;
&lt;li&gt;a colormap,&lt;/li&gt;
&lt;li&gt;a textual color specification,&lt;/li&gt;
&lt;li&gt;a pointer to an &lt;code&gt;XColor&lt;/code&gt; structure that will receive the color actually allocated,&lt;/li&gt;
&lt;li&gt;a pointer to another &lt;code&gt;XColor&lt;/code&gt; structure that will receive the exact color definition.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Since we also need to check whether the operation was successful, it is convenient to define a little helper function called &lt;code&gt;color&lt;/code&gt;, as shown in lines 6–12 and used in lines 29–31.&lt;/p&gt;
&lt;p&gt;Our function accepts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a pointer to a &lt;code&gt;Display&lt;/code&gt; structure,&lt;/li&gt;
&lt;li&gt;the screen number,&lt;/li&gt;
&lt;li&gt;the color specification,&lt;/li&gt;
&lt;li&gt;an &lt;strong&gt;existing fallback pixel value&lt;/strong&gt;, to be used if the requested color cannot be obtained.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;color&lt;/span&gt;(Display &lt;span style="color:#f92672"&gt;*&lt;/span&gt;display, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; screen, &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;char&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;name,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; fallback)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; XColor found, exact;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;XAllocNamedColor&lt;/span&gt;(display, &lt;span style="color:#a6e22e"&gt;DefaultColormap&lt;/span&gt;(display, screen),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; name, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;found, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;exact) &lt;span style="color:#f92672"&gt;?&lt;/span&gt; found.pixel : fallback;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;DefaultColormap&lt;/code&gt; macro gives us the default colormap for the specified screen.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;WhitePixel&lt;/code&gt; and &lt;code&gt;BlackPixel&lt;/code&gt; are two handy macros that return readily available white and black pixel values for that screen.&lt;/p&gt;
&lt;p&gt;We do not have to allocate them ourselves, which makes them particularly useful as fallback values.&lt;/p&gt;
&lt;h1 id="opening-a-window"&gt;Opening a window
&lt;/h1&gt;&lt;p&gt;Our window is finally created in lines 33–35.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;window &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;XCreateSimpleWindow&lt;/span&gt;(display, &lt;span style="color:#a6e22e"&gt;RootWindow&lt;/span&gt;(display, screen),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ae81ff"&gt;100&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;100&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;250&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;150&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;, border_color, bg);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;XCreateSimpleWindow&lt;/code&gt; function requires the following arguments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a pointer to a &lt;code&gt;Display&lt;/code&gt; structure,&lt;/li&gt;
&lt;li&gt;the parent window — in our case, the &lt;strong&gt;root window&lt;/strong&gt;,&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; coordinates relative to the upper-left corner of the parent window,&lt;/li&gt;
&lt;li&gt;the width and height,&lt;/li&gt;
&lt;li&gt;the border width,&lt;/li&gt;
&lt;li&gt;the border pixel value,&lt;/li&gt;
&lt;li&gt;the background pixel value.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All dimensions and coordinates are expressed in pixels.&lt;/p&gt;
&lt;p&gt;Now, what is the deal with that border?&lt;/p&gt;
&lt;p&gt;In this particular case, the window manager will most likely draw its own decorations around our window (or none at all). It may also decide to position the window somewhere other than the coordinates we requested.&lt;/p&gt;
&lt;p&gt;However, if we were creating another window to represent, say, a button inside an existing window, the border width and color could be quite useful.&lt;/p&gt;
&lt;p&gt;Not every window in X11 has to be a top-level application window.&lt;/p&gt;
&lt;p&gt;Also, although our window has now been created, it is not yet visible. We will deal with that minor inconvenience shortly.&lt;/p&gt;
&lt;h1 id="giving-it-an-identity"&gt;Giving it an identity
&lt;/h1&gt;&lt;p&gt;The following function:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XStoreName&lt;/span&gt;(display, window, &lt;span style="color:#e6db74"&gt;&amp;#34;Is it on?&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;sets the window name, commonly used as its title.&lt;/p&gt;
&lt;p&gt;It does not actually draw any text. Instead, it sets a window property called &lt;code&gt;WM_NAME&lt;/code&gt;, which the window manager can read and make use of.&lt;/p&gt;
&lt;p&gt;Most window managers do precisely that, displaying the name in the window&amp;rsquo;s title bar.&lt;/p&gt;
&lt;p&gt;Properties are an important part of X11, but we will leave a more detailed discussion of them for another episode.&lt;/p&gt;
&lt;h1 id="keep-us-informed"&gt;Keep us informed
&lt;/h1&gt;&lt;p&gt;In line 37, we tell the server what we would like the server to tell us about.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XSelectInput&lt;/span&gt;(display, window, ExposureMask &lt;span style="color:#f92672"&gt;|&lt;/span&gt; KeyPressMask);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We specify the types of events we are interested in by combining the appropriate event masks using the bitwise OR operator.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ExposureMask&lt;/code&gt; and &lt;code&gt;KeyPressMask&lt;/code&gt; allow us to receive &lt;code&gt;Expose&lt;/code&gt; and &lt;code&gt;KeyPress&lt;/code&gt; events, respectively.&lt;/p&gt;
&lt;p&gt;The former tells us that some part of our window needs to be redrawn. The latter informs us that a key has been pressed.&lt;/p&gt;
&lt;p&gt;For now, we are not interested in mouse movements, mouse clicks, or any of the many other events that X11 can deliver.&lt;/p&gt;
&lt;p&gt;Some events, such as &lt;code&gt;ClientMessage&lt;/code&gt;, follow different delivery rules and do not require a corresponding event mask.&lt;/p&gt;
&lt;p&gt;We are about to encounter one of them.&lt;/p&gt;
&lt;h1 id="negotiating-the-terms-of-divorce"&gt;Negotiating the terms of divorce
&lt;/h1&gt;&lt;p&gt;In lines 39–41, we encounter &lt;strong&gt;atoms&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Atom wm_delete &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;XInternAtom&lt;/span&gt;(display, &lt;span style="color:#e6db74"&gt;&amp;#34;WM_DELETE_WINDOW&amp;#34;&lt;/span&gt;, False);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Atom wm_protocols &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;XInternAtom&lt;/span&gt;(display, &lt;span style="color:#e6db74"&gt;&amp;#34;WM_PROTOCOLS&amp;#34;&lt;/span&gt;, False);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XSetWMProtocols&lt;/span&gt;(display, window, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;wm_delete, &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Atoms are numeric identifiers associated with strings. They are maintained by the X server and used in various places throughout the protocol, including window properties, selections, and client messages.&lt;/p&gt;
&lt;p&gt;Instead of repeatedly exchanging potentially lengthy strings with the server, we can use their compact numeric equivalents.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;XInternAtom&lt;/code&gt; obtains the identifier associated with a given name. Passing &lt;code&gt;False&lt;/code&gt; as the final argument means that the atom should be created if it does not already exist.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;WM_PROTOCOLS&lt;/code&gt; and &lt;code&gt;WM_DELETE_WINDOW&lt;/code&gt; are part of the standard conventions followed by X11 window managers and applications.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;WM_PROTOCOLS&lt;/code&gt; is a sort of noticeboard attached to our window. It contains a list of protocols that our application declares itself willing to support.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;WM_DELETE_WINDOW&lt;/code&gt; identifies the protocol used to request that an application close one of its windows.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By calling &lt;code&gt;XSetWMProtocols&lt;/code&gt;, we announce that our application understands the latter.&lt;/p&gt;
&lt;p&gt;An example of handling such a request can be found in lines 63–68:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;case&lt;/span&gt; ClientMessage:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (e.xclient.message_type &lt;span style="color:#f92672"&gt;==&lt;/span&gt; wm_protocols &lt;span style="color:#f92672"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; e.xclient.format &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;32&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; (Atom)e.xclient.data.l[&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;] &lt;span style="color:#f92672"&gt;==&lt;/span&gt; wm_delete)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; running &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;break&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;ClientMessage&lt;/code&gt; event can carry 20 bytes of application-defined data. Its &lt;code&gt;format&lt;/code&gt; field specifies whether those data are organized into elements of 8, 16, or 32 bits.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;WM_PROTOCOLS&lt;/code&gt; convention uses the 32-bit format, with the first element identifying the protocol in question.&lt;/p&gt;
&lt;p&gt;And why do we need all this?&lt;/p&gt;
&lt;p&gt;When the user attempts to close our window using a button in its decorations or a keyboard shortcut defined by the window manager, we want our application to receive a polite request to close.&lt;/p&gt;
&lt;p&gt;It can then ask the user whether unsaved work should be discarded, save its current state, or simply clean up after itself and terminate.&lt;/p&gt;
&lt;p&gt;These conventions are defined in the &lt;strong&gt;ICCCM&lt;/strong&gt;, or &lt;a class="link" href="https://xorg.freedesktop.org/releases/current/doc/xorg-docs/icccm/icccm.html" target="_blank" rel="noopener"
 &gt;Inter-Client Communication Conventions Manual&lt;/a&gt;, which describes how X clients, including window managers, should cooperate with one another.&lt;/p&gt;
&lt;p&gt;If we do not advertise support for &lt;code&gt;WM_DELETE_WINDOW&lt;/code&gt;, the window manager may resort to a rather less diplomatic solution: forcibly disconnecting our application from the X server.&lt;/p&gt;
&lt;p&gt;In the case of a simple Xlib application, that will normally result in the termination of the entire process.&lt;/p&gt;
&lt;p&gt;Better negotiate the terms beforehand.&lt;/p&gt;
&lt;h1 id="tools-of-the-trade"&gt;Tools of the trade
&lt;/h1&gt;&lt;p&gt;To draw or write on the surface of a window, we need a &lt;strong&gt;graphics context&lt;/strong&gt;, usually abbreviated to GC.&lt;/p&gt;
&lt;p&gt;We create one in line 43:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gc &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;XCreateGC&lt;/span&gt;(display, window, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;, NULL);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A graphics context holds various drawing parameters, such as the foreground and background pixel values, line width, drawing style, font, and clipping settings.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;XCreateGC&lt;/code&gt; function takes four arguments, but for now the most important ones are the pointer to our &lt;code&gt;Display&lt;/code&gt; structure (by now, hardly a surprise) and the identifier of our window.&lt;/p&gt;
&lt;p&gt;The window tells Xlib which screen and drawable depth the graphics context needs to be compatible with.&lt;/p&gt;
&lt;p&gt;The remaining two arguments allow us to specify which attributes should be initialized and what their values should be.&lt;/p&gt;
&lt;p&gt;Passing &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;NULL&lt;/code&gt; means that we are happy to start with the default settings.&lt;/p&gt;
&lt;p&gt;In the very next line:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XSetForeground&lt;/span&gt;(display, gc, text_color);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;we set the foreground pixel value to the color we intend to use for our text.&lt;/p&gt;
&lt;p&gt;There is much more to graphics contexts, but this will suffice for our first window.&lt;/p&gt;
&lt;h1 id="let-there-be-light"&gt;Let there be light
&lt;/h1&gt;&lt;p&gt;The request to make our window visible is issued in line 46:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XMapWindow&lt;/span&gt;(display, window);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In X11 terminology, we are &lt;em&gt;mapping&lt;/em&gt; the window.&lt;/p&gt;
&lt;p&gt;Creating a window and mapping it are two separate operations. This allows us to configure our window, set its properties, and prepare the necessary resources before it makes its public appearance.&lt;/p&gt;
&lt;p&gt;On a desktop managed by a window manager, the mapping request will normally be handled in cooperation with that manager.&lt;/p&gt;
&lt;p&gt;Our window is now ready to face the world.&lt;/p&gt;
&lt;h1 id="waiting-for-godot"&gt;Waiting for Godot
&lt;/h1&gt;&lt;p&gt;The window has come to life, and from now on we need to receive and handle events coming from the server.&lt;/p&gt;
&lt;p&gt;This is the purpose of our main event loop:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;while&lt;/span&gt; (running) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; XEvent e;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;XNextEvent&lt;/span&gt;(display, &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;e);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;switch&lt;/span&gt; (e.type) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;/* Event handlers go here. */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Events are retrieved by the &lt;code&gt;XNextEvent&lt;/code&gt; function in line 53 and handled by the &lt;code&gt;switch&lt;/code&gt; statement in lines 55–69.&lt;/p&gt;
&lt;p&gt;If the event queue is empty, &lt;code&gt;XNextEvent&lt;/code&gt; flushes any buffered outgoing requests and waits until an event becomes available.&lt;/p&gt;
&lt;p&gt;Consequently, there is no need to keep checking whether something has happened. Our application can spend most of its time doing absolutely nothing without consuming an entire CPU core in the process.&lt;/p&gt;
&lt;p&gt;A surprisingly useful skill.&lt;/p&gt;
&lt;h1 id="hello-cruel-world"&gt;Hello, cruel world!
&lt;/h1&gt;&lt;p&gt;The actual drawing takes place in response to an &lt;code&gt;Expose&lt;/code&gt; event, in lines 56–58.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;case&lt;/span&gt; Expose:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;XDrawString&lt;/span&gt;(display, window, gc, &lt;span style="color:#ae81ff"&gt;20&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;50&lt;/span&gt;, text, (&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;)&lt;span style="color:#a6e22e"&gt;strlen&lt;/span&gt;(text));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;break&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Unlike many modern graphical toolkits, Xlib does not maintain a collection of graphical objects that it can automatically redraw whenever necessary.&lt;/p&gt;
&lt;p&gt;It is up to our application to restore the contents of its window when the server indicates that some part of it needs repainting.&lt;/p&gt;
&lt;p&gt;The text itself is drawn using &lt;code&gt;XDrawString&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In addition to a pointer to the &lt;code&gt;Display&lt;/code&gt; structure, the window identifier, and the graphics context, we specify:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; coordinates,&lt;/li&gt;
&lt;li&gt;the text to be drawn,&lt;/li&gt;
&lt;li&gt;the length of the text in bytes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The coordinates are relative to the upper-left corner of the window&amp;rsquo;s drawing area. There is one important detail, however: the &lt;code&gt;y&lt;/code&gt; coordinate specifies the &lt;strong&gt;baseline of the text&lt;/strong&gt;, rather than its upper edge.&lt;/p&gt;
&lt;p&gt;The color of the text was previously established in line 44 using &lt;code&gt;XSetForeground&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For our simple ASCII string, the traditional &lt;code&gt;XDrawString&lt;/code&gt; function is perfectly adequate. More advanced text rendering, including Unicode and modern fonts, deserves an episode of its own.&lt;/p&gt;
&lt;p&gt;Perhaps even two.&lt;/p&gt;
&lt;h1 id="please-press-q"&gt;Please press Q
&lt;/h1&gt;&lt;p&gt;Our window receives keyboard events, so we can wait for the user to press the Q key and terminate the program when that happens.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;KeyPress&lt;/code&gt; event is handled in lines 59–62.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;case&lt;/span&gt; KeyPress:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;XLookupKeysym&lt;/span&gt;(&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;e.xkey, &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;) &lt;span style="color:#f92672"&gt;==&lt;/span&gt; XK_q)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; running &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;break&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;XLookupKeysym&lt;/code&gt; function translates an X11 keycode into its corresponding symbolic value, known as a &lt;code&gt;KeySym&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The second argument selects an entry in the key&amp;rsquo;s symbol mapping. We use index &lt;code&gt;0&lt;/code&gt;, which normally corresponds to the base, unmodified symbol.&lt;/p&gt;
&lt;p&gt;All that remains is to check whether the returned symbol is &lt;code&gt;XK_q&lt;/code&gt; and, if so, terminate the event loop.&lt;/p&gt;
&lt;p&gt;This is sufficient for our little application, although proper handling of keyboard layouts, modifiers, shortcuts, and text input is considerably more involved.&lt;/p&gt;
&lt;p&gt;We will get there eventually.&lt;/p&gt;
&lt;h1 id="last-one-out-turn-off-the-lights"&gt;Last one out, turn off the lights
&lt;/h1&gt;&lt;p&gt;Before terminating the program, it is good practice to release the resources that we have previously allocated:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XFreeGC&lt;/span&gt;(display, gc);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XDestroyWindow&lt;/span&gt;(display, window);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And finally, we close our connection to the X server:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;XCloseDisplay&lt;/span&gt;(display);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Under normal circumstances, the X server will also release the remaining resources owned by our client when the connection is closed.&lt;/p&gt;
&lt;p&gt;Nevertheless, explicitly releasing resources is a useful habit, particularly once our applications become more complicated and start creating and destroying resources throughout their lifetime.&lt;/p&gt;
&lt;p&gt;And that concludes our first encounter with Xlib.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;printf&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;That&amp;#39;s all folks!&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;\n&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id="further-reading"&gt;Further reading
&lt;/h1&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://xorg.freedesktop.org/releases/current/doc/libX11/libX11/libX11.html" target="_blank" rel="noopener"
 &gt;Xlib — C Language X Interface&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://xorg.freedesktop.org/releases/current/doc/xorg-docs/icccm/icccm.html" target="_blank" rel="noopener"
 &gt;Inter-Client Communication Conventions Manual (ICCCM)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The manual pages are also well worth exploring:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XOpenDisplay
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XAllocNamedColor
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XCreateSimpleWindow
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XSelectInput
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XSetWMProtocols
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XCreateGC
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XMapWindow
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XNextEvent
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XDrawString
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XLookupKeysym
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;man XCloseDisplay
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item></channel></rss>