My Awesome Config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
7.9KB

  1. #!/bin/bash
  2. # lightsOn.sh
  3. # Copyright (c) 2011 iye.cba at gmail com
  4. # url: https://github.com/iye/lightsOn
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # Description: Bash script that prevents the screensaver and display power
  7. # management (DPMS) to be activated when you are watching Flash Videos
  8. # fullscreen on Firefox and Chromium.
  9. # Can detect mplayer and VLC when they are fullscreen too but I have disabled
  10. # this by default.
  11. # lightsOn.sh needs xscreensaver, kscreensaver or gnome-screensaver to work.
  12. # HOW TO USE: Start the script with the number of seconds you want the checks
  13. # for fullscreen to be done. Example:
  14. # "./lightsOn.sh 120 &" will Check every 120 seconds if Mplayer,
  15. # VLC, Firefox or Chromium are fullscreen and delay screensaver and Power Management if so.
  16. # You want the number of seconds to be ~10 seconds less than the time it takes
  17. # your screensaver or Power Management to activate.
  18. # If you don't pass an argument, the checks are done every 50 seconds.
  19. # Modify these variables if you want this script to detect if Mplayer,
  20. # VLC or Firefox Flash Video are Fullscreen and disable
  21. # xscreensaver/kscreensaver/gnome-screensaver and PowerManagement.
  22. mplayer_detection=0
  23. vlc_detection=0
  24. firefox_flash_detection=1
  25. chromium_flash_detection=1
  26. html5_detection=1 #checks if the browser window is fullscreen; will disable the screensaver if the browser window is in fullscreen so it doesn't work correctly if you always use the browser (Firefox or Chromium) in fullscreen
  27. # YOU SHOULD NOT NEED TO MODIFY ANYTHING BELOW THIS LINE
  28. # enumerate all the attached screens
  29. displays=""
  30. while read id
  31. do
  32. displays="$displays $id"
  33. done< <(xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p')
  34. # Detect screensaver been used (xscreensaver, kscreensaver, gnome-screensaver or none)
  35. if [ `pgrep -l xscreensaver | grep -wc xscreensaver` -ge 1 ];then
  36. screensaver=xscreensaver
  37. elif [ `pgrep -l gnome-screensav | grep -wc gnome-screensav` -ge 1 ];then
  38. screensaver=gnome-screensav
  39. elif [ `pgrep -l kscreensaver | grep -wc kscreensaver` -ge 1 ];then
  40. screensaver=kscreensaver
  41. else
  42. screensaver=None
  43. echo "No screensaver detected"
  44. fi
  45. checkFullscreen()
  46. {
  47. # loop through every display looking for a fullscreen window
  48. for display in $displays
  49. do
  50. #get id of active window and clean output
  51. activ_win_id=`DISPLAY=$DISPLAY.${display} xprop -root _NET_ACTIVE_WINDOW`
  52. #activ_win_id=${activ_win_id#*# } #gives error if xprop returns extra ", 0x0" (happens on some distros)
  53. activ_win_id=${activ_win_id:40:9}
  54. # Skip invalid window ids (commented as I could not reproduce a case
  55. # where invalid id was returned, plus if id invalid
  56. # isActivWinFullscreen will fail anyway.)
  57. #if [ "$activ_win_id" = "0x0" ]; then
  58. # continue
  59. #fi
  60. # Check if Active Window (the foremost window) is in fullscreen state
  61. isActivWinFullscreen=`DISPLAY=$DISPLAY.${display} xprop -id $activ_win_id | grep _NET_WM_STATE_FULLSCREEN`
  62. if [[ "$isActivWinFullscreen" = *NET_WM_STATE_FULLSCREEN* ]];then
  63. isAppRunning
  64. var=$?
  65. if [[ $var -eq 1 ]];then
  66. delayScreensaver
  67. fi
  68. fi
  69. done
  70. }
  71. # check if active windows is mplayer, vlc or firefox
  72. #TODO only window name in the variable activ_win_id, not whole line.
  73. #Then change IFs to detect more specifically the apps "<vlc>" and if process name exist
  74. isAppRunning()
  75. {
  76. #Get title of active window
  77. activ_win_title=`xprop -id $activ_win_id | grep "WM_CLASS(STRING)"` # I used WM_NAME(STRING) before, WM_CLASS more accurate.
  78. # Check if user want to detect Video fullscreen on Firefox, modify variable firefox_flash_detection if you dont want Firefox detection
  79. if [ $firefox_flash_detection == 1 ];then
  80. if [[ "$activ_win_title" = *unknown* || "$activ_win_title" = *plugin-container* ]];then
  81. # Check if plugin-container process is running
  82. flash_process=`pgrep -l plugin-containe | grep -wc plugin-containe`
  83. #(why was I using this line avobe? delete if pgrep -lc works ok)
  84. #flash_process=`pgrep -lc plugin-containe`
  85. if [[ $flash_process -ge 1 ]];then
  86. return 1
  87. fi
  88. fi
  89. fi
  90. # Check if user want to detect Video fullscreen on Chromium, modify variable chromium_flash_detection if you dont want Chromium detection
  91. if [ $chromium_flash_detection == 1 ];then
  92. if [[ "$activ_win_title" = *exe* ]];then
  93. # Check if Chromium Flash process is running
  94. if [[ `pgrep -lfc "chromium-browser --type=plugin --plugin-path=/usr/lib/adobe-flashplugin"` -ge 1 || `pgrep -lfc "chromium-browser --type=plugin --plugin-path=/usr/lib/flashplugin-installer"` -ge 1 ]];then
  95. return 1
  96. fi
  97. fi
  98. fi
  99. #html5 (Firefox or Chromium full-screen)
  100. if [ $html5_detection == 1 ];then
  101. if [[ "$activ_win_title" = *chromium-browser* || "$activ_win_title" = *Firefox* ]];then
  102. #check if firefox or chromium is running.
  103. if [[ `pgrep -l firefox | grep -wc firefox` -ge 1 || `pgrep -l chromium-browse | grep -wc chromium-browse` -ge 1 ]]; then
  104. return 1
  105. fi
  106. fi
  107. fi
  108. #check if user want to detect mplayer fullscreen, modify variable mplayer_detection
  109. if [ $mplayer_detection == 1 ];then
  110. if [[ "$activ_win_title" = *mplayer* || "$activ_win_title" = *MPlayer* ]];then
  111. #check if mplayer is running.
  112. #mplayer_process=`pgrep -l mplayer | grep -wc mplayer`
  113. mplayer_process=`pgrep -lc mplayer`
  114. if [ $mplayer_process -ge 1 ]; then
  115. return 1
  116. fi
  117. fi
  118. fi
  119. # Check if user want to detect vlc fullscreen, modify variable vlc_detection
  120. if [ $vlc_detection == 1 ];then
  121. if [[ "$activ_win_title" = *vlc* ]];then
  122. #check if vlc is running.
  123. #vlc_process=`pgrep -l vlc | grep -wc vlc`
  124. vlc_process=`pgrep -lc vlc`
  125. if [ $vlc_process -ge 1 ]; then
  126. return 1
  127. fi
  128. fi
  129. fi
  130. return 0
  131. }
  132. delayScreensaver()
  133. {
  134. # reset inactivity time counter so screensaver is not started
  135. if [ "$screensaver" == "xscreensaver" ]; then
  136. #This tells xscreensaver to pretend that there has just been user activity. This means that if the screensaver is active (the screen is blanked), then this command will cause the screen to un-blank as if there had been keyboard or mouse activity. If the screen is locked, then the password dialog will pop up first, as usual. If the screen is not blanked, then this simulated user activity will re-start the countdown (so, issuing the -deactivate command periodically is one way to prevent the screen from blanking.)
  137. xscreensaver-command -deactivate > /dev/null
  138. elif [ "$screensaver" == "gnome-screensav" ]; then
  139. dbus-send --session --type=method_call --dest=org.gnome.ScreenSaver --reply-timeout=20000 /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity > /dev/null
  140. elif [ "$screensaver" == "kscreensaver" ]; then
  141. qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity > /dev/null
  142. fi
  143. #Check if DPMS is on. If it is, deactivate and reactivate again. If it is not, do nothing.
  144. dpmsStatus=`xset -q | grep -ce 'DPMS is Enabled'`
  145. if [ $dpmsStatus == 1 ];then
  146. xset -dpms
  147. xset dpms
  148. fi
  149. }
  150. delay=$1
  151. # If argument empty, use 50 seconds as default.
  152. if [ -z "$1" ];then
  153. delay=50
  154. fi
  155. # If argument is not integer quit.
  156. if [[ $1 = *[^0-9]* ]]; then
  157. echo "The Argument \"$1\" is not valid, not an integer"
  158. echo "Please use the time in seconds you want the checks to repeat."
  159. echo "You want it to be ~10 seconds less than the time it takes your screensaver or DPMS to activate"
  160. exit 1
  161. fi
  162. while true
  163. do
  164. checkFullscreen
  165. sleep $delay
  166. done
  167. exit 0