swift - 如何在 macOS 10.14 上重置 HIDIdleTime

标签 swift xcode macos

在过去的几天里,我一直在尝试编写一个应用程序来重置 IORegistry > IOHIDSystem > HIDIdleTime 条目。最终目标是防止读取此值的其他应用程序将用户标记为空闲(这不仅与电源管理或防止 sleep 有关)。假设沙盒被禁用并且应用程序具有所有必要的权限(例如可访问性访问)。

以下是我的尝试(目前未成功):

尝试 1 - 移动鼠标光标模拟事件:

变体 1:

let mouseCursorPosition = CGPoint(x: Int.random(in: 0...500), y: Int.random(in: 0...500))
CGWarpMouseCursorPosition(mouseCursorPosition)

变体 2:

CGDisplayMoveCursorToPoint(CGMainDisplayID(), mouseCursorPosition)

变体 3(单独使用 CGEvent 或与上述 2 个变体之一一起使用):

let moveEvent = CGEvent(mouseEventSource: nil, mouseType: 
CGEventType.mouseMoved, mouseCursorPosition: mouseCursorPosition, 
mouseButton: CGMouseButton.left)
moveEvent?.post(tap: CGEventTapLocation.cghidEventTap)

变体 4(使用 IOHIDSetMouseLocation/IOHIDPostEvent):

func moveCursor() {
    let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"))
    if (service == 0) { return }

    var connect:io_connect_t = 0

    let result = IOServiceOpen(service, mach_task_self_, UInt32(kIOHIDParamConnectType), &connect)
    IOObjectRelease(service)

    if (result == kIOReturnSuccess) {
        let cursorX = Int16.random(in: 0...100)
        let cursorY = Int16.random(in: 0...100)

        IOHIDSetMouseLocation(connect, Int32(cursorX), Int32(cursorY))

        let cursorLocation:IOGPoint = IOGPoint(x: cursorX, y: cursorY)

        var event:NXEventData = NXEventData()
        IOHIDPostEvent(connect, UInt32(NX_MOUSEMOVED), cursorLocation, &event, 0, 0, 0)
    }
}

注意:我后来了解到,从 macOS 10.12 开始,IOHIDPostEvent 不会重置 HIDIdleTime(来源:https://github.com/tekezo/Karabiner-Elements/issues/385) .还尝试模拟按键但没有成功。

尝试 2 - 直接覆盖 IORegistry

中的值
func overwriteValue() -> Bool {
    var iterator: io_iterator_t = 0
    defer { IOObjectRelease(iterator) }
    guard IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iterator) == kIOReturnSuccess else { return false }

    let entry: io_registry_entry_t = IOIteratorNext(iterator)
    defer { IOObjectRelease(entry) }
    guard entry != 0 else { return false }

    var value:NSInteger = 0;
    var convertedValue:CFNumber = CFNumberCreate(kCFAllocatorDefault, CFNumberType.nsIntegerType, &value);
    let result = IORegistryEntrySetCFProperty(entry, "HIDIdleTime" as CFString, convertedValue)

    if (result != kIOReturnSuccess) { return false }

    return true
}

虽然这似乎可行(上面的函数返回 true),但该值随后被系统覆盖,系统跟踪内存中的实际空闲时间。从 Apple 为 IOHIDSystem 发布的源代码中对此有了一些了解 here .当前使用this script轻松监控系统空闲时间和测试解决方案。

非常感谢任何建议。如果可能的话,我尽量避免编写自己的虚拟驱动程序(尽管我愿意连接到现有的虚拟驱动程序并尽可能模拟事件)。

最佳答案

关键是注册表属性不是普通属性,而是在每次查询属性时即时生成(请参阅 the source 中的 _idleTimeSerializerCallback)。
长话短说,您需要强制重置 lastUndimEvent,这可以使用 IOHIDParamUserClient 的外部方法 6 来完成。

我不会说 Swift,但这里有一些 C 代码可以做到这一点:

// clang -o t t.c -Wall -O3 -framework CoreFoundation -framework IOKit
#include <stdio.h>
#include <stdint.h>
#include <mach/mach.h>
#include <CoreFoundation/CoreFoundation.h>

extern const mach_port_t kIOMasterPortDefault;

typedef mach_port_t io_object_t;
typedef io_object_t io_service_t;
typedef io_object_t io_connect_t;

kern_return_t IOObjectRelease(io_object_t object);
CFMutableDictionaryRef IOServiceMatching(const char *name) CF_RETURNS_RETAINED;
io_service_t IOServiceGetMatchingService(mach_port_t master, CFDictionaryRef matching CF_RELEASES_ARGUMENT);
kern_return_t IOServiceOpen(io_service_t service, task_t task, uint32_t type, io_connect_t *client);
kern_return_t IOServiceClose(io_connect_t client);
kern_return_t IOConnectCallScalarMethod(io_connect_t client, uint32_t selector, const uint64_t *in, uint32_t inCnt, uint64_t *out, uint32_t *outCnt);

const uint32_t kIOHIDParamConnectType             = 1;
const uint32_t kIOHIDActivityUserIdle             = 3;
const uint32_t kIOHIDActivityReport               = 0;
const uint32_t kIOHIDParam_extSetStateForSelector = 6;

#define LOG(str, args...) do { fprintf(stderr, str "\n", ##args); } while(0)

int hid_reset(void)
{
    int retval = -1;
    kern_return_t ret = 0;
    io_service_t service = MACH_PORT_NULL;
    io_connect_t client = MACH_PORT_NULL;

    service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"));
    LOG("service: %x", service);
    if(!MACH_PORT_VALID(service)) goto out;

    ret = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, &client);
    LOG("client: %x, %s", client, mach_error_string(ret));
    if(ret != KERN_SUCCESS || !MACH_PORT_VALID(client)) goto out;

    uint64_t in[] = { kIOHIDActivityUserIdle, kIOHIDActivityReport };
    ret = IOConnectCallScalarMethod(client, kIOHIDParam_extSetStateForSelector, in, 2, NULL, NULL);
    LOG("extSetStateForSelector: %s", mach_error_string(ret));
    if(ret != KERN_SUCCESS) goto out;

    retval = 0;

out:;
    if(MACH_PORT_VALID(client)) IOServiceClose(client);
    if(MACH_PORT_VALID(service)) IOObjectRelease(service);
    return retval;
}

int main(void)
{
    return hid_reset();
}

它在 High Sierra 上作为非 root 对我有效,还没有在其他地方测试过。不过我确实运行了一个非标准的系统配置,所以如果你在外部方法上收到一个错误提示 (iokit/common) not permitted,很可能你正在点击 mac_iokit_check_hid_control 并且可能需要额外的权利、可访问性许可或类似的东西。

关于swift - 如何在 macOS 10.14 上重置 HIDIdleTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52891033/

相关文章:

ios - 如何动画逆时针旋转

swift - iOS - 在导航栏的标题中添加图像和文本

ios - 我的 UIButtons 有很大的滞后?

ios - 在 iOS 中解码 unicode 字符串

iphone - 直接调用 actionsheet AirPlay 的代码是什么?

python - 如何在MacBook上同时运行两个Python程序?

ios - Swift:停止方法完成

ios - 为什么我的按钮无法从应用程序推送到 Apple map ?

linux - 如何在 Mac OS X 默认终端上禁用来自 "bracketed paste mode"的奇怪字符?

macos - 如何在 Mac OS 上本地挂载 Docker 容器(就像我会使用 USB 驱动器一样)?