2016年9月18日日曜日

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

修正「簡易ポーズエディタ」 で作ったスクリプトの再修正
例によって、上部のボタンは意味なし 。

原点に対する補正値を入れられるようにした。
起動時に、原点復帰を行う。

#!/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 ADJUST0='4'
export ADJUST1='3'
export ADJUST2='-3'
export ADJUST3='5'
export ADJUST4='-4'
export ADJUST5='-3'
export ADJUST6='1'
export ADJUST7='-3'

for((i = 0; i <= 7; i++)); do
    echo $i=$((SERVO$i + ADJUST$i))% > /dev/servoblaster
done

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 + ADJUST5))"% > /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 + ADJUST4))"% > /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 + ADJUST7))"% > /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 + ADJUST8))"% > /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 + ADJUST0))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 1="$((SERVO1 + ADJUST1))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 2="$((SERVO2 + ADJUST2))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 3="$((SERVO3 + ADJUST3))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 4="$((SERVO4 + ADJUST4))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 5="$((SERVO5 + ADJUST5))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 6="$((SERVO6 + ADJUST6))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 7="$((SERVO7 + ADJUST7))"% > /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 + ADJUST0))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 1="$((SERVO1 + ADJUST1))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 2="$((SERVO2 + ADJUST2))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 3="$((SERVO3 + ADJUST3))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 4="$((SERVO4 + ADJUST4))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 5="$((SERVO5 + ADJUST5))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 6="$((SERVO6 + ADJUST6))"% > /dev/servoblaster" >> $FILE0</action>
            <action>echo "echo 7="$((SERVO7 + ADJUST7))"% > /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 + ADJUST0))"% > /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 + ADJUST1))"% > /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 + ADJUST2))"% > /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 + ADJUST3))"% > /dev/servoblaster"</action>
                <action type="refresh">TEXT3</action>
              </vscale>
            </frame>
          </hbox>
        </vbox>
      </hbox>
    </vbox>
  </window>
'

gtkdialog -p MAIN_DIALOG -c 

2016年8月16日火曜日

ワークスペース毎に動くタイリング・シェルスクリプト

前回作成したシェルスクリプトを、ワークスペース毎に操作できるようにする実験


WmCtrlGtk(0)とWmCtrlGtk(1)で、それぞれワークスペース0とワークスペース1を別個に操作する。
(その分、シェルスクリプトから保存するファイルが増えてます。)

#!/bin/bash

PRE_IFS=$IFS
IFS=$'\n'

top_panel_h=0
bottom_panel_h=26
title_h=30
disp_width=$(xwininfo -root | grep 'Width' | cut -f2 -d ':')
disp_height=$(xwininfo -root | grep 'Height' | cut -f2 -d ':')
export current_ws=$(xprop -root -notype _NET_CURRENT_DESKTOP | cut -b 24)

if [ ! $(wmctrl -l | grep "WmCtrlGtk("$current_ws")") ]; then
    export MAIN_DIALOG$current_ws='
    <window title="WmCtrlGtk('$current_ws')">
        <hbox>
            <button>
                <label>h</label>
                <action>'$0' h 200</action>
            </button>
            <button>
                <label>v</label>
                <action>'$0' v 200</action>
            </button>
            <button>
                <label>r</label>
                <action>'$0' r</action>
            </button>
            <button>
                <label>+100</label>
                <action>'$0' 100</action>
            </button>
            <button>
                <label>-100</label>
                <action>'$0' -100</action>
            </button>
        </hbox>
    </window>
    '

    gtkdialog -p MAIN_DIALOG$current_ws -c
fi

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'
    fi
elif [ $# -eq 2 ]; then
    hv_flag=$1
    main_add=$2
else
    exit 0
fi

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

wmctrl -l > wm-ctrl${current_ws}.txt
sed -i '/WmCtrlGtk/d' ./wm-ctrl${current_ws}.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 - top_panel_h - bottom_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 - top_panel_h - bottom_panel_h) / 2))

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

    case $ws_windows in
        1)
            temp_width=$disp_width
            temp_height=$((disp_height - top_panel_h - bottom_panel_h - title_h));; 
        [2-3])
            temp_width=$disp_width2
            temp_height=$((disp_height - top_panel_h - bottom_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)
        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${current_ws}.his
                    echo $win_id'__'$current_ws'_' >> wm-ctrl${current_ws}.his
                else
                    echo $win_id'__'$current_ws'_' >> wm-ctrl${current_ws}.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 - top_panel_h - bottom_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 + top_panel_h)) $((0 + top_panel_h)) $((pos_height + top_panel_h)) \
                      $((pos_height + top_panel_h)))
                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${current_ws}.his
    temp=$hv_flag','$main_add','
    sed -i '1i '$temp'' ./wm-ctrl${current_ws}.his
fi

2016年7月3日日曜日

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

