No Stop No Sleep [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

No Stop No Sleep [Windows]

Post by RASHAD »

Tested with Windows XP,7,8,10 x64

Code: Select all

;LeftClick to Quit

Procedure NoSleep(Para)
  fx.f = $FFFF / GetSystemMetrics_(#SM_CXSCREEN)
  fy.f = $FFFF / GetSystemMetrics_(#SM_CYSCREEN)  
  In.INPUT
  In\type        = #INPUT_MOUSE
  In\mi\dwFlags  = #MOUSEEVENTF_MOVE|#MOUSEEVENTF_ABSOLUTE
  Repeat
    Delay(3000)
    GetCursorPos_(cur.POINT)
    In\mi\dx       = (cur\x+1)  * fx
    In\mi\dy       = (cur\y+1)  * fy
    SendInput_(1,@In,SizeOf(In))
    SendInput_(1,@In,SizeOf(In))
  ForEver
EndProcedure

IconName$ = #PB_Compiler_Home + "examples/sources/Data/CdPlayer.ico"

OpenWindow(0,0,0,0,0,"",#PB_Window_BorderLess)

AddSysTrayIcon(0, WindowID(0), LoadImage(0, IconName$))

Thread = CreateThread(@NoSleep(),30)
Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_SysTray
      Select EventType()
        Case #PB_EventType_LeftClick
          Quit = 1
      EndSelect    
  EndSelect
Until Quit = 1
Egypt my love
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: No Stop No Sleep [Windows]

Post by freak »

There is an API function specifically for this purpose, so you don't need to simulate fake input: https://msdn.microsoft.com/de-de/librar ... 73208.aspx
quidquid Latine dictum sit altum videtur
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: No Stop No Sleep [Windows]

Post by RSBasic »

Image
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: No Stop No Sleep [Windows]

Post by RASHAD »

freak & RSbasic
Sorry ,I know about that but it did not work
I am doing a MMedia player and that did not work with XP Screen saver SP2 to be exact so I got to see some other way to solve my problem
Beside no harm to have another way
Sorry
Egypt my love
User avatar
Roger Hågensen
User
User
Posts: 47
Joined: Wed Mar 25, 2015 1:06 pm
Location: Norway

Re: No Stop No Sleep [Windows]

Post by Roger Hågensen »

If it didn't work you did it wrong.

As per https://msdn.microsoft.com/en-us/librar ... s.85).aspx

"This function does not stop the screen saver from executing."

"to keep the display or system in the working state, the thread must call SetThreadExecutionState periodically."

ES_CONTINUOUS and ES_DISPLAY_REQUIRED should be the only flags you need.

Unless i misread it you should use SetThreadExecutionState(ES_CONTINUOUS|ES_DISPLAY_REQUIRED)
and when done playing you use SetThreadExecutionState(ES_CONTINUOUS) to clear the display flag, the documentation does not say what passing 0 does so do not rely on that.

Alternatively you can use then periodically use SetThreadExecutionState(ES_DISPLAY_REQUIRED) periodically, try once a minute first (I think that is the minimum screen saver setting).


Also note that your code could be a issue at least for people like me that watch video and do other stuff at the same time, I'd never use a player that hogs the mouse pointer preveting me from doing anything else.
And before you say "this will only be used in fullscreen", well what about those with multiple monitors? I often have video fullscreen on my main monitor but other stuff on my secondary monitor.

__________________________________________________
URL tags added
30.09.2016
RSBasic
4 music albums under CC BY license available for free (any use, even commercial) at Skuldwyrm.no
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: No Stop No Sleep [Windows]

Post by RASHAD »

Hi
Should I believe you or MSDN and myself
MSDN says(Read your MSDN link carefully and you did it all ready)
This function does not stop the screen saver from executing.
If it will then MSDN should say use it periodically even if you are using ES_CONTINUOUS

Beside using it periodically just in case
Calling SetThreadExecutionState without ES_CONTINUOUS simply resets the idle timer; to keep the display or system in the working state, the thread must call SetThreadExecutionState periodically.
Did not work with XP SP2 by testing all options (Sorry)
I am not the only one who did that Symantic and Nirsoft and others did it before
I made it available in PB instead of running external exe

Seems everything running smoothly the Player and any application at the same time
Reading and talking can not take over exercising and testing
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2807
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: No Stop No Sleep [Windows]

Post by Michael Vogel »

I am using the following program for windows, which helps to keep my notebook running overnight if needed.

You can use a combination of characters as a start parameter to change the behaviour of the tool, a short information about this will be displayed when using the parameter '?'. While the program is active, you can also use the keyboard to toggle some options, like 'd' to keep the display active or not.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: No Stop No Sleep [Windows]

Post by RASHAD »

Hi MV
I can not depend on external tool
The code embedded into the MMplayer code
It runs when you run your code so if your application crashed for some reason your system still have its defaults no problem
This technique is foolproof for all Windows versions(By testing) and so simple
and no side effects :wink:

Some of other techniques if you want to read some more
http://www.codeproject.com/Tips/125769/ ... creensaver

Code: Select all

SystemParametersInfo_(#SPI_SETSCREENSAVEACTIVE, 1, 0, #SPIF_SENDWININICHANGE)

;****************************************************************

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg)                  
  {
    Case WM_SYSCOMMAND:
    {
      switch (wParam)
      {
        Case SC_SCREENSAVE:  
          Return 0;
        Case SC_MONITORPOWER:
          Return 0;      
      }
      Break;      
    }

    Case WM_CLOSE:                
    {
      PostQuitMessage(0);            
      Return 0;        
    }
  }
  Return DefWindowProc(hWnd,uMsg,wParam,lParam);
  
  ;****************************************************************
 [ SMPlayer Support Forum
  
  Index: src/screensaver.cpp
==================================================
--- src/screensaver.cpp   (revision 5760)
+++ src/screensaver.cpp   (working copy)
@@ -85,7 +85,8 @@
          SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, poweroff, NULL, 0);
       }
       SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, screensaver, NULL, 0);
-      
+        SetThreadExecutionState(ES_CONTINUOUS);
+
       qDebug("WinScreenSaver::restoreState: lowpower: %d, poweroff: %d, screensaver: %d", lowpower, poweroff, screensaver);
 #else
       If (SSCore_TempEnable) {
@@ -117,6 +118,7 @@
       SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
    }
    SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
+    SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
 #else
    If (SSCore_TempDisable) {
       SSCore_TempDisable();
Index: src/screensaver.h
==================================================
--- src/screensaver.h   (revision 5760)
+++ src/screensaver.h   (working copy)
@@ -23,6 +23,8 @@
 
 #ifdef Q_OS_OS2
 #include <QLibrary>
+#else
+#define _WIN32_WINNT 0x0501
 #endif
 
 class WinScreenSaver
 
 ;***************************************************************
 To disable Or enable screen saver using registry key

Set the value of the registry value ScreenSaveActive To 0. From command line you can run the below command For doing this.

Reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t REG_SZ /d 0 /f

To enable screen saver:

Reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t REG_SZ /d 1 /f
  
in Case of a ctrl+alt+del Or a crash the screensaver won't go back to normal

SMPlayer one of the best mmedia players at the moment
I just picked Mouse jiggler to be my favorite
Egypt my love
Post Reply