Advertisement
Guest User

icewm remember class fix

a guest
Jun 11th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. #!/bin/bash
  2. # -*- mode:sh -*-
  3. #
  4. # This program is based on BobC's, PPC's and icewm team's effort.
  5. #
  6.  
  7. # localization
  8. TEXTDOMAINDIR=/usr/share/locale
  9. TEXTDOMAIN=icewm-remember-settings
  10.  
  11. #initial values
  12. focus="-f"
  13. select=0
  14. list=0
  15. loop=1
  16.  
  17. # check for extra options
  18. while [[ $# -ge 1 ]]
  19. do
  20. case $1 in
  21. (-s) select=1 ;;
  22. (-l) list=1 ;;
  23. esac
  24. shift
  25. done
  26.  
  27. # locate winoptions
  28. if [[ -d $ICEWM_PRIVCFG ]]
  29. then
  30. winoptions=$ICEWM_PRIVCFG/winoptions
  31. elif [[ -d $XDG_CONFIG_HOME && -d $XDG_CONFIG_HOME/icewm ]]
  32. then
  33. winoptions=$XDG_CONFIG_HOME/icewm/winoptions
  34. elif [[ -d $HOME/.config/icewm ]]
  35. then
  36. winoptions=$HOME/.config/icewm/winoptions
  37. elif [[ -d $HOME/.icewm ]]
  38. then
  39. winoptions=$HOME/.icewm/winoptions
  40. else
  41. echo "$0: Cannot find your icewm config directory" >&2
  42. exit 1
  43. fi
  44.  
  45. #function to call again this program, but having the user click on the specific window
  46. select_window(){
  47. echo "selecting window"
  48. # remove temporary file
  49. rm -f -- "$temp"
  50. #give select option
  51. select=1
  52. focus=
  53. loop=1
  54. }
  55.  
  56. #function to add/remove changes
  57. save_changes(){
  58. # delete all previous data for the program
  59. #delete geometry
  60. sed -i -e "/$cls_geo/d" "$winoptions"
  61. #delete layer
  62. sed -i -e "/$cls_lay/d" "$winoptions"
  63. #delete workspace
  64. sed -i -e "/$cls_wsp/d" "$winoptions"
  65.  
  66. # add new selected information for the program, reading the temp file
  67. while IFS= read -r line
  68. do
  69. # geometry
  70. if [[ "$line" == "geometry" ]]
  71. then
  72. # add new
  73. echo "$new_geo" >> "$winoptions"
  74. fi
  75. # layer
  76. if [[ "$line" == "layer" ]]
  77. then
  78. # add new
  79. echo "$new_lay" >> "$winoptions"
  80. fi
  81. # workspace
  82. if [[ "$line" == "workspace" ]]
  83. then
  84. # add new
  85. echo "$new_wsp" >> "$winoptions"
  86. fi
  87. done < "$temp"
  88.  
  89. # remove temporary file
  90. rm -f -- "$temp"
  91.  
  92. # let icewm reload the winoptions
  93. icesh winoptions
  94.  
  95. #stop select option
  96. select=0
  97.  
  98. #if list not selected, stop loop
  99. if [[ list -eq 0 ]]; then
  100. loop=0
  101. fi
  102. }
  103.  
  104. #launches help window
  105. help_dialog()
  106. {
  107. yad --image="gtk-dialog-info" --height=250 --width=460 --scroll \
  108. --title=$"HELP" --class="IceWM-remember-settings" --name="help" --borders=10 --center \
  109. --form --field=$"Save the <b>size and position</b>, <b>workspace</b> \
  110. and <b>layer</b> of a window using the IceWM-remember-settings app.":LBL '' \
  111. --field=$"Next time you launch the program, it will remember the \
  112. window properties last saved.":LBL '' \
  113. --field=$"You can also delete this information unticking all options.":LBL '' \
  114. --field=$"Use the <b>Select other</b> option to select a different \
  115. window/program to configure.":LBL '' \
  116. --field=$"You cannot select minimized windows.":LBL '' --separator="" \
  117. --button=gtk-quit:0 --buttons-layout=center
  118. }
  119.  
  120. #launches list dialog
  121. list_dialog(){
  122. #List names of all currently open windows (we may have to exclude Conky from the list) - Original version over at: https://pastebin.com/13em3H11
  123. wmctrl -l|awk '{$3=""; $2=""; $1=""; print $0}' > /tmp/windowlist.txt
  124. #Window to be exluded from the list:
  125. #exclude_from_window_list="Conky (dhc"
  126. grep -v "Conky (" /tmp/windowlist.txt > /tmp/windowlist2.txt; mv /tmp/windowlist2.txt /tmp/windowlist.txt
  127. # Use Yad to select window to "Remeber":
  128. selection=$(yad --class="IceWM-remember-settings" --name="list" --title=$"Add/Remove IceWM Window Defaults" \
  129. --width=550 --height=400 --borders=20 --text=$"<b>Select a non minimezed program. Store it's window properties.</b>" \
  130. --text-align=center --center --separator=" " --list --column=$"What window configuration you want iceWM to remember/forget?" \
  131. --button=gtk-ok:0 --button=gtk-quit:1 < /tmp/windowlist.txt)
  132. #make the window the user selected the active one:
  133. wmctrl -R $selection
  134. # if nothing was selected simply exit
  135. [ -z "$selection" ] && loop=0 && echo "nothing was selected" && exit
  136. }
  137.  
  138. main_dialog(){
  139.  
  140. #restart data
  141. geo=
  142. x=
  143. y=
  144. left=
  145. top=
  146. class=
  147. layer=
  148. work=
  149. appname=
  150. appclass=
  151. appwork=
  152. appgeo=
  153.  
  154. # create temporary file
  155. temp=$(mktemp)
  156.  
  157. # obtain window settings
  158. icesh $focus getGeometry getLayer getWorkspace \
  159. prop WM_CLASS prop _NET_FRAME_EXTENTS >"$temp"
  160.  
  161. # file must have something
  162. if [[ ! -s $temp ]]
  163. then
  164. exit 1
  165. fi
  166.  
  167. # remove punctuation
  168. sed -i -e 's|[+,]| |g' "$temp"
  169.  
  170. ## read values from temp file
  171. while read line; do
  172. if [[ "$line" == "0x"* ]]; then
  173. #Window class information
  174. if [[ $(echo "$line" | awk '{print $2}') == "WM_CLASS"* ]]; then
  175. #echo "$line" | cut -d " " -f 4-
  176. class=$(echo "$line" | cut -d " " -f 4-)
  177. elif [[ $(echo "$line" | awk '{print $2}') == "_NET_FRAME_EXTENTS"* ]]; then
  178. left=$(echo "$line" | cut -d " " -f 4)
  179. top=$(echo "$line" | cut -d " " -f 8)
  180. #workspace number and name
  181. elif [[ $(echo "$line" | awk '{print $2}') =~ ^[0-9]+$ || $(echo "$line" | awk '{print $3}') = '"All"' ]]; then
  182. work=$(echo "$line" | cut -d " " -f 2)
  183. appwork=$(echo "$line" | cut -d " " -f 3-)
  184. #remove " character
  185. appwork=$(echo "$appwork" | sed -e 's|["]||g')
  186. #layer information
  187. elif [[ $(echo "$line" | awk '{print $2}') =~ ^[A-Z][a-z] ]]; then
  188. layer=$(echo "$line" | cut -d " " -f 2)
  189. fi
  190. #geometry information
  191. elif [[ $(echo "$line" | awk '{print $1}') =~ ^[0-9] ]]; then
  192. geo=$(echo "$line" | awk '{print $1}')
  193. x=$(echo "$line" | awk '{print $2}')
  194. y=$(echo "$line" | awk '{print $3}')
  195. fi
  196. done <"$temp"
  197.  
  198. # remove temporary file
  199. rm -f -- "$temp"
  200.  
  201. #initial values for window and class name
  202. alldots=$(echo "${class}" | awk -F. '{print NF-1}') #number of dots inside string
  203. appclass=${class%.*} #delete last dot
  204. appname=$appclass
  205. #determine number of steps to process class string
  206. if [[ ${alldots} -lt 2 ]]; then
  207. exit #classname is incomplete
  208. elif [[ $((alldots%2)) -ne 0 ]]; then
  209. #is not pair, add one
  210. alldots=$alldots+1
  211. fi
  212. #number of times to operate to get name and class variables
  213. alldots=$((alldots / 2))
  214. #process class string to get the name
  215. for i in $(seq 1 $alldots); do
  216. appclass=${appclass%.*} #get window class
  217. appname=${appname#*.} #get window name
  218. done
  219.  
  220. # correct geometry
  221. let x-=$left
  222. let y-=$top
  223. appgeo="${geo}+${x}+${y}"
  224.  
  225. cls_geo="${class}geometry"
  226. new_geo="${cls_geo}: ${geo}+${x}+${y}"
  227.  
  228. cls_lay="${class}layer"
  229. new_lay="${cls_lay}: ${layer}"
  230.  
  231. cls_wsp="${class}workspace"
  232. new_wsp="${cls_wsp}: ${work}"
  233.  
  234. #recreate temp file for yad script
  235. temp=$(mktemp)
  236.  
  237. # main yad dialog
  238. yad --title=$"Add/Remove IceWM Window Defaults" --class="IceWM-remember-settings" --name="IceWM-remember-settings" \
  239. --text=$"Entries shown below are for the <b>$appclass</b> ($appname) window.\n\n\
  240. All options marked will be saved, all unmarked will be deleted.\n" \
  241. --center --borders=20 --checklist --list --separator="" \
  242. --column=$"Select" --column="Entry" --column=$"Type" --column=$"Value" \
  243. true "geometry" $"Geometry" "$appgeo" \
  244. true "layer" $"Layer" "$layer" true $"workspace" "Workspace" "$appwork" >"$temp" \
  245. --hide-column=2 --print-column=2 --height=320 \
  246. --button=gtk-help:"bash -c help_dialog" --button=$"Select other":2 --button=gtk-ok:0 --button=gtk-quit:1
  247.  
  248. ##SELECTION MADE##
  249. exval=$?
  250. case $exval in
  251. 0) save_changes;;
  252. 2) select_window;;
  253. 1) loop=0 ;;
  254. 252) loop=0 ;;
  255. esac
  256.  
  257. # remove temporary file
  258. rm -f -- "$temp"
  259. }
  260.  
  261. ###SCRIPT STARTS HERE###
  262.  
  263. #ready help window
  264. export -f help_dialog
  265.  
  266. while [[ $loop -eq 1 ]]; do
  267. if [[ $select -eq 1 ]]
  268. then
  269. focus=
  270. main_dialog
  271. elif [[ $list -eq 1 ]]
  272. then
  273. focus="-f"
  274. list_dialog
  275. main_dialog
  276. else
  277. loop=0
  278. focus="-f"
  279. main_dialog
  280. fi
  281. done
  282.  
  283. #clean extra yad windows
  284. unset help_dialog
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement