[Windows] Remove redundant DirectSound error codes
[xbmc.git] / xbmc / platform / android / activity / EventLoop.cpp
blobc8d9989ccc48912ac759931a657133d87aef1985
1 /*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "EventLoop.h"
11 #include "XBMCApp.h"
13 #define IS_FROM_SOURCE(v, s) ((v & s) == s)
15 CEventLoop::CEventLoop(android_app* application)
16 : m_application(application), m_activityHandler(NULL), m_inputHandler(NULL)
18 if (m_application == NULL)
19 return;
21 m_application->userData = this;
22 m_application->onAppCmd = activityCallback;
23 m_application->onInputEvent = inputCallback;
26 void CEventLoop::run(IActivityHandler& activityHandler, IInputHandler& inputHandler)
28 m_activityHandler = &activityHandler;
29 m_inputHandler = &inputHandler;
31 CXBMCApp::android_printf("CEventLoop: starting event loop");
33 while (!m_application->destroyRequested)
35 android_poll_source* source = nullptr;
36 int result = ALooper_pollOnce(-1, nullptr, nullptr, reinterpret_cast<void**>(&source));
38 if (result == ALOOPER_POLL_ERROR)
40 CXBMCApp::android_printf("CEventLoop: ALooper_pollOnce returned an error");
41 break;
44 // Process this event.
45 if (source != nullptr)
46 source->process(m_application, source);
49 CXBMCApp::android_printf("CEventLoop: we are being destroyed");
52 void CEventLoop::processActivity(int32_t command)
54 switch (command)
56 case APP_CMD_CONFIG_CHANGED:
57 m_activityHandler->onConfigurationChanged();
58 break;
60 case APP_CMD_INIT_WINDOW:
61 // The window is being shown, get it ready.
62 m_activityHandler->onCreateWindow(m_application->window);
64 // set the proper DPI value
65 m_inputHandler->setDPI(CXBMCApp::Get().GetDPI());
66 break;
68 case APP_CMD_WINDOW_RESIZED:
69 // The window has been resized
70 m_activityHandler->onResizeWindow();
71 break;
73 case APP_CMD_TERM_WINDOW:
74 // The window is being hidden or closed, clean it up.
75 m_activityHandler->onDestroyWindow();
76 break;
78 case APP_CMD_GAINED_FOCUS:
79 m_activityHandler->onGainFocus();
80 break;
82 case APP_CMD_LOST_FOCUS:
83 m_activityHandler->onLostFocus();
84 break;
86 case APP_CMD_LOW_MEMORY:
87 m_activityHandler->onLowMemory();
88 break;
90 case APP_CMD_START:
91 m_activityHandler->onStart();
92 break;
94 case APP_CMD_RESUME:
95 m_activityHandler->onResume();
96 break;
98 case APP_CMD_SAVE_STATE:
99 // The system has asked us to save our current state. Do so.
100 m_activityHandler->onSaveState(&m_application->savedState, &m_application->savedStateSize);
101 break;
103 case APP_CMD_PAUSE:
104 m_activityHandler->onPause();
105 break;
107 case APP_CMD_STOP:
108 m_activityHandler->onStop();
109 break;
111 case APP_CMD_DESTROY:
112 m_activityHandler->onDestroy();
113 break;
115 default:
116 break;
120 int32_t CEventLoop::processInput(AInputEvent* event)
122 int32_t rtn = 0;
123 int32_t type = AInputEvent_getType(event);
124 int32_t source = AInputEvent_getSource(event);
126 // handle joystick input
127 if (IS_FROM_SOURCE(source, AINPUT_SOURCE_GAMEPAD) || IS_FROM_SOURCE(source, AINPUT_SOURCE_JOYSTICK))
129 if (m_inputHandler->onJoyStickEvent(event))
130 return true;
133 switch(type)
135 case AINPUT_EVENT_TYPE_KEY:
136 rtn = m_inputHandler->onKeyboardEvent(event);
137 break;
138 case AINPUT_EVENT_TYPE_MOTION:
139 if (IS_FROM_SOURCE(source, AINPUT_SOURCE_TOUCHSCREEN))
140 rtn = m_inputHandler->onTouchEvent(event);
141 else if (IS_FROM_SOURCE(source, AINPUT_SOURCE_MOUSE))
142 rtn = m_inputHandler->onMouseEvent(event);
143 break;
146 return rtn;
149 void CEventLoop::activityCallback(android_app* application, int32_t command)
151 if (application == NULL || application->userData == NULL)
152 return;
154 CEventLoop& eventLoop = *((CEventLoop*)application->userData);
155 eventLoop.processActivity(command);
158 int32_t CEventLoop::inputCallback(android_app* application, AInputEvent* event)
160 if (application == NULL || application->userData == NULL || event == NULL)
161 return 0;
163 CEventLoop& eventLoop = *((CEventLoop*)application->userData);
165 return eventLoop.processInput(event);