Xlib

Last updated
Xlib
Developer(s) X.Org Foundation
Initial release~1985 [ citation needed ]
Stable release
1.8.8 [1]   OOjs UI icon edit-ltr-progressive.svg / 24 March 2024;1 day ago (24 March 2024)
Repository
Written in C
Type Library
Website www.x.org , documentation: www.x.org/releases/current/doc/libX11/libX11/libX11.html
X11-clients use xlib to communicate with the display server. X11 display server protocol.svg
X11-clients use xlib to communicate with the display server.

Xlib (also known as libX11) is an X Window System protocol client library written 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 X protocol.

Contents

Few applications use Xlib directly; rather, they employ other libraries that use Xlib functions to provide widget toolkits:

Xlib appeared around 1985,[ citation needed ] and is used in GUIs for many Unix-like operating systems. A re-implementation of Xlib was introduced in 2007 using XCB. [2]

Data types

The role of KMS (Kernel mode-setting), Linux example
Linux Graphics Stack 2013.svg
Illustrates the Linux graphics stack current as of 2013-08-24
Xlib and XCB in the X Window System graphics stack.svg
XCB and Xlib are client libraries which implement a display server communications protocol
Free and open-source-software display servers and UI toolkits.svg
The display server sits between the kernel (here: Linux kernel ) and its clients. It communicates with its clients over a given protocol.
SDL Layers.svg
Simple DirectMedia Layer can circumvent Xlib and write directly to framebuffer. An additional port to EGL is also available

The main types of data in Xlib are the Display [3] 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 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 is sent an Expose event when the window or 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

Simple Xlib application drawing a box and text in a window. Without window manager decorations. Xlib square example.png
Simple Xlib application drawing a box and text in a window. Without window manager decorations.
Simple Xlib application drawing a box and text in a window. With IceWM window manager decorations. Xlib square example with ICEWM decorations.png
Simple Xlib application drawing a box and text in a window. With IceWM window manager decorations.

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

/*    Simple Xlib application for creating a window and drawing a box in it.    gcc input.c -o output -lX11*/#include<X11/Xlib.h>#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(void){Display*display;Windowwindow;XEventevent;char*msg="Hello, World!";ints;// open connection to the serverdisplay=XOpenDisplay(NULL);if(display==NULL){fprintf(stderr,"Cannot open display\n");exit(1);}s=DefaultScreen(display);// create windowwindow=XCreateSimpleWindow(display,RootWindow(display,s),10,10,200,200,1,BlackPixel(display,s),WhitePixel(display,s));// select kind of events we are interested inXSelectInput(display,window,ExposureMask|KeyPressMask);// map (show) the windowXMapWindow(display,window);// event loopfor(;;){XNextEvent(display,&event);// draw or redraw the windowif(event.type==Expose){XFillRectangle(display,window,DefaultGC(display,s),20,20,10,10);XDrawString(display,window,DefaultGC(display,s),50,50,msg,strlen(msg));}// exit on key pressif(event.type==KeyPress)break;}// close connection to the serverXCloseDisplay(display);return0;}

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 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, scrollbars, etc. Such widgets are provided by other libraries, which in turn use Xlib. There are two kinds of such libraries:

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.

Related Research Articles

<span class="mw-page-title-main">X Window System</span> Windowing system for bitmap displays on UNIX-like systems

The X Window System is a windowing system for bitmap displays, common on Unix-like operating systems.

<span class="mw-page-title-main">NeWS</span> Discontinued windowing system developed by Sun Microsystems

NeWS is a discontinued windowing system developed by Sun Microsystems in the mid-1980s. Originally known as "SunDew", its primary authors were James Gosling and David S. H. Rosenthal. The NeWS interpreter was based on PostScript extending it to allow interaction and multiple "contexts" to support windows. Like PostScript, NeWS could be used as a complete programming language, but unlike PostScript, NeWS could be used to make complete interactive programs with mouse support and a GUI.

<span class="mw-page-title-main">GNUstep</span> Open source widget toolkit and application development tools

GNUstep is a free software implementation of the Cocoa Objective-C frameworks, widget toolkit, and application development tools for Unix-like operating systems and Microsoft Windows. It is part of the GNU Project.

<span class="mw-page-title-main">Windowing system</span> Software that manages separately different parts of display screens

