2011-8-4 21:37:11

来源:互联网 发布:软件随想录 txt 编辑:程序博客网 时间:2024/05/22 03:39

 

2011-8-4 21:37:11

稍微深入看Xorg如何处理鼠标键盘初始化,需要看看xorg的代码。从本文分析的具体环境如下, 从/var/log/Xorg.0.log获取:


X.Org X Server 1.4.0.90
Release Date: 5 September 2007
X Protocol Version 11, Revision 0
Build Operating System: Linux Ubuntu (xorg-server 2:1.4.1~git20080131-1ubuntu9.2)
Current Operating System: Linux yhe-laptop 2.6.24-23-generic #1 SMP Thu Feb 5 15:00:25 UTC 2009 i686


首 先安装source code:sudo apt-get  source xorg-server, 就能得到源代码xorg-server-1.4.1~git20080131.

不深入看xorg代码,从config 解析看看鼠标键盘的初始化吧.先看看log记录:


Markers: (--) probed, (**) from config file, (==) default setting,
(++) from command line, (!!) notice, (II) informational,
(WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/Xorg.0.log", Time: Mon May  4 10:40:15 2009
(==) Using config file: "/etc/X11/xorg.conf"  ---> xf86HandleConfigFile
(==) ServerLayout "Layout0"  ----->xf86parseLayoutSection
(**) |-->Screen "Screen0" (0)
(**) |   |-->Monitor "Monitor0"
(**) |   |-->Device "Device0"
(**) |-->Input Device "Keyboard0"
(**) |-->Input Device "Mouse0"
(==) Automatically adding devices
(==) Automatically enabling devices

对应代码是hw/xfree86/parser/scan.c , xf86openConfigFile, 通过两个宏来搜索配置文件:
#define DEFAULT_CONF_PATH    "/etc/X11/%S," \
                                                     .....................
#define XCONFIGFILE    "xorg.conf"

继续深入前,看看这个环境的xorg.conf 对应输入部分的内容:

Section "ServerLayout"
    Identifier     "Layout0"
    Screen      0  "Screen0"
    InputDevice    "Keyboard0" "CoreKeyboard"
    InputDevice    "Mouse0" "CorePointer"
EndSection

Section "InputDevice"
    # generated from default
    Identifier     "Mouse0"
    Driver         "mouse"
    Option         "Protocol" "auto"
    Option         "Device" "/dev/psaux"
    Option         "Emulate3Buttons" "no"
    Option         "ZAxisMapping" "4 5"
EndSection

Section "InputDevice"
    # generated from default
    Identifier     "Keyboard0"
    Driver         "kbd"
EndSection

从上面提到的函数可以追踪到键盘配置的解析过程xf86parseInputSection,将鼠标键盘的配置记录下来,接着看log:
(II) Module ABI versions:
X.Org ANSI C Emulation: 0.3
X.Org Video Driver: 2.0
X.Org XInput driver : 2.0
X.Org Server Extension : 0.3
X.Org Font Renderer : 0.5
(II) Loader running on linux  --->xfree86/common/xf86init.c->InitOutput->LoaderInit
....
(++) using VT number 7  --->    xf86OpenConsole();

(II) LoadModule: "kbd"
(II) Loading /usr/lib/xorg/modules/input//kbd_drv.so
(II) Module kbd: vendor="X.Org Foundation"
        compiled for 1.4.0, module version = 1.2.2
        Module class: X.Org XInput Driver
        ABI class: X.Org XInput driver, version 2.0
(II) LoadModule: "mouse"
(II) Loading /usr/lib/xorg/modules/input//mouse_drv.so
(II) Module mouse: vendor="X.Org Foundation"
        compiled for 1.4.0, module version = 1.2.3
        Module class: X.Org XInput Driver
        ABI class: X.Org XInput driver, version 2.0
......
(II) Initializing built-in extension XInputExtension
(II) Initializing built-in extension XTEST

从log可以看到, kbd是加载的kbd_drv.so, 这个driver在另外一个包中, 需要单独获取源代码:sudo apt-get source xserver-xorg-input-kbd. 编译以下就知道是哪几个C文件构成这个xserver 的drv了:
gcc -shared  .libs/kbd.o .libs/lnx_KbdMap.o .libs/lnx_kbd.o .libs/at_scancode.o   -Wl,-soname -Wl,kbd_drv.so -o .libs/kbd_drv.so
也可以参考 man 4 kbd. 我们的配置文件如下, 选择的就是这个驱动, 在man的信息里提到,默认(没有指定device参数的)情况下, 使用tty7来作为输入设备.
Section "InputDevice"
    # generated from default
    Identifier     "Keyboard0"
    Driver         "kbd"
EndSection
打开设备的代码如下, 可以看到在么有指定device的情况下,就用tty7作为键盘输入源.

static Bool
OpenKeyboard(InputInfoPtr pInfo)
{
    KbdDevPtr pKbd = (KbdDevPtr) pInfo->private;
    int i;
    KbdProtocolId prot = PROT_UNKNOWN_KBD;
    char *s;

    s = xf86SetStrOption(pInfo->options, "Protocol", NULL);
    for (i = 0; protocols[i].name; i++) {
        if (xf86NameCmp(s, protocols[i].name) == 0) {
           prot = protocols[i].id;
           break;
        }
    }

    switch (prot) {
        case PROT_STD:
           pInfo->read_input = stdReadInput;
           break;
        default:
           xf86Msg(X_ERROR,"\"%s\" is not a valid keyboard protocol name\n", s);
           xfree(s);
           return FALSE;
    }

    xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, s);
    xfree(s);

    s = xf86SetStrOption(pInfo->options, "Device", NULL);
    if (s == NULL) {
       pInfo->fd = xf86Info.consoleFd;
       pKbd->isConsole = TRUE;
    } else {
       pInfo->fd = open(s, O_RDONLY | O_NONBLOCK | O_EXCL);
       if (pInfo->fd == -1) {
           xf86Msg(X_ERROR, "%s: cannot open \"%s\"\n", pInfo->name, s);
           xfree(s);
           return FALSE;
       }
       pKbd->isConsole = FALSE;
       xfree(s);
    }

    if (pKbd->isConsole)
         pKbd->vtSwitchSupported = TRUE;

    return TRUE;
}

