Xlib

Xlib

Xlib is an X Window System protocol client library in the C programming language. It contains functions for interacting with an X server. These functions allow programmers 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 toolkits:

* Intrinsics (Xt)
* Athena widget set (Xaw)
* Motif
* GTK+
* Qt (X11 version)
* Tk

Xlib appeared around 1985, and is currently used in GUIs for many Unix-like operating systems.

The XCB library is an attempt to replace Xlib.

Data types

The main types of data in Xlib are the Displaycite web|url=http://webcvs.freedesktop.org/xorg/lib/X11/include/X11/Xlib.h?revision=1.6&view=markup|title=Display Structure on freedesktop CVS|date=|work=Tip search for: typedef struct _XDisplay Display|publisher=] 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 operating system, the Display structure contains the file handle of the socket 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 "output buffer". The term "output" in this case refers to the output from the client that is directed to the server: the output buffer can contain all kinds of requests to the server, not only those having a visible effect on the screen. The output 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 output 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 of 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:

# operations on the connection (XOpenDisplay, XCloseDisplay, ...);
# requests to the server, including requests for operations (XCreateWindow, XCreateGC,...) and requests for information (XGetWindowProperty, ...); and
# 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. */

#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 "); exit(1); }

s = DefaultScreen(d);

/* create window */ w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 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. Before entering this loop, the events the application is interested into 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 output buffer if there is no event in the queue.

Other libraries

Xlib does not provide support for buttons, menus, scrollbar, etc. Such widgets are provided by other libraries, which in turn use Xlib. There are two kinds of such libraries:

* libraries built atop of the Intrinsics 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 and Motif;
* libraries that provide widget sets using Xlib directly, without the Xt library, such as GTK+, Qt (X11 version), and FLTK (X11 version).

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 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.

References

See also

* X Window System
* X Window System protocols and architecture

External links

* [http://www.sbin.org/doc/Xlib Xlib Programming Manual]
* [http://tronche.com/gui/x/xlib/function-index.html Manual pages for all Xlib functions]
* [http://www.rahul.net/kenton/bib.html Kenton Lee's pages on X Window and Motif]
* [http://tronche.com/gui/x/xlib-tutorial/ A short tutorial on Xlib]
* [http://users.actcom.co.il/~choo/lupg/tutorials/xlib-programming/xlib-programming.html A longer tutorial on Xlib]
* [http://www.dis.uniroma1.it/%7eliberato/screensaver Using Xlib for creating a screensaver module]
* [http://www.init0.nl/simplex11tk.php Simple X11 toolkit for learning Xlib]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Xlib — est le nom d une bibliothèque logicielle, offrant une implémentation de la partie cliente du protocole X Window System en C. Elle contient des fonctions de bas niveau pour interagir avec un serveur X. Ces fonctions permettent aux programmeurs d… …   Wikipédia en Français

  • Xlib — y otras bibliotecas que la utilizan. Xlib son un conjunto de funciones y macros realizadas en C y utilizadas por un cliente como la interfaz con la versión 11 de X Window System. En resumen, es una interfaz de programación de bajo nivel para X.… …   Wikipedia Español

  • Xlib — (X library, рус. библиотека «икс»)  библиотека функций клиента системы X Window, написанная на языке Си. Содержит функции для взаимодействия с т. н. X сервером. Библиотека позволяет использовать более высокий уровень абстракции, без знания… …   Википедия

  • Xlib — und darauf aufbauende Bibliotheken Xlib ist eine Programmbibliothek für das Zeichnen grafischer Benutzeroberflächen über das X Window System auf unixoiden Systemen. Sie regelt als Client Bibliothek für das X Window Protokoll im X Window System… …   Deutsch Wikipedia

  • Xlib Compatibility Layer — XCB XCB Développeur Jamey Sharp, Josh Triplett, Bart Massey Dernière version …   Wikipédia en Français

  • X Window System core protocol — The X Window System logo The X Window System core protocol[1][2][3] is the base protocol of the X Windo …   Wikipedia

  • XCB — Développeur Jamey Sharp, Josh Triplett, Bart Massey Dernière version …   Wikipédia en Français

  • XCB — Entwickler Jamey Sharp, Josh Triplett, Bart Massey Aktuelle Version 1.7 (August 2010) Betriebssystem …   Deutsch Wikipedia

  • XCB — Desarrollador Jamey Sharp, Josh Triplett, Bart Massey xcb.freedesktop.org …   Wikipedia Español

  • XCB — Infobox Software name = XCB developer = Jamey Sharp, Josh Triplett, Bart Massey latest release version = 1.1.90.1 latest release date = July 17, 2008 [cite mailing list |url=http://lists.freedesktop.org/archives/xcb/2008 July/003622.html |title=… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”