LoginSignup
8
7

More than 5 years have passed since last update.

Mac でディスプレイのスリープ状態をプログラムから解除する方法

Last updated at Posted at 2014-10-14
  • ディスプレイがスリープした状態をプログラムから解除する方法がないか調べていたら、OSX のツール caffeinate(8) を見つけた。
  • caffeinate -u とほぼ同様のことを実現するには、以下のように記述すればよいみたい。
  • ディスプレイだけでなくシステムもスリープしている場合に、ただしく動くかどうかは分からない。
#include <IOKit/pwr_mgt/IOPMLib.h>

#ifndef kIOPMAssertionUserIsActive
/* この文字列定数は IOMPLib.h には定義されていないが (他の定数 kIOPMAssertion* は定義されているのに)、
   caffeinate(8) はこの値で IOPMAssertionCreateWithDescription 関数を呼び出しているようだ。*/
#define kIOPMAssertionUserIsActive CFSTR("UserIsActive")
#endif

void AwakeFromDisplaySleep()
{
    CFTimeInterval timeout = 5; /* magic number: caffeinate(8) は -u オプションが渡された場合この値を使うようだ */
    IOPMAssertionID id = 0;
    IOPMAssertionCreateWithDescription(
        kIOPMAssertionUserIsActive,
        CFSTR("TODO: name"),                  /* 適当に設定する */
        CFSTR("TODO: details"),               /* 適当に設定する */
        CFSTR("TODO: human readable reason"), /* 適当に設定する */
        CFSTR("/System/Library/CoreServices/powerd.bundle"),
        timeout,
        kIOPMAssertionTimeoutActionRelease,
        &id);
}

編集履歴

  • macOS Sierra でも動いたので記事のタイトルを変更した。
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7