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.
11 #include "AndroidExtra.h"
16 #define IS_FROM_SOURCE(v, s) ((v & s) == s)
18 CEventLoop::CEventLoop(android_app
* application
)
20 m_application(application
),
21 m_activityHandler(NULL
), m_inputHandler(NULL
)
23 if (m_application
== NULL
)
26 m_application
->userData
= this;
27 m_application
->onAppCmd
= activityCallback
;
28 m_application
->onInputEvent
= inputCallback
;
31 void CEventLoop::run(IActivityHandler
&activityHandler
, IInputHandler
&inputHandler
)
35 struct android_poll_source
* source
;
37 m_activityHandler
= &activityHandler
;
38 m_inputHandler
= &inputHandler
;
40 CXBMCApp::android_printf("CEventLoop: starting event loop");
43 // We will block forever waiting for events.
44 while ((ident
= ALooper_pollAll(-1, NULL
, &events
, (void**)&source
)) >= 0)
46 // Process this event.
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");
60 void CEventLoop::processActivity(int32_t command
)
64 case APP_CMD_CONFIG_CHANGED
:
65 m_activityHandler
->onConfigurationChanged();
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());
76 case APP_CMD_WINDOW_RESIZED
:
77 // The window has been resized
78 m_activityHandler
->onResizeWindow();
81 case APP_CMD_TERM_WINDOW
:
82 // The window is being hidden or closed, clean it up.
83 m_activityHandler
->onDestroyWindow();
86 case APP_CMD_GAINED_FOCUS
:
87 m_activityHandler
->onGainFocus();
90 case APP_CMD_LOST_FOCUS
:
91 m_activityHandler
->onLostFocus();
94 case APP_CMD_LOW_MEMORY
:
95 m_activityHandler
->onLowMemory();
99 m_activityHandler
->onStart();
103 m_activityHandler
->onResume();
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
);
112 m_activityHandler
->onPause();
116 m_activityHandler
->onStop();
119 case APP_CMD_DESTROY
:
120 m_activityHandler
->onDestroy();
128 int32_t CEventLoop::processInput(AInputEvent
* event
)
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
))
143 case AINPUT_EVENT_TYPE_KEY
:
144 rtn
= m_inputHandler
->onKeyboardEvent(event
);
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
);
157 void CEventLoop::activityCallback(android_app
* application
, int32_t command
)
159 if (application
== NULL
|| application
->userData
== NULL
)
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
)
171 CEventLoop
& eventLoop
= *((CEventLoop
*)application
->userData
);
173 return eventLoop
.processInput(event
);