]>
Commit | Line | Data |
---|---|---|
21ad818f JH |
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
4e5a4405 JH |
21 | #include <libsigrokdecode/libsigrokdecode.h> |
22 | ||
3cc9ad7b | 23 | #include "decoder.hpp" |
21ad818f | 24 | |
67fe5e9c JH |
25 | #include <boost/none_t.hpp> |
26 | ||
2acdb232 JH |
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> | |
67fe5e9c | 33 | |
819f4c25 | 34 | using boost::none; |
182d2f5d | 35 | using std::make_pair; |
819f4c25 | 36 | using std::map; |
182d2f5d | 37 | using std::pair; |
f9abf97e | 38 | using std::shared_ptr; |
819f4c25 | 39 | using std::string; |
182d2f5d | 40 | using std::vector; |
67fe5e9c | 41 | |
61703a01 JH |
42 | using pv::prop::Double; |
43 | using pv::prop::Enum; | |
44 | using pv::prop::Int; | |
45 | using pv::prop::Property; | |
46 | using pv::prop::String; | |
47 | ||
21ad818f | 48 | namespace pv { |
21ad818f JH |
49 | namespace binding { |
50 | ||
3cc9ad7b | 51 | Decoder::Decoder( |
7491a29f JH |
52 | shared_ptr<pv::data::DecoderStack> decoder_stack, |
53 | shared_ptr<data::decode::Decoder> decoder) : | |
8dbbc7f0 JH |
54 | decoder_stack_(decoder_stack), |
55 | decoder_(decoder) | |
67fe5e9c | 56 | { |
8dbbc7f0 | 57 | assert(decoder_); |
67fe5e9c | 58 | |
8dbbc7f0 | 59 | const srd_decoder *const dec = decoder_->decoder(); |
4e5a4405 | 60 | assert(dec); |
67fe5e9c | 61 | |
4e5a4405 | 62 | for (GSList *l = dec->options; l; l = l->next) |
67fe5e9c JH |
63 | { |
64 | const srd_decoder_option *const opt = | |
65 | (srd_decoder_option*)l->data; | |
66 | ||
636782c1 | 67 | const QString name = QString::fromUtf8(opt->desc); |
67fe5e9c | 68 | |
6db73158 JH |
69 | const Property::Getter get = [&, opt]() { |
70 | return getter(opt->id); }; | |
e8d00928 | 71 | const Property::Setter set = [&, opt](Glib::VariantBase value) { |
6db73158 | 72 | setter(opt->id, value); }; |
67fe5e9c JH |
73 | |
74 | shared_ptr<Property> prop; | |
75 | ||
182d2f5d | 76 | if (opt->values) |
6db73158 | 77 | prop = bind_enum(name, opt, get, set); |
a28c3025 JH |
78 | else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d"))) |
79 | prop = shared_ptr<Property>(new Double(name, 2, "", | |
6db73158 | 80 | none, none, get, set)); |
182d2f5d | 81 | else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x"))) |
67fe5e9c | 82 | prop = shared_ptr<Property>( |
6db73158 | 83 | new Int(name, "", none, get, set)); |
67fe5e9c JH |
84 | else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s"))) |
85 | prop = shared_ptr<Property>( | |
6db73158 | 86 | new String(name, get, set)); |
67fe5e9c JH |
87 | else |
88 | continue; | |
89 | ||
8dbbc7f0 | 90 | properties_.push_back(prop); |
67fe5e9c JH |
91 | } |
92 | } | |
93 | ||
3cc9ad7b | 94 | shared_ptr<Property> Decoder::bind_enum( |
182d2f5d JH |
95 | const QString &name, const srd_decoder_option *option, |
96 | Property::Getter getter, Property::Setter setter) | |
97 | { | |
e8d00928 | 98 | vector< pair<Glib::VariantBase, QString> > values; |
182d2f5d | 99 | for (GSList *l = option->values; l; l = l->next) { |
e8d00928 | 100 | Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true); |
182d2f5d JH |
101 | values.push_back(make_pair(var, print_gvariant(var))); |
102 | } | |
103 | ||
104 | return shared_ptr<Property>(new Enum(name, values, getter, setter)); | |
105 | } | |
106 | ||
3cc9ad7b | 107 | Glib::VariantBase Decoder::getter(const char *id) |
67fe5e9c | 108 | { |
4c60462b | 109 | GVariant *val = nullptr; |
615f6d25 | 110 | |
8dbbc7f0 | 111 | assert(decoder_); |
4e5a4405 | 112 | |
67fe5e9c | 113 | // Get the value from the hash table if it is already present |
8dbbc7f0 | 114 | const map<string, GVariant*>& options = decoder_->options(); |
f46e495e | 115 | const auto iter = options.find(id); |
67fe5e9c | 116 | |
615f6d25 JH |
117 | if (iter != options.end()) |
118 | val = (*iter).second; | |
119 | else | |
67fe5e9c | 120 | { |
8dbbc7f0 | 121 | assert(decoder_->decoder()); |
4e5a4405 | 122 | |
67fe5e9c | 123 | // Get the default value if not |
8dbbc7f0 | 124 | for (GSList *l = decoder_->decoder()->options; l; l = l->next) |
67fe5e9c JH |
125 | { |
126 | const srd_decoder_option *const opt = | |
127 | (srd_decoder_option*)l->data; | |
1429b21d | 128 | if (strcmp(opt->id, id) == 0) { |
67fe5e9c | 129 | val = opt->def; |
1429b21d BV |
130 | break; |
131 | } | |
67fe5e9c JH |
132 | } |
133 | } | |
134 | ||
135 | if (val) | |
e8d00928 ML |
136 | return Glib::VariantBase(val, true); |
137 | else | |
138 | return Glib::VariantBase(); | |
67fe5e9c JH |
139 | } |
140 | ||
3cc9ad7b | 141 | void Decoder::setter(const char *id, Glib::VariantBase value) |
21ad818f | 142 | { |
8dbbc7f0 JH |
143 | assert(decoder_); |
144 | decoder_->set_option(id, value.gobj()); | |
7491a29f | 145 | |
8dbbc7f0 JH |
146 | assert(decoder_stack_); |
147 | decoder_stack_->begin_decode(); | |
21ad818f JH |
148 | } |
149 | ||
150 | } // binding | |
21ad818f | 151 | } // pv |