//Generic device callback
int usb_monitor_cb(libusb_context *ctx, libusb_device *device,
                          libusb_hotplug_event event, void *user_data)
{
    struct usb_monitor_ctx *usbmon_ctx = user_data;
    struct libusb_device_descriptor desc;

    libusb_get_device_descriptor(device, &desc);

    //Check if device belongs to a port we manage first. This is required for
    //example for cascading hubs, we need to the hub from the port is is
    //connected to, in addition to the port
    if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
        usb_device_added(usbmon_ctx, device);
    else
        usb_device_removed(usbmon_ctx, device);

    //Multiple callbacks can be called multiple times, so it makes little sense
    //to register a separate ykush callback, when we anyway have to filter here
    if (desc.idVendor == YKUSH_VID &&
        (desc.idProduct == YKUSH_PID || desc.idProduct == YKUSH_PID2)) {
        ykush_event_cb(ctx, device, event, user_data);
    }

    return 0;
}
static void channel_new(SpiceSession *s, SpiceChannel *channel, gpointer data)
{
    spice_connection *conn = data;
    int id;

    g_object_get(channel, "channel-id", &id, NULL);
    conn->channels++;
    SPICE_DEBUG("new channel (#%d)", id);

    if (SPICE_IS_MAIN_CHANNEL(channel)) {
        SPICE_DEBUG("new main channel");
        conn->main = SPICE_MAIN_CHANNEL(channel);
        g_signal_connect(channel, "channel-event",
                         G_CALLBACK(main_channel_event), conn);
        //g_signal_connect(channel, "main-mouse-update",
        //                 G_CALLBACK(main_mouse_update), conn);
        //g_signal_connect(channel, "main-agent-update",
        //                 G_CALLBACK(main_agent_update), conn);
        //main_mouse_update(channel, conn);
        //main_agent_update(channel, conn);
    }

    if (SPICE_IS_DISPLAY_CHANNEL(channel)) {
        if (id >= SPICE_N_ELEMENTS(conn->wins))
            return;
        if (conn->wins[id] != NULL)
            return;
        SPICE_DEBUG("new display channel (#%d)", id);
        conn->wins[id] = create_spice_window(conn, channel, id);
        //g_signal_connect(channel, "display-mark",
        //                 G_CALLBACK(display_mark), conn->wins[id]);
        //update_auto_usbredir_sensitive(conn);
    }

    if (SPICE_IS_INPUTS_CHANNEL(channel)) {
        SPICE_DEBUG("new inputs channel");
        //g_signal_connect(channel, "inputs-modifiers",
        //                 G_CALLBACK(inputs_modifiers), conn);
    }

    if (soundEnabled && SPICE_IS_PLAYBACK_CHANNEL(channel)) {
        SPICE_DEBUG("new audio channel");
        conn->audio = spice_audio_get(s, NULL);
    }

    if (SPICE_IS_USBREDIR_CHANNEL(channel)) {
        __android_log_write(ANDROID_LOG_INFO, "android-spice", "Created USB channel, attempting to add devices");
        SpiceUsbDeviceManager *manager = spice_usb_device_manager_get(s, NULL);
        GPtrArray *devices = spice_usb_device_manager_get_devices(manager);
        if (devices) {
            for (int i = 0; i < devices->len; i++) {
                __android_log_write(ANDROID_LOG_INFO, "android-spicy", "Devices found, connecting...");
                usb_device_added(manager, g_ptr_array_index(devices, i), NULL);
            }
            g_ptr_array_unref(devices);
        }
    }

    //if (SPICE_IS_PORT_CHANNEL(channel)) {
    //    g_signal_connect(channel, "notify::port-opened",
    //                     G_CALLBACK(port_opened), conn);
    //    g_signal_connect(channel, "port-data",
    //                     G_CALLBACK(port_data), conn);
    //    spice_channel_connect(channel);
    //}
}