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