src/timer/wince/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
icculus@1180
     1
/*
icculus@1180
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
icculus@1180
     4
icculus@1180
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
icculus@1180
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@1180
     9
icculus@1180
    10
    This library is distributed in the hope that it will be useful,
icculus@1180
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1180
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
icculus@1180
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1180
    18
icculus@1180
    19
    Sam Lantinga
icculus@1180
    20
    slouken@libsdl.org
icculus@1180
    21
*/
slouken@1402
    22
#include "SDL_config.h"
icculus@1180
    23
slouken@1635
    24
#ifdef SDL_TIMER_WINCE
slouken@1635
    25
slouken@1433
    26
#define WIN32_LEAN_AND_MEAN
slouken@1433
    27
#include <windows.h>
icculus@1180
    28
#include <mmsystem.h>
icculus@1180
    29
slouken@1497
    30
#include "SDL_thread.h"
icculus@1180
    31
#include "SDL_timer.h"
slouken@1361
    32
#include "../SDL_timer_c.h"
icculus@1180
    33
icculus@1180
    34
static Uint64 start_date;
icculus@1180
    35
static Uint64 start_ticks;
icculus@1180
    36
slouken@1662
    37
static Uint64
slouken@1668
    38
wce_ticks(void)
icculus@1180
    39
{
slouken@1668
    40
    return ((Uint64) GetTickCount());
icculus@1180
    41
}
icculus@1180
    42
slouken@1662
    43
static Uint64
slouken@1668
    44
wce_date(void)
icculus@1180
    45
{
slouken@1662
    46
    union
slouken@1662
    47
    {
slouken@1662
    48
        FILETIME ftime;
slouken@1662
    49
        Uint64 itime;
slouken@1662
    50
    } ftime;
slouken@1662
    51
    SYSTEMTIME stime;
icculus@1180
    52
slouken@1668
    53
    GetSystemTime(&stime);
slouken@1668
    54
    SystemTimeToFileTime(&stime, &ftime.ftime);
slouken@1662
    55
    ftime.itime /= 10000;       // Convert 100ns intervals to 1ms intervals
slouken@1662
    56
    // Remove ms portion, which can't be relied on
slouken@1662
    57
    ftime.itime -= (ftime.itime % 1000);
slouken@1662
    58
    return (ftime.itime);
icculus@1180
    59
}
icculus@1180
    60
slouken@1662
    61
static Sint32
slouken@1668
    62
wce_rel_ticks(void)
icculus@1180
    63
{
slouken@1668
    64
    return ((Sint32) (wce_ticks() - start_ticks));
icculus@1180
    65
}
icculus@1180
    66
slouken@1662
    67
static Sint32
slouken@1668
    68
wce_rel_date(void)
icculus@1180
    69
{
slouken@1668
    70
    return ((Sint32) (wce_date() - start_date));
icculus@1180
    71
}
icculus@1180
    72
icculus@1180
    73
/* Return time in ms relative to when SDL was started */
slouken@1662
    74
Uint32
slouken@1668
    75
SDL_GetTicks()
icculus@1180
    76
{
slouken@1668
    77
    Sint32 offset = wce_rel_date() - wce_rel_ticks();
slouken@1662
    78
    if ((offset < -1000) || (offset > 1000)) {
icculus@1180
    79
//    fprintf(stderr,"Time desync(%+d), resyncing\n",offset/1000);
slouken@1662
    80
        start_ticks -= offset;
slouken@1662
    81
    }
icculus@1180
    82
slouken@1668
    83
    return ((Uint32) wce_rel_ticks());
icculus@1180
    84
}
icculus@1180
    85
icculus@1180
    86
/* Give up approx. givem milliseconds to the OS. */
slouken@1662
    87
void
slouken@1668
    88
SDL_Delay(Uint32 ms)
icculus@1180
    89
{
slouken@1668
    90
    Sleep(ms);
icculus@1180
    91
}
icculus@1180
    92
icculus@1180
    93
/* Recard start-time of application for reference */
slouken@1662
    94
void
slouken@1668
    95
SDL_StartTicks(void)
icculus@1180
    96
{
slouken@1668
    97
    start_date = wce_date();
slouken@1668
    98
    start_ticks = wce_ticks();
icculus@1180
    99
}
icculus@1180
   100
icculus@1180
   101
static UINT WIN_timer;
icculus@1180
   102
slouken@1497
   103
#if ( _WIN32_WCE <= 420 )
slouken@1497
   104
slouken@1497
   105
static HANDLE timersThread = 0;
slouken@1497
   106
static HANDLE timersQuitEvent = 0;
slouken@1497
   107
slouken@1662
   108
DWORD
slouken@1668
   109
TimersThreadProc(void *data)
slouken@1497
   110
{
slouken@1668
   111
    while (WaitForSingleObject(timersQuitEvent, 10) == WAIT_TIMEOUT) {
slouken@1668
   112
        SDL_ThreadedTimerCheck();
slouken@1662
   113
    }
slouken@1662
   114
    return 0;
slouken@1497
   115
}
slouken@1497
   116
