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