Title: The XWindows System
1The X-Windows System
2X-Windows
- X-Windows is a network-based windowing system
developed at MIT in 1986. It was designed to
provide a standard graphical interface which
would work on a wide variety of graphic
workstations. By developing a strict graphics
communication protocol (the X-protocol) and a
programming library (Xlib), it is easier to
transfer a program from one system architecture
to another.
3X-protocol
- The X-protocol is the communications language
between the user's screen (the Display or
X-Server) and the running X-windows program (the
X-Client). - Note that the normal sense most programmers have
of the client-server structure are reversed For
X, the program is the client, the 'user' is the
server.
4X-protocol
- X-windows abstracts the communications medium
communications between the display and the
program may be through a variety of mechanisms - through UNIX Interprocess Communications
- If client and server are on the same computer
system - through a physical network
- Ethernet, ATM, Tokenring, etc.
- In the physical network case, the Display may be
an X-terminal, or another computer running an
X-Server - another UNIX system
- a PC running an X-emulator (e.g. Exceed)
5Example of an X Windows Session
- Sequence of events between the server and the
client - The user sets the DISPLAY environment variable
and ensures that the remote computer has
permission to use the display. - The user starts a process (X-client) on the
remote computer. - The X-client then establishes an X display
connection with the users X server. - The X-server and X-client communicate with each
other using the X-protocol to create and destroy
windows, to manage their layout, to receive user
input, and to report events.
6X-Windows
- X Windows provides only a means of creating and
displaying a hierarchy of windows, drawing within
them and receiving input from the users Display.
It is not a graphical user interface by itself.
This means that it does not dictate how to lay
windows out, nor how menus, buttons, and other
interface objects (commonly called Widgets)
should look and work. - This is not a bug, it is truly a feature. The
designers wanted to provide a graphical interface
system which allowed graphical workstations to
work with each other. If the appearance of the
GUI was regulated, then no one would comply and
every vendor would use their own standards
creating impossibly many versions.
7X-Windows Programming
- Writing programs for X windows can be a
frustrating task, even for experienced
programmers. A common program has the following
steps - Get any command-line options the user entered.
- Establish a connection to the users Display
- Load resources using the resource manager
routines - Setup fonts and colors based on values specified
in the resources - Decide on the initial position and size of the
top-level window - Create the top-level window
- Set window manager properties to inform the
window manager of the size and location of the
window - Create a graphics context for the top-level
- Set necessary window attributes
- Select events occurring in the top-level that
should be processed by the application - Map the top-level window, making it visible on
the Display. - Create the rest of the windows in the hierarchy.
Save Ids for later. - Read, interpret and process events in a loop
- When finished, destroy the top-level, close the
connection, and exit.
8X Windows is Event Driven
- One of the characteristics of X windows
programming which differs from standard
programming is that it is event-driven. This
gives the user more control. - After the program initializes its top-level
window, it enters the main part of the program,
the Event Loop - The Event Loop is an infinite loop which checks
for X Events on the Event Queue. - Events are generated by interaction with the
Display. This includes, but are not limited to,
keyboard input, mouse input, window exposure and
communications from the X-server. - If no events are present, it continues looping.
- If an event is present, it processes that event
calling appropriate functions for the event - An X client may select the kind of Events it will
respond to. - The loop is broken only upon exit of the
application.
9What is the X-Protocol
- The X-Protocol is the center of the X-System. It
is a set of bytecodes which transfer information,
events, and more, between the users Display and
the users program. - X was designed to be hardware/OS independent.
Data is exchanged asynchronously over a 2-way
path enabling a stream of 8-bit bytecodes. - X uses most kinds of networks. It may use
standard IPC, network protocols, or packet
protocols like TCP/IP.
10What is Xlib?
- Xlib is the C-library which provides a low-level
interface to the X-Protocol. Its use allows
coders to avoid having to issue X-protocol
bytecodes directly. - Composed of over 300 procedures
- connecting to the Display
- handling events
- handling pixmaps
- creating/destroying windows
- drawing and more
- Xlib programming is tedious, but better than
issuing bytecodes conforming to X-Protocol.
11Xt Intrinsics
- The Xt Intrinsics library is the structure on top
of Xlib, which uses an object-oriented approach
to implementing basic building blocks called
Widgets. - Widgets simplify and organize X-Windows
programming.
12Xt Intrinsics
- Provide a set of utility functions which provide
- one-step initialization of an applications main
window - reading and interpreting user-defined resource
files - Widget creation and destruction
- Event Handling
- X programs using Intrinsics are smaller and
easier to code - Most other toolkits (Motif, Athena,) are built
on top of the Intrinsics - Xlib routines are still needed for drawing lines
and other fine control tasks - The basic data structures in Xt can be used to
derive new objects. - Programs using toolkits tend to use more
resources.
13Example
- Task
- Create a simple, empty window on the screen.
Some resource editing is also included in the
example. In this case, the resource governing
the window's size is adjusted. This resizes the
empty window, making it larger and more visible. - This example program is based from the
examples in the book, The Definitive Guides to
the X Window System Volume Six, Motif
Programming Manual, written by Dan Heller
14/ Include the needed libraries for this sample
program / include ltXm/Xm.hgt / Global
Variables Most of the widgets in the
program are kept global so callback functions
that will appear in later examples may access
them. / Widget toplevel //
A single widget, this will be the
// top level window of the
program (the
// window whose parent is the Root window of
// the X
Terminal XtAppContext app //
The application context, which will
// store some data
internal to XIntrinsics
// that is associated with the
application.
15int main(int argc, char argv)
/------------------------------------------------
---------\ Initialize an X Window, no set
width or height.
Create the top level window, storing
its information in the Widget
toplevel Using the
application context app
"Hello" is the top level window's resource
name No special options needed,
Command line argument filter No fallback
resources required
\------------------------------------------------
---------/ toplevel XtVaAppInitialize
(app, "Hello", NULL, 0,
argc, argv, NULL, NULL) / Change the
toplevel widget's resources /
XtVaSetValues(toplevel, XmNwidth,
200, XmNheight, 100,
NULL) / Map the widget (the top level
window) and make it visible on the screen /
XtRealizeWidget(toplevel)
/-------------------------------------------
\ Enter the main event loop using
the application context in app.
This makes the program wait for events
from the user \------------------------------
-------------/ XtAppMainLoop(app) return
1
16Compiling and Linking X Programs
- You may have to add include paths for the X
headers, and for the X libraries - You have to add links to the X libraries
- Usually -lXt -lX11
- Sometimes more
- cc program.c -o program -lXt -lX11