tdf#157019 PPTX import: Workaround for object placeholder shape preset
[libreoffice.git] / sal / android / libreofficekit-jni.c
blob08827df54f073d5fff0a747f2b0736a4268a9cf9
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <time.h>
20 #include <unistd.h>
22 #include <jni.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")))
51 void
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)
71 struct stat st;
72 int fd;
73 const char *dataDirPath;
74 const char *cacheDirPath;
75 const char *apkFilePath;
77 size_t data_dir_len;
78 const char fontsConf[] = "/etc/fonts/fonts.conf";
79 char *fontsConfPath;
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);
102 if (fd != -1) {
103 close(fd);
104 LOGI("Setting FONTCONFIG_FILE to %s", fontsConfPath);
105 setenv("FONTCONFIG_FILE", fontsConfPath, 1);
106 // DEBUG:
107 //setenv("FC_DEBUG", "8191", 1); // log everything
108 //Java_org_libreoffice_android_Bootstrap_redirect_1stdio(NULL, NULL, JNI_TRUE);
110 free(fontsConfPath);
112 apkFilePath = (*env)->GetStringUTFChars(env, apkFile, NULL);
114 fd = open(apkFilePath, O_RDONLY);
115 if (fd == -1) {
116 LOGE("Could not open %s", apkFilePath);
117 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
118 return JNI_FALSE;
120 if (fstat(fd, &st) == -1) {
121 LOGE("Could not fstat %s", apkFilePath);
122 close(fd);
123 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
124 return JNI_FALSE;
126 apk_file = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
127 close(fd);
129 if (apk_file == MAP_FAILED) {
130 LOGE("Could not mmap %s", apkFilePath);
131 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
132 return JNI_FALSE;
134 apk_file_size = st.st_size;
136 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
138 if (!setup_cdir())
140 LOGE("setup_cdir failed");
141 return JNI_FALSE;
144 if (!setup_assets_tree())
146 LOGE("setup_assets_tree failed");
147 return JNI_FALSE;
150 LOGI("LibreOfficeKit: libreofficekit_initialize finished");
152 return JNI_TRUE;
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";
162 size_t data_dir_len;
164 (void) clazz;
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");
180 return JNI_FALSE;
183 LOGI("LibreOfficeKit successfully initialized");
185 return JNI_TRUE;
188 __attribute__ ((visibility("default")))
189 jobject Java_org_libreoffice_kit_LibreOfficeKit_getLibreOfficeKitHandle
190 (JNIEnv* env, jobject clazz)
192 LibreOfficeKit* aOffice;
194 (void) env;
195 (void) clazz;
197 aOffice = libreofficekit_hook(full_program_dir);
199 return (*env)->NewDirectByteBuffer(env, (void*) aOffice, sizeof(LibreOfficeKit));
202 __attribute__ ((visibility("default")))
203 AAssetManager *
204 lo_get_native_assetmgr(void)
206 return native_asset_manager;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */