X Window研究笔记(9)
转载时请注明出处和作者联系方式
作者联系方式:李先静 <xianjimli at hotmail dot com>
插件式(Plug-in/Add-On)架构如今已经变得非常流行了,Eclipse因把这种技术发挥到极致,而倍受赞誉。这种技术被广泛采用,当然是有它的道理的,可扩展性就是其中好外之一。实际上,这并不是什么新概念,早在20多年前,X Window就采用了类似的设计方法,只是可能很长时间以来,X Window都没有采用目前常用的动态加载机制,结果很多人都以为它是一个巨大的泥团,庞大而混乱,很少有人深入去研究。当然,这只是一种误解,从逻辑上讲,它完全是基于插件式设计。在接下来的几篇文章中,我们介绍一下X Window的扩展机制:
9.X Window扩展机制--核心服务的扩展
X Window的核心服务是放在一个函数表中的,当客户端程序有请求到来时,X Window通过请求号从函数表中找到对应的服务函数,然后调用它,服务函数执行完成后返回结果。函数表其实也是一种抽象,它让框架不必知道实现的功能,扩展功能时,只要修改这个函数表,而不用修改框架。
这个函数表在programs/Xserver/dix/table.c中,其声明如下:
int (* ProcVector[256]) (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
ClientPtr /**//* client */
) =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
ProcBadRequest,
ProcCreateWindow,
ProcChangeWindowAttributes,
ProcGetWindowAttributes,
ProcDestroyWindow,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcDestroySubwindows, /**//* 5 */
ProcChangeSaveSet,
ProcReparentWindow,
ProcMapWindow,
ProcMapSubwindows,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcUnmapWindow, /**//* 10 */
ProcUnmapSubwindows,
ProcConfigureWindow,
ProcCirculateWindow,
ProcGetGeometry,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcQueryTree, /**//* 15 */
ProcInternAtom,
ProcGetAtomName,
ProcChangeProperty,
ProcDeleteProperty,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcGetProperty, /**//* 20 */
ProcListProperties,
ProcSetSelectionOwner,
ProcGetSelectionOwner,
ProcConvertSelection,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcSendEvent, /**//* 25 */
ProcGrabPointer,
ProcUngrabPointer,
ProcGrabButton,
ProcUngrabButton,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcChangeActivePointerGrab, /**//* 30 */
ProcGrabKeyboard,
ProcUngrabKeyboard,
ProcGrabKey,
ProcUngrabKey,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcAllowEvents, /**//* 35 */
ProcGrabServer,
ProcUngrabServer,
ProcQueryPointer,
ProcGetMotionEvents,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcTranslateCoords, /**//* 40 */
ProcWarpPointer,
ProcSetInputFocus,
ProcGetInputFocus,
ProcQueryKeymap,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcOpenFont, /**//* 45 */
ProcCloseFont,
ProcQueryFont,
ProcQueryTextExtents,
ProcListFonts,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcListFontsWithInfo, /**//* 50 */
ProcSetFontPath,
ProcGetFontPath,
ProcCreatePixmap,
ProcFreePixmap,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcCreateGC, /**//* 55 */
ProcChangeGC,
ProcCopyGC,
ProcSetDashes,
ProcSetClipRectangles,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcFreeGC, /**//* 60 */
ProcClearToBackground,
ProcCopyArea,
ProcCopyPlane,
ProcPolyPoint,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcPolyLine, /**//* 65 */
ProcPolySegment,
ProcPolyRectangle,
ProcPolyArc,
ProcFillPoly,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcPolyFillRectangle, /**//* 70 */
ProcPolyFillArc,
ProcPutImage,
ProcGetImage,
ProcPolyText,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcPolyText, /**//* 75 */
ProcImageText8,
ProcImageText16,
ProcCreateColormap,
ProcFreeColormap,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcCopyColormapAndFree, /**//* 80 */
ProcInstallColormap,
ProcUninstallColormap,
ProcListInstalledColormaps,
ProcAllocColor,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcAllocNamedColor, /**//* 85 */
ProcAllocColorCells,
ProcAllocColorPlanes,
ProcFreeColors,
ProcStoreColors,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcStoreNamedColor, /**//* 90 */
ProcQueryColors,
ProcLookupColor,
ProcCreateCursor,
ProcCreateGlyphCursor,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcFreeCursor, /**//* 95 */
ProcRecolorCursor,
ProcQueryBestSize,
ProcQueryExtension,
ProcListExtensions,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcChangeKeyboardMapping, /**//* 100 */
ProcGetKeyboardMapping,
ProcChangeKeyboardControl,
ProcGetKeyboardControl,
ProcBell,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcChangePointerControl, /**//* 105 */
ProcGetPointerControl,
ProcSetScreenSaver,
ProcGetScreenSaver,
ProcChangeHosts,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcListHosts, /**//* 110 */
ProcChangeAccessControl,
ProcChangeCloseDownMode,
ProcKillClient,
ProcRotateProperties,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
ProcForceScreenSaver, /**//* 115 */
ProcSetPointerMapping,
ProcGetPointerMapping,
ProcSetModifierMapping,
ProcGetModifierMapping,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
0, /**//* 120 */
0,
0,
0,
0,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
0, /**//* 125 */
0,
ProcNoOperation
};
还有一个函数表,叫作SwappedProcVector,它与ProcVector中的函数功能上基本是对应的,特殊之处在于,在调用真正的服务前,先预处理一下请求的数据,客户端程序可能运行在不同字节顺序的机器上,这时候要交换一下数据的字节顺序。我们可以看SProcCreateWindow的实现:
Int SProcCreateWindow(ClientPtr client)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
char n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
REQUEST(xCreateWindowReq);
swaps(&stuff->length, n);
REQUEST_AT_LEAST_SIZE(xCreateWindowReq);
swapl(&stuff->wid, n);
swapl(&stuff->parent, n);
swaps(&stuff->x, n);
swaps(&stuff->y, n);
swaps(&stuff->width, n);
swaps(&stuff->height, n);
swaps(&stuff->borderWidth, n);
swaps(&stuff->class, n);
swapl(&stuff->visual, n);
swapl(&stuff->mask, n);
SwapRestL(stuff);
return((* ProcVector[X_CreateWindow])(client));
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
有了这种机制,增加新的功能,只需要增一个处理函数就行了,处理函数并需要知道请求从何而来,何时被调用。相反,要裁剪某些功能,亦只需要从这里拿开这个函数就可以了。当然,实际很少直接修改这个函数表,增加新功能往往是扩展模块来实现,扩展模块最终也是修改这个函数表,这是后话,暂且不提。
(待续)