msi: Refreh controls after spawned dialog is closed.
[wine.git] / server / winstation.c
blobece65c337d21b68395e728bf66ec9ffc89190f74
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "security.h"
41 #include "wine/unicode.h"
44 static struct list winstation_list = LIST_INIT(winstation_list);
46 static void winstation_dump( struct object *obj, int verbose );
47 static struct object_type *winstation_get_type( struct object *obj );
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 unsigned int winstation_map_access( struct object *obj, unsigned int access );
51 static void desktop_dump( struct object *obj, int verbose );
52 static struct object_type *desktop_get_type( struct object *obj );
53 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
54 static void desktop_destroy( struct object *obj );
55 static unsigned int desktop_map_access( struct object *obj, unsigned int access );
57 static const struct object_ops winstation_ops =
59 sizeof(struct winstation), /* size */
60 winstation_dump, /* dump */
61 winstation_get_type, /* get_type */
62 no_add_queue, /* add_queue */
63 NULL, /* remove_queue */
64 NULL, /* signaled */
65 NULL, /* satisfied */
66 no_signal, /* signal */
67 no_get_fd, /* get_fd */
68 winstation_map_access, /* map_access */
69 default_get_sd, /* get_sd */
70 default_set_sd, /* set_sd */
71 no_lookup_name, /* lookup_name */
72 no_open_file, /* open_file */
73 winstation_close_handle, /* close_handle */
74 winstation_destroy /* destroy */
78 static const struct object_ops desktop_ops =
80 sizeof(struct desktop), /* size */
81 desktop_dump, /* dump */
82 desktop_get_type, /* get_type */
83 no_add_queue, /* add_queue */
84 NULL, /* remove_queue */
85 NULL, /* signaled */
86 NULL, /* satisfied */
87 no_signal, /* signal */
88 no_get_fd, /* get_fd */
89 desktop_map_access, /* map_access */
90 default_get_sd, /* get_sd */
91 default_set_sd, /* set_sd */
92 no_lookup_name, /* lookup_name */
93 no_open_file, /* open_file */
94 desktop_close_handle, /* close_handle */
95 desktop_destroy /* destroy */
98 #define DESKTOP_ALL_ACCESS 0x01ff
100 /* create a winstation object */
101 static struct winstation *create_winstation( struct directory *root, const struct unicode_str *name,
102 unsigned int attr, unsigned int flags )
104 struct winstation *winstation;
106 if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) )) /* no backslash allowed in name */
108 set_error( STATUS_INVALID_PARAMETER );
109 return NULL;
112 if ((winstation = create_named_object_dir( root, name, attr, &winstation_ops )))
114 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
116 /* initialize it if it didn't already exist */
117 winstation->flags = flags;
118 winstation->clipboard = NULL;
119 winstation->atom_table = NULL;
120 list_add_tail( &winstation_list, &winstation->entry );
121 list_init( &winstation->desktops );
122 if (!(winstation->desktop_names = create_namespace( 7 )))
124 release_object( winstation );
125 return NULL;
129 return winstation;
132 static void winstation_dump( struct object *obj, int verbose )
134 struct winstation *winstation = (struct winstation *)obj;
136 fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p\n",
137 winstation->flags, winstation->clipboard, winstation->atom_table );
140 static struct object_type *winstation_get_type( struct object *obj )
142 static const WCHAR name[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n'};
143 static const struct unicode_str str = { name, sizeof(name) };
144 return get_object_type( &str );
147 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
149 return (process->winstation != handle);
152 static void winstation_destroy( struct object *obj )
154 struct winstation *winstation = (struct winstation *)obj;
156 list_remove( &winstation->entry );
157 if (winstation->clipboard) release_object( winstation->clipboard );
158 if (winstation->atom_table) release_object( winstation->atom_table );
159 free( winstation->desktop_names );
162 static unsigned int winstation_map_access( struct object *obj, unsigned int access )
164 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | WINSTA_ENUMDESKTOPS | WINSTA_READATTRIBUTES |
165 WINSTA_ENUMERATE | WINSTA_READSCREEN;
166 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | WINSTA_ACCESSCLIPBOARD | WINSTA_CREATEDESKTOP |
167 WINSTA_WRITEATTRIBUTES;
168 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS;
169 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS;
170 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
173 /* retrieve the process window station, checking the handle access rights */
174 struct winstation *get_process_winstation( struct process *process, unsigned int access )
176 return (struct winstation *)get_handle_obj( process, process->winstation,
177 access, &winstation_ops );
180 /* retrieve a pointer to a desktop object */
181 struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle, unsigned int access )
183 return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
186 /* create a desktop object */
187 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
188 unsigned int flags, struct winstation *winstation )
190 struct desktop *desktop;
192 if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) )) /* no backslash allowed in name */
194 set_error( STATUS_INVALID_PARAMETER );
195 return NULL;
198 if ((desktop = create_named_object( winstation->desktop_names, &desktop_ops, name, attr )))
200 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
202 /* initialize it if it didn't already exist */
203 desktop->flags = flags;
204 desktop->winstation = (struct winstation *)grab_object( winstation );
205 desktop->top_window = NULL;
206 desktop->msg_window = NULL;
207 desktop->global_hooks = NULL;
208 desktop->close_timeout = NULL;
209 desktop->foreground_input = NULL;
210 desktop->users = 0;
211 memset( &desktop->cursor, 0, sizeof(desktop->cursor) );
212 memset( desktop->keystate, 0, sizeof(desktop->keystate) );
213 list_add_tail( &winstation->desktops, &desktop->entry );
214 list_init( &desktop->hotkeys );
217 return desktop;
220 static void desktop_dump( struct object *obj, int verbose )
222 struct desktop *desktop = (struct desktop *)obj;
224 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p\n",
225 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
228 static struct object_type *desktop_get_type( struct object *obj )
230 static const WCHAR name[] = {'D','e','s','k','t','o','p'};
231 static const struct unicode_str str = { name, sizeof(name) };
232 return get_object_type( &str );
235 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
237 struct thread *thread;
239 /* check if the handle is currently used by the process or one of its threads */
240 if (process->desktop == handle) return 0;
241 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
242 if (thread->desktop == handle) return 0;
243 return 1;
246 static void desktop_destroy( struct object *obj )
248 struct desktop *desktop = (struct desktop *)obj;
250 free_hotkeys( desktop, 0 );
251 if (desktop->top_window) destroy_window( desktop->top_window );
252 if (desktop->msg_window) destroy_window( desktop->msg_window );
253 if (desktop->global_hooks) release_object( desktop->global_hooks );
254 if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
255 list_remove( &desktop->entry );
256 unlink_named_object( obj );
257 release_object( desktop->winstation );
260 static unsigned int desktop_map_access( struct object *obj, unsigned int access )
262 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | DESKTOP_READOBJECTS | DESKTOP_ENUMERATE;
263 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
264 DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
265 DESKTOP_WRITEOBJECTS;
266 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP;
267 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS;
268 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
271 /* retrieve the thread desktop, checking the handle access rights */
272 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
274 return get_desktop_obj( thread->process, thread->desktop, access );
277 static void close_desktop_timeout( void *private )
279 struct desktop *desktop = private;
281 desktop->close_timeout = NULL;
282 unlink_named_object( &desktop->obj ); /* make sure no other process can open it */
283 post_desktop_message( desktop, WM_CLOSE, 0, 0 ); /* and signal the owner to quit */
286 /* add a user of the desktop and cancel the close timeout */
287 static void add_desktop_user( struct desktop *desktop )
289 desktop->users++;
290 if (desktop->close_timeout)
292 remove_timeout_user( desktop->close_timeout );
293 desktop->close_timeout = NULL;
297 /* remove a user of the desktop and start the close timeout if necessary */
298 static void remove_desktop_user( struct desktop *desktop )
300 assert( desktop->users > 0 );
301 desktop->users--;
303 /* if we have one remaining user, it has to be the manager of the desktop window */
304 if (desktop->users == 1 && get_top_window_owner( desktop ))
306 assert( !desktop->close_timeout );
307 desktop->close_timeout = add_timeout_user( -TICKS_PER_SEC, close_desktop_timeout, desktop );
311 /* set the process default desktop handle */
312 void set_process_default_desktop( struct process *process, struct desktop *desktop,
313 obj_handle_t handle )
315 struct thread *thread;
316 struct desktop *old_desktop;
318 if (process->desktop == handle) return; /* nothing to do */
320 if (!(old_desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error();
321 process->desktop = handle;
323 /* set desktop for threads that don't have one yet */
324 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
325 if (!thread->desktop) thread->desktop = handle;
327 if (!process->is_system && desktop != old_desktop)
329 add_desktop_user( desktop );
330 if (old_desktop) remove_desktop_user( old_desktop );
333 if (old_desktop) release_object( old_desktop );
336 /* connect a process to its window station */
337 void connect_process_winstation( struct process *process, struct thread *parent )
339 struct winstation *winstation = NULL;
340 struct desktop *desktop = NULL;
341 obj_handle_t handle;
343 /* check for an inherited winstation handle (don't ask...) */
344 if ((handle = find_inherited_handle( process, &winstation_ops )))
346 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
348 else if (parent && parent->process->winstation)
350 handle = duplicate_handle( parent->process, parent->process->winstation,
351 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
352 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
354 if (!winstation) goto done;
355 process->winstation = handle;
357 if ((handle = find_inherited_handle( process, &desktop_ops )))
359 desktop = get_desktop_obj( process, handle, 0 );
360 if (!desktop || desktop->winstation != winstation) goto done;
362 else if (parent && parent->desktop)
364 desktop = get_desktop_obj( parent->process, parent->desktop, 0 );
365 if (!desktop || desktop->winstation != winstation) goto done;
366 handle = duplicate_handle( parent->process, parent->desktop,
367 process, 0, 0, DUP_HANDLE_SAME_ACCESS );
370 if (handle) set_process_default_desktop( process, desktop, handle );
372 done:
373 if (desktop) release_object( desktop );
374 if (winstation) release_object( winstation );
375 clear_error();
378 /* close the desktop of a given process */
379 void close_process_desktop( struct process *process )
381 struct desktop *desktop;
383 if (process->desktop && (desktop = get_desktop_obj( process, process->desktop, 0 )))
385 remove_desktop_user( desktop );
386 release_object( desktop );
388 clear_error(); /* ignore errors */
391 /* close the desktop of a given thread */
392 void close_thread_desktop( struct thread *thread )
394 obj_handle_t handle = thread->desktop;
396 thread->desktop = 0;
397 if (handle) close_handle( thread->process, handle );
400 /* create a window station */
401 DECL_HANDLER(create_winstation)
403 struct winstation *winstation;
404 struct unicode_str name;
405 struct directory *root = NULL;
407 reply->handle = 0;
408 get_req_unicode_str( &name );
409 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) return;
411 if ((winstation = create_winstation( root, &name, req->attributes, req->flags )))
413 reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
414 release_object( winstation );
416 if (root) release_object( root );
419 /* open a handle to a window station */
420 DECL_HANDLER(open_winstation)
422 struct winstation *winstation;
423 struct unicode_str name;
424 struct directory *root = NULL;
426 get_req_unicode_str( &name );
427 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) return;
429 if ((winstation = open_object_dir( root, &name, req->attributes, &winstation_ops )))
431 reply->handle = alloc_handle( current->process, &winstation->obj, req->access, req->attributes );
432 release_object( winstation );
434 if (root) release_object( root );
438 /* close a window station */
439 DECL_HANDLER(close_winstation)
441 struct winstation *winstation;
443 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
444 0, &winstation_ops )))
446 if (close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
447 release_object( winstation );
452 /* get the process current window station */
453 DECL_HANDLER(get_process_winstation)
455 reply->handle = current->process->winstation;
459 /* set the process current window station */
460 DECL_HANDLER(set_process_winstation)
462 struct winstation *winstation;
464 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
465 0, &winstation_ops )))
467 /* FIXME: should we close the old one? */
468 current->process->winstation = req->handle;
469 release_object( winstation );
473 /* create a desktop */
474 DECL_HANDLER(create_desktop)
476 struct desktop *desktop;
477 struct winstation *winstation;
478 struct unicode_str name;
480 reply->handle = 0;
481 get_req_unicode_str( &name );
482 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
484 if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
486 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
487 release_object( desktop );
489 release_object( winstation );
493 /* open a handle to a desktop */
494 DECL_HANDLER(open_desktop)
496 struct winstation *winstation;
497 struct unicode_str name;
499 get_req_unicode_str( &name );
501 /* FIXME: check access rights */
502 if (!req->winsta)
503 winstation = get_process_winstation( current->process, 0 );
504 else
505 winstation = (struct winstation *)get_handle_obj( current->process, req->winsta, 0, &winstation_ops );
507 if (winstation)
509 reply->handle = open_object( winstation->desktop_names, &name, &desktop_ops,
510 req->access, req->attributes );
511 release_object( winstation );
515 /* open a handle to current input desktop */
516 DECL_HANDLER(open_input_desktop)
518 /* FIXME: check access rights */
519 struct winstation *winstation = get_process_winstation( current->process, 0 );
520 struct desktop *desktop;
522 if (!winstation) return;
524 if (!(winstation->flags & WSF_VISIBLE))
526 set_error( STATUS_ILLEGAL_FUNCTION );
527 release_object( winstation );
528 return;
531 if ((desktop = get_desktop_obj( current->process, current->process->desktop, 0 )))
533 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
534 release_object( desktop );
536 release_object( winstation );
539 /* close a desktop */
540 DECL_HANDLER(close_desktop)
542 struct desktop *desktop;
544 /* make sure it is a desktop handle */
545 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
546 0, &desktop_ops )))
548 if (close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
549 release_object( desktop );
554 /* get the thread current desktop */
555 DECL_HANDLER(get_thread_desktop)
557 struct thread *thread;
559 if (!(thread = get_thread_from_id( req->tid ))) return;
560 reply->handle = thread->desktop;
561 release_object( thread );
565 /* set the thread current desktop */
566 DECL_HANDLER(set_thread_desktop)
568 struct desktop *old_desktop, *new_desktop;
569 struct winstation *winstation;
571 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
572 return;
574 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
576 release_object( winstation );
577 return;
579 if (new_desktop->winstation != winstation)
581 set_error( STATUS_ACCESS_DENIED );
582 release_object( new_desktop );
583 release_object( winstation );
584 return;
587 /* check if we are changing to a new desktop */
589 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
590 clear_error(); /* ignore error */
592 /* when changing desktop, we can't have any users on the current one */
593 if (old_desktop != new_desktop && current->desktop_users > 0)
594 set_error( STATUS_DEVICE_BUSY );
595 else
596 current->desktop = req->handle; /* FIXME: should we close the old one? */
598 if (!current->process->desktop)
599 set_process_default_desktop( current->process, new_desktop, req->handle );
601 if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
603 if (old_desktop) release_object( old_desktop );
604 release_object( new_desktop );
605 release_object( winstation );
609 /* get/set information about a user object (window station or desktop) */
610 DECL_HANDLER(set_user_object_info)
612 struct object *obj;
613 data_size_t len;
615 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
617 if (obj->ops == &desktop_ops)
619 struct desktop *desktop = (struct desktop *)obj;
620 reply->is_desktop = 1;
621 reply->old_obj_flags = desktop->flags;
622 if (req->flags & SET_USER_OBJECT_SET_FLAGS) desktop->flags = req->obj_flags;
624 else if (obj->ops == &winstation_ops)
626 struct winstation *winstation = (struct winstation *)obj;
627 reply->is_desktop = 0;
628 reply->old_obj_flags = winstation->flags;
629 if (req->flags & SET_USER_OBJECT_SET_FLAGS) winstation->flags = req->obj_flags;
631 else
633 set_error( STATUS_OBJECT_TYPE_MISMATCH );
634 release_object( obj );
635 return;
637 if (obj->ops == &desktop_ops && get_reply_max_size() && (req->flags & SET_USER_OBJECT_GET_FULL_NAME))
639 struct desktop *desktop = (struct desktop *)obj;
640 data_size_t winstation_len, desktop_len;
641 const WCHAR *winstation_name = get_object_name( &desktop->winstation->obj, &winstation_len );
642 const WCHAR *desktop_name = get_object_name( obj, &desktop_len );
643 WCHAR *full_name;
645 if (!winstation_name) winstation_len = 0;
646 if (!desktop_name) desktop_len = 0;
647 len = winstation_len + desktop_len + sizeof(WCHAR);
648 if ((full_name = mem_alloc( len )))
650 memcpy( full_name, winstation_name, winstation_len );
651 full_name[winstation_len / sizeof(WCHAR)] = '\\';
652 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, desktop_name, desktop_len );
653 set_reply_data_ptr( full_name, min( len, get_reply_max_size() ));
656 else
658 const WCHAR *name = get_object_name( obj, &len );
659 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
661 release_object( obj );
665 /* enumerate window stations */
666 DECL_HANDLER(enum_winstation)
668 unsigned int index = 0;
669 struct winstation *winsta;
670 const WCHAR *name;
671 data_size_t len;
673 LIST_FOR_EACH_ENTRY( winsta, &winstation_list, struct winstation, entry )
675 unsigned int access = WINSTA_ENUMERATE;
676 if (req->index > index++) continue;
677 if (!check_object_access( &winsta->obj, &access )) continue;
678 clear_error();
679 reply->next = index;
680 if ((name = get_object_name( &winsta->obj, &len )))
681 set_reply_data( name, min( len, get_reply_max_size() ));
682 return;
684 set_error( STATUS_NO_MORE_ENTRIES );
688 /* enumerate desktops */
689 DECL_HANDLER(enum_desktop)
691 struct winstation *winstation;
692 struct desktop *desktop;
693 unsigned int index = 0;
694 const WCHAR *name;
695 data_size_t len;
697 if (!(winstation = (struct winstation *)get_handle_obj( current->process, req->winstation,
698 WINSTA_ENUMDESKTOPS, &winstation_ops )))
699 return;
701 LIST_FOR_EACH_ENTRY( desktop, &winstation->desktops, struct desktop, entry )
703 unsigned int access = DESKTOP_ENUMERATE;
704 if (req->index > index++) continue;
705 if (!desktop->obj.name) continue;
706 if (!check_object_access( &desktop->obj, &access )) continue;
707 if ((name = get_object_name( &desktop->obj, &len )))
708 set_reply_data( name, min( len, get_reply_max_size() ));
709 release_object( winstation );
710 clear_error();
711 reply->next = index;
712 return;
715 release_object( winstation );
716 set_error( STATUS_NO_MORE_ENTRIES );