sdlfuncs.c 8.65 KB
Newer Older
1
2
3
#include <stdlib.h>	/* getenv()/exit()/atexit() */
#include <stdio.h>	/* NULL */

4
#include "gen_defs.h"
deuce's avatar
deuce committed
5
#include "threadwrap.h"
6
7
#include <SDL.h>
#include "sdlfuncs.h"
8
#include "sdl_con.h"
9
extern int sdl_video_initialized;
10
11
12

struct sdlfuncs sdl;

13
14
15
16
17
18
/* Make xp_dl do static linking */
#ifdef STATIC_SDL
#define STATIC_LINK
#endif
#include <xp_dl.h>

19
20
#include "ciolib.h"

21
22
23
static int sdl_funcs_loaded=0;
static int sdl_initialized=0;
static int sdl_audio_initialized=0;
24

25
static void QuitWrap(void);
26
27
28

int load_sdl_funcs(struct sdlfuncs *sdlf)
{
29
	dll_handle	sdl_dll;
30
	const char *libnames[]={"SDL2", "SDL", NULL};
31
32

	sdlf->gotfuncs=0;
33
	if((sdl_dll=xp_dlopen(libnames,RTLD_LAZY|RTLD_GLOBAL,SDL_PATCHLEVEL))==NULL)
34
		return(-1);
35
36
37

	if((sdlf->Init=xp_dlsym(sdl_dll, SDL_Init))==NULL) {
		xp_dlclose(sdl_dll);
38
39
		return(-1);
	}
40
41
	if((sdlf->Quit=xp_dlsym(sdl_dll, SDL_Quit))==NULL) {
		xp_dlclose(sdl_dll);
42
43
		return(-1);
	}
44
45
	// SDL2: Rename from mutexP/mutexV to LockMutex/UnlockMutex
	if((sdlf->mutexP=xp_dlsym(sdl_dll, SDL_LockMutex))==NULL) {
46
		xp_dlclose(sdl_dll);
47
48
		return(-1);
	}
49
	if((sdlf->mutexV=xp_dlsym(sdl_dll, SDL_UnlockMutex))==NULL) {
50
		xp_dlclose(sdl_dll);
51
52
		return(-1);
	}
53
54
	if((sdlf->PeepEvents=xp_dlsym(sdl_dll, SDL_PeepEvents))==NULL) {
		xp_dlclose(sdl_dll);
55
56
		return(-1);
	}
57
	if((sdlf->GetCurrentVideoDriver=xp_dlsym(sdl_dll, SDL_GetCurrentVideoDriver))==NULL) {
58
		xp_dlclose(sdl_dll);
59
60
		return(-1);
	}
61
62
	if((sdlf->SemWait=xp_dlsym(sdl_dll, SDL_SemWait))==NULL) {
		xp_dlclose(sdl_dll);
63
64
		return(-1);
	}
65
66
67
68
	if((sdlf->SemWaitTimeout=xp_dlsym(sdl_dll, SDL_SemWaitTimeout))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
69
70
	if((sdlf->SemPost=xp_dlsym(sdl_dll, SDL_SemPost))==NULL) {
		xp_dlclose(sdl_dll);
71
72
		return(-1);
	}
73
74
	if((sdlf->EventState=xp_dlsym(sdl_dll, SDL_EventState))==NULL) {
		xp_dlclose(sdl_dll);
75
76
		return(-1);
	}
77
78
	if((sdlf->CreateRGBSurfaceFrom=xp_dlsym(sdl_dll, SDL_CreateRGBSurfaceFrom))==NULL) {
		xp_dlclose(sdl_dll);
79
80
		return(-1);
	}
81
	if((sdlf->RenderPresent=xp_dlsym(sdl_dll, SDL_RenderPresent))==NULL) {
82
		xp_dlclose(sdl_dll);
deuce's avatar
deuce committed
83
84
		return(-1);
	}
85
86
	if((sdlf->SDL_CreateSemaphore=xp_dlsym(sdl_dll, SDL_CreateSemaphore))==NULL) {
		xp_dlclose(sdl_dll);
87
88
		return(-1);
	}
89
90
	if((sdlf->SDL_DestroySemaphore=xp_dlsym(sdl_dll, SDL_DestroySemaphore))==NULL) {
		xp_dlclose(sdl_dll);
91
92
		return(-1);
	}
93
94
	if((sdlf->SDL_CreateMutex=xp_dlsym(sdl_dll, SDL_CreateMutex))==NULL) {
		xp_dlclose(sdl_dll);
95
96
		return(-1);
	}
97
	if((sdlf->WaitEventTimeout=xp_dlsym(sdl_dll, SDL_WaitEventTimeout))==NULL) {
98
		xp_dlclose(sdl_dll);
99
100
		return(-1);
	}
101
102
103
104
	if((sdlf->CreateWindowAndRenderer=xp_dlsym(sdl_dll, SDL_CreateWindowAndRenderer))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
105
106
	if((sdlf->FreeSurface=xp_dlsym(sdl_dll, SDL_FreeSurface))==NULL) {
		xp_dlclose(sdl_dll);
107
108
		return(-1);
	}
109
	if((sdlf->SetWindowTitle=xp_dlsym(sdl_dll, SDL_SetWindowTitle))==NULL) {
110
		xp_dlclose(sdl_dll);
111
112
		return(-1);
	}
113
	if((sdlf->GetWindowSize=xp_dlsym(sdl_dll, SDL_GetWindowSize))==NULL) {
114
		xp_dlclose(sdl_dll);
115
116
		return(-1);
	}
117
	if((sdlf->SetWindowIcon=xp_dlsym(sdl_dll, SDL_SetWindowIcon))==NULL) {
118
		xp_dlclose(sdl_dll);
119
120
		return(-1);
	}
121
	if((sdlf->ShowCursor=xp_dlsym(sdl_dll, SDL_ShowCursor))==NULL) {
122
		xp_dlclose(sdl_dll);
123
124
		return(-1);
	}
125
	if((sdlf->WasInit=xp_dlsym(sdl_dll, SDL_WasInit))==NULL) {
126
		xp_dlclose(sdl_dll);
127
128
		return(-1);
	}
129
	if((sdlf->GetWindowWMInfo=xp_dlsym(sdl_dll, SDL_GetWindowWMInfo))==NULL) {
130
		xp_dlclose(sdl_dll);
131
132
		return(-1);
	}
133
134
	if((sdlf->GetError=xp_dlsym(sdl_dll, SDL_GetError))==NULL) {
		xp_dlclose(sdl_dll);
135
136
		return(-1);
	}
137
138
	if((sdlf->InitSubSystem=xp_dlsym(sdl_dll, SDL_InitSubSystem))==NULL) {
		xp_dlclose(sdl_dll);
139
140
		return(-1);
	}
141
142
	if((sdlf->QuitSubSystem=xp_dlsym(sdl_dll, SDL_QuitSubSystem))==NULL) {
		xp_dlclose(sdl_dll);
143
144
		return(-1);
	}
145
146
	if((sdlf->OpenAudio=xp_dlsym(sdl_dll, SDL_OpenAudio))==NULL) {
		xp_dlclose(sdl_dll);
147
148
		return(-1);
	}
149
150
	if((sdlf->CloseAudio=xp_dlsym(sdl_dll, SDL_CloseAudio))==NULL) {
		xp_dlclose(sdl_dll);
151
152
		return(-1);
	}
153
154
	if((sdlf->PauseAudio=xp_dlsym(sdl_dll, SDL_PauseAudio))==NULL) {
		xp_dlclose(sdl_dll);
155
156
		return(-1);
	}
157
158
	if((sdlf->LockAudio=xp_dlsym(sdl_dll, SDL_LockAudio))==NULL) {
		xp_dlclose(sdl_dll);
159
160
		return(-1);
	}
161
162
	if((sdlf->UnlockAudio=xp_dlsym(sdl_dll, SDL_UnlockAudio))==NULL) {
		xp_dlclose(sdl_dll);
163
164
		return(-1);
	}
165
166
	if((sdlf->GetAudioStatus=xp_dlsym(sdl_dll, SDL_GetAudioStatus))==NULL) {
		xp_dlclose(sdl_dll);
167
168
		return(-1);
	}
169
	if((sdlf->CreateTexture=xp_dlsym(sdl_dll, SDL_CreateTexture))==NULL) {
170
		xp_dlclose(sdl_dll);
deuce's avatar
deuce committed
171
172
		return(-1);
	}
173
	if((sdlf->UpdateTexture=xp_dlsym(sdl_dll, SDL_UpdateTexture))==NULL) {
174
		xp_dlclose(sdl_dll);
deuce's avatar
deuce committed
175
176
		return(-1);
	}
177
	if((sdlf->RenderClear=xp_dlsym(sdl_dll, SDL_RenderClear))==NULL) {
178
		xp_dlclose(sdl_dll);
deuce's avatar
deuce committed
179
180
		return(-1);
	}
181
	if((sdlf->RenderCopy=xp_dlsym(sdl_dll, SDL_RenderCopy))==NULL) {
182
		xp_dlclose(sdl_dll);
183
184
		return(-1);
	}
185
	if((sdlf->SetHint=xp_dlsym(sdl_dll, SDL_SetHint))==NULL) {
186
		xp_dlclose(sdl_dll);
deuce's avatar
deuce committed
187
188
		return(-1);
	}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
	if((sdlf->GetHint=xp_dlsym(sdl_dll, SDL_GetHint))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->GetModState=xp_dlsym(sdl_dll, SDL_GetModState))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->SetWindowSize=xp_dlsym(sdl_dll, SDL_SetWindowSize))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->DestroyTexture=xp_dlsym(sdl_dll, SDL_DestroyTexture))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
deuce's avatar
deuce committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
	if((sdlf->SetWindowFullscreen=xp_dlsym(sdl_dll, SDL_SetWindowFullscreen))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->LockTexture=xp_dlsym(sdl_dll, SDL_LockTexture))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->UnlockTexture=xp_dlsym(sdl_dll, SDL_UnlockTexture))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->QueryTexture=xp_dlsym(sdl_dll, SDL_QueryTexture))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->GetWindowPosition=xp_dlsym(sdl_dll, SDL_GetWindowPosition))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
	if((sdlf->SetWindowPosition=xp_dlsym(sdl_dll, SDL_SetWindowPosition))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
229
230
231
232
	if((sdlf->SetWindowMinimumSize=xp_dlsym(sdl_dll, SDL_SetWindowMinimumSize))==NULL) {
		xp_dlclose(sdl_dll);
		return(-1);
	}
233
234
235
236
237
238
	{
		int (HACK_HACK_HACK *ra)(char *name, Uint32 style, void *hInst);
		if ((ra = xp_dlsym(sdl_dll, SDL_RegisterApp)) != NULL) {
			ra(ciolib_appname, 0, NULL);
		}
	}
239

240
241
242
243
244
245
246
	sdlf->gotfuncs=1;
	sdl_funcs_loaded=1;
	return(0);
}

