]> sigrok.org Git - sigrok-util.git/blob - cross-compile/android/pulseview-cpp.patch
sigrok-cross-android: Restrict Python assets to .py files
[sigrok-util.git] / cross-compile / android / pulseview-cpp.patch
1 diff --git a/CMakeLists.txt b/CMakeLists.txt
2 index 3d63ecd..c386a60 100644
3 --- a/CMakeLists.txt
4 +++ b/CMakeLists.txt
5 @@ -333,12 +333,25 @@ if(WIN32)
6         list(APPEND PULSEVIEW_LINK_LIBS "-lqsvg")
7  endif()
8  
9 +if(ANDROID)
10 +       list(APPEND PULSEVIEW_LINK_LIBS "-llog")
11 +endif()
12 +
13 +if(ANDROID)
14 +add_library(${PROJECT_NAME} SHARED
15 +       ${pulseview_SOURCES}
16 +       ${pulseview_HEADERS_MOC}
17 +       ${pulseview_FORMS_HEADERS}
18 +       ${pulseview_RESOURCES_RCC}
19 +)
20 +else()
21  add_executable(${PROJECT_NAME}
22         ${pulseview_SOURCES}
23         ${pulseview_HEADERS_MOC}
24         ${pulseview_FORMS_HEADERS}
25         ${pulseview_RESOURCES_RCC}
26  )
27 +endif()
28  
29  target_link_libraries(${PROJECT_NAME} ${PULSEVIEW_LINK_LIBS})
30  
31 diff --git a/main.cpp b/main.cpp
32 index 23f3f62..ea5e5e1 100644
33 --- a/main.cpp
34 +++ b/main.cpp
35 @@ -20,6 +20,13 @@
36  
37  #ifdef ENABLE_DECODE
38  #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
39 +#ifdef ANDROID
40 +#include <jni.h>
41 +#endif
42 +#endif
43 +
44 +#ifdef ANDROID
45 +#include <android/log.h>
46  #endif
47  
48  #include <stdint.h>
49 @@ -45,6 +52,112 @@
50  Q_IMPORT_PLUGIN(qsvg)
51  #endif
52  
53 +#if defined(ENABLE_DECODE) && defined(ANDROID)
54 +jint JNI_OnLoad(JavaVM *vm, void *reserved)
55 +{
56 +       JNIEnv* env;
57 +
58 +       (void)reserved;
59 +
60 +       if (vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
61 +               return -1;
62 +       }
63 +
64 +       jclass envc = env->FindClass("org/sigrok/androidutils/Environment");
65 +       jmethodID getEnv =  env->GetStaticMethodID(envc, "getEnvironment",
66 +                                                  "()[Ljava/lang/String;");
67 +       jobjectArray envs =
68 +               (jobjectArray)env->CallStaticObjectMethod(envc, getEnv);
69 +       jsize i, envn = env->GetArrayLength(envs);
70 +       for (i=0; i<envn; i+=2) {
71 +               jstring key = (jstring)env->GetObjectArrayElement(envs, i);
72 +               jstring value = (jstring)env->GetObjectArrayElement(envs, i+1);
73 +               const char *utfkey = env->GetStringUTFChars(key, 0);
74 +               const char *utfvalue = env->GetStringUTFChars(value, 0);
75 +               setenv(utfkey, utfvalue, 1);
76 +               env->ReleaseStringUTFChars(value, utfvalue);
77 +               env->ReleaseStringUTFChars(key, utfkey);
78 +               env->DeleteLocalRef(value);
79 +               env->DeleteLocalRef(key);
80 +       }
81 +       env->DeleteLocalRef(envs);
82 +       env->DeleteLocalRef(envc);
83 +
84 +       return JNI_VERSION_1_6;
85 +}
86 +
87 +void JNI_OnUnload(JavaVM *vm, void *reserved)
88 +{
89 +       (void)vm;
90 +       (void)reserved;
91 +}
92 +#endif
93 +
94 +#ifdef ANDROID
95 +extern "C" {
96 +static int sr_log_callback_android(void *cb_data, int loglevel, const char *format, va_list args)
97 +{
98 +       static const int prio[] = {
99 +               [SR_LOG_NONE] = ANDROID_LOG_SILENT,
100 +               [SR_LOG_ERR] = ANDROID_LOG_ERROR,
101 +               [SR_LOG_WARN] = ANDROID_LOG_WARN,
102 +               [SR_LOG_INFO] = ANDROID_LOG_INFO,
103 +               [SR_LOG_DBG] = ANDROID_LOG_DEBUG,
104 +               [SR_LOG_SPEW] = ANDROID_LOG_VERBOSE,
105 +       };
106 +       int ret;
107 +
108 +       /* This specific log callback doesn't need the void pointer data. */
109 +       (void)cb_data;
110 +
111 +       /* Only output messages of at least the selected loglevel(s). */
112 +       if (loglevel > sr_log_loglevel_get())
113 +               return SR_OK; /* TODO? */
114 +
115 +       if (loglevel < SR_LOG_NONE)
116 +         loglevel = SR_LOG_NONE;
117 +       else if (loglevel > SR_LOG_SPEW)
118 +         loglevel = SR_LOG_SPEW;
119 +
120 +       ret = __android_log_vprint(prio[loglevel], "sr", format, args);
121 +
122 +       return ret;
123 +}
124 +
125 +#ifdef ENABLE_DECODE
126 +static int srd_log_callback_android(void *cb_data, int loglevel, const char *format,
127 +                                   va_list args)
128 +{
129 +       static const int prio[] = {
130 +               [SRD_LOG_NONE] = ANDROID_LOG_SILENT,
131 +               [SRD_LOG_ERR] = ANDROID_LOG_ERROR,
132 +               [SRD_LOG_WARN] = ANDROID_LOG_WARN,
133 +               [SRD_LOG_INFO] = ANDROID_LOG_INFO,
134 +               [SRD_LOG_DBG] = ANDROID_LOG_DEBUG,
135 +               [SRD_LOG_SPEW] = ANDROID_LOG_VERBOSE,
136 +       };
137 +       int ret;
138 +
139 +       /* This specific log callback doesn't need the void pointer data. */
140 +       (void)cb_data;
141 +
142 +       /* Only output messages of at least the selected loglevel(s). */
143 +       if (loglevel > srd_log_loglevel_get())
144 +               return SRD_OK; /* TODO? */
145 +
146 +       if (loglevel < SRD_LOG_NONE)
147 +         loglevel = SRD_LOG_NONE;
148 +       else if (loglevel > SRD_LOG_SPEW)
149 +         loglevel = SRD_LOG_SPEW;
150 +
151 +       ret = __android_log_vprint(prio[loglevel], "srd", format, args);
152 +
153 +       return ret;
154 +}
155 +#endif
156 +}
157 +#endif
158 +
159  void usage()
160  {
161         fprintf(stdout,
162 @@ -71,6 +184,13 @@ int main(int argc, char *argv[])
163         QApplication::setApplicationName("PulseView");
164         QApplication::setOrganizationDomain("sigrok.org");
165  
166 +#ifdef ANDROID
167 +       sr_log_callback_set(sr_log_callback_android, NULL);
168 +#ifdef ENABLE_DECODE
169 +       srd_log_callback_set(srd_log_callback_android, NULL);
170 +#endif
171 +#endif
172 +
173         // Parse arguments
174         while (1) {
175                 static const struct option long_options[] = {