C++ 将Objective-C代码转换为C++;用于检测OS X上的用户空闲时间
我正在开发Qt/C++应用程序,我需要一个简单的函数,可以在MacOSX上以秒为单位检索用户空闲时间 我发现这个代码用于检测用户空闲时间C++ 将Objective-C代码转换为C++;用于检测OS X上的用户空闲时间,c++,macos,qt,operating-system,C++,Macos,Qt,Operating System,我正在开发Qt/C++应用程序,我需要一个简单的函数,可以在MacOSX上以秒为单位检索用户空闲时间 我发现这个代码用于检测用户空闲时间 #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
#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;
}
#包括
/**
返回机器空闲的秒数,如果发生错误,返回-1。
该代码与Tiger/10.4及更高版本(但不是iOS)兼容。
*/
int64_t SystemIdleTime(无效){
int64_t idlesecs=-1;
io迭代器iter=0;
if(IOServiceGetMatchingServices(kiomaterportdefault、IOServiceMatching(“IOHIDSystem”)、&iter)==KERN_SUCCESS){
io_注册表_entry_t entry=IOIteratorNext(iter);
如果(条目){
CFMutableDictionaryRef dict=NULL;
if(IorRegistryEntryCreateCfProperties(entry,&dict,kCFAllocatorDefault,0)=KERN_SUCCESS){
CFNumberRef obj=CFDictionaryGetValue(dict,CFSTR(“HIDIdleTime”);
如果(obj){
int64_t纳秒=0;
if(CFNumberTargetValue(obj、kCFNumberSInt64Type和纳秒)){
idlesecs=(纳秒>>30);//除以10^9可将纳秒转换为秒。
}
}
CFRelease(dict);
}
发布(条目);
}
国际热核实验堆;
}
返回idlesecs;
}
如何将此代码转换为C++,用于与QT/C++项目一起使用?< P>你只需要在链接框架列表中添加<代码> IOKIT.Frase< /C> >。将框架视为共享库和相关资源的捆绑包<代码>IOKit.framework位于
/System/Library/Frameworks/IOKit.framework
我不知道如何在Qt项目中做到这一点;项目应该有一个你想要链接的额外框架的列表。
如果它是一个标准的XCode项目,则会有一个名为“向项目添加框架”的菜单项?这是直C,已经可以在C++项目中使用。我在链接器上有问题:“IOIOSReVIEGETMatCHIGIN服务”等等,没有找到符号。它编译没有问题,但不想看一看,谢谢。我使用XCode,这很有帮助。