]> sigrok.org Git - libsigrok.git/blob - bindings/java/org/sigrok/core/classes/classes.i
Java: ignore Meta::get_config() due to SWIG typemap issues.
[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(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
161 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
162   "java.util.Map<ConfigKey,Variant>"
163
164 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
165   jclass HashMap = jenv->FindClass("java/util/HashMap");
166   jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
167   jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
168     "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
169   jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
170   jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
171   jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
172   jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
173   $result = jenv->NewObject(HashMap, HashMap_init);
174   jlong key;
175   jlong value;
176   for (auto entry : $1)
177   {
178     *(const sigrok::ConfigKey **) &key = entry.first;
179     *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
180     jenv->CallObjectMethod($result, HashMap_put,
181       jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
182       jenv->NewObject(Variant, Variant_init, value, true));
183   }
184 }
185
186 /* Support Driver.scan() with no arguments. */
187 %extend sigrok::Driver {
188   std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
189   {
190     std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
191     return $self->scan(options);
192   }
193 }
194
195 /* Support OutputFormat.create_output(device) with no options. */
196 %extend sigrok::OutputFormat {
197   std::shared_ptr<sigrok::Output> create_output(
198     std::shared_ptr<sigrok::Device> device)
199   {
200     std::map<std::string, std::string> options;
201     return $self->create_output(device, options);
202   }
203 }
204
205 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
206
207 %typemap(in, numinputs=0) JNIEnv * %{
208    $1 = jenv;
209 %} 
210
211 /* Support Java log callbacks. */
212
213 %inline {
214 typedef jobject jlogcallback;
215 }
216
217 %typemap(jni) jlogcallback "jlogcallback"
218 %typemap(jtype) jlogcallback "LogCallback"
219 %typemap(jstype) jlogcallback "LogCallback"
220 %typemap(javain) jlogcallback "$javainput"
221
222 %extend sigrok::Context
223 {
224   void add_log_callback(JNIEnv *env, jlogcallback obj)
225   {
226     jclass obj_class = env->GetObjectClass(obj);
227     jmethodID method = env->GetMethodID(obj_class, "run",
228       "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
229     jclass LogLevel = (jclass) env->NewGlobalRef(
230         env->FindClass("org/sigrok/core/classes/LogLevel"));
231     jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
232     jobject obj_ref = env->NewGlobalRef(obj);
233
234     $self->set_log_callback([=] (
235       const sigrok::LogLevel *loglevel,
236       std::string message)
237     {
238       jlong loglevel_addr;
239       *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
240       jobject loglevel_obj = env->NewObject(
241         LogLevel, LogLevel_init, loglevel_addr, false);
242       jobject message_obj = env->NewStringUTF(message.c_str());
243       env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
244       if (env->ExceptionCheck())
245         throw sigrok::Error(SR_ERR);
246     });
247   }
248 }
249
250 /* Support Java datafeed callbacks. */
251
252 %inline {
253 typedef jobject jdatafeedcallback;
254 }
255
256 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
257 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
258 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
259 %typemap(javain) jdatafeedcallback "$javainput"
260
261 %extend sigrok::Session
262 {
263   void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
264   {
265     jclass obj_class = env->GetObjectClass(obj);
266     jmethodID method = env->GetMethodID(obj_class, "run",
267       "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
268     jclass Device = (jclass) env->NewGlobalRef(
269         env->FindClass("org/sigrok/core/classes/Device"));
270     jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
271     jclass Packet = (jclass) env->NewGlobalRef(
272        env->FindClass("org/sigrok/core/classes/Packet"));
273     jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
274     jobject obj_ref = env->NewGlobalRef(obj);
275
276     $self->add_datafeed_callback([=] (
277       std::shared_ptr<sigrok::Device> device,
278       std::shared_ptr<sigrok::Packet> packet)
279     {
280       jlong device_addr;
281       jlong packet_addr;
282       *(std::shared_ptr<sigrok::Device> **) &device_addr =
283         new std::shared_ptr<sigrok::Device>(device);
284       *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
285         new std::shared_ptr<sigrok::Packet>(packet);
286       jobject device_obj = env->NewObject(
287         Device, Device_init, device_addr, true);
288       jobject packet_obj = env->NewObject(
289         Packet, Packet_init, packet_addr, true);
290       env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
291       if (env->ExceptionCheck())
292         throw sigrok::Error(SR_ERR);
293     });
294   }
295 }
296
297 /* Support Java event source callbacks. */
298
299 %inline {
300 typedef jobject jsourcecallback;
301 }
302
303 %typemap(jni) jsourcecallback "jsourcecallback"
304 %typemap(jtype) jsourcecallback "SourceCallback"
305 %typemap(jstype) jsourcecallback "SourceCallback"
306 %typemap(javain) jsourcecallback "$javainput"
307
308 %extend sigrok::EventSource
309 {
310   std::shared_ptr<sigrok::EventSource> create(
311     int fd, Glib::IOCondition events, int timeout,
312     JNIEnv *env, jsourcecallback obj)
313   {
314     (void) $self;
315     jclass obj_class = env->GetObjectClass(obj);
316     jmethodID method = env->GetMethodID(obj_class, "run", "(I)V");
317     jobject obj_ref = env->NewGlobalRef(obj);
318
319     return sigrok::EventSource::create(fd, events, timeout, [=] (int revents)
320     {
321       bool result = env->CallBooleanMethod(obj_ref, method, revents);
322       if (env->ExceptionCheck())
323         throw sigrok::Error(SR_ERR);
324       return result;
325     });
326   }
327 }
328
329 /* Currently broken due to some std::map typemap issues. */
330 %ignore sigrok::Meta::get_config;
331
332 %include "bindings/swig/classes.i"