前回、作成したシェルスクリプトをGtkDialogを使ってGUI操作できるようにする実験


※追記1(2016/07/09 21:00)※
一応、上部にパネルを表示している場合を想定。
top_panel_hで高さを指定する。

#!/bin/bash

PRE_IFS=$IFS
IFS=$'\n'

top_panel_h=0
bottom_panel_h=26
title_h=30
disp_width=$(xwininfo -root | grep 'Width' | cut -f2 -d ':')
disp_height=$(xwininfo -root | grep 'Height' | cut -f2 -d ':')

if [ ! $(wmctrl -l | grep "WmCtrlGtk") ]; then
    export MAIN_DIALOG='
    <window title="WmCtrlGtk">
        <hbox>
            <button>
                <label>h</label>
                <action>'$0' h</action>
            </button>
            <button>
                <label>v</label>
                <action>'$0' v</action>
            </button>
            <button>
                <label>r</label>
                <action>'$0' r</action>
            </button>
            <button>
                <label>+100</label>
                <action>'$0' 100</action>
            </button>
            <button>
                <label>-100</label>
                <action>'$0' -100</action>
            </button>
        </hbox>
    </window>
    '

    gtkdialog -p MAIN_DIALOG -c
fi

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'
    fi
elif [ $# -eq 2 ]; then
    hv_flag=$1
    main_add=$2
else
    exit 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',')
    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
sed -i '/WmCtrlGtk/d' ./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 - top_panel_h - bottom_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 - top_panel_h - bottom_panel_h) / 2))

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

    case $ws_windows in
        1)
            temp_width=$disp_width
            temp_height=$((disp_height - top_panel_h - bottom_panel_h - title_h));; 
        [2-3])
            temp_width=$disp_width2
            temp_height=$((disp_height - top_panel_h - bottom_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)
        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 - top_panel_h - bottom_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 + top_panel_h)) $((0 + top_panel_h)) $((pos_height + top_panel_h)) \
                      $((pos_height + top_panel_h)))
                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月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 

2016年5月15日日曜日

Raspberry Pi Arch Linux ARMでのWiringPi事情

 The wiringpi2 module has been deprecated, please 'import wiringpi' instead.


以前「4足歩行ロボットを作ってみよう(その1)」でWiringPiを紹介したが、
その時と事情が変わった模様。

前回は、Python用のラッパーWiringPi2-Pythonを使ったが、2016/5/15現在
そのままでは動かない事が分かった。

改めて、WiringPiをインストールして動作確認したので、その方法を書く。

クリーンな状態からやり直すためWiringPiをアンインストールし前回"git clone"した
ディレクトリを削除しておく。

WiringPiをインストール
$ sudo pacman -S wiringpi

こちら https://github.com/WiringPi/WiringPi-PythonのREADME.mdを参照しつつ
ツール関係をインストール
$ sudo pacman -S python-setuptools swig

GITからWiringPi-Pythonをインストール
$ git clone --recursive https://github.com/WiringPi/WiringPi-Python.git
$ cd ./WiringPi-Python
$ swig -python wiringpi.i
$ sudo python3 setup.py install

※上記のswigを忘れない事!!※

これでラッパーが使えるようになるが、Python3でラッパーを呼び出す際に
"import wiringpi2"としていたのが、"import wiringpi"となるので、以前に書いた
コードがあれば修正しておく
Python3で"import wiringpi2"とすると"ImportError"になる。

 ※追記1(2016/05/15 16:50)※
上記の方法でなくとも、別途"python-pip"をインストールの上で、
$ sudo pip3 install wiringpi
することで、簡単にインストール出来ました。


 ※追記2(2017/04/10 20:10)※
kernelが4.9系の場合は、こちらのneuralassemblyさんの修正版をインストールしてください。
(pip3でインストールしていた場合は、予めアンインストールしておいてください)

2016年3月13日日曜日

Scratch フラクタル樹形を描いてみよう

前回、Scratchでコッホ曲線を描いた。今回はフラクタル樹形。

https://scratch.mit.edu/projects/100926629/

なにかフラクタル図形を描くことが目的に成ってるみたいだけど、ちょっと違う。

私は再帰処理は、理解できないので使いたくない。
だから、再帰処理を使わなくても再帰処理っぽいことはできないか?と思う。

で、再帰処理を使えば簡単にできるフラクタル図形だけど、これを再帰処理なしで
やってみよう。と言うのが目的。
要するに『再帰処理が嫌いだ!』って言いたい訳。

で、見た目は同じスクリプト内で自分を呼び出している様に見えるけど、
それは決して『再帰』している訳ではない。
単純に同じスクリプトを何回も実行しているに過ぎないので、
そこんとこ宜しく!」なのだ。

https://scratch.mit.edu/projects/101234609/
(これは、作り直すかもしれない。現状では、パラメータの変更は根元の二股へ
戻った以降に反映されるが、その前でも反映できるようにしたいから)

 ※追記(2016/03/14 20:55)※
