Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: samples/android_sample/jni/eventloop.cc

Issue 11434046: Android rayshader sample. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #include "eventloop.h"
2 #include "bin/log.h"
3
4 EventLoop::EventLoop(android_app* pApplication) :
5 mEnabled(false),
6 mQuit(false),
7 mApplication(pApplication),
8 mActivityHandler(NULL),
9 mInputHandler(NULL) {
10 mApplication->onAppCmd = activityCallback;
11 mApplication->onInputEvent = inputCallback;
12 mApplication->userData = this;
13 }
14
15 void EventLoop::run(ActivityHandler* pActivityHandler,
16 Context* pContext) {
17 int32_t lResult;
18 int32_t lEvents;
19 android_poll_source* lSource;
20
21 app_dummy();
22 mActivityHandler = pActivityHandler;
23 mInputHandler = pContext->mInputHandler;
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 ((lResult = ALooper_pollAll(mEnabled ? 0 : -1,
29 NULL,
30 &lEvents,
31 (void**)&lSource)) >= 0) {
32 if (lSource != NULL) {
33 lSource->process(mApplication, lSource);
34 }
35 if (mApplication->destroyRequested) {
36 return;
37 }
38 }
39 if (mEnabled && !mQuit) {
40 Log::Print("step");
41 if (mActivityHandler->onStep() != 0) {
42 mQuit = true;
43 ANativeActivity_finish(mApplication->activity);
44 }
45 }
46 }
47 }
48
49 // Called when we gain focus.
50 void EventLoop::activate() {
51 Log::Print("activate");
52 if (!mEnabled && mApplication->window != NULL) {
53 mQuit = false;
54 mEnabled = true;
55 if (mActivityHandler->onActivate() != 0) {
56 mQuit = true;
57 ANativeActivity_finish(mApplication->activity);
58 }
59 }
60 }
61
62 // Called when we lose focus.
63 void EventLoop::deactivate() {
64 Log::Print("deactivate");
65 if (mEnabled) {
66 mActivityHandler->onDeactivate();
67 mEnabled = false;
68 }
69 }
70
71 void EventLoop::processActivityEvent(int32_t pCommand) {
72 switch (pCommand) {
73 case APP_CMD_CONFIG_CHANGED:
74 mActivityHandler->onConfigurationChanged();
75 break;
76 case APP_CMD_INIT_WINDOW:
77 mActivityHandler->onCreateWindow();
78 break;
79 case APP_CMD_DESTROY:
80 mActivityHandler->onDestroy();
81 break;
82 case APP_CMD_GAINED_FOCUS:
83 activate();
84 mActivityHandler->onGainedFocus();
85 break;
86 case APP_CMD_LOST_FOCUS:
87 mActivityHandler->onLostFocus();
88 deactivate();
89 break;
90 case APP_CMD_LOW_MEMORY:
91 mActivityHandler->onLowMemory();
92 break;
93 case APP_CMD_PAUSE:
94 mActivityHandler->onPause();
95 deactivate();
96 break;
97 case APP_CMD_RESUME:
98 mActivityHandler->onResume();
99 break;
100 case APP_CMD_SAVE_STATE:
101 mActivityHandler->onSaveState(&mApplication->savedState,
102 &mApplication->savedStateSize);
103 break;
104 case APP_CMD_START:
105 mActivityHandler->onStart();
106 break;
107 case APP_CMD_STOP:
108 mActivityHandler->onStop();
109 break;
110 case APP_CMD_TERM_WINDOW:
111 mActivityHandler->onDestroyWindow();
112 deactivate();
113 break;
114 default:
115 break;
116 }
117 }
118
119 void EventLoop::activityCallback(android_app* pApplication, int32_t pCommand) {
120 EventLoop* pEventLoop = (EventLoop*)pApplication->userData;
121 pEventLoop->processActivityEvent(pCommand);
122 }
123
124 int32_t EventLoop::processInputEvent(AInputEvent* pEvent) {
125 int32_t lEventType = AInputEvent_getType(pEvent);
126 Log::Print("Got input event type %d", lEventType);
127 switch (lEventType) {
128 case AINPUT_EVENT_TYPE_MOTION:
129 if (AInputEvent_getSource(pEvent) == AINPUT_SOURCE_TOUCHSCREEN) {
130 return mInputHandler->onTouchEvent(pEvent);
131 }
132 break;
133 case AINPUT_EVENT_TYPE_KEY:
134 return mInputHandler->onKeyEvent(pEvent);
135 }
136 return 0;
137 }
138
139 int32_t EventLoop::inputCallback(android_app* pApplication,
140 AInputEvent* pEvent) {
141 EventLoop& lEventLoop = *(EventLoop*) pApplication->userData;
142 return lEventLoop.processInputEvent(pEvent);
143 }
144
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698