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

0 件のコメント :

コメントを投稿