Turn off all monitors while watching VLC media on TVUbuntu 18.04 brightness commandSwitch to external monitor by default when it is plugged inErratic display behaviour on 12.04 using an Asus Zenbooktv connected, laptop monitor should turn offForcing grub menu to external monitor connected to laptop via HDMI/VGAUse only one monitor at a time?Grub menu to second monitorUbuntu 16.04 and three monitors - doesn't workCheck if HDMI port is damagedConnect Laptop to External MonitorMonitor malfunctions if plugged before boot after Ubuntu 18.10 upgrade

Misrepresented my work history

Distinguish the explanations of Galadriel's test in LotR

What are the original Russian words for a prostitute?

Data Encryption by Application vs Data Encryption in Database

WTB Horizon 47c - small crack in the middle of the tire

How do we handle pauses in a dialogue?

Can I play a mimic PC?

How to tell someone I'd like to become friends without letting them think I'm romantically interested in them?

Does a wizard need their hands free in order to cause their familiar from the Find Familiar spell to reappear?

How do you move up one folder in Finder?

Why did Old English lose both thorn and eth?

Is it okay to roll multiple attacks that all have advantage in one cluster?

Is this a reference to the film Alien in the novel 2010 Odyssey Two?

The three greedy pirates

Why did Harry Potter get a bedroom?

"was fiction" vs "were fictions"

Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?

Why does wrapping Aluminium foil around my food help it keep warm, aluminium be good conductor should have no effect?

How to know if blackberries are safe to eat

Can Jimmy hang on his rope?

Found and corrected a mistake on someone's else paper -- praxis?

Why different specifications for telescopes and binoculars?

Why is the ladder of the LM always in the dark side of the LM?

Is there any reason why MCU changed the Snap to Blip



Turn off all monitors while watching VLC media on TV


Ubuntu 18.04 brightness commandSwitch to external monitor by default when it is plugged inErratic display behaviour on 12.04 using an Asus Zenbooktv connected, laptop monitor should turn offForcing grub menu to external monitor connected to laptop via HDMI/VGAUse only one monitor at a time?Grub menu to second monitorUbuntu 16.04 and three monitors - doesn't workCheck if HDMI port is damagedConnect Laptop to External MonitorMonitor malfunctions if plugged before boot after Ubuntu 18.10 upgrade






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








9















