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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <libsigrokdecode/libsigrokdecode.h>
23 #include "decoderoptions.hpp"
25 #include <boost/none_t.hpp>
27 #include <pv/data/decoderstack.hpp>
28 #include <pv/data/decode/decoder.hpp>
29 #include <pv/prop/double.hpp>
30 #include <pv/prop/enum.hpp>
31 #include <pv/prop/int.hpp>
32 #include <pv/prop/string.hpp>
38 using std::shared_ptr;
42 using pv::prop::Double;
45 using pv::prop::Property;
46 using pv::prop::String;
51 DecoderOptions::DecoderOptions(
52 shared_ptr<pv::data::DecoderStack> decoder_stack,
53 shared_ptr<data::decode::Decoder> decoder) :
54 decoder_stack_(decoder_stack),
59 const srd_decoder *const dec = decoder_->decoder();
62 for (GSList *l = dec->options; l; l = l->next)
64 const srd_decoder_option *const opt =
65 (srd_decoder_option*)l->data;
67 const QString name = QString::fromUtf8(opt->desc);
69 const Property::Getter get = [&, opt]() {
70 return getter(opt->id); };
71 const Property::Setter set = [&, opt](Glib::VariantBase value) {
72 setter(opt->id, value); };
74 shared_ptr<Property> prop;
77 prop = bind_enum(name, opt, get, set);
78 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
79 prop = shared_ptr<Property>(new Double(name, 2, "",
80 none, none, get, set));
81 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
82 prop = shared_ptr<Property>(
83 new Int(name, "", none, get, set));
84 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
85 prop = shared_ptr<Property>(
86 new String(name, get, set));
90 properties_.push_back(prop);
94 shared_ptr<Property> DecoderOptions::bind_enum(
95 const QString &name, const srd_decoder_option *option,
96 Property::Getter getter, Property::Setter setter)
98 vector< pair<Glib::VariantBase, QString> > values;
99 for (GSList *l = option->values; l; l = l->next) {
100 Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true);
101 values.push_back(make_pair(var, print_gvariant(var)));
104 return shared_ptr<Property>(new Enum(name, values, getter, setter));
107 Glib::VariantBase DecoderOptions::getter(const char *id)
109 GVariant *val = NULL;
113 // Get the value from the hash table if it is already present
114 const map<string, GVariant*>& options = decoder_->options();
115 const auto iter = options.find(id);
117 if (iter != options.end())
118 val = (*iter).second;
121 assert(decoder_->decoder());
123 // Get the default value if not
124 for (GSList *l = decoder_->decoder()->options; l; l = l->next)
126 const srd_decoder_option *const opt =
127 (srd_decoder_option*)l->data;
128 if (strcmp(opt->id, id) == 0) {
136 return Glib::VariantBase(val, true);
138 return Glib::VariantBase();
141 void DecoderOptions::setter(const char *id, Glib::VariantBase value)
144 decoder_->set_option(id, value.gobj());
146 assert(decoder_stack_);
147 decoder_stack_->begin_decode();