]> sigrok.org Git - libsigrok.git/blob - bindings/java/org/sigrok/core/classes/classes.i
SWIG: Declare template specialisations for containers before typemaps.
[libsigrok.git] / bindings / java / org / sigrok / core / classes / classes.i
1 %module classes
2
3 /* Automatically load JNI library. */
4 %pragma(java) jniclasscode=%{
5   static {
6     System.loadLibrary("sigrok_java_core_classes");
7   }
8 %}
9
10 /* Documentation & importing interfaces. */
11 %pragma(java) jniclassimports=%{
12 /**
13  * @mainpage API Reference
14  * 
15  * Introduction
16  * ------------
17  * 
18  * The sigrok-java API provides an object-oriented Java interface to the
19  * functionality in libsigrok. It is built on top of the libsigrokcxx C++ API.
20  * 
21  * Getting started
22  * ---------------
23  * 
24  * Usage of the sigrok-java API needs to begin with a call to Context.create().
25  * This will create the global libsigrok context and returns a Context object.
26  * Methods on this object provide access to the hardware drivers, input and
27  * output formats supported by the library, as well as means of creating other
28  * objects such as sessions and triggers.
29  * 
30  * Error handling
31  * --------------
32  * 
33  * When any libsigrok C API call returns an error, an Error exception is raised,
34  * which provides access to the error code and description.
35  */
36
37 import org.sigrok.core.interfaces.LogCallback;
38 import org.sigrok.core.interfaces.DatafeedCallback;
39 %}
40
41 /* Map Glib::VariantBase to a Variant class in Java */
42 %rename(Variant) VariantBase;
43 namespace Glib {
44   class VariantBase {};
45 }
46
47 %include "bindings/swig/templates.i"
48
49 /* Map between std::vector and java.util.Vector */
50 %define VECTOR(CValue, JValue)
51
52 %typemap(jni) std::vector< CValue > "jobject"
53 %typemap(jtype) std::vector< CValue > "java.util.Vector<JValue>"
54 %typemap(jstype) std::vector< CValue > "java.util.Vector<JValue>"
55
56 %typemap(javain,
57     pre="  $javaclassname temp$javainput = $javaclassname.convertVector($javainput);",
58     pgcppname="temp$javainput")
59   std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
60
61 %typemap(javacode) std::vector< CValue > %{
62   static $javaclassname convertVector(java.util.Vector<JValue> in)
63   {
64     $javaclassname out = new $javaclassname();
65     for (JValue value : in)
66       out.add(value);
67     return out;
68   }
69 %}
70
71 %typemap(javaout) std::vector< CValue > {
72   return (java.util.Vector<JValue>)$jnicall;
73 }
74
75 %typemap(out) std::vector< CValue > {
76   jclass Vector = jenv->FindClass("java/util/Vector");
77   jmethodID Vector_init = jenv->GetMethodID(Vector, "<init>", "()V");
78   jmethodID Vector_add = jenv->GetMethodID(Vector, "add",
79     "(Ljava/lang/Object;)Z");
80   jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue);
81   jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
82   $result = jenv->NewObject(Vector, Vector_init);
83   jlong value;
84   for (auto entry : $1)
85   {
86     *(CValue **) &value = new CValue(entry);
87     jenv->CallObjectMethod($result, Vector_add,
88       jenv->NewObject(Value, Value_init, value, true));
89   }
90 }
91
92 %enddef
93
94 VECTOR(std::shared_ptr<sigrok::Channel>, Channel)
95 VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
96
97 /* Common macro for mapping between std::map and java.util.Map */
98
99 %define MAP_COMMON(CKey, CValue, JKey, JValue)
100
101 %typemap(jstype) std::map< CKey, CValue >
102   "java.util.Map<JKey, JValue>"
103
104 %typemap(javain,
105     pre="  $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
106     pgcppname="temp$javainput")
107   std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
108
109 %typemap(javacode) std::map< CKey, CValue > %{
110   static $javaclassname convertMap(java.util.Map<JKey,JValue> in)
111   {
112     $javaclassname out = new $javaclassname();
113     for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
114       out.set(entry.getKey(), entry.getValue());
115     return out;
116   }
117 %}
118
119 %typemap(javaout) std::map< CKey, CValue > {
120   return (java.util.Map<JKey, JValue>)$jnicall;
121 }
122
123 %enddef
124
125 /* Specialisation for string->string maps. */
126
127 MAP_COMMON(std::string, std::string, String, String)
128
129 %typemap(jni) std::map<std::string, std::string>
130   "jobject"
131 %typemap(jtype) std::map<std::string, std::string>
132   "java.util.Map<String,String>"
133
134 %typemap(out) std::map<std::string, std::string> {
135   jclass HashMap = jenv->FindClass("java/util/HashMap");
136   jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
137   jmethodID put = jenv->GetMethodID(HashMap, "put",
138     "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
139   $result = jenv->NewObject(HashMap, init);
140   for (auto entry : $1)
141     jenv->CallObjectMethod($result, put,
142       jenv->NewStringUTF(entry.first.c_str()),
143       jenv->NewStringUTF(entry.second.c_str()));
144 }
145
146 /* Specialisation macro for string->shared_ptr maps. */
147
148 %define STRING_TO_SHARED_PTR_MAP(ClassName)
149
150 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
151   "jobject"
152 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
153   "java.util.Map<String,ClassName>"
154
155 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
156
157 %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > {
158   jclass HashMap = jenv->FindClass("java/util/HashMap");
159   jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
160   jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
161     "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
162   jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
163   jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
164   $result = jenv->NewObject(HashMap, HashMap_init);
165   jlong value;
166   for (auto entry : $1)
167   {
168     *(std::shared_ptr< sigrok::ClassName > **)&value =
169       new std::shared_ptr< sigrok::ClassName>(entry.second);
170     jenv->CallObjectMethod($result, HashMap_put,
171       jenv->NewStringUTF(entry.first.c_str()),
172       jenv->NewObject(Value, Value_init, value, true));
173   }
174 }
175
176 %enddef
177
178 STRING_TO_SHARED_PTR_MAP(Driver)
179 STRING_TO_SHARED_PTR_MAP(InputFormat)
180 STRING_TO_SHARED_PTR_MAP(OutputFormat)
181
182 /* Specialisation for ConfigKey->Variant maps */
183
184 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
185
186 %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
187 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
188   "java.util.Map<ConfigKey,Variant>"
189
190 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
191   jclass HashMap = jenv->FindClass("java/util/HashMap");
192   jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
193   jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
194     "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
195   jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
196   jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
197   jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
198   jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
199   $result = jenv->NewObject(HashMap, HashMap_init);
200   jlong key;
201   jlong value;
202   for (auto entry : $1)
203   {
204     *(const sigrok::ConfigKey **) &key = entry.first;
205     *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
206     jenv->CallObjectMethod($result, HashMap_put,
207       jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
208       jenv->NewObject(Variant, Variant_init, value, true));
209   }
210 }
211
212 /* Support Driver.scan() with no arguments. */
213 %ignore sigrok::Driver::scan;
214
215 %extend sigrok::Driver {
216   std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
217   {
218     std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
219     return $self->scan(options);
220   }
221
222   std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan(
223     std::map<const sigrok::ConfigKey *, Glib::VariantBase> options)
224   {
225     return $self->scan(options);
226   }
227 }
228
229 /* Support InputFormat.create_input() with or without options. */
230 %ignore sigrok::InputFormat::create_input;
231
232 %extend sigrok::InputFormat {
233   std::shared_ptr<sigrok::Input> create_input()
234   {
235     return $self->create_input();
236   }
237
238   std::shared_ptr<sigrok::Input> create_input(
239     std::map<std::string, Glib::VariantBase> options)
240   {
241     return $self->create_input(options);
242   }
243 }
244
245 /* Support OutputFormat.create_output() with or without options. */
246 %ignore sigrok::OutputFormat::create_output;
247
248 %extend sigrok::OutputFormat {
249   std::shared_ptr<sigrok::Output> create_output(
250     std::shared_ptr<sigrok::Device> device)
251   {
252     return $self->create_output(device);
253   }
254
255   std::shared_ptr<sigrok::Output> create_output(
256     std::shared_ptr<sigrok::Device> device,
257     std::map<std::string, Glib::VariantBase> options)
258   {
259     return $self->create_output(device, options);
260   }
261
262   std::shared_ptr<sigrok::Output> create_output(
263     std::string filename,
264     std::shared_ptr<sigrok::Device> device)
265   {
266     return $self->create_output(filename, device);
267   }
268
269   std::shared_ptr<sigrok::Output> create_output(
270     std::string filename,
271     std::shared_ptr<sigrok::Device> device,
272     std::map<std::string, Glib::VariantBase> options)
273   {
274     return $self->create_output(filename, device, options);
275   }
276 }
277
278 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
279
280 %typemap(in, numinputs=0) JNIEnv * %{
281    $1 = jenv;
282 %} 
283
284 /* Support Java log callbacks. */
285
286 %typemap(javaimports) sigrok::Context
287   "import org.sigrok.core.interfaces.LogCallback;"
288
289 %inline {
290 typedef jobject jlogcallback;
291 }
292
293 %typemap(jni) jlogcallback "jlogcallback"
294 %typemap(jtype) jlogcallback "LogCallback"
295 %typemap(jstype) jlogcallback "LogCallback"
296 %typemap(javain) jlogcallback "$javainput"
297
298 %extend sigrok::Context
299 {
300   void add_log_callback(JNIEnv *env, jlogcallback obj)
301   {
302     jclass obj_class = env->GetObjectClass(obj);
303     jmethodID method = env->GetMethodID(obj_class, "run",
304       "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
305     jclass LogLevel = (jclass) env->NewGlobalRef(
306         env->FindClass("org/sigrok/core/classes/LogLevel"));
307     jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
308     jobject obj_ref = env->NewGlobalRef(obj);
309
310     $self->set_log_callback([=] (
311       const sigrok::LogLevel *loglevel,
312       std::string message)
313     {
314       jlong loglevel_addr;
315       *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
316       jobject loglevel_obj = env->NewObject(
317         LogLevel, LogLevel_init, loglevel_addr, false);
318       jobject message_obj = env->NewStringUTF(message.c_str());
319       env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
320       if (env->ExceptionCheck())
321         throw sigrok::Error(SR_ERR);
322     });
323   }
324 }
325
326 /* Support Java datafeed callbacks. */
327
328 %typemap(javaimports) sigrok::Session
329   "import org.sigrok.core.interfaces.DatafeedCallback;"
330
331 %inline {
332 typedef jobject jdatafeedcallback;
333 }
334
335 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
336 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
337 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
338 %typemap(javain) jdatafeedcallback "$javainput"
339
340 %extend sigrok::Session
341 {
342   void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
343   {
344     jclass obj_class = env->GetObjectClass(obj);
345     jmethodID method = env->GetMethodID(obj_class, "run",
346       "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
347     jclass Device = (jclass) env->NewGlobalRef(
348         env->FindClass("org/sigrok/core/classes/Device"));
349     jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
350     jclass Packet = (jclass) env->NewGlobalRef(
351        env->FindClass("org/sigrok/core/classes/Packet"));
352     jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
353     jobject obj_ref = env->NewGlobalRef(obj);
354
355     $self->add_datafeed_callback([=] (
356       std::shared_ptr<sigrok::Device> device,
357       std::shared_ptr<sigrok::Packet> packet)
358     {
359       jlong device_addr;
360       jlong packet_addr;
361       *(std::shared_ptr<sigrok::Device> **) &device_addr =
362         new std::shared_ptr<sigrok::Device>(device);
363       *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
364         new std::shared_ptr<sigrok::Packet>(packet);
365       jobject device_obj = env->NewObject(
366         Device, Device_init, device_addr, true);
367       jobject packet_obj = env->NewObject(
368         Packet, Packet_init, packet_addr, true);
369       env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
370       if (env->ExceptionCheck())
371         throw sigrok::Error(SR_ERR);
372     });
373   }
374 }
375
376 %include "doc.i"
377
378 %define %attributevector(Class, Type, Name, Get)
379 %attributeval(sigrok::Class, Type, Name, Get);
380 %enddef
381
382 %define %attributemap(Class, Type, Name, Get)
383 %attributeval(sigrok::Class, Type, Name, Get);
384 %enddef
385
386 %define %enumextras(Class)
387 %enddef
388
389 /* Ignore these for now, need fixes. */
390 %ignore sigrok::Context::create_analog_packet;
391 %ignore sigrok::Context::create_meta_packet;
392
393 %include "bindings/swig/classes.i"
394