]> sigrok.org Git - pulseview.git/blame_incremental - pv/prop/binding/decoderoptions.cpp
Add bindings for new device options
[pulseview.git] / pv / prop / binding / decoderoptions.cpp
... / ...
CommitLineData
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
21#include <libsigrokdecode/libsigrokdecode.h>
22
23#include "decoderoptions.h"
24
25#include <boost/bind.hpp>
26#include <boost/foreach.hpp>
27#include <boost/none_t.hpp>
28
29#include <pv/data/decoderstack.h>
30#include <pv/data/decode/decoder.h>
31#include <pv/prop/int.h>
32#include <pv/prop/string.h>
33
34using boost::bind;
35using boost::none;
36using boost::shared_ptr;
37using std::map;
38using std::string;
39
40namespace pv {
41namespace prop {
42namespace binding {
43
44DecoderOptions::DecoderOptions(
45 shared_ptr<pv::data::DecoderStack> decoder_stack,
46 shared_ptr<data::decode::Decoder> decoder) :
47 _decoder_stack(decoder_stack),
48 _decoder(decoder)
49{
50 assert(_decoder);
51
52 const srd_decoder *const dec = _decoder->decoder();
53 assert(dec);
54
55 for (GSList *l = dec->options; l; l = l->next)
56 {
57 const srd_decoder_option *const opt =
58 (srd_decoder_option*)l->data;
59
60 const QString name(opt->desc);
61
62 const Property::Getter getter = bind(
63 &DecoderOptions::getter, this, opt->id);
64 const Property::Setter setter = bind(
65 &DecoderOptions::setter, this, opt->id, _1);
66
67 shared_ptr<Property> prop;
68
69 if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
70 prop = shared_ptr<Property>(
71 new Int(name, "", none, getter, setter));
72 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
73 prop = shared_ptr<Property>(
74 new String(name, getter, setter));
75 else
76 continue;
77
78 _properties.push_back(prop);
79 }
80}
81
82GVariant* DecoderOptions::getter(const char *id)
83{
84 GVariant *val = NULL;
85
86 assert(_decoder);
87
88 // Get the value from the hash table if it is already present
89 const map<string, GVariant*>& options = _decoder->options();
90 map<string, GVariant*>::const_iterator iter = options.find(id);
91
92 if (iter != options.end())
93 val = (*iter).second;
94 else
95 {
96 assert(_decoder->decoder());
97
98 // Get the default value if not
99 for (GSList *l = _decoder->decoder()->options; l; l = l->next)
100 {
101 const srd_decoder_option *const opt =
102 (srd_decoder_option*)l->data;
103 if (strcmp(opt->id, id) == 0) {
104 val = opt->def;
105 break;
106 }
107 }
108 }
109
110 if (val)
111 g_variant_ref(val);
112
113 return val;
114}
115
116void DecoderOptions::setter(const char *id, GVariant *value)
117{
118 assert(_decoder);
119 _decoder->set_option(id, value);
120
121 assert(_decoder_stack);
122 _decoder_stack->begin_decode();
123}
124
125} // binding
126} // prop
127} // pv