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