Fixed "conditional expr is always true due to being unsigned < 0"
[wine/multimedia.git] / scheduler / timer.c
blob210a1d28be7fb6ff725d834cf7da9d3f79dbea27
1 /*
2 * Win32 waitable timers
4 * Copyright 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <string.h>
26 #include "winerror.h"
27 #include "winnls.h"
28 #include "wine/unicode.h"
29 #include "file.h" /* for FILETIME routines */
30 #include "wine/server.h"
33 /***********************************************************************
34 * CreateWaitableTimerA (KERNEL32.@)
36 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
38 WCHAR buffer[MAX_PATH];
40 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
42 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
44 SetLastError( ERROR_FILENAME_EXCED_RANGE );
45 return 0;
47 return CreateWaitableTimerW( sa, manual, buffer );
51 /***********************************************************************
52 * CreateWaitableTimerW (KERNEL32.@)
54 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
56 HANDLE ret;
57 DWORD len = name ? strlenW(name) : 0;
58 if (len >= MAX_PATH)
60 SetLastError( ERROR_FILENAME_EXCED_RANGE );
61 return 0;
63 SERVER_START_REQ( create_timer )
65 req->manual = manual;
66 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
67 wine_server_add_data( req, name, len * sizeof(WCHAR) );
68 SetLastError(0);
69 wine_server_call_err( req );
70 ret = reply->handle;
72 SERVER_END_REQ;
73 return ret;
77 /***********************************************************************
78 * OpenWaitableTimerA (KERNEL32.@)
80 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
82 WCHAR buffer[MAX_PATH];
84 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
86 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
88 SetLastError( ERROR_FILENAME_EXCED_RANGE );
89 return 0;
91 return OpenWaitableTimerW( access, inherit, buffer );
95 /***********************************************************************
96 * OpenWaitableTimerW (KERNEL32.@)
98 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
100 HANDLE ret;
101 DWORD len = name ? strlenW(name) : 0;
102 if (len >= MAX_PATH)
104 SetLastError( ERROR_FILENAME_EXCED_RANGE );
105 return 0;
107 SERVER_START_REQ( open_timer )
109 req->access = access;
110 req->inherit = inherit;
111 wine_server_add_data( req, name, len * sizeof(WCHAR) );
112 wine_server_call_err( req );
113 ret = reply->handle;
115 SERVER_END_REQ;
116 return ret;
120 /***********************************************************************
121 * SetWaitableTimer (KERNEL32.@)
123 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
124 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
126 BOOL ret;
127 LARGE_INTEGER exp = *when;
129 if (exp.s.HighPart < 0) /* relative time */
131 LARGE_INTEGER now;
132 NtQuerySystemTime( &now );
133 exp.QuadPart = RtlLargeIntegerSubtract( now.QuadPart, exp.QuadPart );
136 SERVER_START_REQ( set_timer )
138 if (!exp.s.LowPart && !exp.s.HighPart)
140 /* special case to start timeout on now+period without too many calculations */
141 req->sec = 0;
142 req->usec = 0;
144 else
146 DWORD remainder;
147 req->sec = DOSFS_FileTimeToUnixTime( (FILETIME *)&exp, &remainder );
148 req->usec = remainder / 10; /* convert from 100-ns to us units */
150 req->handle = handle;
151 req->period = period;
152 req->callback = callback;
153 req->arg = arg;
154 if (resume) SetLastError( ERROR_NOT_SUPPORTED ); /* set error but can still succeed */
155 ret = !wine_server_call_err( req );
157 SERVER_END_REQ;
158 return ret;
162 /***********************************************************************
163 * CancelWaitableTimer (KERNEL32.@)
165 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
167 BOOL ret;
168 SERVER_START_REQ( cancel_timer )
170 req->handle = handle;
171 ret = !wine_server_call_err( req );
173 SERVER_END_REQ;
174 return ret;