From http://www.jwz.org/xscreensaver/xscreensaver-5.36.tar.gz
[xscreensaver] / android / project / xscreensaver / src / org / jwz / xscreensaver / XScreenSaverRenderer.java
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>
4  *
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 
11  * implied warranty.
12  */
13
14 package org.jwz.xscreensaver;
15
16 import java.util.Map;
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;
33
34
35 public class XScreenSaverRenderer implements GLSurfaceView.Renderer {
36
37   boolean initTried = false;
38   jwxyz jwxyz_obj = null;
39
40   String hack;
41   int api;
42   Handler.Callback abort_callback;
43
44   Iterable<Map.Entry<String, String>> prefs;
45   Context app;
46   WindowManager wm;
47   Bitmap screenshot;
48
49   GLSurfaceView glview;
50
51   class RenderTask extends TimerTask {
52     public void run() {
53       glview.requestRender();
54     }
55   };
56
57   Timer timer = new Timer();
58
59   private void LOG (String fmt, Object... args) {
60     Log.d ("xscreensaver",
61            this.getClass().getSimpleName() + ": " +
62            String.format (fmt, args));
63   }
64
65   private void except(Exception e) {
66     jwxyz_obj = null;
67     Message m = Message.obtain (null, 0, (Object) e);
68     abort_callback.handleMessage (m);
69   }
70
71   public XScreenSaverRenderer (String hack, int api,
72                                Context app, WindowManager wm,
73                                Bitmap screenshot,
74                                Handler.Callback abort_callback,
75                                GLSurfaceView glview) {
76     super();
77     this.hack   = hack;
78     this.api    = api;
79     this.app    = app;
80     this.wm     = wm;
81     this.prefs  = prefs;
82     this.screenshot = screenshot;
83     this.abort_callback = abort_callback;
84     this.glview = glview;
85     LOG ("init %s %d", hack, api);
86   }
87
88   public void onDrawFrame (GL10 gl) {
89     try {
90       if (jwxyz_obj != null) {
91         long delay = jwxyz_obj.nativeRender();
92         // java.util.Timer doesn't seem to like to re-use TimerTasks, so
93         // there's a slow object churn here: one TimerTask per frame.
94         timer.schedule(new RenderTask(), delay / 1000);
95       }
96     } catch (RuntimeException e) {
97       except (e);
98     }
99   }
100
101   public void onSurfaceChanged(GL10 gl, int w, int h) {
102     try {
103       if (jwxyz_obj == null)
104         jwxyz_obj = new jwxyz (hack, api, app, screenshot, w, h);
105
106       double r;
107
108       Display d = wm.getDefaultDisplay();
109
110       switch (d.getRotation()) {
111       case Surface.ROTATION_90:  r = 90;  break;
112       case Surface.ROTATION_180: r = 180; break;
113       case Surface.ROTATION_270: r = 270; break;
114       default: r = 0; break;
115       }
116
117       jwxyz_obj.nativeResize (w, h, r);
118
119       glview.requestRender();
120
121     } catch (RuntimeException e) {
122       except (e);
123     }
124   }
125
126   public void onSurfaceCreated (GL10 gl, EGLConfig config) {
127     try {
128       LOG ("onSurfaceCreated %s / %s / %s", 
129            gl.glGetString (GL10.GL_VENDOR),
130            gl.glGetString (GL10.GL_RENDERER),
131            gl.glGetString (GL10.GL_VERSION));
132
133       if (!initTried) {
134         initTried = true;
135       } else {
136         if (jwxyz_obj != null) {
137           jwxyz_obj.nativeDone();
138           jwxyz_obj = null;
139         }
140       }
141     } catch (RuntimeException e) {
142       except (e);
143     }
144   }
145
146   public void release() {
147     try {
148       if (jwxyz_obj != null) {
149         jwxyz_obj.nativeDone();
150         jwxyz_obj = null;
151       }
152     } catch (RuntimeException e) {
153       except (e);
154     }
155   }
156
157   public void sendButtonEvent (int x, int y, boolean down) {
158     try {
159       jwxyz_obj.sendButtonEvent (x, y, down);
160     } catch (RuntimeException e) {
161       except (e);
162     }
163   }
164
165   public void sendMotionEvent (int x, int y) {
166     try {
167       jwxyz_obj.sendMotionEvent (x, y);
168     } catch (RuntimeException e) {
169       except (e);
170     }
171   }
172
173   public void sendKeyEvent (KeyEvent event) {
174     try {
175       jwxyz_obj.sendKeyEvent (event);
176     } catch (RuntimeException e) {
177       except (e);
178     }
179   }
180
181
182   static
183   {
184     System.loadLibrary ("xscreensaver");
185   }
186 }