In computing, a windowing system is a software suite that manages separately different parts of display screens. It is a type of graphical user interface (GUI) which implements the WIMP paradigm for a user interface.

X.Org Server is the free and open-source implementation of the X Window System (X11) display server stewarded by the X.Org Foundation.

Tuxedo is a middleware platform used to manage distributed transaction processing in distributed computing environments. Tuxedo is a transaction processing system or transaction-oriented middleware, or enterprise application server for a variety of systems and programming languages. Developed by AT&T in the 1980s, it became a software product of Oracle Corporation in 2008 when they acquired BEA Systems. Tuxedo is now part of the Oracle Fusion Middleware.

<span class="mw-page-title-main">Direct Rendering Infrastructure</span> Framework

The Direct Rendering Infrastructure (DRI) is the framework comprising the modern Linux graphics stack which allows unprivileged user-space programs to issue commands to graphics hardware without conflicting with other programs. The main use of DRI is to provide hardware acceleration for the Mesa implementation of OpenGL. DRI has also been adapted to provide OpenGL acceleration on a framebuffer console without a display server running.

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.

<span class="mw-page-title-main">X Toolkit Intrinsics</span>

X Toolkit Intrinsics is a library that implements an API to facilitate the development of programs with a graphical user interface (GUI) for the X Window System. It can be used in the C language. Design took place late 1980s to early 1990s.

<span class="mw-page-title-main">GDK</span> Software library

GDK is a library that acts as a wrapper around the low-level functions provided by the underlying windowing and graphics systems. GDK lies between the display server and the GTK library, handling basic rendering such as drawing primitives, raster graphics (bitmaps), cursors, fonts, as well as window events and drag-and-drop functionality.

In the X Window System, the program xwd captures the content of a screen or of a window and optionally saves it into a file.

<span class="mw-page-title-main">X Window selection</span>

Selections, cut buffers, and drag-and-drop are the mechanisms used in the X Window System to allow a user to transfer data from one window to another. Selections and cut buffer are typically used when a user selects text or some other data in a window and pastes in another one. Drag-and-drop is used when a user selects something in a window, then clicks on the selection and drags it into another window.

<span class="mw-page-title-main">XCB</span> X protocol C language binding library

XCB is a library implementing the client-side of the X11 display server protocol. XCB is written in the C programming language and distributed under the MIT License. The project was started in 2001 by Bart Massey and aims to replace Xlib.

In the X Window System, the X resources are parameters of computer programs such as the name of the font used in the buttons, the background color of menus, etc. They are used in conjunction with or as an alternative to command line parameters and configuration files.

The X Window System core protocol is the base protocol of the X Window System, which is a networked windowing system for bitmap displays used to build graphical user interfaces on Unix, Unix-like, and other operating systems. The X Window System is based on a client–server model: a single server controls the input/output hardware, such as the screen, the keyboard, and the mouse; all application programs act as clients, interacting with the user and with the other clients via the server. This interaction is regulated by the X Window System core protocol. Other protocols related to the X Window System exist, both built at the top of the X Window System core protocol or as separate protocols.

In computer science, the event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by making a request to some internal or external "event provider", then calls the relevant event handler. The event loop is also sometimes referred to as the message dispatcher, message loop, message pump, or run loop.

In computing, Nano-X is a windowing system which is full featured enough to be used on a PC, an embedded system or a PDA. It is an Open Source project aimed at bringing the features of modern graphical windowing environments to smaller devices and platforms. The project was renamed from Microwindows due to legal threats from Microsoft regarding the Windows trademark.

awesome (window manager) Window manager for X Window System

awesome is a dynamic window manager for the X Window System developed in the C and Lua programming languages. Lua is also used for configuring and extending the window manager. Its development began as a fork of dwm, though has differed considerably since. It aims to be extremely small and fast, yet extensively customizable. It makes it possible for the user to manage windows with the use of keyboard.

<span class="mw-page-title-main">Yesod (web framework)</span>

Yesod is a web framework based on the programming language Haskell for productive development of type-safe, representational state transfer (REST) model based, high performance web applications, developed by Michael Snoyman, et al. It is free and open-source software released under an MIT License.

References

  1. Alan Coopersmith. "[ANNOUNCE] libX11 1.8.8" . Retrieved 25 March 2024.
  2. "XDC2007 Notes". February 9, 2007.
  3. "Display Structure on freedesktop CVS". Tip search for: typedef struct _XDisplay Display.