From http://www.jwz.org/xscreensaver/xscreensaver-5.35.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 javax.microedition.khronos.egl.EGLConfig;
26 import javax.microedition.khronos.opengles.GL10;
27 import org.jwz.xscreensaver.jwxyz;
28 import android.os.Message;
29 import android.os.Handler;
30 import android.util.Log;
31
32
33 public class XScreenSaverRenderer implements GLSurfaceView.Renderer {
34
35   boolean initTried = false;
36   jwxyz jwxyz_obj = null;
37
38   String hack;
39   int api;
40   Handler.Callback abort_callback;
41
42   Iterable<Map.Entry<String, String>> prefs;
43   Context app;
44   WindowManager wm;
45   Bitmap screenshot;
46
47   private void LOG (String fmt, Object... args) {
48     Log.d ("xscreensaver",
49            this.getClass().getSimpleName() + ": " +
50            String.format (fmt, args));
51   }
52
53   private void except(Exception e) {
54     jwxyz_obj = null;
55     Message m = Message.obtain (null, 0, (Object) e);
56     abort_callback.handleMessage (m);
57   }
58
59   public XScreenSaverRenderer (String hack, int api,
60                                Context app, WindowManager wm,
61                                Bitmap screenshot,
62                                Handler.Callback abort_callback) {
63     super();
64     this.hack   = hack;
65     this.api    = api;
66     this.app    = app;
67     this.wm     = wm;
68     this.prefs  = prefs;
69     this.screenshot = screenshot;
70     this.abort_callback = abort_callback;
71     LOG ("init %s %d", hack, api);
72   }
73
74   public void onDrawFrame (GL10 gl) {
75     try {
76       if (jwxyz_obj != null)
77         jwxyz_obj.nativeRender();
78     } catch (RuntimeException e) {
79       except (e);
80     }
81   }
82
83   public void onSurfaceChanged(GL10 gl, int w, int h) {
84     try {
85       if (jwxyz_obj == null)
86         jwxyz_obj = new jwxyz (hack, api, app, screenshot, w, h);
87
88       double r;
89
90       Display d = wm.getDefaultDisplay();
91
92       switch (d.getRotation()) {
93       case Surface.ROTATION_90:  r = 90;  break;
94       case Surface.ROTATION_180: r = 180; break;
95       case Surface.ROTATION_270: r = 270; break;
96       default: r = 0; break;
97       }
98
99       jwxyz_obj.nativeResize (w, h, r);
100
101     } catch (RuntimeException e) {
102       except (e);
103     }
104   }
105
106   public void onSurfaceCreated (GL10 gl, EGLConfig config) {
107     try {
108       LOG ("onSurfaceCreated %s / %s / %s", 
109            gl.glGetString (GL10.GL_VENDOR),
110            gl.glGetString (GL10.GL_RENDERER),
111            gl.glGetString (GL10.GL_VERSION));
112
113       if (!initTried) {
114         initTried = true;
115       } else {
116         if (jwxyz_obj != null) {
117           jwxyz_obj.nativeDone();
118           jwxyz_obj = null;
119         }
120       }
121     } catch (RuntimeException e) {
122       except (e);
123     }
124   }
125
126   public void release() {
127     try {
128       if (jwxyz_obj != null) {
129         jwxyz_obj.nativeDone();
130         jwxyz_obj = null;
131       }
132     } catch (RuntimeException e) {
133       except (e);
134     }
135   }
136
137   public void sendButtonEvent (int x, int y, boolean down) {
138     try {
139       jwxyz_obj.sendButtonEvent (x, y, down);
140     } catch (RuntimeException e) {
141       except (e);
142     }
143   }
144
145   public void sendMotionEvent (int x, int y) {
146     try {
147       jwxyz_obj.sendMotionEvent (x, y);
148     } catch (RuntimeException e) {
149       except (e);
150     }
151   }
152
153   public void sendKeyEvent (KeyEvent event) {
154     try {
155       jwxyz_obj.sendKeyEvent (event);
156     } catch (RuntimeException e) {
157       except (e);
158     }
159   }
160
161
162   static
163   {
164     System.loadLibrary ("xscreensaver");
165   }
166 }