]> sigrok.org Git - pulseview.git/blobdiff - pv/session.cpp
Merge DecoderStack into DecodeSignal
[pulseview.git] / pv / session.cpp
index eec1a051b5a8838ba3f81b58c0759adbadb024a9..f96fc869ecad613816196072409c4d1458e198ad 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <QDebug>
 #include <QFileInfo>
 
 #include <cassert>
@@ -32,7 +33,6 @@
 #include "data/analog.hpp"
 #include "data/analogsegment.hpp"
 #include "data/decode/decoder.hpp"
-#include "data/decoderstack.hpp"
 #include "data/logic.hpp"
 #include "data/logicsegment.hpp"
 #include "data/signalbase.hpp"
 
 #include "toolbars/mainbar.hpp"
 
-#include "view/analogsignal.hpp"
-#include "view/decodetrace.hpp"
-#include "view/logicsignal.hpp"
-#include "view/signal.hpp"
-#include "view/view.hpp"
+#include "views/trace/analogsignal.hpp"
+#include "views/trace/decodetrace.hpp"
+#include "views/trace/logicsignal.hpp"
+#include "views/trace/signal.hpp"
+#include "views/trace/view.hpp"
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
 #ifdef ENABLE_DECODE
 #include <libsigrokdecode/libsigrokdecode.h>
+#include "data/decodesignal.hpp"
 #endif
 
 using std::bad_alloc;
@@ -172,7 +173,7 @@ void Session::save_settings(QSettings &settings) const
 {
        map<string, string> dev_info;
        list<string> key_list;
-       int stacks = 0, views = 0;
+       int decode_signals = 0, views = 0;
 
        if (device_) {
                shared_ptr<devices::HardwareDevice> hw_device =
@@ -216,14 +217,8 @@ void Session::save_settings(QSettings &settings) const
                for (shared_ptr<data::SignalBase> base : signalbases_) {
 #ifdef ENABLE_DECODE
                        if (base->is_decode_signal()) {
-                               shared_ptr<pv::data::DecoderStack> decoder_stack =
-                                               base->decoder_stack();
-                               shared_ptr<data::decode::Decoder> top_decoder =
-                                               decoder_stack->stack().front();
-
-                               settings.beginGroup("decoder_stack" + QString::number(stacks++));
-                               settings.setValue("id", top_decoder->decoder()->id);
-                               settings.setValue("name", top_decoder->decoder()->name);
+                               settings.beginGroup("decode_signal" + QString::number(decode_signals++));
+                               base->save_settings(settings);
                                settings.endGroup();
                        } else
 #endif
@@ -234,7 +229,7 @@ void Session::save_settings(QSettings &settings) const
                        }
                }
 
-               settings.setValue("decoder_stacks", stacks);
+               settings.setValue("decode_signals", decode_signals);
 
                // Save view states and their signal settings
                // Note: main_view must be saved as view0
@@ -318,14 +313,12 @@ void Session::restore_settings(QSettings &settings)
 
                // Restore decoders
 #ifdef ENABLE_DECODE
-               int stacks = settings.value("decoder_stacks").toInt();
-
-               for (int i = 0; i < stacks; i++) {
-                       settings.beginGroup("decoder_stack" + QString::number(i++));
-
-                       QString id = settings.value("id").toString();
-                       add_decoder(srd_decoder_get_by_id(id.toStdString().c_str()));
+               int decode_signals = settings.value("decode_signals").toInt();
 
+               for (int i = 0; i < decode_signals; i++) {
+                       settings.beginGroup("decode_signal" + QString::number(i++));
+                       // TODO Split up add_decoder() into add_decode_signal() and add_decoder(),
+                       // then call add_decode_signal() and signal->restore_settings() here
                        settings.endGroup();
                }
 #endif
@@ -434,26 +427,76 @@ void Session::set_default_device()
        set_device((iter == devices.end()) ? devices.front() : *iter);
 }
 
+/**
+ * Convert generic options to data types that are specific to InputFormat.
+ *
+ * @param[in] user_spec Vector of tokenized words, string format.
+ * @param[in] fmt_opts Input format's options, result of InputFormat::options().
+ *
+ * @return Map of options suitable for InputFormat::create_input().
+ */
+map<string, Glib::VariantBase>
+Session::input_format_options(vector<string> user_spec,
+               map<string, shared_ptr<Option>> fmt_opts)
+{
+       map<string, Glib::VariantBase> result;
+
+       for (auto entry : user_spec) {
+               /*
+                * Split key=value specs. Accept entries without separator
+                * (for simplified boolean specifications).
+                */
+               string key, val;
+               size_t pos = entry.find("=");
+               if (pos == std::string::npos) {
+                       key = entry;
+                       val = "";
+               } else {
+                       key = entry.substr(0, pos);
+                       val = entry.substr(pos + 1);
+               }
+
+               /*
+                * Skip user specifications that are not a member of the
+                * format's set of supported options. Have the text input
+                * spec converted to the required input format specific
+                * data type.
+                */
+               auto found = fmt_opts.find(key);
+               if (found == fmt_opts.end())
+                       continue;
+               shared_ptr<Option> opt = found->second;
+               result[key] = opt->parse_string(val);
+       }
+
+       return result;
+}
+
 void Session::load_init_file(const string &file_name, const string &format)
 {
        shared_ptr<InputFormat> input_format;
+       map<string, Glib::VariantBase> input_opts;
 
        if (!format.empty()) {
                const map<string, shared_ptr<InputFormat> > formats =
                        device_manager_.context()->input_formats();
+               auto user_opts = pv::util::split_string(format, ":");
+               string user_name = user_opts.front();
+               user_opts.erase(user_opts.begin());
                const auto iter = find_if(formats.begin(), formats.end(),
                        [&](const pair<string, shared_ptr<InputFormat> > f) {
-                               return f.first == format; });
+                               return f.first == user_name; });
                if (iter == formats.end()) {
                        main_bar_->session_error(tr("Error"),
                                tr("Unexpected input format: %s").arg(QString::fromStdString(format)));
                        return;
                }
-
                input_format = (*iter).second;
+               input_opts = input_format_options(user_opts,
+                       input_format->options());
        }
 
