The Wayback Machine - https://web.archive.org/web/20070103060448/http://www.visi.com:80/~grante/Xtut/

An Introduction to X11 User Interfaces

A tutorial prepared for SciC 8011, Spring 1995, by

Grant Edwards


If you want hardcopy of this tutorial, a properly typset version is available as a gzipped postscript file Xtut.ps.gz or as a PDF file:Xtut.pdf. Source files for the demo programs: sources.tar.gz.

Overview

This paper provides an introductory look at the X Window System. It is intended to provide the reader with information in two areas:

Introduction to

The official name is ``The X Window System.'' It is considered tres uncool to call it ``X Windows'' -- presumably because it sounds too much like ``Microsoft Windows.'' The preferred moniker seems to be simply ``X.'' If you want to sound like an old timer you can call it ``X11'' implying that you might have been around at MIT when version 10 was in use. Most experienced users refer to it as simply ``X'' or as ``X11R3'' if a particular version/release is to be specified.

X is a system for implementing a window-based user interface on bit-mapped displays. More technically, it is a specification for a high level communications protocol that is used between two programs. One of these programs is called the ``server'' and does the actually drawing on the screen and receives the raw input from the keyboard and mouse. The other program is called the ``client'' and this is where the useful work gets done. These programs may be on different computers, or they may be on the same computer.

It is important to notice that the location of the client and server are reversed compared to many client/server situations. This often causes confusion among people new to X. For example, you have a Sun workstation at your desk: you are running a CAD/simulation program on a large computer somewhere else and displaying the user interface on your desktop Sun. The Sun is running the server (it's what talks to the keyboard, mouse, and graphics hardware). The CAD program on the large computer is the ``client'' program.

The client requests that graphical operations be performed and the server provides those services.

The X protocol specification defines what goes back and forth between those two programs. The X protocol is usually implemented as a layer on top of another transport layer such as TCP/IP.

History

X was developed in a joint effort by MIT and Digital Equipment Corporation based somewhat on the ``W'' windowing system previously developed at Stanford. In 1986, X10 became the first widely used version of X, though it was quickly replaced by version 11 a year later. Since then X11 has gone through 5 more ``releases'' and is now at release 6 (referred to as X11R6). Almost all workstation vendors have adopted X11 as their windowing system, though many vendors still have proprietary extensions built into the X servers shipped with their hardware. For example, Sun Microsystems includes a display Postscript extension as well as a SunView extension in its X servers.

What X isn't

The X specifications do not specify what a program's user interface should look like, what a window manager should do, or how applications should behave. X is simply a low-level method of creating windows; receiving user inputs (key-presses and mouse events); and drawing strings, drawing lines, curves and bitmaps. The behavior of the application and the design and style of the user interface are left entirely up to client programs. While this allows for a great deal of flexibility, it results in the lack of a common ``look and feel'' for X applications.

Printing is another area where the X specifications are mute. X does not provide any printing or hardcopy services as does Microsoft Windows or the Mac OS tool box. In the absence of any other standard, postscript has been largely adopted as a de facto standard for hardcopy output by designers of X applications.

The X Protocol

Services provided by the Server

The X server provides a set of fairly low-level services that are used by clients to implement whatever user interface is desired. These services are basically the following: That's it. The X protocol doesn't know anything about radio buttons, scroll bars, sliders, text entry fields or menu bars. Those are all implemented in the client -- usually using off-the shelf libraries and toolkits.

The Client/Server Interface

As stated earlier, the X protocol defines what passes back and forth between the client and the server. The information that travels between client and server is broken up into ``packets'' at the X protocol level (which is different than Ethernet or TCP/IP packets or frames). There are four types of packets:

Low level X programming: Xlib

It is theoretically possible to write a client program using just the standard C run-time library routines. The client would have to open a socket-based connection to a server, construct requests and write them to the socket and interpret the reply, event, and error error packets. Though possible, this is unnecessary since there are libraries available that contain routines to interface to an X server using the X protocol. The most common such library is called ``Xlib'' and is intended to be called from the C programming language. The definitive books on Xlib are probably those published by O'Reilly and Associates: Xlib Programming Manual and Xlib Reference Manual. Though Xlib hides the details of communicating with the server, it does little else. The functions provided by Xlib perform only the basic tasks described above. Even a simple program written using nothing but Xlib can require large amounts of code. A ``hello world'' program [the ``basicwn'' program shown as an example in by Adrian Nye's Xlib Programming Manual] whose output is shown below requires 200 lines of C code.

This program does nothing other than create a window, check the window size and the size of the root display, display the text shown, and wait for any key or mouse button before exiting. The source code for this program is available for the morbidly curious.

