OLD | NEW |
(Empty) | |
| 1 #include "jni/eventloop.h" |
| 2 #include "bin/log.h" |
| 3 |
| 4 EventLoop::EventLoop(android_app* application) |
| 5 : enabled_(false), |
| 6 quit_(false), |
| 7 application_(application), |
| 8 activity_handler_(NULL), |
| 9 input_handler_(NULL) { |
| 10 application_->onAppCmd = ActivityCallback; |
| 11 application_->onInputEvent = InputCallback; |
| 12 application_->userData = this; |
| 13 } |
| 14 |
| 15 void EventLoop::Run(ActivityHandler* activity_handler, |
| 16 Context* context) { |
| 17 int32_t result; |
| 18 int32_t events; |
| 19 android_poll_source* source; |
| 20 |
| 21 app_dummy(); |
| 22 activity_handler_ = activity_handler; |
| 23 input_handler_ = context->input_handler; |
| 24 Log::Print("Starting event loop"); |
| 25 while (true) { |
| 26 // If not enabled, block on events. If enabled, don't block |
| 27 // so we can do useful work in onStep. |
| 28 while ((result = ALooper_pollAll(enabled_ ? 0 : -1, |
| 29 NULL, |
| 30 &events, |
| 31 reinterpret_cast<void**>(&source))) >= 0) { |
| 32 if (source != NULL) { |
| 33 source->process(application_, source); |
| 34 } |
| 35 if (application_->destroyRequested) { |
| 36 return; |
| 37 } |
| 38 } |
| 39 if (enabled_ && !quit_) { |
| 40 Log::Print("step"); |
| 41 if (activity_handler_->OnStep() != 0) { |
| 42 quit_ = true; |
| 43 ANativeActivity_finish(application_->activity); |
| 44 } |
| 45 } |
| 46 } |
| 47 } |
| 48 |
| 49 // Called when we gain focus. |
| 50 void EventLoop::Activate() { |
| 51 Log::Print("activate"); |
| 52 if (!enabled_ && application_->window != NULL) { |
| 53 quit_ = false; |
| 54 enabled_ = true; |
| 55 if (activity_handler_->OnActivate() != 0) { |
| 56 quit_ = true; |
| 57 ANativeActivity_finish(application_->activity); |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 // Called when we lose focus. |
| 63 void EventLoop::Deactivate() { |
| 64 Log::Print("deactivate"); |
| 65 if (enabled_) { |
| 66 activity_handler_->OnDeactivate(); |
| 67 enabled_ = false; |
| 68 } |
| 69 } |
| 70 |
| 71 void EventLoop::ProcessActivityEvent(int32_t command) { |
| 72 switch (command) { |
| 73 case APP_CMD_CONFIG_CHANGED: |
| 74 activity_handler_->OnConfigurationChanged(); |
| 75 break; |
| 76 case APP_CMD_INIT_WINDOW: |
| 77 activity_handler_->OnCreateWindow(); |
| 78 break; |
| 79 case APP_CMD_DESTROY: |
| 80 activity_handler_->OnDestroy(); |
| 81 break; |
| 82 case APP_CMD_GAINED_FOCUS: |
| 83 Activate(); |
| 84 activity_handler_->OnGainedFocus(); |
| 85 break; |
| 86 case APP_CMD_LOST_FOCUS: |
| 87 activity_handler_->OnLostFocus(); |
| 88 Deactivate(); |
| 89 break; |
| 90 case APP_CMD_LOW_MEMORY: |
| 91 activity_handler_->OnLowMemory(); |
| 92 break; |
| 93 case APP_CMD_PAUSE: |
| 94 activity_handler_->OnPause(); |
| 95 Deactivate(); |
| 96 break; |
| 97 case APP_CMD_RESUME: |
| 98 activity_handler_->OnResume(); |
| 99 break; |
| 100 case APP_CMD_SAVE_STATE: |
| 101 activity_handler_->OnSaveState(&application_->savedState, |
| 102 &application_->savedStateSize); |
| 103 break; |
| 104 case APP_CMD_START: |
| 105 activity_handler_->OnStart(); |
| 106 break; |
| 107 case APP_CMD_STOP: |
| 108 activity_handler_->OnStop(); |
| 109 break; |
| 110 case APP_CMD_TERM_WINDOW: |
| 111 activity_handler_->OnDestroyWindow(); |
| 112 Deactivate(); |
| 113 break; |
| 114 default: |
| 115 break; |
| 116 } |
| 117 } |
| 118 |
| 119 int32_t EventLoop::ProcessInputEvent(AInputEvent* event) { |
| 120 int32_t event_type = AInputEvent_getType(event); |
| 121 Log::Print("Got input event type %d", event_type); |
| 122 switch (event_type) { |
| 123 case AINPUT_EVENT_TYPE_MOTION: |
| 124 if (AInputEvent_getSource(event) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 125 return input_handler_->OnTouchEvent(event); |
| 126 } |
| 127 break; |
| 128 case AINPUT_EVENT_TYPE_KEY: |
| 129 return input_handler_->OnKeyEvent(event); |
| 130 } |
| 131 return 0; |
| 132 } |
| 133 |
| 134 void EventLoop::ActivityCallback(android_app* application, int32_t command) { |
| 135 EventLoop* event_loop = reinterpret_cast<EventLoop*>(application->userData); |
| 136 event_loop->ProcessActivityEvent(command); |
| 137 } |
| 138 |
| 139 int32_t EventLoop::InputCallback(android_app* application, |
| 140 AInputEvent* event) { |
| 141 EventLoop* event_loop = reinterpret_cast<EventLoop*>(application->userData); |
| 142 return event_loop->ProcessInputEvent(event); |
| 143 } |
| 144 |
OLD | NEW |