Index: samples/android_sample/jni/input_service.cc |
=================================================================== |
--- samples/android_sample/jni/input_service.cc (revision 0) |
+++ samples/android_sample/jni/input_service.cc (revision 0) |
@@ -0,0 +1,117 @@ |
+#include <android_native_app_glue.h> |
+#include <cmath> |
+ |
+#include "input_service.h" |
+#include "bin/log.h" |
+ |
+InputService::InputService(android_app* pApplication, |
+ VMGlue* pVmGlue, |
+ const int32_t& pWidth, |
+ const int32_t& pHeight) : |
+ mApplication(pApplication), |
+ mVmGlue(pVmGlue), |
+ mWidth(pWidth), |
+ mHeight(pHeight) { |
+} |
+ |
+int32_t InputService::start() { |
+ if ((mWidth == 0) || (mHeight == 0)) { |
+ return -1; |
+ } |
+ return 0; |
+} |
+ |
+bool InputService::onTouchEvent(AInputEvent* pEvent) { |
+ int32_t lType = AMotionEvent_getAction(pEvent); |
+ const char *lFunction = NULL; |
+ switch (lType) { |
+ case AMOTION_EVENT_ACTION_DOWN: |
+ lFunction = "onMotionDown"; |
+ break; |
+ case AMOTION_EVENT_ACTION_UP: |
+ lFunction = "onMotionUp"; |
+ break; |
+ case AMOTION_EVENT_ACTION_MOVE: |
+ lFunction = "onMotionMove"; |
+ break; |
+ case AMOTION_EVENT_ACTION_CANCEL: |
+ lFunction = "onMotionCancel"; |
+ break; |
+ case AMOTION_EVENT_ACTION_OUTSIDE: |
+ lFunction = "onMotionOutside"; |
+ break; |
+ case AMOTION_EVENT_ACTION_POINTER_DOWN: |
+ lFunction = "onMotionPointerDown"; |
+ break; |
+ case AMOTION_EVENT_ACTION_POINTER_UP: |
+ lFunction = "onMotionPointerUp"; |
+ break; |
+ default: |
+ break; |
+ } |
+ if (lFunction != NULL) { |
+ // For now we just get the last coords. |
+ float lMoveX = AMotionEvent_getX(pEvent, 0); |
+ float lMoveY = AMotionEvent_getY(pEvent, 0); |
+ int64_t lWhen = AMotionEvent_getEventTime(pEvent); |
+ Log::Print("Got motion event %d at %f, %f", lType, lMoveX, lMoveY); |
+ |
+ if (mVmGlue->onMotionEvent(lFunction, lWhen, lMoveX, lMoveY) != 0) { |
+ return false; |
+ } |
+ } else { |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+bool InputService::onKeyEvent(AInputEvent* pEvent) { |
+ int32_t lType = AKeyEvent_getAction(pEvent); |
+ const char *lFunction = NULL; |
+ switch (lType) { |
+ case AKEY_EVENT_ACTION_DOWN: |
+ lFunction = "onKeyDown"; |
+ break; |
+ case AKEY_EVENT_ACTION_UP: |
+ lFunction = "onKeyUp"; |
+ break; |
+ case AKEY_EVENT_ACTION_MULTIPLE: |
+ lFunction = "onKeyMultiple"; |
+ break; |
+ } |
+ if (lFunction != NULL) { |
+ int32_t lFlags = AKeyEvent_getFlags(pEvent); |
+ /* Get the key code of the key event. |
+ * This is the physical key that was pressed, not the Unicode character. */ |
+ int32_t lKeyCode = AKeyEvent_getKeyCode(pEvent); |
+ /* Get the meta key state. */ |
+ int32_t lMetaState = AKeyEvent_getMetaState(pEvent); |
+ /* Get the repeat count of the event. |
+ * For both key up an key down events, this is the number of times the key |
+ * has repeated with the first down starting at 0 and counting up from |
+ * there. For multiple key events, this is the number of down/up pairs |
+ * that have occurred. */ |
+ int32_t lRepeat = AKeyEvent_getRepeatCount(pEvent); |
+ |
+ /* Get the time of the most recent key down event, in the |
+ * java.lang.System.nanoTime() time base. If this is a down event, |
+ * this will be the same as eventTime. |
+ * Note that when chording keys, this value is the down time of the most |
+ * recently pressed key, which may not be the same physical key of this |
+ * event. */ |
+ int64_t lKeyDownTime = AKeyEvent_getDownTime(pEvent); |
+ |
+ /* Get the time this event occurred, in the |
+ * java.lang.System.nanoTime() time base. */ |
+ int64_t lWhen = AKeyEvent_getEventTime(pEvent); |
+ |
+ Log::Print("Got key event %d %d", lType, lKeyCode); |
+ if (mVmGlue->onKeyEvent(lFunction, lWhen, lFlags, lKeyCode, |
+ lMetaState, lRepeat) != 0) { |
+ return false; |
+ } |
+ } else { |
+ return false; |
+ } |
+ return true; |
+} |