+/**
+ * Attempts to autodetect the format. Failing that
+ * @param filename The filename of the input file.
+ * @return A pointer to the 'struct sr_input_format' that should be used,
+ * or NULL if no input format was selected or auto-detected.
+ */
+sr_input_format* SigSession::determine_input_file_format(
+ const string &filename)
+{
+ int i;
+
+ /* If there are no input formats, return NULL right away. */
+ sr_input_format *const *const inputs = sr_input_list();
+ if (!inputs) {
+ g_critical("No supported input formats available.");
+ return NULL;
+ }
+
+ /* Otherwise, try to find an input module that can handle this file. */
+ for (i = 0; inputs[i]; i++) {
+ if (inputs[i]->format_match(filename.c_str()))
+ break;
+ }
+
+ /* Return NULL if no input module wanted to touch this. */
+ if (!inputs[i]) {
+ g_critical("Error: no matching input module found.");
+ return NULL;
+ }
+
+ return inputs[i];
+}
+
+sr_input* SigSession::load_input_file_format(const string &filename,
+ function<void (const QString)> error_handler,
+ sr_input_format *format)
+{
+ struct stat st;
+ sr_input *in;
+
+ if (!format && !(format =
+ determine_input_file_format(filename.c_str()))) {
+ /* The exact cause was already logged. */
+ return NULL;
+ }
+
+ if (stat(filename.c_str(), &st) == -1) {
+ error_handler(tr("Failed to load file"));
+ return NULL;
+ }
+
+ /* Initialize the input module. */
+ if (!(in = new sr_input)) {
+ qDebug("Failed to allocate input module.\n");
+ return NULL;
+ }
+
+ in->format = format;
+ in->param = NULL;
+ if (in->format->init &&
+ in->format->init(in, filename.c_str()) != SR_OK) {
+ qDebug("Input format init failed.\n");
+ return NULL;
+ }
+
+ sr_session_new();
+
+ if (sr_session_dev_add(in->sdi) != SR_OK) {
+ qDebug("Failed to use device.\n");
+ sr_session_destroy();
+ return NULL;
+ }
+
+ return in;
+}
+
+void SigSession::update_signals()
+{
+ assert(_capture_state == Stopped);
+
+ shared_ptr<view::Signal> signal;
+ unsigned int logic_probe_count = 0;
+ unsigned int analog_probe_count = 0;
+
+ // Detect what data types we will receive
+ if(_sdi) {
+ for (const GSList *l = _sdi->probes; l; l = l->next) {
+ const sr_probe *const probe = (const sr_probe *)l->data;
+ if (!probe->enabled)
+ continue;
+
+ switch(probe->type) {
+ case SR_PROBE_LOGIC:
+ logic_probe_count++;
+ break;
+
+ case SR_PROBE_ANALOG:
+ analog_probe_count++;
+ break;
+ }
+ }
+ }
+
+ // Create data containers for the data snapshots
+ {
+ lock_guard<mutex> data_lock(_data_mutex);
+
+ _logic_data.reset();
+ if (logic_probe_count != 0) {
+ _logic_data.reset(new data::Logic(
+ logic_probe_count));
+ assert(_logic_data);
+ }
+
+ _analog_data.reset();
+ if (analog_probe_count != 0) {
+ _analog_data.reset(new data::Analog());
+ assert(_analog_data);
+ }
+ }
+
+ // Make the Signals list
+ {
+ lock_guard<mutex> lock(_signals_mutex);
+
+ _signals.clear();
+
+ if(_sdi) {
+ for (const GSList *l = _sdi->probes; l; l = l->next) {
+ const sr_probe *const probe =
+ (const sr_probe *)l->data;
+ assert(probe);
+
+ switch(probe->type) {
+ case SR_PROBE_LOGIC:
+ signal = shared_ptr<view::Signal>(
+ new view::LogicSignal(probe,
+ _logic_data));
+ break;
+
+ case SR_PROBE_ANALOG:
+ signal = shared_ptr<view::Signal>(
+ new view::AnalogSignal(probe,
+ _analog_data));
+ break;
+ }
+
+ _signals.push_back(signal);
+ }
+ }
+ }
+
+ signals_changed();
+}
+