]> sigrok.org Git - libsigrok.git/blobdiff - bindings/java/org/sigrok/core/classes/classes.i
bindings/java: Fix build issue with SWIG 4.x.
[libsigrok.git] / bindings / java / org / sigrok / core / classes / classes.i
index 894c72f75b9d501daa582b75e744451588018df8..e953fe5d8331052215b59e5c97608c50e851da8a 100644 (file)
 %pragma(java) jniclassimports=%{
 /**
  * @mainpage API Reference
- * 
+ *
  * Introduction
  * ------------
- * 
+ *
  * The sigrok-java API provides an object-oriented Java interface to the
  * functionality in libsigrok. It is built on top of the libsigrokcxx C++ API.
- * 
+ *
  * Getting started
  * ---------------
- * 
+ *
  * Usage of the sigrok-java API needs to begin with a call to Context.create().
  * This will create the global libsigrok context and returns a Context object.
  * Methods on this object provide access to the hardware drivers, input and
  * output formats supported by the library, as well as means of creating other
  * objects such as sessions and triggers.
- * 
+ *
  * Error handling
  * --------------
- * 
+ *
  * When any libsigrok C API call returns an error, an Error exception is raised,
  * which provides access to the error code and description.
  */
@@ -76,7 +76,7 @@ namespace Glib {
   for (auto entry : $1)
   {
     *(CValue **) &value = new CValue(entry);
-    jenv->CallObjectMethod($result, Vector_add,
+    jenv->CallBooleanMethod($result, Vector_add,
       jenv->NewObject(Value, Value_init, value, true));
   }
 }
@@ -94,10 +94,18 @@ VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
   "java.util.Map<JKey, JValue>"
 
 %typemap(javain,
+/* SWIG 4.0.0 changed the std::map wrappers in an incompatible way. */
+#if SWIG_VERSION >= 0x040000
+    pre="  $javaclassname temp$javainput = new $javaclassname();
+    for (java.util.Map.Entry<JKey, JValue> entry : $javainput.entrySet())
+      temp$javainput.put(entry.getKey(), entry.getValue());",
+    pgcppname="temp$javainput")
+#else
     pre="  $javaclassname temp$javainput = new $javaclassname();
     for (java.util.Map.Entry<JKey, JValue> entry : $javainput.entrySet())
       temp$javainput.set(entry.getKey(), entry.getValue());",
     pgcppname="temp$javainput")
+#endif
   std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
 
 %typemap(javaout) std::map< CKey, CValue > {
@@ -193,49 +201,77 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
   }
 }
 
-/* Specialisation for ConfigKey->set<Capability> maps */
+/* Pass JNIEnv parameter to C++ extension methods requiring it. */
 
-MAP_COMMON(const sigrok::ConfigKey *, std::set<enum sigrok::Capability>,
-  ConfigKey, java.util.Set<Capability>)
+%typemap(in, numinputs=0) JNIEnv * %{
+   $1 = jenv;
+%}
 
-%typemap(jni) std::map<const sigrok::ConfigKey *, std::set<enum sigrok::Capability> > "jobject"
-%typemap(jtype) std::map<const sigrok::ConfigKey *, std::set<enum sigrok::Capability> >
-  "java.util.Map<ConfigKey,java.util.Set<Capability>>"
+/* Thread safe JNIEnv handling */
 
-%typemap(out) std::map<const sigrok::ConfigKey *, std::set<enum sigrok::Capability> > {
-  jclass HashMap = jenv->FindClass("java/util/HashMap");
-  jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
-  jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
-    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
-  jclass HashSet = jenv->FindClass("java/util/HashSet");
-  jmethodID HashSet_init = jenv->GetMethodID(HashSet, "<init>", "()V");
-  jmethodID HashSet_add = jenv->GetMethodID(HashSet, "add",
-    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
-  jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
-  jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
-  jclass Capability = jenv->FindClass("org/sigrok/core/classes/Capability");
-  jmethodID Capability_swigToEnum = jenv->GetStaticMethodID(Capability,
-    "swigToEnum", "(I)Lorg/sigrok/core/classes/Capability;");
-  $result = jenv->NewObject(HashMap, HashMap_init);
-  jlong key = 0;
-  for (auto map_entry : $1)
-  {
-    *(const sigrok::ConfigKey **) &key = map_entry.first;
-    jobject value = jenv->NewObject(HashSet, HashSet_init);
-    for (auto &set_entry : map_entry.second)
-      jenv->CallObjectMethod(value, HashSet_add,
-        jenv->CallStaticObjectMethod(Capability,
-          Capability_swigToEnum, set_entry));
-    jenv->CallObjectMethod($result, HashMap_put,
-      jenv->NewObject(ConfigKey, ConfigKey_init, key, false), value);
+%inline {
+namespace {
+  class ScopedEnv {
+    public:
+      ScopedEnv(JavaVM *jvm);
+      ~ScopedEnv();
+      JNIEnv* operator-> () { return env; }
+      operator bool () const { return (bool)env; }
+    private:
+      JavaVM *jvm;
+      JNIEnv *env;
+      int env_status;
+  };
+  ScopedEnv::ScopedEnv(JavaVM *jvm) : jvm(jvm), env(NULL) {
+    env_status = jvm->GetEnv((void **)&env, JNI_VERSION_1_2);
+    if (env_status == JNI_EDETACHED) {
+%#if defined(__ANDROID__)
+      jvm->AttachCurrentThread(&env, NULL);
+%#else
+      jvm->AttachCurrentThread((void **)&env, NULL);
+%#endif
+    }
   }
+  ScopedEnv::~ScopedEnv() {
+    if (env_status == JNI_EDETACHED) {
+      jvm->DetachCurrentThread();
+    }
+  }
+}
 }
 
-/* Pass JNIEnv parameter to C++ extension methods requiring it. */
+/* "Smartpointer" for Java references. */
 
-%typemap(in, numinputs=0) JNIEnv * %{
-   $1 = jenv;
-%} 
+%inline {
+namespace {
+  class GlobalRefBase
+  {
+    protected:
+      GlobalRefBase (JavaVM *jvm, jobject ref);
+      ~GlobalRefBase ();
+      JavaVM *jvm;
+      jobject jref;
+  };
+  GlobalRefBase::GlobalRefBase (JavaVM *jvm, jobject ref) : jvm(jvm), jref(0) {
+    ScopedEnv env(jvm);
+    if (env && ref)
+      jref = env->NewGlobalRef(ref);
+  }
+  GlobalRefBase::~GlobalRefBase () {
+    ScopedEnv env(jvm);
+    if(env && jref)
+      env->DeleteGlobalRef(jref);
+  }
+  template <class Jtype>
+  class GlobalRef : private GlobalRefBase
+  {
+    public:
+      GlobalRef (JavaVM *jvm, Jtype ref) : GlobalRefBase(jvm, ref) {}
+      GlobalRef (const GlobalRef &ref) : GlobalRefBase(ref.jvm, ref.jref) {}
+      operator Jtype () const { return static_cast<Jtype>(jref); }
+  };
+}
+}
 
 /* Support Java log callbacks. */
 
@@ -255,18 +291,22 @@ typedef jobject jlogcallback;
 {
   void add_log_callback(JNIEnv *env, jlogcallback obj)
   {
+    JavaVM *jvm = NULL;
+    env->GetJavaVM(&jvm);
     jclass obj_class = env->GetObjectClass(obj);
     jmethodID method = env->GetMethodID(obj_class, "run",
       "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
-    jclass LogLevel = (jclass) env->NewGlobalRef(
-        env->FindClass("org/sigrok/core/classes/LogLevel"));
+    GlobalRef<jclass> LogLevel(jvm, env->FindClass("org/sigrok/core/classes/LogLevel"));
     jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
-    jobject obj_ref = env->NewGlobalRef(obj);
+    GlobalRef<jobject> obj_ref(jvm, obj);
 
     $self->set_log_callback([=] (
       const sigrok::LogLevel *loglevel,
       std::string message)
     {
+      ScopedEnv env(jvm);
+      if (!env)
+        throw sigrok::Error(SR_ERR);
       jlong loglevel_addr = 0;
       *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
       jobject loglevel_obj = env->NewObject(
@@ -297,21 +337,24 @@ typedef jobject jdatafeedcallback;
 {
   void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
   {
+    JavaVM *jvm = NULL;
+    env->GetJavaVM(&jvm);
     jclass obj_class = env->GetObjectClass(obj);
     jmethodID method = env->GetMethodID(obj_class, "run",
       "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
-    jclass Device = (jclass) env->NewGlobalRef(
-        env->FindClass("org/sigrok/core/classes/Device"));
+    GlobalRef<jclass> Device(jvm, env->FindClass("org/sigrok/core/classes/Device"));
     jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
-    jclass Packet = (jclass) env->NewGlobalRef(
-       env->FindClass("org/sigrok/core/classes/Packet"));
+    GlobalRef<jclass> Packet(jvm, env->FindClass("org/sigrok/core/classes/Packet"));
     jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
-    jobject obj_ref = env->NewGlobalRef(obj);
+    GlobalRef<jobject> obj_ref(jvm, obj);
 
     $self->add_datafeed_callback([=] (
       std::shared_ptr<sigrok::Device> device,
       std::shared_ptr<sigrok::Packet> packet)
     {
+      ScopedEnv env(jvm);
+      if (!env)
+        throw sigrok::Error(SR_ERR);
       jlong device_addr = 0;
       jlong packet_addr = 0;
       *(std::shared_ptr<sigrok::Device> **) &device_addr =