1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 #include <sal/types.h>
14 #include <vcl/event.hxx>
15 #include <android/log.h>
17 #include <osl/detail/android-bootstrap.h>
19 #include <LibreOfficeKit/LibreOfficeKit.h>
26 jfieldID
getHandleField(JNIEnv
* pEnv
, jobject aObject
)
28 jclass clazz
= pEnv
->GetObjectClass(aObject
);
29 return pEnv
->GetFieldID(clazz
, "handle", "Ljava/nio/ByteBuffer;");
33 T
* getHandle(JNIEnv
* pEnv
, jobject aObject
)
35 jobject aHandle
= pEnv
->GetObjectField(aObject
, getHandleField(pEnv
, aObject
));
36 return reinterpret_cast<T
*>(pEnv
->GetDirectBufferAddress(aHandle
));
39 const char* copyJavaString(JNIEnv
* pEnv
, jstring aJavaString
)
41 const char* pTemp
= pEnv
->GetStringUTFChars(aJavaString
, NULL
);
42 const char* pClone
= strdup(pTemp
);
43 pEnv
->ReleaseStringUTFChars(aJavaString
, pTemp
);
48 } // anonymous namespace
50 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Office_getError
51 (JNIEnv
* pEnv
, jobject aObject
)
53 LibreOfficeKit
* pLibreOfficeKit
= getHandle
<LibreOfficeKit
>(pEnv
, aObject
);
54 char* pError
= pLibreOfficeKit
->pClass
->getError(pLibreOfficeKit
);
55 return pEnv
->NewStringUTF(pError
);
58 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Office_destroy
59 (JNIEnv
* pEnv
, jobject aObject
)
61 LibreOfficeKit
* pLibreOfficeKit
= getHandle
<LibreOfficeKit
>(pEnv
, aObject
);
62 pLibreOfficeKit
->pClass
->destroy(pLibreOfficeKit
);
65 extern "C" SAL_JNI_EXPORT
void JNICALL
Java_org_libreoffice_kit_Office_destroyAndExit(JNIEnv
* pEnv
, jobject aObject
)
67 LibreOfficeKit
* pLibreOfficeKit
= getHandle
<LibreOfficeKit
>(pEnv
, aObject
);
68 pLibreOfficeKit
->pClass
->destroy(pLibreOfficeKit
);
69 // Stopgap fix: _exit() to force the OS to restart the LO activity.
70 // Better than to hang.
79 jmethodID aJavaCallbackMethod
;
84 static CallbackData gCallbackData
;
85 static CallbackData gCallbackDataLOKit
;
88 * Handle retrieved callback
90 void messageCallback(int nType
, const char* pPayload
, void* pData
)
92 CallbackData
* pCallbackData
= (CallbackData
*) pData
;
94 JavaVM
* pJavaVM
= lo_get_javavm();
96 bool bIsAttached
= false;
98 int status
= pJavaVM
->GetEnv((void **) &pEnv
, JNI_VERSION_1_6
);
102 status
= pJavaVM
->AttachCurrentThread(&pEnv
, NULL
);
110 jstring sPayload
= pEnv
->NewStringUTF(pPayload
);
112 jvalue aParameter
[2];
113 aParameter
[0].i
= nType
;
114 aParameter
[1].l
= sPayload
;
116 pEnv
->CallVoidMethodA(pCallbackData
->aObject
, pCallbackData
->aJavaCallbackMethod
, aParameter
);
118 pEnv
->DeleteLocalRef(sPayload
);
122 pJavaVM
->DetachCurrentThread();
126 } // anonymous namespace
128 extern "C" SAL_JNI_EXPORT jobject JNICALL Java_org_libreoffice_kit_Office_documentLoadNative
129 (JNIEnv
* pEnv
, jobject aObject
, jstring documentPath
)
131 const char* aCloneDocumentPath
= copyJavaString(pEnv
, documentPath
);
132 LibreOfficeKit
* pLibreOfficeKit
= getHandle
<LibreOfficeKit
>(pEnv
, aObject
);
134 LibreOfficeKitDocument
* pDocument
= pLibreOfficeKit
->pClass
->documentLoad(pLibreOfficeKit
, aCloneDocumentPath
);
136 if (pDocument
== NULL
)
139 jobject aHandle
= pEnv
->NewDirectByteBuffer((void*) pDocument
, sizeof(LibreOfficeKitDocument
));
144 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Office_setDocumentPassword
145 (JNIEnv
* pEnv
, jobject aObject
, jstring sUrl
, jstring sPassword
)
147 LibreOfficeKit
* pLibreOfficeKit
= getHandle
<LibreOfficeKit
>(pEnv
, aObject
);
149 char const* pUrl
= copyJavaString(pEnv
, sUrl
);
150 if (sPassword
== NULL
) {
151 pLibreOfficeKit
->pClass
->setDocumentPassword(pLibreOfficeKit
, pUrl
, nullptr);
153 char const* pPassword
= copyJavaString(pEnv
, sPassword
);
154 pLibreOfficeKit
->pClass
->setDocumentPassword(pLibreOfficeKit
, pUrl
, pPassword
);
158 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Office_setOptionalFeatures
159 (JNIEnv
* pEnv
, jobject aObject
, jlong options
)
161 LibreOfficeKit
* pLibreOfficeKit
= getHandle
<LibreOfficeKit
>(pEnv
, aObject
);
163 unsigned long long pOptions
= (unsigned long long)options
;
165 pLibreOfficeKit
->pClass
->setOptionalFeatures(pLibreOfficeKit
, pOptions
);
168 /** Implementation of org.libreoffice.kit.Office.bindMessageCallback method */
169 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Office_bindMessageCallback
170 (JNIEnv
* pEnv
, jobject aObject
)
172 LibreOfficeKit
* pLibreOfficeKit
= getHandle
<LibreOfficeKit
>(pEnv
, aObject
);
174 gCallbackDataLOKit
.aObject
= (jobject
) pEnv
->NewGlobalRef(aObject
);
175 jclass aClass
= pEnv
->GetObjectClass(aObject
);
176 gCallbackDataLOKit
.aClass
= (jclass
) pEnv
->NewGlobalRef(aClass
);
178 gCallbackDataLOKit
.aJavaCallbackMethod
= pEnv
->GetMethodID(aClass
, "messageRetrievedLOKit", "(ILjava/lang/String;)V");
180 pLibreOfficeKit
->pClass
->registerCallback(pLibreOfficeKit
, messageCallback
, (void*) &gCallbackDataLOKit
);
185 /** Implementation of org.libreoffice.kit.Document.bindMessageCallback method */
186 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_bindMessageCallback
187 (JNIEnv
* pEnv
, jobject aObject
)
189 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
191 gCallbackData
.aObject
= (jobject
) pEnv
->NewGlobalRef(aObject
);
192 jclass aClass
= pEnv
->GetObjectClass(aObject
);
193 gCallbackData
.aClass
= (jclass
) pEnv
->NewGlobalRef(aClass
);
195 gCallbackData
.aJavaCallbackMethod
= pEnv
->GetMethodID(aClass
, "messageRetrieved", "(ILjava/lang/String;)V");
197 pDocument
->pClass
->registerCallback(pDocument
, messageCallback
, (void*) &gCallbackData
);
200 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_destroy
201 (JNIEnv
* pEnv
, jobject aObject
)
203 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
204 pDocument
->pClass
->destroy(pDocument
);
207 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_setPart
208 (JNIEnv
* pEnv
, jobject aObject
, jint aPart
)
210 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
211 pDocument
->pClass
->setPart(pDocument
, aPart
);
214 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getPart
215 (JNIEnv
* pEnv
, jobject aObject
)
217 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
218 return (jint
) pDocument
->pClass
->getPart(pDocument
);
221 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getPartPageRectangles
222 (JNIEnv
* pEnv
, jobject aObject
)
224 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
225 char* pRectangles
= pDocument
->pClass
->getPartPageRectangles(pDocument
);
226 return pEnv
->NewStringUTF(pRectangles
);
229 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getParts
230 (JNIEnv
* pEnv
, jobject aObject
)
232 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
233 return (jint
) pDocument
->pClass
->getParts(pDocument
);
236 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getPartName
237 (JNIEnv
* pEnv
, jobject aObject
, jint nPartIndex
)
239 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
240 char* pPartName
= pDocument
->pClass
->getPartName(pDocument
, nPartIndex
);
241 return pEnv
->NewStringUTF(pPartName
);
244 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_setPartMode
245 (JNIEnv
* pEnv
, jobject aObject
, jint nPartMode
)
247 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
249 pDocument
->pClass
->setPartMode(pDocument
, nPartMode
);
252 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_getDocumentTypeNative
253 (JNIEnv
* pEnv
, jobject aObject
)
255 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
256 return (jint
) pDocument
->pClass
->getDocumentType(pDocument
);
259 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_paintTileNative
260 (JNIEnv
* pEnv
, jobject aObject
, jobject aByteBuffer
,
261 jint nCanvasWidth
, jint nCanvasHeight
, jint nTilePosX
, jint nTilePosY
,
262 jint nTileWidth
, jint nTileHeight
)
264 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
266 unsigned char* buffer
= (unsigned char*) pEnv
->GetDirectBufferAddress(aByteBuffer
);
267 pDocument
->pClass
->paintTile(pDocument
, buffer
, nCanvasWidth
, nCanvasHeight
, nTilePosX
, nTilePosY
, nTileWidth
, nTileHeight
);
270 extern "C" SAL_JNI_EXPORT jlong JNICALL Java_org_libreoffice_kit_Document_getDocumentHeight
271 (JNIEnv
* pEnv
, jobject aObject
)
273 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
276 pDocument
->pClass
->getDocumentSize(pDocument
, &nWidth
, &nHeight
);
280 extern "C" SAL_JNI_EXPORT jlong JNICALL Java_org_libreoffice_kit_Document_getDocumentWidth
281 (JNIEnv
* pEnv
, jobject aObject
)
283 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
286 pDocument
->pClass
->getDocumentSize(pDocument
, &nWidth
, &nHeight
);
290 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_initializeForRendering
291 (JNIEnv
* pEnv
, jobject aObject
)
293 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
294 pDocument
->pClass
->initializeForRendering(pDocument
, NULL
);
297 extern "C" SAL_JNI_EXPORT jint JNICALL Java_org_libreoffice_kit_Document_saveAs
298 (JNIEnv
* pEnv
, jobject aObject
, jstring sUrl
, jstring sFormat
, jstring sOptions
)
300 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
302 const char* pUrl
= pEnv
->GetStringUTFChars(sUrl
, NULL
);
303 const char* pFormat
= pEnv
->GetStringUTFChars(sFormat
, NULL
);
304 const char* pOptions
= pEnv
->GetStringUTFChars(sOptions
, NULL
);
306 int result
= pDocument
->pClass
->saveAs(pDocument
, pUrl
, pFormat
, pOptions
);
308 pEnv
->ReleaseStringUTFChars(sUrl
, pUrl
);
309 pEnv
->ReleaseStringUTFChars(sFormat
, pFormat
);
310 pEnv
->ReleaseStringUTFChars(sOptions
, pOptions
);
315 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_postKeyEvent
316 (JNIEnv
* pEnv
, jobject aObject
, jint nType
, jint nCharCode
, jint nKeyCode
)
318 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
319 pDocument
->pClass
->postKeyEvent(pDocument
, nType
, nCharCode
, nKeyCode
);
322 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_postMouseEvent
323 (JNIEnv
* pEnv
, jobject aObject
, jint type
, jint x
, jint y
, jint count
, jint button
, jint modifier
)
325 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
326 pDocument
->pClass
->postMouseEvent(pDocument
, type
, x
, y
, count
, button
, modifier
);
329 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_postUnoCommand
330 (JNIEnv
* pEnv
, jobject aObject
, jstring command
, jstring arguments
, jboolean bNotifyWhenFinished
)
332 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
334 const char* pCommand
= pEnv
->GetStringUTFChars(command
, NULL
);
335 const char* pArguments
= nullptr;
336 if (arguments
!= NULL
)
337 pArguments
= pEnv
->GetStringUTFChars(arguments
, NULL
);
339 pDocument
->pClass
->postUnoCommand(pDocument
, pCommand
, pArguments
, bNotifyWhenFinished
);
341 pEnv
->ReleaseStringUTFChars(command
, pCommand
);
342 if (arguments
!= NULL
)
343 pEnv
->ReleaseStringUTFChars(arguments
, pArguments
);
346 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_setTextSelection
347 (JNIEnv
* pEnv
, jobject aObject
, jint type
, jint x
, jint y
)
349 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
350 pDocument
->pClass
->setTextSelection(pDocument
, type
, x
, y
);
353 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getTextSelection
354 (JNIEnv
* pEnv
, jobject aObject
, jstring mimeType
)
356 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
358 const char* pMimeType
= pEnv
->GetStringUTFChars(mimeType
, NULL
);
360 char* pUsedMimeType
= 0;
361 LibreOfficeKitDocumentClass
* pcls
= pDocument
->pClass
;
362 char* pSelection
= pcls
->getTextSelection(pDocument
, pMimeType
, &pUsedMimeType
);
365 pEnv
->ReleaseStringUTFChars(mimeType
, pMimeType
);
367 return pEnv
->NewStringUTF(pSelection
);
370 extern "C" SAL_JNI_EXPORT jboolean JNICALL Java_org_libreoffice_kit_Document_paste
371 (JNIEnv
* pEnv
, jobject aObject
, jstring mimeType
, jstring data
)
373 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
375 const char* pMimeType
= pEnv
->GetStringUTFChars(mimeType
, NULL
);
376 const char* pData
= pEnv
->GetStringUTFChars(data
, NULL
);
377 const size_t nSize
= pEnv
->GetStringLength(data
);
379 LibreOfficeKitDocumentClass
* pcls
= pDocument
->pClass
;
380 bool result
= pcls
->paste(pDocument
, pMimeType
, pData
, nSize
);
381 pEnv
->ReleaseStringUTFChars(mimeType
, pMimeType
);
382 pEnv
->ReleaseStringUTFChars(data
, pData
);
387 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_setGraphicSelection
388 (JNIEnv
* pEnv
, jobject aObject
, jint type
, jint x
, jint y
)
390 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
391 pDocument
->pClass
->setGraphicSelection(pDocument
, type
, x
, y
);
394 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_resetSelection
395 (JNIEnv
* pEnv
, jobject aObject
)
397 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
398 pDocument
->pClass
->resetSelection(pDocument
);
401 extern "C" SAL_JNI_EXPORT jstring JNICALL Java_org_libreoffice_kit_Document_getCommandValues
402 (JNIEnv
* pEnv
, jobject aObject
, jstring command
)
404 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
406 const char* pCommand
= pEnv
->GetStringUTFChars(command
, NULL
);
408 char* pValue
= pDocument
->pClass
->getCommandValues(pDocument
, pCommand
);
410 pEnv
->ReleaseStringUTFChars(command
, pCommand
);
412 return pEnv
->NewStringUTF(pValue
);
415 extern "C" SAL_JNI_EXPORT
void JNICALL Java_org_libreoffice_kit_Document_setClientZoom
416 (JNIEnv
* pEnv
, jobject aObject
, jint nTilePixelWidth
, jint nTilePixelHeight
, jint nTileTwipWidth
, jint nTileTwipHeight
)
418 LibreOfficeKitDocument
* pDocument
= getHandle
<LibreOfficeKitDocument
>(pEnv
, aObject
);
419 pDocument
->pClass
->setClientZoom(pDocument
, nTilePixelWidth
, nTilePixelHeight
, nTileTwipWidth
, nTileTwipHeight
);
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */