如何從命令列取得X視窗邊框寬度?

如何從命令列取得X視窗邊框寬度?

我已經嘗試過xdotoolxwininfoxprop,但它們似乎都返回視窗的內容大小,其中不包括邊框寬度。有沒有可以找到這個邊框寬度的命令列工具?希望該工具能夠在不同的 EWMH 相容視窗管理器上運作。

答案1

根據您的視窗管理器,您可以用來xwininfo -tree -root列出所有視窗層次結構,然後將樹從目標視窗向上處理到視窗管理器放置在目標周圍的視窗框架。

以下腳本迭代地執行此操作,xwininfo -tree僅在目標視窗上執行以尋找父窗口,然後重複在樹中逐步向上移動的過程,直到父視窗成為根視窗 ( Parent window id: ...(the root window))。它假設以根為父級的視窗是所需的框架。

透過添加,-stats您可以輕鬆存取視窗的寬度和高度。例如,在xterm724 x 1069 像素中,我們得到 742 x 1087 像素的幀:

$ xwininfo -tree -stats -id $WINDOWID 
  Parent window id: 0x8002ff (has no name)
  ...
  Width: 724
  Height: 1069
$ xwininfo -tree -stats -id 0x8002ff
  Parent window id: 0x8002fe (has no name)
  ...
  Width: 724
  Height: 1069
$ xwininfo -tree -stats -id 0x8002fe
  Parent window id: 0xc1 (the root window) (has no name)
  ...
  Width: 742
  Height: 1087

這是腳本,以視窗 ID 號碼作為參數:

#!/bin/bash
# http://unix.stackexchange.com/a/331516/119298
getwh(){
    xwininfo -tree -stats -id "$id" | 
    awk '/Parent window id:/{ parent = ($0~/the root window/)?0:$4; }
        / Width:/  { w = $2 }
        / Height:/ { h = $2 }
        END            { printf "%s %d %d\n",parent,w,h }'
}
id=${1:-${WINDOWID?}}
set -- $(getwh "$id")
w=$2 h=$3
while id=$1
      [ "$id" != 0 ]      
do    set -- $(getwh "$id")
done
let bw=$2-w  bh=$3-h
echo "total border w and h $bw $bh"

它會列印total border w and h 18 18,並且您需要將它們除以 2 以使邊框寬度假設對稱。如果情況並非如此,例如標題列較大,則還需要使用 x 和 y 偏移量的差異來計算頂部、底部、左側、右側各個邊框。

答案2

一種迂迴的方法是使用 xwd 轉儲內容,然後測量產生的影像。

xwd -frame | xwdtopnm | head -2

xwdtppnm來自 netpbm 套件

相關內容