Xlib
Encyclopedia
Xlib is an X Window System
X Window System
The X window system is a computer software system and network protocol that provides a basis for graphical user interfaces and rich input device capability for networked computers...

 protocol client library
Library (computer science)
In computer science, a library is a collection of resources used to develop software. These may include pre-written code and subroutines, classes, values or type specifications....

 written in the C programming language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

. It contains function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

s for interacting with an X server
Server (computing)
In the context of client-server architecture, a server is a computer program running to serve the requests of other programs, the "clients". Thus, the "server" performs some computational task on behalf of "clients"...

. These functions allow programmer
Programmer
A programmer, computer programmer or coder is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software. One who practices or professes a formal approach to...

s to write programs without knowing the details of the protocol. Few applications use Xlib directly; rather, they employ other libraries that use Xlib functions to provide widget toolkit
Widget toolkit
In computing, a widget toolkit, widget library, or GUI toolkit is a set of widgets for use in designing applications with graphical user interfaces...

s:

  • Intrinsics
    Intrinsics
    X Toolkit Intrinsics is a library used in the X Window System. More precisely, it is a library that uses the low-level Xlib library and provides a friendly API to develop X11 software with graphical widgets...

     (Xt)
  • Athena widget set
    Xaw
    Xaw is short for the X Window System Athena widget set, which is a set of widgets to implement simple user interfaces based upon the X Toolkit Intrinsics...

     (Xaw)
  • Motif
    Motif (widget toolkit)
    In computing, Motif refers to both a graphical user interface specification and the widget toolkit for building applications that follow that specification under the X Window System on Unix and other POSIX-compliant systems. It emerged in the 1980s as Unix workstations were on the rise, as a...

  • FLTK
    FLTK
    FLTK is a cross-platform GUI library developed by Bill Spitzak and others. Made with 3D graphics programming in mind, it has an interface to OpenGL, but it is also suitable for general GUI programming....

  • GTK+
    GTK+
    GTK+ is a cross-platform widget toolkit for creating graphical user interfaces. It is licensed under the terms of the GNU LGPL, allowing both free and proprietary software to use it. It is one of the most popular toolkits for the X Window System, along with Qt.The name GTK+ originates from GTK;...

  • Qt
    Qt (toolkit)
    Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface , and also used for developing non-GUI programs such as command-line tools and consoles for servers...

     (X11 version)
  • Tk


Xlib appeared around 1985, and is currently used in GUI
Gui
Gui or guee is a generic term to refer to grilled dishes in Korean cuisine. These most commonly have meat or fish as their primary ingredient, but may in some cases also comprise grilled vegetables or other vegetarian ingredients. The term derives from the verb, "gupda" in Korean, which literally...

s for many Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s. The XCB
XCB
In computing, XCB is a C language binding for the X Window System. It is implemented as free software and aims to replace Xlib. The project was started in 2001 by Bart Massey....

 library is an attempt to replace Xlib. While currently Xlib is still in wide use, Xlib's implementation nowadays can use XCB as a low-level transport layer.

Data types

The main types of data in Xlib are the Display structure and the types of the identifiers.

Informally, a display is a physical or virtual device where graphical operations are done. The Display structure of the Xlib library contains information about the display, but more importantly it contains information relative to the channel between the client and the server. For example, in a Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 operating system, the Display structure contains the file handle of the socket
Unix domain socket
A Unix domain socket or IPC socket is a data communications endpoint for exchanging data between processes executing within the same host operating system. While similar in functionality to...

 of this channel (this can be retrieved using the ConnectionNumber macro.) Most Xlib functions have a Display structure as an argument because they either operate on the channel or are relative to a specific channel. In particular, all Xlib functions that interact with the server need this structure for accessing the channel. Some other functions need this structure, even if they operate locally, because they operate on data relative to a specific channel. Operations of this kind include for example operations on the event queue, which is described below.

Windows, colormaps, etc. are managed by the server, which means that the data about their actual implementation is all stored in the server. The client operates on these objects by using their identifiers. The client cannot directly operate on an object, but can only request the server to perform the operation specifying the identifier of the object.

The types Windows, Pixmap, Font, Colormap, etc. are all identifiers, which are 32-bit integers (just as in the X11 protocol itself). A client “creates” a window by requesting that the server create a window. This is done via a call to an Xlib function that returns an identifier for the window, that is, a number. This identifier can then be used by the client for requesting other operations on the same window to the server.

The identifiers are unique to the server. Most of them can be used by different applications to refer to the same objects. For example, two applications connecting with the same server use the same identifier to refer to the same window. These two applications use two different channels, and therefore have two different Display structures; however, when they request operations on the same identifier, these operations will be done on the same object.

Protocol and events

The Xlib functions that send requests to the server usually do not send these requests immediately but store them in a buffer, called the request buffer. The term request in this case refers to the request from the client that is directed to the server: the request buffer can contain all kinds of requests to the server, not only those having a visible effect on the screen. The request buffer is guaranteed to be flushed (i.e., all requests done so far are sent to the server) after a call to the functions XSync or XFlush, after a call to a function that returns a value from the server (these functions block until the answer is received), and in some other conditions.

Xlib stores the received events in a queue. The client application can inspect and retrieve events from the queue. While the X server sends events asynchronously, applications using the Xlib library are required to explicitly call Xlib functions for accessing the events in the queue. Some of these functions may block; in this case, they also flush the request buffer.

Errors are instead received and treated asynchronously: the application can provide an error handler that will be called whenever an error message from the server is received.

The content of a window is not guaranteed to be preserved if the window or one of its parts are made not visible. In this case, the application are sent an Expose event when the window of one part of it is made visible again. The application is then supposed to draw the window content again.

