2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <libsigrokdecode/libsigrokdecode.h>
22 #include "decoder.hpp"
24 #include <boost/none_t.hpp>
26 #include <pv/data/decode/decoder.hpp>
27 #include <pv/data/decodesignal.hpp>
28 #include <pv/prop/double.hpp>
29 #include <pv/prop/enum.hpp>
30 #include <pv/prop/int.hpp>
31 #include <pv/prop/string.hpp>
36 using std::shared_ptr;
40 using pv::prop::Double;
43 using pv::prop::Property;
44 using pv::prop::String;
50 shared_ptr<pv::data::DecodeSignal> decode_signal,
51 shared_ptr<data::decode::Decoder> decoder) :
52 decode_signal_(decode_signal),
57 const srd_decoder *const dec = decoder_->decoder();
60 for (GSList *l = dec->options; l; l = l->next) {
61 const srd_decoder_option *const opt =
62 (srd_decoder_option*)l->data;
64 const QString name = QString::fromUtf8(opt->desc);
66 const Property::Getter get = [&, opt]() {
67 return getter(opt->id); };
68 const Property::Setter set = [&, opt](Glib::VariantBase value) {
69 setter(opt->id, value); };
71 shared_ptr<Property> prop;
74 prop = bind_enum(name, "", opt, get, set);
75 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
76 prop = shared_ptr<Property>(new Double(name, "", 2, "",
77 none, none, get, set));
78 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
79 prop = shared_ptr<Property>(
80 new Int(name, "", "", none, get, set));
81 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
82 prop = shared_ptr<Property>(
83 new String(name, "", get, set));
87 properties_.push_back(prop);
91 shared_ptr<Property> Decoder::bind_enum(
92 const QString &name, const QString &desc,
93 const srd_decoder_option *option,
94 Property::Getter getter, Property::Setter setter)
96 vector< pair<Glib::VariantBase, QString> > values;
97 for (GSList *l = option->values; l; l = l->next) {
98 Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true);
99 values.emplace_back(var, print_gvariant(var));
102 return shared_ptr<Property>(new Enum(name, desc, values, getter, setter));
105 Glib::VariantBase Decoder::getter(const char *id)
107 GVariant *val = nullptr;
111 // Get the value from the hash table if it is already present
112 const map<string, GVariant*>& options = decoder_->options();
113 const auto iter = options.find(id);
115 if (iter != options.end())
116 val = (*iter).second;
118 assert(decoder_->decoder());
120 // Get the default value if not
121 for (GSList *l = decoder_->decoder()->options; l; l = l->next) {
122 const srd_decoder_option *const opt =
123 (srd_decoder_option*)l->data;
124 if (strcmp(opt->id, id) == 0) {
131 return (val) ? Glib::VariantBase(val, true) : Glib::VariantBase();
134 void Decoder::setter(const char *id, Glib::VariantBase value)
137 decoder_->set_option(id, value.gobj());
139 assert(decode_signal_);
140 decode_signal_->begin_decode();
143 } // namespace binding