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