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