[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / platform / android / activity / EventLoop.cpp
blob47ecdad8463dcc28a1e128663a9c06f6a374dfbc
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 "AndroidExtra.h"
12 #include "XBMCApp.h"
14 #include <dlfcn.h>
16 #define IS_FROM_SOURCE(v, s) ((v & s) == s)
18 CEventLoop::CEventLoop(android_app* application)
19 : m_enabled(false),
20 m_application(application),
21 m_activityHandler(NULL), m_inputHandler(NULL)
23 if (m_application == NULL)
24 return;
26 m_application->userData = this;
27 m_application->onAppCmd = activityCallback;
28 m_application->onInputEvent = inputCallback;
31 void CEventLoop::run(IActivityHandler &activityHandler, IInputHandler &inputHandler)
33 int ident;
34 int events;
35 struct android_poll_source* source;
37 m_activityHandler = &activityHandler;
38 m_inputHandler = &inputHandler;
40 CXBMCApp::android_printf("CEventLoop: starting event loop");
41 while (true)
43 // We will block forever waiting for events.
44 while ((ident = ALooper_pollAll(-1, NULL, &events, (void**)&source)) >= 0)
46 // Process this event.
47 if (source != NULL)
48 source->process(m_application, source);
50 // Check if we are exiting.
51 if (m_application->destroyRequested)
53 CXBMCApp::android_printf("CEventLoop: we are being destroyed");
54 return;
60 void CEventLoop::processActivity(int32_t command)
62 switch (command)
64 case APP_CMD_CONFIG_CHANGED:
65 m_activityHandler->onConfigurationChanged();
66 break;
68 case APP_CMD_INIT_WINDOW:
69 // The window is being shown, get it ready.
70 m_activityHandler->onCreateWindow(m_application->window);
72 // set the proper DPI value
73 m_inputHandler->setDPI(CXBMCApp::Get().GetDPI());
74 break;
76 case APP_CMD_WINDOW_RESIZED:
77 // The window has been resized
78 m_activityHandler->onResizeWindow();
79 break;
81 case APP_CMD_TERM_WINDOW:
82 // The window is being hidden or closed, clean it up.
83 m_activityHandler->onDestroyWindow();
84 break;
86 case APP_CMD_GAINED_FOCUS:
87 m_activityHandler->onGainFocus();
88 break;
90 case APP_CMD_LOST_FOCUS:
91 m_activityHandler->onLostFocus();
92 break;
94 case APP_CMD_LOW_MEMORY:
95 m_activityHandler->onLowMemory();
96 break;
98 case APP_CMD_START:
99 m_activityHandler->onStart();
100 break;
102 case APP_CMD_RESUME:
103 m_activityHandler->onResume();
104 break;
106 case APP_CMD_SAVE_STATE:
107 // The system has asked us to save our current state. Do so.
108 m_activityHandler->onSaveState(&m_application->savedState, &m_application->savedStateSize);
109 break;
111 case APP_CMD_PAUSE:
112 m_activityHandler->onPause();
113 break;
115 case APP_CMD_STOP:
116 m_activityHandler->onStop();
117 break;
119 case APP_CMD_DESTROY:
120 m_activityHandler->onDestroy();
121 break;
123 default:
124 break;
128 int32_t CEventLoop::processInput(AInputEvent* event)
130 int32_t rtn = 0;
131 int32_t type = AInputEvent_getType(event);
132 int32_t source = AInputEvent_getSource(event);
134 // handle joystick input
135 if (IS_FROM_SOURCE(source, AINPUT_SOURCE_GAMEPAD) || IS_FROM_SOURCE(source, AINPUT_SOURCE_JOYSTICK))
137 if (m_inputHandler->onJoyStickEvent(event))
138 return true;
141 switch(type)
143 case AINPUT_EVENT_TYPE_KEY:
144 rtn = m_inputHandler->onKeyboardEvent(event);
145 break;
146 case AINPUT_EVENT_TYPE_MOTION:
147 if (IS_FROM_SOURCE(source, AINPUT_SOURCE_TOUCHSCREEN))
148 rtn = m_inputHandler->onTouchEvent(event);
149 else if (IS_FROM_SOURCE(source, AINPUT_SOURCE_MOUSE))
150 rtn = m_inputHandler->onMouseEvent(event);
151 break;
154 return rtn;
157 void CEventLoop::activityCallback(android_app* application, int32_t command)
159 if (application == NULL || application->userData == NULL)
160 return;
162 CEventLoop& eventLoop = *((CEventLoop*)application->userData);
163 eventLoop.processActivity(command);
166 int32_t CEventLoop::inputCallback(android_app* application, AInputEvent* event)
168 if (application == NULL || application->userData == NULL || event == NULL)
169 return 0;
171 CEventLoop& eventLoop = *((CEventLoop*)application->userData);
173 return eventLoop.processInput(event);