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つ以上だと、メインの意味はなくなるので注意)

  1. #!/bin/bash
  2.  
  3. PRE_IFS=$IFS
  4. IFS=$'\n'
  5.  
  6. panel_h=28
  7. title_h=28
  8. disp_width=$(xwininfo -root | grep 'Width' | cut -f2 -d ':' | sed 's/^[ \t]*//')
  9. disp_height=$(xwininfo -root | grep 'Height' | cut -f2 -d ':' | sed 's/^[ \t]*//')
  10.  
  11. rotate_flag=0
  12. if [ $# -eq 1 ]; then
  13. if [ $1 = 'h' -o $1 = 'v' ]; then
  14. hv_flag=$1
  15. main_add=0
  16. elif [ $1 = 'r' ]; then
  17. rotate_flag=1
  18. elif [ $1 -gt 0 -o $1 -lt 0 ]; then
  19. rotate_flag=2
  20. else
  21. hv_flag='h'
  22. #main_add=$1
  23. fi
  24. elif [ $# -eq 2 ]; then
  25. hv_flag=$1
  26. main_add=$2
  27. else
  28. hv_flag='h'
  29. main_add=0
  30. fi
  31.  
  32. temp_file='./wm-ctrl.txt'
  33. if [ $rotate_flag -gt 0 ]; then
  34. hv_flag=$(sed -n 1p ./wm-ctrl.his | cut -f1 -d',')
  35. main_add=$(sed -n 1p ./wm-ctrl.his | cut -f2 -d',' | sed 's/^[ \t]*//')
  36. if [ $rotate_flag -eq 1 ]; then
  37. temp_1=$(sed -n 2p ./wm-ctrl.his)
  38. sed -i '$a '$temp_1'' ./wm-ctrl.his
  39. sed -i 1,2d ./wm-ctrl.his
  40. else
  41. sed -i 1d ./wm-ctrl.his
  42. main_add=$((main_add + $1))
  43. fi
  44. sed -i 's/_/ /g' ./wm-ctrl.his
  45. temp_file='./wm-ctrl.his'
  46. fi
  47.  
  48. wmctrl -l > wm-ctrl.txt
  49. txt_lines=$(wc $temp_file | sed 's/^[ \t]*//' | cut -f1 -d " ")
  50. if [ $txt_lines -gt 0 ]; then
  51. current_ws=$(xprop -root -notype _NET_CURRENT_DESKTOP | cut -b 24)
  52. #active_win=$(xprop -root -notype _NET_ACTIVE_WINDOW | cut -b 33-43)
  53.  
  54. ws_windows=0
  55. for((i = 1; i <= $txt_lines; i++)); do
  56. ws_flag=$(sed -n $i'p' $temp_file | cut -b 12-13)
  57. if [ $ws_flag -eq $current_ws ]; then
  58. let ws_windows++
  59. fi
  60. done
  61.  
  62. disp_width2=$((disp_width / 2))
  63. disp_height2=$(($(((disp_height - panel_h) / 2)) - title_h))
  64. disp_width_main=$((disp_width2 + main_add))
  65. disp_height_main=$((disp_height2 + main_add))
  66. disp_width_sub=$((disp_width2 - main_add))
  67. disp_height_sub=$((disp_height2 - main_add))
  68.  
  69. pos_width=$((disp_width / 2))
  70. pos_height=$(((disp_height - panel_h) / 2))
  71.  
  72. if [ $hv_flag = 'v' ]; then
  73. x_pos=(0 0 $pos_width $pos_width)
  74. y_pos=(0 $((pos_height + main_add)) $((pos_height + main_add)) 0)
  75. else
  76. x_pos=(0 $((pos_width + main_add)) $((pos_width + main_add)) 0)
  77. y_pos=(0 0 $pos_height $pos_height)
  78. fi
  79.  
  80. case $ws_windows in
  81. 1)
  82. temp_width=$disp_width
  83. temp_height=$((disp_height - panel_h - title_h));;
  84. [2-3])
  85. temp_width=$disp_width2
  86. temp_height=$((disp_height - panel_h - title_h));;
  87. *)
  88. temp_width=$disp_width2
  89. temp_height=$disp_height2;;
  90. esac
  91.  
  92. window_array=()
  93. n=0
  94. for((i = 1; i <= $txt_lines; i++)); do
  95. if [ $n -gt 3 ]; then
  96. n=0
  97. fi
  98. ws_flag=$(sed -n $i'p' $temp_file | cut -b 11-13 | sed 's/^[ \t]*//')
  99. if [ $ws_flag -eq $current_ws ]; then
  100. win_id=$(sed -n $i'p' $temp_file | cut -b 1-10)
  101. if [ $rotate_flag -eq 0 ]; then
  102. if [ $n -eq 0 ]; then
  103. echo $hv_flag','$main_add',' > wm-ctrl.his
  104. echo $win_id'__'$current_ws'_' >> wm-ctrl.his
  105. else
  106. echo $win_id'__'$current_ws'_' >> wm-ctrl.his
  107. fi
  108. fi
  109. if [ $ws_windows -eq 2 ]; then
  110. if [ $n -eq 0 ]; then
  111. if [ $hv_flag = 'v' ]; then
  112. temp_width=$disp_width
  113. temp_height=$disp_height_main
  114. else
  115. temp_width=$disp_width_main
  116. fi
  117. else
  118. if [ $hv_flag = 'v' ]; then
  119. temp_width=$disp_width
  120. temp_height=$disp_height_sub
  121. else
  122. temp_width=$disp_width_sub
  123. temp_height=$((disp_height - panel_h - title_h))
  124. fi
  125. fi
  126. fi
  127. if [ $ws_windows -eq 3 ]; then
  128. if [ $n -eq 0 ]; then
  129. if [ $hv_flag = 'v' ]; then
  130. temp_width=$disp_width
  131. temp_height=$disp_height_main
  132. else
  133. temp_width=$disp_width_main
  134. fi
  135. fi
  136. fi
  137. if [ $ws_windows -gt 2 -a $n -gt 0 ]; then
  138. if [ $hv_flag = 'v' ]; then
  139. temp_width=$disp_width2
  140. temp_height=$disp_height_sub
  141. else
  142. temp_width=$disp_width_sub
  143. temp_height=$disp_height2
  144. fi
  145. fi
  146. if [ $ws_windows -gt 3 -a $n -gt 0 ]; then
  147. x_pos=(0 $pos_width $pos_width 0)
  148. y_pos=(0 0 $pos_height $pos_height)
  149. temp_width=$disp_width2
  150. temp_height=$disp_height2
  151. fi
  152. window_array+=( $win_id','${x_pos[$n]}','${y_pos[$n]}','$temp_width','$temp_height )
  153. let n++
  154. fi
  155. done
  156.  
  157. for par in ${window_array[@]}; do
  158. for((i = 1; i <= 5; i++)); do
  159. declare par$i=$(echo ${par} | cut -f$i -d ',')
  160. done
  161. wmctrl -i -r $par1 -e 0,$par2,$par3,$par4,$par5
  162. done
  163. fi
  164.  
  165. if [ $rotate_flag -gt 0 ]; then
  166. sed -i 's/ /_/g' ./wm-ctrl.his
  167. temp=$hv_flag','$main_add','
  168. sed -i '1i '$temp'' ./wm-ctrl.his
  169. fi

