1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <unistd.h>
#include <jni.h>
#include <sal/types.h>
#include <android/log.h>
#include <osl/detail/android-bootstrap.h>
#define LOK_USE_UNSTABLE_API
#include <LibreOfficeKit/LibreOfficeKit.h>
/* LibreOfficeKit */
namespace
{
jfieldID getHandleField(JNIEnv* pEnv, jobject aObject)
{
jclass clazz = pEnv->GetObjectClass(aObject);
return pEnv->GetFieldID(clazz, "handle", "Ljava/nio/ByteBuffer;");
}
template <typename T>
T* getHandle(JNIEnv* pEnv, jobject aObject)
{
jobject aHandle = pEnv->GetObjectField(aObject, getHandleField(pEnv, aObject));
return reinterpret_cast<T*>(pEnv->GetDirectBufferAddress(aHandle));
}
const char* copyJavaString(JNIEnv* pEnv, jstring aJavaString)
{
const char* pClone = NULL;
const char* pTemp = pEnv->GetStringUTFChars(aJavaString, NULL);
pClone = strdup(pTemp);
pEnv->ReleaseStringUTFChars(aJavaString, pTemp);
return pClone;
}
} // anonymous namespace
extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Office_getError
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
char* pError = pLibreOfficeKit->pClass->getError(pLibreOfficeKit);
return pEnv->NewStringUTF(pError);
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_destroy
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
pLibreOfficeKit->pClass->destroy(pLibreOfficeKit);
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_destroyAndExit(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
pLibreOfficeKit->pClass->destroy(pLibreOfficeKit);
// Stopgap fix: _exit() to force the OS to restart the LO activity.
// Better than to hang.
_exit(0);
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Office_postKeyEvent
(JNIEnv* pEnv, jobject aObject, jint nType, jint nCode)
{
LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
pLibreOfficeKit->pClass->postKeyEvent(pLibreOfficeKit, nType, nCode);
}
namespace
{
struct CallbackData
{
jmethodID aJavaCallbackMethod;
jclass aClass;
jobject aObject;
};
static CallbackData gCallbackData;
/**
* Handle retrieved callback
*/
void messageCallback(int nType, const char* pPayload, void* pData)
{
CallbackData* pCallbackData = (CallbackData*) pData;
JavaVM* pJavaVM = lo_get_javavm();
JNIEnv* pEnv;
bool bIsAttached = false;
int status = pJavaVM->GetEnv((void **) &pEnv, JNI_VERSION_1_6);
if(status < 0)
{
status = pJavaVM->AttachCurrentThread(&pEnv, NULL);
if(status < 0)
{
return;
}
bIsAttached = true;
}
jstring sPayload = pEnv->NewStringUTF(pPayload);
jvalue aParameter[2];
aParameter[0].i = nType;
aParameter[1].l = sPayload;
pEnv->CallVoidMethodA(pCallbackData->aObject, pCallbackData->aJavaCallbackMethod, aParameter);
pEnv->DeleteLocalRef(sPayload);
if (bIsAttached)
{
pJavaVM->DetachCurrentThread();
}
}
} // anonymous namespace
extern "C" SAL_JNI_EXPORT jobject JNICALL Java_org_libreoffice_kit_Office_documentLoadNative
(JNIEnv* pEnv, jobject aObject, jstring documentPath)
{
const char* aCloneDocumentPath = copyJavaString(pEnv, documentPath);
LibreOfficeKit* pLibreOfficeKit = getHandle<LibreOfficeKit>(pEnv, aObject);
LibreOfficeKitDocument* pDocument = pLibreOfficeKit->pClass->documentLoad(pLibreOfficeKit, aCloneDocumentPath);
if (pDocument == NULL)
return NULL;
jobject aHandle = pEnv->NewDirectByteBuffer((void*) pDocument, sizeof(LibreOfficeKitDocument));
return aHandle;
}
/* Document */
/** Implementation of org.libreoffice.kit.Document.bindMessageCallback method */
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_bindMessageCallback
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
gCallbackData.aObject = (jobject) pEnv->NewGlobalRef(aObject);
jclass aClass = pEnv->GetObjectClass(aObject);
gCallbackData.aClass = (jclass) pEnv->NewGlobalRef(aClass);
gCallbackData.aJavaCallbackMethod = pEnv->GetMethodID(aClass, "messageRetrieved", "(ILjava/lang/String;)V");
pDocument->pClass->registerCallback(pDocument, messageCallback, (void*) &gCallbackData);
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_destroy
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
pDocument->pClass->destroy(pDocument);
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_setPart
(JNIEnv* pEnv, jobject aObject, jint aPart)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
pDocument->pClass->setPart(pDocument, aPart);
}
extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getPart
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
return (jint) pDocument->pClass->getPart(pDocument);
}
extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getParts
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
return (jint) pDocument->pClass->getParts(pDocument);
}
extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getPartName
(JNIEnv* pEnv, jobject aObject, jint nPartIndex)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
char* pPartName = pDocument->pClass->getPartName(pDocument, nPartIndex);
return pEnv->NewStringUTF(pPartName);
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_setPartMode
(JNIEnv* pEnv, jobject aObject, jint nPartMode)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
pDocument->pClass->setPartMode(pDocument, (LibreOfficeKitPartMode) nPartMode);
}
extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getDocumentTypeNative
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
return (jint) pDocument->pClass->getDocumentType(pDocument);
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_paintTileNative
(JNIEnv* pEnv, jobject aObject, jobject aByteBuffer,
jint nCanvasWidth, jint nCanvasHeight, jint nTilePosX, jint nTilePosY,
jint nTileWidth, jint nTileHeight)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
unsigned char* buffer = (unsigned char*) pEnv->GetDirectBufferAddress(aByteBuffer);
int nStride = 0;
pDocument->pClass->paintTile(pDocument, buffer, nCanvasWidth, nCanvasHeight, &nStride, nTilePosX, nTilePosY, nTileWidth, nTileHeight);
(void) nStride;
}
extern "C" SAL_JNI_EXPORT jlong JNICALL Java_org_libreoffice_kit_Document_getDocumentHeight
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
long nWidth;
long nHeight;
pDocument->pClass->getDocumentSize(pDocument, &nWidth, &nHeight);
return nHeight;
}
extern "C" SAL_JNI_EXPORT jlong JNICALL Java_org_libreoffice_kit_Document_getDocumentWidth
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
long nWidth;
long nHeight;
pDocument->pClass->getDocumentSize(pDocument, &nWidth, &nHeight);
return nWidth;
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_initializeForRendering
(JNIEnv* pEnv, jobject aObject)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
pDocument->pClass->initializeForRendering(pDocument);
}
extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Office_saveAs
(JNIEnv* pEnv, jobject aObject, jstring sUrl, jstring sFormat, jstring sOptions)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
const char* pUrl = pEnv->GetStringUTFChars(sUrl, NULL);
const char* pFormat = pEnv->GetStringUTFChars(sFormat, NULL);
const char* pOptions = pEnv->GetStringUTFChars(sOptions, NULL);
int result = pDocument->pClass->saveAs(pDocument, pUrl, pFormat, pOptions);
pEnv->ReleaseStringUTFChars(sUrl, pUrl);
pEnv->ReleaseStringUTFChars(sFormat, pFormat);
pEnv->ReleaseStringUTFChars(sOptions, pOptions);
return result;
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_Document_postMouseEvent
(JNIEnv* pEnv, jobject aObject, jint type, jint x, jint y, jint count)
{
LibreOfficeKitDocument* pDocument = getHandle<LibreOfficeKitDocument>(pEnv, aObject);
pDocument->pClass->postMouseEvent(pDocument, type, x, y, count);
}
/* DirectBufferAllocator */
extern "C" SAL_JNI_EXPORT jobject JNICALL Java_org_libreoffice_kit_DirectBufferAllocator_allocateDirectBufferNative
(JNIEnv* pEnv, jclass /*aClass*/, jint nSize)
{
jobject aBuffer = NULL;
if (nSize > 0)
{
void* pMemory = malloc(nSize);
if (pMemory != NULL)
{
aBuffer = pEnv->NewDirectByteBuffer(pMemory, nSize);
if (aBuffer == NULL)
{
free(pMemory);
}
}
}
return aBuffer;
}
extern "C" SAL_JNI_EXPORT void JNICALL Java_org_libreoffice_kit_DirectBufferAllocator_freeDirectBufferNative
(JNIEnv* pEnv, jclass, jobject aBuffer)
{
free(pEnv->GetDirectBufferAddress(aBuffer));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|