Perform autoranging for analog traces also when new data comes in
[pulseview.git] / pv / binding / decoder.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <libsigrokdecode/libsigrokdecode.h>
21
22 #include "decoder.hpp"
23
24 #include <boost/none_t.hpp>
25
26 #include <pv/data/decoderstack.hpp>
27 #include <pv/data/decode/decoder.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>
32
33 using boost::none;
34 using std::make_pair;
35 using std::map;
36 using std::pair;
37 using std::shared_ptr;
38 using std::string;
39 using std::vector;
40
41 using pv::prop::Double;
42 using pv::prop::Enum;
43 using pv::prop::Int;
44 using pv::prop::Property;
45 using pv::prop::String;
46
47 namespace pv {
48 namespace binding {
49
50 Decoder::Decoder(
51         shared_ptr<pv::data::DecoderStack> decoder_stack,
52         shared_ptr<data::decode::Decoder> decoder) :
53         decoder_stack_(decoder_stack),
54         decoder_(decoder)
55 {
56         assert(decoder_);
57
58         const srd_decoder *const dec = decoder_->decoder();
59         assert(dec);
60
61         for (GSList *l = dec->options; l; l = l->next) {
62                 const srd_decoder_option *const opt =
63                         (srd_decoder_option*)l->data;
64
65                 const QString name = QString::fromUtf8(opt->desc);
66
67                 const Property::Getter get = [&, opt]() {
68                         return getter(opt->id); };
69                 const Property::Setter set = [&, opt](Glib::VariantBase value) {
70                         setter(opt->id, value); };
71
72                 shared_ptr<Property> prop;
73
74                 if (opt->values)
75                         prop = bind_enum(name, opt, get, set);
76                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
77                         prop = shared_ptr<Property>(new Double(name, 2, "",
78                                 none, none, get, set));
79                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
80                         prop = shared_ptr<Property>(
81                                 new Int(name, "", none, get, set));
82                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
83                         prop = shared_ptr<Property>(
84                                 new String(name, get, set));
85                 else
86                         continue;
87
88                 properties_.push_back(prop);
89         }
90 }
91
92 shared_ptr<Property> Decoder::bind_enum(
93         const QString &name, const srd_decoder_option *option,
94         Property::Getter getter, Property::Setter setter)
95 {
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.push_back(make_pair(var, print_gvariant(var)));
100         }
101
102         return shared_ptr<Property>(new Enum(name, values, getter, setter));
103 }
104
105 Glib::VariantBase Decoder::getter(const char *id)
106 {
107         GVariant *val = nullptr;
108
109         assert(decoder_);
110
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);
114
115         if (iter != options.end())
116                 val = (*iter).second;
117         else {
118                 assert(decoder_->decoder());
119
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) {
125                                 val = opt->def;
126                                 break;
127                         }
128                 }
129         }
130
131         if (val)
132                 return Glib::VariantBase(val, true);
133         else
134                 return Glib::VariantBase();
135 }
136
137 void Decoder::setter(const char *id, Glib::VariantBase value)
138 {
139         assert(decoder_);
140         decoder_->set_option(id, value.gobj());
141
142         assert(decoder_stack_);
143         decoder_stack_->begin_decode();
144 }
145
146 } // binding
147 } // pv