]>
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 | ||
21ad818f JH |
23 | #include "decoderoptions.h" |
24 | ||
7491a29f | 25 | #include <boost/bind.hpp> |
67fe5e9c JH |
26 | #include <boost/foreach.hpp> |
27 | #include <boost/none_t.hpp> | |
28 | ||
6e89374a | 29 | #include <pv/data/decoderstack.h> |
7491a29f | 30 | #include <pv/data/decode/decoder.h> |
182d2f5d | 31 | #include <pv/prop/enum.h> |
67fe5e9c JH |
32 | #include <pv/prop/int.h> |
33 | #include <pv/prop/string.h> | |
34 | ||
819f4c25 JH |
35 | using boost::bind; |
36 | using boost::none; | |
37 | using boost::shared_ptr; | |
182d2f5d | 38 | using std::make_pair; |
819f4c25 | 39 | using std::map; |
182d2f5d | 40 | using std::pair; |
819f4c25 | 41 | using std::string; |
182d2f5d | 42 | using std::vector; |
67fe5e9c | 43 | |
21ad818f JH |
44 | namespace pv { |
45 | namespace prop { | |
46 | namespace binding { | |
47 | ||
7491a29f JH |
48 | DecoderOptions::DecoderOptions( |
49 | shared_ptr<pv::data::DecoderStack> decoder_stack, | |
50 | shared_ptr<data::decode::Decoder> decoder) : | |
51 | _decoder_stack(decoder_stack), | |
4e5a4405 | 52 | _decoder(decoder) |
67fe5e9c | 53 | { |
4e5a4405 | 54 | assert(_decoder); |
67fe5e9c | 55 | |
4e5a4405 JH |
56 | const srd_decoder *const dec = _decoder->decoder(); |
57 | assert(dec); | |
67fe5e9c | 58 | |
4e5a4405 | 59 | for (GSList *l = dec->options; l; l = l->next) |
67fe5e9c JH |
60 | { |
61 | const srd_decoder_option *const opt = | |
62 | (srd_decoder_option*)l->data; | |
63 | ||
636782c1 | 64 | const QString name = QString::fromUtf8(opt->desc); |
67fe5e9c JH |
65 | |
66 | const Property::Getter getter = bind( | |
67 | &DecoderOptions::getter, this, opt->id); | |
68 | const Property::Setter setter = bind( | |
69 | &DecoderOptions::setter, this, opt->id, _1); | |
70 | ||
71 | shared_ptr<Property> prop; | |
72 | ||
182d2f5d JH |
73 | if (opt->values) |
74 | prop = bind_enum(name, opt, getter, setter); | |
75 | else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x"))) | |
67fe5e9c JH |
76 | prop = shared_ptr<Property>( |
77 | new Int(name, "", none, getter, setter)); | |
78 | else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s"))) | |
79 | prop = shared_ptr<Property>( | |
80 | new String(name, getter, setter)); | |
81 | else | |
82 | continue; | |
83 | ||
84 | _properties.push_back(prop); | |
85 | } | |
86 | } | |
87 | ||
182d2f5d JH |
88 | shared_ptr<Property> DecoderOptions::bind_enum( |
89 | const QString &name, const srd_decoder_option *option, | |
90 | Property::Getter getter, Property::Setter setter) | |
91 | { | |
92 | vector< pair<GVariant*, QString> > values; | |
93 | for (GSList *l = option->values; l; l = l->next) { | |
94 | GVariant *const var = (GVariant*)l->data; | |
95 | assert(var); | |
96 | values.push_back(make_pair(var, print_gvariant(var))); | |
97 | } | |
98 | ||
99 | return shared_ptr<Property>(new Enum(name, values, getter, setter)); | |
100 | } | |
101 | ||
67fe5e9c JH |
102 | GVariant* DecoderOptions::getter(const char *id) |
103 | { | |
615f6d25 JH |
104 | GVariant *val = NULL; |
105 | ||
4e5a4405 JH |
106 | assert(_decoder); |
107 | ||
67fe5e9c | 108 | // Get the value from the hash table if it is already present |
615f6d25 JH |
109 | const map<string, GVariant*>& options = _decoder->options(); |
110 | map<string, GVariant*>::const_iterator iter = options.find(id); | |
67fe5e9c | 111 | |
615f6d25 JH |
112 | if (iter != options.end()) |
113 | val = (*iter).second; | |
114 | else | |
67fe5e9c | 115 | { |
4e5a4405 JH |
116 | assert(_decoder->decoder()); |
117 | ||
67fe5e9c | 118 | // Get the default value if not |
4e5a4405 | 119 | for (GSList *l = _decoder->decoder()->options; l; l = l->next) |
67fe5e9c JH |
120 | { |
121 | const srd_decoder_option *const opt = | |
122 | (srd_decoder_option*)l->data; | |
1429b21d | 123 | if (strcmp(opt->id, id) == 0) { |
67fe5e9c | 124 | val = opt->def; |
1429b21d BV |
125 | break; |
126 | } | |
67fe5e9c JH |
127 | } |
128 | } | |
129 | ||
130 | if (val) | |
131 | g_variant_ref(val); | |
132 | ||
133 | return val; | |
134 | } | |
135 | ||
136 | void DecoderOptions::setter(const char *id, GVariant *value) | |
21ad818f | 137 | { |
4e5a4405 JH |
138 | assert(_decoder); |
139 | _decoder->set_option(id, value); | |
7491a29f JH |
140 | |
141 | assert(_decoder_stack); | |
142 | _decoder_stack->begin_decode(); | |
21ad818f JH |
143 | } |
144 | ||
145 | } // binding | |
146 | } // prop | |
147 | } // pv |