Xlib
lastmod 2021-01-03created 2017-02-0612 edits
Xlib 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 help programmers write programs without knowing the details of the communication protocol. Applications that use Xlib directly are relatively rare; they usually rely on other libraries that use Xlib functions to provide widget toolkits:
- Intrinsics (Xt)
- Athena widget set (Xaw)
- Motif
- FLTK
- GTK+
- Qt (X11 version)
- Tk
Xlib appeared around 1985 and is currently used in the GUIs of many Unix-like operating systems. The XCB library was created with the aim of replacing Xlib. Xlib is still widely used, but what Xlib implements today can instead use XCB as a low-level transport layer.
Categories
Functions
Simple example
#include <X11/Xlib.h>
int main()
{
Display * d;
d = XOpenDisplay("localhost:0.0");
XCloseDisplay(d);
return 0;
}
Then compile it as follows.
Let's create a simple window
Create a window and display it.
#include <X11/Xlib.h>
int main()
{
Display *d;
Window w, root;
d = XOpenDisplay(NULL);
root = XDefaultRootWindow(d);
w = XCreateSimpleWindow(d, root, 50, 50, 400, 300, 2, BlackPixel(d,0), WhitePixel(d,0));
XMapWindow(d, w);
XFlush(d);
getchar();
XCloseDisplay(d);
return 0;
}
This example introduces a new data type, Window, and new functions such as XDefaultRootWindow, XCreateSimpleWindow, XMapWindow, and XFlush. Note that BlackPixel() and WhitePixel() are macros.
Favorite site
- Wikipedia (en) Xlib
- Stackoverflow - How to load bmp file using x11 window background
- Stackoverflow - how to get a screen pixel's color in x11
Tutorials
- The Xlib Manual
- A Brief intro to X11 Programming
- Basic Graphics Programming With The Xlib Library - Part II
- [Recommended] KLDP - X Window Programming Basics Course