How could I temporarily disable screensaver?

General FreeBASIC programming questions.
Post Reply
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

How could I temporarily disable screensaver?

Post by Fox »

Hi,

I am working on a little FreeBASIC program, which do not take any keyboard/mouse input. After few minutes of work, my OS (linux) is starting a screensaver, and I have to press any key or move the mouse to get back to my program.

Is there any (hopefully portable) way to prevent the OS from launching any screensaver when my program runs?
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Post by sir_mud »

is this Xscreensaver or a DE provided screensaver utility?
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

Post by Fox »

sir_mud wrote:is this Xscreensaver or a DE provided screensaver utility?
From your question I guess that my hope for a portable way is hopeless :-P

In my case, this is a the KDE power management that shuts my screen off, but it's me - for other users it could be a Gnome screensaver, or a Windows *.scr...
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

Post by Fox »

Okay, I looked into the sourcode of VLC (which is able to disable screensaver on my PC), and I found out that it calls simply some commands in a periodic way:

Code: Select all

xdg-screensaver reset
xscreensaver-command -deactivate
gnome-screensaver-command --poke
I will have to test if it really works for my program, but it seems to be the way to go, at least on Linux...

Now, what could I do to disable screensaver on Windows?
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

Fox wrote:Now, what could I do to disable screensaver on Windows?
On Windows you can periodically send faux mouse or keyboard events to a window. This should fool the screensaver into thinking someone is using the computer.

-Vince
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

Post by Fox »

Hi again,

Here is the code that I am using now to avoid screen saver to happen on my Linux PC (and this code works very well for me!) :)

Code: Select all

SUB InhibitScreenSaver()

  #IFDEF __FB_LINUX__
    SHELL "xdg-screensaver reset"             ' The supposed "standard" way (which doesn't always work)
    SHELL "xscreensaver-command -deactivate"  ' The X way
    SHELL "qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity" ' The D-BUS way (compatible with KDE4)
    SHELL "gnome-screensaver-command --poke"  ' The GNOME way
  #ENDIF

  #IFDEF __FB_WIN32__
  #ENDIF

END SUB
Now, I am looking for some way to achieve the same thing on Windows...
vdecampo wrote: On Windows you can periodically send faux mouse or keyboard events to a window. This should fool the screensaver into thinking someone is using the computer.
Yeah, sure... but how could I send such "faux mouse events"? :-D
Is there some winapi crap out there that would allow me to send such fake signals?
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

Maybe this will work...

Code: Select all

#Include "windows.bi"
'0=Disable / 1=Enable
Function EnableScreenSaver(Enable As Integer) As Integer
   Return IIf(SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, Enable, 0, 0) > 0,1,0)
End Function
Fox
Posts: 353
Joined: Aug 08, 2006 13:39
Location: Lille, France
Contact:

Post by Fox »

vdecampo wrote:Maybe this will work...

Code: Select all

#Include "windows.bi"
'0=Disable / 1=Enable
Function EnableScreenSaver(Enable As Integer) As Integer
   Return IIf(SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, Enable, 0, 0) > 0,1,0)
End Function
Thanks! I quickly tested it on a VirtualBox, and it does seem to disable the screensaver indeed!

However, this would require to call the function one time when the program starts (to disable the screensaver), and then one time when the program ends (to reenable it). And then, if my program doesn't terminates in a clean way (crash, or killed by the user), then I'm leaving the user without his screensaver... I'd like to avoid that as much as possible. In linux, the idea is to reset the screensaver every now and then, and this seems like a much more nice way to deal with screensaver (so it won't kick in as far as my app is resetting the timer from time to time). Any chance to have such behavior on the Windoze platform maybe? :-P
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

Fox wrote:<snip>
And then, if my program doesn't terminates in a clean way (crash, or killed by the user), then I'm leaving the user without his screensaver... I'd like to avoid that as much as possible.
<snip>
Add a destructor that is called when the program terminates normally, and don't write code that crashes.. abnormally.

Better yet, don't write any Windoze code using that "winapi crap" ;-)
Post Reply