QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SDLPlatform.cc
Go to the documentation of this file.
1#include "SDLPlatform.h"
2
3#include <functional>
4
5#ifdef Q_OS_ANDROID
6#include <QtCore/QJniObject>
7#endif
8
9#include <SDL3/SDL.h>
10
11namespace SDLPlatform {
12
13// ============================================================================
14// Version Info
15// ============================================================================
16
17QString getVersion()
18{
19 int version = SDL_GetVersion();
20 int major = SDL_VERSIONNUM_MAJOR(version);
21 int minor = SDL_VERSIONNUM_MINOR(version);
22 int micro = SDL_VERSIONNUM_MICRO(version);
23 return QStringLiteral("%1.%2.%3").arg(major).arg(minor).arg(micro);
24}
25
26QString getRevision()
27{
28 const char *rev = SDL_GetRevision();
29 return rev ? QString::fromUtf8(rev) : QString();
30}
31
32// ============================================================================
33// Hints Management
34// ============================================================================
35
36bool setHint(const QString &name, const QString &value)
37{
38 return SDL_SetHint(qPrintable(name), qPrintable(value));
39}
40
41QString getHint(const QString &name)
42{
43 const char *value = SDL_GetHint(qPrintable(name));
44 return value ? QString::fromUtf8(value) : QString();
45}
46
47bool getHintBoolean(const QString &name, bool defaultValue)
48{
49 return SDL_GetHintBoolean(qPrintable(name), defaultValue);
50}
51
52bool resetHint(const QString &name)
53{
54 return SDL_ResetHint(qPrintable(name));
55}
56
58{
59 SDL_ResetHints();
60}
61
62// ============================================================================
63// Platform / Capability Queries
64// ============================================================================
65
66QString getPlatform()
67{
68 return QString::fromUtf8(SDL_GetPlatform());
69}
70
72{
73#ifdef Q_OS_ANDROID
74 return true;
75#else
76 return false;
77#endif
78}
79
80bool isIOS()
81{
82#ifdef Q_OS_IOS
83 return true;
84#else
85 return false;
86#endif
87}
88
90{
91#ifdef Q_OS_WIN
92 return true;
93#else
94 return false;
95#endif
96}
97
98bool isMacOS()
99{
100#ifdef Q_OS_MACOS
101 return true;
102#else
103 return false;
104#endif
105}
106
108{
109#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
110 return true;
111#else
112 return false;
113#endif
114}
115
117{
118#ifdef Q_OS_ANDROID
119 return QJniObject::callStaticMethod<jboolean>("org/libsdl/app/SDLActivity", "isTablet", "()Z");
120#else
121 return false;
122#endif
123}
124
125bool isTV()
126{
127#ifdef Q_OS_ANDROID
128 return QJniObject::callStaticMethod<jboolean>("org/libsdl/app/SDLActivity", "isAndroidTV", "()Z");
129#else
130 return false;
131#endif
132}
133
135{
136#ifdef Q_OS_ANDROID
137 return QJniObject::callStaticMethod<jboolean>("org/libsdl/app/SDLActivity", "isDeXMode", "()Z");
138#else
139 return false;
140#endif
141}
142
144{
145#ifdef Q_OS_ANDROID
146 return QJniObject::callStaticMethod<jboolean>("org/libsdl/app/SDLActivity", "isChromebook", "()Z");
147#else
148 return false;
149#endif
150}
151
152// ============================================================================
153// System Info
154// ============================================================================
155
157{
158 return SDL_GetNumLogicalCPUCores();
159}
160
162{
163 return SDL_GetSystemRAM();
164}
165
166// ============================================================================
167// Android-Specific
168// ============================================================================
169
171{
172#ifdef Q_OS_ANDROID
173 return SDL_GetAndroidSDKVersion();
174#else
175 return 0;
176#endif
177}
178
180{
181#ifdef Q_OS_ANDROID
182 const char *path = SDL_GetAndroidInternalStoragePath();
183 return path ? QString::fromUtf8(path) : QString();
184#else
185 return QString();
186#endif
187}
188
190{
191#ifdef Q_OS_ANDROID
192 const char *path = SDL_GetAndroidExternalStoragePath();
193 return path ? QString::fromUtf8(path) : QString();
194#else
195 return QString();
196#endif
197}
198
200{
201#ifdef Q_OS_ANDROID
202 Uint32 state = SDL_GetAndroidExternalStorageState();
203 int result = StorageStateNone;
204 if (state & SDL_ANDROID_EXTERNAL_STORAGE_READ) {
205 result |= StorageStateRead;
206 }
207 if (state & SDL_ANDROID_EXTERNAL_STORAGE_WRITE) {
208 result |= StorageStateWrite;
209 }
210 return result;
211#else
212 return StorageStateNone;
213#endif
214}
215
216#ifdef Q_OS_ANDROID
217static void androidPermissionCallback(void *userdata, const char * /*permission*/, bool granted)
218{
219 auto *callback = static_cast<std::function<void(bool)>*>(userdata);
220 if (callback) {
221 (*callback)(granted);
222 delete callback;
223 }
224}
225#endif
226
227bool requestAndroidPermission(const QString &permission, std::function<void(bool granted)> callback)
228{
229#ifdef Q_OS_ANDROID
230 if (!callback) {
231 return false;
232 }
233 auto *callbackPtr = new std::function<void(bool)>(std::move(callback));
234 if (!SDL_RequestAndroidPermission(qPrintable(permission), androidPermissionCallback, callbackPtr)) {
235 delete callbackPtr;
236 return false;
237 }
238 return true;
239#else
240 Q_UNUSED(permission);
241 if (callback) {
242 callback(true);
243 }
244 return true;
245#endif
246}
247
248// ============================================================================
249// Device Power Info
250// ============================================================================
251
252QString getDevicePowerInfo(int *seconds, int *percent)
253{
254 int sec = -1;
255 int pct = -1;
256 SDL_PowerState state = SDL_GetPowerInfo(&sec, &pct);
257
258 if (seconds) {
259 *seconds = sec;
260 }
261 if (percent) {
262 *percent = pct;
263 }
264
265 switch (state) {
266 case SDL_POWERSTATE_ON_BATTERY:
267 return QStringLiteral("On Battery");
268 case SDL_POWERSTATE_NO_BATTERY:
269 return QStringLiteral("No Battery");
270 case SDL_POWERSTATE_CHARGING:
271 return QStringLiteral("Charging");
272 case SDL_POWERSTATE_CHARGED:
273 return QStringLiteral("Charged");
274 case SDL_POWERSTATE_ERROR:
275 return QStringLiteral("Error");
276 case SDL_POWERSTATE_UNKNOWN:
277 default:
278 return QStringLiteral("Unknown");
279 }
280}
281
282} // namespace SDLPlatform
bool isTV()
Check if running on a TV device.
int getAndroidExternalStorageState()
QString getHint(const QString &name)
Get an SDL hint value.
bool isMacOS()
bool isAndroid()
Check if we're running on a specific platform.
void resetHints()
Reset all hints to defaults.
bool resetHint(const QString &name)
Reset an SDL hint to default.
int getSystemRAM()
Get system RAM in MB.
bool isWindows()
bool setHint(const QString &name, const QString &value)
Set an SDL hint.
QString getDevicePowerInfo(int *seconds, int *percent)
bool requestAndroidPermission(const QString &permission, std::function< void(bool granted)> callback)
QString getAndroidInternalStoragePath()
QString getPlatform()
Get the current platform name.
bool getHintBoolean(const QString &name, bool defaultValue)
Get an SDL hint as boolean.
bool isTablet()
Check if tablet mode is active (Android/Windows)
bool isDeXMode()
Check if running in DeX mode (Samsung)
QString getAndroidExternalStoragePath()
int getNumLogicalCPUCores()
Get number of logical CPU cores.
int getAndroidSDKVersion()
QString getRevision()
Get SDL revision string (git hash)
bool isChromebook()
Check if running on a Chromebook (Chrome OS)
QString getVersion()
Get SDL version as string (e.g., "3.4.0")