]> sigrok.org Git - pulseview.git/blob - pv/data/decode/decoder.cpp
Split annotation data set into rows, and improved painting.
[pulseview.git] / pv / data / decode / decoder.cpp
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 "decoder.h"
24
25 #include <pv/view/logicsignal.h>
26
27 using boost::shared_ptr;
28 using std::map;
29 using std::string;
30
31 namespace pv {
32 namespace data {
33 namespace decode {
34
35 Decoder::Decoder(const srd_decoder *const dec) :
36         _decoder(dec)
37 {
38 }
39
40 Decoder::~Decoder()
41 {
42         for (map<string, GVariant*>::const_iterator i = _options.begin();
43                 i != _options.end(); i++)
44                 g_variant_unref((*i).second);
45 }
46
47 const srd_decoder* Decoder::decoder() const
48 {
49         return _decoder;
50 }
51
52 const map<const srd_probe*, shared_ptr<view::LogicSignal> >&
53 Decoder::probes() const
54 {
55         return _probes;
56 }
57
58 void Decoder::set_probes(std::map<const srd_probe*,
59         boost::shared_ptr<view::LogicSignal> > probes)
60 {
61         _probes = probes;
62 }
63
64 const std::map<std::string, GVariant*>& Decoder::options() const
65 {
66         return _options;
67 }
68
69 void Decoder::set_option(const char *id, GVariant *value)
70 {
71         assert(value);
72         g_variant_ref(value);
73         _options[id] = value;
74 }
75
76 srd_decoder_inst* Decoder::create_decoder_inst(
77         srd_session *const session) const
78 {
79         GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
80                 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
81
82         for (map<string, GVariant*>::const_iterator i = _options.begin();
83                 i != _options.end(); i++)
84         {
85                 GVariant *const value = (*i).second;
86                 g_variant_ref(value);
87                 g_hash_table_replace(opt_hash, (void*)g_strdup(
88                         (*i).first.c_str()), value);
89         }
90
91         srd_decoder_inst *const decoder_inst = srd_inst_new(
92                 session, _decoder->id, opt_hash);
93         g_hash_table_destroy(opt_hash);
94
95         if(!decoder_inst)
96                 return NULL;
97
98         // Setup the probes
99         GHashTable *const probes = g_hash_table_new_full(g_str_hash,
100                 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
101
102         for(map<const srd_probe*, shared_ptr<view::LogicSignal> >::
103                 const_iterator i = _probes.begin();
104                 i != _probes.end(); i++)
105         {
106                 shared_ptr<view::LogicSignal> signal((*i).second);
107                 GVariant *const gvar = g_variant_new_int32(
108                         signal->probe()->index);
109                 g_variant_ref_sink(gvar);
110                 g_hash_table_insert(probes, (*i).first->id, gvar);
111         }
112
113         srd_inst_probe_set_all(decoder_inst, probes);
114
115         return decoder_inst;
116 }
117
118 } // decode
119 } // data
120 } // pv