Skip to content
Search

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.

gcc -o ex01 ex01.c -I/usr/include/X11 -lX11

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

Tutorials

author Zer0 sitemap