+
+/**
+ * Add an event source for a GIOChannel.
+ *
+ * @param channel The GIOChannel.
+ * @param events Events to poll on.
+ * @param timeout Max time to wait before the callback is called, ignored if 0.
+ * @param cb Callback function to add. Must not be NULL.
+ * @param cb_data Data for the callback function. Can be NULL.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ * SR_ERR_MALLOC upon memory allocation errors.
+ */
+SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
+ int timeout, sr_receive_data_callback_t cb, void *cb_data)
+{
+ GPollFD p;
+
+#ifdef _WIN32
+ g_io_channel_win32_make_pollfd(channel, events, &p);
+#else
+ p.fd = g_io_channel_unix_get_fd(channel);
+ p.events = events;
+#endif
+
+ return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
+}
+
+/**
+ * Remove the source belonging to the specified channel.
+ *
+ * @todo Add more error checks and logging.
+ *
+ * @param channel The channel for which the source should be removed.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
+ * internal errors.
+ */
+static int _sr_session_source_remove(gintptr poll_object)
+{
+ struct source *new_sources;
+ GPollFD *new_pollfds;
+ unsigned int old;
+
+ if (!session->sources || !session->num_sources) {
+ sr_err("session: %s: sources was NULL", __func__);
+ return SR_ERR_BUG;
+ }
+
+ for (old = 0; old < session->num_sources; old++) {
+ if (session->sources[old].poll_object == poll_object)
+ break;
+ }
+
+ /* fd not found, nothing to do */
+ if (old == session->num_sources)
+ return SR_OK;
+
+ session->num_sources -= 1;
+
+ if (old != session->num_sources) {
+ memmove(&session->pollfds[old], &session->pollfds[old+1],
+ (session->num_sources - old) * sizeof(GPollFD));
+ memmove(&session->sources[old], &session->sources[old+1],
+ (session->num_sources - old) * sizeof(struct source));
+ }
+
+ new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
+ if (!new_pollfds && session->num_sources > 0) {
+ sr_err("session: %s: new_pollfds malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
+ new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
+ if (!new_sources && session->num_sources > 0) {
+ sr_err("session: %s: new_sources malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
+ session->pollfds = new_pollfds;
+ session->sources = new_sources;
+
+ return SR_OK;
+}
+
+/**
+ * Remove the source belonging to the specified file descriptor.
+ *
+ * @param fd The file descriptor for which the source should be removed.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
+ * internal errors.
+ */
+SR_API int sr_session_source_remove(int fd)
+{
+ return _sr_session_source_remove((gintptr)fd);
+}
+
+/**
+ * Remove the source belonging to the specified poll descriptor.
+ *
+ * @param pollfd The poll descriptor for which the source should be removed.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
+ * internal errors.
+ */
+SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
+{
+ return _sr_session_source_remove((gintptr)pollfd);
+}
+
+/**
+ * Remove the source belonging to the specified channel.
+ *
+ * @param channel The channel for which the source should be removed.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
+ * internal errors.
+ */
+SR_API int sr_session_source_remove_channel(GIOChannel *channel)
+{
+ return _sr_session_source_remove((gintptr)channel);
+}
+
+/** @} */