2016年6月19日日曜日

wmctrlでOpenboxウィンドウのタイリング

wmctrlを使ったシェルスクリプトでOpenboxのウィンドウを
タイリング操作してみる。


(録画にリソースをとられているので緩慢な動きですが、
実際は、、、。もう少しだけマシかな?)

※一応スクリプトを載せますが、ググればもっと素晴らしいスクリプトが出てくるので、
興味のある方はそちらを見てくださいね。
https://bbs.archlinux.org/viewtopic.php?id=119020
尚、スクリプトには別途、wmctrl とxorg-apps のインストールが必要です※

※追記1(2016/06/21 20:25)※
スクリプトの書式を変更。機能的な変化はなし。
※追記2(2016/06/27 21:50)※
余分な処理を削除。機能的な変化なし。
※追記3(2016/06/28 22:30)※
メインウィンドウのローテーションを保持したままサイズ変更出きるようにした。
第一引数に数値(ピクセル単位)だけを指定すると、メインウィンドウのサイズが変更される。
プラスの数値で広く、マイナスの数値で狭くなる。
<使い方>
第一引数には、h、v、r及び数値の指定ができる。
h  ウィンドウを横に並べる。第2引数に数値を指定すると、その数値の分だけメインウィンドウが広くなる。
v  ウィンドウを縦に並べる。第2引数に数値を指定すると、その数値の分だけメインウィンドウが広くなる。
r  メインウィンドウを入れ替える(ローテートする)。
数値  メインウィンドウのサイズ変更。
(ウィンドウが4つ以上だと、メインの意味はなくなるので注意)

#!/bin/bash

PRE_IFS=$IFS
IFS=$'\n'

panel_h=28
title_h=28
disp_width=$(xwininfo -root | grep 'Width' | cut -f2 -d ':' | sed 's/^[ \t]*//')
disp_height=$(xwininfo -root | grep 'Height' | cut -f2 -d ':' | sed 's/^[ \t]*//')

