| 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. |
| 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 XOpenDisplay(). XOpenDisplay() returns a pointer to a Display structure. This display structure is passed to most Xlib calls to communicate with the server. |
| Fundamental Types |
X has fundamental types that are used to represent drawable surfaces. These types are:
- Drawable
- Window
- Pixmap
- XdbeBackBuffer
The above types are all XIDs. An XID is a unique integer associated with an object. A Window or Pixmap can generally be used in places where a Drawable is specifed as the argument type for an Xlib function. For debugging purposes it is often useful to use a pattern of printf ("winid %ld\n", some_window); |
| Windows and what they are |
| A common misconception is that a window is 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 frames with titlebars, and buttons. Most applications are made up of many windows, and windows within windows. |
| What's a widget? |
| A widget is usually associated with a window. A few simple examples of widgets are:
Some of you may know of widgets as "components." |
| Pixmaps |
| A Pixmap is a drawable that is offscreen. A Pixmap can be drawn on, and then copied to a window. It's also possible to copy a pixmap to another pixmap. A pixmap is useful for buffering what you will draw. |
| Events and their place in X |
The X server sends messages known as events, to a client so that it may know of:
- mouse button presses/releases
- keyboard presses/releases
- a new window
- exposure of a window through raising it, uncovering it, or creating it
- a window being resized
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 XSelectInput() function is used. It takes a mask of OR'd flags. For example: XSelectInput (dis, win, ExposureMask | KeyPressMask | ButtonPressMask); |
| 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 XNextEvent(). 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. |
| The Request Queue |
| Xlib functions that send requests are buffered. In many cases an XFlush (dis); is needed to force the buffered requests to be sent/flushed to the server. Another useful function is XSync which flushes and then waits until the processing of a request is complete. |
| 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 or 32 bits for each pixel. The older displays may only allow for 8 bits. Colors may be represented differently depending on the system.
[talk about the XParseColor/XAllocColor combination]
[learn more about colormaps and document how I have used them]
|
| Images |
|
XCreateImage
XPutImage
XGetImage
pitfalls, ximage->data being in the server's order by default after XCreateImage.
get_image_byte_order() vs. build system byte order
|
| Mapping/Unmapping Windows |
|
|
| Windows without titlebars |
|
|
| Helping Yourself |
| The X man pages are vital when programming with Xlib. A very common way to help yourself is to run man -k keyword. 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 xsrc/xc/lib/X11. It's very useful to understand how to use grep when finding how a function is implemented, such as which file, and which line. grep 'XCreateGC[ \t](' * |