将Objective-C代码转换为C ++,以检测OS X上的用户空闲时间 [英] Convert Objective-C code to C++ for detecting user idle time on OS X

查看:77
本文介绍了将Objective-C代码转换为C ++,以检测OS X上的用户空闲时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Qt/C ++应用程序,我需要简单的功能来检索我在Mac OS X上的用户空闲时间(以秒为单位).

I'm developing Qt/C++ application and I need simple function that retrive me User idle time in seconds on Mac OS X.

我找到了用于检测用户空闲时间的代码.

I found this code for detection User idle time.

#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;
}    

如何将此代码转换为与Qt/C ++项目一起使用的C ++?

How to convert this code to C++, to be used with my Qt/C++ project?

推荐答案

您只需要在链接框架的列表中添加IOKit.framework.将框架视为共享库和相关资源的捆绑. IOKit.framework位于

You just need to add IOKit.framework in the list of linked frameworks. Think of a framework as a bundle of shared libraries and associated resources. IOKit.framework is at

 /System/Library/Frameworks/IOKit.framework

我不知道如何在Qt项目中做到这一点;该项目应具有要链接的其他框架的列表. 如果是标准的XCode项目,则有一个名为add a framework to the project或类似名称的菜单项.

I don't know how to do that in a Qt project; the project should have a list of extra frameworks which you want to link against. If it's a standard XCode project, there's a menu entry called add a framework to the project or something like that.

这篇关于将Objective-C代码转换为C ++,以检测OS X上的用户空闲时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