我正在开发 Qt/C++ 应用程序,我需要简单的函数来在 Mac OS X 上以秒为单位检索用户空闲时间。
我发现这个代码用于检测用户空闲时间。
#include <IOKit/IOKitLib.h>
/**
Returns the number of seconds the machine has been idle or -1 if an error occurs.
The code is compatible with Tiger/10.4 and later (but not iOS).
*/
int64_t SystemIdleTime(void) {
int64_t idlesecs = -1;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj) {
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
idlesecs = (nanoseconds >> 30); // Divide by 10^9 to convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
}
如何将此代码转换为 C++,以便与我的 Qt/C++ 项目一起使用?
你只需要添加IOKit.framework
在链接框架列表中。将框架视为共享库和相关资源的捆绑包。IOKit.framework
is at
/System/Library/Frameworks/IOKit.framework
我不知道如何在 Qt 项目中做到这一点;该项目应该有一个您想要链接的额外框架的列表。
如果它是一个标准的 XCode 项目,则有一个名为的菜单项add a framework to the project
或类似的东西。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)