在 Lion 上使用 IOKit 进行睡眠显示

发布于 2024-12-09 15:59:51 字数 617 浏览 6 评论 0原文

我有以下代码,应该使 Mac 上的显示器休眠。我在 Lion 上尝试过,但似乎没有任何作用。我通过创建一个准系统窗口 Mac 应用程序来测试代码,该应用程序在窗口中带有一个按钮和一个 IBAction 方法。当按下按钮时,将调用下面的函数,但什么也没有发生。

关于为什么它不起作用有什么建议吗?

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

int display_sleep(void)
{
    io_registry_entry_t reg = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");

    if (reg) 
    {
        IORegistryEntrySetCFProperty(reg, CFSTR("IORequestIdle"), kCFBooleanTrue);
        IOObjectRelease(reg);
    } 
    else 
    {
        return 1;
    }

    return 0;
}

I have the following code that is suppose to sleep the display on a Mac. I've tried it on Lion but it doesn't seem to do anything. I tested the code by creating a barebones window Mac app with a button in the window and an IBAction method. When the button is pressed, the function below is called, however nothing happens.

Any suggestions as to why it doesn't work?

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

int display_sleep(void)
{
    io_registry_entry_t reg = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");

    if (reg) 
    {
        IORegistryEntrySetCFProperty(reg, CFSTR("IORequestIdle"), kCFBooleanTrue);
        IOObjectRelease(reg);
    } 
    else 
    {
        return 1;
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文