2016年7月3日日曜日

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

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


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

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