Showing the changes to gui-android/cpp/android.c (1 of 27 files). Show all
Unified · Split
| @@ -0,0 +1,230 @@ | ||
| 1 | +#include <jni.h> | |
| 2 | +#include <android/native_window.h> | |
| 3 | +#include <android/log.h> | |
| 4 | + | |
| 5 | +#include "u.h" | |
| 6 | +#include "lib.h" | |
| 7 | +#include "dat.h" | |
| 8 | +#include "fns.h" | |
| 9 | + | |
| 10 | +#include <draw.h> | |
| 11 | +#include <memdraw.h> | |
| 12 | +#include <keyboard.h> | |
| 13 | +#include <cursor.h> | |
| 14 | +#include "screen.h" | |
| 15 | + | |
| 16 | +Memimage *gscreen = nil; | |
| 17 | +extern int screenWidth; | |
| 18 | +extern int screenHeight; | |
| 19 | +extern ANativeWindow *window; | |
| 20 | +extern jobject mainActivityObj; | |
| 21 | +extern JavaVM *jvm; | |
| 22 | + | |
| 23 | +char* | |
| 24 | +clipread(void) | |
| 25 | +{ | |
| 26 | + char *ret; | |
| 27 | + const char *s; | |
| 28 | + JNIEnv *env; | |
| 29 | + jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL); | |
| 30 | + if (rs != JNI_OK) { | |
| 31 | + __android_log_print(ANDROID_LOG_WARN, "drawterm", "AttachCurrentThread returned error: %d", rs); | |
| 32 | + return strdup(""); | |
| 33 | + } | |
| 34 | + jclass clazz = (*env)->GetObjectClass(env, mainActivityObj); | |
| 35 | + jmethodID methodID = (*env)->GetMethodID(env, clazz, "getClipBoard", "()Ljava/lang/String;"); | |
| 36 | + jstring str = (jstring)(*env)->CallObjectMethod(env, mainActivityObj, methodID); | |
| 37 | + s = (*env)->GetStringUTFChars(env, str, NULL); | |
| 38 | + ret = strdup(s); | |
| 39 | + (*env)->ReleaseStringUTFChars(env, str, s); | |
| 40 | + (*jvm)->DetachCurrentThread(jvm); | |
| 41 | + return ret; | |
| 42 | +} | |
| 43 | + | |
| 44 | +int | |
| 45 | +clipwrite(char *buf) | |
| 46 | +{ | |
| 47 | + JNIEnv *env; | |
| 48 | + jint rs = (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6); | |
| 49 | + if(rs != JNI_OK) { | |
| 50 | + __android_log_print(ANDROID_LOG_WARN, "drawterm", "GetEnv returned error: %d", rs); | |
| 51 | + return 0; | |
| 52 | + } | |
| 53 | + jclass clazz = (*env)->GetObjectClass(env, mainActivityObj); | |
| 54 | + jmethodID methodID = (*env)->GetMethodID(env, clazz, "setClipBoard", "(Ljava/lang/String;)V"); | |
| 55 | + jstring str = (*env)->NewStringUTF(env, buf); | |
| 56 | + (*env)->CallVoidMethod(env, mainActivityObj, methodID, str); | |
| 57 | + return 0; | |
| 58 | +} | |
| 59 | + | |
| 60 | +void | |
| 61 | +show_notification(char *buf) | |
| 62 | +{ | |
| 63 | + JNIEnv *env; | |
| 64 | + jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL); | |
| 65 | + if(rs != JNI_OK) { | |
| 66 | + __android_log_print(ANDROID_LOG_WARN, "drawterm", "AttachCurrentThread returned error: %d", rs); | |
| 67 | + return; | |
| 68 | + } | |
| 69 | + jclass clazz = (*env)->GetObjectClass(env, mainActivityObj); | |
| 70 | + jmethodID methodID = (*env)->GetMethodID(env, clazz, "showNotification", "(Ljava/lang/String;)V"); | |
| 71 | + jstring str = (*env)->NewStringUTF(env, buf); | |
| 72 | + (*env)->CallVoidMethod(env, mainActivityObj, methodID, str); | |
| 73 | + (*jvm)->DetachCurrentThread(jvm); | |
| 74 | + return; | |
| 75 | +} | |
| 76 | + | |
| 77 | +int | |
| 78 | +num_cameras() | |
| 79 | +{ | |
| 80 | + JNIEnv *env; | |
| 81 | + int n; | |
| 82 | + jint rs = (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6); | |
| 83 | + if(rs != JNI_OK) { | |
| 84 | + __android_log_print(ANDROID_LOG_WARN, "drawterm", "GetEnv returned error: %d", rs); | |
| 85 | + return 0; | |
| 86 | + } | |
| 87 | + jclass clazz = (*env)->GetObjectClass(env, mainActivityObj); | |
| 88 | + jmethodID methodID = (*env)->GetMethodID(env, clazz, "numCameras", "()I"); | |
| 89 | + n = (*env)->CallIntMethod(env, mainActivityObj, methodID); | |
| 90 | + return n; | |
| 91 | +} | |
| 92 | + | |
| 93 | +void | |
| 94 | +take_picture(int id) | |
| 95 | +{ | |
| 96 | + JNIEnv *env; | |
| 97 | + jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL); | |
| 98 | + if(rs != JNI_OK) { | |
| 99 | + __android_log_print(ANDROID_LOG_WARN, "drawterm", "AttachCurrentThread returned error: %d", rs); | |
| 100 | + return; | |
| 101 | + } | |
| 102 | + jclass clazz = (*env)->GetObjectClass(env, mainActivityObj); | |
| 103 | + jmethodID methodID = (*env)->GetMethodID(env, clazz, "takePicture", "(I)V"); | |
| 104 | + (*env)->CallVoidMethod(env, mainActivityObj, methodID, id); | |
| 105 | + (*jvm)->DetachCurrentThread(jvm); | |
| 106 | + return; | |
| 107 | +} | |
| 108 | + | |
| 109 | +void | |
| 110 | +setcolor(ulong i, ulong r, ulong g, ulong b) | |
| 111 | +{ | |
| 112 | + return; | |
| 113 | +} | |
| 114 | + | |
| 115 | +void | |
| 116 | +getcolor(ulong v, ulong *r, ulong *g, ulong *b) | |
| 117 | +{ | |
| 118 | + *r = (v>>16)&0xFF; | |
| 119 | + *g = (v>>8)&0xFF; | |
| 120 | + *b = v&0xFF; | |
| 121 | +} | |
| 122 | + | |
| 123 | +void | |
| 124 | +flushmemscreen(Rectangle r) | |
| 125 | +{ | |
| 126 | + ANativeWindow_Buffer buffer; | |
| 127 | + uint8_t *pixels; | |
| 128 | + int x, y, o, b; | |
| 129 | + ARect bounds; | |
| 130 | + | |
| 131 | + if (window == NULL) | |
| 132 | + return; | |
| 133 | + | |
| 134 | + memset(&buffer, 0, sizeof(buffer)); | |
| 135 | + | |
| 136 | + bounds.left = r.min.x; | |
| 137 | + bounds.top = r.min.y; | |
| 138 | + bounds.right = r.max.x; | |
| 139 | + bounds.bottom = r.max.y; | |
| 140 | + | |
| 141 | + if (ANativeWindow_lock(window, &buffer, &bounds) != 0) { | |
| 142 | + __android_log_print(ANDROID_LOG_WARN, "drawterm", "Unable to lock window buffer"); | |
| 143 | + return; | |
| 144 | + } | |
| 145 | + | |
| 146 | + r.min.x = bounds.left; | |
| 147 | + r.min.y = bounds.top; | |
| 148 | + r.max.x = bounds.right; | |
| 149 | + r.max.y = bounds.bottom; | |
| 150 | + | |
| 151 | + pixels = (uint8_t*)buffer.bits; | |
| 152 | + for (y = r.min.y; y < r.max.y; y++) | |
| 153 | + for (x = r.min.x; x < r.max.x; x++) { | |
| 154 | + o = (y * screenWidth + x) * 4; | |
| 155 | + b = (y * buffer.stride + x) * 4; | |
| 156 | + pixels[b+3] = 0xFF; | |
| 157 | + pixels[b+2] = gscreen->data->bdata[o+0]; | |
| 158 | + pixels[b+1] = gscreen->data->bdata[o+1]; | |
| 159 | + pixels[b+0] = gscreen->data->bdata[o+2]; | |
| 160 | + } | |
| 161 | + | |
| 162 | + if (ANativeWindow_unlockAndPost(window) != 0) { | |
| 163 | + __android_log_print(ANDROID_LOG_WARN, "drawterm", "Unable to unlock and post window buffer"); | |
| 164 | + } | |
| 165 | + return; | |
| 166 | +} | |
| 167 | + | |
| 168 | +void | |
| 169 | +screeninit(void) | |
| 170 | +{ | |
| 171 | + Rectangle r = Rect(0,0,screenWidth,screenHeight); | |
| 172 | + memimageinit(); | |
| 173 | + screensize(r, XRGB32); | |
| 174 | + if (gscreen == nil) | |
| 175 | + panic("screensize failed"); | |
| 176 | + gscreen->clipr = r; | |
| 177 | + terminit(); | |
| 178 | + qlock(&drawlock); | |
| 179 | + flushmemscreen(r); | |
| 180 | + qunlock(&drawlock); | |
| 181 | + return; | |
| 182 | +} | |
| 183 | + | |
| 184 | +void | |
| 185 | +screensize(Rectangle r, ulong chan) | |
| 186 | +{ | |
| 187 | + Memimage *mi; | |
| 188 | + | |
| 189 | + mi = allocmemimage(r, chan); | |
| 190 | + if (mi == nil) | |
| 191 | + return; | |
| 192 | + | |
| 193 | + if (gscreen != nil) | |
| 194 | + freememimage(gscreen); | |
| 195 | + | |
| 196 | + gscreen = mi; | |
| 197 | + gscreen->clipr = ZR; | |
| 198 | +} | |
| 199 | + | |
| 200 | +Memdata* | |
| 201 | +attachscreen(Rectangle *r, ulong *chan, int *depth, int *width, int *softscreen) | |
| 202 | +{ | |
| 203 | + *r = gscreen->clipr; | |
| 204 | + *depth = gscreen->depth; | |
| 205 | + *chan = gscreen->chan; | |
| 206 | + *width = gscreen->width; | |
| 207 | + *softscreen = 1; | |
| 208 | + | |
| 209 | + gscreen->data->ref++; | |
| 210 | + return gscreen->data; | |
| 211 | +} | |
| 212 | + | |
| 213 | +void | |
| 214 | +setcursor(void) | |
| 215 | +{ | |
| 216 | + return; | |
| 217 | +} | |
| 218 | + | |
| 219 | +void | |
| 220 | +mouseset(Point xy) | |
| 221 | +{ | |
| 222 | + return; | |
| 223 | +} | |
| 224 | + | |
| 225 | +void | |
| 226 | +guimain(void) | |
| 227 | +{ | |
| 228 | + cpubody(); | |
| 229 | +} | |
| 230 | + | |