int init_sdl_video(void)
{
247
	char	*drivername;
248
	int		use_sdl_video=FALSE;
249
#ifdef _WIN32
250
	char		*driver_env=NULL;
251
#endif
252

253
254
255
	if(sdl_video_initialized)
		return(0);

256
257
	load_sdl_funcs(&sdl);

258
259
	if (!sdl.gotfuncs)
		return -1;
260

261
262
	use_sdl_video=TRUE;

263
	sdl.SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2" );
264
	sdl.SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1" );
265
#ifdef _WIN32
266
267
268
269
270
271
272
273
	/* Fail to windib (ie: No mouse attached) */
	if(sdl.Init(SDL_INIT_VIDEO)) {
		// SDL2: We can likely do better now...
		driver_env=getenv("SDL_VIDEODRIVER");
		if(driver_env==NULL || strcmp(driver_env,"windib")) {
			putenv("SDL_VIDEODRIVER=windib");
			WinExec(GetCommandLine(), SW_SHOWDEFAULT);
			return(0);
274
		}
275
276
		/* Sure ,we can't use video, but audio is still valid! */
		if(sdl.Init(0)==0)
277
			sdl_initialized=TRUE;
278
279
280
281
282
	}
	else {
		sdl_video_initialized=TRUE;
		sdl_initialized=TRUE;
	}
283
#else
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
	/*
	 * SDL2: Is the below comment still true for SDL2?
	 * On Linux, SDL doesn't properly detect availability of the
	 * framebuffer apparently.  This results in remote connections
	 * displaying on the local framebuffer... a definate no-no.
	 * This ugly hack attempts to prevent this... of course, remote X11
	 * connections must still be allowed.
	 */
	if((!use_sdl_video) || ((getenv("REMOTEHOST")!=NULL || getenv("SSH_CLIENT")!=NULL) && getenv("DISPLAY")==NULL)) {
		/* Sure ,we can't use video, but audio is still valid! */
		if(sdl.Init(0)==0)
			sdl_initialized=TRUE;
	}
	else {
		if(sdl.Init(SDL_INIT_VIDEO)==0) {
			sdl_initialized=TRUE;
			sdl_video_initialized=TRUE;
301
302
		}
		else {
303
304
			/* Sure ,we can't use video, but audio is still valid! */
			if(sdl.Init(0)==0)
305
306
				sdl_initialized=TRUE;
		}
307
	}
308
#endif
309
310
311
312
313
314
315
316
	if(sdl_video_initialized && (drivername = sdl.GetCurrentVideoDriver())!=NULL) {
		/* Unacceptable drivers */
		if((!strcmp(drivername, "caca")) || (!strcmp(drivername,"aalib")) || (!strcmp(drivername,"dummy"))) {
			sdl.QuitSubSystem(SDL_INIT_VIDEO);
			sdl_video_initialized=FALSE;
		}
		else {
			sdl_video_initialized=TRUE;
317
318
		}
	}
319

320
	if(sdl_video_initialized) {
deuce's avatar
deuce committed
321
		SetThreadName("SDL Main");
322
		atexit(QuitWrap);
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
		return 0;
	}

	return(-1);
}

int init_sdl_audio(void)
{
	if(!sdl_initialized)
		return(-1);
	if(sdl_audio_initialized)
		return(0);
	if(sdl.InitSubSystem(SDL_INIT_AUDIO)==0) {
		sdl_audio_initialized=TRUE;
		return(0);
	}
	return(-1);
}

static void QuitWrap(void)
{
	if (sdl_initialized) {
		exit_sdl_con();
		if(sdl.Quit)
			sdl.Quit();
348
349
	}
}