xorg-server的c语言入门

xorg官网Xlib - C Language X Interface

初级教程Xlib programming: a short tutorial

linux的桌面图形架构图,使用client与server模式,server使用x11(X Window System protocol)协议,client通过x11协议告诉server怎么显示,显示什么,就像http协议,xlib是封装协议的c库,使用xlib可以方便与xserver建立连接,显示图像,不用关注协议具体的字节格式等等

xorg-server

xlib是对x11显示服务协议的封装

x11定义了客户端到服务器端的显示模式

下面的小例子,演示怎么使用xlib库

1
2
3
测试库文件,有桌面环境的应该都有
sudo find / -name Xlib.h
sudo find / -name libX11.so

prog-2.cc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Written by Ch. Tronche (http://tronche.lri.fr:8000/)
// Copyright by the author. This is unmaintained, no-warranty free software.
// Please use freely. It is appreciated (but by no means mandatory) to
// acknowledge the author's contribution. Thank you.
// Started on Thu Jun 26 23:29:03 1997

//
// Xlib tutorial: 2nd program
// Make a window appear on the screen and draw a line inside.
// If you don't understand this program, go to
// http://tronche.lri.fr:8000/gui/x/xlib-tutorial/2nd-program-anatomy.html
//

#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h> // I include this to test return values the lazy way
#include <unistd.h> // So we got the profile for 10 seconds

#define NIL (0) // A name for the void pointer

main()
{
// Open the display

Display *dpy = XOpenDisplay(NIL);
assert(dpy);

// Get some colors

int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

// Create the window

Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, blackColor, blackColor);

// We want to get MapNotify events

XSelectInput(dpy, w, StructureNotifyMask);

// "Map" the window (that is, make it appear on the screen)

XMapWindow(dpy, w);

// Create a "Graphics Context"

GC gc = XCreateGC(dpy, w, 0, NIL);

// Tell the GC we draw using the white color

XSetForeground(dpy, gc, whiteColor);

// Wait for the MapNotify event

for(;;) {
XEvent e;
XNextEvent(dpy, &e);
if (e.type == MapNotify)
break;
}

// Draw the line

XDrawLine(dpy, w, gc, 10, 60, 180, 20);

// Send the "DrawLine" request to the server

XFlush(dpy);

// Wait for 10 seconds

sleep(10);
}
1
2
gcc -L /usr/lib/ -lX11 -o xorg prog-2.cc
./xorg

桌面出现一个小黑窗口,上面一条白线,10s后关闭,只关闭窗口程序没结束

但是不能直接在启动桌面前的黑屏终端运行,因为没有启动 x server


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 hui740024906@163.com

文章标题:xorg-server的c语言入门

文章字数:517

本文作者:qianggetaba

发布时间:2019-08-09, 11:29:40

最后更新:2019-09-09, 10:01:58

原始链接:https://qianggetaba.com/2019/08/09/xorg-server-newbie/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