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;
42 Handler.Callback abort_callback;
44 Iterable<Map.Entry<String, String>> prefs;
51 class RenderTask extends TimerTask {
53 glview.requestRender();
57 Timer timer = new Timer();
59 private void LOG (String fmt, Object... args) {
60 Log.d ("xscreensaver",
61 this.getClass().getSimpleName() + ": " +
62 String.format (fmt, args));
65 private void except(Exception e) {
67 Message m = Message.obtain (null, 0, (Object) e);
68 abort_callback.handleMessage (m);
71 public XScreenSaverRenderer (String hack, int api,
72 Context app, WindowManager wm,
74 Handler.Callback abort_callback,
75 GLSurfaceView glview) {
82 this.screenshot = screenshot;
83 this.abort_callback = abort_callback;
85 LOG ("init %s %d", hack, api);
88 public void onDrawFrame (GL10 gl) {
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);
96 } catch (RuntimeException e) {
101 public void onSurfaceChanged(GL10 gl, int w, int h) {
103 if (jwxyz_obj == null)
104 jwxyz_obj = new jwxyz (hack, api, app, screenshot, w, h);
108 Display d = wm.getDefaultDisplay();
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;
117 jwxyz_obj.nativeResize (w, h, r);
119 glview.requestRender();
121 } catch (RuntimeException e) {
126 public void onSurfaceCreated (GL10 gl, EGLConfig config) {
128 LOG ("onSurfaceCreated %s / %s / %s",
129 gl.glGetString (GL10.GL_VENDOR),
130 gl.glGetString (GL10.GL_RENDERER),
131 gl.glGetString (GL10.GL_VERSION));
136 if (jwxyz_obj != null) {
137 jwxyz_obj.nativeDone();
141 } catch (RuntimeException e) {
146 public void release() {
148 if (jwxyz_obj != null) {
149 jwxyz_obj.nativeDone();
152 } catch (RuntimeException e) {
157 public void sendButtonEvent (int x, int y, boolean down) {
159 jwxyz_obj.sendButtonEvent (x, y, down);
160 } catch (RuntimeException e) {
165 public void sendMotionEvent (int x, int y) {
167 jwxyz_obj.sendMotionEvent (x, y);
168 } catch (RuntimeException e) {
173 public void sendKeyEvent (KeyEvent event) {
175 jwxyz_obj.sendKeyEvent (event);
176 } catch (RuntimeException e) {
184 System.loadLibrary ("xscreensaver");