簡體   English   中英

將Objective-C代碼轉換為C ++以檢測OS X上的用戶空閑時間

[英]Convert Objective-C code to C++ for detecting user idle time on OS X

我正在開發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

 /System/Library/Frameworks/IOKit.framework

我不知道如何在Qt項目中做到這一點; 該項目應該有一個您想要鏈接的額外框架列表。 如果它是一個標准的XCode項目,那么有一個名為add a framework to the project的菜單項或類似的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM