dinput: Implement GetDeviceInfo for evdev joystick.
[wine/wine64.git] / server / event.c
bloba7d65b36bea41b632b4d416a6125f86762160397
1 /*
2 * Server-side event management
4 * Copyright (C) 1998 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
38 struct event
40 struct object obj; /* object header */
41 int manual_reset; /* is it a manual reset event? */
42 int signaled; /* event has been signaled */
45 static void event_dump( struct object *obj, int verbose );
46 static int event_signaled( struct object *obj, struct thread *thread );
47 static int event_satisfied( struct object *obj, struct thread *thread );
48 static unsigned int event_map_access( struct object *obj, unsigned int access );
49 static int event_signal( struct object *obj, unsigned int access);
51 static const struct object_ops event_ops =
53 sizeof(struct event), /* size */
54 event_dump, /* dump */
55 add_queue, /* add_queue */
56 remove_queue, /* remove_queue */
57 event_signaled, /* signaled */
58 event_satisfied, /* satisfied */
59 event_signal, /* signal */
60 no_get_fd, /* get_fd */
61 event_map_access, /* map_access */
62 no_lookup_name, /* lookup_name */
63 no_open_file, /* open_file */
64 no_close_handle, /* close_handle */
65 no_destroy /* destroy */
69 struct event *create_event( struct directory *root, const struct unicode_str *name,
70 unsigned int attr, int manual_reset, int initial_state )
72 struct event *event;
74 if ((event = create_named_object_dir( root, name, attr, &event_ops )))
76 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
78 /* initialize it if it didn't already exist */
79 event->manual_reset = manual_reset;
80 event->signaled = initial_state;
83 return event;
86 struct event *get_event_obj( struct process *process, obj_handle_t handle, unsigned int access )
88 return (struct event *)get_handle_obj( process, handle, access, &event_ops );
91 void pulse_event( struct event *event )
93 event->signaled = 1;
94 /* wake up all waiters if manual reset, a single one otherwise */
95 wake_up( &event->obj, !event->manual_reset );
96 event->signaled = 0;
99 void set_event( struct event *event )
101 event->signaled = 1;
102 /* wake up all waiters if manual reset, a single one otherwise */
103 wake_up( &event->obj, !event->manual_reset );
106 void reset_event( struct event *event )
108 event->signaled = 0;
111 static void event_dump( struct object *obj, int verbose )
113 struct event *event = (struct event *)obj;
114 assert( obj->ops == &event_ops );
115 fprintf( stderr, "Event manual=%d signaled=%d ",
116 event->manual_reset, event->signaled );
117 dump_object_name( &event->obj );
118 fputc( '\n', stderr );
121 static int event_signaled( struct object *obj, struct thread *thread )
123 struct event *event = (struct event *)obj;
124 assert( obj->ops == &event_ops );
125 return event->signaled;
128 static int event_satisfied( struct object *obj, struct thread *thread )
130 struct event *event = (struct event *)obj;
131 assert( obj->ops == &event_ops );
132 /* Reset if it's an auto-reset event */
133 if (!event->manual_reset) event->signaled = 0;
134 return 0; /* Not abandoned */
137 static unsigned int event_map_access( struct object *obj, unsigned int access )
139 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | EVENT_QUERY_STATE;
140 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
141 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
142 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL | EVENT_ALL_ACCESS;
143 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
146 static int event_signal( struct object *obj, unsigned int access )
148 struct event *event = (struct event *)obj;
149 assert( obj->ops == &event_ops );
151 if (!(access & EVENT_MODIFY_STATE))
153 set_error( STATUS_ACCESS_DENIED );
154 return 0;
156 set_event( event );
157 return 1;
160 /* create an event */
161 DECL_HANDLER(create_event)
163 struct event *event;
164 struct unicode_str name;
165 struct directory *root = NULL;
167 reply->handle = 0;
168 get_req_unicode_str( &name );
169 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
170 return;
172 if ((event = create_event( root, &name, req->attributes, req->manual_reset, req->initial_state )))
174 reply->handle = alloc_handle( current->process, event, req->access, req->attributes );
175 release_object( event );
178 if (root) release_object( root );
181 /* open a handle to an event */
182 DECL_HANDLER(open_event)
184 struct unicode_str name;
185 struct directory *root = NULL;
186 struct event *event;
188 get_req_unicode_str( &name );
189 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
190 return;
192 if ((event = open_object_dir( root, &name, req->attributes, &event_ops )))
194 reply->handle = alloc_handle( current->process, &event->obj, req->access, req->attributes );
195 release_object( event );
198 if (root) release_object( root );
201 /* do an event operation */
202 DECL_HANDLER(event_op)
204 struct event *event;
206 if (!(event = get_event_obj( current->process, req->handle, EVENT_MODIFY_STATE ))) return;
207 switch(req->op)
209 case PULSE_EVENT:
210 pulse_event( event );
211 break;
212 case SET_EVENT:
213 set_event( event );
214 break;
215 case RESET_EVENT:
216 reset_event( event );
217 break;
218 default:
219 set_error( STATUS_INVALID_PARAMETER );
220 break;
222 release_object( event );