rotate_flag=0
if [ $# -eq 1 ]; then
    if [ $1 = 'h' -o $1 = 'v' ]; then
        hv_flag=$1
        main_add=0
    elif [ $1 = 'r' ]; then
        rotate_flag=1
    elif [ $1 -gt 0 -o $1 -lt 0 ]; then
        rotate_flag=2
    else
        hv_flag='h'
        #main_add=$1
    fi
elif [ $# -eq 2 ]; then
    hv_flag=$1
    main_add=$2
else
    hv_flag='h'
    main_add=0
fi

temp_file='./wm-ctrl.txt'
if [ $rotate_flag -gt 0 ]; then
    hv_flag=$(sed -n 1p ./wm-ctrl.his | cut -f1 -d',')
    main_add=$(sed -n 1p ./wm-ctrl.his | cut -f2 -d',' | sed 's/^[ \t]*//')
    if [ $rotate_flag -eq 1 ]; then
        temp_1=$(sed -n 2p ./wm-ctrl.his)
        sed -i '$a '$temp_1'' ./wm-ctrl.his
        sed -i 1,2d ./wm-ctrl.his
    else
        sed -i 1d ./wm-ctrl.his
        main_add=$((main_add + $1))
    fi
    sed -i 's/_/ /g' ./wm-ctrl.his
    temp_file='./wm-ctrl.his'
fi

wmctrl -l > wm-ctrl.txt
txt_lines=$(wc $temp_file | sed 's/^[ \t]*//' | cut -f1 -d " ")
if [ $txt_lines -gt 0 ]; then
    current_ws=$(xprop -root -notype _NET_CURRENT_DESKTOP | cut -b 24)
    #active_win=$(xprop -root -notype _NET_ACTIVE_WINDOW | cut -b 33-43)

    ws_windows=0
    for((i = 1; i <= $txt_lines; i++)); do
        ws_flag=$(sed -n $i'p' $temp_file | cut -b 12-13)
        if [ $ws_flag -eq $current_ws ]; then
            let ws_windows++
        fi
    done

    disp_width2=$((disp_width / 2))
    disp_height2=$(($(((disp_height - panel_h) / 2)) - title_h))
    disp_width_main=$((disp_width2 + main_add))
    disp_height_main=$((disp_height2 + main_add))
    disp_width_sub=$((disp_width2 - main_add))
    disp_height_sub=$((disp_height2 - main_add))

    pos_width=$((disp_width / 2))
    pos_height=$(((disp_height - panel_h) / 2))

    if [ $hv_flag = 'v' ]; then
        x_pos=(0 0 $pos_width $pos_width)
        y_pos=(0 $((pos_height + main_add)) $((pos_height + main_add)) 0)
    else
        x_pos=(0 $((pos_width + main_add)) $((pos_width + main_add)) 0)
        y_pos=(0 0 $pos_height $pos_height)
    fi

    case $ws_windows in
        1)
            temp_width=$disp_width
            temp_height=$((disp_height - panel_h - title_h));; 
        [2-3])
            temp_width=$disp_width2
            temp_height=$((disp_height - panel_h - title_h));;
        *)
            temp_width=$disp_width2
            temp_height=$disp_height2;;
    esac

    window_array=()
    n=0
    for((i = 1; i <= $txt_lines; i++)); do
        if [ $n -gt 3 ]; then
            n=0
        fi
        ws_flag=$(sed -n $i'p' $temp_file | cut -b 11-13 | sed 's/^[ \t]*//')
        if [ $ws_flag -eq $current_ws ]; then
            win_id=$(sed -n $i'p' $temp_file | cut -b 1-10)
            if [ $rotate_flag -eq 0 ]; then
                if [ $n -eq 0 ]; then
                    echo $hv_flag','$main_add',' > wm-ctrl.his
                    echo $win_id'__'$current_ws'_' >> wm-ctrl.his
                else
                    echo $win_id'__'$current_ws'_' >> wm-ctrl.his
                fi
            fi
            if [ $ws_windows -eq 2 ]; then
                if [ $n -eq 0 ]; then
                    if [ $hv_flag = 'v' ]; then
                        temp_width=$disp_width
                        temp_height=$disp_height_main
                    else
                        temp_width=$disp_width_main
                    fi
                else
                    if [ $hv_flag = 'v' ]; then
                        temp_width=$disp_width
                        temp_height=$disp_height_sub
                    else
                        temp_width=$disp_width_sub
                        temp_height=$((disp_height - panel_h - title_h))
                    fi
                fi
            fi            
            if [ $ws_windows -eq 3 ]; then
                if [ $n -eq 0 ]; then
                    if [ $hv_flag = 'v' ]; then
                        temp_width=$disp_width
                        temp_height=$disp_height_main
                    else
                        temp_width=$disp_width_main
                    fi
                fi
            fi
            if [ $ws_windows -gt 2 -a $n -gt 0 ]; then
                if [ $hv_flag = 'v' ]; then
                    temp_width=$disp_width2
                    temp_height=$disp_height_sub
                else
                    temp_width=$disp_width_sub
                    temp_height=$disp_height2
                fi
            fi
            if [ $ws_windows -gt 3 -a $n -gt 0 ]; then
                x_pos=(0 $pos_width $pos_width 0)
                y_pos=(0 0 $pos_height $pos_height)
                temp_width=$disp_width2
                temp_height=$disp_height2
            fi
            window_array+=( $win_id','${x_pos[$n]}','${y_pos[$n]}','$temp_width','$temp_height )
            let n++
        fi
    done

    for par in ${window_array[@]}; do
        for((i = 1; i <= 5; i++)); do
            declare par$i=$(echo ${par} | cut -f$i -d ',')
        done
        wmctrl -i -r $par1 -e 0,$par2,$par3,$par4,$par5
    done
fi

if [ $rotate_flag -gt 0 ]; then
    sed -i 's/ /_/g' ./wm-ctrl.his
    temp=$hv_flag','$main_add','
    sed -i '1i '$temp'' ./wm-ctrl.his
fi

2016年6月5日日曜日

修正「簡易ポーズエディタ」

Raspberry Pi 2 4足歩行ロボット用「簡易ポーズエディタ」 で作ったスクリプトの修正。
例によって、上部のボタンは意味なし 。


レイアウト修正と、ファイル「新規作成」の修正及び、「ポーズ追加」ボタンの付加。

中央下の「新規作成」ボタンでスクリプトファイル(形式のテキストファイル)作成。

「ポーズ追加」ボタンで、次のポーズを追加。

テキストファイルなので、実行権限はなし。
動きの無いサーボモータに対しても同位置の指示が書き込みされている。
ポーズ間のスリープは1秒固定。

実際には、上の修正をしながらシェルスクリプトとして手作業で仕上げて行く。

#!/bin/bash

export test00='
echo $USER
'
export SERVO0='50'
export SERVO1='50'
export SERVO2='50'
export SERVO3='50'
export SERVO4='50'
export SERVO5='50'
export SERVO6='50'
export SERVO7='50'

