Skip to content

Commit

Permalink
Merge branch 'master' into sdl3
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR committed Nov 3, 2024
2 parents 2476de0 + d139c61 commit e59f595
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 57 deletions.
16 changes: 5 additions & 11 deletions src/network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,6 @@ int Socket::poll(struct pollfd *fds, unsigned int nfds, int timeout_ms)
* but 95 was also supported with an additional download.
*/

// For LoadObject/LoadFunction
#include "../SDLmzx.h"

static struct
{
/* These are Winsock 2.0 functions that should be present all the
Expand Down Expand Up @@ -314,7 +311,7 @@ static struct
// These functions were only implemented as of Windows Vista.
int (WSAAPI *WSAPoll)(struct pollfd *fds, unsigned long nfds, int timeout);

void *handle;
struct dso_library *handle;
}
socksyms;

Expand Down Expand Up @@ -358,7 +355,7 @@ static void socket_free_syms(void)
{
if(socksyms.handle)
{
SDL_UnloadObject(socksyms.handle);
platform_unload_library(socksyms.handle);
socksyms.handle = NULL;
}
}
Expand All @@ -367,11 +364,11 @@ static boolean socket_load_syms(void)
{
int i;

socksyms.handle = SDL_LoadObject(WINSOCK2);
socksyms.handle = platform_load_library(WINSOCK2);
if(!socksyms.handle)
{
warn("Failed to load Winsock 2.0, falling back to Winsock\n");
socksyms.handle = SDL_LoadObject(WINSOCK);
socksyms.handle = platform_load_library(WINSOCK);
if(!socksyms.handle)
{
warn("Failed to load Winsock fallback\n");
Expand All @@ -381,10 +378,7 @@ static boolean socket_load_syms(void)

for(i = 0; socksyms_map[i].name; i++)
{
dso_fn **sym_ptr = socksyms_map[i].sym_ptr.value;
*sym_ptr = SDL_LoadFunction(socksyms.handle, socksyms_map[i].name);

if(!*sym_ptr)
if(!platform_load_function(socksyms.handle, &(socksyms_map[i])))
{
// Skip these NT 5.1 WS2 extensions; we can fall back
if((strcmp(socksyms_map[i].name, "freeaddrinfo") == 0) ||
Expand Down
31 changes: 31 additions & 0 deletions src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,44 @@ int real_main(int argc, char *argv[]);
#include <stdint.h>
#include <time.h>

/* Use as (dso_fn_ptr *) to store a loaded void(*)(void) to a function pointer. */
typedef void (*dso_fn_ptr)(void);
struct dso_library;

/* Initialize a (dso_fn_ptr *) via (void *) to avoid strict aliasing warnings. */
union dso_fn_ptr_ptr
{
void *in;
dso_fn_ptr *value;
};

/* SDL1/2, dlopen return void * instead of a function pointer. */
union dso_suppress_warning
{
void *in;
dso_fn_ptr out;
};

struct dso_syms_map
{
const char *name;
union dso_fn_ptr_ptr sym_ptr;
};

#define DSO_MAP_END { NULL, { NULL }}

CORE_LIBSPEC void delay(uint32_t ms);
CORE_LIBSPEC uint64_t get_ticks(void);
CORE_LIBSPEC boolean platform_init(void);
CORE_LIBSPEC void platform_quit(void);
CORE_LIBSPEC boolean platform_system_time(struct tm *tm,
int64_t *epoch, int32_t *nano);

CORE_LIBSPEC struct dso_library *platform_load_library(const char *name);
CORE_LIBSPEC void platform_unload_library(struct dso_library *library);
CORE_LIBSPEC boolean platform_load_function(struct dso_library *library,
const struct dso_syms_map *syms_map);

__M_END_DECLS

#endif // __PLATFORM_H
56 changes: 48 additions & 8 deletions src/platform_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,50 @@ uint64_t get_ticks(void)
#endif
}

#if SDL_VERSION_ATLEAST(3,0,0)
typedef SDL_SharedObject *dso_library_ptr;
#else
typedef void *dso_library_ptr;
#endif

/* Note: for OpenGL, use (SDL_)GL_LoadLibrary instead. */
struct dso_library *platform_load_library(const char *name)
{
dso_library_ptr handle = SDL_LoadObject(name);
if(handle)
return (struct dso_library *)handle;
return NULL;
}

void platform_unload_library(struct dso_library *library)
{
dso_library_ptr handle = (dso_library_ptr)library;
SDL_UnloadObject(handle);
}

/* Note: for OpenGL, use (SDL_)GL_GetProcAddress (via gl_load_syms) instead. */
boolean platform_load_function(struct dso_library *library,
const struct dso_syms_map *syms_map)
{
dso_library_ptr handle = (dso_library_ptr)library;

#if SDL_VERSION_ATLEAST(3,0,0)
SDL_FunctionPointer *dest = (SDL_FunctionPointer *)syms_map->sym_ptr.value;
#else
void **dest = (void **)syms_map->sym_ptr.in;
#endif
if(!syms_map->name || !dest)
return false;

*dest = SDL_LoadFunction(handle, syms_map->name);
if(!*dest)
{
debug("--DSO--- failed to load: %s\n", syms_map->name);
return false;
}
return true;
}

