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/.
18 #include <sys/types.h>
24 #include <android/log.h>
25 #include <android/asset_manager.h>
26 #include <android/asset_manager_jni.h>
28 #include <osl/detail/android-bootstrap.h>
30 #include <LibreOfficeKit/LibreOfficeKit.h>
32 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "LibreOfficeKit", __VA_ARGS__))
33 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "LibreOfficeKit", __VA_ARGS__))
35 /* These are valid / used in all apps. */
36 extern const char* data_dir
;
37 extern const char* cache_dir
;
38 extern void* apk_file
;
39 extern int apk_file_size
;
40 AAssetManager
* native_asset_manager
;
42 extern void Java_org_libreoffice_android_Bootstrap_putenv(JNIEnv
* env
, jobject clazz
, jstring string
);
43 extern void Java_org_libreoffice_android_Bootstrap_redirect_1stdio(JNIEnv
* env
, jobject clazz
, jboolean state
);
45 extern LibreOfficeKit
* libreofficekit_hook(const char* install_path
);
47 static char *full_program_dir
= NULL
;
49 /// Call the same method from Bootstrap.
50 __attribute__ ((visibility("default")))
52 Java_org_libreoffice_kit_LibreOfficeKit_putenv
53 (JNIEnv
* env
, jobject clazz
, jstring string
)
55 Java_org_libreoffice_android_Bootstrap_putenv(env
, clazz
, string
);
58 /// Call the same method from Bootstrap.
59 __attribute__ ((visibility("default")))
60 void Java_org_libreoffice_kit_LibreOfficeKit_redirectStdio
61 (JNIEnv
* env
, jobject clazz
, jboolean state
)
63 Java_org_libreoffice_android_Bootstrap_redirect_1stdio(env
, clazz
, state
);
66 /// Initialize the LibreOfficeKit.
67 __attribute__ ((visibility("default")))
68 jboolean
libreofficekit_initialize(JNIEnv
* env
,
69 jstring dataDir
, jstring cacheDir
, jstring apkFile
, jobject assetManager
)
73 const char *dataDirPath
;
74 const char *cacheDirPath
;
75 const char *apkFilePath
;
78 const char fontsConf
[] = "/etc/fonts/fonts.conf";
81 setenv("OOO_DISABLE_RECOVERY", "1", 1);
83 native_asset_manager
= AAssetManager_fromJava(env
, assetManager
);
85 dataDirPath
= (*env
)->GetStringUTFChars(env
, dataDir
, NULL
);
86 data_dir
= strdup(dataDirPath
);
87 (*env
)->ReleaseStringUTFChars(env
, dataDir
, dataDirPath
);
89 cacheDirPath
= (*env
)->GetStringUTFChars(env
, cacheDir
, NULL
);
90 cache_dir
= strdup(cacheDirPath
);
91 (*env
)->ReleaseStringUTFChars(env
, cacheDir
, cacheDirPath
);
93 // TMPDIR is used by osl_getTempDirURL()
94 setenv("TMPDIR", cache_dir
, 1);
96 data_dir_len
= strlen(data_dir
);
97 fontsConfPath
= malloc(data_dir_len
+ sizeof(fontsConf
));
98 strncpy(fontsConfPath
, data_dir
, data_dir_len
);
99 strncpy(fontsConfPath
+ data_dir_len
, fontsConf
, sizeof(fontsConf
));
101 fd
= open(fontsConfPath
, O_RDONLY
);
104 LOGI("Setting FONTCONFIG_FILE to %s", fontsConfPath
);
105 setenv("FONTCONFIG_FILE", fontsConfPath
, 1);
107 //setenv("FC_DEBUG", "8191", 1); // log everything
108 //Java_org_libreoffice_android_Bootstrap_redirect_1stdio(NULL, NULL, JNI_TRUE);
112 apkFilePath
= (*env
)->GetStringUTFChars(env
, apkFile
, NULL
);
114 fd
= open(apkFilePath
, O_RDONLY
);
116 LOGE("Could not open %s", apkFilePath
);
117 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
120 if (fstat(fd
, &st
) == -1) {
121 LOGE("Could not fstat %s", apkFilePath
);
123 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
126 apk_file
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
129 if (apk_file
== MAP_FAILED
) {
130 LOGE("Could not mmap %s", apkFilePath
);
131 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
134 apk_file_size
= st
.st_size
;
136 (*env
)->ReleaseStringUTFChars(env
, apkFile
, apkFilePath
);
140 LOGE("setup_cdir failed");
144 if (!setup_assets_tree())
146 LOGE("setup_assets_tree failed");
150 LOGI("LibreOfficeKit: libreofficekit_initialize finished");
155 /// Initialize the LibreOfficeKit.
156 __attribute__ ((visibility("default")))
157 jboolean Java_org_libreoffice_kit_LibreOfficeKit_initializeNative
158 (JNIEnv
* env
, jobject clazz
,
159 jstring dataDir
, jstring cacheDir
, jstring apkFile
, jobject assetManager
)
161 const char program_dir
[] = "/program";
166 libreofficekit_initialize(env
, dataDir
, cacheDir
, apkFile
, assetManager
);
168 // LibreOfficeKit expects a path to the program/ directory
169 free(full_program_dir
);
170 data_dir_len
= strlen(data_dir
);
171 full_program_dir
= malloc(data_dir_len
+ sizeof(program_dir
));
173 strncpy(full_program_dir
, data_dir
, data_dir_len
);
174 strncpy(full_program_dir
+ data_dir_len
, program_dir
, sizeof(program_dir
));
176 // Initialize LibreOfficeKit
177 if (!libreofficekit_hook(full_program_dir
))
179 LOGE("libreofficekit_hook returned null");
183 LOGI("LibreOfficeKit successfully initialized");
188 __attribute__ ((visibility("default")))
189 jobject Java_org_libreoffice_kit_LibreOfficeKit_getLibreOfficeKitHandle
190 (JNIEnv
* env
, jobject clazz
)
192 LibreOfficeKit
* aOffice
;
197 aOffice
= libreofficekit_hook(full_program_dir
);
199 return (*env
)->NewDirectByteBuffer(env
, (void*) aOffice
, sizeof(LibreOfficeKit
));
202 __attribute__ ((visibility("default")))
204 lo_get_native_assetmgr(void)
206 return native_asset_manager
;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */