Xlib是一种X Window System协议的客户端,以C语言撰写。其功能是与X server沟通。这样的功能可以让程序人员撰写程序时,毋须了解其协议的细节。但甚少应用程序会直接使用Xlib;通常是透过其他的函数库来调用Xlib用以提供部件工具箱(widget toolkits):
Xlib发表于1985年,目前使用在许多的Unix-like操作系统上。
目前XCB有可能取代Xlib.
资料类型
Xlib主要的资料类型是Display
[2]结构。
示例
下面是一个XLib的范列,产生一个视窗。
/*
Simple Xlib application drawing a box in a window.
gcc input.c -o output -lX11
*/
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main(void)
{
Display *d;
Window w;
XEvent e;
char *msg = "Hello, World!";
int s;
bool done = false;
/* open connection with the server */
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
/* create window */
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 640, 480, 0,
BlackPixel(d, s), WhitePixel(d, s));
/* register interest in the delete window message */
Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
XSetWMProtocols(d, w, &wmDeleteMessage, 1);
/* select kind of events we are interested in */
XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask);
/* map (show) the window */
XMapWindow(d, w);
/* event loop */
while (!done) {
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 */
switch(e.type){
case KeyPress:
XDestroyWindow(d, w);
break;
case DestroyNotify:
done = true;
break;
case ClientMessage:
if (e.xclient.data.l[0] == wmDeleteMessage){
done = true;
}
break;
}
}
/* close connection to server */
XCloseDisplay(d);
return 0;
}
注释
外部链接
Wikiwand in your browser!
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.