export MAIN_DIALOG='
  <window>
    <vbox>
      <hbox>
        <vbox>
          <hbox>
            <frame サーボ5(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1">
                <default>50</default>
                <variable>SERVO5</variable>
                <action>bash -c "echo 5="$SERVO5"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT5</action>
              </vscale>
            </frame>
            <frame サーボ4(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1">
                <default>50</default>
                <variable>SERVO4</variable>
                <action>bash -c "echo 4="$SERVO4"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT4</action>
              </vscale>
            </frame>
          </hbox>
          <hbox>
            <frame サーボ7(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
                <default>50</default>
                <variable>SERVO7</variable>
                <action>bash -c "echo 7="$SERVO7"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT7</action>
              </vscale>
            </frame>
            <frame サーボ6(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1">
                <default>50</default>
                <variable>SERVO6</variable>
                <action>bash -c "echo 6="$SERVO6"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT6</action>
              </vscale>
            </frame>
          </hbox>
        </vbox>

        <vbox>
          <hbox>
            <button>
              <label>ボタン 1</label>
              <action>echo $LANG</action>
            </button>
            <vbox>
              <button width-request="110">
                <label>前進</label>
                <action>bash -c "$test00"</action>
              </button>
              <button>
                <label>後退</label>
              </button>
            </vbox>
            <button>
              <label>ボタン 4</label>
            </button>
          </hbox>
          <vbox>
            <frame>
              <text>
                <variable>TEXT0</variable>
                <input>echo "echo 0="$SERVO0"% > /dev/servoblaster"</input>
              </text>
              <text>
                <variable>TEXT1</variable>
                <input>echo "echo 1="$SERVO1"% > /dev/servoblaster"</input>
              </text>
              <text>
                <variable>TEXT2</variable>
                <input>echo "echo 2="$SERVO2"% > /dev/servoblaster"</input>
              </text>
              <text>
                <variable>TEXT3</variable>
                <input>echo "echo 3="$SERVO3"% > /dev/servoblaster"</input>
              </text>
              <text>
                <variable>TEXT4</variable>
               <input>echo "echo 4="$SERVO4"% > /dev/servoblaster"</input>
              </text>
              <text>
                <variable>TEXT5</variable>
                <input>echo "echo 5="$SERVO5"% > /dev/servoblaster"</input>
              </text>
              <text>
                <variable>TEXT6</variable>
                <input>echo "echo 6="$SERVO6"% > /dev/servoblaster"</input>
              </text>
              <text>
                <variable>TEXT7</variable>
                <input>echo "echo 7="$SERVO7"% > /dev/servoblaster"</input>
              </text>
            </frame>
          </vbox>
          <entry>
            <variable>FILE0</variable>
            <default>"./temp.txt"</default>
          </entry>
          <button>
            <label>新規作成</label>
            <action>echo "#!/bin/bash" > $FILE0</action>
            <action>echo "" >> $FILE0</action>
            <action>echo "echo 0="$SERVO0"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 1="$SERVO1"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 2="$SERVO2"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 3="$SERVO3"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 4="$SERVO4"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 5="$SERVO5"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 6="$SERVO6"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 7="$SERVO7"% > /dev/servoblaster" >> $FILE0</action>
          </button>
          <button>
            <label>ポーズ追加</label>
            <action>echo "" >> $FILE0</action>
            <action>echo "sleep 1" >> $FILE0</action>
            <action>echo "" >> $FILE0</action>
            <action>echo "echo 0="$SERVO0"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 1="$SERVO1"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 2="$SERVO2"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 3="$SERVO3"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 4="$SERVO4"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 5="$SERVO5"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 6="$SERVO6"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 7="$SERVO7"% > /dev/servoblaster" >> $FILE0</action>
          </button>
        </vbox>

        <vbox>
          <hbox>
            <frame サーボ0(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
                <default>50</default>
                <variable>SERVO0</variable>
                <action>bash -c "echo 0="$SERVO0"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT0</action>
              </vscale>
            </frame>
            <frame サーボ1(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
                <default>50</default>
                <variable>SERVO1</variable>
                <action>bash -c "echo 1="$SERVO1"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT1</action>
              </vscale>
            </frame>
          </hbox>
          <hbox>
            <frame サーボ2(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
                <default>50</default>
                <variable>SERVO2</variable>
                <action>bash -c "echo 2="$SERVO2"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT2</action>
              </vscale>
            </frame>
            <frame サーボ3(%)>
              <vscale height-request="200" range-min="10" range-max="90" range-step="1">
                <default>50</default>
                <variable>SERVO3</variable>
                <action>bash -c "echo 3="$SERVO3"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT3</action>
              </vscale>
            </frame>
          </hbox>
        </vbox>
      </hbox>
    </vbox>
  </window>
'

gtkdialog -p MAIN_DIALOG -c