2 * Copyright (C) 2012-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "EventLoop.h"
23 #include "AndroidExtra.h"
25 CEventLoop::CEventLoop(android_app* application)
27 m_application(application),
28 m_activityHandler(NULL), m_inputHandler(NULL)
30 if (m_application == NULL)
33 m_application->userData = this;
34 m_application->onAppCmd = activityCallback;
35 m_application->onInputEvent = inputCallback;
38 void CEventLoop::run(IActivityHandler &activityHandler, IInputHandler &inputHandler)
42 struct android_poll_source* source;
44 m_activityHandler = &activityHandler;
45 m_inputHandler = &inputHandler;
47 CXBMCApp::android_printf("CEventLoop: starting event loop");
50 // We will block forever waiting for events.
51 while ((ident = ALooper_pollAll(-1, NULL, &events, (void**)&source)) >= 0)
53 // Process this event.
55 source->process(m_application, source);
57 // Check if we are exiting.
58 if (m_application->destroyRequested)
60 CXBMCApp::android_printf("CEventLoop: we are being destroyed");
67 void CEventLoop::processActivity(int32_t command)
71 case APP_CMD_CONFIG_CHANGED:
72 m_activityHandler->onConfigurationChanged();
75 case APP_CMD_INIT_WINDOW:
76 // The window is being shown, get it ready.
77 m_activityHandler->onCreateWindow(m_application->window);
79 // set the proper DPI value
80 m_inputHandler->setDPI(CXBMCApp::GetDPI());
83 case APP_CMD_WINDOW_RESIZED:
84 // The window has been resized
85 m_activityHandler->onResizeWindow();
88 case APP_CMD_TERM_WINDOW:
89 // The window is being hidden or closed, clean it up.
90 m_activityHandler->onDestroyWindow();
93 case APP_CMD_GAINED_FOCUS:
94 m_activityHandler->onGainFocus();
97 case APP_CMD_LOST_FOCUS:
98 m_activityHandler->onLostFocus();
101 case APP_CMD_LOW_MEMORY:
102 m_activityHandler->onLowMemory();
106 m_activityHandler->onStart();
110 m_activityHandler->onResume();
113 case APP_CMD_SAVE_STATE:
114 // The system has asked us to save our current state. Do so.
115 m_activityHandler->onSaveState(&m_application->savedState, &m_application->savedStateSize);
119 m_activityHandler->onPause();
123 m_activityHandler->onStop();
126 case APP_CMD_DESTROY:
127 m_activityHandler->onDestroy();
135 int32_t CEventLoop::processInput(AInputEvent* event)
138 int32_t type = AInputEvent_getType(event);
139 int32_t source = AInputEvent_getSource(event);
140 int32_t repeat = AKeyEvent_getRepeatCount(event);
141 int32_t keycod = AKeyEvent_getKeyCode(event);
145 case AINPUT_EVENT_TYPE_KEY:
146 if (source & AINPUT_SOURCE_GAMEPAD || source & AINPUT_SOURCE_JOYSTICK)
148 if (m_inputHandler->onJoyStickKeyEvent(event))
151 if (source & AINPUT_SOURCE_CLASS_BUTTON)
152 rtn = m_inputHandler->onKeyboardEvent(event);
154 case AINPUT_EVENT_TYPE_MOTION:
157 case AINPUT_SOURCE_TOUCHSCREEN:
158 rtn = m_inputHandler->onTouchEvent(event);
160 case AINPUT_SOURCE_MOUSE:
161 rtn = m_inputHandler->onMouseEvent(event);
163 case AINPUT_SOURCE_GAMEPAD:
164 case AINPUT_SOURCE_JOYSTICK:
165 rtn = m_inputHandler->onJoyStickMotionEvent(event);
174 void CEventLoop::activityCallback(android_app* application, int32_t command)
176 if (application == NULL || application->userData == NULL)
179 CEventLoop& eventLoop = *((CEventLoop*)application->userData);
180 eventLoop.processActivity(command);
183 int32_t CEventLoop::inputCallback(android_app* application, AInputEvent* event)
185 if (application == NULL || application->userData == NULL || event == NULL)
188 CEventLoop& eventLoop = *((CEventLoop*)application->userData);
190 return eventLoop.processInput(event);