crypt32: Abstract contexts.
[wine/multimedia.git] / server / winstation.c
blob12392986412e330a39c386215b7ce5b307da62fc
1 /*
2 * Server-side window stations and desktops handling
4 * Copyright (C) 2002, 2005 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 <stdio.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winternl.h"
34 #include "object.h"
35 #include "handle.h"
36 #include "request.h"
37 #include "process.h"
38 #include "user.h"
39 #include "file.h"
40 #include "wine/unicode.h"
43 static struct list winstation_list = LIST_INIT(winstation_list);
44 static struct winstation *interactive_winstation;
45 static struct namespace *winstation_namespace;
47 static void winstation_dump( struct object *obj, int verbose );
48 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
49 static void winstation_destroy( struct object *obj );
50 static void desktop_dump( struct object *obj, int verbose );
51 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
52 static void desktop_destroy( struct object *obj );
54 static const struct object_ops winstation_ops =
56 sizeof(struct winstation), /* size */
57 winstation_dump, /* dump */
58 no_add_queue, /* add_queue */
59 NULL, /* remove_queue */
60 NULL, /* signaled */
61 NULL, /* satisfied */
62 no_signal, /* signal */
63 no_get_fd, /* get_fd */
64 no_map_access, /* map_access */
65 no_lookup_name, /* lookup_name */
66 winstation_close_handle, /* close_handle */
67 winstation_destroy /* destroy */
71 static const struct object_ops desktop_ops =
73 sizeof(struct desktop), /* size */
74 desktop_dump, /* dump */
75 no_add_queue, /* add_queue */
76 NULL, /* remove_queue */
77 NULL, /* signaled */
78 NULL, /* satisfied */
79 no_signal, /* signal */
80 no_get_fd, /* get_fd */
81 no_map_access, /* map_access */
82 no_lookup_name, /* lookup_name */
83 desktop_close_handle, /* close_handle */
84 desktop_destroy /* destroy */
87 #define DESKTOP_ALL_ACCESS 0x01ff
89 /* create a winstation object */
90 static struct winstation *create_winstation( const struct unicode_str *name, unsigned int attr,
91 unsigned int flags )
93 struct winstation *winstation;
95 if (!winstation_namespace && !(winstation_namespace = create_namespace( 7 )))
96 return NULL;
98 if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) )) /* no backslash allowed in name */
100 set_error( STATUS_INVALID_PARAMETER );
101 return NULL;
104 if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, attr )))
106 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
108 /* initialize it if it didn't already exist */
109 winstation->flags = flags;
110 winstation->clipboard = NULL;
111 winstation->atom_table = NULL;
112 list_add_tail( &winstation_list, &winstation->entry );
113 list_init( &winstation->desktops );
116 return winstation;
119 static void winstation_dump( struct object *obj, int verbose )
121 struct winstation *winstation = (struct winstation *)obj;
123 fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p ",
124 winstation->flags, winstation->clipboard, winstation->atom_table );
125 dump_object_name( &winstation->obj );
126 fputc( '\n', stderr );
129 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
131 return (process->winstation != handle);
134 static void winstation_destroy( struct object *obj )
136 struct winstation *winstation = (struct winstation *)obj;
138 if (winstation == interactive_winstation) interactive_winstation = NULL;
139 list_remove( &winstation->entry );
140 if (winstation->clipboard) release_object( winstation->clipboard );
141 if (winstation->atom_table) release_object( winstation->atom_table );
144 /* retrieve the process window station, checking the handle access rights */
145 struct winstation *get_process_winstation( struct process *process, unsigned int access )
147 return (struct winstation *)get_handle_obj( process, process->winstation,
148 access, &winstation_ops );
151 /* build the full name of a desktop object */
152 static WCHAR *build_desktop_name( const struct unicode_str *name,
153 struct winstation *winstation, struct unicode_str *res )
155 const WCHAR *winstation_name;
156 WCHAR *full_name;
157 size_t winstation_len;
159 if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))
161 set_error( STATUS_INVALID_PARAMETER );
162 return NULL;
165 if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
166 winstation_len = 0;
168 res->len = winstation_len + name->len + sizeof(WCHAR);
169 if (!(full_name = mem_alloc( res->len ))) return NULL;
170 memcpy( full_name, winstation_name, winstation_len );
171 full_name[winstation_len / sizeof(WCHAR)] = '\\';
172 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name->str, name->len );
173 res->str = full_name;
174 return full_name;
177 /* retrieve a pointer to a desktop object */
178 inline static struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
179 unsigned int access )
181 return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
184 /* create a desktop object */
185 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
186 unsigned int flags, struct winstation *winstation )
188 struct desktop *desktop;
189 struct unicode_str full_str;
190 WCHAR *full_name;
192 if (!(full_name = build_desktop_name( name, winstation, &full_str ))) return NULL;
194 if ((desktop = create_named_object( winstation_namespace, &desktop_ops, &full_str, attr )))
196 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
198 /* initialize it if it didn't already exist */
199 desktop->flags = flags;
200 desktop->winstation = (struct winstation *)grab_object( winstation );
201 desktop->top_window = NULL;
202 desktop->global_hooks = NULL;
203 desktop->close_timeout = NULL;
204 desktop->users = 0;
205 list_add_tail( &winstation->desktops, &desktop->entry );
208 free( full_name );
209 return desktop;
212 static void desktop_dump( struct object *obj, int verbose )
214 struct desktop *desktop = (struct desktop *)obj;
216 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
217 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
218 dump_object_name( &desktop->obj );
219 fputc( '\n', stderr );
222 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
224 struct thread *thread;
226 /* check if the handle is currently used by the process or one of its threads */
227 if (process->desktop == handle) return 0;
228 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
229 if (thread->desktop == handle) return 0;
230 return 1;
233 static void desktop_destroy( struct object *obj )
235 struct desktop *desktop = (struct desktop *)obj;
237 if (desktop->top_window) destroy_window( desktop->top_window );
238 if (desktop->global_hooks) release_object( desktop->global_hooks );
239 if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
240 list_remove( &desktop->entry );
241 release_object( desktop->winstation );
244 /* retrieve the thread desktop, checking the handle access rights */
245 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
247 return get_desktop_obj( thread->process, thread->desktop, access );
250 /* connect a process to its window station */
251 void connect_process_winstation( struct process *process, const struct unicode_str *name )
253 struct winstation *winstation;
255 if (process->winstation) return; /* already has one */
257 /* check for an inherited winstation handle (don't ask...) */
258 if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
260 if (name)
262 winstation = create_winstation( name, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0 );
264 else
266 if (!interactive_winstation)
268 static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
269 static const struct unicode_str winsta0 = { winsta0W, sizeof(winsta0W) };
270 interactive_winstation = create_winstation( &winsta0, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0 );
271 winstation = interactive_winstation;
273 else winstation = (struct winstation *)grab_object( interactive_winstation );
275 if (winstation)
277 process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, 0 );
278 release_object( winstation );
280 clear_error(); /* ignore errors */
283 /* connect a process to its main desktop */
284 void connect_process_desktop( struct process *process, const struct unicode_str *name )
286 struct desktop *desktop;
287 struct winstation *winstation;
289 if (process->desktop) return; /* already has one */
291 if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
293 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
294 static const struct unicode_str default_str = { defaultW, sizeof(defaultW) };
296 if (!name) name = &default_str;
297 if ((desktop = create_desktop( name, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0, winstation )))
299 process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, 0 );
300 desktop->users++;
301 if (desktop->close_timeout)
303 remove_timeout_user( desktop->close_timeout );
304 desktop->close_timeout = NULL;
306 release_object( desktop );
308 release_object( winstation );
310 clear_error(); /* ignore errors */
313 static void close_desktop_timeout( void *private )
315 struct desktop *desktop = private;
317 desktop->close_timeout = NULL;
318 unlink_named_object( &desktop->obj ); /* make sure no other process can open it */
319 close_desktop_window( desktop ); /* and signal the owner to quit */
322 /* close the desktop of a given process */
323 void close_process_desktop( struct process *process )
325 struct desktop *desktop;
327 if (process->desktop && (desktop = get_desktop_obj( process, process->desktop, 0 )))
329 assert( desktop->users > 0 );
330 desktop->users--;
331 /* if we have one remaining user, it has to be the manager of the desktop window */
332 if (desktop->users == 1 && get_top_window_owner( desktop ))
334 struct timeval when;
335 gettimeofday( &when, NULL );
336 add_timeout( &when, 1000 );
337 desktop->close_timeout = add_timeout_user( &when, close_desktop_timeout, desktop );
339 release_object( desktop );
341 clear_error(); /* ignore errors */
344 /* close the desktop of a given thread */
345 void close_thread_desktop( struct thread *thread )
347 obj_handle_t handle = thread->desktop;
349 thread->desktop = 0;
350 if (handle) close_handle( thread->process, handle, NULL );
351 clear_error(); /* ignore errors */
355 /* create a window station */
356 DECL_HANDLER(create_winstation)
358 struct winstation *winstation;
359 struct unicode_str name;
361 reply->handle = 0;
362 get_req_unicode_str( &name );
363 if ((winstation = create_winstation( &name, req->attributes, req->flags )))
365 reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
366 release_object( winstation );
370 /* open a handle to a window station */
371 DECL_HANDLER(open_winstation)
373 struct unicode_str name;
375 get_req_unicode_str( &name );
376 if (winstation_namespace)
377 reply->handle = open_object( winstation_namespace, &name, &winstation_ops, req->access,
378 req->attributes );
379 else
380 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
384 /* close a window station */
385 DECL_HANDLER(close_winstation)
387 struct winstation *winstation;
389 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
390 0, &winstation_ops )))
392 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_ACCESS_DENIED );
393 release_object( winstation );
398 /* get the process current window station */
399 DECL_HANDLER(get_process_winstation)
401 reply->handle = current->process->winstation;
405 /* set the process current window station */
406 DECL_HANDLER(set_process_winstation)
408 struct winstation *winstation;
410 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
411 0, &winstation_ops )))
413 /* FIXME: should we close the old one? */
414 current->process->winstation = req->handle;
415 release_object( winstation );
419 /* create a desktop */
420 DECL_HANDLER(create_desktop)
422 struct desktop *desktop;
423 struct winstation *winstation;
424 struct unicode_str name;
426 reply->handle = 0;
427 get_req_unicode_str( &name );
428 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
430 if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
432 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
433 release_object( desktop );
435 release_object( winstation );
439 /* open a handle to a desktop */
440 DECL_HANDLER(open_desktop)
442 struct winstation *winstation;
443 struct unicode_str name;
445 get_req_unicode_str( &name );
446 if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
448 struct unicode_str full_str;
449 WCHAR *full_name;
451 if ((full_name = build_desktop_name( &name, winstation, &full_str )))
453 reply->handle = open_object( winstation_namespace, &full_str, &desktop_ops, req->access,
454 req->attributes );
455 free( full_name );
457 release_object( winstation );
462 /* close a desktop */
463 DECL_HANDLER(close_desktop)
465 struct desktop *desktop;
467 /* make sure it is a desktop handle */
468 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
469 0, &desktop_ops )))
471 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
472 release_object( desktop );
477 /* get the thread current desktop */
478 DECL_HANDLER(get_thread_desktop)
480 struct thread *thread;
482 if (!(thread = get_thread_from_id( req->tid ))) return;
483 reply->handle = thread->desktop;
484 release_object( thread );
488 /* set the thread current desktop */
489 DECL_HANDLER(set_thread_desktop)
491 struct desktop *old_desktop, *new_desktop;
492 struct winstation *winstation;
494 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
495 return;
497 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
499 release_object( winstation );
500 return;
502 if (new_desktop->winstation != winstation)
504 set_error( STATUS_ACCESS_DENIED );
505 release_object( new_desktop );
506 release_object( winstation );
507 return;
510 /* check if we are changing to a new desktop */
512 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
513 clear_error(); /* ignore error */
515 /* when changing desktop, we can't have any users on the current one */
516 if (old_desktop != new_desktop && current->desktop_users > 0)
517 set_error( STATUS_DEVICE_BUSY );
518 else
519 current->desktop = req->handle; /* FIXME: should we close the old one? */
521 if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
523 if (old_desktop) release_object( old_desktop );
524 release_object( new_desktop );
525 release_object( winstation );
529 /* get/set information about a user object (window station or desktop) */
530 DECL_HANDLER(set_user_object_info)
532 struct object *obj;
534 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
536 if (obj->ops == &desktop_ops)
538 struct desktop *desktop = (struct desktop *)obj;
539 reply->is_desktop = 1;
540 reply->old_obj_flags = desktop->flags;
541 if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
543 else if (obj->ops == &winstation_ops)
545 struct winstation *winstation = (struct winstation *)obj;
546 reply->is_desktop = 0;
547 reply->old_obj_flags = winstation->flags;
548 if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
550 else
552 set_error( STATUS_OBJECT_TYPE_MISMATCH );
553 release_object( obj );
554 return;
556 if (get_reply_max_size())
558 size_t len;
559 const WCHAR *ptr, *name = get_object_name( obj, &len );
561 /* if there is a backslash return the part of the name after it */
562 if (name && (ptr = memchrW( name, '\\', len )))
564 len -= (ptr + 1 - name) * sizeof(WCHAR);
565 name = ptr + 1;
567 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
569 release_object( obj );