Understanding X From Source Code
THOSS X Group
blc03@mails.tsinghua.edu.cn
December 21, 2006
Contents
1 ାṌ
1.1 X㈧㐋ദ᱘Ắᔡ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 X㈧㐋䒛Тѿ㈧㏿Ჰ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 ۲ำ⎼ⴭ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4 ⎼ⴭⰚᒁ㏿Ჰ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 XServerκᘶྑཐ
2.1 ڗㆧ䉰⎼ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 ڢЂᢚ㏿Ჰ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 䉰⎼ネ⤲ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4 䉰⎼⮰㏰㏳ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 DIX
3.1 mainܩ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1.1 ݉ࡂ͙⮰रᅮϐξ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 DispatchηТᓖ⣛ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
InitExtensions
4 OS
4.1 䄯Ꮢস䄣ⅮϐЄ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2 ᝣ䔊ᣑ⮰ᐦ⿷ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3 BlockHandler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5 DDX
5.1 ᢚ㏿Ჰ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2 䒿ڑ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3 䒿ܦ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ephyr
6 ӱβ
6.1 Wrappers সdevPrivates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.1.1
devPrivates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.1.2 Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.2 ҈䭋݃WorkQueue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3
4
4
4
5
6
8
9
11
12
12
14
14
15
18
18
21
24
25
25
25
28
32
36
36
36
36
36
1
զṌ
䔅ン᪳ᶏᄲϺX⮰⎼ⴭܦࣽ⌝ڑ⮰㔯ᄋXߍஔरദ᱘Ắᔡࣶڢ⣜ᐻȠϺ͙喏Иᄲⰷݜ
̬͖ᵸ䲎㈧㐋⮰ദᵲ᳢᭛ຮҁ䃪䃍স⣜喏⌝ڑ⮰ܲᄲҫИ႒ݜ⣹䉡⮰㈧㐋䃪䃍⮰र
∁ȟ㑂⼷⮰रឬጓȟᵴं㜹⮰र࣋݅Ƞ
⩝κरẮᔡ⮰ϐࣵ喏ߌ̶᪳ᶏ⮰̹⛋喏䔅ン᪳ᶏᰬҟ⮰䬱䄧ᐻຮ̷喝
Figure 1: 䬱䄧∁
ऺ㐙⮰「㞮͙喏И̬「⮰ᐬܦ䔅̬「ᄲ㺭㼏۟⮰䬚䷄喏♢ऺڹ㏿ऴ⎼ⴭ⮰ܲস䬼
䛶͙ఊむ䔅χ䬚䷄Ƞ᪠ン᪳ᶏ⮰Ⱊᴳ࠱᠘喝
• ⌝ڑ⤲㼏XẮᔡ
• ᢸᤍXߍஔ⮰Џⴭ
2
• ႒Ό㈧㐋䃪䃍∁, ⛋ᖵ፤⩔⮰㑂⼷ឬጓ,႒ц⣜ᵴ⮰ं㜹࣋݅
1 ାṌ
じ̬「͙喏ИᄲⰷⰷX⮰ദ᱘పᮛ喝X⮰ദ᱘Ắᔡসദ᱘㏿ᲰȠ㔯㭽ݜ䔅᭛̬ンڟκX⎼ⴭ⮰
᪳ᶏ喏ఌₐ喏䔅䛸̹ц㐅ܦ๖ๆ⮰㏲㞮喏Ꭲ̀Иц㏿ऴЏⴭᲑ㔯ᄋ䔅̬ദ᱘పᮛȠ
1.1 Xᠿᤱࡏ༊ဉய
X᭛ู̬͖ᱮ⮰㈧㐋喏Ѳ᭛Ⴏᝬദκ⮰ദ᱘Ắᔡࢠガࢁᬿᛮ:
• DisplaysসScreens: ᭪㈧㐋সᅻᎁ
• Server-Clinet Model: ߍஔ-ᝣ〛Ὅಷ
• Window Management: ⿃एネ⤲
• Events: ηТ
• X Extensions: Xផᆁ
ສᚵᠿᤱڝਛૡ
X㈧㐋͙喏᭪㈧㐋㷗͵ͦ⩝̬͖䩚Ⰴȟ̬͖呌ᴳȟ̬͖ᝂๆ͖ᅻᎁ㏰⮰҈々Ƞ
ޜ-হᝩ၇ࠤ
X᭛䲎ऽ㑽㐈⮰⿃ए㈧㐋喏̬͖Ꮐ⩔⼷Ꮋ̹䰬㺭䭱Ⴘ᭪⮰҈々̶䓼㵸喏ႯछБ҈ͦ
ᝣ〛䓼㵸䔈⼷ᱦஔ̶ȠXߍஔ䓼㵸κ᱘, ᣓݢ⼷Ꮋ⮰᭪Ꭲ㣣ᓃ⩔ᝣ⮰䒿ڑȠ͐㔱䬠⮰ϐξ
䕆䓳ⰤᏀ⮰ࡻ䃚㑽㐈̶ь䒿Ƞᝣ〛䓼㵸κ䔈⼷喏ߍஔ䓼㵸κ᱘喏䔅᭛̺ᮚ䕆⮰ߍஔ-
ᝣ〛Ὅಷ⮰䛹㺭ࡦݗȠ
ᣓݢ᭪⮰⼷Ꮋ㷗⼜̬͖ͦߍஔ(server), Ⴏ䰬㺭Ⴘ⮰҈࠱᠘:
1. ᣓݢ᭪㈧㐋喏٭䃤ๆ͖ᝣ⼷Ꮋ⮰䃫䬚Ƞ
2. ា⩔ᝣ䒿ڑь䔾㐅ⰤᏀ⮰ᝣȠ
3. 㼏䛶Ꭲន㵸Ბ㜖ᝣ⮰䄣ⅮȠ
4. 㐠័ูᱮ⮰ᢚ㏿ᲰȠ
ᝂسាᑩ
X㈧㐋͙喏̬͖Ꮐ⩔⼷ᎻᎢ̹䭱ᣓݢ⿃एਖ䛸᭪ȟ๓ᄻ᭛ๆᄽふふηᗱȠ⩝κๆ͖
ᝣ⮰ႄ喏̬͖ᝣ̹㘩Ӊ䊂κ➥⮰⿃ए䙹㒚喏Ⴏख㘩ॶ䃵Ⴏᝬጸ᱇⮰᭪⮰ѹ㒚স
๓ᄻȠᅻᎁ⮰ጯᅬБࣶ⩔ᝣ⮰ϐξᐻ⩝̬͖ࢁ⠘⮰⼷Ꮋᣓݢ喏⼜ͷͦ⿃एネ⤲ஔ(window
manager)Ƞ
⿃एネ⤲ஔ䭱̶᭛ҫ⩔βXlib㑂ۅ⮰̬͖Ꮐ⩔⼷Ꮋ喏̹䓳Ⴏ㷗䉷δβ➥₶⮰ᱯ䭼喏Бӫネ⤲
ᅻᎁ̶⿃एጯᅬȠ
3
ͽυ
Xਞ
X㈧㐋᭛छផᆁ⮰喏Ⴏ͵β̬ផᆁᱦݢȠᎢ̹᭛͖ߍஔ䘩ᩛᠭᝬᰵ⮰ផᆁ喏ᝬБᏀ⩔⼷
Ꮋҫ⩔̬͖ផᆁ⮰ᬢՅ喏ᓱ䶧仂ٴ䄎䬚ߍஔ᭛॒Ӈ䄑ផᆁȠ
1.2 Xᠿᤱℒυϵᠿᤥ༶
1.3 Ԝࢵሾᘶ
छБݜXorg々◥࣮㔯⎼ⴭ⮰̷䒩স㑂䃽∁:
⁎䓺ា㑂䃽͙䕳ݜ⮰䬚䷄ᩪݜ❴̶1䃔䃦ȠᎢᄲ䃔䃦⮰㏿ߌڑݜ䔅ン᪳ᶏ͙ᲑȠ
http://wiki.x.org/wiki/ModularDevelopersGuide
ᣔ㡼ҫ⩔kscope䬱䄧⎼ⴭ喏ႯӉ䊂κcscope, dot, ctags̵͖⼷ᎻȠ
1.4 ሾᘶᗇଡ଼ᤥ༶
app 䔅͖Ⱊᒁ̷࠱ॗ̬χXᏀ⩔⼷Ꮋ喏࠱᠘twm䔅͖ᰬガࢁ⮰⿃एネ⤲ஔȠ
doc 䔅͖Ⱊᒁ̷࠱ॗ⮰᪳ᶏ࠱᠘Xࡻ䃚㻰㠯喏X䘔ܲὍಃ(℀ຮႵڔὍಃ)ふ⮰䃪䃍᪳ᶏ喏Ѳ᭛㑦
ͻᄥXserver᪠ѿ䃪䃍⮰䔜Ƞऒโ喏Xlib⮰࣮㔯ڸछБxorg-docs/hardcopy/X11 ̷
ឪݜȠ
xserver 䔅͖Ⱊᒁ̷࠱ॗxserver⮰⎼ЏⴭȠ
lib ࠱ॗXߍஔ䰬㺭ҫ⩔⮰Ꮏ喏ᰵχᎿ䭱̶ख㘩㷗XߍஔЏⴭҫ⩔喏ᄲႯИ⠘⿷ܦᲑ᭛ͦ
βᰠຩ⮰ὍಃࡂȠ
1http://www.newsmth.org/bbsdoc.php?board=THOSS
4
2 XServerκᘶྑཐ
xserverⰚᒁ̷࠱ॗๆ͖Xߍஔ⮰⎼ⴭ喏࠱᠘Xorg, Xnestসദκkdrive⮰ๆ͖ߍஔȠ
Xߍஔ⮰Џⴭܲͦఇ͖䘔ܲ㏰㏳喝
• 䃪ำᬌڟᅮ(DIX), 䔅̬䘔ܲ⮰Џⴭ㷗ᝬᰵXserver⮰⣜ڝϗȠ
• ᧹҈㈧㐋ᅮ(OS), 䔅̬䘔ܲ⮰Џⴭ䮻ⱬ᧹҈㈧㐋⮰̹स㔸̹स喏Ѳ᭛㷗䔅͖᧹҈㈧㐋̶⮰ప
ᒎ䃪ำڝϗȠ
• 䃪ำⰤڟᅮ(DDX), 䔅̬䘔ܲ䮻ⱬ᧹҈㈧㐋সపᒎ䃪ำ⮰㏰ऴ⮰̹स㔸̹सȠ
• ផᆁᣑए喏䔅̬䘔ܲͦ⩔㐋̬⮰ᐻऽX serverߌڑ⮰ߋ㘩ӇᩛᠭȠ
DIXᅮᰵຮ̷⮰ڗㆧ䉰⎼:
• Window
• Pixmap
• Screen
• Device
• Colormap
• Font
• Cursor
• Graphics Contexts
䉰⎼⩔ⰤᏀ⮰struct㶔喏ڢ͙࠱᠘̵͖䘔ܲ喝
• ᆊᕓ⃡喏⩔κԉႄⰤᏀ⮰ԍᖛȠ
• ܩᠳ䦴
• ⻭ᰵႃ⃡喏DDXЏⴭ⩔ᲑԉႄѨᰵᢚȠ
䔅χ㏿Ჰ͵ᰵᄥᏀह⼜⮰͖͐.h᪳Тڱ喏℀ຮscreenᄥᏀκscreenint.hসscrnintstr.hȠ
screenint.h͵โ䘔ҫ⩔ᬢ䰬㺭⮰㏿Ჰসܩ࣋ಷ喏Ѳ̹ᯠ䱞ڱ䘔⮰⣜; scrnintstr.h ͙
͵ڱ䘔⣜⩔⮰ᢚ㏿ᲰȠ
DIX䕆䓳䄯⩔㏿Ჰ⮰ܩᠳ䦴ᲑႸႯ⮰҈Ƞ䔅χᠳ䦴⩝DDXⰠᣑᝂ䬠ᣑ⮰䃪Ƞᆊᕓ̬⃡㝘
⩝DIXᅮ䃪喏DDX̹㘩ԚᩥႯИȠ
DIXЏⴭᩪ㒚dix/Ⱊᒁ̷喏ԉ᠘
main.c window.c colormap.c, dispatch.c, events.c ...
͖XẮᔡ喏ദ᱘̶䘩ᰵ̬͖ᄥᏀ⮰⣜᪳ТȠ
ߍஔⰤڟ䘔ܲ⮰Џⴭᩪ㒚hw/Ⱊᒁ̷Ƞڢ͙喏hw/xfree86/Ⱊᒁ̷࠱ॗβXorg⮰Џⴭ; hw/kdrive̷
࠱᠘βദκkdrive⮰ๆ͖ߍஔȠ⩝κദκkdrive⮰ߍஔ℀䒯⮰ガࢁ喏ИᄲϺڢ͙⮰̬
͖XephyrڑȠڟκ䔅͖ߍஔ⮰䄠ᬺ喏छБ࣮㻭hw/kdrive/ephyr/README, 䔅䛸ᑁ⩔̬䘔
ܲ:
Xephyr is a a kdrive server that outputs to a window on a pre-existing ’host’ X display.
Think Xnest but with support for modern extensions like composite, damage and randr.
Unlike Xnest which is an X proxy, i.e. limited to the capabilities of the host X server,
Xephyr is a real X server which uses the host X server window as "framebuffer" via
fast SHM XImages.
5
It also has support for ’visually’ debugging what the server is painting.
kdrive⮰Ϸ㏹छБ࣮㻭ⰤᏀ⮰man᪳Т,hw/kdrive/Xkdrive.man:
man ./Xdrive.man
2.1 Ө᠋‚ሾ
̷䲎И䕆䓳ᆁⰤᏀ⮰ᢚ㏿ᲰᲑ᥊⌱Ẇڗ䉰⎼᭛ຮҁX㈧㐋͙㐠័Ƞ
Screen
Xᩛᠭ̬͖᭪㈧㐋࠱ॗๆ͖ᅻᎁȠะ⤲ᅻᎁᝬ䰬㺭⮰ᢚԍᖛ㷗࠱ॗScreenRec ͙(xserver/-
include/scrnintstr.h):
typedef struct _Screen {
int
ATOM
short
short
short
unsigned char
DepthPtr
unsigned long
unsigned long
...
myNum; /* index of this instance in Screens[] */
id;
width, height;
mmWidth, mmHeight;
numDepths;
rootDepth;
allowedDepths;
rootVisual;
defColormap;
/* anybody can get a piece of this array */
DevUnion
...
*devPrivates;
CloseScreen;
/* Random screen procedures */
CloseScreenProcPtr
...
/* Window Procedures */
/* Pixmap procedures */
/* Font procedures */
/* Cursor Procedures */
...
} ScreenRec;
ຮݹ䲎ᝬϷ㏹⮰喏ScreenᄥᏀ⮰㏿Ჰ࠱ॗ̵͖䘔ܲ喝
• ᆊᕓ⃡: ԉႄԍᖛȠ
• ܩᠳ䦴: ᠳऽႸⰤᏀ⮰᧹҈ᝬ⩔⮰ҷ⼷喏⩝DDXЏⴭ䃪Ƞ
• ⻭ᰵႃ⃡喏DDXЏⴭ⩔Ბԉႄ⻭ᰵᢚȠ
ڔᅬऄ䛻screenInfoԉႄβᠳऽᝬᰵScreenRec⮰ᠳ䦴㏰:
typedef struct _ScreenInfo {
int
int
int
int
imageByteOrder;
bitmapScanlineUnit;
bitmapScanlinePad;
bitmapBitOrder;
6
int
PixmapFormatRec
numPixmapFormats;
int
int
ScreenPtr
int
} ScreenInfo;
formats[MAXFORMATS];
arraySize;
numScreens;
screens[MAXSCREENS];
numVideoScreens;
extern ScreenInfo screenInfo;
सᬢ喏screenInfoԉႄβ䄤ຮbitmap⮰bit-orderふ⮰ڔᅬ䙹㒚Ƞ
Device
Window,Pixmap,Drawable
WindowসPixmap䘩᭛Drawable⮰ၼㆧ喏drawableछБ㻲̬͖ͦछБڢ㶔䲎⩧ప⮰䲎喏Ⴏᝂ
㔱᭛̬͖ᅻᎁ̶⮰window, ᝂ㔱᭛̬͖ڱႄ͙pixmapȠ
drawableᄥᏀ⮰ᢚ㏿Ჰຮ̷(include/pixmapstr.h):
typedef struct _Drawable {
unsigned char
unsigned char
unsigned char
unsigned char
XID
short
short
unsigned short
unsigned short
ScreenPtr
unsigned long
} DrawableRec;
/* DRAWABLE_ */
type;
class; /* specific to type */
depth;
bitsPerPixel;
id;
x;
y;
width;
height;
pScreen;
serialNumber;
/* resource id */
/* window: screen absolute, pixmap: 0 */
/* window: screen absolute, pixmap: 0 */
typeႃ⃡⮰ंըͦDRAWABLE_PIXMAP, DRAWABLE_WINDOW, UNDRAWABLE_WINDOW; UNDRAWABLE_WINDOW⩔
κᴳ䃲InputOnlyㆧಷ⮰windowȠserialNumber ႃ⃡⮰ंըᝬᰵ⮰drawable͙ਜ਼̬喏Ⴏ⩔κ
䃐GC͙㷭ޖԍᖛ⮰ᰵᩴᕓȠScreen͙ⰤᏀ⮰ܩᠳ䦴ᠳऽβ⩔κ᧹҈drawable⮰ҷ⼷ȠpScreenႃ
⃡ᠳऽ䔅͖drawableڟ㖀⮰screenȠ
pixmap㏿Ჰ͵ຮ̷喝
typedef struct _Pixmap {
DrawableRec
int
int
DevUnion
#ifdef COMPOSITE
short
short
#endif
} PixmapRec;
drawable;
refcnt;
devKind;
devPrivate;
screen_x;
screen_y;
䕆䓳drawableႃ⃡喏pixmapβdrawable⮰ၼㆧ喏䔅᭛C͙⣜ㆧ㐓ឫ⮰፤⩔∁Ƞ
7
Colormap
Graphics Contexts
Font
Cursor
2.2 ӱβ๎൝ᤥ༶
Client
̬͖䔊ᣑߍஔ⮰ᝣ⼷ᎻᄥᏀκ̬͖Clientᢚ㏿Ჰ喏Ⴏ͵κinclude/dixstruct.h:
typedef struct _Client {
index;
clientAsMask;
requestBuffer;
osPrivate;
swapped;
int
Mask
pointer
pointer
Bool
ReplySwapPtr pSwapReplyFunc;
XID
int
int
int
int
errorValue;
sequence;
closeDownMode;
clientGone;
noClientException;
/* for OS layer, including scheduler */
/* this client died or needs to be
* killed */
lastDrawableID;
lastGC;
lastGCID;
DrawablePtr lastDrawable;
Drawable
GCPtr
GContext
SaveSetElt
int
pointer
int
*saveSet;
numSaved;
screenPrivate[MAXSCREENS];
(**requestVector) (
ClientPtr /* pClient */);
req_len;
CARD32
Bool
int
ClientState clientState;
DevUnion
*devPrivates;
big_requests;
priority;
/* length of current request */
/* supports large requests */
unsigned long replyBytesRemaining;
struct _FontResolution * (*fontResFunc) (
/* no need for font.h */
ClientPtr
int *
/* pClient */,
/* num */);
} ClientRec;
requestBuffer⩔κᩪ㒚Ბ㜖ᝣ⮰䄣ⅮȠosPrivate ⩔κԉႄosᅮ䰬㺭ҫ⩔⮰⻭ᰵᢚ喏䔅
OSᅮ̬「ᰵᝬ⊵ࣶȠ
8