画面が眠っまたは覚醒している場合C
このビットが検出されます。
CoreGraphics.java
:
import com.sun.jna.*;
import com.sun.jna.ptr.IntByReference;
public interface CoreGraphics extends Library {
CoreGraphics INSTANCE = (CoreGraphics)Native.loadLibrary("CoreGraphics", CoreGraphics.class);
class int32_t extends IntegerType {
public static final int SIZE = 4;
public int32_t() {
this(0);
}
public int32_t(long value) {
super(SIZE, value, false);
}
};
public static class CGError extends int32_t {
public static final CGError Success = new CGError(0);
public CGError() { this(0); }
public CGError(int value) { super(value); }
};
public static class CGDirectDisplayID extends int32_t {
};
CGError CGGetActiveDisplayList(int maxDisplays, CGDirectDisplayID[] activeDisplays, IntByReference displayCount);
boolean CGDisplayIsAsleep(CGDirectDisplayID disp);
}
例プログラム
CGDirectDisplayID ids[20];
uint32_t num_ids;
if (kCGErrorSuccess != CGGetActiveDisplayList(20, ids, &num_ids)) {
printf("Oops\n");
return 1;
}
boolean_t asleep = true;
for (int i = 0; i < num_ids; i++) {
asleep &= CGDisplayIsAsleep(ids[i]);
}
if (asleep) {
printf("Asleep\n");
return 1;
} else {
printf("Awake\n");
return 0;
}
これはhorridly使用して、いくつかのJNAに変換することができますインターフェイスを利用する - jna_monitors_run.java
:
import com.sun.jna.*;
import com.sun.jna.ptr.IntByReference;
public class jna_monitors_run {
static final int MAX_DISPLAYS = 20;
public static void main(String[] args) {
IntByReference ib = new IntByReference();
CoreGraphics.CGDirectDisplayID ids[] = new CoreGraphics.CGDirectDisplayID[MAX_DISPLAYS];
if (! CoreGraphics.CGError.Success.equals(CoreGraphics.INSTANCE.CGGetActiveDisplayList(
MAX_DISPLAYS, ids, ib))) {
System.exit(2);
}
boolean is_asleep = true;
int i = ib.getValue();
while (--i >= 0) {
is_asleep &= CoreGraphics.INSTANCE.CGDisplayIsAsleep(ids[i]);
}
System.out.println(is_asleep);
System.exit(is_asleep ? 1 : 0);
}
}
どのようにCの作品は動作しますか? – platelminto
私は[サンプルプロジェクト](https://github.com/petesh/IsDisplayAsleep)を使用中のコードを示すgithubに配置しました。 – Petesh
ありがとうございました。ちょっとしたやり直しをしてもうまくいきました。 Pity OS Xはあまり複雑ではありません: – platelminto