- GSList *l, *d;
- struct device *device;
- struct datastore *ds;
- struct zip *zipfile;
- struct zip_source *src;
- int bufcnt, devcnt, tmpfile, ret, error;
- char version[1], rawname[16], metafile[32], *buf;
-
- /* Quietly delete it first, libzip wants replace ops otherwise. */
- unlink(filename);
-
- if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
- return SIGROK_ERR;
-
- /* Version */
- version[0] = '1';
- if (!(src = zip_source_buffer(zipfile, version, 1, 0)))
- return SIGROK_ERR;
- if (zip_add(zipfile, "version", src) == -1) {
- g_message("error saving version into zipfile: %s",
- zip_strerror(zipfile));
- return SIGROK_ERR;
- }
-
- /* Metadata */
- strcpy(metafile, "sigrok-meta-XXXXXX");
- if ((tmpfile = g_mkstemp(metafile)) == -1)
- return SIGROK_ERR;
- close(tmpfile);
- make_metadata(metafile);
- if (!(src = zip_source_file(zipfile, metafile, 0, -1)))
- return SIGROK_ERR;
- if (zip_add(zipfile, "metadata", src) == -1)
- return SIGROK_ERR;
- unlink(metafile);
-
- /* Raw */
- devcnt = 1;
- for (l = session->devices; l; l = l->next) {
- device = l->data;
- ds = device->datastore;
- if (ds) {
- buf = malloc(ds->num_units * ds->ds_unitsize +
- DATASTORE_CHUNKSIZE);
- bufcnt = 0;
- for (d = ds->chunklist; d; d = d->next) {
- memcpy(buf + bufcnt, d->data,
- DATASTORE_CHUNKSIZE);
- bufcnt += DATASTORE_CHUNKSIZE;
- }
- if (!(src = zip_source_buffer(zipfile, buf,
- ds->num_units * ds->ds_unitsize, TRUE)))
- return SIGROK_ERR;
- snprintf(rawname, 15, "raw-%d", devcnt);
- if (zip_add(zipfile, rawname, src) == -1)
- return SIGROK_ERR;
- }
- devcnt++;
+ return _sr_session_source_add(pollfd, timeout, cb,
+ cb_data, (gintptr)pollfd);
+}
+
+/**
+ * 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.
+ *
+ * @retval SR_OK Success.
+ * @retval SR_ERR_ARG Invalid argument.
+ * @retval SR_ERR_MALLOC Memory allocation error.
+ *
+ * @since 0.3.0
+ */
+SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
+ int timeout, sr_receive_data_callback 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 poll_object The channel for which the source should be removed.
+ *
+ * @retval SR_OK Success
+ * @retval SR_ERR_ARG Invalid arguments
+ * @retval SR_ERR_MALLOC Memory allocation error
+ * @retval SR_ERR_BUG Internal error
+ */
+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("%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("%s: new_pollfds malloc failed", __func__);
+ return SR_ERR_MALLOC;