Sometimes I like to watch VLC media on my TV connected to the HDMI port on my laptop and I now have to:



  • Start VLC on the TV:

    • Positioned on the TV by CCSM.

    • The screen saver is being turned off by VLC.


  • Close the laptop

  • Turn off the monitor (If it's on)

because I like to sit in the dark while watching TV!



Has anyone solved the above already for themselves through a script?



The TV is always connected to HDMI and the monitor always to the VGA port if that makes things easier.










share|improve this question






























    9















    Sometimes I like to watch VLC media on my TV connected to the HDMI port on my laptop and I now have to:



    • Start VLC on the TV:

      • Positioned on the TV by CCSM.

      • The screen saver is being turned off by VLC.


    • Close the laptop

    • Turn off the monitor (If it's on)

    because I like to sit in the dark while watching TV!



    Has anyone solved the above already for themselves through a script?



    The TV is always connected to HDMI and the monitor always to the VGA port if that makes things easier.










    share|improve this question


























      9












      9








      9


      3






      Sometimes I like to watch VLC media on my TV connected to the HDMI port on my laptop and I now have to:



      • Start VLC on the TV:

        • Positioned on the TV by CCSM.

        • The screen saver is being turned off by VLC.


      • Close the laptop

      • Turn off the monitor (If it's on)

      because I like to sit in the dark while watching TV!



      Has anyone solved the above already for themselves through a script?



      The TV is always connected to HDMI and the monitor always to the VGA port if that makes things easier.










      share|improve this question
















      Sometimes I like to watch VLC media on my TV connected to the HDMI port on my laptop and I now have to:



      • Start VLC on the TV:

        • Positioned on the TV by CCSM.

        • The screen saver is being turned off by VLC.


      • Close the laptop

      • Turn off the monitor (If it's on)

      because I like to sit in the dark while watching TV!



      Has anyone solved the above already for themselves through a script?



      The TV is always connected to HDMI and the monitor always to the VGA port if that makes things easier.







      vlc external-monitor tv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 1 at 21:47







      Fabby

















      asked Jul 1 at 0:06









      FabbyFabby

      28.2k17 gold badges68 silver badges171 bronze badges




      28.2k17 gold badges68 silver badges171 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          10














          I've created a script that slowly dims two monitors after 5 minutes of keyboard or mouse inactivity while you watch a movie on a third monitor. When you move your mouse or use your keyboard the two dimmed monitors quickly brighten (but not suddenly to shock you).





          Save the script below to a filename in your path such as /usr/local/bin/movie.sh or /home/your_user_name/bin/movie.sh and then use:



          chmod a+x /path/to/script/movie.sh


          Xorg's package xprintidle is required to track how long it's been since a mouse movement or keyboard activity:



          sudo apt install xprintidle


          Then to launch the script use:



          movie.sh


          To learn more about the script and how it works use:



          movie.sh anyparameter


          and debug / trace statements are displayed in your terminal.



          Note: When testing, change TIME_TO_FADE from 310000 to 10000 so you only have to wait 10 seconds instead of 5 minutes for fading to start.



          The script



          #!/bin/bash

          # NAME: movie.sh
          # PATH: ~/eyesome
          # DESC: Dim monitors 1 and 3 after 5 minutes of activity
          # DATE: June 27, 2019. Modified June 30, 2019.
          # PARM: Pass any parameter 1 to generate trace output
          # NOTE: No notes.

          # TODO: Increase limit of 3 with array with up to 9 monitor names, eg use:
          # XrandrNames=( "eDP-1-1" "" "DP-1-1" "HDMI-1-1" ) # Gives 4 monitors


          : <<'END'
          /* -----------------------------------------------------------------------------

          CUSTOM NOTES
          ============
          Put your own free form notes here for disabling third party auto-brightness
          sunrise / sunset applications like nightlight, redshift, flux, eyesome, etc.

          CONFIGURATION FILE LOCATION
          ===========================
          $ ll /usr/local/bin/.eyesome-cfg
          -rw-r--r-- 1 root root 610 Jun 27 18:31 /usr/local/bin/.eyesome-cfg

          CONFIGURATION FILE RECORD LAYOUT
          ================================
          $ cat /usr/local/bin/.eyesome-cfg
          https://www.timeanddate.com/sun/canada/edmonton|70.000000|120.000000|90.000000|
          1.000000|TRUE| | | | |1|Disabled|Hardware|Alien 17" Builtin DIsplay|
          intel_backlight|eDP-1-1|3500.000000|1.000000|1.000000|1.000000|850.000000|
          1.000000|0.780000|0.650000|3500|1.000000:1.000000:1.000000| | | | |2|Enabled|
          Software|Sony 50" TV|xrandr|HDMI-0|1.000000|1.000000|1.000000|1.000000|0.750000|
          1.000000|0.950000|0.850000|1.00|1.000000:1.000000:1.000000| | | | |3|Disabled|
          Software|Toshibal 43" 4K TV|xrandr|DP-1-1|1.000000|1.000000|1.000000|1.000000|
          0.650000|1.000000|0.880000|0.780000|1.00|1.000000:1.000000:1.000000| | | |

          ----------------------------------------------------------------------------- */
          END

          # If sudo powers not needed, eg no 3rd party auto-brightening to disable,
          # then comment out the next four lines:
          if [[ $(id -u) != 0 ]]; then # root powers needed to call this script
          echo >&2 "$0 must be called with sudo powers"
          exit 1
          fi

          # To echo debug/trace statements to screen, pass any parameter to script
          Debug=0
          [[ $# -gt 0 ]] && Debug=1

          # To get list of your monitors use: xrandr --verbose | grep " connected"
          MON1="eDP-1-1" # eDP-1-1 = Laptop display
          MON2="" # HDMI-0 = Sony TV
          MON3="DP-1-1" # DP-1-1 = Toshiba TV

          # If you elect to turn backlight complete off and on you must use sudo
          BACK="intel_backlight" # Setting to blank means no backlight to turn off/on
          BACK="" # After xrandr dims backlight will stay on

          # All seconds in millisconds, eg 1000 milliseconds = 1 second
          TIME_TO_FADE=310000 # Keyboard/Mouse idle time before dim 300000 = 5 minutes
          TIME_TO_WAKE=2000 # 2 second time or less to trigger brightening
          FADE_SLEEP=.15 # When fading sleep seconds between monitor changes
          WAKE_SLEEP=.025 # When waking sleep seconds between monitor changes

          # Optional configuration file for 3rd party auto brightnes / color temperature
          CFG_FILE=""
          # if nothing else is automatically controlling monitors comment out next line
          #CFG_FILE=/usr/local/bin/.eyesome-cfg

          DisableAutoBright () Disabled/g' "$CFG_FILE"
          [[ $MON2 != "" ]] && sed -i 's/2 # DisableAutoBright

          EnableAutoBright () Enabled/g' "$CFG_FILE"
          [[ $MON3 != "" ]] && sed -i 's/3 # EnableAutoBright

          MonArr=() # Index 0= Monitor # 1 to 3, 1=Xrandr Name, 2=Connected/Disconnected,
          # 3=Save Bright "100"=1.0, 4=Current Bright "75"=0.75, 5=xprintidle cnt

          SetBrightness ()

          # Parm 1: XrandrMonitor Name
          # Parm 2: Math Brigthness Value
          local MON CurrBright
          MON="$1"
          MathBright="$2"

          [[ "$MathBright:0:1" == "-" ]] && MathBright=0 # Negative not allowed
          [[ "$MathBright" -gt 999 ]] && MathBright=999 # Can't go over 9.99

          if [[ "$#MathBright" -eq 3 ]] ; then
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright="$MathBright:0:1.$MathBright:1:2"
          elif [[ "$#MathBright" -eq 1 ]] ; then
          CurrBright=".0$MathBright"
          else
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright=".$MathBright:0:2"
          fi

          # Even if brigthness hasn't changed we want to set it each time so
          # that `xrandr` will lag sleep cycle by 25 milliseconds
          if [[ $CurrBright == ".0" ]] ; then
          xrandr --output "$MON" --off
          else
          xrandr --output "$MON" --brightness "$CurrBright" # Set new brightness
          fi


          GetCurrBright () tail -n1 )
          [[ $CurrBright == "" ]] && echo "$1 Monitor not found in xorg"; return;

          MonStatus="Connected"
          MonCurrBright="$CurrBright##* " # Get brightness level with decimal place

          Left=$MonCurrBright%%"."* # Extract left of decimal point
          Right=$MonCurrBright#*"." # Extract right of decimal point

          [[ "$Left" != 0 ]] && MonMathBright="$Left"00 # 1.0 becomes "100"
          [[ "$#Right" -eq 1 ]] && Right="$Right"0 # 0.5 becomes "50"
          MonMathBright=$(( MonMathBright + Right ))

          # GetCurrBright

          BrightenMonitors ()
          # Turn monitor on and slowly raise brightness to saved setting
          [[ "$BACK" != "" ]] && echo 0 > "/sys/class/backlight/$BACK/bl_power"

          local i m LastPercent NewMathBright
          LastPercent=0
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          # If dimming interrupted, start from last percentage and not 0%
          [[ $MonPercent -gt "$LastPercent" ]] && LastPercent="$MonPercent"
          done
          ((Debug)) && echo Starting percentage: "$LastPercent"

          # Brighten by increments of 4% for quicker waking
          for (( i = LastPercent ; i <= 100 ; i=i+4 )); do # loop from 0% to 100%
          [[ $i -ge 96 ]] && i=100 # If 96, next loop will be 100
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          [[ $MonMathBright -eq 0 ]] && MonMathBright=1
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          SetByNdx "$m"
          sleep "$WAKE_SLEEP" # Very quick increase, so smaller sleep
          done
          done

          # BrightenMonitors

          DimMonitors ()

          local i m NewMathBright Elapsed
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          # Get starting full brightness level, blank monitor name returns zeros
          GetCurrBright "$MonName"
          ((Debug)) && echo "$MonName Starting brightness: $MonMathBright"
          SetByNdx "$m"
          done

          # Dim by increments of 1% for slow fading
          for (( i = 100 ; i >= 0 ; i-- )); do # loop from 100% to 0% brightness
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          MonPercent="$i"
          SetByNdx "$m"
          done
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          sleep "$FADE_SLEEP" # slowly dim so longer sleep
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          done

          [[ "$BACK" != "" ]] && echo 4 > "/sys/class/backlight/$BACK/bl_power"

          # DimMonitors

          GetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonNumber="$MonArr[$1]" # 3rd party monitor number, eg "3"
          MonName="$MonArr[$1+1]" # Xrandr Monitor name, eg "DP-1-1"
          MonStatus="$MonArr[$1+2]" # Xrandr Connected/Disconnected
          MonCurrBright="$MonArr[$1+3]" # Value when dimming first starts eg 1.00
          MonMathBright="$MonArr[$1+4]" # Math implied decimal 100 = 1.00, 50 = .5
          MonPercent="$MonArr[$1+5]" # Last percentage brightness set
          # GetByNdx

          SetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonArr[$1]="$MonNumber" # 3rd party monitor number, eg "3"
          MonArr[$1+1]="$MonName" # Xrandr Monitor name, eg "DP-1-1"
          MonArr[$1+2]="$MonStatus" # Xrandr Connected/Disconnected
          MonArr[$1+3]="$MonCurrBright" # Value when dimming first starts eg 1.00
          MonArr[$1+4]="$MonMathBright" # Implied decimal 100 = 1.00, 50 = .5
          MonArr[$1+5]="$MonPercent" # Last percentage brightness set


          InitializeMonArr ()

          # Initialize arrays with Xrandr name, status, curret bright, math bright, %
          MonArr=()
          MonArr+=( "1" "$MON1" "Cconnected" "1.0" "100" "0" )
          GetByNdx 0
          SetByNdx 0
          MonArr+=( "2" "$MON2" "Disconnected" ".0" "0" "0" )
          GetByNdx 1
          SetByNdx 1
          MonArr+=( "3" "$MON3" "Connected" "1.0" "100" "0" )
          GetByNdx 2
          SetByNdx 2

          MON_COL_CNT=6
          MonArrCnt="$#MonArr[@]"

          # InitializeMonArr

          Dimmed=false
          InitializeMonArr
          if ((Debug)) ; then
          echo "Starting movie.sh"
          echo "Monitor Array Dump: #, Name, Curr Bright, Math Bright, Last Percent"
          echo "$MonArr[*]"
          fi

          # Loop until CTRL + C
          while true ; do

          Elapsed=$(xprintidle)

          if [[ $Elapsed -lt "$TIME_TO_WAKE" ]] ; then
          if [[ $Dimmed == true ]] ; then
          ((Debug)) && echo -e $'n'"Wake after: $Elapsed milliseconds"
          Dimmed=false
          BrightenMonitors
          EnableAutoBright
          fi
          elif [[ $Elapsed -gt "$TIME_TO_FADE" ]] ; then
          if [[ $Dimmed == false ]] ; then
          ((Debug)) && echo -e $'n'"Fade after: $Elapsed milliseconds"
          Dimmed=true
          DisableAutoBright
          # TODO: THey jiggle mouse whilst dimming to stop
          DimMonitors
          continue # Avoid first sleep incase mouse moved.
          fi
          fi

          sleep 1 # Wait a sec until next keyboard / mouse idle test

          done





          share|improve this answer




















          • 2





            Impressive, that's far more readable and maintainable than I expected.

            – Mast
            Jul 1 at 9:03













          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1155093%2fturn-off-all-monitors-while-watching-vlc-media-on-tv%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          10














          I've created a script that slowly dims two monitors after 5 minutes of keyboard or mouse inactivity while you watch a movie on a third monitor. When you move your mouse or use your keyboard the two dimmed monitors quickly brighten (but not suddenly to shock you).





          Save the script below to a filename in your path such as /usr/local/bin/movie.sh or /home/your_user_name/bin/movie.sh and then use:



          chmod a+x /path/to/script/movie.sh


          Xorg's package xprintidle is required to track how long it's been since a mouse movement or keyboard activity:



          sudo apt install xprintidle


          Then to launch the script use:



          movie.sh


          To learn more about the script and how it works use:



          movie.sh anyparameter


          and debug / trace statements are displayed in your terminal.



          Note: When testing, change TIME_TO_FADE from 310000 to 10000 so you only have to wait 10 seconds instead of 5 minutes for fading to start.



          The script



          #!/bin/bash

          # NAME: movie.sh
          # PATH: ~/eyesome
          # DESC: Dim monitors 1 and 3 after 5 minutes of activity
          # DATE: June 27, 2019. Modified June 30, 2019.
          # PARM: Pass any parameter 1 to generate trace output
          # NOTE: No notes.

          # TODO: Increase limit of 3 with array with up to 9 monitor names, eg use:
          # XrandrNames=( "eDP-1-1" "" "DP-1-1" "HDMI-1-1" ) # Gives 4 monitors


          : <<'END'
          /* -----------------------------------------------------------------------------

          CUSTOM NOTES
          ============
          Put your own free form notes here for disabling third party auto-brightness
          sunrise / sunset applications like nightlight, redshift, flux, eyesome, etc.

          CONFIGURATION FILE LOCATION
          ===========================
          $ ll /usr/local/bin/.eyesome-cfg
          -rw-r--r-- 1 root root 610 Jun 27 18:31 /usr/local/bin/.eyesome-cfg

          CONFIGURATION FILE RECORD LAYOUT
          ================================
          $ cat /usr/local/bin/.eyesome-cfg
          https://www.timeanddate.com/sun/canada/edmonton|70.000000|120.000000|90.000000|
          1.000000|TRUE| | | | |1|Disabled|Hardware|Alien 17" Builtin DIsplay|
          intel_backlight|eDP-1-1|3500.000000|1.000000|1.000000|1.000000|850.000000|
          1.000000|0.780000|0.650000|3500|1.000000:1.000000:1.000000| | | | |2|Enabled|
          Software|Sony 50" TV|xrandr|HDMI-0|1.000000|1.000000|1.000000|1.000000|0.750000|
          1.000000|0.950000|0.850000|1.00|1.000000:1.000000:1.000000| | | | |3|Disabled|
          Software|Toshibal 43" 4K TV|xrandr|DP-1-1|1.000000|1.000000|1.000000|1.000000|
          0.650000|1.000000|0.880000|0.780000|1.00|1.000000:1.000000:1.000000| | | |

          ----------------------------------------------------------------------------- */
          END

          # If sudo powers not needed, eg no 3rd party auto-brightening to disable,
          # then comment out the next four lines:
          if [[ $(id -u) != 0 ]]; then # root powers needed to call this script
          echo >&2 "$0 must be called with sudo powers"
          exit 1
          fi

          # To echo debug/trace statements to screen, pass any parameter to script
          Debug=0
          [[ $# -gt 0 ]] && Debug=1

          # To get list of your monitors use: xrandr --verbose | grep " connected"
          MON1="eDP-1-1" # eDP-1-1 = Laptop display
          MON2="" # HDMI-0 = Sony TV
          MON3="DP-1-1" # DP-1-1 = Toshiba TV

          # If you elect to turn backlight complete off and on you must use sudo
          BACK="intel_backlight" # Setting to blank means no backlight to turn off/on
          BACK="" # After xrandr dims backlight will stay on

          # All seconds in millisconds, eg 1000 milliseconds = 1 second
          TIME_TO_FADE=310000 # Keyboard/Mouse idle time before dim 300000 = 5 minutes
          TIME_TO_WAKE=2000 # 2 second time or less to trigger brightening
          FADE_SLEEP=.15 # When fading sleep seconds between monitor changes
          WAKE_SLEEP=.025 # When waking sleep seconds between monitor changes

          # Optional configuration file for 3rd party auto brightnes / color temperature
          CFG_FILE=""
          # if nothing else is automatically controlling monitors comment out next line
          #CFG_FILE=/usr/local/bin/.eyesome-cfg

          DisableAutoBright () Disabled/g' "$CFG_FILE"
          [[ $MON2 != "" ]] && sed -i 's/2 # DisableAutoBright

          EnableAutoBright () Enabled/g' "$CFG_FILE"
          [[ $MON3 != "" ]] && sed -i 's/3 # EnableAutoBright

          MonArr=() # Index 0= Monitor # 1 to 3, 1=Xrandr Name, 2=Connected/Disconnected,
          # 3=Save Bright "100"=1.0, 4=Current Bright "75"=0.75, 5=xprintidle cnt

          SetBrightness ()

          # Parm 1: XrandrMonitor Name
          # Parm 2: Math Brigthness Value
          local MON CurrBright
          MON="$1"
          MathBright="$2"

          [[ "$MathBright:0:1" == "-" ]] && MathBright=0 # Negative not allowed
          [[ "$MathBright" -gt 999 ]] && MathBright=999 # Can't go over 9.99

          if [[ "$#MathBright" -eq 3 ]] ; then
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright="$MathBright:0:1.$MathBright:1:2"
          elif [[ "$#MathBright" -eq 1 ]] ; then
          CurrBright=".0$MathBright"
          else
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright=".$MathBright:0:2"
          fi

          # Even if brigthness hasn't changed we want to set it each time so
          # that `xrandr` will lag sleep cycle by 25 milliseconds
          if [[ $CurrBright == ".0" ]] ; then
          xrandr --output "$MON" --off
          else
          xrandr --output "$MON" --brightness "$CurrBright" # Set new brightness
          fi


          GetCurrBright () tail -n1 )
          [[ $CurrBright == "" ]] && echo "$1 Monitor not found in xorg"; return;

          MonStatus="Connected"
          MonCurrBright="$CurrBright##* " # Get brightness level with decimal place

          Left=$MonCurrBright%%"."* # Extract left of decimal point
          Right=$MonCurrBright#*"." # Extract right of decimal point

          [[ "$Left" != 0 ]] && MonMathBright="$Left"00 # 1.0 becomes "100"
          [[ "$#Right" -eq 1 ]] && Right="$Right"0 # 0.5 becomes "50"
          MonMathBright=$(( MonMathBright + Right ))

          # GetCurrBright

          BrightenMonitors ()
          # Turn monitor on and slowly raise brightness to saved setting
          [[ "$BACK" != "" ]] && echo 0 > "/sys/class/backlight/$BACK/bl_power"

          local i m LastPercent NewMathBright
          LastPercent=0
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          # If dimming interrupted, start from last percentage and not 0%
          [[ $MonPercent -gt "$LastPercent" ]] && LastPercent="$MonPercent"
          done
          ((Debug)) && echo Starting percentage: "$LastPercent"

          # Brighten by increments of 4% for quicker waking
          for (( i = LastPercent ; i <= 100 ; i=i+4 )); do # loop from 0% to 100%
          [[ $i -ge 96 ]] && i=100 # If 96, next loop will be 100
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          [[ $MonMathBright -eq 0 ]] && MonMathBright=1
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          SetByNdx "$m"
          sleep "$WAKE_SLEEP" # Very quick increase, so smaller sleep
          done
          done

          # BrightenMonitors

          DimMonitors ()

          local i m NewMathBright Elapsed
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          # Get starting full brightness level, blank monitor name returns zeros
          GetCurrBright "$MonName"
          ((Debug)) && echo "$MonName Starting brightness: $MonMathBright"
          SetByNdx "$m"
          done

          # Dim by increments of 1% for slow fading
          for (( i = 100 ; i >= 0 ; i-- )); do # loop from 100% to 0% brightness
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          MonPercent="$i"
          SetByNdx "$m"
          done
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          sleep "$FADE_SLEEP" # slowly dim so longer sleep
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          done

          [[ "$BACK" != "" ]] && echo 4 > "/sys/class/backlight/$BACK/bl_power"

          # DimMonitors

          GetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonNumber="$MonArr[$1]" # 3rd party monitor number, eg "3"
          MonName="$MonArr[$1+1]" # Xrandr Monitor name, eg "DP-1-1"
          MonStatus="$MonArr[$1+2]" # Xrandr Connected/Disconnected
          MonCurrBright="$MonArr[$1+3]" # Value when dimming first starts eg 1.00
          MonMathBright="$MonArr[$1+4]" # Math implied decimal 100 = 1.00, 50 = .5
          MonPercent="$MonArr[$1+5]" # Last percentage brightness set
          # GetByNdx

          SetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonArr[$1]="$MonNumber" # 3rd party monitor number, eg "3"
          MonArr[$1+1]="$MonName" # Xrandr Monitor name, eg "DP-1-1"
          MonArr[$1+2]="$MonStatus" # Xrandr Connected/Disconnected
          MonArr[$1+3]="$MonCurrBright" # Value when dimming first starts eg 1.00
          MonArr[$1+4]="$MonMathBright" # Implied decimal 100 = 1.00, 50 = .5
          MonArr[$1+5]="$MonPercent" # Last percentage brightness set


          InitializeMonArr ()

          # Initialize arrays with Xrandr name, status, curret bright, math bright, %
          MonArr=()
          MonArr+=( "1" "$MON1" "Cconnected" "1.0" "100" "0" )
          GetByNdx 0
          SetByNdx 0
          MonArr+=( "2" "$MON2" "Disconnected" ".0" "0" "0" )
          GetByNdx 1
          SetByNdx 1
          MonArr+=( "3" "$MON3" "Connected" "1.0" "100" "0" )
          GetByNdx 2
          SetByNdx 2

          MON_COL_CNT=6
          MonArrCnt="$#MonArr[@]"

          # InitializeMonArr

          Dimmed=false
          InitializeMonArr
          if ((Debug)) ; then
          echo "Starting movie.sh"
          echo "Monitor Array Dump: #, Name, Curr Bright, Math Bright, Last Percent"
          echo "$MonArr[*]"
          fi

          # Loop until CTRL + C
          while true ; do

          Elapsed=$(xprintidle)

          if [[ $Elapsed -lt "$TIME_TO_WAKE" ]] ; then
          if [[ $Dimmed == true ]] ; then
          ((Debug)) && echo -e $'n'"Wake after: $Elapsed milliseconds"
          Dimmed=false
          BrightenMonitors
          EnableAutoBright
          fi
          elif [[ $Elapsed -gt "$TIME_TO_FADE" ]] ; then
          if [[ $Dimmed == false ]] ; then
          ((Debug)) && echo -e $'n'"Fade after: $Elapsed milliseconds"
          Dimmed=true
          DisableAutoBright
          # TODO: THey jiggle mouse whilst dimming to stop
          DimMonitors
          continue # Avoid first sleep incase mouse moved.
          fi
          fi

          sleep 1 # Wait a sec until next keyboard / mouse idle test

          done





          share|improve this answer




















          • 2





            Impressive, that's far more readable and maintainable than I expected.

            – Mast
            Jul 1 at 9:03















          10














          I've created a script that slowly dims two monitors after 5 minutes of keyboard or mouse inactivity while you watch a movie on a third monitor. When you move your mouse or use your keyboard the two dimmed monitors quickly brighten (but not suddenly to shock you).





          Save the script below to a filename in your path such as /usr/local/bin/movie.sh or /home/your_user_name/bin/movie.sh and then use:



          chmod a+x /path/to/script/movie.sh


          Xorg's package xprintidle is required to track how long it's been since a mouse movement or keyboard activity:



          sudo apt install xprintidle


          Then to launch the script use:



          movie.sh


          To learn more about the script and how it works use:



          movie.sh anyparameter


          and debug / trace statements are displayed in your terminal.



          Note: When testing, change TIME_TO_FADE from 310000 to 10000 so you only have to wait 10 seconds instead of 5 minutes for fading to start.



          The script



          #!/bin/bash

          # NAME: movie.sh
          # PATH: ~/eyesome
          # DESC: Dim monitors 1 and 3 after 5 minutes of activity
          # DATE: June 27, 2019. Modified June 30, 2019.
          # PARM: Pass any parameter 1 to generate trace output
          # NOTE: No notes.

          # TODO: Increase limit of 3 with array with up to 9 monitor names, eg use:
          # XrandrNames=( "eDP-1-1" "" "DP-1-1" "HDMI-1-1" ) # Gives 4 monitors


          : <<'END'
          /* -----------------------------------------------------------------------------

          CUSTOM NOTES
          ============
          Put your own free form notes here for disabling third party auto-brightness
          sunrise / sunset applications like nightlight, redshift, flux, eyesome, etc.

          CONFIGURATION FILE LOCATION
          ===========================
          $ ll /usr/local/bin/.eyesome-cfg
          -rw-r--r-- 1 root root 610 Jun 27 18:31 /usr/local/bin/.eyesome-cfg

          CONFIGURATION FILE RECORD LAYOUT
          ================================
          $ cat /usr/local/bin/.eyesome-cfg
          https://www.timeanddate.com/sun/canada/edmonton|70.000000|120.000000|90.000000|
          1.000000|TRUE| | | | |1|Disabled|Hardware|Alien 17" Builtin DIsplay|
          intel_backlight|eDP-1-1|3500.000000|1.000000|1.000000|1.000000|850.000000|
          1.000000|0.780000|0.650000|3500|1.000000:1.000000:1.000000| | | | |2|Enabled|
          Software|Sony 50" TV|xrandr|HDMI-0|1.000000|1.000000|1.000000|1.000000|0.750000|
          1.000000|0.950000|0.850000|1.00|1.000000:1.000000:1.000000| | | | |3|Disabled|
          Software|Toshibal 43" 4K TV|xrandr|DP-1-1|1.000000|1.000000|1.000000|1.000000|
          0.650000|1.000000|0.880000|0.780000|1.00|1.000000:1.000000:1.000000| | | |

          ----------------------------------------------------------------------------- */
          END

          # If sudo powers not needed, eg no 3rd party auto-brightening to disable,
          # then comment out the next four lines:
          if [[ $(id -u) != 0 ]]; then # root powers needed to call this script
          echo >&2 "$0 must be called with sudo powers"
          exit 1
          fi

          # To echo debug/trace statements to screen, pass any parameter to script
          Debug=0
          [[ $# -gt 0 ]] && Debug=1

          # To get list of your monitors use: xrandr --verbose | grep " connected"
          MON1="eDP-1-1" # eDP-1-1 = Laptop display
          MON2="" # HDMI-0 = Sony TV
          MON3="DP-1-1" # DP-1-1 = Toshiba TV

          # If you elect to turn backlight complete off and on you must use sudo
          BACK="intel_backlight" # Setting to blank means no backlight to turn off/on
          BACK="" # After xrandr dims backlight will stay on

          # All seconds in millisconds, eg 1000 milliseconds = 1 second
          TIME_TO_FADE=310000 # Keyboard/Mouse idle time before dim 300000 = 5 minutes
          TIME_TO_WAKE=2000 # 2 second time or less to trigger brightening
          FADE_SLEEP=.15 # When fading sleep seconds between monitor changes
          WAKE_SLEEP=.025 # When waking sleep seconds between monitor changes

          # Optional configuration file for 3rd party auto brightnes / color temperature
          CFG_FILE=""
          # if nothing else is automatically controlling monitors comment out next line
          #CFG_FILE=/usr/local/bin/.eyesome-cfg

          DisableAutoBright () Disabled/g' "$CFG_FILE"
          [[ $MON2 != "" ]] && sed -i 's/2 # DisableAutoBright

          EnableAutoBright () Enabled/g' "$CFG_FILE"
          [[ $MON3 != "" ]] && sed -i 's/3 # EnableAutoBright

          MonArr=() # Index 0= Monitor # 1 to 3, 1=Xrandr Name, 2=Connected/Disconnected,
          # 3=Save Bright "100"=1.0, 4=Current Bright "75"=0.75, 5=xprintidle cnt

          SetBrightness ()

          # Parm 1: XrandrMonitor Name
          # Parm 2: Math Brigthness Value
          local MON CurrBright
          MON="$1"
          MathBright="$2"

          [[ "$MathBright:0:1" == "-" ]] && MathBright=0 # Negative not allowed
          [[ "$MathBright" -gt 999 ]] && MathBright=999 # Can't go over 9.99

          if [[ "$#MathBright" -eq 3 ]] ; then
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright="$MathBright:0:1.$MathBright:1:2"
          elif [[ "$#MathBright" -eq 1 ]] ; then
          CurrBright=".0$MathBright"
          else
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright=".$MathBright:0:2"
          fi

          # Even if brigthness hasn't changed we want to set it each time so
          # that `xrandr` will lag sleep cycle by 25 milliseconds
          if [[ $CurrBright == ".0" ]] ; then
          xrandr --output "$MON" --off
          else
          xrandr --output "$MON" --brightness "$CurrBright" # Set new brightness
          fi


          GetCurrBright () tail -n1 )
          [[ $CurrBright == "" ]] && echo "$1 Monitor not found in xorg"; return;

          MonStatus="Connected"
          MonCurrBright="$CurrBright##* " # Get brightness level with decimal place

          Left=$MonCurrBright%%"."* # Extract left of decimal point
          Right=$MonCurrBright#*"." # Extract right of decimal point

          [[ "$Left" != 0 ]] && MonMathBright="$Left"00 # 1.0 becomes "100"
          [[ "$#Right" -eq 1 ]] && Right="$Right"0 # 0.5 becomes "50"
          MonMathBright=$(( MonMathBright + Right ))

          # GetCurrBright

          BrightenMonitors ()
          # Turn monitor on and slowly raise brightness to saved setting
          [[ "$BACK" != "" ]] && echo 0 > "/sys/class/backlight/$BACK/bl_power"

          local i m LastPercent NewMathBright
          LastPercent=0
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          # If dimming interrupted, start from last percentage and not 0%
          [[ $MonPercent -gt "$LastPercent" ]] && LastPercent="$MonPercent"
          done
          ((Debug)) && echo Starting percentage: "$LastPercent"

          # Brighten by increments of 4% for quicker waking
          for (( i = LastPercent ; i <= 100 ; i=i+4 )); do # loop from 0% to 100%
          [[ $i -ge 96 ]] && i=100 # If 96, next loop will be 100
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          [[ $MonMathBright -eq 0 ]] && MonMathBright=1
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          SetByNdx "$m"
          sleep "$WAKE_SLEEP" # Very quick increase, so smaller sleep
          done
          done

          # BrightenMonitors

          DimMonitors ()

          local i m NewMathBright Elapsed
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          # Get starting full brightness level, blank monitor name returns zeros
          GetCurrBright "$MonName"
          ((Debug)) && echo "$MonName Starting brightness: $MonMathBright"
          SetByNdx "$m"
          done

          # Dim by increments of 1% for slow fading
          for (( i = 100 ; i >= 0 ; i-- )); do # loop from 100% to 0% brightness
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          MonPercent="$i"
          SetByNdx "$m"
          done
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          sleep "$FADE_SLEEP" # slowly dim so longer sleep
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          done

          [[ "$BACK" != "" ]] && echo 4 > "/sys/class/backlight/$BACK/bl_power"

          # DimMonitors

          GetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonNumber="$MonArr[$1]" # 3rd party monitor number, eg "3"
          MonName="$MonArr[$1+1]" # Xrandr Monitor name, eg "DP-1-1"
          MonStatus="$MonArr[$1+2]" # Xrandr Connected/Disconnected
          MonCurrBright="$MonArr[$1+3]" # Value when dimming first starts eg 1.00
          MonMathBright="$MonArr[$1+4]" # Math implied decimal 100 = 1.00, 50 = .5
          MonPercent="$MonArr[$1+5]" # Last percentage brightness set
          # GetByNdx

          SetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonArr[$1]="$MonNumber" # 3rd party monitor number, eg "3"
          MonArr[$1+1]="$MonName" # Xrandr Monitor name, eg "DP-1-1"
          MonArr[$1+2]="$MonStatus" # Xrandr Connected/Disconnected
          MonArr[$1+3]="$MonCurrBright" # Value when dimming first starts eg 1.00
          MonArr[$1+4]="$MonMathBright" # Implied decimal 100 = 1.00, 50 = .5
          MonArr[$1+5]="$MonPercent" # Last percentage brightness set


          InitializeMonArr ()

          # Initialize arrays with Xrandr name, status, curret bright, math bright, %
          MonArr=()
          MonArr+=( "1" "$MON1" "Cconnected" "1.0" "100" "0" )
          GetByNdx 0
          SetByNdx 0
          MonArr+=( "2" "$MON2" "Disconnected" ".0" "0" "0" )
          GetByNdx 1
          SetByNdx 1
          MonArr+=( "3" "$MON3" "Connected" "1.0" "100" "0" )
          GetByNdx 2
          SetByNdx 2

          MON_COL_CNT=6
          MonArrCnt="$#MonArr[@]"

          # InitializeMonArr

          Dimmed=false
          InitializeMonArr
          if ((Debug)) ; then
          echo "Starting movie.sh"
          echo "Monitor Array Dump: #, Name, Curr Bright, Math Bright, Last Percent"
          echo "$MonArr[*]"
          fi

          # Loop until CTRL + C
          while true ; do

          Elapsed=$(xprintidle)

          if [[ $Elapsed -lt "$TIME_TO_WAKE" ]] ; then
          if [[ $Dimmed == true ]] ; then
          ((Debug)) && echo -e $'n'"Wake after: $Elapsed milliseconds"
          Dimmed=false
          BrightenMonitors
          EnableAutoBright
          fi
          elif [[ $Elapsed -gt "$TIME_TO_FADE" ]] ; then
          if [[ $Dimmed == false ]] ; then
          ((Debug)) && echo -e $'n'"Fade after: $Elapsed milliseconds"
          Dimmed=true
          DisableAutoBright
          # TODO: THey jiggle mouse whilst dimming to stop
          DimMonitors
          continue # Avoid first sleep incase mouse moved.
          fi
          fi

          sleep 1 # Wait a sec until next keyboard / mouse idle test

          done





          share|improve this answer




















          • 2





            Impressive, that's far more readable and maintainable than I expected.

            – Mast
            Jul 1 at 9:03













          10












          10








          10







          I've created a script that slowly dims two monitors after 5 minutes of keyboard or mouse inactivity while you watch a movie on a third monitor. When you move your mouse or use your keyboard the two dimmed monitors quickly brighten (but not suddenly to shock you).





          Save the script below to a filename in your path such as /usr/local/bin/movie.sh or /home/your_user_name/bin/movie.sh and then use:



          chmod a+x /path/to/script/movie.sh


          Xorg's package xprintidle is required to track how long it's been since a mouse movement or keyboard activity:



          sudo apt install xprintidle


          Then to launch the script use:



          movie.sh


          To learn more about the script and how it works use:



          movie.sh anyparameter


          and debug / trace statements are displayed in your terminal.



          Note: When testing, change TIME_TO_FADE from 310000 to 10000 so you only have to wait 10 seconds instead of 5 minutes for fading to start.



          The script



          #!/bin/bash

          # NAME: movie.sh
          # PATH: ~/eyesome
          # DESC: Dim monitors 1 and 3 after 5 minutes of activity
          # DATE: June 27, 2019. Modified June 30, 2019.
          # PARM: Pass any parameter 1 to generate trace output
          # NOTE: No notes.

          # TODO: Increase limit of 3 with array with up to 9 monitor names, eg use:
          # XrandrNames=( "eDP-1-1" "" "DP-1-1" "HDMI-1-1" ) # Gives 4 monitors


          : <<'END'
          /* -----------------------------------------------------------------------------

          CUSTOM NOTES
          ============
          Put your own free form notes here for disabling third party auto-brightness
          sunrise / sunset applications like nightlight, redshift, flux, eyesome, etc.

          CONFIGURATION FILE LOCATION
          ===========================
          $ ll /usr/local/bin/.eyesome-cfg
          -rw-r--r-- 1 root root 610 Jun 27 18:31 /usr/local/bin/.eyesome-cfg

          CONFIGURATION FILE RECORD LAYOUT
          ================================
          $ cat /usr/local/bin/.eyesome-cfg
          https://www.timeanddate.com/sun/canada/edmonton|70.000000|120.000000|90.000000|
          1.000000|TRUE| | | | |1|Disabled|Hardware|Alien 17" Builtin DIsplay|
          intel_backlight|eDP-1-1|3500.000000|1.000000|1.000000|1.000000|850.000000|
          1.000000|0.780000|0.650000|3500|1.000000:1.000000:1.000000| | | | |2|Enabled|
          Software|Sony 50" TV|xrandr|HDMI-0|1.000000|1.000000|1.000000|1.000000|0.750000|
          1.000000|0.950000|0.850000|1.00|1.000000:1.000000:1.000000| | | | |3|Disabled|
          Software|Toshibal 43" 4K TV|xrandr|DP-1-1|1.000000|1.000000|1.000000|1.000000|
          0.650000|1.000000|0.880000|0.780000|1.00|1.000000:1.000000:1.000000| | | |

          ----------------------------------------------------------------------------- */
          END

          # If sudo powers not needed, eg no 3rd party auto-brightening to disable,
          # then comment out the next four lines:
          if [[ $(id -u) != 0 ]]; then # root powers needed to call this script
          echo >&2 "$0 must be called with sudo powers"
          exit 1
          fi

          # To echo debug/trace statements to screen, pass any parameter to script
          Debug=0
          [[ $# -gt 0 ]] && Debug=1

          # To get list of your monitors use: xrandr --verbose | grep " connected"
          MON1="eDP-1-1" # eDP-1-1 = Laptop display
          MON2="" # HDMI-0 = Sony TV
          MON3="DP-1-1" # DP-1-1 = Toshiba TV

          # If you elect to turn backlight complete off and on you must use sudo
          BACK="intel_backlight" # Setting to blank means no backlight to turn off/on
          BACK="" # After xrandr dims backlight will stay on

          # All seconds in millisconds, eg 1000 milliseconds = 1 second
          TIME_TO_FADE=310000 # Keyboard/Mouse idle time before dim 300000 = 5 minutes
          TIME_TO_WAKE=2000 # 2 second time or less to trigger brightening
          FADE_SLEEP=.15 # When fading sleep seconds between monitor changes
          WAKE_SLEEP=.025 # When waking sleep seconds between monitor changes

          # Optional configuration file for 3rd party auto brightnes / color temperature
          CFG_FILE=""
          # if nothing else is automatically controlling monitors comment out next line
          #CFG_FILE=/usr/local/bin/.eyesome-cfg

          DisableAutoBright () Disabled/g' "$CFG_FILE"
          [[ $MON2 != "" ]] && sed -i 's/2 # DisableAutoBright

          EnableAutoBright () Enabled/g' "$CFG_FILE"
          [[ $MON3 != "" ]] && sed -i 's/3 # EnableAutoBright

          MonArr=() # Index 0= Monitor # 1 to 3, 1=Xrandr Name, 2=Connected/Disconnected,
          # 3=Save Bright "100"=1.0, 4=Current Bright "75"=0.75, 5=xprintidle cnt

          SetBrightness ()

          # Parm 1: XrandrMonitor Name
          # Parm 2: Math Brigthness Value
          local MON CurrBright
          MON="$1"
          MathBright="$2"

          [[ "$MathBright:0:1" == "-" ]] && MathBright=0 # Negative not allowed
          [[ "$MathBright" -gt 999 ]] && MathBright=999 # Can't go over 9.99

          if [[ "$#MathBright" -eq 3 ]] ; then
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright="$MathBright:0:1.$MathBright:1:2"
          elif [[ "$#MathBright" -eq 1 ]] ; then
          CurrBright=".0$MathBright"
          else
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright=".$MathBright:0:2"
          fi

          # Even if brigthness hasn't changed we want to set it each time so
          # that `xrandr` will lag sleep cycle by 25 milliseconds
          if [[ $CurrBright == ".0" ]] ; then
          xrandr --output "$MON" --off
          else
          xrandr --output "$MON" --brightness "$CurrBright" # Set new brightness
          fi


          GetCurrBright () tail -n1 )
          [[ $CurrBright == "" ]] && echo "$1 Monitor not found in xorg"; return;

          MonStatus="Connected"
          MonCurrBright="$CurrBright##* " # Get brightness level with decimal place

          Left=$MonCurrBright%%"."* # Extract left of decimal point
          Right=$MonCurrBright#*"." # Extract right of decimal point

          [[ "$Left" != 0 ]] && MonMathBright="$Left"00 # 1.0 becomes "100"
          [[ "$#Right" -eq 1 ]] && Right="$Right"0 # 0.5 becomes "50"
          MonMathBright=$(( MonMathBright + Right ))

          # GetCurrBright

          BrightenMonitors ()
          # Turn monitor on and slowly raise brightness to saved setting
          [[ "$BACK" != "" ]] && echo 0 > "/sys/class/backlight/$BACK/bl_power"

          local i m LastPercent NewMathBright
          LastPercent=0
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          # If dimming interrupted, start from last percentage and not 0%
          [[ $MonPercent -gt "$LastPercent" ]] && LastPercent="$MonPercent"
          done
          ((Debug)) && echo Starting percentage: "$LastPercent"

          # Brighten by increments of 4% for quicker waking
          for (( i = LastPercent ; i <= 100 ; i=i+4 )); do # loop from 0% to 100%
          [[ $i -ge 96 ]] && i=100 # If 96, next loop will be 100
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          [[ $MonMathBright -eq 0 ]] && MonMathBright=1
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          SetByNdx "$m"
          sleep "$WAKE_SLEEP" # Very quick increase, so smaller sleep
          done
          done

          # BrightenMonitors

          DimMonitors ()

          local i m NewMathBright Elapsed
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          # Get starting full brightness level, blank monitor name returns zeros
          GetCurrBright "$MonName"
          ((Debug)) && echo "$MonName Starting brightness: $MonMathBright"
          SetByNdx "$m"
          done

          # Dim by increments of 1% for slow fading
          for (( i = 100 ; i >= 0 ; i-- )); do # loop from 100% to 0% brightness
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          MonPercent="$i"
          SetByNdx "$m"
          done
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          sleep "$FADE_SLEEP" # slowly dim so longer sleep
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          done

          [[ "$BACK" != "" ]] && echo 4 > "/sys/class/backlight/$BACK/bl_power"

          # DimMonitors

          GetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonNumber="$MonArr[$1]" # 3rd party monitor number, eg "3"
          MonName="$MonArr[$1+1]" # Xrandr Monitor name, eg "DP-1-1"
          MonStatus="$MonArr[$1+2]" # Xrandr Connected/Disconnected
          MonCurrBright="$MonArr[$1+3]" # Value when dimming first starts eg 1.00
          MonMathBright="$MonArr[$1+4]" # Math implied decimal 100 = 1.00, 50 = .5
          MonPercent="$MonArr[$1+5]" # Last percentage brightness set
          # GetByNdx

          SetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonArr[$1]="$MonNumber" # 3rd party monitor number, eg "3"
          MonArr[$1+1]="$MonName" # Xrandr Monitor name, eg "DP-1-1"
          MonArr[$1+2]="$MonStatus" # Xrandr Connected/Disconnected
          MonArr[$1+3]="$MonCurrBright" # Value when dimming first starts eg 1.00
          MonArr[$1+4]="$MonMathBright" # Implied decimal 100 = 1.00, 50 = .5
          MonArr[$1+5]="$MonPercent" # Last percentage brightness set


          InitializeMonArr ()

          # Initialize arrays with Xrandr name, status, curret bright, math bright, %
          MonArr=()
          MonArr+=( "1" "$MON1" "Cconnected" "1.0" "100" "0" )
          GetByNdx 0
          SetByNdx 0
          MonArr+=( "2" "$MON2" "Disconnected" ".0" "0" "0" )
          GetByNdx 1
          SetByNdx 1
          MonArr+=( "3" "$MON3" "Connected" "1.0" "100" "0" )
          GetByNdx 2
          SetByNdx 2

          MON_COL_CNT=6
          MonArrCnt="$#MonArr[@]"

          # InitializeMonArr

          Dimmed=false
          InitializeMonArr
          if ((Debug)) ; then
          echo "Starting movie.sh"
          echo "Monitor Array Dump: #, Name, Curr Bright, Math Bright, Last Percent"
          echo "$MonArr[*]"
          fi

          # Loop until CTRL + C
          while true ; do

          Elapsed=$(xprintidle)

          if [[ $Elapsed -lt "$TIME_TO_WAKE" ]] ; then
          if [[ $Dimmed == true ]] ; then
          ((Debug)) && echo -e $'n'"Wake after: $Elapsed milliseconds"
          Dimmed=false
          BrightenMonitors
          EnableAutoBright
          fi
          elif [[ $Elapsed -gt "$TIME_TO_FADE" ]] ; then
          if [[ $Dimmed == false ]] ; then
          ((Debug)) && echo -e $'n'"Fade after: $Elapsed milliseconds"
          Dimmed=true
          DisableAutoBright
          # TODO: THey jiggle mouse whilst dimming to stop
          DimMonitors
          continue # Avoid first sleep incase mouse moved.
          fi
          fi

          sleep 1 # Wait a sec until next keyboard / mouse idle test

          done





          share|improve this answer















          I've created a script that slowly dims two monitors after 5 minutes of keyboard or mouse inactivity while you watch a movie on a third monitor. When you move your mouse or use your keyboard the two dimmed monitors quickly brighten (but not suddenly to shock you).





          Save the script below to a filename in your path such as /usr/local/bin/movie.sh or /home/your_user_name/bin/movie.sh and then use:



          chmod a+x /path/to/script/movie.sh


          Xorg's package xprintidle is required to track how long it's been since a mouse movement or keyboard activity:



          sudo apt install xprintidle


          Then to launch the script use:



          movie.sh


          To learn more about the script and how it works use:



          movie.sh anyparameter


          and debug / trace statements are displayed in your terminal.



          Note: When testing, change TIME_TO_FADE from 310000 to 10000 so you only have to wait 10 seconds instead of 5 minutes for fading to start.



          The script



          #!/bin/bash

          # NAME: movie.sh
          # PATH: ~/eyesome
          # DESC: Dim monitors 1 and 3 after 5 minutes of activity
          # DATE: June 27, 2019. Modified June 30, 2019.
          # PARM: Pass any parameter 1 to generate trace output
          # NOTE: No notes.

          # TODO: Increase limit of 3 with array with up to 9 monitor names, eg use:
          # XrandrNames=( "eDP-1-1" "" "DP-1-1" "HDMI-1-1" ) # Gives 4 monitors


          : <<'END'
          /* -----------------------------------------------------------------------------

          CUSTOM NOTES
          ============
          Put your own free form notes here for disabling third party auto-brightness
          sunrise / sunset applications like nightlight, redshift, flux, eyesome, etc.

          CONFIGURATION FILE LOCATION
          ===========================
          $ ll /usr/local/bin/.eyesome-cfg
          -rw-r--r-- 1 root root 610 Jun 27 18:31 /usr/local/bin/.eyesome-cfg

          CONFIGURATION FILE RECORD LAYOUT
          ================================
          $ cat /usr/local/bin/.eyesome-cfg
          https://www.timeanddate.com/sun/canada/edmonton|70.000000|120.000000|90.000000|
          1.000000|TRUE| | | | |1|Disabled|Hardware|Alien 17" Builtin DIsplay|
          intel_backlight|eDP-1-1|3500.000000|1.000000|1.000000|1.000000|850.000000|
          1.000000|0.780000|0.650000|3500|1.000000:1.000000:1.000000| | | | |2|Enabled|
          Software|Sony 50" TV|xrandr|HDMI-0|1.000000|1.000000|1.000000|1.000000|0.750000|
          1.000000|0.950000|0.850000|1.00|1.000000:1.000000:1.000000| | | | |3|Disabled|
          Software|Toshibal 43" 4K TV|xrandr|DP-1-1|1.000000|1.000000|1.000000|1.000000|
          0.650000|1.000000|0.880000|0.780000|1.00|1.000000:1.000000:1.000000| | | |

          ----------------------------------------------------------------------------- */
          END

          # If sudo powers not needed, eg no 3rd party auto-brightening to disable,
          # then comment out the next four lines:
          if [[ $(id -u) != 0 ]]; then # root powers needed to call this script
          echo >&2 "$0 must be called with sudo powers"
          exit 1
          fi

          # To echo debug/trace statements to screen, pass any parameter to script
          Debug=0
          [[ $# -gt 0 ]] && Debug=1

          # To get list of your monitors use: xrandr --verbose | grep " connected"
          MON1="eDP-1-1" # eDP-1-1 = Laptop display
          MON2="" # HDMI-0 = Sony TV
          MON3="DP-1-1" # DP-1-1 = Toshiba TV

          # If you elect to turn backlight complete off and on you must use sudo
          BACK="intel_backlight" # Setting to blank means no backlight to turn off/on
          BACK="" # After xrandr dims backlight will stay on

          # All seconds in millisconds, eg 1000 milliseconds = 1 second
          TIME_TO_FADE=310000 # Keyboard/Mouse idle time before dim 300000 = 5 minutes
          TIME_TO_WAKE=2000 # 2 second time or less to trigger brightening
          FADE_SLEEP=.15 # When fading sleep seconds between monitor changes
          WAKE_SLEEP=.025 # When waking sleep seconds between monitor changes

          # Optional configuration file for 3rd party auto brightnes / color temperature
          CFG_FILE=""
          # if nothing else is automatically controlling monitors comment out next line
          #CFG_FILE=/usr/local/bin/.eyesome-cfg

          DisableAutoBright () Disabled/g' "$CFG_FILE"
          [[ $MON2 != "" ]] && sed -i 's/2 # DisableAutoBright

          EnableAutoBright () Enabled/g' "$CFG_FILE"
          [[ $MON3 != "" ]] && sed -i 's/3 # EnableAutoBright

          MonArr=() # Index 0= Monitor # 1 to 3, 1=Xrandr Name, 2=Connected/Disconnected,
          # 3=Save Bright "100"=1.0, 4=Current Bright "75"=0.75, 5=xprintidle cnt

          SetBrightness ()

          # Parm 1: XrandrMonitor Name
          # Parm 2: Math Brigthness Value
          local MON CurrBright
          MON="$1"
          MathBright="$2"

          [[ "$MathBright:0:1" == "-" ]] && MathBright=0 # Negative not allowed
          [[ "$MathBright" -gt 999 ]] && MathBright=999 # Can't go over 9.99

          if [[ "$#MathBright" -eq 3 ]] ; then
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright="$MathBright:0:1.$MathBright:1:2"
          elif [[ "$#MathBright" -eq 1 ]] ; then
          CurrBright=".0$MathBright"
          else
          MathBright="$MathBright"000 # Pad with lots of zeros
          CurrBright=".$MathBright:0:2"
          fi

          # Even if brigthness hasn't changed we want to set it each time so
          # that `xrandr` will lag sleep cycle by 25 milliseconds
          if [[ $CurrBright == ".0" ]] ; then
          xrandr --output "$MON" --off
          else
          xrandr --output "$MON" --brightness "$CurrBright" # Set new brightness
          fi


          GetCurrBright () tail -n1 )
          [[ $CurrBright == "" ]] && echo "$1 Monitor not found in xorg"; return;

          MonStatus="Connected"
          MonCurrBright="$CurrBright##* " # Get brightness level with decimal place

          Left=$MonCurrBright%%"."* # Extract left of decimal point
          Right=$MonCurrBright#*"." # Extract right of decimal point

          [[ "$Left" != 0 ]] && MonMathBright="$Left"00 # 1.0 becomes "100"
          [[ "$#Right" -eq 1 ]] && Right="$Right"0 # 0.5 becomes "50"
          MonMathBright=$(( MonMathBright + Right ))

          # GetCurrBright

          BrightenMonitors ()
          # Turn monitor on and slowly raise brightness to saved setting
          [[ "$BACK" != "" ]] && echo 0 > "/sys/class/backlight/$BACK/bl_power"

          local i m LastPercent NewMathBright
          LastPercent=0
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          # If dimming interrupted, start from last percentage and not 0%
          [[ $MonPercent -gt "$LastPercent" ]] && LastPercent="$MonPercent"
          done
          ((Debug)) && echo Starting percentage: "$LastPercent"

          # Brighten by increments of 4% for quicker waking
          for (( i = LastPercent ; i <= 100 ; i=i+4 )); do # loop from 0% to 100%
          [[ $i -ge 96 ]] && i=100 # If 96, next loop will be 100
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          [[ $MonMathBright -eq 0 ]] && MonMathBright=1
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          SetByNdx "$m"
          sleep "$WAKE_SLEEP" # Very quick increase, so smaller sleep
          done
          done

          # BrightenMonitors

          DimMonitors ()

          local i m NewMathBright Elapsed
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          # Get starting full brightness level, blank monitor name returns zeros
          GetCurrBright "$MonName"
          ((Debug)) && echo "$MonName Starting brightness: $MonMathBright"
          SetByNdx "$m"
          done

          # Dim by increments of 1% for slow fading
          for (( i = 100 ; i >= 0 ; i-- )); do # loop from 100% to 0% brightness
          for (( m = 0 ; m < MonArrCnt ; m=m+MON_COL_CNT )); do #loop for each monitor
          GetByNdx "$m"
          [[ $MonStatus == Disconnected ]] && continue
          NewMathBright=$(( MonMathBright * i / 100 ))
          SetBrightness "$MonName" "$NewMathBright"
          MonPercent="$i"
          SetByNdx "$m"
          done
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          sleep "$FADE_SLEEP" # slowly dim so longer sleep
          Elapsed=$(xprintidle)
          [[ $Elapsed -lt 2000 ]] && break
          done

          [[ "$BACK" != "" ]] && echo 4 > "/sys/class/backlight/$BACK/bl_power"

          # DimMonitors

          GetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonNumber="$MonArr[$1]" # 3rd party monitor number, eg "3"
          MonName="$MonArr[$1+1]" # Xrandr Monitor name, eg "DP-1-1"
          MonStatus="$MonArr[$1+2]" # Xrandr Connected/Disconnected
          MonCurrBright="$MonArr[$1+3]" # Value when dimming first starts eg 1.00
          MonMathBright="$MonArr[$1+4]" # Math implied decimal 100 = 1.00, 50 = .5
          MonPercent="$MonArr[$1+5]" # Last percentage brightness set
          # GetByNdx

          SetByNdx ()
          # Parm1 = index number, 0 = first monitor array entry
          MonArr[$1]="$MonNumber" # 3rd party monitor number, eg "3"
          MonArr[$1+1]="$MonName" # Xrandr Monitor name, eg "DP-1-1"
          MonArr[$1+2]="$MonStatus" # Xrandr Connected/Disconnected
          MonArr[$1+3]="$MonCurrBright" # Value when dimming first starts eg 1.00
          MonArr[$1+4]="$MonMathBright" # Implied decimal 100 = 1.00, 50 = .5
          MonArr[$1+5]="$MonPercent" # Last percentage brightness set


          InitializeMonArr ()

          # Initialize arrays with Xrandr name, status, curret bright, math bright, %
          MonArr=()
          MonArr+=( "1" "$MON1" "Cconnected" "1.0" "100" "0" )
          GetByNdx 0
          SetByNdx 0
          MonArr+=( "2" "$MON2" "Disconnected" ".0" "0" "0" )
          GetByNdx 1
          SetByNdx 1
          MonArr+=( "3" "$MON3" "Connected" "1.0" "100" "0" )
          GetByNdx 2
          SetByNdx 2

          MON_COL_CNT=6
          MonArrCnt="$#MonArr[@]"

          # InitializeMonArr

          Dimmed=false
          InitializeMonArr
          if ((Debug)) ; then
          echo "Starting movie.sh"
          echo "Monitor Array Dump: #, Name, Curr Bright, Math Bright, Last Percent"
          echo "$MonArr[*]"
          fi

          # Loop until CTRL + C
          while true ; do

          Elapsed=$(xprintidle)

          if [[ $Elapsed -lt "$TIME_TO_WAKE" ]] ; then
          if [[ $Dimmed == true ]] ; then
          ((Debug)) && echo -e $'n'"Wake after: $Elapsed milliseconds"
          Dimmed=false
          BrightenMonitors
          EnableAutoBright
          fi
          elif [[ $Elapsed -gt "$TIME_TO_FADE" ]] ; then
          if [[ $Dimmed == false ]] ; then
          ((Debug)) && echo -e $'n'"Fade after: $Elapsed milliseconds"
          Dimmed=true
          DisableAutoBright
          # TODO: THey jiggle mouse whilst dimming to stop
          DimMonitors
          continue # Avoid first sleep incase mouse moved.
          fi
          fi

          sleep 1 # Wait a sec until next keyboard / mouse idle test

          done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 1 at 14:36

























          answered Jul 1 at 0:14









          WinEunuuchs2UnixWinEunuuchs2Unix

          53.5k14 gold badges103 silver badges206 bronze badges




          53.5k14 gold badges103 silver badges206 bronze badges







          • 2





            Impressive, that's far more readable and maintainable than I expected.

            – Mast
            Jul 1 at 9:03












          • 2





            Impressive, that's far more readable and maintainable than I expected.

            – Mast
            Jul 1 at 9:03







          2




          2





          Impressive, that's far more readable and maintainable than I expected.

          – Mast
          Jul 1 at 9:03





          Impressive, that's far more readable and maintainable than I expected.

          – Mast
          Jul 1 at 9:03

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1155093%2fturn-off-all-monitors-while-watching-vlc-media-on-tv%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

          Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

          Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림