box_splash 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/bin/ash
  2. #splash window - compatible with yaf-splash
  3. #built upon gtkdialog-splash, written by mave, june 2010.
  4. #other contributors: BarryK, shinobar, 01micko
  5. #gpl license see: /usr/share/doc/legal/gpl-2.0.txt
  6. #Mar 2014, zigbert
  7. [ ! -f /tmp/yaf-splash ] && ln -s "`which gtkdialog`" /tmp/yaf-splash
  8. ROOTGEOM="`xwininfo -root | grep -o ' \\-geometry .*' | cut -f 3 -d ' '`" #ex: 1280x800+0+0
  9. ROOTX=`echo -n "$ROOTGEOM" | cut -f 1 -d 'x'`
  10. ROOTY=`echo -n "$ROOTGEOM" | cut -f 1 -d '+' | cut -f 2 -d 'x'`
  11. MAX_CHAR_WIDTH=40 #Maximum number of chars in one line
  12. MARGIN_DESKTOP=40 #WM bars could conflict if pushing splashes all into the corner
  13. helptext="usage: /path/box_splash [OPTIONS] [-timeout SEC] -text TEXT | -kill PID
  14. -text \"EXPRESSION\"
  15. -timeout COUNT (in seconds)
  16. -icon GTK-XXX (for example: gtk-info all gtk-stock-symbols, default: none)
  17. OR path/to/pixmap.png|gif|jpeg|svg
  18. -icon_width WIDTH in pixels
  19. -bg COLOR (background color red, blue, yellow..., default: grey)
  20. -bg_gradient true|false (default: true)
  21. -fg COLOR (font color, default: black)
  22. -placement center|mouse|top|bottom|top-left|bottom-left (default: center)
  23. -close never|mouseover|box (default is mouseover)
  24. -deco TITLE (shows windows decorations, with title)
  25. -font \"NAME\"
  26. -fontsize SIZE
  27. -align ALIGNMENT left, right or center
  28. -margin SIZE (default: 10)
  29. -border true|false (default: true)
  30. -kill PID (process ID called before:
  31. PID=0 auto-search for the last one,
  32. PID=xxxxx kill the last one read by PID=\$!"
  33. BasePIDFile="/tmp/GTKDIALOG-SPLASH"
  34. close='mouseover'
  35. text=""
  36. timeout=0
  37. icon=""
  38. font="DejaVu bold"
  39. fontsize="10"
  40. margin=10
  41. border=true
  42. pid=""
  43. placement='center'
  44. deco='false'
  45. title='box_splash'
  46. ICON_WIDTH=32
  47. ALIGN=0 #left
  48. #now, let's see if the default colors is set in config file
  49. [ -s $HOME/.config/ptheme/gtkdialog_active ] && . $HOME/.config/ptheme/gtkdialog_active
  50. [ ! "$BOX_SPLASH_BG" ] && BOX_SPLASH_BG=grey
  51. [ ! "$BOX_SPLASH_BG_GRADIENT" ] && BOX_SPLASH_BG_GRADIENT=true
  52. [ ! "$BOX_SPLASH_FG" ] && BOX_SPLASH_FG=black
  53. bg=$BOX_SPLASH_BG
  54. bg_gradient=$BOX_SPLASH_BG_GRADIENT
  55. fg=$BOX_SPLASH_FG
  56. #parameters
  57. if [ $# = 0 ]; then echo "$helptext"; exit; fi #no parameters
  58. while [ $# != 0 ]; do
  59. I=1
  60. while [ $I -le `echo $# | wc -c` ]; do
  61. case $1 in
  62. -align)
  63. case $2 in
  64. right) ALIGN=1 ;;
  65. center) ALIGN=2 ;;
  66. *) ALIGN=0 ;; #left
  67. esac
  68. shift
  69. ;;
  70. -bg) bg="$2"; shift;;
  71. -bg_gradient) bg_gradient="$2"; shift;;
  72. -border)
  73. border="$2"
  74. [ $border = false ] && notebook_border='show-border="false"'
  75. shift
  76. ;;
  77. -close) close="$2"; shift;;
  78. -deco) title="$2"; shift;;
  79. -fg) fg="$2"; shift;;
  80. -font)
  81. N=$(echo $2 | sed -e 's/^.[^0-9]*//'| cut -b1-2| tr -dc '0-9')
  82. [ "$N" ] && fontsize=''
  83. font="$2"
  84. shift
  85. ;;
  86. -fontsize)
  87. fontsize="$2"
  88. [ $2 = small ] && fontsize="8"
  89. [ $2 = medium ] && fontsize="10"
  90. [ $2 = large ] && fontsize="12"
  91. [ $2 = x-large ] && fontsize="14"
  92. shift
  93. ;;
  94. -h|-help|--help) echo "$helptext"; exit;;
  95. -icon) ICON="$2"; shift;;
  96. -icon_width) ICON_WIDTH="$2"; shift;;
  97. -kill)
  98. pid=$2
  99. if [ ${#pid} -eq -1 ]; then
  100. dlgPID=`ps|grep -w 'GTKDIALOG_SPLASH'`
  101. kill $dlgPID 2>/dev/null
  102. else
  103. TmpFile=$BasePIDFile${pid}
  104. dlgPID="`cat $TmpFile`"
  105. kill $dlgPID 2>/dev/null
  106. rm -f $TmpFile
  107. exit
  108. fi
  109. shift
  110. ;;
  111. -margin) margin="$2"; shift;;
  112. -placement) placement="$2"; shift;;
  113. -text) text="$2"; shift;;
  114. -timeout) timeout="$2"; shift;;
  115. esac
  116. shift
  117. I=$(($I+1))
  118. done
  119. done
  120. #put it here in case command have -icon_width specified after -icon
  121. if [ "$ICON" ]; then
  122. icon="<pixmap width-request=\"$ICON_WIDTH\" space-expand=\"false\" space-fill=\"false\">
  123. <width>$ICON_WIDTH</width>
  124. <input file stock=\"$ICON\"></input>
  125. </pixmap>"
  126. [ "`echo $ICON | grep '^gtk'`" = "" ] && icon="<pixmap width-request=\"$ICON_WIDTH\" space-expand=\"false\" space-fill=\"false\">
  127. <width>$ICON_WIDTH</width>
  128. <input file>\"$ICON\"</input>
  129. </pixmap>"
  130. fi
  131. #define height and width of the window
  132. WIDTH=$(($WIDTH+$margin))
  133. HEIGHT=$(($HEIGHT+$margin))
  134. if [ "$border" = "true" ]; then
  135. WIDTH=$(($WIDTH+10))
  136. HEIGHT=$(($HEIGHT+10))
  137. fi
  138. NR_CHARS="`echo "$text" | wc -c`"
  139. NR_LINES=`awk -v VAR1=$NR_CHARS -v VAR2=$MAX_CHAR_WIDTH 'BEGIN{print int((VAR1/VAR2)+1)}'`
  140. [ $NR_CHARS -gt $MAX_CHAR_WIDTH ] && CHAR_WIDTH=$MAX_CHAR_WIDTH || CHAR_WIDTH=$NR_CHARS
  141. TMP=`awk -v VAR1=$CHAR_WIDTH -v VAR2=$fontsize 'BEGIN{print int(VAR1*(VAR2*1.0))}'`
  142. WIDTH=$(($WIDTH+$TMP))
  143. TMP=`awk -v VAR1=$NR_LINES -v VAR2=$fontsize 'BEGIN{print int(VAR1*(VAR2*2.0))}'`
  144. HEIGHT=$(($HEIGHT+$TMP))
  145. [ $NR_LINES -gt 1 ] && HEIGHT=$(($HEIGHT+$fontsize)) #gtkdialog <text> widgets does not has such accurate wrapping as we calculate. It wraps words, not chars
  146. if [ "$icon" ]; then
  147. WIDTH=$(($WIDTH+$ICON_WIDTH+10))
  148. [ $HEIGHT -lt 62 ] && HEIGHT=62
  149. fi
  150. #process placement, default is center...
  151. WINPLACE=''
  152. WINTYPE=1
  153. case $placement in
  154. mouse) GEOMPARAM=''; WINPLACE='window-position="2"'; WINTYPE=2;;
  155. top) X=$((($ROOTX/2)-($WIDTH/2))); Y=$MARGIN_DESKTOP; WINTYPE=10;;
  156. top-right) X=$(($ROOTX-$WIDTH-$MARGIN_DESKTOP)); Y=$MARGIN_DESKTOP; WINTYPE=10;;
  157. top-left) X=$MARGIN_DESKTOP; Y=$MARGIN_DESKTOP; WINTYPE=10;;
  158. bottom) X=$((($ROOTX/2)-($WIDTH/2))); Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP)); WINTYPE=10;;
  159. bottom-right) X=$(($ROOTX-$WIDTH-$MARGIN_DESKTOP)); Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP)); WINTYPE=10;;
  160. bottom-left) X=$MARGIN_DESKTOP; Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP)); WINTYPE=10;;
  161. esac
  162. #set backgound
  163. if [ $bg_gradient = true ]; then
  164. #build svg-background image
  165. echo '
  166. <svg version="1.1" width="1000" height="1000">
  167. <defs>
  168. <linearGradient id="LG_01" x1="7" y1="80" x2="0" y2="0" gradientUnits="userSpaceOnUse">
  169. <stop style="stop-color:'$bg';stop-opacity:1" offset="0" />
  170. <stop style="stop-color:'$bg';stop-opacity:0.1" offset="1" />
  171. </linearGradient>
  172. </defs>
  173. <rect style="fill:url(#LG_01);fill-opacity:1" width="1000" height="1000"/>
  174. </svg>' > /tmp/gtkdialog_splash_bg.svg #full opacity
  175. echo '
  176. <svg version="1.1" width="1000" height="1000">
  177. <defs>
  178. <linearGradient id="LG_01" x1="7" y1="80" x2="0" y2="0" gradientUnits="userSpaceOnUse">
  179. <stop style="stop-color:'$bg';stop-opacity:1" offset="0" />
  180. <stop style="stop-color:'$bg';stop-opacity:0.1" offset="1" />
  181. </linearGradient>
  182. </defs>
  183. <rect style="fill:url(#LG_01);fill-opacity:0.5;stroke-width:0" width="1000" height="1000"/>
  184. </svg>' > /tmp/gtkdialog_splash_border.svg #a bit transparent
  185. if [ $border = false ]; then
  186. gtk_border='bg_pixmap[NORMAL] = "gtkdialog_splash_bg.svg"'
  187. else
  188. gtk_border='bg_pixmap[NORMAL] = "gtkdialog_splash_border.svg"'
  189. gtk_bg='bg_pixmap[NORMAL] = "gtkdialog_splash_bg.svg"'
  190. fi
  191. else
  192. gtk_bg="bg[NORMAL] = \"$bg\""
  193. gtk_border="bg[NORMAL] = \"$bg\""
  194. fi
  195. #gtk-theme
  196. gtkrc='
  197. pixmap_path "/tmp"
  198. style "gtkdialog-splash"
  199. {
  200. '"$gtk_border"'
  201. }
  202. class "GtkWindow" style "gtkdialog-splash"
  203. style "notebook"
  204. {
  205. '"$gtk_bg"'
  206. xthickness = '"$(($margin+5))"'
  207. ythickness = '$margin'
  208. }
  209. class "GtkNotebook" style "notebook"
  210. style "font"
  211. {
  212. font_name="'"$font"' '"$fontsize"'"
  213. text[NORMAL]="'$fg'"
  214. fg[NORMAL]="'$fg'"
  215. }
  216. class "GtkLabel" style "font"'
  217. echo $gtkrc > /tmp/gtkdialog-splash_gtkrc
  218. export GTK2_RC_FILES=/tmp/gtkdialog-splash_gtkrc
  219. #process close of window, default is mouseover...
  220. CLOSETAG1=''
  221. CLOSETAG2='<action signal="enter-notify-event" type="exit">Exit on mouse-over</action>'
  222. case $close in
  223. never)
  224. CLOSETAG2=''
  225. ;;
  226. box)
  227. CLOSETAG2=''
  228. CLOSETAG1='<vbox><button>'"`/usr/lib/gtkdialog/xml_button-icon cancel`"'<action type="exit">Exit on click close-box</action></button></vbox>'
  229. ;;
  230. esac
  231. #gui
  232. export GTKDIALOG_SPLASH='
  233. <window title="'${title}'" focus-on-map="false" skip-taskbar-hint="true" icon-name="gtk-preferences" resizable="false" decorated="'${deco}'" '${WINPLACE}'>
  234. <vbox>
  235. <notebook show-tabs="false" '"$notebook_border"' space-expand="true" space-fill="true">
  236. <hbox>
  237. <vbox>
  238. '${icon}'
  239. <text space-expand="true" space-fill="true"><label>""</label></text>
  240. </vbox>
  241. <text justify="'${ALIGN}'" width-chars="'${CHAR_WIDTH}'" space-expand="true" space-fill="true">
  242. <label>"'${text}'"</label>
  243. </text>
  244. '${CLOSETAG1}'
  245. </hbox>
  246. </notebook>
  247. </vbox>
  248. '${CLOSETAG2}'
  249. </window>'
  250. case $WINTYPE in
  251. 2) #mouse
  252. [ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH #to keep same pid.
  253. /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH &
  254. ;;
  255. 10)
  256. [ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --geometry="$WIDTH"x"$HEIGHT"+"${X}"+"${Y}" #to keep same pid.
  257. /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --geometry="$WIDTH"x"$HEIGHT"+"${X}"+"${Y}" &
  258. ;;
  259. *) #center
  260. [ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --center #to keep same pid.
  261. /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --center &
  262. ;;
  263. esac
  264. dlgPID=$!
  265. pidPATTERN="^${dlgPID} "
  266. while [ $timeout -ne 0 ];do #100604
  267. sleep 1
  268. ALLPS="`ps`"
  269. timeout=`expr $timeout - 1`
  270. [ "`echo "$ALLPS" | sed -e 's%^ *%%' | grep "$pidPATTERN"`" == "" ] && exit #already killed.
  271. done
  272. kill $dlgPID
  273. echo 'EXIT="Exit on timeout"'