slouken@1662
   117
int
slouken@1668
   118
SDL_SYS_TimerInit(void)
icculus@1180
   119
{
slouken@1662
   120
    // create a thread to process a threaded timers
slouken@1662
   121
    // SetTimer does not suit the needs because 
slouken@1662
   122
    // TimerCallbackProc will be called only when WM_TIMER occured
slouken@1497
   123
slouken@1668
   124
    timersQuitEvent = CreateEvent(0, TRUE, FALSE, 0);
slouken@1662
   125
    if (!timersQuitEvent) {
slouken@1668
   126
        SDL_SetError("Cannot create event for timers thread");
slouken@1662
   127
        return -1;
slouken@1662
   128
    }
slouken@1668
   129
    timersThread = CreateThread(NULL, 0, TimersThreadProc, 0, 0, 0);
slouken@1662
   130
    if (!timersThread) {
slouken@1662
   131
        SDL_SetError
slouken@1662
   132
            ("Cannot create timers thread, check amount of RAM available");
slouken@1662
   133
        return -1;
slouken@1662
   134
    }
slouken@1668
   135
    SetThreadPriority(timersThread, THREAD_PRIORITY_HIGHEST);
slouken@1497
   136
slouken@1668
   137
    return (SDL_SetTimerThreaded(1));
icculus@1180
   138
}
icculus@1180
   139
slouken@1662
   140
void
slouken@1668
   141
SDL_SYS_TimerQuit(void)
icculus@1180
   142
{
slouken@1668
   143
    SetEvent(timersQuitEvent);
slouken@1668
   144
    if (WaitForSingleObject(timersThread, 2000) == WAIT_TIMEOUT)
slouken@1668
   145
        TerminateThread(timersThread, 0);
slouken@1668
   146
    CloseHandle(timersThread);
slouken@1668
   147
    CloseHandle(timersQuitEvent);
slouken@1662
   148
    return;
icculus@1180
   149
}
icculus@1180
   150
slouken@1497
   151
#else
icculus@1180
   152
slouken@1497
   153
#pragma comment(lib, "mmtimer.lib")
slouken@1497
   154
slouken@1497
   155
/* Data to handle a single periodic alarm */
slouken@1497
   156
static UINT timerID = 0;
slouken@1497
   157
slouken@1662
   158
static void CALLBACK
slouken@1668
   159
HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
icculus@1180
   160
{
slouken@1668
   161
    SDL_ThreadedTimerCheck();
slouken@1497
   162
}
icculus@1180
   163
slouken@1497
   164
slouken@1662
   165
int
slouken@1668
   166
SDL_SYS_TimerInit(void)
slouken@1497
   167
{
slouken@1662
   168
    MMRESULT result;
slouken@1497
   169
slouken@1662
   170
    /* Set timer resolution */
slouken@1668
   171
    result = timeBeginPeriod(TIMER_RESOLUTION);
slouken@1662
   172
    if (result != TIMERR_NOERROR) {
slouken@1668
   173
        SDL_SetError("Warning: Can't set %d ms timer resolution",
slouken@1668
   174
                     TIMER_RESOLUTION);
slouken@1662
   175
    }
slouken@1662
   176
    /* Allow 10 ms of drift so we don't chew on CPU */
slouken@1662
   177
    timerID =
slouken@1668
   178
        timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC);
slouken@1662
   179
    if (!timerID) {
slouken@1668
   180
        SDL_SetError("timeSetEvent() failed");
slouken@1662
   181
        return (-1);
slouken@1662
   182
    }
slouken@1668
   183
    return (SDL_SetTimerThreaded(1));
icculus@1180
   184
}
icculus@1180
   185
slouken@1662
   186
void
slouken@1668
   187
SDL_SYS_TimerQuit(void)
slouken@1497
   188
{
slouken@1662
   189
    if (timerID) {
slouken@1668
   190
        timeKillEvent(timerID);
slouken@1662
   191
    }
slouken@1668
   192
    timeEndPeriod(TIMER_RESOLUTION);
slouken@1497
   193
}
slouken@1497
   194
slouken@1497
   195
#endif
slouken@1497
   196
slouken@1662
   197
int
slouken@1668
   198
SDL_SYS_StartTimer(void)
icculus@1180
   199
{
slouken@1668
   200
    SDL_SetError("Internal logic error: WinCE uses threaded timer");
slouken@1662
   201
    return (-1);
icculus@1180
   202
}
icculus@1180
   203
slouken@1662
   204
void
slouken@1668
   205
SDL_SYS_StopTimer(void)
icculus@1180
   206
{
slouken@1662
   207
    return;
icculus@1180
   208
}
slouken@1635
   209
slouken@1635
   210
#endif /* SDL_TIMER_WINCE */
slouken@1662
   211
/* vi: set ts=4 sw=4 expandtab: */