| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 1 #include "jni/eventloop.h" | 5 #include "jni/eventloop.h" |
| 2 #include "bin/log.h" | 6 #include "jni/log.h" |
| 3 | 7 |
| 4 EventLoop::EventLoop(android_app* application) | 8 EventLoop::EventLoop(android_app* application) |
| 5 : enabled_(false), | 9 : enabled_(false), |
| 6 quit_(false), | 10 quit_(false), |
| 7 application_(application), | 11 application_(application), |
| 8 activity_handler_(NULL), | 12 activity_handler_(NULL), |
| 9 input_handler_(NULL) { | 13 input_handler_(NULL) { |
| 10 application_->onAppCmd = ActivityCallback; | 14 application_->onAppCmd = ActivityCallback; |
| 11 application_->onInputEvent = InputCallback; | 15 application_->onInputEvent = InputCallback; |
| 12 application_->userData = this; | 16 application_->userData = this; |
| 13 } | 17 } |
| 14 | 18 |
| 15 void EventLoop::Run(ActivityHandler* activity_handler, | 19 void EventLoop::Run(ActivityHandler* activity_handler, |
| 16 Context* context) { | 20 Context* context) { |
| 17 int32_t result; | 21 int32_t result; |
| 18 int32_t events; | 22 int32_t events; |
| 19 android_poll_source* source; | 23 android_poll_source* source; |
| 20 | 24 |
| 21 app_dummy(); | 25 app_dummy(); |
| 22 activity_handler_ = activity_handler; | 26 activity_handler_ = activity_handler; |
| 23 input_handler_ = context->input_handler; | 27 input_handler_ = context->input_handler; |
| 24 Log::Print("Starting event loop"); | 28 LOGI("Starting event loop"); |
| 25 while (true) { | 29 while (true) { |
| 26 // If not enabled, block on events. If enabled, don't block | 30 // If not enabled, block on events. If enabled, don't block |
| 27 // so we can do useful work in onStep. | 31 // so we can do useful work in onStep. |
| 28 while ((result = ALooper_pollAll(enabled_ ? 0 : -1, | 32 while ((result = ALooper_pollAll(enabled_ ? 0 : -1, |
| 29 NULL, | 33 NULL, |
| 30 &events, | 34 &events, |
| 31 reinterpret_cast<void**>(&source))) >= 0) { | 35 reinterpret_cast<void**>(&source))) >= 0) { |
| 32 if (source != NULL) { | 36 if (source != NULL) { |
| 33 source->process(application_, source); | 37 source->process(application_, source); |
| 34 } | 38 } |
| 35 if (application_->destroyRequested) { | 39 if (application_->destroyRequested) { |
| 36 return; | 40 return; |
| 37 } | 41 } |
| 38 } | 42 } |
| 39 if (enabled_ && !quit_) { | 43 if (enabled_ && !quit_) { |
| 40 Log::Print("step"); | 44 LOGI("step"); |
| 41 if (activity_handler_->OnStep() != 0) { | 45 if (activity_handler_->OnStep() != 0) { |
| 42 quit_ = true; | 46 quit_ = true; |
| 43 ANativeActivity_finish(application_->activity); | 47 ANativeActivity_finish(application_->activity); |
| 44 } | 48 } |
| 45 } | 49 } |
| 46 } | 50 } |
| 47 } | 51 } |
| 48 | 52 |
| 49 // Called when we gain focus. | 53 // Called when we gain focus. |
| 50 void EventLoop::Activate() { | 54 void EventLoop::Activate() { |
| 51 Log::Print("activate"); | 55 LOGI("activate"); |
| 52 if (!enabled_ && application_->window != NULL) { | 56 if (!enabled_ && application_->window != NULL) { |
| 53 quit_ = false; | 57 quit_ = false; |
| 54 enabled_ = true; | 58 enabled_ = true; |
| 55 if (activity_handler_->OnActivate() != 0) { | 59 if (activity_handler_->OnActivate() != 0) { |
| 56 quit_ = true; | 60 quit_ = true; |
| 57 ANativeActivity_finish(application_->activity); | 61 ANativeActivity_finish(application_->activity); |
| 58 } | 62 } |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 // Called when we lose focus. | 66 // Called when we lose focus. |
| 63 void EventLoop::Deactivate() { | 67 void EventLoop::Deactivate() { |
| 64 Log::Print("deactivate"); | 68 LOGI("deactivate"); |
| 65 if (enabled_) { | 69 if (enabled_) { |
| 66 activity_handler_->OnDeactivate(); | 70 activity_handler_->OnDeactivate(); |
| 67 enabled_ = false; | 71 enabled_ = false; |
| 68 } | 72 } |
| 69 } | 73 } |
| 70 | 74 |
| 71 void EventLoop::ProcessActivityEvent(int32_t command) { | 75 void EventLoop::ProcessActivityEvent(int32_t command) { |
| 72 switch (command) { | 76 switch (command) { |
| 73 case APP_CMD_CONFIG_CHANGED: | 77 case APP_CMD_CONFIG_CHANGED: |
| 74 activity_handler_->OnConfigurationChanged(); | 78 activity_handler_->OnConfigurationChanged(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 activity_handler_->OnDestroyWindow(); | 115 activity_handler_->OnDestroyWindow(); |
| 112 Deactivate(); | 116 Deactivate(); |
| 113 break; | 117 break; |
| 114 default: | 118 default: |
| 115 break; | 119 break; |
| 116 } | 120 } |
| 117 } | 121 } |
| 118 | 122 |
| 119 int32_t EventLoop::ProcessInputEvent(AInputEvent* event) { | 123 int32_t EventLoop::ProcessInputEvent(AInputEvent* event) { |
| 120 int32_t event_type = AInputEvent_getType(event); | 124 int32_t event_type = AInputEvent_getType(event); |
| 121 Log::Print("Got input event type %d", event_type); | 125 LOGI("Got input event type %d", event_type); |
| 122 switch (event_type) { | 126 switch (event_type) { |
| 123 case AINPUT_EVENT_TYPE_MOTION: | 127 case AINPUT_EVENT_TYPE_MOTION: |
| 124 if (AInputEvent_getSource(event) == AINPUT_SOURCE_TOUCHSCREEN) { | 128 if (AInputEvent_getSource(event) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 125 return input_handler_->OnTouchEvent(event); | 129 return input_handler_->OnTouchEvent(event); |
| 126 } | 130 } |
| 127 break; | 131 break; |
| 128 case AINPUT_EVENT_TYPE_KEY: | 132 case AINPUT_EVENT_TYPE_KEY: |
| 129 return input_handler_->OnKeyEvent(event); | 133 return input_handler_->OnKeyEvent(event); |
| 130 } | 134 } |
| 131 return 0; | 135 return 0; |
| 132 } | 136 } |
| 133 | 137 |
| 134 void EventLoop::ActivityCallback(android_app* application, int32_t command) { | 138 void EventLoop::ActivityCallback(android_app* application, int32_t command) { |
| 135 EventLoop* event_loop = reinterpret_cast<EventLoop*>(application->userData); | 139 EventLoop* event_loop = reinterpret_cast<EventLoop*>(application->userData); |
| 136 event_loop->ProcessActivityEvent(command); | 140 event_loop->ProcessActivityEvent(command); |
| 137 } | 141 } |
| 138 | 142 |
| 139 int32_t EventLoop::InputCallback(android_app* application, | 143 int32_t EventLoop::InputCallback(android_app* application, |
| 140 AInputEvent* event) { | 144 AInputEvent* event) { |
| 141 EventLoop* event_loop = reinterpret_cast<EventLoop*>(application->userData); | 145 EventLoop* event_loop = reinterpret_cast<EventLoop*>(application->userData); |
| 142 return event_loop->ProcessInputEvent(event); | 146 return event_loop->ProcessInputEvent(event); |
| 143 } | 147 } |
| 144 | |
| OLD | NEW |