summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/core/unix
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/core/unix
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/core/unix')
-rw-r--r--contrib/SDL-3.2.8/src/core/unix/SDL_appid.c75
-rw-r--r--contrib/SDL-3.2.8/src/core/unix/SDL_appid.h30
-rw-r--r--contrib/SDL-3.2.8/src/core/unix/SDL_poll.c95
-rw-r--r--contrib/SDL-3.2.8/src/core/unix/SDL_poll.h33
4 files changed, 233 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/core/unix/SDL_appid.c b/contrib/SDL-3.2.8/src/core/unix/SDL_appid.c
new file mode 100644
index 0000000..996e216
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/core/unix/SDL_appid.c
@@ -0,0 +1,75 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "SDL_internal.h"
23
24#include "SDL_appid.h"
25#include <unistd.h>
26
27const char *SDL_GetExeName(void)
28{
29 static const char *proc_name = NULL;
30
31 // TODO: Use a fallback if BSD has no mounted procfs (OpenBSD has no procfs at all)
32 if (!proc_name) {
33#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_FREEBSD) || defined (SDL_PLATFORM_NETBSD)
34 static char linkfile[1024];
35 int linksize;
36
37#if defined(SDL_PLATFORM_LINUX)
38 const char *proc_path = "/proc/self/exe";
39#elif defined(SDL_PLATFORM_FREEBSD)
40 const char *proc_path = "/proc/curproc/file";
41#elif defined(SDL_PLATFORM_NETBSD)
42 const char *proc_path = "/proc/curproc/exe";
43#endif
44 linksize = readlink(proc_path, linkfile, sizeof(linkfile) - 1);
45 if (linksize > 0) {
46 linkfile[linksize] = '\0';
47 proc_name = SDL_strrchr(linkfile, '/');
48 if (proc_name) {
49 ++proc_name;
50 } else {
51 proc_name = linkfile;
52 }
53 }
54#endif
55 }
56
57 return proc_name;
58}
59
60const char *SDL_GetAppID(void)
61{
62 const char *id_str = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING);
63
64 if (!id_str) {
65 // If the hint isn't set, try to use the application's executable name
66 id_str = SDL_GetExeName();
67 }
68
69 if (!id_str) {
70 // Finally, use the default we've used forever
71 id_str = "SDL_App";
72 }
73
74 return id_str;
75}
diff --git a/contrib/SDL-3.2.8/src/core/unix/SDL_appid.h b/contrib/SDL-3.2.8/src/core/unix/SDL_appid.h
new file mode 100644
index 0000000..3482521
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/core/unix/SDL_appid.h
@@ -0,0 +1,30 @@
1/*
2Simple DirectMedia Layer
3Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any damages
7arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose,
10including commercial applications, and to alter it and redistribute it
11freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
172. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
193. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "SDL_internal.h"
23
24#ifndef SDL_appid_h_
25#define SDL_appid_h_
26
27extern const char *SDL_GetExeName(void);
28extern const char *SDL_GetAppID(void);
29
30#endif // SDL_appid_h_
diff --git a/contrib/SDL-3.2.8/src/core/unix/SDL_poll.c b/contrib/SDL-3.2.8/src/core/unix/SDL_poll.c
new file mode 100644
index 0000000..572ed5a
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/core/unix/SDL_poll.c
@@ -0,0 +1,95 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "SDL_internal.h"
23
24#include "SDL_poll.h"
25
26#ifdef HAVE_POLL
27#include <poll.h>
28#else
29#include <sys/time.h>
30#include <sys/types.h>
31#include <unistd.h>
32#endif
33#include <errno.h>
34
35int SDL_IOReady(int fd, int flags, Sint64 timeoutNS)
36{
37 int result;
38
39 SDL_assert(flags & (SDL_IOR_READ | SDL_IOR_WRITE));
40
41 // Note: We don't bother to account for elapsed time if we get EINTR
42 do {
43#ifdef HAVE_POLL
44 struct pollfd info;
45 int timeoutMS;
46
47 info.fd = fd;
48 info.events = 0;
49 if (flags & SDL_IOR_READ) {
50 info.events |= POLLIN | POLLPRI;
51 }
52 if (flags & SDL_IOR_WRITE) {
53 info.events |= POLLOUT;
54 }
55 // FIXME: Add support for ppoll() for nanosecond precision
56 if (timeoutNS > 0) {
57 timeoutMS = (int)SDL_NS_TO_MS(timeoutNS + (SDL_NS_PER_MS - 1));
58 } else if (timeoutNS == 0) {
59 timeoutMS = 0;
60 } else {
61 timeoutMS = -1;
62 }
63 result = poll(&info, 1, timeoutMS);
64#else
65 fd_set rfdset, *rfdp = NULL;
66 fd_set wfdset, *wfdp = NULL;
67 struct timeval tv, *tvp = NULL;
68
69 // If this assert triggers we'll corrupt memory here
70 SDL_assert(fd >= 0 && fd < FD_SETSIZE);
71
72 if (flags & SDL_IOR_READ) {
73 FD_ZERO(&rfdset);
74 FD_SET(fd, &rfdset);
75 rfdp = &rfdset;
76 }
77 if (flags & SDL_IOR_WRITE) {
78 FD_ZERO(&wfdset);
79 FD_SET(fd, &wfdset);
80 wfdp = &wfdset;
81 }
82
83 if (timeoutNS >= 0) {
84 tv.tv_sec = (timeoutNS / SDL_NS_PER_SECOND);
85 tv.tv_usec = SDL_NS_TO_US((timeoutNS % SDL_NS_PER_SECOND) + (SDL_NS_PER_US - 1));
86 tvp = &tv;
87 }
88
89 result = select(fd + 1, rfdp, wfdp, NULL, tvp);
90#endif // HAVE_POLL
91
92 } while (result < 0 && errno == EINTR && !(flags & SDL_IOR_NO_RETRY));
93
94 return result;
95}
diff --git a/contrib/SDL-3.2.8/src/core/unix/SDL_poll.h b/contrib/SDL-3.2.8/src/core/unix/SDL_poll.h
new file mode 100644
index 0000000..571619d
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/core/unix/SDL_poll.h
@@ -0,0 +1,33 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "SDL_internal.h"
23
24#ifndef SDL_poll_h_
25#define SDL_poll_h_
26
27#define SDL_IOR_READ 0x1
28#define SDL_IOR_WRITE 0x2
29#define SDL_IOR_NO_RETRY 0x4
30
31extern int SDL_IOReady(int fd, int flags, Sint64 timeoutNS);
32
33#endif // SDL_poll_h_