-       load_file(QString::fromStdString(file_name), input_format);
+       load_file(QString::fromStdString(file_name), input_format, input_opts);
 }
 
 void Session::load_file(QString file_name,
@@ -607,62 +650,34 @@ bool Session::add_decoder(srd_decoder *const dec)
        if (!dec)
                return false;
 
-       map<const srd_channel*, shared_ptr<data::SignalBase> > channels;
-       shared_ptr<data::DecoderStack> decoder_stack;
-
        try {
-               // Create the decoder
-               decoder_stack = make_shared<data::DecoderStack>(*this, dec);
-
-               // Make a list of all the channels
-               vector<const srd_channel*> all_channels;
-               for (const GSList *i = dec->channels; i; i = i->next)
-                       all_channels.push_back((const srd_channel*)i->data);
-               for (const GSList *i = dec->opt_channels; i; i = i->next)
-                       all_channels.push_back((const srd_channel*)i->data);
-
-               // Auto select the initial channels
-               for (const srd_channel *pdch : all_channels)
-                       for (shared_ptr<data::SignalBase> b : signalbases_) {
-                               if (b->logic_data()) {
-                                       if (QString::fromUtf8(pdch->name).toLower().
-                                               contains(b->name().toLower()))
-                                               channels[pdch] = b;
-                               }
-                       }
-
-               assert(decoder_stack);
-               assert(!decoder_stack->stack().empty());
-               assert(decoder_stack->stack().front());
-               decoder_stack->stack().front()->set_channels(channels);
-
                // Create the decode signal
-               shared_ptr<data::SignalBase> signalbase =
-                       make_shared<data::SignalBase>(nullptr, data::SignalBase::DecodeChannel);
+               shared_ptr<data::DecodeSignal> signal =
+                       make_shared<data::DecodeSignal>(*this);
 
-               signalbase->set_decoder_stack(decoder_stack);
-               signalbases_.insert(signalbase);
+               signalbases_.insert(signal);
 
+               // Add the decode signal to all views
                for (shared_ptr<views::ViewBase> view : views_)
-                       view->add_decode_signal(signalbase);
+                       view->add_decode_signal(signal);
+
+               // Add decoder
+               signal->stack_decoder(dec);
        } catch (runtime_error e) {
                return false;
        }
 
        signals_changed();
 
-       // Do an initial decode
-       decoder_stack->begin_decode();
-
        return true;
 }
 
