1 /* xscreensaver, Copyright (c) 2016-2018 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * This file is three related things:
13 * - It is the Android-specific C companion to jwxyz-gl.c;
14 * - It is how C calls into Java to do things that OpenGL does not
15 * have access to without Java-based APIs;
16 * - It is how the jwxyz.java class calls into C to run the hacks.
19 #ifdef HAVE_ANDROID /* whole file */
29 #include <GLES/glext.h>
31 #include <android/bitmap.h>
32 #include <android/log.h>
33 #include <android/native_window_jni.h>
36 #include "screenhackI.h"
39 #include "jwxyz-android.h"
40 #include "textclient.h"
41 #include "grabscreen.h"
45 #define countof(x) (sizeof(x)/sizeof(*(x)))
47 extern struct xscreensaver_function_table *xscreensaver_function_table;
49 struct function_table_entry {
51 struct xscreensaver_function_table *xsft;
54 #include "gen/function-table.h"
58 struct event_queue *next;
61 static void send_queued_events (struct running_hack *rh);
64 const char *progclass;
67 static JavaVM *global_jvm;
68 static jmp_buf jmp_target;
70 static double current_rotation = 0;
72 extern void check_gl_error (const char *type);
75 jwxyz_logv(Bool error, const char *fmt, va_list args)
77 __android_log_vprint(error ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO,
78 "xscreensaver", fmt, args);
80 /* The idea here is that if the device/emulator dies shortly after a log
81 message, then waiting here for a short while should increase the odds
82 that adb logcat can pick up the message before everything blows up. Of
83 course, doing this means dumping a ton of messages will slow things down
89 ts.tv_nsec = 25 * 1000000;
94 /* Handle an abort on Android
95 TODO: Test that Android handles aborts properly
98 jwxyz_abort (const char *fmt, ...)
100 /* Send error to Android device log */
105 va_start (args, fmt);
106 jwxyz_logv(True, fmt, args);
110 va_start (args, fmt);
111 vsprintf (buf, fmt, args);
115 (*global_jvm)->AttachCurrentThread (global_jvm, &env, NULL);
117 if (! (*env)->ExceptionOccurred(env)) {
118 // If there's already an exception queued, let's just go with that one.
119 // Else, queue a Java exception to be thrown.
120 (*env)->ThrowNew (env, (*env)->FindClass(env, "java/lang/RuntimeException"),
124 // Nonlocal exit out of the jwxyz code.
125 longjmp (jmp_target, 1);
129 /* We get to keep live references to Java classes in use because the VM can
130 unload a class that isn't being used, which invalidates field and method
132 https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp17074
136 // #### only need one var I think
137 static size_t classRefCount = 0;
138 static jobject globalRefjwxyz, globalRefIterable, globalRefIterator,
141 static jfieldID runningHackField;
142 static jmethodID iterableIterator, iteratorHasNext, iteratorNext;
143 static jmethodID entryGetKey, entryGetValue;
145 static pthread_mutex_t mutg = PTHREAD_MUTEX_INITIALIZER;
147 static void screenhack_do_fps (Display *, Window, fps_state *, void *);
148 static char *get_string_resource_window (Window window, char *name);
151 /* Also creates double-buffered windows. */
153 create_pixmap (Window win, Drawable p)
156 // https://web.archive.org/web/20140213220709/http://blog.vlad1.com/2010/07/01/how-to-go-mad-while-trying-to-render-to-a-texture/
157 // https://software.intel.com/en-us/articles/using-opengl-es-to-accelerate-apps-with-legacy-2d-guis
158 // https://www.khronos.org/registry/egl/extensions/ANDROID/EGL_ANDROID_image_native_buffer.txt
160 Assert (p->frame.width, "p->frame.width");
161 Assert (p->frame.height, "p->frame.height");
163 if (win->window.rh->jwxyz_gl_p) {
164 struct running_hack *rh = win->window.rh;
167 glGenTextures (1, &p->texture);
168 glBindTexture (GL_TEXTURE_2D, p->texture);
170 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA,
171 to_pow2(p->frame.width), to_pow2(p->frame.height),
172 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
174 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
178 attribs[0] = EGL_WIDTH;
179 attribs[1] = p->frame.width;
180 attribs[2] = EGL_HEIGHT;
181 attribs[3] = p->frame.height;
182 attribs[4] = EGL_NONE;
184 p->egl_surface = eglCreatePbufferSurface(rh->egl_display, rh->egl_config,
186 Assert (p->egl_surface != EGL_NO_SURFACE,
187 "XCreatePixmap: got EGL_NO_SURFACE");
190 p->image_data = malloc (p->frame.width * p->frame.height * 4);
196 free_pixmap (struct running_hack *rh, Pixmap p)
198 if (rh->jwxyz_gl_p) {
200 glDeleteTextures (1, &p->texture);
202 eglDestroySurface(rh->egl_display, p->egl_surface);
205 free (p->image_data);
211 prepare_context (struct running_hack *rh)
214 /* TODO: Adreno recommends against doing this every frame. */
215 Assert (eglMakeCurrent(rh->egl_display, rh->egl_surface, rh->egl_surface,
217 "eglMakeCurrent failed");
220 /* Don't set matrices here; set them when an Xlib call triggers
221 jwxyz_bind_drawable/jwxyz_set_matrices.
224 rh->current_drawable = NULL;
226 if (rh->xsft->visual == GL_VISUAL)
227 jwzgles_make_current (rh->gles_state);
231 // Initialized OpenGL and runs the screenhack's init function.
234 doinit (jobject jwxyz_obj, struct running_hack *rh, JNIEnv *env,
235 const struct function_table_entry *chosen,
236 jobject defaults, jint w, jint h, jobject jni_surface)
238 if (setjmp (jmp_target)) goto END; // Jump here from jwxyz_abort and return.
240 progname = chosen->progname;
241 rh->xsft = chosen->xsft;
243 rh->jobject = jwxyz_obj; // update this every time we call into C
245 (*env)->GetJavaVM (env, &global_jvm);
247 # undef ya_rand_init // This is the one and only place it is allowed
250 Window wnd = (Window) calloc(1, sizeof(*wnd));
252 wnd->frame.width = w;
253 wnd->frame.height = h;
257 progclass = rh->xsft->progclass;
259 if ((*env)->ExceptionOccurred(env)) abort();
261 // This has to come before resource processing. It does not do graphics.
262 if (rh->xsft->setup_cb)
263 rh->xsft->setup_cb(rh->xsft, rh->xsft->setup_arg);
265 if ((*env)->ExceptionOccurred(env)) abort();
267 // Load the defaults.
268 // Unceremoniously stolen from [PrefsReader defaultsToDict:].
270 jclass c = (*env)->GetObjectClass (env, defaults);
271 jmethodID m = (*env)->GetMethodID (env, c, "put",
272 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
274 if ((*env)->ExceptionOccurred(env)) abort();
276 const struct { const char *key, *val; } default_defaults[] = {
277 { "doubleBuffer", "True" },
278 { "multiSample", "False" },
279 { "texFontCacheSize", "30" },
280 { "textMode", "date" },
282 "https://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss" },
283 { "grabDesktopImages", "True" },
284 { "chooseRandomImages", "True" },
287 for (int i = 0; i < countof(default_defaults); i++) {
288 const char *key = default_defaults[i].key;
289 const char *val = default_defaults[i].val;
290 char *key2 = malloc (strlen(progname) + strlen(key) + 2);
291 strcpy (key2, progname);
295 // defaults.put(key2, val);
296 jstring jkey = (*env)->NewStringUTF (env, key2);
297 jstring jval = (*env)->NewStringUTF (env, val);
298 (*env)->CallObjectMethod (env, defaults, m, jkey, jval);
299 (*env)->DeleteLocalRef (env, jkey);
300 (*env)->DeleteLocalRef (env, jval);
301 // Log ("default0: \"%s\" = \"%s\"", key2, val);
305 const char *const *defs = rh->xsft->defaults;
307 char *line = strdup (*defs);
310 while (*key == '.' || *key == '*' || *key == ' ' || *key == '\t')
313 while (*val && *val != ':')
315 if (*val != ':') abort();
317 while (*val == ' ' || *val == '\t')
320 unsigned long L = strlen(val);
321 while (L > 0 && (val[L-1] == ' ' || val[L-1] == '\t'))
324 char *key2 = malloc (strlen(progname) + strlen(key) + 2);
325 strcpy (key2, progname);
329 // defaults.put(key2, val);
330 jstring jkey = (*env)->NewStringUTF (env, key2);
331 jstring jval = (*env)->NewStringUTF (env, val);
332 (*env)->CallObjectMethod (env, defaults, m, jkey, jval);
333 (*env)->DeleteLocalRef (env, jkey);
334 (*env)->DeleteLocalRef (env, jval);
335 // Log ("default: \"%s\" = \"%s\"", key2, val);
341 (*env)->DeleteLocalRef (env, c);
342 if ((*env)->ExceptionOccurred(env)) abort();
345 /* Note: https://source.android.com/devices/graphics/arch-egl-opengl */
347 /* ####: This is lame, use a resource. */
349 rh->xsft->visual == DEFAULT_VISUAL &&
350 strcmp (progname, "kumppa") &&
351 strcmp (progname, "petri") &&
352 strcmp (progname, "slip") &&
353 strcmp (progname, "testx11");
355 Log ("init: %s @ %dx%d: using JWXYZ_%s", progname, w, h,
356 rh->jwxyz_gl_p ? "GL" : "IMAGE");
358 rh->egl_p = rh->jwxyz_gl_p || rh->xsft->visual == GL_VISUAL;
361 // GL init. Must come after resource processing.
363 rh->egl_display = eglGetDisplay (EGL_DEFAULT_DISPLAY);
364 Assert (rh->egl_display != EGL_NO_DISPLAY, "init: EGL_NO_DISPLAY");
366 int egl_major, egl_minor;
367 Assert (eglInitialize (rh->egl_display, &egl_major, &egl_minor),
368 "eglInitialize failed");
370 // TODO: Skip depth and (probably) alpha for Xlib.
371 // TODO: Could ask for EGL_SWAP_BEHAVIOR_PRESERVED_BIT here...maybe?
372 // TODO: Probably should try to ask for EGL_PBUFFER_BIT.
373 // TODO: Do like visual-gl.c and work from a list of configs.
374 /* Probably don't need EGL_FRAMEBUFFER_TARGET_ANDROID here if GLSurfaceView
377 EGLint config_attribs[] = {
387 Assert (eglChooseConfig (rh->egl_display, config_attribs,
388 &rh->egl_config, 1, &num_config),
389 "eglChooseConfig failed");
390 Assert (num_config == 1, "no EGL config chosen");
392 EGLint no_attribs[] = {EGL_NONE};
393 rh->egl_ctx = eglCreateContext (rh->egl_display, rh->egl_config,
394 EGL_NO_CONTEXT, no_attribs);
395 Assert (rh->egl_ctx != EGL_NO_CONTEXT, "init: EGL_NO_CONTEXT");
397 ANativeWindow *native_window =
398 ANativeWindow_fromSurface (env, jni_surface);
400 rh->egl_surface = eglCreateWindowSurface (rh->egl_display, rh->egl_config,
401 native_window, no_attribs);
402 Assert (rh->egl_surface != EGL_NO_SURFACE, "init: EGL_NO_SURFACE");
404 ANativeWindow_release (native_window);
406 rh->native_window = ANativeWindow_fromSurface (env, jni_surface);
408 int result = ANativeWindow_setBuffersGeometry (rh->native_window, w, h,
409 WINDOW_FORMAT_RGBX_8888);
411 // Maybe check this earlier?
412 Log ("can't set format (%d), surface may be invalid.", result);
413 (*env)->ThrowNew (env,
414 (*env)->FindClass(env, "org/jwz/xscreensaver/jwxyz$SurfaceLost"),
417 ANativeWindow_release (rh->native_window);
418 rh->native_window = NULL;
423 prepare_context (rh);
426 Log ("init %s / %s / %s",
427 glGetString (GL_VENDOR),
428 glGetString (GL_RENDERER),
429 glGetString (GL_VERSION));
432 if (rh->jwxyz_gl_p) {
433 const GLubyte *extensions = glGetString (GL_EXTENSIONS);
434 rh->gl_fbo_p = jwzgles_gluCheckExtension (
435 (const GLubyte *)"GL_OES_framebuffer_object", extensions);
438 glGetIntegerv (GL_FRAMEBUFFER_BINDING_OES, &rh->fb_default);
439 Assert (!rh->fb_default, "default framebuffer not current framebuffer");
440 glGenFramebuffersOES (1, &rh->fb_pixmap);
443 wnd->egl_surface = rh->egl_surface;
446 rh->frontbuffer_p = False;
448 if (rh->xsft->visual == DEFAULT_VISUAL ||
449 (rh->xsft->visual == GL_VISUAL &&
450 strcmp("True", get_string_resource_window(wnd, "doubleBuffer")))) {
452 rh->frontbuffer_p = True;
454 # if 0 /* Might need to be 0 for Adreno...? */
455 if (egl_major > 1 || (egl_major == 1 && egl_minor >= 2)) {
457 eglGetConfigAttrib(rh->egl_display, rh->egl_config, EGL_SURFACE_TYPE,
459 if(surface_type & EGL_SWAP_BEHAVIOR_PRESERVED_BIT) {
460 eglSurfaceAttrib(rh->egl_display, rh->egl_surface, EGL_SWAP_BEHAVIOR,
461 EGL_BUFFER_PRESERVED);
462 rh->frontbuffer_p = False;
467 if (rh->frontbuffer_p) {
468 /* create_pixmap needs rh->gl_fbo_p and wnd->frame. */
469 create_pixmap (wnd, wnd);
471 /* No preserving backbuffers, so manual blit from back to "front". */
472 rh->frontbuffer.type = PIXMAP;
473 rh->frontbuffer.frame = wnd->frame;
474 rh->frontbuffer.pixmap.depth = visual_depth (NULL, NULL);
477 rh->frontbuffer.texture = 0;
479 Assert (wnd->egl_surface != rh->egl_surface,
480 "oops: wnd->egl_surface == rh->egl_surface");
481 rh->frontbuffer.egl_surface = rh->egl_surface;
486 rh->dpy = jwxyz_gl_make_display(wnd);
490 if (rh->xsft->visual == DEFAULT_VISUAL)
491 create_pixmap (wnd, wnd);
493 wnd->image_data = NULL;
495 static const unsigned char rgba_bytes[] = {0, 1, 2, 3};
496 rh->dpy = jwxyz_image_make_display(wnd, rgba_bytes);
500 Assert(wnd == XRootWindow(rh->dpy, 0), "Wrong root window.");
501 // TODO: Zero looks right, but double-check that is the right number
503 /* Requires valid rh->dpy. */
505 rh->copy_gc = XCreateGC (rh->dpy, &rh->frontbuffer, 0, NULL);
507 if (rh->xsft->visual == GL_VISUAL)
508 rh->gles_state = jwzgles_make_state();
521 # ifdef GETTIMEOFDAY_TWO_ARGS
523 gettimeofday(&now, &tzp);
528 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
533 // Animates a single frame of the current hack.
536 drawXScreenSaver (JNIEnv *env, struct running_hack *rh)
539 double fps0=0, fps1=0, fps2=0, fps3=0, fps4=0;
540 fps0 = fps1 = fps2 = fps3 = fps4 = double_time();
543 unsigned long delay = 0;
545 if (setjmp (jmp_target)) goto END; // Jump here from jwxyz_abort and return.
547 Window wnd = rh->window;
549 prepare_context (rh);
552 /* There is some kind of weird redisplay race condition between Settings
553 and the launching hack: e.g., Abstractile does XClearWindow at init,
554 but the screen is getting filled with random bits. So let's wait a
555 few frames before really starting up.
557 TODO: Is this still true?
559 if (++rh->frame_count < 8) {
560 /* glClearColor (1.0, 0.0, 1.0, 0.0); */
561 glClear (GL_COLOR_BUFFER_BIT); /* We always need to draw *something*. */
567 fps1 = double_time();
570 // The init function might do graphics (e.g. XClearWindow) so it has
571 // to be run from inside onDrawFrame, not onSurfaceChanged.
573 if (! rh->initted_p) {
575 void *(*init_cb) (Display *, Window, void *) =
576 (void *(*)(Display *, Window, void *)) rh->xsft->init_cb;
578 if (rh->xsft->visual == DEFAULT_VISUAL) {
580 get_pixel_resource (rh->dpy, 0, "background", "Background");
581 XSetWindowBackground (rh->dpy, wnd, bg);
582 XClearWindow (rh->dpy, wnd);
585 rh->closure = init_cb (rh->dpy, wnd, rh->xsft->setup_arg);
586 rh->initted_p = True;
588 /* ignore_rotation_p doesn't quite work at the moment. */
589 rh->ignore_rotation_p = False;
591 (rh->xsft->visual == DEFAULT_VISUAL &&
592 get_boolean_resource (rh->dpy, "ignoreRotation", "IgnoreRotation"));
595 if (get_boolean_resource (rh->dpy, "doFPS", "DoFPS")) {
596 rh->fpst = fps_init (rh->dpy, wnd);
597 if (! rh->xsft->fps_cb) rh->xsft->fps_cb = screenhack_do_fps;
600 rh->xsft->fps_cb = 0;
603 if ((*env)->ExceptionOccurred(env)) abort();
607 fps2 = double_time();
610 // Apparently events don't come in on the drawing thread, and JNI flips
611 // out. So we queue them there and run them here.
612 // TODO: Events should be coming in on the drawing thread now, so dump this.
613 send_queued_events (rh);
616 fps3 = double_time();
619 delay = rh->xsft->draw_cb(rh->dpy, wnd, rh->closure);
622 jwxyz_gl_flush (rh->dpy);
625 fps4 = double_time();
627 if (rh->fpst && rh->xsft->fps_cb)
628 rh->xsft->fps_cb (rh->dpy, wnd, rh->fpst, rh->closure);
631 if (rh->jwxyz_gl_p && rh->frontbuffer_p) {
632 jwxyz_gl_copy_area (rh->dpy, wnd, &rh->frontbuffer, rh->copy_gc,
633 0, 0, wnd->frame.width, wnd->frame.height,
637 // Getting failure here before/during/after resize, sometimes. Log sez:
638 // W/Adreno-EGLSUB(18428): <DequeueBuffer:607>: dequeue native buffer fail: No such device, buffer=0x5f93bf5c, handle=0x0
639 if (!eglSwapBuffers(rh->egl_display, rh->egl_surface)) {
640 Log ("eglSwapBuffers failed: 0x%x (probably asynchronous resize)",
644 ANativeWindow_Buffer buffer;
645 ARect rect = {0, 0, wnd->frame.width, wnd->frame.height};
646 int32_t result = ANativeWindow_lock(rh->native_window, &buffer, &rect);
648 Log ("ANativeWindow_lock failed (result = %d), frame dropped", result);
650 /* Android can resize surfaces asynchronously. */
651 if (wnd->frame.width != buffer.width ||
652 wnd->frame.height != buffer.height) {
653 Log ("buffer/window size mismatch: %dx%d (format = %d), wnd: %dx%d",
654 buffer.width, buffer.height, buffer.format,
655 wnd->frame.width, wnd->frame.height);
658 Assert (buffer.format == WINDOW_FORMAT_RGBA_8888 ||
659 buffer.format == WINDOW_FORMAT_RGBX_8888,
660 "bad buffer format");
662 jwxyz_blit (wnd->image_data, jwxyz_image_pitch (wnd), 0, 0,
663 buffer.bits, buffer.stride * 4, 0, 0,
664 MIN(wnd->frame.width, buffer.width),
665 MIN(wnd->frame.height, buffer.height));
666 // TODO: Clear any area to sides and bottom.
668 ANativeWindow_unlockAndPost (rh->native_window);
675 Log("## FPS prep = %-6d init = %-6d events = %-6d draw = %-6d fps = %-6d\n",
676 (int) ((fps1-fps0)*1000000),
677 (int) ((fps2-fps1)*1000000),
678 (int) ((fps3-fps2)*1000000),
679 (int) ((fps4-fps3)*1000000),
680 (int) ((double_time()-fps4)*1000000));
687 // Extracts the C structure that is stored in the jwxyz Java object.
688 static struct running_hack *
689 getRunningHack (JNIEnv *env, jobject thiz)
691 jlong result = (*env)->GetLongField (env, thiz, runningHackField);
692 struct running_hack *rh = (struct running_hack *)(intptr_t)result;
694 rh->jobject = thiz; // update this every time we call into C
698 // Look up a class and mark it global in the provided variable.
700 acquireClass (JNIEnv *env, const char *className, jobject *globalRef)
702 jclass clazz = (*env)->FindClass(env, className);
703 *globalRef = (*env)->NewGlobalRef(env, clazz);
708 /* Note: to find signature strings for native methods:
709 cd ./project/xscreensaver/build/intermediates/classes/debug/
710 javap -s -p org.jwz.xscreensaver.jwxyz
714 // Implementation of jwxyz's nativeInit Java method.
716 JNIEXPORT void JNICALL
717 Java_org_jwz_xscreensaver_jwxyz_nativeInit (JNIEnv *env, jobject thiz,
718 jstring jhack, jobject defaults,
722 pthread_mutex_lock(&mutg);
724 struct running_hack *rh = calloc(1, sizeof(struct running_hack));
726 if ((*env)->ExceptionOccurred(env)) abort();
729 if (!classRefCount) {
730 jclass classjwxyz = (*env)->GetObjectClass(env, thiz);
731 globalRefjwxyz = (*env)->NewGlobalRef(env, classjwxyz);
732 runningHackField = (*env)->GetFieldID
733 (env, classjwxyz, "nativeRunningHackPtr", "J");
734 if ((*env)->ExceptionOccurred(env)) abort();
736 jclass classIterable =
737 acquireClass(env, "java/lang/Iterable", &globalRefIterable);
738 iterableIterator = (*env)->GetMethodID
739 (env, classIterable, "iterator", "()Ljava/util/Iterator;");
740 if ((*env)->ExceptionOccurred(env)) abort();
742 jclass classIterator =
743 acquireClass(env, "java/util/Iterator", &globalRefIterator);
744 iteratorHasNext = (*env)->GetMethodID
745 (env, classIterator, "hasNext", "()Z");
746 iteratorNext = (*env)->GetMethodID
747 (env, classIterator, "next", "()Ljava/lang/Object;");
748 if ((*env)->ExceptionOccurred(env)) abort();
750 jclass classMapEntry =
751 acquireClass(env, "java/util/Map$Entry", &globalRefMapEntry);
752 entryGetKey = (*env)->GetMethodID
753 (env, classMapEntry, "getKey", "()Ljava/lang/Object;");
754 entryGetValue = (*env)->GetMethodID
755 (env, classMapEntry, "getValue", "()Ljava/lang/Object;");
756 if ((*env)->ExceptionOccurred(env)) abort();
761 // Store the C struct into the Java object.
762 (*env)->SetLongField(env, thiz, runningHackField, (jlong)(intptr_t)rh);
764 // TODO: Sort the list so binary search works.
765 const char *hack =(*env)->GetStringUTFChars(env, jhack, NULL);
769 if (chosen == countof(function_table)) {
770 Log ("Hack not found: %s", hack);
773 if (!strcmp(function_table[chosen].progname, hack))
778 (*env)->ReleaseStringUTFChars(env, jhack, hack);
780 doinit (thiz, rh, env, &function_table[chosen], defaults, w, h,
783 pthread_mutex_unlock(&mutg);
787 JNIEXPORT void JNICALL
788 Java_org_jwz_xscreensaver_jwxyz_nativeResize (JNIEnv *env, jobject thiz,
789 jint w, jint h, jdouble rot)
791 pthread_mutex_lock(&mutg);
792 if (setjmp (jmp_target)) goto END; // Jump here from jwxyz_abort and return.
794 current_rotation = rot;
796 Log ("native rotation: %f", current_rotation);
798 struct running_hack *rh = getRunningHack(env, thiz);
800 prepare_context (rh);
803 glViewport (0, 0, w, h);
805 int result = ANativeWindow_setBuffersGeometry (rh->native_window, w, h,
806 WINDOW_FORMAT_RGBX_8888);
808 Log ("failed to resize surface (%d)", result);
811 Window wnd = rh->window;
814 wnd->frame.width = w;
815 wnd->frame.height = h;
817 if (ignore_rotation_p(rh->dpy) &&
818 rot != 0 && rot != 180 && rot != -180) {
822 wnd->frame.width = w;
823 wnd->frame.height = h;
826 if (rh->jwxyz_gl_p) {
827 if (rh->frontbuffer_p) {
828 free_pixmap (rh, wnd);
829 create_pixmap (wnd, wnd);
831 rh->frontbuffer.frame = wnd->frame;
833 rh->frontbuffer.egl_surface = rh->egl_surface;
836 jwxyz_window_resized (rh->dpy);
837 } else if (rh->xsft->visual == DEFAULT_VISUAL) {
838 free_pixmap (rh, wnd);
839 create_pixmap (wnd, wnd);
840 XClearWindow (rh->dpy, wnd); // TODO: This is lame. Copy the bits.
844 rh->xsft->reshape_cb (rh->dpy, rh->window, rh->closure,
845 wnd->frame.width, wnd->frame.height);
847 if (rh->xsft->visual == GL_VISUAL) {
848 glMatrixMode (GL_PROJECTION);
849 glRotatef (-rot, 0, 0, 1);
850 glMatrixMode (GL_MODELVIEW);
854 pthread_mutex_unlock(&mutg);
858 JNIEXPORT jlong JNICALL
859 Java_org_jwz_xscreensaver_jwxyz_nativeRender (JNIEnv *env, jobject thiz)
861 pthread_mutex_lock(&mutg);
862 struct running_hack *rh = getRunningHack(env, thiz);
863 jlong result = drawXScreenSaver(env, rh);
864 pthread_mutex_unlock(&mutg);
869 // TODO: Check Java side is calling this properly
870 JNIEXPORT void JNICALL
871 Java_org_jwz_xscreensaver_jwxyz_nativeDone (JNIEnv *env, jobject thiz)
873 pthread_mutex_lock(&mutg);
874 if (setjmp (jmp_target)) goto END; // Jump here from jwxyz_abort and return.
876 struct running_hack *rh = getRunningHack(env, thiz);
878 prepare_context (rh);
881 rh->xsft->free_cb (rh->dpy, rh->window, rh->closure);
883 XFreeGC (rh->dpy, rh->copy_gc);
884 if (rh->xsft->visual == GL_VISUAL)
885 jwzgles_free_state ();
888 jwxyz_gl_free_display(rh->dpy);
890 jwxyz_image_free_display(rh->dpy);
893 // eglDestroy* probably isn't necessary here.
894 eglMakeCurrent (rh->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
896 eglDestroySurface (rh->egl_display, rh->egl_surface);
897 eglDestroyContext (rh->egl_display, rh->egl_ctx);
898 eglTerminate (rh->egl_display);
900 if (rh->xsft->visual == DEFAULT_VISUAL)
901 free_pixmap (rh, rh->window);
902 if (rh->native_window)
903 ANativeWindow_release (rh->native_window);
907 (*env)->SetLongField(env, thiz, runningHackField, 0);
910 if (!classRefCount) {
911 (*env)->DeleteGlobalRef(env, globalRefjwxyz);
912 (*env)->DeleteGlobalRef(env, globalRefIterable);
913 (*env)->DeleteGlobalRef(env, globalRefIterator);
914 (*env)->DeleteGlobalRef(env, globalRefMapEntry);
918 pthread_mutex_unlock(&mutg);
923 send_event (struct running_hack *rh, XEvent *e)
925 // Assumes mutex is locked and context is prepared
927 int *xP = 0, *yP = 0;
928 switch (e->xany.type) {
929 case ButtonPress: case ButtonRelease:
939 // Rotate the coordinates in the events to match the pixels.
941 if (ignore_rotation_p (rh->dpy)) {
942 Window win = XRootWindow (rh->dpy, 0);
943 int w = win->frame.width;
944 int h = win->frame.height;
946 switch ((int) current_rotation) {
947 case 180: case -180: // #### untested
952 swap = *xP; *xP = *yP; *yP = swap;
955 case -90: case 270: // #### untested
956 swap = *xP; *xP = *yP; *yP = swap;
962 rh->window->window.last_mouse_x = *xP;
963 rh->window->window.last_mouse_y = *yP;
966 return (rh->xsft->event_cb
967 ? rh->xsft->event_cb (rh->dpy, rh->window, rh->closure, e)
973 send_queued_events (struct running_hack *rh)
975 struct event_queue *event, *next;
976 if (! rh->event_queue) return;
977 for (event = rh->event_queue, next = event->next;
979 event = next, next = (event ? event->next : 0)) {
980 if (! send_event (rh, &event->event)) {
981 // #### flash the screen or something
990 queue_event (JNIEnv *env, jobject thiz, XEvent *e)
992 pthread_mutex_lock (&mutg);
993 struct running_hack *rh = getRunningHack (env, thiz);
994 struct event_queue *q = (struct event_queue *) malloc (sizeof(*q));
995 memcpy (&q->event, e, sizeof(*e));
998 // Put it at the end.
999 struct event_queue *oq;
1000 for (oq = rh->event_queue; oq && oq->next; oq = oq->next)
1005 rh->event_queue = q;
1007 pthread_mutex_unlock (&mutg);
1011 JNIEXPORT void JNICALL
1012 Java_org_jwz_xscreensaver_jwxyz_sendButtonEvent (JNIEnv *env, jobject thiz,
1013 int x, int y, jboolean down)
1016 memset (&e, 0, sizeof(e));
1017 e.xany.type = (down ? ButtonPress : ButtonRelease);
1018 e.xbutton.button = Button1;
1021 queue_event (env, thiz, &e);
1024 JNIEXPORT void JNICALL
1025 Java_org_jwz_xscreensaver_jwxyz_sendMotionEvent (JNIEnv *env, jobject thiz,
1029 memset (&e, 0, sizeof(e));
1030 e.xany.type = MotionNotify;
1033 queue_event (env, thiz, &e);
1036 JNIEXPORT void JNICALL
1037 Java_org_jwz_xscreensaver_jwxyz_sendKeyEvent (JNIEnv *env, jobject thiz,
1042 memset (&e, 0, sizeof(e));
1043 e.xkey.keycode = code;
1044 e.xkey.state = code;
1045 e.xany.type = (down_p ? KeyPress : KeyRelease);
1046 queue_event (env, thiz, &e);
1047 e.xany.type = KeyRelease;
1048 queue_event (env, thiz, &e);
1052 /***************************************************************************
1053 Backend functions for jwxyz-gl.c
1057 finish_bind_drawable (Display *dpy, Drawable dst)
1061 glViewport (0, 0, dst->frame.width, dst->frame.height);
1062 jwxyz_set_matrices (dpy, dst->frame.width, dst->frame.height, False);
1067 bind_drawable_fbo (struct running_hack *rh, Drawable d)
1069 glBindFramebufferOES (GL_FRAMEBUFFER_OES,
1070 d->texture ? rh->fb_pixmap : rh->fb_default);
1072 glFramebufferTexture2DOES (GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
1073 GL_TEXTURE_2D, d->texture, 0);
1079 jwxyz_bind_drawable (Display *dpy, Window w, Drawable d)
1081 struct running_hack *rh = w->window.rh;
1082 JNIEnv *env = w->window.rh->jni_env;
1083 if ((*env)->ExceptionOccurred(env)) abort();
1084 if (rh->current_drawable != d) {
1086 bind_drawable_fbo (rh, d);
1088 eglMakeCurrent (rh->egl_display, d->egl_surface, d->egl_surface, rh->egl_ctx);
1090 finish_bind_drawable (dpy, d);
1091 rh->current_drawable = d;
1096 jwxyz_gl_copy_area (Display *dpy, Drawable src, Drawable dst, GC gc,
1097 int src_x, int src_y,
1098 unsigned int width, unsigned int height,
1099 int dst_x, int dst_y)
1101 Window w = XRootWindow (dpy, 0);
1102 struct running_hack *rh = w->window.rh;
1104 jwxyz_gl_flush (dpy);
1106 if (rh->gl_fbo_p && src->texture && src != dst) {
1107 bind_drawable_fbo (rh, dst);
1108 finish_bind_drawable (dpy, dst);
1109 rh->current_drawable = NULL;
1111 jwxyz_gl_set_gc (dpy, gc);
1113 glBindTexture (GL_TEXTURE_2D, src->texture);
1115 jwxyz_gl_draw_image (dpy, gc, GL_TEXTURE_2D, to_pow2(src->frame.width),
1116 to_pow2(src->frame.height),
1117 src_x, src->frame.height - src_y - height,
1118 jwxyz_drawable_depth (src), width, height,
1119 dst_x, dst_y, False);
1125 // Hilarious display corruption ahoy! (Note to self: it's on the emulator.)
1126 // TODO for Dave: Recheck behavior on the emulator with the better Pixmap support.
1128 rh->current_drawable = NULL;
1130 bind_drawable_fbo (rh, src);
1132 eglMakeCurrent (rh->egl_display, dst->egl_surface, src->egl_surface, rh->egl_ctx);
1134 jwxyz_gl_copy_area_read_tex_image (dpy, src->frame.height, src_x, src_y,
1135 width, height, dst_x, dst_y);
1138 bind_drawable_fbo (rh, dst);
1139 finish_bind_drawable (dpy, dst);
1141 jwxyz_gl_copy_area_write_tex_image (dpy, gc, src_x, src_y, width, height,
1146 jwxyz_gl_copy_area_read_pixels (dpy, src, dst, gc, src_x, src_y,
1147 width, height, dst_x, dst_y);
1154 jwxyz_assert_drawable (Window main_window, Drawable d)
1156 check_gl_error("jwxyz_assert_drawable");
1161 jwxyz_assert_gl (void)
1163 check_gl_error("jwxyz_assert_gl");
1167 /***************************************************************************
1168 Backend functions for jwxyz-image.c
1172 jwxyz_image_pitch (Drawable d)
1174 return d->frame.width * 4;
1178 jwxyz_image_data (Drawable d)
1180 Assert (d->image_data, "no image storage (i.e. keep Xlib off the Window)");
1181 return d->image_data;
1186 jwxyz_frame (Drawable d)
1193 jwxyz_drawable_depth (Drawable d)
1195 return (d->type == WINDOW
1196 ? visual_depth (NULL, NULL)
1202 jwxyz_get_pos (Window w, XPoint *xvpos, XPoint *xp)
1208 xp->x = w->window.last_mouse_x;
1209 xp->y = w->window.last_mouse_y;
1215 screenhack_do_fps (Display *dpy, Window w, fps_state *fpst, void *closure)
1217 fps_compute (fpst, 0, -1);
1223 XCreatePixmap (Display *dpy, Drawable d,
1224 unsigned int width, unsigned int height, unsigned int depth)
1226 Window win = XRootWindow(dpy, 0);
1228 Pixmap p = malloc(sizeof(*p));
1232 p->frame.width = width;
1233 p->frame.height = height;
1235 Assert(depth == 1 || depth == visual_depth(NULL, NULL),
1236 "XCreatePixmap: bad depth");
1237 p->pixmap.depth = depth;
1239 create_pixmap (win, p);
1241 /* For debugging. */
1243 jwxyz_bind_drawable (dpy, win, p);
1244 glClearColor (frand(1), frand(1), frand(1), 0);
1245 glClear (GL_COLOR_BUFFER_BIT);
1253 XFreePixmap (Display *d, Pixmap p)
1255 struct running_hack *rh = XRootWindow(d, 0)->window.rh;
1257 if (rh->jwxyz_gl_p) {
1260 if (rh->current_drawable == p)
1261 rh->current_drawable = NULL;
1264 free_pixmap (rh, p);
1271 current_device_rotation (void)
1273 return current_rotation;
1277 ignore_rotation_p (Display *dpy)
1279 struct running_hack *rh = XRootWindow(dpy, 0)->window.rh;
1280 return rh->ignore_rotation_p;
1285 jstring_dup (JNIEnv *env, jstring str)
1287 Assert (str, "expected jstring, not null");
1288 const char *cstr = (*env)->GetStringUTFChars (env, str, 0);
1289 size_t len = (*env)->GetStringUTFLength (env, str) + 1;
1290 char *result = malloc (len);
1292 memcpy (result, cstr, len);
1294 (*env)->ReleaseStringUTFChars (env, str, cstr);
1300 get_string_resource_window (Window window, char *name)
1302 JNIEnv *env = window->window.rh->jni_env;
1303 jobject obj = window->window.rh->jobject;
1305 if ((*env)->ExceptionOccurred(env)) abort();
1306 jstring jstr = (*env)->NewStringUTF (env, name);
1307 jclass c = (*env)->GetObjectClass (env, obj);
1308 jmethodID m = (*env)->GetMethodID (env, c, "getStringResource",
1309 "(Ljava/lang/String;)Ljava/lang/String;");
1310 if ((*env)->ExceptionOccurred(env)) abort();
1313 ? (*env)->CallObjectMethod (env, obj, m, jstr)
1315 (*env)->DeleteLocalRef (env, c);
1316 (*env)->DeleteLocalRef (env, jstr);
1319 ret = jstring_dup (env, jvalue);
1321 Log("pref %s = %s", name, (ret ? ret : "(null)"));
1327 get_string_resource (Display *dpy, char *name, char *class)
1329 return get_string_resource_window (RootWindow (dpy, 0), name);
1333 /* Returns the contents of the URL. */
1335 textclient_mobile_url_string (Display *dpy, const char *url)
1337 Window window = RootWindow (dpy, 0);
1338 JNIEnv *env = window->window.rh->jni_env;
1339 jobject obj = window->window.rh->jobject;
1341 jstring jstr = (*env)->NewStringUTF (env, url);
1342 jclass c = (*env)->GetObjectClass (env, obj);
1343 jmethodID m = (*env)->GetMethodID (env, c, "loadURL",
1344 "(Ljava/lang/String;)Ljava/nio/ByteBuffer;");
1345 if ((*env)->ExceptionOccurred(env)) abort();
1347 ? (*env)->CallObjectMethod (env, obj, m, jstr)
1349 (*env)->DeleteLocalRef (env, c);
1350 (*env)->DeleteLocalRef (env, jstr);
1352 char *body = (char *) (buf ? (*env)->GetDirectBufferAddress (env, buf) : 0);
1355 int L = (*env)->GetDirectBufferCapacity (env, buf);
1356 body2 = malloc (L + 1);
1357 memcpy (body2, body, L);
1360 body2 = strdup ("ERROR");
1364 (*env)->DeleteLocalRef (env, buf);
1371 jwxyz_scale (Window main_window)
1373 // TODO: Use the actual device resolution.
1379 jwxyz_default_font_family (int require)
1381 /* Font families in XLFDs are totally ignored (for now). */
1382 return "sans-serif";
1387 jwxyz_load_native_font (Window window,
1388 int traits_jwxyz, int mask_jwxyz,
1389 const char *font_name_ptr, size_t font_name_length,
1390 int font_name_type, float size,
1391 char **family_name_ret,
1392 int *ascent_ret, int *descent_ret)
1394 JNIEnv *env = window->window.rh->jni_env;
1395 jobject obj = window->window.rh->jobject;
1397 jstring jname = NULL;
1398 if (font_name_ptr) {
1399 char *name_nul = malloc(font_name_length + 1);
1400 memcpy(name_nul, font_name_ptr, font_name_length);
1401 name_nul[font_name_length] = 0;
1402 jname = (*env)->NewStringUTF (env, name_nul);
1406 jclass c = (*env)->GetObjectClass (env, obj);
1407 jmethodID m = (*env)->GetMethodID (env, c, "loadFont",
1408 "(IILjava/lang/String;IF)[Ljava/lang/Object;");
1409 if ((*env)->ExceptionOccurred(env)) abort();
1411 jobjectArray array = (m
1412 ? (*env)->CallObjectMethod (env, obj, m, (jint)mask_jwxyz,
1413 (jint)traits_jwxyz, jname,
1414 (jint)font_name_type, (jfloat)size)
1417 (*env)->DeleteLocalRef (env, c);
1420 jobject font = (*env)->GetObjectArrayElement (env, array, 0);
1421 jobject family_name =
1422 (jstring) ((*env)->GetObjectArrayElement (env, array, 1));
1423 jobject asc = (*env)->GetObjectArrayElement (env, array, 2);
1424 jobject desc = (*env)->GetObjectArrayElement (env, array, 3);
1425 if ((*env)->ExceptionOccurred(env)) abort();
1427 if (family_name_ret)
1428 *family_name_ret = jstring_dup (env, family_name);
1430 jobject paint = (*env)->NewGlobalRef (env, font);
1431 if ((*env)->ExceptionOccurred(env)) abort();
1433 c = (*env)->GetObjectClass(env, asc);
1434 m = (*env)->GetMethodID (env, c, "floatValue", "()F");
1435 if ((*env)->ExceptionOccurred(env)) abort();
1437 *ascent_ret = (int) (*env)->CallFloatMethod (env, asc, m);
1438 *descent_ret = (int) (*env)->CallFloatMethod (env, desc, m);
1440 return (void *) paint;
1448 jwxyz_release_native_font (Display *dpy, void *native_font)
1450 Window window = RootWindow (dpy, 0);
1451 JNIEnv *env = window->window.rh->jni_env;
1452 if ((*env)->ExceptionOccurred(env)) abort();
1453 (*env)->DeleteGlobalRef (env, (jobject) native_font);
1454 if ((*env)->ExceptionOccurred(env)) abort();
1458 /* If the local reference table fills up, use this to figure out where
1459 you missed a call to DeleteLocalRef. */
1461 static void dump_reference_tables(JNIEnv *env)
1463 jclass c = (*env)->FindClass(env, "dalvik/system/VMDebug");
1464 jmethodID m = (*env)->GetStaticMethodID (env, c, "dumpReferenceTables",
1466 (*env)->CallStaticVoidMethod (env, c, m);
1467 (*env)->DeleteLocalRef (env, c);
1472 // Returns the metrics of the multi-character, single-line UTF8 or Latin1
1473 // string. If pixmap_ret is provided, also renders the text.
1476 jwxyz_render_text (Display *dpy, void *native_font,
1477 const char *str, size_t len, Bool utf8, Bool antialias_p,
1478 XCharStruct *cs, char **pixmap_ret)
1480 Window window = RootWindow (dpy, 0);
1481 JNIEnv *env = window->window.rh->jni_env;
1482 jobject obj = window->window.rh->jobject;
1487 s2 = malloc (len + 1);
1488 memcpy (s2, str, len);
1490 } else { // Convert Latin1 to UTF8
1491 s2 = malloc (len * 2 + 1);
1492 unsigned char *s3 = (unsigned char *) s2;
1494 for (i = 0; i < len; i++) {
1495 unsigned char c = ((unsigned char *) str)[i];
1499 *s3++ = (0xC0 | (0x03 & (c >> 6)));
1500 *s3++ = (0x80 | (0x3F & c));
1506 jstring jstr = (*env)->NewStringUTF (env, s2);
1507 jclass c = (*env)->GetObjectClass (env, obj);
1508 jmethodID m = (*env)->GetMethodID (env, c, "renderText",
1509 "(Landroid/graphics/Paint;Ljava/lang/String;ZZ)Ljava/nio/ByteBuffer;");
1510 if ((*env)->ExceptionOccurred(env)) abort();
1513 ? (*env)->CallObjectMethod (env, obj, m,
1514 (jobject) native_font,
1516 (pixmap_ret ? JNI_TRUE : JNI_FALSE),
1519 (*env)->DeleteLocalRef (env, c);
1520 (*env)->DeleteLocalRef (env, jstr);
1523 if ((*env)->ExceptionOccurred(env)) abort();
1524 unsigned char *bits = (unsigned char *)
1525 (buf ? (*env)->GetDirectBufferAddress (env, buf) : 0);
1528 int L = (*env)->GetDirectBufferCapacity (env, buf);
1529 if (L < 10) abort();
1530 cs->lbearing = (bits[i] << 8) | (bits[i+1] & 0xFF); i += 2;
1531 cs->rbearing = (bits[i] << 8) | (bits[i+1] & 0xFF); i += 2;
1532 cs->width = (bits[i] << 8) | (bits[i+1] & 0xFF); i += 2;
1533 cs->ascent = (bits[i] << 8) | (bits[i+1] & 0xFF); i += 2;
1534 cs->descent = (bits[i] << 8) | (bits[i+1] & 0xFF); i += 2;
1537 char *pix = malloc (L - i);
1539 memcpy (pix, bits + i, L - i);
1543 memset (cs, 0, sizeof(*cs));
1549 (*env)->DeleteLocalRef (env, buf);
1554 jwxyz_unicode_character_name (Display *dpy, Font fid, unsigned long uc)
1556 JNIEnv *env = XRootWindow (dpy, 0)->window.rh->jni_env;
1557 /* FindClass doesn't like to load classes if GetStaticMethodID fails. Huh? */
1559 c = (*env)->FindClass (env, "java/lang/Character"),
1560 c2 = (*env)->FindClass (env, "java/lang/NoSuchMethodError");
1562 if ((*env)->ExceptionOccurred(env)) abort();
1563 jmethodID m = (*env)->GetStaticMethodID (
1564 env, c, "getName", "(I)Ljava/lang/String;");
1565 jthrowable exc = (*env)->ExceptionOccurred(env);
1567 if ((*env)->IsAssignableFrom(env, (*env)->GetObjectClass(env, exc), c2)) {
1568 (*env)->ExceptionClear (env);
1569 Assert (!m, "jwxyz_unicode_character_name: m?");
1578 jstring name = (*env)->CallStaticObjectMethod (env, c, m, (jint)uc);
1580 ret = jstring_dup (env, name);
1584 asprintf(&ret, "U+%.4lX", uc);
1591 /* Called from utils/grabclient.c */
1593 jwxyz_draw_random_image (Display *dpy, Drawable drawable, GC gc)
1595 Window window = RootWindow (dpy, 0);
1596 struct running_hack *rh = window->window.rh;
1597 JNIEnv *env = rh->jni_env;
1598 jobject obj = rh->jobject;
1601 get_boolean_resource (rh->dpy, "chooseRandomImages", "ChooseRandomImages");
1603 get_boolean_resource (rh->dpy, "grabDesktopImages", "GrabDesktopImages");
1605 get_boolean_resource (rh->dpy, "rotateImages", "RotateImages");
1607 if (!images_p && !grab_p)
1610 if (grab_p && images_p) {
1611 grab_p = !(random() & 5); /* if both, screenshot 1/5th of the time */
1615 jclass c = (*env)->GetObjectClass (env, obj);
1616 jmethodID m = (*env)->GetMethodID (env, c,
1619 : "checkThenLoadRandomImage"),
1620 "(IIZ)[Ljava/lang/Object;");
1621 if ((*env)->ExceptionOccurred(env)) abort();
1622 jobjectArray img_name = (
1624 ? (*env)->CallObjectMethod (env, obj, m,
1625 drawable->frame.width, drawable->frame.height,
1626 (rotate_p ? JNI_TRUE : JNI_FALSE))
1628 if ((*env)->ExceptionOccurred(env)) abort();
1629 (*env)->DeleteLocalRef (env, c);
1632 fprintf (stderr, "failed to load %s\n", (grab_p ? "screenshot" : "image"));
1636 jobject jbitmap = (*env)->GetObjectArrayElement (env, img_name, 0);
1638 AndroidBitmapInfo bmp_info;
1639 AndroidBitmap_getInfo (env, jbitmap, &bmp_info);
1641 XImage *img = XCreateImage (dpy, NULL, visual_depth(NULL, NULL),
1643 bmp_info.width, bmp_info.height, 0,
1646 AndroidBitmap_lockPixels (env, jbitmap, (void **) &img->data);
1648 XPutImage (dpy, drawable, gc, img, 0, 0,
1649 (drawable->frame.width - bmp_info.width) / 2,
1650 (drawable->frame.height - bmp_info.height) / 2,
1651 bmp_info.width, bmp_info.height);
1653 AndroidBitmap_unlockPixels (env, jbitmap);
1655 XDestroyImage (img);
1657 return jstring_dup (env, (*env)->GetObjectArrayElement (env, img_name, 1));
1662 jwxyz_png_to_ximage (Display *dpy, Visual *visual,
1663 const unsigned char *png_data, unsigned long data_size)
1665 Window window = RootWindow (dpy, 0);
1666 struct running_hack *rh = window->window.rh;
1667 JNIEnv *env = rh->jni_env;
1668 jobject obj = rh->jobject;
1669 jclass c = (*env)->GetObjectClass (env, obj);
1670 jmethodID m = (*env)->GetMethodID (env, c, "decodePNG",
1671 "([B)Landroid/graphics/Bitmap;");
1672 if ((*env)->ExceptionOccurred(env)) abort();
1673 jbyteArray jdata = (*env)->NewByteArray (env, data_size);
1674 (*env)->SetByteArrayRegion (env, jdata, 0,
1675 data_size, (const jbyte *) png_data);
1678 ? (*env)->CallObjectMethod (env, obj, m, jdata)
1680 if ((*env)->ExceptionOccurred(env)) abort();
1681 (*env)->DeleteLocalRef (env, c);
1682 (*env)->DeleteLocalRef (env, jdata);
1686 AndroidBitmapInfo bmp_info;
1687 AndroidBitmap_getInfo (env, jbitmap, &bmp_info);
1689 XImage *img = XCreateImage (dpy, NULL, 32, ZPixmap, 0, NULL,
1690 bmp_info.width, bmp_info.height, 8,
1693 AndroidBitmap_lockPixels (env, jbitmap, (void **) &bits);
1694 img->data = (char *) calloc (img->bytes_per_line, img->height);
1695 memcpy (img->data, bits, img->bytes_per_line * img->height);
1696 AndroidBitmap_unlockPixels (env, jbitmap);
1698 // Java should have returned ARGB data.
1699 // WTF, why isn't ANDROID_BITMAP_FORMAT_ARGB_8888 defined?
1700 if (bmp_info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) abort();
1701 # ifndef __BYTE_ORDER__ // A GCC (and Clang)-ism.
1702 # error Need a __BYTE_ORDER__.
1703 # elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1704 img->byte_order = img->bitmap_bit_order = LSBFirst;
1705 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1706 img->byte_order = img->bitmap_bit_order = MSBFirst;
1708 # error Need a __BYTE_ORDER__.
1711 static const union {
1714 } c0 = {{0xff, 0x00, 0x00, 0x00}}, c1 = {{0x00, 0xff, 0x00, 0x00}},
1715 c2 = {{0x00, 0x00, 0xff, 0x00}};
1717 img->red_mask = c0.pixel;
1718 img->green_mask = c1.pixel;
1719 img->blue_mask = c2.pixel;
1724 #endif /* HAVE_ANDROID */