OLD | NEW |
(Empty) | |
| 1 #include <android_native_app_glue.h> |
| 2 #include <cmath> |
| 3 |
| 4 #include "input_service.h" |
| 5 #include "bin/log.h" |
| 6 |
| 7 InputService::InputService(android_app* pApplication, |
| 8 VMGlue* pVmGlue, |
| 9 const int32_t& pWidth, |
| 10 const int32_t& pHeight) : |
| 11 mApplication(pApplication), |
| 12 mVmGlue(pVmGlue), |
| 13 mWidth(pWidth), |
| 14 mHeight(pHeight) { |
| 15 } |
| 16 |
| 17 int32_t InputService::start() { |
| 18 if ((mWidth == 0) || (mHeight == 0)) { |
| 19 return -1; |
| 20 } |
| 21 return 0; |
| 22 } |
| 23 |
| 24 bool InputService::onTouchEvent(AInputEvent* pEvent) { |
| 25 int32_t lType = AMotionEvent_getAction(pEvent); |
| 26 const char *lFunction = NULL; |
| 27 switch (lType) { |
| 28 case AMOTION_EVENT_ACTION_DOWN: |
| 29 lFunction = "onMotionDown"; |
| 30 break; |
| 31 case AMOTION_EVENT_ACTION_UP: |
| 32 lFunction = "onMotionUp"; |
| 33 break; |
| 34 case AMOTION_EVENT_ACTION_MOVE: |
| 35 lFunction = "onMotionMove"; |
| 36 break; |
| 37 case AMOTION_EVENT_ACTION_CANCEL: |
| 38 lFunction = "onMotionCancel"; |
| 39 break; |
| 40 case AMOTION_EVENT_ACTION_OUTSIDE: |
| 41 lFunction = "onMotionOutside"; |
| 42 break; |
| 43 case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 44 lFunction = "onMotionPointerDown"; |
| 45 break; |
| 46 case AMOTION_EVENT_ACTION_POINTER_UP: |
| 47 lFunction = "onMotionPointerUp"; |
| 48 break; |
| 49 default: |
| 50 break; |
| 51 } |
| 52 if (lFunction != NULL) { |
| 53 // For now we just get the last coords. |
| 54 float lMoveX = AMotionEvent_getX(pEvent, 0); |
| 55 float lMoveY = AMotionEvent_getY(pEvent, 0); |
| 56 int64_t lWhen = AMotionEvent_getEventTime(pEvent); |
| 57 Log::Print("Got motion event %d at %f, %f", lType, lMoveX, lMoveY); |
| 58 |
| 59 if (mVmGlue->onMotionEvent(lFunction, lWhen, lMoveX, lMoveY) != 0) { |
| 60 return false; |
| 61 } |
| 62 } else { |
| 63 return false; |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool InputService::onKeyEvent(AInputEvent* pEvent) { |
| 69 int32_t lType = AKeyEvent_getAction(pEvent); |
| 70 const char *lFunction = NULL; |
| 71 switch (lType) { |
| 72 case AKEY_EVENT_ACTION_DOWN: |
| 73 lFunction = "onKeyDown"; |
| 74 break; |
| 75 case AKEY_EVENT_ACTION_UP: |
| 76 lFunction = "onKeyUp"; |
| 77 break; |
| 78 case AKEY_EVENT_ACTION_MULTIPLE: |
| 79 lFunction = "onKeyMultiple"; |
| 80 break; |
| 81 } |
| 82 if (lFunction != NULL) { |
| 83 int32_t lFlags = AKeyEvent_getFlags(pEvent); |
| 84 /* Get the key code of the key event. |
| 85 * This is the physical key that was pressed, not the Unicode character. */ |
| 86 int32_t lKeyCode = AKeyEvent_getKeyCode(pEvent); |
| 87 /* Get the meta key state. */ |
| 88 int32_t lMetaState = AKeyEvent_getMetaState(pEvent); |
| 89 /* Get the repeat count of the event. |
| 90 * For both key up an key down events, this is the number of times the key |
| 91 * has repeated with the first down starting at 0 and counting up from |
| 92 * there. For multiple key events, this is the number of down/up pairs |
| 93 * that have occurred. */ |
| 94 int32_t lRepeat = AKeyEvent_getRepeatCount(pEvent); |
| 95 |
| 96 /* Get the time of the most recent key down event, in the |
| 97 * java.lang.System.nanoTime() time base. If this is a down event, |
| 98 * this will be the same as eventTime. |
| 99 * Note that when chording keys, this value is the down time of the most |
| 100 * recently pressed key, which may not be the same physical key of this |
| 101 * event. */ |
| 102 int64_t lKeyDownTime = AKeyEvent_getDownTime(pEvent); |
| 103 |
| 104 /* Get the time this event occurred, in the |
| 105 * java.lang.System.nanoTime() time base. */ |
| 106 int64_t lWhen = AKeyEvent_getEventTime(pEvent); |
| 107 |
| 108 Log::Print("Got key event %d %d", lType, lKeyCode); |
| 109 if (mVmGlue->onKeyEvent(lFunction, lWhen, lFlags, lKeyCode, |
| 110 lMetaState, lRepeat) != 0) { |
| 111 return false; |
| 112 } |
| 113 } else { |
| 114 return false; |
| 115 } |
| 116 return true; |
| 117 } |
OLD | NEW |