Convert Objective-C code in C ++ to determine user downtime in OS X

I am developing a Qt / C ++ application and I need a simple function that returns me user downtime in seconds on Mac OS X.

I found this code to detect user downtime.

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

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

+3
source share
1 answer

You just need to add IOKit.frameworkto the list of related frameworks. Think of a structure as a collection of shared libraries and related resources. IOKit.frameworklocated in

 /System/Library/Frameworks/IOKit.framework

, Qt; , . XCode, , add a framework to the project - .

+3

Source: https://habr.com/ru/post/1761102/


All Articles