Skip to content
Permalink
Browse files

Windows CE patches contributed by Rainer Loritz

  • Loading branch information
Sam Lantinga committed May 23, 2001
1 parent 4e2bc45 commit 80d11f278fbd50040ff18ef80a0c9a62a29b3b8e
@@ -231,3 +231,21 @@ const SDL_version * SDL_Linked_Version(void)
return(&version);
}

#if defined(_WIN32_WCE)
/* Need to include DllMain() on Windows CE for some reason.. */
#include <windows.h>

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif /* _WIN32_WCE */
@@ -37,6 +37,9 @@ static char rcsid =
#include "SDL_timer.h"
#include "SDL_audio_c.h"
#include "SDL_dibaudio.h"
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
#include "win_ce_semaphore.h"
#endif


/* Audio driver functions */
@@ -112,18 +115,33 @@ static void CALLBACK FillSound(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance,
return;

/* Signal that we are done playing a buffer */
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
ReleaseSemaphoreCE(audio_sem, 1, NULL);
#else
ReleaseSemaphore(audio_sem, 1, NULL);
#endif
}

static void SetMMerror(char *function, MMRESULT code)
{
int len;
char errbuf[MAXERRORLENGTH];
#ifdef _WIN32_WCE
wchar_t werrbuf[MAXERRORLENGTH];
#endif

sprintf(errbuf, "%s: ", function);
len = strlen(errbuf);

#ifdef _WIN32_WCE
/* UNICODE version */
waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH-len);
WideCharToMultiByte(CP_ACP,0,werrbuf,-1,errbuf+len,MAXERRORLENGTH-len,NULL,NULL);
#else
waveOutGetErrorText(code, errbuf+len, MAXERRORLENGTH-len);
SDL_SetError("%s", errbuf);
#endif

SDL_SetError("%s",errbuf);
}

/* Set high priority for the audio thread */
@@ -135,7 +153,11 @@ static void DIB_ThreadInit(_THIS)
void DIB_WaitAudio(_THIS)
{
/* Wait for an audio chunk to finish */
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
WaitForSemaphoreCE(audio_sem, INFINITE);
#else
WaitForSingleObject(audio_sem, INFINITE);
#endif
}

Uint8 *DIB_GetAudioBuf(_THIS)
@@ -176,7 +198,11 @@ void DIB_CloseAudio(_THIS)

/* Close up audio */
if ( audio_sem ) {
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
CloseSynchHandle(audio_sem);
#else
CloseHandle(audio_sem);
#endif
}
if ( sound ) {
waveOutClose(sound);
@@ -267,7 +293,11 @@ int DIB_OpenAudio(_THIS, SDL_AudioSpec *spec)
#endif

/* Create the audio buffer semaphore */
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
audio_sem = CreateSemaphoreCE(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL);
#else
audio_sem = CreateSemaphore(NULL, NUM_BUFFERS-1, NUM_BUFFERS, NULL);
#endif
if ( audio_sem == NULL ) {
SDL_SetError("Couldn't create semaphore");
return(-1);
@@ -30,6 +30,7 @@ static char rcsid =
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "SDL_error.h"
@@ -19,9 +19,40 @@
#undef main
#endif

/* Do we really not want stdio redirection with Windows CE? */
#ifdef _WIN32_WCE
#define NO_STDIO_REDIRECT
#endif

/* The standard output files */
#define STDOUT_FILE "stdout.txt"
#define STDERR_FILE "stderr.txt"
#define STDOUT_FILE TEXT("stdout.txt")
#define STDERR_FILE TEXT("stderr.txt")

#ifdef _WIN32_WCE
/* seems to be undefined in Win CE although in online help */
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))

/* seems to be undefined in Win CE although in online help */
char *strrchr(char *str, int c)
{
char *p;

/* Skip to the end of the string */
p=str;
while (*p)
p++;

/* Look for the given character */
while ( (p >= str) && (*p != (CHAR)c) )
p--;

/* Return NULL if character not found */
if ( p < str ) {
p = NULL;
}
return p;
}
#endif /* _WIN32_WCE */

/* Parse a command line buffer into arguments */
static int ParseCommandLine(char *cmdline, char **argv)
@@ -92,15 +123,18 @@ static BOOL OutOfMemory(void)
}

