3 /* Automatically load JNI library. */
4 %pragma(java) jniclasscode=%{
6 System.loadLibrary("sigrok_java_core_classes");
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;
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"
24 jclass FileDescriptor = jenv->FindClass("java/io/FileDescriptor");
25 jfieldID fd = jenv->GetFieldID(FileDescriptor, "fd", "I");
26 $1 = jenv->GetIntField($input, fd);
29 /* Map Glib::VariantBase to a Variant class in Java */
30 %rename(Variant) VariantBase;
35 /* Map between std::vector and java.util.Vector */
36 %define VECTOR(CValue, JValue)
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>"
43 pre=" $javaclassname temp$javainput = $javaclassname.convertVector($javainput);",
44 pgcppname="temp$javainput")
45 std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
47 %typemap(javacode) std::vector< CValue > %{
48 static $javaclassname convertVector(java.util.Vector<JValue> in)
50 $javaclassname out = new $javaclassname();
51 for (JValue value : in)
57 %typemap(javaout) std::vector< CValue > {
58 return (java.util.Vector<JValue>)$jnicall;
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);
72 *(CValue **) &value = new CValue(entry);
73 jenv->CallObjectMethod($result, Vector_add,
74 jenv->NewObject(Value, Value_init, value, true));
80 VECTOR(std::shared_ptr<sigrok::Channel>, Channel)
81 VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
83 /* Common macro for mapping between std::map and java.util.Map */
85 %define MAP_COMMON(CKey, CValue, JKey, JValue)
87 %typemap(jstype) std::map< CKey, CValue >
88 "java.util.Map<JKey, JValue>"
91 pre=" $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
92 pgcppname="temp$javainput")
93 std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
95 %typemap(javacode) std::map< CKey, CValue > %{
96 static $javaclassname convertMap(java.util.Map<JKey,JValue> in)
98 $javaclassname out = new $javaclassname();
99 for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
100 out.set(entry.getKey(), entry.getValue());
105 %typemap(javaout) std::map< CKey, CValue > {
106 return (java.util.Map<JKey, JValue>)$jnicall;
111 /* Specialisation for string->string maps. */
113 MAP_COMMON(std::string, std::string, String, String)
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()));
127 /* Specialisation macro for string->shared_ptr maps. */
129 %define STRING_TO_SHARED_PTR_MAP(ClassName)
131 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
133 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
134 "java.util.Map<String,ClassName>"
136 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
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);
147 for (auto entry : $1)
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));
159 STRING_TO_SHARED_PTR_MAP(Driver)
160 STRING_TO_SHARED_PTR_MAP(InputFormat)
161 STRING_TO_SHARED_PTR_MAP(OutputFormat)
163 /* Specialisation for ConfigKey->Variant maps */
165 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
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>"
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);
183 for (auto entry : $1)
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));
193 /* Support Driver.scan() with no arguments. */
194 %extend sigrok::Driver {
195 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
197 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
198 return $self->scan(options);
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)
207 std::map<std::string, Glib::VariantBase> options;
208 return $self->create_output(device, options);
212 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
214 %typemap(in, numinputs=0) JNIEnv * %{
218 /* Support Java log callbacks. */
220 %typemap(javaimports) sigrok::Context
221 "import org.sigrok.core.interfaces.LogCallback;"
224 typedef jobject jlogcallback;
227 %typemap(jni) jlogcallback "jlogcallback"
228 %typemap(jtype) jlogcallback "LogCallback"
229 %typemap(jstype) jlogcallback "LogCallback"
230 %typemap(javain) jlogcallback "$javainput"
232 %extend sigrok::Context
234 void add_log_callback(JNIEnv *env, jlogcallback obj)
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);
244 $self->set_log_callback([=] (
245 const sigrok::LogLevel *loglevel,
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);
260 /* Support Java datafeed callbacks. */
262 %typemap(javaimports) sigrok::Session
263 "import org.sigrok.core.interfaces.DatafeedCallback;"
266 typedef jobject jdatafeedcallback;
269 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
270 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
271 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
272 %typemap(javain) jdatafeedcallback "$javainput"
274 %extend sigrok::Session
276 void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
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);
289 $self->add_datafeed_callback([=] (
290 std::shared_ptr<sigrok::Device> device,
291 std::shared_ptr<sigrok::Packet> packet)
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);
310 /* Support Java event source callbacks. */
312 %typemap(javaimports) sigrok::EventSource
313 "import org.sigrok.core.interfaces.SourceCallback;"
316 typedef jobject jsourcecallback;
319 %typemap(jni) jsourcecallback "jsourcecallback"
320 %typemap(jtype) jsourcecallback "SourceCallback"
321 %typemap(jstype) jsourcecallback "SourceCallback"
322 %typemap(javain) jsourcecallback "$javainput"
324 %extend sigrok::EventSource
326 std::shared_ptr<sigrok::EventSource> create(
327 int fd, Glib::IOCondition events, int timeout,
328 JNIEnv *env, jsourcecallback obj)
331 jclass obj_class = env->GetObjectClass(obj);
332 jmethodID method = env->GetMethodID(obj_class, "run", "(I)V");
333 jobject obj_ref = env->NewGlobalRef(obj);
335 return sigrok::EventSource::create(fd, events, timeout, [=] (int revents)
337 bool result = env->CallBooleanMethod(obj_ref, method, revents);
338 if (env->ExceptionCheck())
339 throw sigrok::Error(SR_ERR);
345 /* Currently broken due to some std::map typemap issues. */
346 %ignore sigrok::Meta::get_config;
349 %include "bindings/swig/classes.i"