
set ::section_color "#93b1e9" 

puts {<html>
	<head>
		<title>Beginner's Xlib Tutorial</title>
	</head>
	<body>
		<H1>Beginner's Xlib Tutorial</H1>

		<i>By George Peter Staplin -- GeorgePS at XMission.com</i>

		<br>

		<b>This document may be copied and distributed provided that the author is given credit.</b>
		
	<table>
}


proc section {name txt} {
 puts "\t\t\t<tr>"
 puts "\t\t\t\t<th align=\"left\" bgcolor=\"$::section_color\">$name</th>"
 puts "\t\t\t</tr>"
 puts "\t\t\t<tr>"
 puts "\t\t\t\t<td>$txt</td>"
 puts "\t\t\t</tr>"
}


section "Basic Overview" {The intention of this tutorial is to teach you how to use the Xlib interface for programming X, and how to learn more so that you can write advanced applications that require the speed or power of Xlib.}


section "The Design of X" {X is designed to make communication between the application and the drawing layer over a network be abstract.  The user application is known as a client, and the program that draws what the client requests is known as a server.  A client/application connects to a server using <b>XOpenDisplay()</b>.  <b>XOpenDisplay()</b> returns a pointer to a Display structure.  This display structure is passed to most Xlib calls to communicate with the server.}


section "Fundamental Types" {X has fundamental types that are used to represent drawable surfaces.  The following types are classified as <b>Drawables</b>:
	<ul type="square">
		<li><b>Window</b></li>
		<li><b>Pixmap</b></li>
		<li><b>XdbeBackBuffer</b></li>
	</ul>The above types and the <b>Drawable</b> type are <b>XID</b>s.  An <b>XID</b> is a unique integer associated with an object.  A <b>Window</b> or <b>Pixmap</b> can generally be used in places where a <b>Drawable</b> is specifed as the argument type for an Xlib function. For debugging purposes it is often useful to use a pattern of <i>printf ("winid %lu\n", some_window);</i>}


section "Windows and Layers of Windows" {A common misconception is that a window is only what is dragged around on a desktop.  X has layers of windows.  The root window is also known as the background window.  It can have a background image, animation, and other fun things.  Immediate children of the root window are generally reparented into windows with titlebars, and buttons.  Most applications are made up of many windows, and windows within windows.}


section "Definition of a Widget" {A <b>widget</b> is usually associated with a window.  A few simple examples of widgets are:
	<ul type="square">
		<li>buttons</li>
		<li>menus</li>
		<li>scrollbars</li>
	</ul>
Some of you may know of widgets as "components."}


