ar.c

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 470 行 · 第 1/2 页

C
470
字号
    if (FindHIDDevices(kIOMasterPortDefault, &hidObjectIterator))        return -1;    // Multiple controls could be found, we only use the first usable one.    while ((hidDevice = IOIteratorNext(hidObjectIterator))) {        if (CreateHIDDeviceInterface(hidDevice, &hidDeviceInterface) < 0) {            hidDeviceInterface = NULL;            IOObjectRelease(hidDevice);            continue;        }        if (getHIDCookies((IOHIDDeviceInterface122 **)hidDeviceInterface,                          &cookies,                          &nr_cookies) < 0) {            (*hidDeviceInterface)->Release(hidDeviceInterface);            hidDeviceInterface = NULL;            IOObjectRelease(hidDevice);            continue;        }        IOObjectRelease(hidDevice);        break;    }    if (hidDeviceInterface == NULL)        goto mp_input_ar_init_error;    // Open the device.    if ((*hidDeviceInterface)->open(hidDeviceInterface,                              kIOHIDOptionsTypeSeizeDevice) != kIOReturnSuccess)        goto mp_input_ar_init_error;    hidDeviceIsOpen = 1;    if ((queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface)) == NULL            || *queue == NULL)        goto mp_input_ar_init_error;    // Create the queue.    (*queue)->create(queue, 0, MAX_QUEUE_SIZE);    // Add elements to the queue to make the queue work.    // On tiger, it's a sequence from 1 to 21,    // maybe it's the range of cookie values.    for (i = 0;i < nr_cookies;i++)        (*queue)->addElement(queue, cookies[i], 0);    // not used anymore    if (cookies != NULL)        free(cookies);    // Start data delivery to the queue.    (*queue)->start(queue);    // not useful anymore    IOObjectRelease(hidObjectIterator);    inited = 1;    return 0;mp_input_ar_init_error:    if (cookies != NULL)        free(cookies);    if (hidDeviceInterface != NULL) {        if (*hidDeviceInterface != NULL) {            (*hidDeviceInterface)->close(hidDeviceInterface);            (*hidDeviceInterface)->Release(hidDeviceInterface);        }        hidDeviceInterface = NULL;    }    IOObjectRelease(hidObjectIterator);    return -1;}int is_mplayer_front(){    ProcessSerialNumber myProc, frProc;    Boolean sameProc;    pid_t parentPID;    if (GetFrontProcess(&frProc) == noErr            && GetCurrentProcess(&myProc) == noErr            && SameProcess(&frProc, &myProc, &sameProc) == noErr) {        if (sameProc)            return 1;        // If MPlayer is running in slave mode, also check parent process.        if (slave_mode && GetProcessPID(&frProc, &parentPID) == noErr)            return parentPID==getppid();    }    return 0;}int mp_input_ar_read(int fd){    int i, down = 0;    int ret = MP_INPUT_NOTHING;    AbsoluteTime zeroTime = {0,0};    IOHIDEventStruct event;    static int prev_event = 0;    IOReturn result = kIOReturnSuccess;    const cookie_keycode_map_t *ar_code;    int cookie_nr = 0;    char cookie_queue[MAX_QUEUE_SIZE];    int value_queue[MAX_QUEUE_SIZE];    if (inited == 0)        return MP_INPUT_NOTHING;    while ((result = (*queue)->getNextEvent(queue, &event, zeroTime, 0)) == kIOReturnSuccess) {#ifdef TEST        printf(" - event cookie: %d, value: %d, long value: %d\n",              (int)event.elementCookie, (int)event.value, (int)event.longValue);#endif        // Shorten cookie sequence by removing cookies value 5 and 18,        // since 5 always follows 6 (on tiger), 18 follows 19 (on leopard).        if ((int)event.elementCookie == 5                || ((int)event.elementCookie == 18 && is_leopard))            continue;        // Check valid cookie range.        if ((int)event.elementCookie > 35 || (int)event.elementCookie < 2) {            cookie_nr = 0;            continue;        }        cookie_queue[cookie_nr] = (char)(int)event.elementCookie;        value_queue[cookie_nr++] = event.value;        // 4 cookies are necessary to make up a valid sequence.        if (cookie_nr>=4) {            // Find matching sequence.            ar_code = ar_codes;            while (ar_code->cookies != NULL &&                    (cookie_nr < ar_code->seq_len ||                     0 != memcmp(ar_code->cookies,                                 &cookie_queue[cookie_nr-ar_code->seq_len],                                 ar_code->seq_len)))                ++ar_code;            if (ar_code->cookies != NULL) {                ret = ar_code->keycode;                switch (ret) {                    // For these 4 keys, the remote can keep a hold state.                    case AR_VUP:                    case AR_VDOWN:                    case AR_NEXT_HOLD:                    case AR_PREV_HOLD:                        for (i = cookie_nr-ar_code->seq_len; i < cookie_nr; ++i) {                            if (value_queue[i]) {                                down = MP_KEY_DOWN;                                break;                            }                        }                        break;                    default:                        down = 0;                }            }        }    }    if (!is_mplayer_front()) {        if (hidDeviceIsOpen) {            (*hidDeviceInterface)->close(hidDeviceInterface);            hidDeviceIsOpen = 0;            // Read out all pending events.            while (result == kIOReturnSuccess)                result = (*queue)->getNextEvent(queue, &event, zeroTime, 0);        }        return MP_INPUT_NOTHING;    }    // If we are switched from running as a foreground process to a    // background process and back again, re-open the device to make    // sure we are not affected by the system or other applications    // using the Apple Remote.    else if (!hidDeviceIsOpen) {        if ((*hidDeviceInterface)->open(hidDeviceInterface,                              kIOHIDOptionsTypeSeizeDevice) == kIOReturnSuccess)            hidDeviceIsOpen = 1;    }    if (ret > 0)        prev_event = ret;    return ret | down;}void mp_input_ar_close(int fd){    if (inited == 0)        return;    // Close the device.    (*hidDeviceInterface)->close(hidDeviceInterface);    // Stop data delivery to queue.    (*queue)->stop(queue);    // Dispose of queue.    (*queue)->dispose(queue);    // Release the queue we allocated.    (*queue)->Release(queue);    // Release the interface.    (*hidDeviceInterface)->Release(hidDeviceInterface);    inited = 0;}#ifdef TESTint main(void){    int ret;    if (mp_input_ar_init() < 0) {        printf("Unable to initialize Apple Remote.\n");        return 1;    }    while (1) {        switch ((ret = mp_input_ar_read(0)) & ~MP_KEY_DOWN) {            case AR_PLAY:       printf(" - AR_PLAY."); break;            case AR_PLAY_HOLD:  printf(" - AR_PLAY_HOLD."); break;            case AR_NEXT:       printf(" - AR_NEXT."); break;            case AR_NEXT_HOLD:  printf(" - AR_NEXT_HOLD."); break;            case AR_PREV:       printf(" - AR_PREV."); break;            case AR_PREV_HOLD:  printf(" - AR_PREV_HOLD."); break;            case AR_MENU:       printf(" - AR_MENU."); break;            case AR_MENU_HOLD:  printf(" - AR_MENU_HOLD."); break;            case AR_VUP:        printf(" - AR_VUP."); break;            case AR_VDOWN:      printf(" - AR_VDOWN."); break;        }        if ((ret > 0 )&&(ret & MP_KEY_DOWN))            printf(" [hold]");        if (ret > 0)            printf("\n");    }    mp_input_ar_close(0);    return 0;}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?