首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >你如何让机器保持清醒?

你如何让机器保持清醒?
EN

Stack Overflow用户
提问于 2008-09-09 20:50:12
回答 18查看 44.3K关注 0票数 32

我有一个用Java编写的服务器软件,可以在Windows和OS X上运行。(它不是在服务器上运行,而是在普通用户的PC上运行--有点像torrent客户端。)我希望软件向操作系统发出信号,使机器在活动时保持唤醒(防止其进入睡眠模式)。

当然,我不希望有一个跨平台的解决方案,但我希望有一些非常小的C程序/脚本,我的应用程序可以产生通知操作系统保持清醒。

有什么想法吗?

EN

Stack Overflow用户

发布于 2021-05-08 06:11:41

为了配合用户Gili提供的使用JNA的Windows解决方案,这里是用于MacOS的JNA解决方案。

首先,JNA库接口:

代码语言:javascript
复制
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.mac.CoreFoundation;
import com.sun.jna.ptr.IntByReference;

public interface ExampleIOKit extends Library {
    ExampleIOKit INSTANCE = Native.load("IOKit", ExampleIOKit.class);

    CoreFoundation.CFStringRef kIOPMAssertPreventUserIdleSystemSleep = CoreFoundation.CFStringRef.createCFString("PreventUserIdleSystemSleep");
    CoreFoundation.CFStringRef kIOPMAssertPreventUserIdleDisplaySleep = CoreFoundation.CFStringRef.createCFString("PreventUserIdleDisplaySleep");

    int kIOReturnSuccess = 0;

    int kIOPMAssertionLevelOff = 0;
    int kIOPMAssertionLevelOn = 255;

    int IOPMAssertionCreateWithName(CoreFoundation.CFStringRef assertionType,
                                    int assertionLevel,
                                    CoreFoundation.CFStringRef reasonForActivity,
                                    IntByReference assertionId);

    int IOPMAssertionRelease(int assertionId);
}

下面是调用JNA方法打开或关闭睡眠保护的示例:

代码语言:javascript
复制
public class Example {
    private static final Logger _log = LoggerFactory.getLogger(Example.class);

    private int sleepPreventionAssertionId = 0;

    public void updateSleepPrevention(final boolean isEnabled) {
        if (isEnabled) {
            if (sleepPreventionAssertionId == 0) {
                final var assertionIdRef = new IntByReference(0);
                final var reason = CoreFoundation.CFStringRef.createCFString(
                    "Example preventing display sleep");
                final int result = ExampleIOKit.INSTANCE.IOPMAssertionCreateWithName(
                    ExampleIOKit.kIOPMAssertPreventUserIdleDisplaySleep,
                    ExampleIOKit.kIOPMAssertionLevelOn, reason, assertionIdRef);
                if (result == ExampleIOKit.kIOReturnSuccess) {
                    _log.info("Display sleep prevention enabled");
                    sleepPreventionAssertionId = assertionIdRef.getValue();
                }
                else {
                    _log.error("IOPMAssertionCreateWithName returned {}", result);
                }
            }
        }
        else {
            if (sleepPreventionAssertionId != 0) {
                final int result = ExampleIOKit.INSTANCE.IOPMAssertionRelease(sleepPreventionAssertionId);
                if (result == ExampleIOKit.kIOReturnSuccess) {
                    _log.info("Display sleep prevention disabled");
                }
                else {
                    _log.error("IOPMAssertionRelease returned {}", result);
                }
                sleepPreventionAssertionId = 0;
            }
        }
    }
}
票数 1
EN
查看全部 18 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档