#ifdef __WIN32__
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
Expand All @@ -82,17 +126,13 @@ uint64_t get_ticks(void)
static void set_dpi_aware(void)
{
BOOL (*_SetProcessDPIAware)(void) = NULL;
void *handle;

handle = SDL_LoadObject("User32.dll");
struct dso_library *handle = platform_load_library("User32.dll");
if(handle)
{
union dso_fn_ptr_ptr sym_ptr = { &_SetProcessDPIAware };
dso_fn **dest = sym_ptr.value;

*dest = SDL_LoadFunction(handle, "SetProcessDPIAware");
struct dso_syms_map sym = { "SetProcessDPIAware", { &_SetProcessDPIAware } };

if(_SetProcessDPIAware && !_SetProcessDPIAware())
if(platform_load_function(handle, &sym) && !_SetProcessDPIAware())
{
warn("failed to SetProcessDPIAware!\n");
}
Expand All @@ -101,7 +141,7 @@ static void set_dpi_aware(void)
if(!_SetProcessDPIAware)
debug("couldn't load SetProcessDPIAware.\n");

SDL_UnloadObject(handle);
platform_unload_library(handle);
}
else
debug("couldn't load User32.dll: %s\n", SDL_GetError());
Expand Down
14 changes: 7 additions & 7 deletions src/platform_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ static void convert_timestamp(int64_t *epoch, int32_t *nano,
*/
static boolean system_time_win32(int64_t *epoch, int32_t *nano)
{
static struct dso_library *dll;
static void (WINAPI *_GetSystemTimePreciseAsFileTime)(LPFILETIME) = NULL;
static const struct dso_syms_map sym =
{ "GetSystemTimePreciseAsFileTime", { &_GetSystemTimePreciseAsFileTime } };
static boolean init = false;
FILETIME ft;

if(!init)
{
void *dll = SDL_LoadObject("Kernel32.dll");
// Note: can't unload, or the function pointer will no longer be valid.
dll = platform_load_library("Kernel32.dll");
if(dll)
{
union dso_fn_ptr_ptr tmp;
tmp.in = (dso_fn **)&_GetSystemTimePreciseAsFileTime;
*(tmp.value) = SDL_LoadFunction(dll, "GetSystemTimePreciseAsFileTime");
SDL_UnloadObject(dll);
}
platform_load_function(dll, &sym);

init = true;
}

Expand Down
6 changes: 4 additions & 2 deletions src/render_egl.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ static enum gl_lib_type gl_type;

#ifdef ANDROID

dso_fn *GL_GetProcAddress(const char *proc)
dso_fn_ptr GL_GetProcAddress(const char *proc)
{
return (dso_fn *)dlsym(glso, proc);
union dso_suppress_warning value;
value.in = dlsym(glso, proc);
return value.out;
}

#endif /* ANDROID */
Expand Down
12 changes: 3 additions & 9 deletions src/render_egl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,17 @@ boolean GL_LoadLibrary(enum gl_lib_type type);

#ifndef ANDROID

static inline dso_fn *GL_GetProcAddress(const char *proc)
static inline dso_fn_ptr GL_GetProcAddress(const char *proc)
{
union suppress_warning
{
void (*in)(void);
dso_fn *out;
} t;
t.in = eglGetProcAddress(proc);
return t.out;
return eglGetProcAddress(proc);
}

#else /* ANDROID */

/* Android's eglGetProcAddress is currently broken, so we
* have to roll our own..
*/
dso_fn *GL_GetProcAddress(const char *proc);
dso_fn_ptr GL_GetProcAddress(const char *proc);

#endif /* !ANDROID */

Expand Down
4 changes: 3 additions & 1 deletion src/render_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "platform.h"
#include "render.h"
#include "util.h"

Expand Down Expand Up @@ -88,7 +89,8 @@ boolean gl_load_syms(const struct dso_syms_map *map)

for(i = 0; map[i].name != NULL; i++)
{
dso_fn **sym_ptr = map[i].sym_ptr.value;
dso_fn_ptr *sym_ptr = map[i].sym_ptr.value;

*sym_ptr = GL_GetProcAddress(map[i].name);
if(!*sym_ptr)
{
Expand Down
9 changes: 8 additions & 1 deletion src/render_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,16 @@ static inline boolean GL_LoadLibrary(enum gl_lib_type type)
return false;
}

static inline void *GL_GetProcAddress(const char *proc)
static inline dso_fn_ptr GL_GetProcAddress(const char *proc)
{
#if SDL_VERSION_ATLEAST(3,0,0)
return SDL_GL_GetProcAddress(proc);
#else
/* SDL1/2 returns void * instead of a function pointer. */
union dso_suppress_warning value;
value.in = SDL_GL_GetProcAddress(proc);
return value.out;
#endif
}

#endif // CONFIG_RENDER_GL_FIXED || CONFIG_RENDER_GL_PROGRAM
Expand Down
18 changes: 0 additions & 18 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,6 @@ static inline size_t round_to_power_of_two(size_t v)
return v + 1;
}

/* Use as (dso_fn **) to store a loaded (void *) to a function pointer. */
typedef void dso_fn;

/* Initialize a (dso_fn **) via (void *) to avoid strict aliasing warnings. */
union dso_fn_ptr_ptr
{
void *in;
dso_fn **value;
};

struct dso_syms_map
{
const char *name;
union dso_fn_ptr_ptr sym_ptr;
};

#define DSO_MAP_END { NULL, { NULL }}

#include <sys/types.h>

#if defined(__WIN32__) && defined(__STRICT_ANSI__)
Expand Down

0 comments on commit e59f595

Please sign in to comment.