summaryrefslogtreecommitdiff
path: root/win32/contrib/timer/timer.c
blob: 179b7e18f92d595b0dfe3312152bc4c526b295cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* 
 * Copyright (C) 2000-2001 the xine project
 * 
 * This file is part of xine, a unix video player.
 * 
 * xine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * xine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 * WIN32 PORT,
 * by Matthew Grooms <elon@altavista.com>
 *
 * timer.c - Missing unix timer functions
 *
 */

#include "stdio.h"
#include "timer.h"

#include <signal.h>

#ifndef SIGALRM
#define SIGALRM 14
#endif

 /*
	this function returns somewhat
	accurate unix time with the data
	accurate to the first call to get
	of day and the resolution accurate
	to ~ miliseconds.
*/

static time_t startseconds = 0;

int gettimeofday( struct timeval *tp, struct timezone *tzp )
{
	MMTIME mmtime;

	// clock() returns time in miliseconds

	if( !startseconds )
		startseconds = time( 0 );

	timeGetSystemTime( &mmtime, sizeof( mmtime ) );      

	tp->tv_sec	= ( mmtime.u.ms / 1000 ) + startseconds;
	tp->tv_usec	= ( mmtime.u.ms % 1000 ) * 1000;

	return 0;
};

/*
	These functions are designed to mimick
	a subset of itimer for use with the 
	alarm signal on win32. This is just
	enough for xine to work.
*/

static HANDLE sigalarm = 0;

int setitimer( int which, struct itimerval * value, struct itimerval *ovalue )
{
	long int miliseconds;
		
	if( !sigalarm )
		sigalarm = CreateEvent( 0, FALSE, TRUE, "SIGALARM" );

    miliseconds = value->it_value.tv_usec / 1000;

	timeSetEvent( miliseconds, 0, ( LPTIMECALLBACK ) sigalarm, 0, TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE );

	return 0;
}

/*
	Wait for sigalarm to wake the thread
*/

int pause( void )
{
	WaitForSingleObject( sigalarm, INFINITE );

	return 0;
}

int nanosleep( const struct timespec * rqtp, struct timespec * rmtp )
{
	Sleep( rqtp->tv_nsec / 1000000 );

	return 0;
}

unsigned int sleep( unsigned int seconds )
{
	Sleep( seconds * 1000 );
	return 0;
}

static UINT alarmid = 0;
void CALLBACK AlarmCallback(UINT wTimerID, 
							UINT msg, 
							DWORD dwUser, 
							DWORD dw1, 
							DWORD dw2)
{
	if (alarmid) {
		alarmid = 0;
		raise(SIGALRM);
	}
} 

int alarm( int sec )
{
	long int miliseconds;

	if (sec) {
	  if (!alarmid) {
	    miliseconds = sec * 1000;
		timeSetEvent( miliseconds, 0, ( LPTIMECALLBACK ) AlarmCallback, 0, TIME_ONESHOT | TIME_CALLBACK_FUNCTION );
	  }
	}
	else {
	  if (alarmid) {
	    timeKillEvent(alarmid);
		alarmid = 0;
	  }
	}

	return 0;
}