-void Session::remove_decode_signal(shared_ptr<data::SignalBase> signalbase)
+void Session::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
 {
-       signalbases_.erase(signalbase);
+       signalbases_.erase(signal);
 
        for (shared_ptr<views::ViewBase> view : views_)
-               view->remove_decode_signal(signalbase);
+               view->remove_decode_signal(signal);
 
        signals_changed();
 }
@@ -734,22 +749,22 @@ void Session::update_signals()
 
        // Make the signals list
        for (shared_ptr<views::ViewBase> viewbase : views_) {
-               views::TraceView::View *trace_view =
-                       qobject_cast<views::TraceView::View*>(viewbase.get());
+               views::trace::View *trace_view =
+                       qobject_cast<views::trace::View*>(viewbase.get());
 
                if (trace_view) {
-                       unordered_set< shared_ptr<views::TraceView::Signal> >
+                       unordered_set< shared_ptr<views::trace::Signal> >
                                prev_sigs(trace_view->signals());
                        trace_view->clear_signals();
 
                        for (auto channel : sr_dev->channels()) {
                                shared_ptr<data::SignalBase> signalbase;
-                               shared_ptr<views::TraceView::Signal> signal;
+                               shared_ptr<views::trace::Signal> signal;
 
                                // Find the channel in the old signals
                                const auto iter = find_if(
                                        prev_sigs.cbegin(), prev_sigs.cend(),
-                                       [&](const shared_ptr<views::TraceView::Signal> &s) {
+                                       [&](const shared_ptr<views::trace::Signal> &s) {
                                                return s->base()->channel() == channel;
                                        });
                                if (iter != prev_sigs.end()) {
@@ -777,8 +792,8 @@ void Session::update_signals()
                                                                signalbase.get(), SLOT(on_capture_state_changed(int)));
                                                }
 
-                                               signal = shared_ptr<views::TraceView::Signal>(
-                                                       new views::TraceView::LogicSignal(*this,
+                                               signal = shared_ptr<views::trace::Signal>(
+                                                       new views::trace::LogicSignal(*this,
                                                                device_, signalbase));
                                                trace_view->add_signal(signal);
                                                break;
@@ -798,8 +813,8 @@ void Session::update_signals()
                                                                signalbase.get(), SLOT(on_capture_state_changed(int)));
                                                }
 
-                                               signal = shared_ptr<views::TraceView::Signal>(
-                                                       new views::TraceView::AnalogSignal(
+                                               signal = shared_ptr<views::trace::Signal>(
+                                                       new views::trace::AnalogSignal(
                                                                *this, signalbase));
                                                trace_view->add_signal(signal);
                                                break;
@@ -849,7 +864,14 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
        set_capture_state(device_->session()->trigger() ?
                AwaitingTrigger : Running);
 
-       device_->run();
+       try {
+               device_->run();
+       } catch (Error e) {
+               error_handler(e.what());
+               set_capture_state(Stopped);
+               return;
+       }
+
        set_capture_state(Stopped);
 
        // Confirm that SR_DF_END was received