/* Remove the output files if there was no output written */
static void cleanup_output(void)
static void __cdecl cleanup_output(void)
{
#ifndef NO_STDIO_REDIRECT
FILE *file;
int empty;
#endif

/* Flush the output in case anything is queued */
fclose(stdout);
fclose(stderr);

#ifndef NO_STDIO_REDIRECT
/* See if the files have any output in them */
file = fopen(STDOUT_FILE, "rb");
if ( file ) {
@@ -118,9 +152,11 @@ static void cleanup_output(void)
remove(STDERR_FILE);
}
}
#endif
}

#ifdef _MSC_VER /* The VC++ compiler needs main defined */
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
/* The VC++ compiler needs main defined */
#define console_main main
#endif

@@ -177,13 +213,22 @@ int console_main(int argc, char *argv[])
}

/* This is where execution begins [windowed apps] */
#ifdef _WIN32_WCE
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
#else
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
#endif
{
HINSTANCE handle;
char **argv;
int argc;
char *cmdline;
#ifdef _WIN32_WCE
wchar_t *bufp;
int nLen;
#else
char *bufp;
#endif
#ifndef NO_STDIO_REDIRECT
FILE *newfp;
#endif
@@ -192,7 +237,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
keep them open. This is a hack.. hopefully it will be fixed
someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
*/
handle = LoadLibrary("DDRAW.DLL");
handle = LoadLibrary(TEXT("DDRAW.DLL"));
if ( handle != NULL ) {
FreeLibrary(handle);
}
@@ -225,13 +270,26 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
setbuf(stderr, NULL); /* No buffering */
#endif /* !NO_STDIO_REDIRECT */

#ifdef _WIN32_WCE
nLen = wcslen(szCmdLine)+128+1;
bufp = (wchar_t *)alloca(nLen*2);
GetModuleFileName(NULL, bufp, 128);
wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen);
nLen = wcslen(bufp)+1;
cmdline = (char *)alloca(nLen);
if ( cmdline == NULL ) {
return OutOfMemory();
}
WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
#else
/* Grab the command line (use alloca() on Windows) */
bufp = GetCommandLine();
cmdline = (char *)alloca(strlen(bufp)+1);
if ( cmdline == NULL ) {
return OutOfMemory();
}
strcpy(cmdline, bufp);
#endif

/* Parse it into argv and argc */
argc = ParseCommandLine(cmdline, NULL);
@@ -33,56 +33,17 @@ static char rcsid =

#include "SDL_error.h"
#include "SDL_thread.h"

#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
#include "win_ce_semaphore.h"
#endif

/* No semaphores on Windows CE earlier than 3.0, hmm... */

/* Create a semaphore */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_SetError("Semaphores not supported on WinCE");
return(NULL);
}

/* Free the semaphore */
void SDL_DestroySemaphore(SDL_sem *sem)
{
return;
}

int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
SDL_SetError("Semaphores not supported on WinCE");
return(-1);
}

int SDL_SemTryWait(SDL_sem *sem)
{
return SDL_SemWaitTimeout(sem, 0);
}

int SDL_SemWait(SDL_sem *sem)
{
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}

/* Returns the current count of the semaphore */
Uint32 SDL_SemValue(SDL_sem *sem)
{
return(0);
}

int SDL_SemPost(SDL_sem *sem)
{
SDL_SetError("Semaphores not supported on WinCE");
return(-1);
}

#else

struct SDL_semaphore {
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
SYNCHHANDLE id;
#else
HANDLE id;
#endif
Uint32 volatile count;
};

@@ -96,7 +57,11 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
sem = (SDL_sem *)malloc(sizeof(*sem));
if ( sem ) {
/* Create the semaphore, with max value 32K */
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL);
#else
sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL);
#endif
sem->count = initial_value;
if ( ! sem->id ) {
SDL_SetError("Couldn't create semaphore");
@@ -114,7 +79,11 @@ void SDL_DestroySemaphore(SDL_sem *sem)
{
if ( sem ) {
if ( sem->id ) {
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
CloseSynchHandle(sem->id);
#else
CloseHandle(sem->id);
#endif
sem->id = 0;
}
free(sem);
@@ -136,7 +105,11 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
} else {
dwMilliseconds = (DWORD)timeout;
}
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
#else
switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
#endif
case WAIT_OBJECT_0:
--sem->count;
retval = 0;
@@ -184,12 +157,14 @@ int SDL_SemPost(SDL_sem *sem)
* is waiting for this semaphore.
*/
++sem->count;
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) {
#else
if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) {
#endif
--sem->count; /* restore */
SDL_SetError("ReleaseSemaphore() failed");
return -1;
}
return 0;
}

#endif /* _WIN32_WCE */

0 comments on commit 80d11f2

Please sign in to comment.