Debian8.2 xfce桌面設置雙屏

最近想折騰 Xfce,並且 Fedora 的包都好老啊,因此換了 Arch Linux… 果真裝完就有的折騰了:Xfce 4.0 竟然不支持雙顯示器擴展桌面(extended desktop)!若是經過調用 xrandr 動態增長顯示器的方法,在拔出顯示器的時候竟然筆記本內置的顯示器分辨率設置會亂掉…shell

針對不能自動檢測、自動調整的問題,我發現了其實能夠用 inotify 檢測 /sys/class/drm/card0-VGA-1/status 這個文件的改動(其實作不到,繼續讀),而後在有改動的時候調用 xrandr 來配置/關閉外接顯示器。bash

首先俺在 Terminal 裏面運行:學習

[jiehan@tpx300 ~]$ cat /sys/class/drm/card0-VGA-1/status
connected

而後在拔出 VGA1 上的顯示器後,再運行能顯示:spa

[jiehan@tpx300 ~]$ cat /sys/class/drm/card0-VGA-1/status
disconnected

既然如此,inotifywait -e modify /sys/class/drm/card0-VGA-1/status 就能在/sys/class/drm/card0-VGA-1/status 被改動的時候返回了吧?命令行

—— 但是… 事實不是這樣的(廢話,要是這樣的話還折騰啥呢?爲啥呢,由於 inotify FAQ 裏有這句話:code

Q: Can I watch sysfs (procfs, nfs…)?
Simply spoken: yes, but with some limitations. These limitations vary between kernel versions and tend to get smaller. Please read information about particular filesystems.orm

誒,那看看總共能用啥吧… 來監視一下全部的事件,以下是運行後插拔顯示器 stdout 的輸出:事件

[jiehan@tpx300 ~]$ inotifywait -m /sys/class/drm/card0-VGA-1/status 2> /dev/null
/sys/class/drm/card0-VGA-1/status OPEN 
/sys/class/drm/card0-VGA-1/status ACCESS 
/sys/class/drm/card0-VGA-1/status CLOSE_NOWRITE,CLOSE 
/sys/class/drm/card0-VGA-1/status OPEN 
/sys/class/drm/card0-VGA-1/status ACCESS 
/sys/class/drm/card0-VGA-1/status CLOSE_NOWRITE,CLOSE 
^C

啥?OPEN、ACCESS、CLOSE_NOWRITE 和 CLOSE 事件?爲啥實際 MODIFY 了可是沒事件呢?估計是由於 /sys 是個 sysfs 吧,鬼知道… 因此咱們只能 inotifywait 任何一個關於/sys/class/drm/card0-VGA-1/status 的事件了。這也就要求你沒事不要老去讀那個文件,否則每次你的腳本都得判斷一下…rem

好吧,那就醬紫:get

[jiehan@tpx300 ~]$ cat bin/dual-monitor-watch 
#!/bin/bash 
function connection_check {
    if [ "connected" = $( cat /sys/class/drm/card0-VGA-1/status ) ]; then
        setup_monitorelse
        turn_off_monitorfi
    } 
    
function setup_monitor {
    notify-send 'Secondary monitor detected' 'xrandr --output VGA1 --auto --right-of LVDS1' --icon=dialog-information
    xrandr --output VGA1 --auto --right-of LVDS1
} 

function turn_off_monitor {
    notify-send 'Secondary monitor disconnected' 'xrandr --output VGA1 --off' --icon=dialog-information
    xrandr --output VGA1 --off
} 

notify-send 'Ready to handle secondary monitor' --icon=dialog-information
connection_check

while inotifywait /sys/class/drm/card0-VGA-1/status &> /dev/null; do
    connection_checkdone
done

而後能夠把 /home/$USER/bin/dual-monitor-watch 給 chmod +x 了而後給放到 Session and Startup 裏面自動啓動就好啦~

說明:

  • inotifywait 在 inotify-tools 包中,使用前得先安裝。

  • VGA1 這樣的標識能夠經過 xrandr -q 獲得。

限制:

  • 這個腳本沒作鎖,因此進入桌面那次運行就好了別反覆運行…

  • 這個腳本確實有問題,好比若是你先拔了顯示器在 xrandr 還沒運行完的時候就又給顯示器插上了,那 inotifywait 還沒來及運行呢… 懶得改了,自重吧… 反正是你本身電腦 :D

參考連接:

https://jiehan.org/tech/xfce-dual-monitor-with-hotplugging-capabilities/

我的閱後感想:

我靠,真的不通常,好吧,我認可我收穫了不少東西。

很值得學習的。。。


PS. 一些備註

xrandr 命令行能夠很方便地切換雙屏,經常使用方式以下,其餘的能夠本身探索: xrandr --output VGA --same-as LVDS --auto 打開外接顯示器(最高分辨率),與筆記本液晶屏幕顯示一樣內容(克隆) xrandr --output VGA --same-as LVDS --mode 1024x768 打開外接顯示器(分辨率爲1024x768),與筆記本液晶屏幕顯示一樣內容(克隆) xrandr --output VGA --right-of LVDS --auto 打開外接顯示器(最高分辨率),設置爲右側擴展屏幕 xrandr --output VGA --off 關閉外接顯示器 xrandr --output VGA --auto --output LVDS --off 打開外接顯示器,同時關閉筆記本液晶屏幕(只用外接顯示器工做) xrandr --output VGA --off --output LVDS --auto 關閉外接顯示器,同時打開筆記本液晶屏幕 (只用筆記本液晶屏) (最後兩種狀況請當心操做,不要誤把兩個屏幕都關掉了。。。。)
相關文章
相關標籤/搜索