Learning to Use X11
When I started programming many years ago, on a system very, very different from what we use now, producing graphical output from programs was easy; all the necessary commands were usually built right into the language. Later, when I moved to C and UNIX, things were no longer simple. Not only does C not include any graphics manipulation functions, per se, but all graphical output in UNIX has to go through the standard UNIX windowing system: the X Window System, release 11, version 6.6 (its current incarnation), or X11 for short.
Unfortunately, X11 is rather hard to approach. First of all, it is huge (the O'Reilly series of printed manuals and references includes six volumes with close to 6,000 pages among them, not counting the additional four volumes and 2,000 pages when we include Motif--I know of hardly any other language, library, framework or application that big). However, the fact that it is based on concepts quite different from the ones currently in use makes it difficult to understand. To top it all off, the X11 documentation makes use of a specialized and not-so-obvious terminology. Therefore, we need to establish a minimal glossary.
The X Window System was specifically designed to allow the graphical output of a program running on one machine to appear on a different machine, possibly one that is physically remote and/or a different make and architecture. In other words, X11 was designed to be a platform-independent, networked graphics framework.
In X11 parlance, the "display" denotes the box on which the graphical output will appear. Interestingly, an individual display is defined by the X11 documentation as having exactly one keyboard and one pointer (i.e., mouse), but potentially multiple CPUs, monitors, etc.
The "screen" corresponds to the actual physical display device; in most cases this will be a monitor. X11 allows for an arbitrary number of screens to be connected to each display. Think of a workstation with two monitors or a departmental server, connected to a larger number of (relatively dumb) X terminals.
Finally, a "window" is a rectangular area of the screen that can be used for input and output. If the rectangular area is not directly associated with a screen, but instead resides in memory, it is referred to as a "pixmap". Pixmaps and windows share the property of being "drawable" and can be used interchangeably in some function calls. It is important to remember that to X11 a window is merely a rectangular area on the screen. As such, it does not include things like titlebars, scrollbars and other GUI elements that we have come to associate with the word window. If these elements are present, they are controlled by a different program called a window manager.
Every GUI-oriented computer ships with a mouse or equivalent. When X11 came about, the development of graphical input devices was still in its infancy. Consequently the X11 documentation always speaks (somewhat bashfully) of a "pointer" (a generic term for mice), trackballs, digitizing tablets or other yet-to-be-invented graphical input devices. A final cause of confusion is the specific usage of the words client and server in X11: a "client" is any application that creates data for graphical output. The "server" is the program that manages the shared resource accessed by all clients, namely the (finite) amount of screen real estate. The unfortunate consequence of this naming convention is that the (X11) client typically executes on the server (machine), while the (X11) server runs on the client (computer).
We are now ready to write a simple example program that demonstrates the most important concepts and functions for programming with X11. The program will pop up a window on the screen, draw an X in it and disappear after a mouse-click into the window area. The following is a line-by-line explanation of the program given in Listing 1.
Line 1: Xlib.h is the most important header file for X11 programming. It defines several structs and macros used throughout an X11 program and provides function prototypes for all the basic functions in the library. Other headers are part of X11 as well. If those are needed, Xlib.h usually has to be included before any of the other headers because they depend on it. Strangely, the dependent headers do not themselves include Xlib.h.
Lines 5 and 6: Display is a struct defined in Xlib.h that represents the display, i.e., the box on which the graphical output is meant to appear. The library function XOpenDisplay attempts to establish a connection to the X11 server on this machine. As argument, it takes a zero-terminated string with the display_name, in the following format: hostname:servernumber.screennumber. If the argument is NULL, the display_name defaults to the value of the environment variable DISPLAY. If no connection can be established, XOpenDisplay will return NULL.
Line 9: we obtain an identifier to be used for the screen. DefaultScreen is not a function; it is a macro defined in Xlib.h. X11 provides macros like this as accessors for the elements of the Display struct that are accessible to the programmer. Members of this and other library structs should never be accessed directly, only through the provided macros. Variables for which no macros are provided should be considered "private".
Lines 10 and 11: colors in X11 are identified by integer numbers. Since X11 is meant to be platform independent, it does not make any assumptions about the capabilities of any device (i.e., the screen not the display) to render colors. Only two colors are guaranteed to be available, black and white. Note that the actual appearance of these values does not have to correspond to black and white pixels; think of those old monitors with green (or amber) letters on a black background.
Line 14: finally, we are ready to create a window. The data type Window is not a struct as one might assume. Rather it is typedefed to some integer data type and merely provides an identifier for a window.
Line 15: each window is the child of some other window and is geometrically contained by it. Here we set the root window (the entire screen) of the default screen as the parent window.
Line 16: the coordinates of the upper-left-hand corner of the new window with respect to the parent window, designated in pixels. X11 uses graphical coordinates with the origin in the upper left and with the positive X-axis running to the right and the positive Y-axis running down. However, since the placement of new windows is under control of the window manager, the new window can pop up pretty much anywhere on the screen, regardless of the values of these two arguments--expect surprises.
Line 17: the width and height (in that order) of the new window, in pixels.
Line 18: the width and color of the border of the window. This is not the border that may be added by the window manager.
Line 19: the color of the background of the window.
Line 21: so far we have created a data structure for the window, but only in memory. In X11 a window is not automatically displayed when it is created. Making the window visible is a separate process, called mapping. The function XMapWindow consequentially maps the window on the specified display. Note that it is not necessary to specify the screen again; when the window was created, it was explicitly created as child of a parent window, which itself is associated with a specific screen.
Practical Task Scheduling Deployment
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.View Now!
|The Firebird Project's Firebird Relational Database||Jul 29, 2016|
|Stunnel Security for Oracle||Jul 28, 2016|
|SUSE LLC's SUSE Manager||Jul 21, 2016|
|My +1 Sword of Productivity||Jul 20, 2016|
|Non-Linux FOSS: Caffeine!||Jul 19, 2016|
|Murat Yener and Onur Dundar's Expert Android Studio (Wrox)||Jul 18, 2016|
- The Firebird Project's Firebird Relational Database
- Stunnel Security for Oracle
- My +1 Sword of Productivity
- SUSE LLC's SUSE Manager
- Managing Linux Using Puppet
- Non-Linux FOSS: Caffeine!
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Parsing an RSS News Feed with a Bash Script
- Google's SwiftShader Released
- Doing for User Space What We Did for Kernel Space
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide