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