section "Definition of a Gadget" {A <b>gadget</b> is typically an item that is drawn onto its parent, and doesn't have a window for itself.  Text labels are implemented in some toolkits as <b>gadgets</b>.}


section "Pixmaps" {A <b>Pixmap</b> is a <b>Drawable</b> that is offscreen.  A <b>Pixmap</b> can be drawn on, and then copied to a window.  It's also possible to copy a <b>Pixmap</b> to another <b>Pixmap</b>.  A <b>Pixmap</b> is useful for buffering what you will draw.}


section "Events and their place in X" {The X server sends messages known as events, to a client so that it may know of:
	<ul type="square">
		<li>mouse button presses/releases</li>
		<li>keyboard key presses/releases</li>
		<li>a new window</li>
		<li>exposure of a window through raising it, uncovering it, or creating it</li>
		<li>the resizing of a window</li>
	</ul>
The X server doesn't send a client every single event that occurs, unless explicitly told to.  To tell the X server which events an application needs the <b>XSelectInput()</b> function is used.  It takes a mask of OR'd flags.  For example: <i>XSelectInput (dis, win, ExposureMask | KeyPressMask | ButtonPressMask);</i>}


section "The XEvent Structure" {An <b>XEvent</b> structure contains a union of structures for the various types of events.  For example, an <b>XButtonEvent</b> is contained in the <b>XEvent</b> structure with the name "xbutton"  These structures are documented in the manual pages for their respective names.}


section "The Event Queue" {When events are generated by user interaction or background tasks, they are placed into a queue that is specific to each display connection.  This queue of events is then processed by the client.  A common function for the purpose of retrieving and removing an item from the queue is <b>XNextEvent()</b>.  There are several other functions that deal with the event queue.  Some check for events without removing them from the queue, others check for specific classes of events.}


section "The Request Buffer" {Xlib functions that send requests are buffered.  In many cases an <i>XFlush (dis);</i> is needed to force the buffered requests to be sent/flushed to the server.  Another related function is <i>XSync</i> which flushes and then waits until the processing of a request is complete.}


section "Colors" {X has been ported to dozens of different kinds of machines.  From black and white displays, to 32-bit or higher displays.  Most common displays are now using 16, 24, or 32 bits for each pixel.  Older displays may only allow for 8 bits.  Colors may be represented differently depending on the system.  Some systems may have colormaps that retain a variable number of color cells.

<b>XParseColor()</b> is used to convert a symbolic name to a series of RGB values for an <b>XColor</b> structure.  <b>XAllocColor</b> is used after a color is parsed, and its RGB components stored in a structure.  XAllocColor will match the color's RGB components to a cell in the colormap for the display.}


section "Images" {<p>X uses an <b>XImage</b> structure for storage of regions of the display, that can be manipulated as sequential bytes of memory in the ->data member of the structure, or via the <b>XPutPixel()</b>/<b>XGetPixel()</b> functions.</p>

<p>The format of the XImage ->data member is dependent on the visual, and byte order of the display.  An X server may run on a Sparc&#153; while the client runs on an Intel Pentium&#153;. This means that the data will need to be reformated, because the byte order is different on these two processors.  If the data is not reformatted then the colors may be incorrect.  There are two formats for the <i>->byte_order</i> member; <b>LSBFirst (least significant byte first), and </b>MSBFirst (most significant byte first).  See <b>XPutImage</b> below.</p>

<p>The <b>XPutPixel</b> and <b>XGetPixel</b> functions know how to properly handle the display's visual formats, and translate as needed.  However, the large number of function calls needed for setting/manipulating an image may cause a slowdown if they are used.</p>}


section "XCreateImage" {The <b>XCreateImage()</b> function is used to create an <b>XImage</b> structure that represents the image data.  This structure is then passed to the <b>XPutImage()</b> function to transfer the content to a <b>Pixmap</b> or <b>Window</b> or <b>XdbeBackBuffer</b>.  By default the <i>->byte_order</i> member of an <b>XImage</b> returned is in the format of server (MSBFirst or LSBFirst (see above)).}


section "XPutImage" {The <b>XPutImage()</b> function is used to transfer an image to the server.  It also performs byte order conversions if the <i>->byte_order</i> of the <b>XImage</b> is different than the server's byte order.}


section "Finding the Byte Order" {This is best done at configuration time of your software, before it's compiled.  A simple function that can be used to find the byte order is:

<pre>
int get_byte_order (void) {
	union {
		char c[sizeof(short)];
		short s;
	} order;

	order.s = 1;
	if ((1 == order.c[0])) {
		return LSBFirst;
        }
	return MSBFirst; 
}
</pre>}


section "Mapping/Displaying Windows" {After a window is created it isn't visible on the screen.  To display a window the <b>XMapWindow()</b> function is used.  There are other functions that perform mapping as well, and they are documented on the man page for the <b>XMapWindow</b> function.}


section "Unmapping/Hiding Windows" {The function <b>XUnmapWindow()</b> is typically used to unmap a window.  The contents of the display will be lost in most cases, and you will need to redraw it, after mapping it again (if you do remap in the future).}


section "Helping Yourself" {The X man pages are vital when programming with Xlib.  A very common way to help yourself is to run <i>man -k keyword</i>.  When the documentation isn't clear you may find it useful to consult the postscript documentation that is available with the X sources.  The X sources are also useful when the documentation is unclear.  The Xlib sources can be found in <i>xsrc/xc/lib/X11</i>.  It's very useful to understand how to use grep when finding how a function is implemented, such as which file, and which line.  For example: <i>grep 'XCreateGC[ \t](' *</i>}


section "Learn by Example" {The various utilities distributed with the X distribution are incredibly useful for learning how to use Xlib, and its many features.}


section "Help!?  I'm stuck!" {This happens to most X programmers.  Some aspects of X may be confusing you, or you're uncertain of something, or misunderstand a key concept.  If the steps outlined in <b>Helping Yourself</b> above don't help then I suggest you study the Usenet archives for <b>comp.windows.x</b> at <a href="http://groups.google.com">Google Groups</a>}


puts {
	</table>
	</body>
</html>}