]> sigrok.org Git - libsigrok.git/blob - bindings/java/org/sigrok/core/classes/classes.i
75793b2531a39e8f5858cb9bc9af239a18824b73
[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->CallBooleanMethod($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 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
197
198 %typemap(in, numinputs=0) JNIEnv * %{
199    $1 = jenv;
200 %}
201
202 /* Thread safe JNIEnv handling */
203
204 %inline {
205 namespace {
206   class ScopedEnv {
207     public:
208       ScopedEnv(JavaVM *jvm);
209       ~ScopedEnv();
210       JNIEnv* operator-> () { return env; }
211       operator bool () const { return (bool)env; }
212     private:
213       JavaVM *jvm;
214       JNIEnv *env;
215       int env_status;
216   };
217   ScopedEnv::ScopedEnv(JavaVM *jvm) : jvm(jvm), env(NULL) {
218     env_status = jvm->GetEnv((void **)&env, JNI_VERSION_1_2);
219     if (env_status == JNI_EDETACHED) {
220 %#if defined(__ANDROID__)
221       jvm->AttachCurrentThread(&env, NULL);
222 %#else
223       jvm->AttachCurrentThread((void **)&env, NULL);
224 %#endif
225     }
226   }
227   ScopedEnv::~ScopedEnv() {
228     if (env_status == JNI_EDETACHED) {
229       jvm->DetachCurrentThread();
230     }
231   }
232 }
233 }
234
235 /* "Smartpointer" for Java references. */
236
237 %inline {
238 namespace {
239   class GlobalRefBase
240   {
241     protected:
242       GlobalRefBase (JavaVM *jvm, jobject ref);
243       ~GlobalRefBase ();
244       JavaVM *jvm;
245       jobject jref;
246   };
247   GlobalRefBase::GlobalRefBase (JavaVM *jvm, jobject ref) : jvm(jvm), jref(0) {
248     ScopedEnv env(jvm);
249     if (env && ref)
250       jref = env->NewGlobalRef(ref);
251   }
252   GlobalRefBase::~GlobalRefBase () {
253     ScopedEnv env(jvm);
254     if(env && jref)
255       env->DeleteGlobalRef(jref);
256   }
257   template <class Jtype>
258   class GlobalRef : private GlobalRefBase
259   {
260     public:
261       GlobalRef (JavaVM *jvm, Jtype ref) : GlobalRefBase(jvm, ref) {}
262       GlobalRef (const GlobalRef &ref) : GlobalRefBase(ref.jvm, ref.jref) {}
263       operator Jtype () const { return static_cast<Jtype>(jref); }
264   };
265 }
266 }
267
268 /* Support Java log callbacks. */
269
270 %typemap(javaimports) sigrok::Context
271   "import org.sigrok.core.interfaces.LogCallback;"
272
273 %inline {
274 typedef jobject jlogcallback;
275 }
276
277 %typemap(jni) jlogcallback "jlogcallback"
278 %typemap(jtype) jlogcallback "LogCallback"
279 %typemap(jstype) jlogcallback "LogCallback"
280 %typemap(javain) jlogcallback "$javainput"
281
282 %extend sigrok::Context
283 {
284   void add_log_callback(JNIEnv *env, jlogcallback obj)
285   {
286     JavaVM *jvm = NULL;
287     env->GetJavaVM(&jvm);
288     jclass obj_class = env->GetObjectClass(obj);
289     jmethodID method = env->GetMethodID(obj_class, "run",
290       "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
291     GlobalRef<jclass> LogLevel(jvm, env->FindClass("org/sigrok/core/classes/LogLevel"));
292     jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
293     GlobalRef<jobject> obj_ref(jvm, obj);
294
295     $self->set_log_callback([=] (
296       const sigrok::LogLevel *loglevel,
297       std::string message)
298     {
299       ScopedEnv env(jvm);
300       if (!env)
301         throw sigrok::Error(SR_ERR);
302       jlong loglevel_addr = 0;
303       *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
304       jobject loglevel_obj = env->NewObject(
305         LogLevel, LogLevel_init, loglevel_addr, false);
306       jobject message_obj = env->NewStringUTF(message.c_str());
307       env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
308       if (env->ExceptionCheck())
309         throw sigrok::Error(SR_ERR);
310     });
311   }
312 }
313
314 /* Support Java datafeed callbacks. */
315
316 %typemap(javaimports) sigrok::Session
317   "import org.sigrok.core.interfaces.DatafeedCallback;"
318
319 %inline {
320 typedef jobject jdatafeedcallback;
321 }
322
323 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
324 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
325 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
326 %typemap(javain) jdatafeedcallback "$javainput"
327
328 %extend sigrok::Session
329 {
330   void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
331   {
332     JavaVM *jvm = NULL;
333     env->GetJavaVM(&jvm);
334     jclass obj_class = env->GetObjectClass(obj);
335     jmethodID method = env->GetMethodID(obj_class, "run",
336       "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
337     GlobalRef<jclass> Device(jvm, env->FindClass("org/sigrok/core/classes/Device"));
338     jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
339     GlobalRef<jclass> Packet(jvm, env->FindClass("org/sigrok/core/classes/Packet"));
340     jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
341     GlobalRef<jobject> obj_ref(jvm, obj);
342
343     $self->add_datafeed_callback([=] (
344       std::shared_ptr<sigrok::Device> device,
345       std::shared_ptr<sigrok::Packet> packet)
346     {
347       ScopedEnv env(jvm);
348       if (!env)
349         throw sigrok::Error(SR_ERR);
350       jlong device_addr = 0;
351       jlong packet_addr = 0;
352       *(std::shared_ptr<sigrok::Device> **) &device_addr =
353         new std::shared_ptr<sigrok::Device>(device);
354       *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
355         new std::shared_ptr<sigrok::Packet>(packet);
356       jobject device_obj = env->NewObject(
357         Device, Device_init, device_addr, true);
358       jobject packet_obj = env->NewObject(
359         Packet, Packet_init, packet_addr, true);
360       env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
361       if (env->ExceptionCheck())
362         throw sigrok::Error(SR_ERR);
363     });
364   }
365 }
366
367 %include "doc.i"
368
369 %define %enumextras(Class)
370 %enddef
371
372 /* Ignore these for now, need fixes. */
373 %ignore sigrok::Context::create_analog_packet;
374 %ignore sigrok::Context::create_meta_packet;
375 %ignore sigrok::Meta::config;
376
377 %include "bindings/swig/classes.i"
378