2016年6月5日日曜日

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

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


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

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

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

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

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

  1. #!/bin/bash
  2.  
  3. export test00='
  4. echo $USER
  5. '
  6. export SERVO0='50'
  7. export SERVO1='50'
  8. export SERVO2='50'
  9. export SERVO3='50'
  10. export SERVO4='50'
  11. export SERVO5='50'
  12. export SERVO6='50'
  13. export SERVO7='50'
  14.  
  15. export MAIN_DIALOG='
  16. <window>
  17. <vbox>
  18. <hbox>
  19. <vbox>
  20. <hbox>
  21. <frame サーボ5(%)>
  22. <vscale height-request="200" range-min="10" range-max="90" range-step="1">
  23. <default>50</default>
  24. <variable>SERVO5</variable>
  25. <action>bash -c "echo 5="$SERVO5"% > /dev/servoblaster"</action>
  26. <action type="refresh">TEXT5</action>
  27. </vscale>
  28. </frame>
  29. <frame サーボ4(%)>
  30. <vscale height-request="200" range-min="10" range-max="90" range-step="1">
  31. <default>50</default>
  32. <variable>SERVO4</variable>
  33. <action>bash -c "echo 4="$SERVO4"% > /dev/servoblaster"</action>
  34. <action type="refresh">TEXT4</action>
  35. </vscale>
  36. </frame>
  37. </hbox>
  38. <hbox>
  39. <frame サーボ7(%)>
  40. <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
  41. <default>50</default>
  42. <variable>SERVO7</variable>
  43. <action>bash -c "echo 7="$SERVO7"% > /dev/servoblaster"</action>
  44. <action type="refresh">TEXT7</action>
  45. </vscale>
  46. </frame>
  47. <frame サーボ6(%)>
  48. <vscale height-request="200" range-min="10" range-max="90" range-step="1">
  49. <default>50</default>
  50. <variable>SERVO6</variable>
  51. <action>bash -c "echo 6="$SERVO6"% > /dev/servoblaster"</action>
  52. <action type="refresh">TEXT6</action>
  53. </vscale>
  54. </frame>
  55. </hbox>
  56. </vbox>
  57.  
  58. <vbox>
  59. <hbox>
  60. <button>
  61. <label>ボタン 1</label>
  62. <action>echo $LANG</action>
  63. </button>
  64. <vbox>
  65. <button width-request="110">
  66. <label>前進</label>
  67. <action>bash -c "$test00"</action>
  68. </button>
  69. <button>
  70. <label>後退</label>
  71. </button>
  72. </vbox>
  73. <button>
  74. <label>ボタン 4</label>
  75. </button>
  76. </hbox>
  77. <vbox>
  78. <frame>
  79. <text>
  80. <variable>TEXT0</variable>
  81. <input>echo "echo 0="$SERVO0"% > /dev/servoblaster"</input>
  82. </text>
  83. <text>
  84. <variable>TEXT1</variable>
  85. <input>echo "echo 1="$SERVO1"% > /dev/servoblaster"</input>
  86. </text>
  87. <text>
  88. <variable>TEXT2</variable>
  89. <input>echo "echo 2="$SERVO2"% > /dev/servoblaster"</input>
  90. </text>
  91. <text>
  92. <variable>TEXT3</variable>
  93. <input>echo "echo 3="$SERVO3"% > /dev/servoblaster"</input>
  94. </text>
  95. <text>
  96. <variable>TEXT4</variable>
  97. <input>echo "echo 4="$SERVO4"% > /dev/servoblaster"</input>
  98. </text>
  99. <text>
  100. <variable>TEXT5</variable>
  101. <input>echo "echo 5="$SERVO5"% > /dev/servoblaster"</input>
  102. </text>
  103. <text>
  104. <variable>TEXT6</variable>
  105. <input>echo "echo 6="$SERVO6"% > /dev/servoblaster"</input>
  106. </text>
  107. <text>
  108. <variable>TEXT7</variable>
  109. <input>echo "echo 7="$SERVO7"% > /dev/servoblaster"</input>
  110. </text>
  111. </frame>
  112. </vbox>
  113. <entry>
  114. <variable>FILE0</variable>
  115. <default>"./temp.txt"</default>
  116. </entry>
  117. <button>
  118. <label>新規作成</label>
  119. <action>echo "#!/bin/bash" > $FILE0</action>
  120. <action>echo "" >> $FILE0</action>
  121. <action>echo "echo 0="$SERVO0"% > /dev/servoblaster" >> $FILE0</action>
  122. <action>echo "echo 1="$SERVO1"% > /dev/servoblaster" >> $FILE0</action>
  123. <action>echo "echo 2="$SERVO2"% > /dev/servoblaster" >> $FILE0</action>
  124. <action>echo "echo 3="$SERVO3"% > /dev/servoblaster" >> $FILE0</action>
  125. <action>echo "echo 4="$SERVO4"% > /dev/servoblaster" >> $FILE0</action>
  126. <action>echo "echo 5="$SERVO5"% > /dev/servoblaster" >> $FILE0</action>
  127. <action>echo "echo 6="$SERVO6"% > /dev/servoblaster" >> $FILE0</action>
  128. <action>echo "echo 7="$SERVO7"% > /dev/servoblaster" >> $FILE0</action>
  129. </button>
  130. <button>
  131. <label>ポーズ追加</label>
  132. <action>echo "" >> $FILE0</action>
  133. <action>echo "sleep 1" >> $FILE0</action>
  134. <action>echo "" >> $FILE0</action>
  135. <action>echo "echo 0="$SERVO0"% > /dev/servoblaster" >> $FILE0</action>
  136. <action>echo "echo 1="$SERVO1"% > /dev/servoblaster" >> $FILE0</action>
  137. <action>echo "echo 2="$SERVO2"% > /dev/servoblaster" >> $FILE0</action>
  138. <action>echo "echo 3="$SERVO3"% > /dev/servoblaster" >> $FILE0</action>
  139. <action>echo "echo 4="$SERVO4"% > /dev/servoblaster" >> $FILE0</action>
  140. <action>echo "echo 5="$SERVO5"% > /dev/servoblaster" >> $FILE0</action>
  141. <action>echo "echo 6="$SERVO6"% > /dev/servoblaster" >> $FILE0</action>
  142. <action>echo "echo 7="$SERVO7"% > /dev/servoblaster" >> $FILE0</action>
  143. </button>
  144. </vbox>
  145.  
  146. <vbox>
  147. <hbox>
  148. <frame サーボ0(%)>
  149. <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
  150. <default>50</default>
  151. <variable>SERVO0</variable>
  152. <action>bash -c "echo 0="$SERVO0"% > /dev/servoblaster"</action>
  153. <action type="refresh">TEXT0</action>
  154. </vscale>
  155. </frame>
  156. <frame サーボ1(%)>
  157. <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
  158. <default>50</default>
  159. <variable>SERVO1</variable>
  160. <action>bash -c "echo 1="$SERVO1"% > /dev/servoblaster"</action>
  161. <action type="refresh">TEXT1</action>
  162. </vscale>
  163. </frame>
  164. </hbox>
  165. <hbox>
  166. <frame サーボ2(%)>
  167. <vscale height-request="200" range-min="10" range-max="90" range-step="1" inverted="true">
  168. <default>50</default>
  169. <variable>SERVO2</variable>
  170. <action>bash -c "echo 2="$SERVO2"% > /dev/servoblaster"</action>
  171. <action type="refresh">TEXT2</action>
  172. </vscale>
  173. </frame>
  174. <frame サーボ3(%)>
  175. <vscale height-request="200" range-min="10" range-max="90" range-step="1">
  176. <default>50</default>
  177. <variable>SERVO3</variable>
  178. <action>bash -c "echo 3="$SERVO3"% > /dev/servoblaster"</action>
  179. <action type="refresh">TEXT3</action>
  180. </vscale>
  181. </frame>
  182. </hbox>
  183. </vbox>
  184. </hbox>
  185. </vbox>
  186. </window>
  187. '
  188.  
  189. gtkdialog -p MAIN_DIALOG -c