The good old xorg.conf and the new xorg.conf.d

自從 Xorg xserver 宣佈要放棄使用 hal 的自動設定機制後,Xorg 開發者們就開始討論與實做各種可能的 config backend。最後定案的是在 Linux 上直接使用 udev 來偵測 input device,在這個後端實做的早期也想把設定的工作一起做在 udev rules 裡,但顯然這樣的做法是有其侷限的。很多人說這只是把 hal 的 .fdi 換個寫法放到 udev rules 裡罷了,同樣是對 user 極不友善的做法,而且使用 udev 在非 Linux 的平台就不能用了。

最後一個很聰明的辦法是把 auto probe 與 config 這兩個行為分開,讓 udev 只管找到新的裝置,config 則集中放在 xorg.conf.d 中。xorg.conf.d 中設定檔的格式和 xorg.conf 是一樣的,因此不但 code 可以重用,使用者也不用學新的語法,其他平台則可以用不同的 backend。

因為 udev backend 是以 Debian Developer - Julien Cristau 為主開發的, Debian 很早就可以使用 udev backend 了,最近 Debian 更從 xorg-xserver 1.8 backport 了 xorg.conf.d 的修改,所以設定方式又不太一樣了。(大概只有 Debian 是這樣一改再改吧,其他 distro 應是無痛升級)

參考 xorg.conf(5),X 在啟動時會去找

    /etc/X11/<cmdline>
    /usr/etc/X11/<cmdline>
    /etc/X11/$XORGCONFIG
    /usr/etc/X11/$XORGCONFIG
    /etc/X11/xorg.conf-4
    /etc/X11/xorg.conf
    /etc/xorg.conf
    /usr/etc/X11/xorg.conf.<hostname>
    /usr/etc/X11/xorg.conf-4
    /usr/etc/X11/xorg.conf
    /usr/lib/X11/xorg.conf.<hostname>
    /usr/lib/X11/xorg.conf-4
    /usr/lib/X11/xorg.conf

這些地方的設定檔,因此設定不用全放在一個檔案裡了。這也是近來很多 daemon 要支援多重設定檔常用的方法,可以讓 vendor 或 distro 的設定與 user 的設定可以各自獨立出來。

接下來我們有興趣的地方是 InputClass 這個新的 Section,就是它讓我們可以設定隨插即用裝置。

    Section "InputClass"
        Identifier "name"
        entries
        ...
        options
        ...
    EndSection

就像 xorg.conf 內其他 Section 一樣,Identifier 是必需的,我們可以用各種 Match rule 去比對裝置的特性並用 Option 加以設定。所有的 InputClass 都可以被其之後的 InputClass 取代,因此使用者可以用新設定檔取代系統的設定。

現在可以用的 Match rule 有

    MatchProduct        "matchproduct"
    MatchVendor         "matchvendor"
    MatchDevicePath     "matchdevice"
    MatchTag            "matchtag"
    MatchIsKeyboard     "bool"
    MatchIsPointer      "bool"
    MatchIsJoystick     "bool"
    MatchIsTablet       "bool"
    MatchIsTouchpad     "bool"
    MatchIsTouchscreen  "bool"

前四個可以用來 match 特定的裝置名稱、路徑,其他的則可以用來 match 一般的裝置特性如滑鼠、鍵盤等。如我要設定 touchpad 的話,可以用

    # /etc/X11/xorg.conf.d/10-synaptics.conf
    Section "InputClass"
        Identifier "Synaptics"
        MatchIsTouchpad "true"
     
        Option "TapButton1" "1"
        Option "HorizEdgeScroll" "true"
    EndSection

來設定。再加上 X 會自己找到可以用的 driver,和以前方法比起來,是不是簡單很多呢? 😊