http://www.perlmonks.org?node_id=745114

My company laptop has some remnants from the previous IT regime's mindset (one that is all too common) and so I'm not allowed to adjust how quickly the screen-saver / desktop-locker kicks in.

I have shows that get recorded for me but that I rarely have the time to sit down and watch. So I transfer them to my work laptop and watch them (mostly just listen to them, actually) during my bus commute.

I finally got a really nice biking backpack I've wanted so now I can even listen to them when I commute by bicycle and I even have a little program to cue up cuts to skip over the commercials for me.

The first day that I tried that, playback stopped about half-way to the office. I stopped and checked the laptop and the screensaver had kicked in. At work I googled and got the suggestion that I run Windows Media Player with its volume turned down while it was configured to prevent screen-saver activation. The ride home demonstrated that this is not sufficient to prevent administratively mandated screen-saver timeouts.

More googling at home mostly found people noting that when one doesn't have administrative rights, one can't install a program to thwart the screen-saver activation anyway so no such programs are offered.

So I wrote the following Perl program:

#!/usr/bin/perl -w use strict; use Win32::GuiTest qw< GetCursorPos MouseMoveAbsPix >; sleep 5; my( $x, $y )= GetCursorPos(); my @dx= ( 10, -10, -10, 10 ); my @dy= ( 10, 10, -10, -10 ); while( 1 ) { sleep 1; my( $x1, $y1 )= GetCursorPos(); exit if( $x != $x1 || $y != $y1 ); $x += $dx[0]; push @dx, shift @dx; $y += $dy[0]; push @dy, shift @dy; MouseMoveAbsPix( $x, $y ); }

It just waits five seconds and then makes the mouse pointer move around in a nice little diamond pattern until you bump your mouse.

This morning's bike commute I enjoyed the audio from uninterrupted video playback. Thanks, Perl.

- tye        

Replies are listed 'Best First'.
Re: Keep the playback rolling (Win32)
by BrowserUk (Pope) on Feb 20, 2009 at 11:58 UTC

    You can disable the screensaver for the duration of a program using this. Supply the normal command line for the app as the arguments to this. To test it, set your screensaver timeout to 1 minute and run a command like:

    noss perl -le"sleep(1), print for 1 .. 61"

    noss.pl:

    #! perl -slw use strict; use Win32::API::Prototype; ApiLink( 'user32', q[ BOOL SystemParametersInfo( UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni ) ] ) or die $^E; SystemParametersInfo( 0x0011, 0, 0, 0 ) or die $^E; ## SS off system @ARGV; SystemParametersInfo( 0x0011, 1, 0, 0 ) or die $^E; ## SS on

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I would guess that this is along the lines of what Windows Media Player does to disable the screen-saver and so it seems likely to not work for peons trying to disable administratively mandated screen-saver timeouts. But that is just a guess.

      - tye        

Re: Keep the playback rolling (Win32)
by Lawliet (Curate) on Feb 20, 2009 at 04:04 UTC

    I knew this sounded familiar!

    And you didn't even know bears could type.

Re: Keep the playback rolling (Win32)
by Anonymous Monk on Feb 20, 2009 at 11:06 UTC
    In doing something similar I found that you don't have to visibily move the mouse pointer to keep the screensaver at bay (although you obviously lose your cool diamond effect!)
    use Win32::GuiTest qw(SendMouseMoveRel); while (1) { SendMouseMoveRel(0,0); sleep 1; }
      I think at 0,0 its still visible, you need -10,-10 ;D
Re: Keep the playback rolling (Win32)
by pmonk4ever (Friar) on Feb 20, 2009 at 01:36 UTC
    Thanks, Tye!

    I too suffer from that particular disease and your solution is being put to use as soon as I finish this reply! Wow, what a relief!

    ki6jux

    "No trees were harmed in the creation of this node. However, a rather large number of electrons were somewhat inconvenienced."

Re: Keep the playback rolling (Win32)
by BrowserUk (Pope) on Apr 03, 2009 at 06:00 UTC

    A variation on your theme, I for the first time had a use for this--watching F1 practice sessions via the web and my screen saver kept kicking in. As I can't use Win32::API, I grabbed your code.

    There were a couple of things that bugged me. I found the diamond pattern a little distracting. And if I used the mouse to adjust the volume or dismiss an email notification, the loop terminated and I had to switch away to restart it.

    So I reduced the movement to a simple +1/-1 giggle at a reduced frequency and made it so that it wouldn't quit the loop until I moved the mouse to the top left corner.

    #!/usr/bin/perl -w use strict; use Win32::GuiTest qw< GetCursorPos MouseMoveAbsPix >; my( $x, $y )= GetCursorPos(); my $toggle = 1; while( sleep 5 ) { my( $x1, $y1 )= GetCursorPos(); exit unless $x1 or $y1; ( $x, $y ) = ( $x1, $y1 ), next unless $x == $x1 && $y == $y1; $x = $x1 + $toggle; $y = $y1 + $toggle; $toggle *= -1; MouseMoveAbsPix( $x, $y ); }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      FYI, based on another reply, you don't need to even move the pointer at all. But I haven't tested that (and one might want the subtle movement to remind one that the screen-saver-thwarting is still in effect, in case one wants their screen saved). Thanks for the updated version.

      - tye        

Re: Keep the playback rolling (Win32)
by Anonymous Monk on Mar 10, 2009 at 16:42 UTC
    The code sometimes does not work it exits becuase "y" some times will not be equal to "y1". I dont understand why this is happening. To verify I just printed out the x, x1, y and y1 before the "exit". After couple of iterations it exits and it prints as follows. x=190, y=72, x1=190, y1=72 x=200, y=82, x1=200, y1=81 I observed that Always y1 is less than 1.

      It might be because your optical mouse decided to jiggle the pointer one pixel? You could certainly add a tolerance for how much the mouse pointer needs to move to terminate it or remove that check entirely and just Ctrl-C the script when you want it to stop. This sensitive test is fine for my particular use case. Note that it also fails if the mouse pointer is too close to the wrong screen edge.

      This pl2bat-wrapped version avoids both such problems:

      @rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -x -S %0 %* if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #!perl -w #line 15 use strict; use Win32::GuiTest qw< GetCursorPos MouseMoveAbsPix >; sleep 5; my( $x, $y )= GetCursorPos(); my @dx= ( 10, -10, -10, 10 ); my @dy= ( 10, 10, -10, -10 ); while( 1 ) { sleep 1; my( $x1, $y1 )= GetCursorPos(); exit if( 10 < abs( $x - $x1 ) || 10 < abs( $y - $y1 ) ); $x += $dx[0]; push @dx, shift @dx; $y += $dy[0]; push @dy, shift @dy; MouseMoveAbsPix( $x, $y ); } __END__ :endofperl

      - tye