OLD | NEW |
(Empty) | |
| 1 #include <android_native_app_glue.h> |
| 2 #include <cmath> |
| 3 |
| 4 #include "bin/log.h" |
| 5 #include "jni/input_service.h" |
| 6 |
| 7 InputService::InputService(android_app* application, |
| 8 VMGlue* vm_glue, |
| 9 const int32_t& width, |
| 10 const int32_t& height) : |
| 11 application_(application), |
| 12 vm_glue_(vm_glue), |
| 13 width_(width), |
| 14 height_(height) { |
| 15 } |
| 16 |
| 17 int32_t InputService::Start() { |
| 18 if ((width_ == 0) || (height_ == 0)) { |
| 19 return -1; |
| 20 } |
| 21 return 0; |
| 22 } |
| 23 |
| 24 bool InputService::OnTouchEvent(AInputEvent* event) { |
| 25 int32_t type = AMotionEvent_getAction(event); |
| 26 const char *function = NULL; |
| 27 switch (type) { |
| 28 case AMOTION_EVENT_ACTION_DOWN: |
| 29 function = "onMotionDown"; |
| 30 break; |
| 31 case AMOTION_EVENT_ACTION_UP: |
| 32 function = "onMotionUp"; |
| 33 break; |
| 34 case AMOTION_EVENT_ACTION_MOVE: |
| 35 function = "onMotionMove"; |
| 36 break; |
| 37 case AMOTION_EVENT_ACTION_CANCEL: |
| 38 function = "onMotionCancel"; |
| 39 break; |
| 40 case AMOTION_EVENT_ACTION_OUTSIDE: |
| 41 function = "onMotionOutside"; |
| 42 break; |
| 43 case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 44 function = "onMotionPointerDown"; |
| 45 break; |
| 46 case AMOTION_EVENT_ACTION_POINTER_UP: |
| 47 function = "onMotionPointerUp"; |
| 48 break; |
| 49 default: |
| 50 break; |
| 51 } |
| 52 if (function != NULL) { |
| 53 // For now we just get the last coords. |
| 54 float move_x = AMotionEvent_getX(event, 0); |
| 55 float move_y = AMotionEvent_getY(event, 0); |
| 56 int64_t when = AMotionEvent_getEventTime(event); |
| 57 Log::Print("Got motion event %d at %f, %f", type, move_x, move_y); |
| 58 |
| 59 if (vm_glue_->OnMotionEvent(function, when, move_x, move_y) != 0) { |
| 60 return false; |
| 61 } |
| 62 } else { |
| 63 return false; |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool InputService::OnKeyEvent(AInputEvent* event) { |
| 69 int32_t type = AKeyEvent_getAction(event); |
| 70 const char *function = NULL; |
| 71 switch (type) { |
| 72 case AKEY_EVENT_ACTION_DOWN: |
| 73 function = "onKeyDown"; |
| 74 break; |
| 75 case AKEY_EVENT_ACTION_UP: |
| 76 function = "onKeyUp"; |
| 77 break; |
| 78 case AKEY_EVENT_ACTION_MULTIPLE: |
| 79 function = "onKeyMultiple"; |
| 80 break; |
| 81 } |
| 82 if (function != NULL) { |
| 83 int32_t flags = AKeyEvent_getFlags(event); |
| 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 key_code = AKeyEvent_getKeyCode(event); |
| 87 /* Get the meta key state. */ |
| 88 int32_t meta_state = AKeyEvent_getMetaState(event); |
| 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 repeat = AKeyEvent_getRepeatCount(event); |
| 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 key_down_time = AKeyEvent_getDownTime(event); |
| 103 |
| 104 /* Get the time this event occurred, in the |
| 105 * java.lang.System.nanoTime() time base. */ |
| 106 int64_t when = AKeyEvent_getEventTime(event); |
| 107 |
| 108 Log::Print("Got key event %d %d", type, key_code); |
| 109 if (vm_glue_->OnKeyEvent(function, when, flags, key_code, |
| 110 meta_state, repeat) != 0) { |
| 111 return false; |
| 112 } |
| 113 } else { |
| 114 return false; |
| 115 } |
| 116 return true; |
| 117 } |
| 118 |
OLD | NEW |