Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Xlib

From Wikipedia, the free encyclopedia
Client library for the X Window System
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Xlib" – news ·newspapers ·books ·scholar ·JSTOR
(September 2018) (Learn how and when to remove this message)
Xlib
DeveloperX.Org Foundation
Initial releaseSeptember 1985[1]
Stable release
1.8.13[2] Edit this on Wikidata / 7 February 2026; 11 days ago (7 February 2026)
Written inC
TypeLibrary
Websitewww.x.org, documentation:www.x.org/releases/current/doc/libX11/libX11/libX11.html
Repository
X11-clients use xlib to communicate with thedisplay server.

Xlib (also known aslibX11) is anX Window System protocol clientlibrary written in theC programming language. It containsfunctions for interacting with an Xserver. These functions allowprogrammers to write programs without knowing the details of theX protocol.

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

Xlib, which was first publicly released in September 1985,[1] is used inGUIs for manyUnix-likeoperating systems. A re-implementation of Xlib was introduced in 2007 usingXCB.[3]

Data types

[edit]
The role of KMS (Kernel mode-setting), Linux example
The Linux graphic stack
Illustrates theLinux graphics stack current as of 2013-08-24
Scheme: Humane-machine_interaction
XCB and Xlib are client libraries which implement adisplay servercommunications protocol
The place of certain Linux kernel modules
The display server sits between thekernel (here:Linux kernel) and its clients. It communicates with its clients over a given protocol.
The place of certain Linux kernel modules
Simple DirectMedia Layer can circumvent Xlib and write directly toframebuffer. An additional port toEGL is also available

The main types of data in Xlib are theDisplay[4] structure and the types of the identifiers.

Informally, a display is a physical or virtual device where graphical operations are done. TheDisplay 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 aUnix-like operating system, theDisplay structure contains the file handle of thesocket of this channel (this can be retrieved using theConnectionNumber macro.) Most Xlib functions have aDisplay 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 theiridentifiers. 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 typesWindows,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 differentDisplay structures; however, when they request operations on the same identifier, these operations will be done on the same object.

Protocol and events

[edit]

The Xlib functions that send requests to the server usually do not send these requests immediately but store them in a buffer, called therequest buffer. The termrequest 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 functionsXSync orXFlush, 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 anExpose 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

[edit]

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

[edit]
Simple Xlib application drawing a box and text in a window. Withoutwindow manager decorations.
Simple Xlib application drawing a box and text in a window. WithIceWMwindow 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 callingXOpenDisplay. It then requests the creation of a window withXCreateSimpleWindow. A separate call toXMapWindow is necessary for mapping the window, that is, for making it visible on the screen.

The square is drawn by callingXFillRectangle. 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 anExpose event.

The drawing of the window content is therefore made inside theloop handling the events. Before entering this loop, the events the application is interested in are selected, in this case withXSelectInput. 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 functionXNextEvent blocks and flushes the request buffer if there is no event in the queue.

Other libraries

[edit]

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

  • libraries built atop of theX Toolkit Intrinsics library (Xt), which provides support for widgets but does not provide any particular widget; specific widgets are provided bywidget set libraries that use Xt, such asXaw andMotif;
  • libraries that provide widget sets using Xlib directly, without the Xt library, such as the X versions ofGTK,Qt,FLTK andfpGUI.

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 handleExpose events and redraw the window content.

TheXCB 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

[edit]
  1. ^abScheifler, Robert W.; Gettys, James; Newman, Ron (1988).X Window System(PDF). Digital Press. p. xxvi.
  2. ^Alan Coopersmith (7 February 2026)."[ANNOUNCE] libX11 1.8.13". Retrieved8 February 2026.
  3. ^"XDC2007 Notes". February 9, 2007.
  4. ^"Display Structure on freedesktop CVS".Tip search for: typedef struct _XDisplay Display. Archived fromthe original on 2008-01-31. Retrieved2007-08-24.

External links

[edit]
The WikibookX Window Programming has a page on the topic of:XLib
Architecture
Extensions
Components
and notable
implementations
Display servers
Client libraries
Display managers
Session managers
Window managers
(comparison)
Compositing
Stacking
Tiling
Standards
Applications
Low-level platform-specific
OnAmigaOS
OnClassic Mac OS,macOS
OnWindows
OnUnix
OnBeOS,Haiku
OnAndroid
CLI
Low Level Cross-platform
CLI
C
Java
High-level, platform-specific
OnAmigaOS
OnClassic Mac OS,macOS
Object Pascal
Objective-C,Swift
C++
CLI
OnWindows
CLI
C++
Object Pascal
OnUnix andX11
High-level, cross-platform
C
C++
Objective-C
CLI
Adobe Flash
Go
Haskell
Java
JavaScript
Common Lisp
Lua
Pascal
Object Pascal
Perl
PHP
Python
Ruby
Tcl
XML
shell
Dart
Retrieved from "https://en.wikipedia.org/w/index.php?title=Xlib&oldid=1292781241"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp