Skip to content

Instantly share code, notes, and snippets.

@OnlyInAmerica
Created May 3, 2016 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OnlyInAmerica/32c36d0a8495439828795f2717d9fe7e to your computer and use it in GitHub Desktop.
Save OnlyInAmerica/32c36d0a8495439828795f2717d9fe7e to your computer and use it in GitHub Desktop.
A SWIG interface file to map Matrix's olm C interface to Java
/*
* Call swig: "swig -java -c++ -package org.chatsecure.olmwrapper -outdir ~/Code/olmJava olm.i"
*/
%module olmwrapper
%{
/* Put headers and other declarations to include in the wrapper code here */
#include "olm.hh"
};
typedef unsigned int size_t; // Technically we'd want int on 32bit, long on 64 bit?
%}
typedef unsigned int size_t; // Technically we'd want int on 32bit, long on 64 bit?
%include "olm.hh"
%include "typemaps.i" // Standard Java <--> C Type mappings
/*
* Map void* to Java's Direct ByteBuffer. We need to allocate mutable memory in order to construct
* the Olm structs like Account, Session etc. The safest way to do this seems to be Java's Direct ByteBuffer.
* The Olm methods that take allocated memory use a void * type.
*/
%typemap(jni) (void * BYTE) "jobject"
%typemap(jtype) (void * BYTE) "java.nio.ByteBuffer"
%typemap(jstype) (void * BYTE) "java.nio.ByteBuffer"
%typemap(javain) (void * BYTE) "$javainput"
%typemap(in) (void * BYTE) {
if (!$input) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "Java data is null!");
return $null;
}
$1 = ($1_ltype) JCALL1(GetDirectBufferAddress, jenv, $input);
}
/*
* Define when to apply the new typemap: if SWIG sees a method signature (also
* partial signature) it applies the patterns. "BYTE" is the placeholder for
* the real parameter name.
*/
%apply (void * BYTE) { (void * memory) };
@OnlyInAmerica
Copy link
Author

Currently the ByteBuffer <--> (void *) input typemap isn't working. An example function signature I'm trying to target from Olm.hh is OlmSession * olm_session(void * memory);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment