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