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