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.
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
)
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");
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
)
56 case APP_CMD_CONFIG_CHANGED
:
57 m_activityHandler
->onConfigurationChanged();
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());
68 case APP_CMD_WINDOW_RESIZED
:
69 // The window has been resized
70 m_activityHandler
->onResizeWindow();
73 case APP_CMD_TERM_WINDOW
:
74 // The window is being hidden or closed, clean it up.
75 m_activityHandler
->onDestroyWindow();
78 case APP_CMD_GAINED_FOCUS
:
79 m_activityHandler
->onGainFocus();
82 case APP_CMD_LOST_FOCUS
:
83 m_activityHandler
->onLostFocus();
86 case APP_CMD_LOW_MEMORY
:
87 m_activityHandler
->onLowMemory();
91 m_activityHandler
->onStart();
95 m_activityHandler
->onResume();
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
);
104 m_activityHandler
->onPause();
108 m_activityHandler
->onStop();
111 case APP_CMD_DESTROY
:
112 m_activityHandler
->onDestroy();
120 int32_t CEventLoop::processInput(AInputEvent
* event
)
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
))
135 case AINPUT_EVENT_TYPE_KEY
:
136 rtn
= m_inputHandler
->onKeyboardEvent(event
);
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
);
149 void CEventLoop::activityCallback(android_app
* application
, int32_t command
)
151 if (application
== NULL
|| application
->userData
== NULL
)
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
)
163 CEventLoop
& eventLoop
= *((CEventLoop
*)application
->userData
);
165 return eventLoop
.processInput(event
);