Functions

The functions in the Xlib library can be grouped in:
  1. operations on the connection (XOpenDisplay, XCloseDisplay, ...);
  2. requests to the server, including requests for operations (XCreateWindow, XCreateGC,...) and requests for information (XGetWindowProperty, ...); and
  3. operations that are local to the client: operations on the event queue (XNextEvent, XPeekEvent, ...) and other operations on local data (XLookupKeysym, XParseGeometry, XSetRegion, XCreateImage, XSaveContext, ...)

Example

The following program creates a window with a little black square in it.


/*
Simple Xlib application drawing a box in a window.
gcc input.c -o output -lX11
*/

#include
#include
#include
#include

int main(void) {
Display *d;
Window w;
XEvent e;
char *msg = "Hello, World!";
int s;

/* open connection with the server */
d = XOpenDisplay(NULL);
if (d NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}

s = DefaultScreen(d);

/* create window */
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 200, 1,
BlackPixel(d, s), WhitePixel(d, s));

/* select kind of events we are interested in */
XSelectInput(d, w, ExposureMask | KeyPressMask);

/* map (show) the window */
XMapWindow(d, w);

/* event loop */
while (1) {
XNextEvent(d, &e);
/* draw or redraw the window */
if (e.type Expose) {
XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
}
/* exit on key press */
if (e.type KeyPress)
break;
}

/* close connection to server */
XCloseDisplay(d);

return 0;
}


The client creates a connection with the server by calling XOpenDisplay. It then requests the creation of a window with XCreateSimpleWindow. A separate call to XMapWindow is necessary for mapping the window, that is, for making it visible on the screen.

The square is drawn by calling XFillRectangle. This operation can only be performed after the window is created. However, performing it once may not be enough. Indeed, the content of the window is not always guaranteed to be preserved. For example, if the window is covered and then uncovered again, its content might require being redrawn. The program is informed that the window or a part of it has to be drawn by the reception of an Expose event.

The drawing of the window content is therefore made inside the loop handling the events
Event loop
In computer science, the event loop, message dispatcher, message loop, message pump, or run loop is a programming construct that waits for and dispatches events or messages in a program...

. Before entering this loop, the events the application is interested in are selected, in this case with XSelectInput. The event loop waits for an incoming event: if this event is a key press, the application exits; if it is an expose event, the window content is drawn. The function XNextEvent blocks and flushes the request buffer if there is no event in the queue.
Other libraries
Xlib does not provide support for buttons, menus, scrollbar, etc. Such widget
Widget (computing)
In computer programming, a widget is an element of a graphical user interface that displays an information arrangement changeable by the user, such as a window or a text box. The defining characteristic of a widget is to provide a single interaction point for the direct manipulation of a given...

s are provided by other libraries, which in turn use Xlib. There are two kinds of such libraries:
  • libraries built atop of the Intrinsics
    Intrinsics
    X Toolkit Intrinsics is a library used in the X Window System. More precisely, it is a library that uses the low-level Xlib library and provides a friendly API to develop X11 software with graphical widgets...

     library (Xt), which provides support for widgets but does not provide any particular widget; specific widgets are provided by widget set libraries that use Xt, such as Xaw
    Xaw
    Xaw is short for the X Window System Athena widget set, which is a set of widgets to implement simple user interfaces based upon the X Toolkit Intrinsics...

     and Motif
    Motif (widget toolkit)
    In computing, Motif refers to both a graphical user interface specification and the widget toolkit for building applications that follow that specification under the X Window System on Unix and other POSIX-compliant systems. It emerged in the 1980s as Unix workstations were on the rise, as a...

    ;
  • libraries that provide widget sets using Xlib directly, without the Xt library, such as the X versions of GTK+
    GTK+
    GTK+ is a cross-platform widget toolkit for creating graphical user interfaces. It is licensed under the terms of the GNU LGPL, allowing both free and proprietary software to use it. It is one of the most popular toolkits for the X Window System, along with Qt.The name GTK+ originates from GTK;...

    , Qt
    Qt (toolkit)
    Qt is a cross-platform application framework that is widely used for developing application software with a graphical user interface , and also used for developing non-GUI programs such as command-line tools and consoles for servers...

    , FLTK
    FLTK
    FLTK is a cross-platform GUI library developed by Bill Spitzak and others. Made with 3D graphics programming in mind, it has an interface to OpenGL, but it is also suitable for general GUI programming....

     and fpGUI
    FpGUI
    fpGUI, the Free Pascal GUI toolkit, is a cross-platform graphical user interface toolkit developed by Graeme Geldenhuys. fpGUI is open source and free software, licensed under a Modified LGPL license...

    .


Applications using any of these widget libraries typically specify the content of the window before entering the main loop and do not need to explicitly handle Expose events and redraw the window content.

The XCB
XCB
In computing, XCB is a C language binding for the X Window System. It is implemented as free software and aims to replace Xlib. The project was started in 2001 by Bart Massey....

 library is an alternative to Xlib. Its two main aims are: reduction in library size and direct access to the X11 protocol. A modification of Xlib has been produced to use XCB as a low-level layer.
See also


  • CLX (Common Lisp)
    CLX (Common Lisp)
    CLX is the standard X Window System protocol client library for the Common Lisp programming language,1:3 the C equivalence of which is Xlib....

  • X Window System
    X Window System
    The X window system is a computer software system and network protocol that provides a basis for graphical user interfaces and rich input device capability for networked computers...

  • X Window System protocols and architecture
    X Window System protocols and architecture
    In computing, the X Window System is a network-transparent windowing system for bitmap displays. This article details the protocols and technical structure of X11.-Client–server model and network transparency:...


External links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK