コード例 #1
0
ファイル: monitor_devices.c プロジェクト: systronix/ty
int main(void)
{
    hs_monitor *monitor = NULL;
    hs_poll_source sources[2];
    int r;

    r = hs_monitor_new(NULL, 0, &monitor);
    if (r < 0)
        goto cleanup;

    /* Enumerate devices and start listening to OS notifications. The list is refreshed and the
       callback is called only when hs_monitor_refresh() is called. Use hs_monitor_get_poll_handle()
       to get a pollable descriptor and integrate it to your event loop. */
    r = hs_monitor_start(monitor);
    if (r < 0)
        goto cleanup;

    /* hs_monitor_list() uses a cached device list in the monitor object, which is only updated
       when you call hs_monitor_start() and hs_monitor_refresh(). */
    r = hs_monitor_list(monitor, device_callback, NULL);
    if (r < 0)
        goto cleanup;

    /* Add the waitable descriptor provided by the monitor to the descriptor set, it will
       become ready (POLLIN) when there are pending events. */
    sources[0].desc = hs_monitor_get_poll_handle(monitor);
    /* We also want to poll the terminal/console input buffer, to exit on key presses. */
#ifdef _WIN32
    sources[1].desc = GetStdHandle(STD_INPUT_HANDLE);
#else
    sources[1].desc = STDIN_FILENO;
#endif

    printf("Monitoring devices (press RETURN to end):\n");
    do {
        /* This function is non-blocking, if there are no pending events it does nothing and
           returns immediately. It calls the callback function for each notification (add or
           remove) and updates the device list accessed by hs_monitor_list(). */
        r = hs_monitor_refresh(monitor, device_callback, NULL);
        if (r < 0)
            goto cleanup;

        /* This function returns the number of ready sources, 0 if it times out or a negative
           error code. You can simply check each source's ready field after each call.  */
        r = hs_poll(sources, 2, -1);
    } while (r > 0 && !sources[1].ready);

    if (sources[1].ready) {
#ifndef _WIN32
        /* Clear the terminal input buffer, just to avoid the extra return/characters from
           showing up when this program exits. This has nothing to do with libhs. */
        tcflush(STDIN_FILENO, TCIFLUSH);
#endif
        r = 0;
    }

cleanup:
    hs_monitor_free(monitor);
    return -r;
}
コード例 #2
0
ファイル: monitor_devices.c プロジェクト: sumotoy/libhs
int main(void)
{
    hs_monitor *monitor = NULL;
    /* This is a quick way to clear the descriptor set, you are free to use `set->count = 0;`
       or hs_descriptor_set_clear(). */
    hs_descriptor_set set = {0};
    int r;

    r = hs_monitor_new(&monitor);
    if (r < 0)
        goto cleanup;

    printf("Current devices:\n");

    /* hs_monitor_new() goes through the device tree and makes an initial device list, so we
       don't need hs_monitor_refresh() yet. But if you don't call hs_monitor_list()
       immediately, you need to use hs_monitor_refresh() before hs_monitor_list(). */
    r = hs_monitor_list(monitor, device_callback, NULL);
    if (r < 0)
        goto cleanup;

    /* Register our event callback, this is called from within hs_monitor_refresh() whenever
       something interesting happens (i.e. a device is added or removed). */
    r = hs_monitor_register_callback(monitor, device_callback, NULL);
    if (r < 0)
        goto cleanup;
    printf("\n");

    /* Add the waitable descriptor provided by the monitor to the descriptor set, it will
       become ready when there are pending events. */
    hs_descriptor_set_add(&set, hs_monitor_get_descriptor(monitor), 1);
    /* We also want to poll the terminal/console input buffer. */
#ifdef _WIN32
    hs_descriptor_set_add(&set, GetStdHandle(STD_INPUT_HANDLE), 2);
#else
    hs_descriptor_set_add(&set, STDIN_FILENO, 2);
#endif

    printf("Monitoring devices (press RETURN to end):\n");
    do {
        /* This function is non-blocking, if there are no pending events it does nothing and
           returns immediately. */
        r = hs_monitor_refresh(monitor);
        if (r < 0)
            goto cleanup;

        /* This function returns the value associated with the ready descriptor, in this case
           1 when there are monitor events and 2 when RETURN is pressed. */
        r = hs_poll(&set, -1);
    } while (r == 1);

    if (r == 2) {
#ifndef _WIN32
        /* Clear the terminal input buffer, just to avoid the extra return/characters from
        * showing up when this program exits. This has nothing to do with libhs. */
        tcflush(STDIN_FILENO, TCIFLUSH);
#endif
        r = 0;
    }

cleanup:
    hs_monitor_free(monitor);
    return -r;
}