views:

877

answers:

10

I have a piece of server-ish software written in Java to run on Windows and OS X. (It is not running on a server, but just a normal user's PC - something like a torrent client.) I would like the software to signal to the OS to keep the machine awake (prevent it from going into sleep mode) while it is active.

Of course I don't expect there to be a cross platform solution, but I would love to have some very minimal C programs/scripts that my app can spawn to inform the OS to stay awake.

Any ideas?

A: 

Run a command inside a timer like pinging the server..

Gulzar
A: 

Wouldn't it be easier to disable the power management on the server? It might be argued that servers shouldn't go into powersave mode?

zigdon
I don't think it is a server. I think it runs on a workstation.
Keng
Correct, it is meant to run on a user's workstation and therefore I do not want to change any system settings. But good thought.
Frank Krueger
+1  A: 

I have a very brute-force technique of moving the mouse 1 point in the x direction and then back every 3 minutes.

There may me a more elegant solution but it's a quick fix.

Keng
+6  A: 

On Windows, use the SystemParametersInfo function. It's a Swiss army-style function that lets you get/set all sorts of system settings.

To disable the screen shutting off, for instance:

SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0 );

Just be sure to set it back when you're done...

Matt Dillard
Very bad idea. If the program crashes then you've just changed user settings without permission. You should process the WM_POWERBROADCAST message instead.
Steve Hanov
This is definitely the danger of using this approach; this can be somewhat mitigated (in C++) by creating a class which makes this call in its constructor, and resets it in the destructor.Processing the WM_POWERBROADCAST event may also work, but only with certain flavors of Windows.
Matt Dillard
A: 

I'd just do a function (or download a freebie app) that moves the mouse around. Inelegant, but easy.

Paulj
+1  A: 

I use this code to keep my workstation from locking. It's currently only set to move the mouse once every minute, you could easily adjust it though.

It's a hack, not an elegant solution.

import java.awt.*;
import java.util.*;
public class Hal{

    public static void main(String[] args) throws Exception{
     Robot hal = new Robot();
     Random random = new Random();
     while(true){
      hal.delay(1000 * 60);
      int x = random.nextInt() % 640;
      int y = random.nextInt() % 480;
      hal.mouseMove(x,y);
     }
    }
}
ScArcher2
Why not just move the mouse 1 pixel as @Keng mentioned? This seems like it would drive the user nuts.
Frank Krueger
You could do that, but I didn't need to. I just posted the code I'm using. It took me like 2 minutes to write, and I haven't needed to change it. If you want to post a modified version to move it one pixel that would be cool.
ScArcher2
I just made a "mousemover" like that some years ago: http://simu.wikidot.com/java:java#toc0 :-)
Carlos Heuberger
+3  A: 

Wouldn't all the suggestions moving the mouse back and forth drive the user crazy? I know I'd remove any app that would do that as soon as I can isolate it.

zigdon
moving it one pixel isn't noticable to anyone but cyborgs and cyborgs just devnull frustration anyway so your all good.
Keng
+1  A: 

I've been using pmset to control sleep mode on my Mac for awhile now, and it's pretty easy to integrate. Here's a rough example of how you could call that program from Java to disable/enable sleep mode. Note that you need root privileges to run pmset, and therefore you'll need them to run this program.

import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * Disable sleep mode (record current setting beforehand), and re-enable sleep
 * mode. Works with Mac OS X using the "pmset" command.
 */
public class SleepSwitch {

    private int sleepTime = -1;

    public void disableSleep() throws IOException {
        if (sleepTime != -1) {
            // sleep time is already recorded, assume sleep is disabled
            return;
        }

        // query pmset for the current setting
        Process proc = Runtime.getRuntime().exec("pmset -g");
        BufferedInputStream is = new BufferedInputStream(proc.getInputStream());
        StringBuffer output = new StringBuffer();
        int c;
        while ((c = is.read()) != -1) {
            output.append((char) c);
        }
        is.close();

        // parse the current setting and store the sleep time
        String outString = output.toString();
        String setting = outString.substring(outString.indexOf(" sleep\t")).trim();
        setting = setting.substring(7, setting.indexOf(" ")).trim();
        sleepTime = Integer.parseInt(setting);

        // set the sleep time to zero (disable sleep)
        Runtime.getRuntime().exec("pmset sleep 0");
    }

    public void enableSleep() throws IOException {
        if (sleepTime == -1) {
            // sleep time is not recorded, assume sleep is enabled
            return;
        }

        // set the sleep time to the previously stored value
        Runtime.getRuntime().exec("pmset sleep " + sleepTime);

        // reset the stored sleep time
        sleepTime = -1;
    }
}
David Crow
Good answer, but in this case I wouldn't dare ask for elevation. What's the point of security if every app asks for special privileges?
Frank Krueger
True, it is inconvenient to grant privileges for minor tasks. But if you wrap pmset in a suid script, that could solve the problem (the script can be executed as root without prompting). Of course, suid scripts can be potential security holes...
David Crow
+4  A: 

I've heard stories of people getting a large sub-woofer, then duct taping a box lid to the top. You can then put the mouse in the box and turn up the music. Preferably something with a lot of bass that will keep the mouse moving around.

Casey Watson
+1 for hilarity :)
Eric Wendelin
Dude, you had me rolling on the floor!
Ogre Psalm33
There was a Youtube video (which I'm unable to find) of the "ultimate AFK hack" for WoW (going idle teleports you from PvP zones and thus stops you from leeching points from your team's efforts). It was an oscillating electric fan, with a pencil taped to its back. Every few seconds, when the fan turned in one direction, the pen hit the jump button and so the player was kept non-idle. Last I heard, the anti-hack module bundled with WoW couldn't detect that :)
gustafc
A: 

You can use the program Caffeine caffiene to keep your workstation awake. You could run the program via the open command in os X.

Milhous