X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;ds=sidebyside;f=pv%2Fsession.cpp;h=c0bcd670de26d0af866bf9cb87705ad506d5a57e;hb=3b2ead4fbeb899188564ec9c69c216708311a881;hp=f6fb7085582a7ce19a1941c71e5effa3a040c91c;hpb=33e1afbe17cfa91c24adb775aba923f07fbeed01;p=pulseview.git
diff --git a/pv/session.cpp b/pv/session.cpp
index f6fb708..c0bcd67 100644
--- a/pv/session.cpp
+++ b/pv/session.cpp
@@ -14,95 +14,94 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * along with this program; if not, see .
*/
-#ifdef _WIN32
-// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
-#define NOGDI
-#define NORESOURCE
-#endif
-#include
-#include
-
+#include
#include
#include
+#include
#include
#include
#include
-#include "session.hpp"
#include "devicemanager.hpp"
+#include "mainwindow.hpp"
+#include "session.hpp"
#include "data/analog.hpp"
#include "data/analogsegment.hpp"
-#include "data/decoderstack.hpp"
+#include "data/decode/decoder.hpp"
#include "data/logic.hpp"
#include "data/logicsegment.hpp"
#include "data/signalbase.hpp"
-#include "data/decode/decoder.hpp"
#include "devices/hardwaredevice.hpp"
+#include "devices/inputfile.hpp"
#include "devices/sessionfile.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
#ifdef ENABLE_DECODE
#include
+#include "data/decodesignal.hpp"
#endif
-using boost::shared_lock;
-using boost::shared_mutex;
-using boost::unique_lock;
-
+using std::bad_alloc;
using std::dynamic_pointer_cast;
+using std::find_if;
using std::function;
using std::lock_guard;
using std::list;
+using std::make_pair;
+using std::make_shared;
using std::map;
+using std::max;
+using std::move;
using std::mutex;
+using std::pair;
using std::recursive_mutex;
-using std::set;
+using std::runtime_error;
using std::shared_ptr;
using std::string;
+using std::unique_ptr;
using std::unordered_set;
using std::vector;
using sigrok::Analog;
using sigrok::Channel;
-using sigrok::ChannelType;
using sigrok::ConfigKey;
using sigrok::DatafeedCallbackFunction;
using sigrok::Error;
-using sigrok::Header;
+using sigrok::InputFormat;
using sigrok::Logic;
using sigrok::Meta;
using sigrok::Packet;
-using sigrok::PacketPayload;
using sigrok::Session;
-using sigrok::SessionDevice;
using Glib::VariantBase;
-using Glib::Variant;
namespace pv {
+
+shared_ptr Session::sr_context;
+
Session::Session(DeviceManager &device_manager, QString name) :
device_manager_(device_manager),
default_name_(name),
name_(name),
capture_state_(Stopped),
- cur_samplerate_(0)
+ cur_samplerate_(0),
+ data_saved_(true)
{
}
@@ -149,17 +148,17 @@ void Session::set_name(QString name)
name_changed();
}
-const std::list< std::shared_ptr > Session::views() const
+const list< shared_ptr > Session::views() const
{
return views_;
}
-std::shared_ptr Session::main_view() const
+shared_ptr Session::main_view() const
{
return main_view_;
}
-void Session::set_main_bar(std::shared_ptr main_bar)
+void Session::set_main_bar(shared_ptr main_bar)
{
main_bar_ = main_bar;
}
@@ -169,11 +168,16 @@ shared_ptr Session::main_bar() const
return main_bar_;
}
+bool Session::data_saved() const
+{
+ return data_saved_;
+}
+
void Session::save_settings(QSettings &settings) const
{
map dev_info;
list key_list;
- int stacks = 0, views = 0;
+ int decode_signals = 0, views = 0;
if (device_) {
shared_ptr hw_device =
@@ -183,15 +187,15 @@ void Session::save_settings(QSettings &settings) const
settings.setValue("device_type", "hardware");
settings.beginGroup("device");
- key_list.push_back("vendor");
- key_list.push_back("model");
- key_list.push_back("version");
- key_list.push_back("serial_num");
- key_list.push_back("connection_id");
+ key_list.emplace_back("vendor");
+ key_list.emplace_back("model");
+ key_list.emplace_back("version");
+ key_list.emplace_back("serial_num");
+ key_list.emplace_back("connection_id");
dev_info = device_manager_.get_device_info(device_);
- for (string key : key_list) {
+ for (string& key : key_list) {
if (dev_info.count(key))
settings.setValue(QString::fromUtf8(key.c_str()),
QString::fromUtf8(dev_info.at(key).c_str()));
@@ -203,7 +207,7 @@ void Session::save_settings(QSettings &settings) const
}
shared_ptr sessionfile_device =
- dynamic_pointer_cast< devices::SessionFile >(device_);
+ dynamic_pointer_cast(device_);
if (sessionfile_device) {
settings.setValue("device_type", "sessionfile");
@@ -213,18 +217,22 @@ void Session::save_settings(QSettings &settings) const
settings.endGroup();
}
+ shared_ptr inputfile_device =
+ dynamic_pointer_cast(device_);
+
+ if (inputfile_device) {
+ settings.setValue("device_type", "inputfile");
+ settings.beginGroup("device");
+ inputfile_device->save_meta_to_settings(settings);
+ settings.endGroup();
+ }
+
// Save channels and decoders
- for (shared_ptr base : signalbases_) {
+ for (const shared_ptr& base : signalbases_) {
#ifdef ENABLE_DECODE
if (base->is_decode_signal()) {
- shared_ptr decoder_stack =
- base->decoder_stack();
- std::shared_ptr 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
@@ -235,7 +243,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
@@ -243,7 +251,7 @@ void Session::save_settings(QSettings &settings) const
main_view_->save_settings(settings);
settings.endGroup();
- for (shared_ptr view : views_) {
+ for (const shared_ptr& view : views_) {
if (view != main_view_) {
settings.beginGroup("view" + QString::number(views++));
view->save_settings(settings);
@@ -267,11 +275,11 @@ void Session::restore_settings(QSettings &settings)
// Re-select last used device if possible but only if it's not demo
settings.beginGroup("device");
- key_list.push_back("vendor");
- key_list.push_back("model");
- key_list.push_back("version");
- key_list.push_back("serial_num");
- key_list.push_back("connection_id");
+ key_list.emplace_back("vendor");
+ key_list.emplace_back("model");
+ key_list.emplace_back("version");
+ key_list.emplace_back("serial_num");
+ key_list.emplace_back("connection_id");
for (string key : key_list) {
const QString k = QString::fromStdString(key);
@@ -280,7 +288,7 @@ void Session::restore_settings(QSettings &settings)
const string value = settings.value(k).toString().toStdString();
if (!value.empty())
- dev_info.insert(std::make_pair(key, value));
+ dev_info.insert(make_pair(key, value));
}
if (dev_info.count("model") > 0)
@@ -292,20 +300,34 @@ void Session::restore_settings(QSettings &settings)
settings.endGroup();
}
- if (device_type == "sessionfile") {
- settings.beginGroup("device");
- QString filename = settings.value("filename").toString();
- settings.endGroup();
+ if ((device_type == "sessionfile") || (device_type == "inputfile")) {
+ if (device_type == "sessionfile") {
+ settings.beginGroup("device");
+ QString filename = settings.value("filename").toString();
+ settings.endGroup();
+
+ if (QFileInfo(filename).isReadable()) {
+ device = make_shared(device_manager_.context(),
+ filename.toStdString());
+ }
+ }
- if (QFileInfo(filename).isReadable()) {
- device = std::make_shared(device_manager_.context(),
- filename.toStdString());
+ if (device_type == "inputfile") {
+ settings.beginGroup("device");
+ device = make_shared(device_manager_.context(),
+ settings);
+ settings.endGroup();
+ }
+
+ if (device) {
set_device(device);
- // TODO Perform error handling
- start_capture([](QString infoMessage) { (void)infoMessage; });
+ start_capture([](QString infoMessage) {
+ // TODO Emulate noquote()
+ qDebug() << "Session error:" << infoMessage; });
- set_name(QFileInfo(filename).fileName());
+ set_name(QString::fromStdString(
+ dynamic_pointer_cast(device)->display_name(device_manager_)));
}
}
@@ -319,14 +341,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));
+ shared_ptr signal = add_decode_signal();
+ signal->restore_settings(settings);
settings.endGroup();
}
#endif
@@ -338,7 +358,7 @@ void Session::restore_settings(QSettings &settings)
settings.beginGroup("view" + QString::number(i));
if (i > 0) {
- view::ViewType type = (view::ViewType)settings.value("type").toInt();
+ views::ViewType type = (views::ViewType)settings.value("type").toInt();
add_view(name_, type, this);
views_.back()->restore_settings(settings);
} else
@@ -349,6 +369,18 @@ void Session::restore_settings(QSettings &settings)
}
}
+void Session::select_device(shared_ptr device)
+{
+ try {
+ if (device)
+ set_device(device);
+ else
+ set_default_device();
+ } catch (const QString &e) {
+ MainWindow::show_session_error(tr("Failed to select device"), e);
+ }
+}
+
void Session::set_device(shared_ptr device)
{
assert(device);
@@ -361,24 +393,25 @@ void Session::set_device(shared_ptr device)
device_.reset();
- // Revert name back to default name (e.g. "Untitled-1") as the data is gone
+ // Revert name back to default name (e.g. "Session 1") as the data is gone
name_ = default_name_;
name_changed();
- // Remove all stored data
- for (std::shared_ptr view : views_) {
+ // Remove all stored data and reset all views
+ for (shared_ptr view : views_) {
view->clear_signals();
#ifdef ENABLE_DECODE
- view->clear_decode_traces();
+ view->clear_decode_signals();
#endif
+ view->reset_view_state();
}
- for (const shared_ptr d : all_signal_data_)
+ for (const shared_ptr& d : all_signal_data_)
d->clear();
all_signal_data_.clear();
signalbases_.clear();
cur_logic_segment_.reset();
- for (auto entry : cur_analog_segments_) {
+ for (auto& entry : cur_analog_segments_) {
shared_ptr(entry.first).reset();
shared_ptr(entry.second).reset();
}
@@ -387,22 +420,24 @@ void Session::set_device(shared_ptr device)
signals_changed();
- device_ = std::move(device);
+ device_ = move(device);
try {
device_->open();
} catch (const QString &e) {
device_.reset();
- device_changed();
- throw;
+ MainWindow::show_session_error(tr("Failed to open device"), e);
}
- device_->session()->add_datafeed_callback([=]
- (shared_ptr device, shared_ptr packet) {
- data_feed_in(device, packet);
- });
+ if (device_) {
+ device_->session()->add_datafeed_callback([=]
+ (shared_ptr device, shared_ptr packet) {
+ data_feed_in(device, packet);
+ });
+
+ update_signals();
+ }
- update_signals();
device_changed();
}
@@ -415,13 +450,122 @@ void Session::set_default_device()
return;
// Try and find the demo device and select that by default
- const auto iter = std::find_if(devices.begin(), devices.end(),
+ const auto iter = find_if(devices.begin(), devices.end(),
[] (const shared_ptr &d) {
- return d->hardware_device()->driver()->name() ==
- "demo"; });
+ return d->hardware_device()->driver()->name() == "demo"; });
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
+Session::input_format_options(vector user_spec,
+ map> fmt_opts)
+{
+ map 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