riched20: EM_SETCHARFORMAT must fail and return 0 with TM_PLAINTEXT and SF_SELECTION.
[wine] / server / timer.c
1 /*
2  * Waitable timers management
3  *
4  * Copyright (C) 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <stdarg.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35
36 #include "file.h"
37 #include "handle.h"
38 #include "request.h"
39
40 struct timer
41 {
42     struct object        obj;       /* object header */
43     int                  manual;    /* manual reset */
44     int                  signaled;  /* current signaled state */
45     unsigned int         period;    /* timer period in ms */
46     timeout_t            when;      /* next expiration */
47     struct timeout_user *timeout;   /* timeout user */
48     struct thread       *thread;    /* thread that set the APC function */
49     void                *callback;  /* callback APC function */
50     void                *arg;       /* callback argument */
51 };
52
53 static void timer_dump( struct object *obj, int verbose );
54 static int timer_signaled( struct object *obj, struct thread *thread );
55 static int timer_satisfied( struct object *obj, struct thread *thread );
56 static unsigned int timer_map_access( struct object *obj, unsigned int access );
57 static void timer_destroy( struct object *obj );
58
59 static const struct object_ops timer_ops =
60 {
61     sizeof(struct timer),      /* size */
62     timer_dump,                /* dump */
63     add_queue,                 /* add_queue */
64     remove_queue,              /* remove_queue */
65     timer_signaled,            /* signaled */
66     timer_satisfied,           /* satisfied */
67     no_signal,                 /* signal */
68     no_get_fd,                 /* get_fd */
69     timer_map_access,          /* map_access */
70     no_lookup_name,            /* lookup_name */
71     no_open_file,              /* open_file */
72     no_close_handle,           /* close_handle */
73     timer_destroy              /* destroy */
74 };
75
76
77 /* create a timer object */
78 static struct timer *create_timer( struct directory *root, const struct unicode_str *name,
79                                    unsigned int attr, int manual )
80 {
81     struct timer *timer;
82
83     if ((timer = create_named_object_dir( root, name, attr, &timer_ops )))
84     {
85         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
86         {
87             /* initialize it if it didn't already exist */
88             timer->manual   = manual;
89             timer->signaled = 0;
90             timer->when     = 0;
91             timer->period   = 0;
92             timer->timeout  = NULL;
93             timer->thread   = NULL;
94         }
95     }
96     return timer;
97 }
98
99 /* callback on timer expiration */
100 static void timer_callback( void *private )
101 {
102     struct timer *timer = (struct timer *)private;
103
104     /* queue an APC */
105     if (timer->thread)
106     {
107         apc_call_t data;
108
109         memset( &data, 0, sizeof(data) );
110         if (timer->callback)
111         {
112             data.type       = APC_TIMER;
113             data.timer.func = timer->callback;
114             data.timer.time = timer->when;
115             data.timer.arg  = timer->arg;
116         }
117         else data.type = APC_NONE;  /* wake up only */
118
119         if (!thread_queue_apc( timer->thread, &timer->obj, &data ))
120         {
121             release_object( timer->thread );
122             timer->thread = NULL;
123         }
124     }
125
126     if (timer->period)  /* schedule the next expiration */
127     {
128         timer->when += (timeout_t)timer->period * 10000;
129         timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
130     }
131     else timer->timeout = NULL;
132
133     /* wake up waiters */
134     timer->signaled = 1;
135     wake_up( &timer->obj, 0 );
136 }
137
138 /* cancel a running timer */
139 static int cancel_timer( struct timer *timer )
140 {
141     int signaled = timer->signaled;
142
143     if (timer->timeout)
144     {
145         remove_timeout_user( timer->timeout );
146         timer->timeout = NULL;
147     }
148     if (timer->thread)
149     {
150         thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
151         release_object( timer->thread );
152         timer->thread = NULL;
153     }
154     return signaled;
155 }
156
157 /* set the timer expiration and period */
158 static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
159                       void *callback, void *arg )
160 {
161     int signaled = cancel_timer( timer );
162     if (timer->manual)
163     {
164         period = 0;  /* period doesn't make any sense for a manual timer */
165         timer->signaled = 0;
166     }
167     timer->when     = (expire <= 0) ? current_time - expire : max( expire, current_time );
168     timer->period   = period;
169     timer->callback = callback;
170     timer->arg      = arg;
171     if (callback) timer->thread = (struct thread *)grab_object( current );
172     timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
173     return signaled;
174 }
175
176 static void timer_dump( struct object *obj, int verbose )
177 {
178     struct timer *timer = (struct timer *)obj;
179     assert( obj->ops == &timer_ops );
180     fprintf( stderr, "Timer manual=%d when=%s period=%u ",
181              timer->manual, get_timeout_str(timer->when), timer->period );
182     dump_object_name( &timer->obj );
183     fputc( '\n', stderr );
184 }
185
186 static int timer_signaled( struct object *obj, struct thread *thread )
187 {
188     struct timer *timer = (struct timer *)obj;
189     assert( obj->ops == &timer_ops );
190     return timer->signaled;
191 }
192
193 static int timer_satisfied( struct object *obj, struct thread *thread )
194 {
195     struct timer *timer = (struct timer *)obj;
196     assert( obj->ops == &timer_ops );
197     if (!timer->manual) timer->signaled = 0;
198     return 0;
199 }
200
201 static unsigned int timer_map_access( struct object *obj, unsigned int access )
202 {
203     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
204     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
205     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
206     if (access & GENERIC_ALL)     access |= TIMER_ALL_ACCESS;
207     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
208 }
209
210 static void timer_destroy( struct object *obj )
211 {
212     struct timer *timer = (struct timer *)obj;
213     assert( obj->ops == &timer_ops );
214
215     if (timer->timeout) remove_timeout_user( timer->timeout );
216     if (timer->thread) release_object( timer->thread );
217 }
218
219 /* create a timer */
220 DECL_HANDLER(create_timer)
221 {
222     struct timer *timer;
223     struct unicode_str name;
224     struct directory *root = NULL;
225
226     reply->handle = 0;
227     get_req_unicode_str( &name );
228     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
229         return;
230
231     if ((timer = create_timer( root, &name, req->attributes, req->manual )))
232     {
233         reply->handle = alloc_handle( current->process, timer, req->access, req->attributes );
234         release_object( timer );
235     }
236
237     if (root) release_object( root );
238 }
239
240 /* open a handle to a timer */
241 DECL_HANDLER(open_timer)
242 {
243     struct unicode_str name;
244     struct directory *root = NULL;
245     struct timer *timer;
246
247     get_req_unicode_str( &name );
248     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
249         return;
250
251     if ((timer = open_object_dir( root, &name, req->attributes, &timer_ops )))
252     {
253         reply->handle = alloc_handle( current->process, &timer->obj, req->access, req->attributes );
254         release_object( timer );
255     }
256
257     if (root) release_object( root );
258 }
259
260 /* set a waitable timer */
261 DECL_HANDLER(set_timer)
262 {
263     struct timer *timer;
264
265     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
266                                                  TIMER_MODIFY_STATE, &timer_ops )))
267     {
268         reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
269         release_object( timer );
270     }
271 }
272
273 /* cancel a waitable timer */
274 DECL_HANDLER(cancel_timer)
275 {
276     struct timer *timer;
277
278     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
279                                                  TIMER_MODIFY_STATE, &timer_ops )))
280     {
281         reply->signaled = cancel_timer( timer );
282         release_object( timer );
283     }
284 }
285
286 /* Get information on a waitable timer */
287 DECL_HANDLER(get_timer_info)
288 {
289     struct timer *timer;
290
291     if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
292                                                  TIMER_QUERY_STATE, &timer_ops )))
293     {
294         reply->when      = timer->when;
295         reply->signaled  = timer->signaled;
296         release_object( timer );
297     }
298 }