1 /* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * xscreensaver, Copyright (c) 2016 Jamie Zawinski <jwz@jwz.org>
3 * and Dennis Sheil <dennis@panaceasupplies.com>
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation. No representations are made about the suitability of this
10 * software for any purpose. It is provided "as is" without express or
14 package org.jwz.xscreensaver;
17 import java.lang.RuntimeException;
18 import android.view.Display;
19 import android.view.WindowManager;
20 import android.view.Surface;
21 import android.view.KeyEvent;
22 import android.content.Context;
23 import android.graphics.Bitmap;
24 import android.opengl.GLSurfaceView;
25 import java.util.Timer;
26 import java.util.TimerTask;
27 import javax.microedition.khronos.egl.EGLConfig;
28 import javax.microedition.khronos.opengles.GL10;
29 import org.jwz.xscreensaver.jwxyz;
30 import android.os.Message;
31 import android.os.Handler;
32 import android.util.Log;
35 public class XScreenSaverRenderer implements GLSurfaceView.Renderer {
37 boolean initTried = false;
38 jwxyz jwxyz_obj = null;
41 Handler.Callback abort_callback;
48 class RenderTask extends TimerTask {
50 glview.requestRender();
54 Timer timer = new Timer();
56 private void LOG (String fmt, Object... args) {
57 Log.d ("xscreensaver",
58 this.getClass().getSimpleName() + ": " +
59 String.format (fmt, args));
62 private void except(Exception e) {
64 Message m = Message.obtain (null, 0, (Object) e);
65 abort_callback.handleMessage (m);
68 public XScreenSaverRenderer (String hack,
71 Handler.Callback abort_callback,
72 GLSurfaceView glview) {
76 this.screenshot = screenshot;
77 this.abort_callback = abort_callback;
79 LOG ("init %s", hack);
81 this.glview.setEGLConfigChooser (8, 8, 8, 8, 16, 0);
82 this.glview.setRenderer (this);
83 this.glview.setRenderMode (GLSurfaceView.RENDERMODE_WHEN_DIRTY);
86 static public String saverNameOf (Object obj) {
87 // Extract the saver name from e.g. "gen.Daydream$BouncingCow"
88 String name = obj.getClass().getSimpleName();
89 int index = name.lastIndexOf('$');
92 name = name.substring (index, name.length() - index);
94 return name.toLowerCase();
97 public void onDrawFrame (GL10 gl) {
99 if (jwxyz_obj != null) {
100 long delay = jwxyz_obj.nativeRender();
101 // java.util.Timer doesn't seem to like to re-use TimerTasks, so
102 // there's a slow object churn here: one TimerTask per frame.
103 timer.schedule(new RenderTask(), delay / 1000);
105 } catch (RuntimeException e) {
110 public void onSurfaceChanged(GL10 gl, int w, int h) {
112 if (jwxyz_obj == null)
113 jwxyz_obj = new jwxyz (hack, app, screenshot, w, h);
117 Display d = glview.getDisplay();
120 switch (d.getRotation()) {
121 case Surface.ROTATION_90: r = 90; break;
122 case Surface.ROTATION_180: r = 180; break;
123 case Surface.ROTATION_270: r = 270; break;
127 jwxyz_obj.nativeResize (w, h, r);
129 glview.requestRender();
131 } catch (RuntimeException e) {
136 public void onSurfaceCreated (GL10 gl, EGLConfig config) {
138 LOG ("onSurfaceCreated %s / %s / %s",
139 gl.glGetString (GL10.GL_VENDOR),
140 gl.glGetString (GL10.GL_RENDERER),
141 gl.glGetString (GL10.GL_VERSION));
146 if (jwxyz_obj != null) {
147 jwxyz_obj.nativeDone();
151 } catch (RuntimeException e) {
156 public void release() {
158 if (jwxyz_obj != null) {
159 jwxyz_obj.nativeDone();
162 } catch (RuntimeException e) {
167 public void sendButtonEvent (int x, int y, boolean down) {
169 jwxyz_obj.sendButtonEvent (x, y, down);
170 } catch (RuntimeException e) {
175 public void sendMotionEvent (int x, int y) {
177 jwxyz_obj.sendMotionEvent (x, y);
178 } catch (RuntimeException e) {
183 public void sendKeyEvent (KeyEvent event) {
185 jwxyz_obj.sendKeyEvent (event);
186 } catch (RuntimeException e) {
194 System.loadLibrary ("xscreensaver");