..........
(**) Option "CoreKeyboard"
(**) Keyboard0: always reports core events
(**) Option "Protocol" "standard" 
(**) Keyboard0: Protocol: standard  xserver-xorg-input-kbd/src/lnx_kbd.c ->OpenKeyboard
(**) Option "AutoRepeat" "500 30"
(**) Option "XkbRules" "xorg"
(**) Keyboard0: XkbRules: "xorg"
(**) Option "XkbModel" "pc105"
(**) Keyboard0: XkbModel: "pc105"
(**) Option "XkbLayout" "us"
(**) Keyboard0: XkbLayout: "us"
(**) Option "CustomKeycodes" "off"
(**) Keyboard0: CustomKeycodes disabled


鼠标类似,也是单独下载驱动的源码:sudo apt-get source xserver-xorg-input-mouse, make 然后知道其组成:
gcc -shared  .libs/mouse.o .libs/pnp.o   -Wl,-soname -Wl,mouse_drv.so -o .libs/mouse_drv.so

Section "InputDevice"
    # generated from default
    Identifier     "Mouse0"
    Driver         "mouse"
    Option         "Protocol" "auto"
    Option         "Device" "/dev/psaux"
    Option         "Emulate3Buttons" "no"
    Option         "ZAxisMapping" "4 5"
EndSection


(**) Option "Protocol" "auto"
(**) Mouse0: Device: "/dev/psaux"  xserver-xorg-input-mouse/src/mouse.c:MousePreInit
(**) Mouse0: Protocol: "auto"
(**) Option "CorePointer"
(**) Mouse0: always reports core events
(**) Option "Device" "/dev/psaux"
(**) Option "Emulate3Buttons" "no"
(**) Option "ZAxisMapping" "4 5"
(**) Mouse0: ZAxisMapping: buttons 4 and 5
(**) Mouse0: Buttons: 9
(**) Mouse0: Sensitivity: 1
(II) evaluating device (Mouse0)
(II) XINPUT: Adding extended input device "Mouse0" (type: MOUSE)
(II) XINPUT: Adding extended input device "Mouse0" (type: MOUSE)
(II) evaluating device (Keyboard0)
(II) XINPUT: Adding extended input device "Keyboard0" (type: KEYBOARD)
(--) Mouse0: PnP-detected protocol: "ExplorerPS/2"
(II) Mouse0: ps2EnableDataReporting: succeeded

原创粉丝点击