描画中に随時変更可能&パラメータのランダム生成型を作りました。
https://scratch.mit.edu/projects/100445168/

2016年2月28日日曜日

Scratch ローカル変数??


Scratchで変数を作る時、「すべてのスプライト用」「このスプライトのみ」を
選択できる。

これをもって、「すべてのスプライト用」をグローバル変数、
 「このスプライトのみ」をローカル変数と解説している記事をよく目にする。
これは首を傾げずにはいられないですよね。

 或るスプライトに属する変数ではあるけど、それって、そのスプライトに関する
全てのスクリプトで参照・変更できるんですよね、、、。
スクリプト(プロシージャ)内でのみ機能する変数ではないので、それを単純に
ローカル変数として紹介してしまうのは、どうなんでしょう。

(2.0で定義ブロックってのが作れるらしいですが、その定義内にスコープが限定される
変数があるっぽいですね。)

Scratch スプライトに「向き」は無いよね

「何らかの角度」に関する数値は持ってると思うけど。

Scratch 「ずっと」をbreakで抜ける


Scratchの「ずっと」ブロックには、終了後に他のブロックを組み合わせる為の
凸がない。

「ずっと」同じ処理を繰り返すのだから、次の処理を実行できないのは、
当然の理屈の様に思える。
ってか、普通にbreakして次の処理へ行きたいよね!!

仕方ないので、「[ ]を送って待つ」と「スクリプトを止める」を使う。


https://scratch.mit.edu/projects/99517398/#editor

今回は、ループ処理が終了するのを待ってから次へ行くので、
「[ ]を送って待つ」を使った。
並行にしたい場合は、「[ ]を送る」にすればメッセージを送った後に
次の処理が始まる。
(くっ、下らねぇ、、、orz)

Scratch カウンタ変数でfor文


Scratchでは、「[10]回繰り返す」ブロックがある。
これ、違和感あるよね。(誰が10回を数えてるんだよ!)

なので、カウンタ変数を使ってfor文にする。


https://scratch.mit.edu/projects/99516656/#editor

この場合「まで繰り返す」の「まで」は、"i < 10"の間、真を返す。
"i = 10"になったら、ループが終了する。
 ここで、"i < 10"ってやっちゃうと、"i > 9"の間真を返すので要注意!!
(くっ、下らねぇ、、、orz)

2016年2月27日土曜日

Scratch はじめの「10歩」でつまずいて


Raspbianには、Scratch(と言うプログラミング環境)が入っているので、使ってみた。
(例によって、一般的な使い方や、入門編の類の話は書かないので、その辺はググってください)

先ずは、ネコを動かすところからなんだけど、
「10歩動かす」ブロックを使っても、10歩動いているようには見えない。

twitter上で悪態をついたところ、翻訳者の先生から返信を頂いたりして恐縮至極ですが、
やっぱり↓こんな風に動いて欲しいよね。
(以後、Web版のScratch 2.0を参照して行きます)
https://scratch.mit.edu/projects/99501857/#editor
 参考:https://scratch.mit.edu/discuss/topic/26943/

でも、 この「10歩動かす」ブロックの肝は、決して「歩」(距離・長さ)ではなく、
どの方向に動くかってことだと思う。所謂タートルグラフィックスと言うもの。
頭の固い老人は「10歩」と言う字面に拘ってしまうが、 子供は「動くことが楽しい」ので、
関係ないらしい。
もし、ここで引っ掛かった人は、『こう言うものだ』と思って、先に進んだ方が
良いかもしれない。
(この辺の話を、教育用のツールとして、どのように説明すべきか等を突き詰めたい人は、
それもまた面白いでしょうが。)

それならば、特性を生かした使い方をしてみよう。


https://scratch.mit.edu/projects/98641324/
タートルグラフィックスならば、小難しいことを考えなくても、この曲がり角で、
どっちを向いて進めば良いかを判定するだけで、コッホ曲線が描ける。
勿論、再帰処理を使えばもっとスッキリしたスクリプトになる。
私は、再帰処理だと、自分が今何処(どの階層)に 居るのか分からなくなるので使えない。
検索すれば、スマートなスクリプトが出てくるので、そちらを参考にして欲しい。

勢いで、作ってはみたものの、制御ブロック等に不思議な違和感があったことを
認めざるを得ない。
https://twitter.com/AO_o10yan/status/698413436561027074
 あるいは逆に、他の言語では構文を組み合わせて作らなければならない一連の操作が、
一つの単純なブロックにまとめられているのを目にした時、不満にも似た感情が沸く
のを禁じ得ない。
ってか、そのブロック使った方が簡単・便利だから使うけどね!!

いずれにしろ、Raspberry PiのOSにRaspbianを使っているなら、非常に面白いので、
使ってみるべきだと思う。