As you might guess, writing an entire application using Xlib directly is not practical. The client application must specify every window to be drawn and every bit of text or line to be drawn. The application must then read events from the the server and process them. When a window is resized, the application must take appropriate actions and redraw everything in the correct places. What is needed is a higher level interface that hides much of the gritty details from the application programmer.

X Toolkits

Since Xlib is obviously not practical as a basis for applications programming, various toolkits have been written that provide another layer above Xlib. Several of the more well known toolkits are Describing all of these toolkits individually is beyond the scope of this tutorial, but we will take a look examples using the X Toolkit (Xt) as well as Tk and make a few comments on some of the the others.

All of these toolkits provide an additional level of abstraction. Rather than dealing with windows, lines, text strings, key-presses and mouse buttons, the applications programmer deals with objects called ``widgets'' that would be recognizable to the end user: list-boxes, text-entry fields, buttons, pop-up menus, etc. The application programmer now writes code to create these high-level objects and to specify what action is to be taken when a button is pressed or a menu entry is selected. The toolkits handle most (if not all) of the X events, and invoke the call-back routines provided by the application as required.

Though toolkits provide ways for users to define new widgets, many applications are written using only predefined widgets Much of the rest contain a single customized widget along with predefined widgets.

The Xt Toolkit

Xt provides a set of routines that are used by C programs to implement and manage widgets. Along with Xt, the standard X11 distribution includes a set of example widgets called the Athena widgets. While the Athena widgets were intended only as a demo showing how Xt is used, they formed the basis for many applications. [Project Athena was the name of the group at MIT that developed X -- and Athena was for many years the name of the machine from which you ftp'd X related files.]

The typical Athena look is exemplified by rectangular monochrome buttons and an overall flat, monochrome look similar to (but uglier than) the original Macintosh interface. [Later extensions to Athena widgets added support for ``shaped'' buttons which allowed for Macintosh-like buttons with rounded corners.] While the Athena widgets can be seen used in much of the free software available on the net, they are rarely used in commercial products which tend to use ``prettier'' toolkits.

The output of a small application using Xt and the Athena widgets is shown above. This application displays a main window containing a label widget at the top containing the text ``Pick one'' and below that two buttons. If the left button is pressed, ``Hi there'' is written to stdout; if the right button is pressed, the program exits. This program took 43 lines of C code and is available for examination by clicking here. This is obviously an improvement over the Xlib program since it takes significantly fewer lines of code even though the application is more complex.

The OPEN LOOK Toolkit

OPEN LOOK started out as a user-interface specification developed jointly by AT&T; and Sun Microsystems. One implementation of the OPEN LOOK standard was done by AT&T; using a superset of Xt called Xt+ and a set of OPEN LOOK widgets. Another implementation of this standard is a user interface library called XView developed by Sun. The XView library is not based on Xt, but is an entirely independent toolkit. Both implementations share the same ``look and feel'' and applications should behave similarly. Compared to Xt/Athena, the OPEN LOOK widget set offers a richer feature set and more complex widgets. We won't look at an example program, but the typical OPEN LOOK style can be seen below.



For several years OPEN LOOK competed with Motif for the hearts and minds of developers and users. Recently, the organizations who were backing OPEN LOOK seem to be loosing the battle and have begun moving to the Motif toolkit. The OPEN LOOK FAQ is avialable from faqs.org.

The Motif Toolkit

The Motif toolkit was developed by the Open Software Foundation (OSF), which includes Digital Equipment Corporation, Hewlett-Packard, and a few others. The application program interface (API) has recently been released into the public domain, but all of the implementations of Motif now available are proprietary, commercial products. While there aren't currently any free Motif-compliant toolkits available there is work in progress on several such projects which go by names like Lesstif and Notif. The Motif toolkit provides functionality similar to the OPEN LOOK toolkit, though the widgets have a different look and feel.

The Motif look and feel is shown by a dialog box below:

One of the oft-cited differences in philosophy between Motif and OPEN LOOK involve the movement of the mouse pointer on the screen. Using the Motif library, the mouse pointer only moves when the user moves the mouse. The OPEN LOOK widgets will move the mouse pointer independently. In X parlance this is known as ``warping'' the pointer. This may seem like a trivial point, but many users strongly object to having the mouse pointer moved when they didn't move the mouse. [I happen to agree with them. I find that having the mouse pointer jump to another portion of the screen on its own is very jarring and disrupts my already fragile thought processes. I've been told recently that XView's pointer warping "feature" can be disabled by the user.]

The Tk Toolkit

The Tk toolkit was developed at UC Berkeley by John Ousterhout as a follow-on for the Tcl interpreted language. Tcl (Tool command language) was developed to provide a common command language for a set of interactive software tools being developed at UCB in the EE department for VLSI design. The Tk toolkit was latter added to provide an X11 based GUI for Tcl.

Tcl is a string-based, interpreted language similar to a Unix ``shell'' language. Up to this point, all of the toolkits that have been discussed were for use by C programs. Tk, however, was designed to be used with an interpreter, and all communication between the Tk toolkit and the user program is done with strings rather than C language data structures. Since its original use with the Tcl interpreter, the Tk toolkit has been adapted for use with interpreters for other languages including scheme, python, and perl.

Tk was designed to have a ``look and feel'' very similar to Motif, and users switching back forth between Motif and Tk applications will not notice paradigm differences the way they would switching between OPEN LOOK and either Motif or Tk.

The Tk toolkit provides a very high level of abstraction and the user doesn't see any of the details of X11. A simple application can be written in just a few lines of code. The Xt/Athena program was re-written using the STk Scheme interpreter that incorporates the Tk toolkit. The application window produced is shown here:

The scheme version looks like this:

(label '.toplabel :text "Pick one") (button '.helloButton :text "Hello" :command '(format #t "Hi There\n")) (button '.exitButton :text "Exit" :command '(exit)) (pack .toplabel :side 'top :padx 5 :pady 5) (pack .helloButton :side 'left :padx 10 :pady 5) (pack .exitButton :side 'right :padx 10 :pady 5) The equivalent program in Tcl would look something like: label .toplabel -text "Pick one" button .helloButton -text "Hello" -command {puts "Hello"} button .exitButton -text "Exit" -command exit pack .toplabel -side top -padx 5 -pady 5 pack .helloButton -side left -padx 10 -pady 5 pack .exitButton -side right -padx 10 -pady 5 As you can see, the number of lines required to implement a trivial X11 program has been considerably reduced from the 200 lines required using Xlib and even from the 43 lines required using Xt/Athena.

Originally, the intended use for Tcl/Tk was to provide an easy way to program a user interface (either graphical using Tk or command-line using Tcl alone). The real work in the application was done by C language routines that implemented application specific Tcl commands which were invoked by the Tcl based user-interface. This allowed a set of basic blocks to be implemented in C and then they could be ``glued'' together using Tcl.

However, the popularity of Tcl has resulted in fairly large applications being written solely in Tcl/Tk with little or no additional C coding. Though Tcl is the most popular language that has been mated with Tk, some prefer a more powerful or general programming language like scheme. One of the drawbacks of Tcl is that all data is stored as strings --- thus the interpreter is roughly an order of magnitude slower than a language like scheme. While the speed of the Tcl interpreter is quite adequate for use as a user interface, it's a bit slow if any intensive computation is to be done. Both Tcl/Tk and STk are free software packages that run on most Unix/X11 systems. The standard reference for Tcl/Tk is John Ousterhout's book Tcl and the Tk Toolkit.

STk was developed by Erick Gallesio at the University of Nice in France. The STk FAQ is available via WWW, and the software can be ftp'd from ftp.cs.umn.edu/pub/misc/STk.

Custom and Add-on Widgets

No matter what toolkit you choose, there is a distinct possibility that the standard widgets either just won't do what you want or the performance is so poor that something else must be done. That something else is buying or writing a new widget. There are available commercial add-on packages of widgets for some toolkits. These widgets offer some very sophisticated, high-level functionality. Recently, add-on widget packages that do moderately sophisticated rendering have become available. Some of the products seen advertised include ``XRT Widgets'' from KL Group and the ``Generic Logic Toolkit'' from Generic Logic.

Or, if you've got more time/money than you can find a use for, you can write your own custom widget. Toolkits generally provide a way for the user to create a new widget. This is generally done in an ``object oriented'' way based on one of the existing widgets:

This is not something to be undertaken lightly. In order to write a new widget, you will have to learn more about Xlib and the internals of your toolkit than you really want to. Writing a moderately complex widget will probably take at least two person-weeks assuming you are an experienced C programmer with a basic understanding of Xlib and the internals of the toolkit.

Standard Disclaimer

Writing an non-trivial X11 based program with a graphical user interface is not easy. Writing one with a good user interface is even harder. Coming up with a good user interface involves a large amount of trail-and-error even for experienced designers.

Even with a high level toolkit like Tk, a full-featured user interface for a non-trivial application requires a significant amount of effort. Writing a significant user interface using Xlib would be masochistic at best and futile at worst.

Summary

Hopefully, we've seen that

Shameless Plug for the Author's Favorite Toolkit

If you really want to write an X11 application, especially if you want to throw something together quickly, consider STk. If you just can't stomach scheme (which is a small, elegant, statically scoped dialect of Lisp), then take a look at Tcl/Tk -- it's the same toolkit with a different language. They're both free, and both can produce a professional looking job with a minimum of fuss.