Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Win32 waitable timers |
| 3 | * |
| 4 | * Copyright 1999 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | #include "winerror.h" |
Alexandre Julliard | 072dfb5 | 2000-09-25 23:30:56 +0000 | [diff] [blame] | 10 | #include "winnls.h" |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 11 | #include "wine/unicode.h" |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 12 | #include "file.h" /* for FILETIME routines */ |
| 13 | #include "server.h" |
| 14 | |
| 15 | |
| 16 | /*********************************************************************** |
| 17 | * CreateWaitableTimerA (KERNEL32.861) |
| 18 | */ |
| 19 | HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name ) |
| 20 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 21 | HANDLE ret; |
| 22 | DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0; |
| 23 | if (len >= MAX_PATH) |
| 24 | { |
| 25 | SetLastError( ERROR_FILENAME_EXCED_RANGE ); |
| 26 | return 0; |
| 27 | } |
| 28 | SERVER_START_REQ |
| 29 | { |
| 30 | struct create_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 31 | |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 32 | req->manual = manual; |
| 33 | req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle); |
| 34 | if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len ); |
| 35 | SetLastError(0); |
| 36 | server_call( REQ_CREATE_TIMER ); |
| 37 | ret = req->handle; |
| 38 | } |
| 39 | SERVER_END_REQ; |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 40 | if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */ |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 41 | return ret; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
| 45 | /*********************************************************************** |
| 46 | * CreateWaitableTimerW (KERNEL32.862) |
| 47 | */ |
| 48 | HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name ) |
| 49 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 50 | HANDLE ret; |
| 51 | DWORD len = name ? strlenW(name) : 0; |
| 52 | if (len >= MAX_PATH) |
| 53 | { |
| 54 | SetLastError( ERROR_FILENAME_EXCED_RANGE ); |
| 55 | return 0; |
| 56 | } |
| 57 | SERVER_START_REQ |
| 58 | { |
| 59 | struct create_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 60 | |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 61 | req->manual = manual; |
| 62 | req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle); |
| 63 | memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) ); |
| 64 | SetLastError(0); |
| 65 | server_call( REQ_CREATE_TIMER ); |
| 66 | ret = req->handle; |
| 67 | } |
| 68 | SERVER_END_REQ; |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 69 | if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */ |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 70 | return ret; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | /*********************************************************************** |
| 75 | * OpenWaitableTimerA (KERNEL32.881) |
| 76 | */ |
| 77 | HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name ) |
| 78 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 79 | HANDLE ret; |
| 80 | DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0; |
| 81 | if (len >= MAX_PATH) |
| 82 | { |
| 83 | SetLastError( ERROR_FILENAME_EXCED_RANGE ); |
| 84 | return 0; |
| 85 | } |
| 86 | SERVER_START_REQ |
| 87 | { |
| 88 | struct open_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 89 | |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 90 | req->access = access; |
| 91 | req->inherit = inherit; |
| 92 | if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len ); |
| 93 | server_call( REQ_OPEN_TIMER ); |
| 94 | ret = req->handle; |
| 95 | } |
| 96 | SERVER_END_REQ; |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 97 | if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */ |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 98 | return ret; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | /*********************************************************************** |
| 103 | * OpenWaitableTimerW (KERNEL32.882) |
| 104 | */ |
| 105 | HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name ) |
| 106 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 107 | HANDLE ret; |
| 108 | DWORD len = name ? strlenW(name) : 0; |
| 109 | if (len >= MAX_PATH) |
| 110 | { |
| 111 | SetLastError( ERROR_FILENAME_EXCED_RANGE ); |
| 112 | return 0; |
| 113 | } |
| 114 | SERVER_START_REQ |
| 115 | { |
| 116 | struct open_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 117 | |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 118 | req->access = access; |
| 119 | req->inherit = inherit; |
| 120 | memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) ); |
| 121 | server_call( REQ_OPEN_TIMER ); |
| 122 | ret = req->handle; |
| 123 | } |
| 124 | SERVER_END_REQ; |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 125 | if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */ |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 126 | return ret; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
| 130 | /*********************************************************************** |
| 131 | * SetWaitableTimer (KERNEL32.894) |
| 132 | */ |
| 133 | BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period, |
| 134 | PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume ) |
| 135 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 136 | BOOL ret; |
Alexandre Julliard | d76f9f9 | 2000-10-01 01:40:42 +0000 | [diff] [blame] | 137 | LARGE_INTEGER exp = *when; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 138 | |
Alexandre Julliard | d76f9f9 | 2000-10-01 01:40:42 +0000 | [diff] [blame] | 139 | if (exp.s.HighPart < 0) /* relative time */ |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 140 | { |
Alexandre Julliard | d76f9f9 | 2000-10-01 01:40:42 +0000 | [diff] [blame] | 141 | LARGE_INTEGER now; |
| 142 | NtQuerySystemTime( &now ); |
| 143 | exp.QuadPart = RtlLargeIntegerSubtract( now.QuadPart, exp.QuadPart ); |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 144 | } |
Alexandre Julliard | 000c980 | 1999-12-13 01:42:03 +0000 | [diff] [blame] | 145 | |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 146 | SERVER_START_REQ |
Alexandre Julliard | 000c980 | 1999-12-13 01:42:03 +0000 | [diff] [blame] | 147 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 148 | struct set_timer_request *req = server_alloc_req( sizeof(*req), 0 ); |
| 149 | |
Alexandre Julliard | d76f9f9 | 2000-10-01 01:40:42 +0000 | [diff] [blame] | 150 | if (!exp.s.LowPart && !exp.s.HighPart) |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 151 | { |
| 152 | /* special case to start timeout on now+period without too many calculations */ |
| 153 | req->sec = 0; |
| 154 | req->usec = 0; |
| 155 | } |
| 156 | else |
| 157 | { |
Alexandre Julliard | d76f9f9 | 2000-10-01 01:40:42 +0000 | [diff] [blame] | 158 | DWORD remainder; |
| 159 | req->sec = DOSFS_FileTimeToUnixTime( (FILETIME *)&exp, &remainder ); |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 160 | req->usec = remainder / 10; /* convert from 100-ns to us units */ |
| 161 | } |
| 162 | req->handle = handle; |
| 163 | req->period = period; |
| 164 | req->callback = callback; |
| 165 | req->arg = arg; |
| 166 | if (resume) SetLastError( ERROR_NOT_SUPPORTED ); /* set error but can still succeed */ |
| 167 | ret = !server_call( REQ_SET_TIMER ); |
Alexandre Julliard | 000c980 | 1999-12-13 01:42:03 +0000 | [diff] [blame] | 168 | } |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 169 | SERVER_END_REQ; |
| 170 | return ret; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | /*********************************************************************** |
| 175 | * CancelWaitableTimer (KERNEL32.857) |
| 176 | */ |
| 177 | BOOL WINAPI CancelWaitableTimer( HANDLE handle ) |
| 178 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 179 | BOOL ret; |
| 180 | SERVER_START_REQ |
| 181 | { |
| 182 | struct cancel_timer_request *req = server_alloc_req( sizeof(*req), 0 ); |
| 183 | req->handle = handle; |
| 184 | ret = !server_call( REQ_CANCEL_TIMER ); |
| 185 | } |
| 186 | SERVER_END_REQ; |
| 187 | return ret; |
Alexandre Julliard | ad47a30 | 1999-11-29 01:58:35 +0000 | [diff] [blame] | 188 | } |