Added internal 32-bit versions of GDISelectPalette and
[wine] / scheduler / timer.c
1 /*
2  * Win32 waitable timers
3  *
4  * Copyright 1999 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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"
31
32
33 /***********************************************************************
34  *           CreateWaitableTimerA    (KERNEL32.@)
35  */
36 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
37 {
38     WCHAR buffer[MAX_PATH];
39
40     if (!name) return CreateWaitableTimerW( sa, manual, NULL );
41
42     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
43     {
44         SetLastError( ERROR_FILENAME_EXCED_RANGE );
45         return 0;
46     }
47     return CreateWaitableTimerW( sa, manual, buffer );
48 }
49
50
51 /***********************************************************************
52  *           CreateWaitableTimerW    (KERNEL32.@)
53  */
54 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
55 {
56     HANDLE ret;
57     DWORD len = name ? strlenW(name) : 0;
58     if (len >= MAX_PATH)
59     {
60         SetLastError( ERROR_FILENAME_EXCED_RANGE );
61         return 0;
62     }
63     SERVER_START_REQ( create_timer )
64     {
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;
71     }
72     SERVER_END_REQ;
73     return ret;
74 }
75
76
77 /***********************************************************************
78  *           OpenWaitableTimerA    (KERNEL32.@)
79  */
80 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
81 {
82     WCHAR buffer[MAX_PATH];
83
84     if (!name) return OpenWaitableTimerW( access, inherit, NULL );
85
86     if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
87     {
88         SetLastError( ERROR_FILENAME_EXCED_RANGE );
89         return 0;
90     }
91     return OpenWaitableTimerW( access, inherit, buffer );
92 }
93
94
95 /***********************************************************************
96  *           OpenWaitableTimerW    (KERNEL32.@)
97  */
98 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
99 {
100     HANDLE ret;
101     DWORD len = name ? strlenW(name) : 0;
102     if (len >= MAX_PATH)
103     {
104         SetLastError( ERROR_FILENAME_EXCED_RANGE );
105         return 0;
106     }
107     SERVER_START_REQ( open_timer )
108     {
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;
114     }
115     SERVER_END_REQ;
116     return ret;
117 }
118
119
120 /***********************************************************************
121  *           SetWaitableTimer    (KERNEL32.@)
122  */
123 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
124                               PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
125 {
126     BOOL ret;
127     LARGE_INTEGER exp = *when;
128
129     if (exp.s.HighPart < 0)  /* relative time */
130     {
131         LARGE_INTEGER now;
132         NtQuerySystemTime( &now );
133         exp.QuadPart = RtlLargeIntegerSubtract( now.QuadPart, exp.QuadPart );
134     }
135
136     SERVER_START_REQ( set_timer )
137     {
138         if (!exp.s.LowPart && !exp.s.HighPart)
139         {
140             /* special case to start timeout on now+period without too many calculations */
141             req->sec  = 0;
142             req->usec = 0;
143         }
144         else
145         {
146             DWORD remainder;
147             req->sec  = DOSFS_FileTimeToUnixTime( (FILETIME *)&exp, &remainder );
148             req->usec = remainder / 10;  /* convert from 100-ns to us units */
149         }
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 );
156     }
157     SERVER_END_REQ;
158     return ret;
159 }
160
161
162 /***********************************************************************
163  *           CancelWaitableTimer    (KERNEL32.@)
164  */
165 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
166 {
167     BOOL ret;
168     SERVER_START_REQ( cancel_timer )
169     {
170         req->handle = handle;
171         ret = !wine_server_call_err( req );
172     }
173     SERVER_END_REQ;
174     return ret;
175 }