|
@@ -27,6 +27,7 @@ import android.graphics.*;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.media.*;
|
|
import android.media.*;
|
|
import android.hardware.*;
|
|
import android.hardware.*;
|
|
|
|
+import android.content.pm.ActivityInfo;
|
|
|
|
|
|
/**
|
|
/**
|
|
SDL Activity
|
|
SDL Activity
|
|
@@ -38,6 +39,13 @@ public class SDLActivity extends Activity {
|
|
public static boolean mIsPaused, mIsSurfaceReady, mHasFocus;
|
|
public static boolean mIsPaused, mIsSurfaceReady, mHasFocus;
|
|
public static boolean mExitCalledFromJava;
|
|
public static boolean mExitCalledFromJava;
|
|
|
|
|
|
|
|
+ /** If shared libraries (e.g. SDL or the native application) could not be loaded. */
|
|
|
|
+ public static boolean mBrokenLibraries;
|
|
|
|
+
|
|
|
|
+ // If we want to separate mouse and touch events.
|
|
|
|
+ // This is only toggled in native code when a hint is set!
|
|
|
|
+ public static boolean mSeparateMouseAndTouch;
|
|
|
|
+
|
|
// Main components
|
|
// Main components
|
|
protected static SDLActivity mSingleton;
|
|
protected static SDLActivity mSingleton;
|
|
protected static SDLSurface mSurface;
|
|
protected static SDLSurface mSurface;
|
|
@@ -47,22 +55,37 @@ public class SDLActivity extends Activity {
|
|
|
|
|
|
// This is what SDL runs in. It invokes SDL_main(), eventually
|
|
// This is what SDL runs in. It invokes SDL_main(), eventually
|
|
protected static Thread mSDLThread;
|
|
protected static Thread mSDLThread;
|
|
-
|
|
|
|
|
|
+
|
|
// Audio
|
|
// Audio
|
|
protected static AudioTrack mAudioTrack;
|
|
protected static AudioTrack mAudioTrack;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * This method is called by SDL before loading the native shared libraries.
|
|
|
|
+ * It can be overridden to provide names of shared libraries to be loaded.
|
|
|
|
+ * The default implementation returns the defaults. It never returns null.
|
|
|
|
+ * An array returned by a new implementation must at least contain "SDL2".
|
|
|
|
+ * Also keep in mind that the order the libraries are loaded may matter.
|
|
|
|
+ * @return names of shared libraries to be loaded (e.g. "SDL2", "main").
|
|
|
|
+ */
|
|
|
|
+ protected String[] getLibraries() {
|
|
|
|
+ return new String[] {
|
|
|
|
+ //"SDL2",
|
|
|
|
+ // "SDL2_image",
|
|
|
|
+ // "SDL2_mixer",
|
|
|
|
+ // "SDL2_net",
|
|
|
|
+ // "SDL2_ttf",
|
|
|
|
+ //"main"
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
// Load the .so
|
|
// Load the .so
|
|
- static {
|
|
|
|
- //System.loadLibrary("SDL2");
|
|
|
|
- //System.loadLibrary("SDL2_image");
|
|
|
|
- //System.loadLibrary("SDL2_mixer");
|
|
|
|
- //System.loadLibrary("SDL2_net");
|
|
|
|
- //System.loadLibrary("SDL2_ttf");
|
|
|
|
- //System.loadLibrary("main");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ public void loadLibraries() {
|
|
|
|
+ for (String lib : getLibraries()) {
|
|
|
|
+ System.loadLibrary(lib);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * This method is called by SDL using JNI.
|
|
|
|
* This method is called by SDL before starting the native application thread.
|
|
* This method is called by SDL before starting the native application thread.
|
|
* It can be overridden to provide the arguments after the application name.
|
|
* It can be overridden to provide the arguments after the application name.
|
|
* The default implementation returns an empty array. It never returns null.
|
|
* The default implementation returns an empty array. It never returns null.
|
|
@@ -71,7 +94,7 @@ public class SDLActivity extends Activity {
|
|
protected String[] getArguments() {
|
|
protected String[] getArguments() {
|
|
return new String[0];
|
|
return new String[0];
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public static void initialize() {
|
|
public static void initialize() {
|
|
// The static nature of the singleton and Android quirkyness force us to initialize everything here
|
|
// The static nature of the singleton and Android quirkyness force us to initialize everything here
|
|
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
|
|
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
|
|
@@ -83,6 +106,7 @@ public class SDLActivity extends Activity {
|
|
mSDLThread = null;
|
|
mSDLThread = null;
|
|
mAudioTrack = null;
|
|
mAudioTrack = null;
|
|
mExitCalledFromJava = false;
|
|
mExitCalledFromJava = false;
|
|
|
|
+ mBrokenLibraries = false;
|
|
mIsPaused = false;
|
|
mIsPaused = false;
|
|
mIsSurfaceReady = false;
|
|
mIsSurfaceReady = false;
|
|
mHasFocus = true;
|
|
mHasFocus = true;
|
|
@@ -91,16 +115,54 @@ public class SDLActivity extends Activity {
|
|
// Setup
|
|
// Setup
|
|
@Override
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
- Log.v("SDL", "onCreate():" + mSingleton);
|
|
|
|
|
|
+ Log.v(TAG, "Device: " + android.os.Build.DEVICE);
|
|
|
|
+ Log.v(TAG, "Model: " + android.os.Build.MODEL);
|
|
|
|
+ Log.v(TAG, "onCreate(): " + mSingleton);
|
|
super.onCreate(savedInstanceState);
|
|
super.onCreate(savedInstanceState);
|
|
-
|
|
|
|
|
|
+
|
|
SDLActivity.initialize();
|
|
SDLActivity.initialize();
|
|
// So we can call stuff from static callbacks
|
|
// So we can call stuff from static callbacks
|
|
mSingleton = this;
|
|
mSingleton = this;
|
|
|
|
|
|
|
|
+ // Load shared libraries
|
|
|
|
+ String errorMsgBrokenLib = "";
|
|
|
|
+ try {
|
|
|
|
+ loadLibraries();
|
|
|
|
+ } catch(UnsatisfiedLinkError e) {
|
|
|
|
+ System.err.println(e.getMessage());
|
|
|
|
+ mBrokenLibraries = true;
|
|
|
|
+ errorMsgBrokenLib = e.getMessage();
|
|
|
|
+ } catch(Exception e) {
|
|
|
|
+ System.err.println(e.getMessage());
|
|
|
|
+ mBrokenLibraries = true;
|
|
|
|
+ errorMsgBrokenLib = e.getMessage();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (mBrokenLibraries)
|
|
|
|
+ {
|
|
|
|
+ AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
|
|
|
|
+ dlgAlert.setMessage("An error occurred while trying to start the application. Please try again and/or reinstall."
|
|
|
|
+ + System.getProperty("line.separator")
|
|
|
|
+ + System.getProperty("line.separator")
|
|
|
|
+ + "Error: " + errorMsgBrokenLib);
|
|
|
|
+ dlgAlert.setTitle("SDL Error");
|
|
|
|
+ dlgAlert.setPositiveButton("Exit",
|
|
|
|
+ new DialogInterface.OnClickListener() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onClick(DialogInterface dialog,int id) {
|
|
|
|
+ // if this button is clicked, close current activity
|
|
|
|
+ SDLActivity.mSingleton.finish();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ dlgAlert.setCancelable(false);
|
|
|
|
+ dlgAlert.create().show();
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
// Set up the surface
|
|
// Set up the surface
|
|
mSurface = new SDLSurface(getApplication());
|
|
mSurface = new SDLSurface(getApplication());
|
|
-
|
|
|
|
|
|
+
|
|
if(Build.VERSION.SDK_INT >= 12) {
|
|
if(Build.VERSION.SDK_INT >= 12) {
|
|
mJoystickHandler = new SDLJoystickHandler_API12();
|
|
mJoystickHandler = new SDLJoystickHandler_API12();
|
|
}
|
|
}
|
|
@@ -112,20 +174,41 @@ public class SDLActivity extends Activity {
|
|
mLayout.addView(mSurface);
|
|
mLayout.addView(mSurface);
|
|
|
|
|
|
setContentView(mLayout);
|
|
setContentView(mLayout);
|
|
|
|
+
|
|
|
|
+ // Get filename from "Open with" of another application
|
|
|
|
+ Intent intent = getIntent();
|
|
|
|
+
|
|
|
|
+ if (intent != null && intent.getData() != null) {
|
|
|
|
+ String filename = intent.getData().getPath();
|
|
|
|
+ if (filename != null) {
|
|
|
|
+ Log.v(TAG, "Got filename: " + filename);
|
|
|
|
+ SDLActivity.onNativeDropFile(filename);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// Events
|
|
// Events
|
|
@Override
|
|
@Override
|
|
protected void onPause() {
|
|
protected void onPause() {
|
|
- Log.v("SDL", "onPause()");
|
|
|
|
|
|
+ Log.v(TAG, "onPause()");
|
|
super.onPause();
|
|
super.onPause();
|
|
|
|
+
|
|
|
|
+ if (SDLActivity.mBrokenLibraries) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
SDLActivity.handlePause();
|
|
SDLActivity.handlePause();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
protected void onResume() {
|
|
protected void onResume() {
|
|
- Log.v("SDL", "onResume()");
|
|
|
|
|
|
+ Log.v(TAG, "onResume()");
|
|
super.onResume();
|
|
super.onResume();
|
|
|
|
+
|
|
|
|
+ if (SDLActivity.mBrokenLibraries) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
SDLActivity.handleResume();
|
|
SDLActivity.handleResume();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -133,7 +216,11 @@ public class SDLActivity extends Activity {
|
|
@Override
|
|
@Override
|
|
public void onWindowFocusChanged(boolean hasFocus) {
|
|
public void onWindowFocusChanged(boolean hasFocus) {
|
|
super.onWindowFocusChanged(hasFocus);
|
|
super.onWindowFocusChanged(hasFocus);
|
|
- Log.v("SDL", "onWindowFocusChanged(): " + hasFocus);
|
|
|
|
|
|
+ Log.v(TAG, "onWindowFocusChanged(): " + hasFocus);
|
|
|
|
+
|
|
|
|
+ if (SDLActivity.mBrokenLibraries) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
SDLActivity.mHasFocus = hasFocus;
|
|
SDLActivity.mHasFocus = hasFocus;
|
|
if (hasFocus) {
|
|
if (hasFocus) {
|
|
@@ -143,14 +230,27 @@ public class SDLActivity extends Activity {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void onLowMemory() {
|
|
public void onLowMemory() {
|
|
- Log.v("SDL", "onLowMemory()");
|
|
|
|
|
|
+ Log.v(TAG, "onLowMemory()");
|
|
super.onLowMemory();
|
|
super.onLowMemory();
|
|
|
|
+
|
|
|
|
+ if (SDLActivity.mBrokenLibraries) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
SDLActivity.nativeLowMemory();
|
|
SDLActivity.nativeLowMemory();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
protected void onDestroy() {
|
|
protected void onDestroy() {
|
|
- Log.v("SDL", "onDestroy()");
|
|
|
|
|
|
+ Log.v(TAG, "onDestroy()");
|
|
|
|
+
|
|
|
|
+ if (SDLActivity.mBrokenLibraries) {
|
|
|
|
+ super.onDestroy();
|
|
|
|
+ // Reset everything in case the user re opens the app
|
|
|
|
+ SDLActivity.initialize();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
// Send a quit message to the application
|
|
// Send a quit message to the application
|
|
SDLActivity.mExitCalledFromJava = true;
|
|
SDLActivity.mExitCalledFromJava = true;
|
|
SDLActivity.nativeQuit();
|
|
SDLActivity.nativeQuit();
|
|
@@ -160,13 +260,13 @@ public class SDLActivity extends Activity {
|
|
try {
|
|
try {
|
|
SDLActivity.mSDLThread.join();
|
|
SDLActivity.mSDLThread.join();
|
|
} catch(Exception e) {
|
|
} catch(Exception e) {
|
|
- Log.v("SDL", "Problem stopping thread: " + e);
|
|
|
|
|
|
+ Log.v(TAG, "Problem stopping thread: " + e);
|
|
}
|
|
}
|
|
SDLActivity.mSDLThread = null;
|
|
SDLActivity.mSDLThread = null;
|
|
|
|
|
|
- //Log.v("SDL", "Finished waiting for SDL thread");
|
|
|
|
|
|
+ //Log.v(TAG, "Finished waiting for SDL thread");
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
super.onDestroy();
|
|
super.onDestroy();
|
|
// Reset everything in case the user re opens the app
|
|
// Reset everything in case the user re opens the app
|
|
SDLActivity.initialize();
|
|
SDLActivity.initialize();
|
|
@@ -174,6 +274,11 @@ public class SDLActivity extends Activity {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
|
|
|
+
|
|
|
|
+ if (SDLActivity.mBrokenLibraries) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
int keyCode = event.getKeyCode();
|
|
int keyCode = event.getKeyCode();
|
|
// Ignore certain special keys so they're handled by Android
|
|
// Ignore certain special keys so they're handled by Android
|
|
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
|
|
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
|
|
@@ -210,7 +315,7 @@ public class SDLActivity extends Activity {
|
|
mSurface.handleResume();
|
|
mSurface.handleResume();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/* The native thread has finished */
|
|
/* The native thread has finished */
|
|
public static void handleNativeExit() {
|
|
public static void handleNativeExit() {
|
|
SDLActivity.mSDLThread = null;
|
|
SDLActivity.mSDLThread = null;
|
|
@@ -304,7 +409,8 @@ public class SDLActivity extends Activity {
|
|
public static native void nativeQuit();
|
|
public static native void nativeQuit();
|
|
public static native void nativePause();
|
|
public static native void nativePause();
|
|
public static native void nativeResume();
|
|
public static native void nativeResume();
|
|
- public static native void onNativeResize(int x, int y, int format);
|
|
|
|
|
|
+ public static native void onNativeDropFile(String filename);
|
|
|
|
+ public static native void onNativeResize(int x, int y, int format, float rate);
|
|
public static native int onNativePadDown(int device_id, int keycode);
|
|
public static native int onNativePadDown(int device_id, int keycode);
|
|
public static native int onNativePadUp(int device_id, int keycode);
|
|
public static native int onNativePadUp(int device_id, int keycode);
|
|
public static native void onNativeJoy(int device_id, int axis,
|
|
public static native void onNativeJoy(int device_id, int axis,
|
|
@@ -314,15 +420,16 @@ public class SDLActivity extends Activity {
|
|
public static native void onNativeKeyDown(int keycode);
|
|
public static native void onNativeKeyDown(int keycode);
|
|
public static native void onNativeKeyUp(int keycode);
|
|
public static native void onNativeKeyUp(int keycode);
|
|
public static native void onNativeKeyboardFocusLost();
|
|
public static native void onNativeKeyboardFocusLost();
|
|
|
|
+ public static native void onNativeMouse(int button, int action, float x, float y);
|
|
public static native void onNativeTouch(int touchDevId, int pointerFingerId,
|
|
public static native void onNativeTouch(int touchDevId, int pointerFingerId,
|
|
- int action, float x,
|
|
|
|
|
|
+ int action, float x,
|
|
float y, float p);
|
|
float y, float p);
|
|
public static native void onNativeAccel(float x, float y, float z);
|
|
public static native void onNativeAccel(float x, float y, float z);
|
|
public static native void onNativeSurfaceChanged();
|
|
public static native void onNativeSurfaceChanged();
|
|
public static native void onNativeSurfaceDestroyed();
|
|
public static native void onNativeSurfaceDestroyed();
|
|
public static native void nativeFlipBuffers();
|
|
public static native void nativeFlipBuffers();
|
|
- public static native int nativeAddJoystick(int device_id, String name,
|
|
|
|
- int is_accelerometer, int nbuttons,
|
|
|
|
|
|
+ public static native int nativeAddJoystick(int device_id, String name,
|
|
|
|
+ int is_accelerometer, int nbuttons,
|
|
int naxes, int nhats, int nballs);
|
|
int naxes, int nhats, int nballs);
|
|
public static native int nativeRemoveJoystick(int device_id);
|
|
public static native int nativeRemoveJoystick(int device_id);
|
|
public static native String nativeGetHint(String name);
|
|
public static native String nativeGetHint(String name);
|
|
@@ -447,33 +554,33 @@ public class SDLActivity extends Activity {
|
|
int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
|
|
int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
|
|
int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
|
|
int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
|
|
int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
|
|
int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
|
|
-
|
|
|
|
- Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + (sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ Log.v(TAG, "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + (sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
|
|
|
|
+
|
|
// Let the user pick a larger buffer if they really want -- but ye
|
|
// Let the user pick a larger buffer if they really want -- but ye
|
|
// gods they probably shouldn't, the minimums are horrifyingly high
|
|
// gods they probably shouldn't, the minimums are horrifyingly high
|
|
// latency already
|
|
// latency already
|
|
desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
|
|
desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
|
|
-
|
|
|
|
|
|
+
|
|
if (mAudioTrack == null) {
|
|
if (mAudioTrack == null) {
|
|
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
|
|
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
|
|
channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
|
|
channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
|
|
-
|
|
|
|
|
|
+
|
|
// Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
|
|
// Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
|
|
// Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java
|
|
// Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java
|
|
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()
|
|
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()
|
|
-
|
|
|
|
|
|
+
|
|
if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
|
|
if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
|
|
- Log.e("SDL", "Failed during initialization of Audio Track");
|
|
|
|
|
|
+ Log.e(TAG, "Failed during initialization of Audio Track");
|
|
mAudioTrack = null;
|
|
mAudioTrack = null;
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
mAudioTrack.play();
|
|
mAudioTrack.play();
|
|
}
|
|
}
|
|
-
|
|
|
|
- Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ Log.v(TAG, "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
|
|
|
|
+
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -492,7 +599,7 @@ public class SDLActivity extends Activity {
|
|
// Nom nom
|
|
// Nom nom
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- Log.w("SDL", "SDL audio: error return from write(short)");
|
|
|
|
|
|
+ Log.w(TAG, "SDL audio: error return from write(short)");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -513,7 +620,7 @@ public class SDLActivity extends Activity {
|
|
// Nom nom
|
|
// Nom nom
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- Log.w("SDL", "SDL audio: error return from write(byte)");
|
|
|
|
|
|
+ Log.w(TAG, "SDL audio: error return from write(byte)");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -833,11 +940,11 @@ class SDLMain implements Runnable {
|
|
|
|
|
|
/**
|
|
/**
|
|
SDLSurface. This is what we draw on, so we need to know when it's created
|
|
SDLSurface. This is what we draw on, so we need to know when it's created
|
|
- in order to do anything useful.
|
|
|
|
|
|
+ in order to do anything useful.
|
|
|
|
|
|
Because of this, that's where we set up the SDL thread
|
|
Because of this, that's where we set up the SDL thread
|
|
*/
|
|
*/
|
|
-class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
|
|
|
|
+class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
View.OnKeyListener, View.OnTouchListener, SensorEventListener {
|
|
View.OnKeyListener, View.OnTouchListener, SensorEventListener {
|
|
|
|
|
|
// Sensors
|
|
// Sensors
|
|
@@ -847,20 +954,20 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
// Keep track of the surface size to normalize touch events
|
|
// Keep track of the surface size to normalize touch events
|
|
protected static float mWidth, mHeight;
|
|
protected static float mWidth, mHeight;
|
|
|
|
|
|
- // Startup
|
|
|
|
|
|
+ // Startup
|
|
public SDLSurface(Context context) {
|
|
public SDLSurface(Context context) {
|
|
super(context);
|
|
super(context);
|
|
- getHolder().addCallback(this);
|
|
|
|
-
|
|
|
|
|
|
+ getHolder().addCallback(this);
|
|
|
|
+
|
|
setFocusable(true);
|
|
setFocusable(true);
|
|
setFocusableInTouchMode(true);
|
|
setFocusableInTouchMode(true);
|
|
requestFocus();
|
|
requestFocus();
|
|
- setOnKeyListener(this);
|
|
|
|
- setOnTouchListener(this);
|
|
|
|
|
|
+ setOnKeyListener(this);
|
|
|
|
+ setOnTouchListener(this);
|
|
|
|
|
|
mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
|
mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
|
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
|
|
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
|
|
-
|
|
|
|
|
|
+
|
|
if(Build.VERSION.SDK_INT >= 12) {
|
|
if(Build.VERSION.SDK_INT >= 12) {
|
|
setOnGenericMotionListener(new SDLGenericMotionListener_API12());
|
|
setOnGenericMotionListener(new SDLGenericMotionListener_API12());
|
|
}
|
|
}
|
|
@@ -869,7 +976,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
mWidth = 1.0f;
|
|
mWidth = 1.0f;
|
|
mHeight = 1.0f;
|
|
mHeight = 1.0f;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public void handleResume() {
|
|
public void handleResume() {
|
|
setFocusable(true);
|
|
setFocusable(true);
|
|
setFocusableInTouchMode(true);
|
|
setFocusableInTouchMode(true);
|
|
@@ -878,7 +985,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
setOnTouchListener(this);
|
|
setOnTouchListener(this);
|
|
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
|
|
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public Surface getNativeSurface() {
|
|
public Surface getNativeSurface() {
|
|
return getHolder().getSurface();
|
|
return getHolder().getSurface();
|
|
}
|
|
}
|
|
@@ -953,8 +1060,44 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
|
|
|
|
mWidth = width;
|
|
mWidth = width;
|
|
mHeight = height;
|
|
mHeight = height;
|
|
- SDLActivity.onNativeResize(width, height, sdlFormat);
|
|
|
|
- Log.v("SDL", "Window size:" + width + "x"+height);
|
|
|
|
|
|
+ SDLActivity.onNativeResize(width, height, sdlFormat, mDisplay.getRefreshRate());
|
|
|
|
+ Log.v("SDL", "Window size: " + width + "x" + height);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ boolean skip = false;
|
|
|
|
+ int requestedOrientation = SDLActivity.mSingleton.getRequestedOrientation();
|
|
|
|
+
|
|
|
|
+ if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
|
|
|
|
+ {
|
|
|
|
+ // Accept any
|
|
|
|
+ }
|
|
|
|
+ else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
|
|
|
|
+ {
|
|
|
|
+ if (mWidth > mHeight) {
|
|
|
|
+ skip = true;
|
|
|
|
+ }
|
|
|
|
+ } else if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
|
|
|
|
+ if (mWidth < mHeight) {
|
|
|
|
+ skip = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Special Patch for Square Resolution: Black Berry Passport
|
|
|
|
+ if (skip) {
|
|
|
|
+ double min = Math.min(mWidth, mHeight);
|
|
|
|
+ double max = Math.max(mWidth, mHeight);
|
|
|
|
+
|
|
|
|
+ if (max / min < 1.20) {
|
|
|
|
+ Log.v("SDL", "Don't skip on such aspect-ratio. Could be a square resolution.");
|
|
|
|
+ skip = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (skip) {
|
|
|
|
+ Log.v("SDL", "Skip .. Surface is not ready.");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
// Set mIsSurfaceReady to 'true' *before* making a call to handleResume
|
|
// Set mIsSurfaceReady to 'true' *before* making a call to handleResume
|
|
SDLActivity.mIsSurfaceReady = true;
|
|
SDLActivity.mIsSurfaceReady = true;
|
|
@@ -968,7 +1111,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");
|
|
final Thread sdlThread = new Thread(new SDLMain(), "SDLThread");
|
|
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
|
|
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
|
|
sdlThread.start();
|
|
sdlThread.start();
|
|
-
|
|
|
|
|
|
+
|
|
// Set up a listener thread to catch when the native thread ends
|
|
// Set up a listener thread to catch when the native thread ends
|
|
SDLActivity.mSDLThread = new Thread(new Runnable(){
|
|
SDLActivity.mSDLThread = new Thread(new Runnable(){
|
|
@Override
|
|
@Override
|
|
@@ -977,16 +1120,20 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
sdlThread.join();
|
|
sdlThread.join();
|
|
}
|
|
}
|
|
catch(Exception e){}
|
|
catch(Exception e){}
|
|
- finally{
|
|
|
|
|
|
+ finally{
|
|
// Native thread has finished
|
|
// Native thread has finished
|
|
if (! SDLActivity.mExitCalledFromJava) {
|
|
if (! SDLActivity.mExitCalledFromJava) {
|
|
SDLActivity.handleNativeExit();
|
|
SDLActivity.handleNativeExit();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
+ }, "SDLThreadListener");
|
|
SDLActivity.mSDLThread.start();
|
|
SDLActivity.mSDLThread.start();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (SDLActivity.mHasFocus) {
|
|
|
|
+ SDLActivity.handleResume();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// unused
|
|
// unused
|
|
@@ -1000,8 +1147,8 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
// Dispatch the different events depending on where they come from
|
|
// Dispatch the different events depending on where they come from
|
|
// Some SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
|
|
// Some SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
|
|
// So, we try to process them as DPAD or GAMEPAD events first, if that fails we try them as KEYBOARD
|
|
// So, we try to process them as DPAD or GAMEPAD events first, if that fails we try them as KEYBOARD
|
|
-
|
|
|
|
- if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
|
|
|
|
|
|
+
|
|
|
|
+ if ( (event.getSource() & InputDevice.SOURCE_GAMEPAD) != 0 ||
|
|
(event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
|
|
(event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
|
|
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
if (SDLActivity.onNativePadDown(event.getDeviceId(), keyCode) == 0) {
|
|
if (SDLActivity.onNativePadDown(event.getDeviceId(), keyCode) == 0) {
|
|
@@ -1013,7 +1160,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if( (event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
|
|
if( (event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
|
|
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
//Log.v("SDL", "key down: " + keyCode);
|
|
//Log.v("SDL", "key down: " + keyCode);
|
|
@@ -1026,7 +1173,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1038,68 +1185,98 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
final int pointerCount = event.getPointerCount();
|
|
final int pointerCount = event.getPointerCount();
|
|
int action = event.getActionMasked();
|
|
int action = event.getActionMasked();
|
|
int pointerFingerId;
|
|
int pointerFingerId;
|
|
|
|
+ int mouseButton;
|
|
int i = -1;
|
|
int i = -1;
|
|
float x,y,p;
|
|
float x,y,p;
|
|
-
|
|
|
|
- switch(action) {
|
|
|
|
- case MotionEvent.ACTION_MOVE:
|
|
|
|
- for (i = 0; i < pointerCount; i++) {
|
|
|
|
- pointerFingerId = event.getPointerId(i);
|
|
|
|
- x = event.getX(i) / mWidth;
|
|
|
|
- y = event.getY(i) / mHeight;
|
|
|
|
- p = event.getPressure(i);
|
|
|
|
- SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
-
|
|
|
|
- case MotionEvent.ACTION_UP:
|
|
|
|
- case MotionEvent.ACTION_DOWN:
|
|
|
|
- // Primary pointer up/down, the index is always zero
|
|
|
|
- i = 0;
|
|
|
|
- case MotionEvent.ACTION_POINTER_UP:
|
|
|
|
- case MotionEvent.ACTION_POINTER_DOWN:
|
|
|
|
- // Non primary pointer up/down
|
|
|
|
- if (i == -1) {
|
|
|
|
- i = event.getActionIndex();
|
|
|
|
|
|
+
|
|
|
|
+ // !!! FIXME: dump this SDK check after 2.0.4 ships and require API14.
|
|
|
|
+ if (event.getSource() == InputDevice.SOURCE_MOUSE && SDLActivity.mSeparateMouseAndTouch) {
|
|
|
|
+ if (Build.VERSION.SDK_INT < 14) {
|
|
|
|
+ mouseButton = 1; // For Android==12 all mouse buttons are the left button
|
|
|
|
+ } else {
|
|
|
|
+ try {
|
|
|
|
+ mouseButton = (Integer) event.getClass().getMethod("getButtonState").invoke(event);
|
|
|
|
+ } catch(Exception e) {
|
|
|
|
+ mouseButton = 1; // oh well.
|
|
}
|
|
}
|
|
-
|
|
|
|
- pointerFingerId = event.getPointerId(i);
|
|
|
|
- x = event.getX(i) / mWidth;
|
|
|
|
- y = event.getY(i) / mHeight;
|
|
|
|
- p = event.getPressure(i);
|
|
|
|
- SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
|
|
|
|
- break;
|
|
|
|
-
|
|
|
|
- case MotionEvent.ACTION_CANCEL:
|
|
|
|
- for (i = 0; i < pointerCount; i++) {
|
|
|
|
|
|
+ }
|
|
|
|
+ SDLActivity.onNativeMouse(mouseButton, action, event.getX(0), event.getY(0));
|
|
|
|
+ } else {
|
|
|
|
+ switch(action) {
|
|
|
|
+ case MotionEvent.ACTION_MOVE:
|
|
|
|
+ for (i = 0; i < pointerCount; i++) {
|
|
|
|
+ pointerFingerId = event.getPointerId(i);
|
|
|
|
+ x = event.getX(i) / mWidth;
|
|
|
|
+ y = event.getY(i) / mHeight;
|
|
|
|
+ p = event.getPressure(i);
|
|
|
|
+ if (p > 1.0f) {
|
|
|
|
+ // may be larger than 1.0f on some devices
|
|
|
|
+ // see the documentation of getPressure(i)
|
|
|
|
+ p = 1.0f;
|
|
|
|
+ }
|
|
|
|
+ SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
|
+ // Primary pointer up/down, the index is always zero
|
|
|
|
+ i = 0;
|
|
|
|
+ case MotionEvent.ACTION_POINTER_UP:
|
|
|
|
+ case MotionEvent.ACTION_POINTER_DOWN:
|
|
|
|
+ // Non primary pointer up/down
|
|
|
|
+ if (i == -1) {
|
|
|
|
+ i = event.getActionIndex();
|
|
|
|
+ }
|
|
|
|
+
|
|
pointerFingerId = event.getPointerId(i);
|
|
pointerFingerId = event.getPointerId(i);
|
|
x = event.getX(i) / mWidth;
|
|
x = event.getX(i) / mWidth;
|
|
y = event.getY(i) / mHeight;
|
|
y = event.getY(i) / mHeight;
|
|
p = event.getPressure(i);
|
|
p = event.getPressure(i);
|
|
- SDLActivity.onNativeTouch(touchDevId, pointerFingerId, MotionEvent.ACTION_UP, x, y, p);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
|
|
+ if (p > 1.0f) {
|
|
|
|
+ // may be larger than 1.0f on some devices
|
|
|
|
+ // see the documentation of getPressure(i)
|
|
|
|
+ p = 1.0f;
|
|
|
|
+ }
|
|
|
|
+ SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p);
|
|
|
|
+ break;
|
|
|
|
|
|
- default:
|
|
|
|
- break;
|
|
|
|
|
|
+ case MotionEvent.ACTION_CANCEL:
|
|
|
|
+ for (i = 0; i < pointerCount; i++) {
|
|
|
|
+ pointerFingerId = event.getPointerId(i);
|
|
|
|
+ x = event.getX(i) / mWidth;
|
|
|
|
+ y = event.getY(i) / mHeight;
|
|
|
|
+ p = event.getPressure(i);
|
|
|
|
+ if (p > 1.0f) {
|
|
|
|
+ // may be larger than 1.0f on some devices
|
|
|
|
+ // see the documentation of getPressure(i)
|
|
|
|
+ p = 1.0f;
|
|
|
|
+ }
|
|
|
|
+ SDLActivity.onNativeTouch(touchDevId, pointerFingerId, MotionEvent.ACTION_UP, x, y, p);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
return true;
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
// Sensor events
|
|
// Sensor events
|
|
public void enableSensor(int sensortype, boolean enabled) {
|
|
public void enableSensor(int sensortype, boolean enabled) {
|
|
// TODO: This uses getDefaultSensor - what if we have >1 accels?
|
|
// TODO: This uses getDefaultSensor - what if we have >1 accels?
|
|
if (enabled) {
|
|
if (enabled) {
|
|
- mSensorManager.registerListener(this,
|
|
|
|
- mSensorManager.getDefaultSensor(sensortype),
|
|
|
|
|
|
+ mSensorManager.registerListener(this,
|
|
|
|
+ mSensorManager.getDefaultSensor(sensortype),
|
|
SensorManager.SENSOR_DELAY_GAME, null);
|
|
SensorManager.SENSOR_DELAY_GAME, null);
|
|
} else {
|
|
} else {
|
|
- mSensorManager.unregisterListener(this,
|
|
|
|
|
|
+ mSensorManager.unregisterListener(this,
|
|
mSensorManager.getDefaultSensor(sensortype));
|
|
mSensorManager.getDefaultSensor(sensortype));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
|
// TODO
|
|
// TODO
|
|
@@ -1131,7 +1308,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
|
|
y / SensorManager.GRAVITY_EARTH,
|
|
y / SensorManager.GRAVITY_EARTH,
|
|
event.values[2] / SensorManager.GRAVITY_EARTH - 1);
|
|
event.values[2] / SensorManager.GRAVITY_EARTH - 1);
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/* This is a fake invisible editor view that receives the input and defines the
|
|
/* This is a fake invisible editor view that receives the input and defines the
|
|
@@ -1173,7 +1350,7 @@ class DummyEdit extends View implements View.OnKeyListener {
|
|
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
//
|
|
//
|
|
@Override
|
|
@Override
|
|
public boolean onKeyPreIme (int keyCode, KeyEvent event) {
|
|
public boolean onKeyPreIme (int keyCode, KeyEvent event) {
|
|
@@ -1252,7 +1429,7 @@ class SDLInputConnection extends BaseInputConnection {
|
|
public native void nativeSetComposingText(String text, int newCursorPosition);
|
|
public native void nativeSetComposingText(String text, int newCursorPosition);
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public boolean deleteSurroundingText(int beforeLength, int afterLength) {
|
|
|
|
|
|
+ public boolean deleteSurroundingText(int beforeLength, int afterLength) {
|
|
// Workaround to capture backspace key. Ref: http://stackoverflow.com/questions/14560344/android-backspace-in-webview-baseinputconnection
|
|
// Workaround to capture backspace key. Ref: http://stackoverflow.com/questions/14560344/android-backspace-in-webview-baseinputconnection
|
|
if (beforeLength == 1 && afterLength == 0) {
|
|
if (beforeLength == 1 && afterLength == 0) {
|
|
// backspace
|
|
// backspace
|
|
@@ -1266,7 +1443,7 @@ class SDLInputConnection extends BaseInputConnection {
|
|
|
|
|
|
/* A null joystick handler for API level < 12 devices (the accelerometer is handled separately) */
|
|
/* A null joystick handler for API level < 12 devices (the accelerometer is handled separately) */
|
|
class SDLJoystickHandler {
|
|
class SDLJoystickHandler {
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Handles given MotionEvent.
|
|
* Handles given MotionEvent.
|
|
* @param event the event to be handled.
|
|
* @param event the event to be handled.
|
|
@@ -1298,11 +1475,11 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
return arg0.getAxis() - arg1.getAxis();
|
|
return arg0.getAxis() - arg1.getAxis();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private ArrayList<SDLJoystick> mJoysticks;
|
|
private ArrayList<SDLJoystick> mJoysticks;
|
|
-
|
|
|
|
|
|
+
|
|
public SDLJoystickHandler_API12() {
|
|
public SDLJoystickHandler_API12() {
|
|
-
|
|
|
|
|
|
+
|
|
mJoysticks = new ArrayList<SDLJoystick>();
|
|
mJoysticks = new ArrayList<SDLJoystick>();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1313,18 +1490,24 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
// For example, in the case of the XBox 360 wireless dongle,
|
|
// For example, in the case of the XBox 360 wireless dongle,
|
|
// so the first controller seen by SDL matches what the receiver
|
|
// so the first controller seen by SDL matches what the receiver
|
|
// considers to be the first controller
|
|
// considers to be the first controller
|
|
-
|
|
|
|
|
|
+
|
|
for(int i=deviceIds.length-1; i>-1; i--) {
|
|
for(int i=deviceIds.length-1; i>-1; i--) {
|
|
SDLJoystick joystick = getJoystick(deviceIds[i]);
|
|
SDLJoystick joystick = getJoystick(deviceIds[i]);
|
|
if (joystick == null) {
|
|
if (joystick == null) {
|
|
joystick = new SDLJoystick();
|
|
joystick = new SDLJoystick();
|
|
InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
|
|
InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
|
|
- if( (joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
|
|
|
|
|
|
+
|
|
|
|
+ if (
|
|
|
|
+ (joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0
|
|
|
|
+ ||
|
|
|
|
+ (joystickDevice.getSources() & InputDevice.SOURCE_CLASS_BUTTON) != 0
|
|
|
|
+ )
|
|
|
|
+ {
|
|
joystick.device_id = deviceIds[i];
|
|
joystick.device_id = deviceIds[i];
|
|
joystick.name = joystickDevice.getName();
|
|
joystick.name = joystickDevice.getName();
|
|
joystick.axes = new ArrayList<InputDevice.MotionRange>();
|
|
joystick.axes = new ArrayList<InputDevice.MotionRange>();
|
|
joystick.hats = new ArrayList<InputDevice.MotionRange>();
|
|
joystick.hats = new ArrayList<InputDevice.MotionRange>();
|
|
-
|
|
|
|
|
|
+
|
|
List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges();
|
|
List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges();
|
|
Collections.sort(ranges, new RangeComparator());
|
|
Collections.sort(ranges, new RangeComparator());
|
|
for (InputDevice.MotionRange range : ranges ) {
|
|
for (InputDevice.MotionRange range : ranges ) {
|
|
@@ -1338,14 +1521,14 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
mJoysticks.add(joystick);
|
|
mJoysticks.add(joystick);
|
|
- SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1,
|
|
|
|
|
|
+ SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1,
|
|
joystick.axes.size(), joystick.hats.size()/2, 0);
|
|
joystick.axes.size(), joystick.hats.size()/2, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/* Check removed devices */
|
|
/* Check removed devices */
|
|
ArrayList<Integer> removedDevices = new ArrayList<Integer>();
|
|
ArrayList<Integer> removedDevices = new ArrayList<Integer>();
|
|
for(int i=0; i < mJoysticks.size(); i++) {
|
|
for(int i=0; i < mJoysticks.size(); i++) {
|
|
@@ -1358,7 +1541,7 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
removedDevices.add(Integer.valueOf(device_id));
|
|
removedDevices.add(Integer.valueOf(device_id));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
for(int i=0; i < removedDevices.size(); i++) {
|
|
for(int i=0; i < removedDevices.size(); i++) {
|
|
int device_id = removedDevices.get(i).intValue();
|
|
int device_id = removedDevices.get(i).intValue();
|
|
SDLActivity.nativeRemoveJoystick(device_id);
|
|
SDLActivity.nativeRemoveJoystick(device_id);
|
|
@@ -1368,9 +1551,9 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
protected SDLJoystick getJoystick(int device_id) {
|
|
protected SDLJoystick getJoystick(int device_id) {
|
|
for(int i=0; i < mJoysticks.size(); i++) {
|
|
for(int i=0; i < mJoysticks.size(); i++) {
|
|
if (mJoysticks.get(i).device_id == device_id) {
|
|
if (mJoysticks.get(i).device_id == device_id) {
|
|
@@ -1378,9 +1561,9 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
public boolean handleMotionEvent(MotionEvent event) {
|
|
public boolean handleMotionEvent(MotionEvent event) {
|
|
if ( (event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
|
|
if ( (event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
|
|
int actionPointerIndex = event.getActionIndex();
|
|
int actionPointerIndex = event.getActionIndex();
|
|
@@ -1394,7 +1577,7 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
/* Normalize the value to -1...1 */
|
|
/* Normalize the value to -1...1 */
|
|
float value = ( event.getAxisValue( range.getAxis(), actionPointerIndex) - range.getMin() ) / range.getRange() * 2.0f - 1.0f;
|
|
float value = ( event.getAxisValue( range.getAxis(), actionPointerIndex) - range.getMin() ) / range.getRange() * 2.0f - 1.0f;
|
|
SDLActivity.onNativeJoy(joystick.device_id, i, value );
|
|
SDLActivity.onNativeJoy(joystick.device_id, i, value );
|
|
- }
|
|
|
|
|
|
+ }
|
|
for (int i = 0; i < joystick.hats.size(); i+=2) {
|
|
for (int i = 0; i < joystick.hats.size(); i+=2) {
|
|
int hatX = Math.round(event.getAxisValue( joystick.hats.get(i).getAxis(), actionPointerIndex ) );
|
|
int hatX = Math.round(event.getAxisValue( joystick.hats.get(i).getAxis(), actionPointerIndex ) );
|
|
int hatY = Math.round(event.getAxisValue( joystick.hats.get(i+1).getAxis(), actionPointerIndex ) );
|
|
int hatY = Math.round(event.getAxisValue( joystick.hats.get(i+1).getAxis(), actionPointerIndex ) );
|
|
@@ -1407,14 +1590,49 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
|
|
class SDLGenericMotionListener_API12 implements View.OnGenericMotionListener {
|
|
// Generic Motion (mouse hover, joystick...) events go here
|
|
// Generic Motion (mouse hover, joystick...) events go here
|
|
- // We only have joysticks yet
|
|
|
|
@Override
|
|
@Override
|
|
public boolean onGenericMotion(View v, MotionEvent event) {
|
|
public boolean onGenericMotion(View v, MotionEvent event) {
|
|
- return SDLActivity.handleJoystickMotionEvent(event);
|
|
|
|
|
|
+ float x, y;
|
|
|
|
+ int mouseButton;
|
|
|
|
+ int action;
|
|
|
|
+
|
|
|
|
+ switch ( event.getSource() ) {
|
|
|
|
+ case InputDevice.SOURCE_JOYSTICK:
|
|
|
|
+ case InputDevice.SOURCE_GAMEPAD:
|
|
|
|
+ case InputDevice.SOURCE_DPAD:
|
|
|
|
+ SDLActivity.handleJoystickMotionEvent(event);
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ case InputDevice.SOURCE_MOUSE:
|
|
|
|
+ action = event.getActionMasked();
|
|
|
|
+ switch (action) {
|
|
|
|
+ case MotionEvent.ACTION_SCROLL:
|
|
|
|
+ x = event.getAxisValue(MotionEvent.AXIS_HSCROLL, 0);
|
|
|
|
+ y = event.getAxisValue(MotionEvent.AXIS_VSCROLL, 0);
|
|
|
|
+ SDLActivity.onNativeMouse(0, action, x, y);
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ case MotionEvent.ACTION_HOVER_MOVE:
|
|
|
|
+ x = event.getX(0);
|
|
|
|
+ y = event.getY(0);
|
|
|
|
+
|
|
|
|
+ SDLActivity.onNativeMouse(0, action, x, y);
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Event was not managed
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
}
|
|
}
|