- 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_NOK;
-
- /* version */
- version[0] = '1';
- if( !(src = zip_source_buffer(zipfile, version, 1, 0)) )
- return SIGROK_NOK;
- if(zip_add(zipfile, "version", src) == -1) {
- g_message("error saving version into zipfile: %s", zip_strerror(zipfile));
- return SIGROK_NOK;
- }
-
- /* metadata */
- strcpy(metafile, "sigrok-meta-XXXXXX");
- if( (tmpfile = g_mkstemp(metafile)) == -1)
- return SIGROK_NOK;
- close(tmpfile);
- make_metadata(metafile);
- if( !(src = zip_source_file(zipfile, metafile, 0, -1)) )
- return SIGROK_NOK;
- if(zip_add(zipfile, "metadata", src) == -1)
- return SIGROK_NOK;
- 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_NOK;
- snprintf(rawname, 15, "raw-%d", devcnt);
- if(zip_add(zipfile, rawname, src) == -1)
- return SIGROK_NOK;
- }
- devcnt++;
+ (void) session;
+
+ 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(struct sr_session *session,
+ GIOChannel *channel, int events, int timeout,
+ sr_receive_data_callback cb, void *cb_data)
+{
+ GPollFD p;
+
+ (void) session;
+
+#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 sr_session *session = sr_current_session;
+ 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;