wow64: In wow64_NtSetInformationToken forward TokenIntegrityLevel.
[wine.git] / server / winstation.c
blob45c7defa1f7e0ec7f1ba0df3720685b0a632dee3
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"
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <sys/types.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"
33 #include "ntuser.h"
35 #include "object.h"
36 #include "handle.h"
37 #include "request.h"
38 #include "process.h"
39 #include "user.h"
40 #include "file.h"
41 #include "security.h"
43 #define DESKTOP_ALL_ACCESS 0x01ff
45 static struct list winstation_list = LIST_INIT(winstation_list);
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 struct object *winstation_lookup_name( struct object *obj, struct unicode_str *name,
50 unsigned int attr, struct object *root );
51 static void winstation_destroy( struct object *obj );
52 static void desktop_dump( struct object *obj, int verbose );
53 static int desktop_link_name( struct object *obj, struct object_name *name, struct object *parent );
54 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
55 static void desktop_destroy( struct object *obj );
57 static const WCHAR winstation_name[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n'};
59 struct type_descr winstation_type =
61 { winstation_name, sizeof(winstation_name) }, /* name */
62 STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS, /* valid_access */
63 { /* mapping */
64 STANDARD_RIGHTS_READ | WINSTA_READSCREEN | WINSTA_ENUMERATE | WINSTA_READATTRIBUTES | WINSTA_ENUMDESKTOPS,
65 STANDARD_RIGHTS_WRITE | WINSTA_WRITEATTRIBUTES | WINSTA_CREATEDESKTOP | WINSTA_ACCESSCLIPBOARD,
66 STANDARD_RIGHTS_EXECUTE | WINSTA_EXITWINDOWS | WINSTA_ACCESSGLOBALATOMS,
67 STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS
71 static const struct object_ops winstation_ops =
73 sizeof(struct winstation), /* size */
74 &winstation_type, /* type */
75 winstation_dump, /* dump */
76 no_add_queue, /* add_queue */
77 NULL, /* remove_queue */
78 NULL, /* signaled */
79 NULL, /* satisfied */
80 no_signal, /* signal */
81 no_get_fd, /* get_fd */
82 default_map_access, /* map_access */
83 default_get_sd, /* get_sd */
84 default_set_sd, /* set_sd */
85 default_get_full_name, /* get_full_name */
86 winstation_lookup_name, /* lookup_name */
87 directory_link_name, /* link_name */
88 default_unlink_name, /* unlink_name */
89 no_open_file, /* open_file */
90 no_kernel_obj_list, /* get_kernel_obj_list */
91 winstation_close_handle, /* close_handle */
92 winstation_destroy /* destroy */
96 static const WCHAR desktop_name[] = {'D','e','s','k','t','o','p'};
98 struct type_descr desktop_type =
100 { desktop_name, sizeof(desktop_name) }, /* name */
101 STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS, /* valid_access */
102 { /* mapping */
103 STANDARD_RIGHTS_READ | DESKTOP_ENUMERATE | DESKTOP_READOBJECTS,
104 STANDARD_RIGHTS_WRITE | DESKTOP_WRITEOBJECTS | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD
105 | DESKTOP_HOOKCONTROL | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW,
106 STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP,
107 STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS
111 static const struct object_ops desktop_ops =
113 sizeof(struct desktop), /* size */
114 &desktop_type, /* type */
115 desktop_dump, /* dump */
116 no_add_queue, /* add_queue */
117 NULL, /* remove_queue */
118 NULL, /* signaled */
119 NULL, /* satisfied */
120 no_signal, /* signal */
121 no_get_fd, /* get_fd */
122 default_map_access, /* map_access */
123 default_get_sd, /* get_sd */
124 default_set_sd, /* set_sd */
125 default_get_full_name, /* get_full_name */
126 no_lookup_name, /* lookup_name */
127 desktop_link_name, /* link_name */
128 default_unlink_name, /* unlink_name */
129 no_open_file, /* open_file */
130 no_kernel_obj_list, /* get_kernel_obj_list */
131 desktop_close_handle, /* close_handle */
132 desktop_destroy /* destroy */
135 /* create a winstation object */
136 static struct winstation *create_winstation( struct object *root, const struct unicode_str *name,
137 unsigned int attr, unsigned int flags )
139 struct winstation *winstation;
141 if ((winstation = create_named_object( root, &winstation_ops, name, attr, NULL )))
143 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
145 /* initialize it if it didn't already exist */
146 winstation->flags = flags;
147 winstation->input_desktop = NULL;
148 winstation->clipboard = NULL;
149 winstation->atom_table = NULL;
150 list_add_tail( &winstation_list, &winstation->entry );
151 list_init( &winstation->desktops );
152 if (!(winstation->desktop_names = create_namespace( 7 )))
154 release_object( winstation );
155 return NULL;
158 else clear_error();
160 return winstation;
163 static void winstation_dump( struct object *obj, int verbose )
165 struct winstation *winstation = (struct winstation *)obj;
167 fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p\n",
168 winstation->flags, winstation->clipboard, winstation->atom_table );
171 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
173 return (process->winstation != handle);
176 static struct object *winstation_lookup_name( struct object *obj, struct unicode_str *name,
177 unsigned int attr, struct object *root )
179 struct winstation *winstation = (struct winstation *)obj;
180 struct object *found;
182 assert( obj->ops == &winstation_ops );
184 if (!name) return NULL; /* open the winstation itself */
186 if (get_path_element( name->str, name->len ) < name->len) /* no backslash allowed in name */
188 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
189 return NULL;
192 if ((found = find_object( winstation->desktop_names, name, attr )))
193 name->len = 0;
195 return found;
198 static void winstation_destroy( struct object *obj )
200 struct winstation *winstation = (struct winstation *)obj;
202 list_remove( &winstation->entry );
203 if (winstation->clipboard) release_object( winstation->clipboard );
204 if (winstation->atom_table) release_object( winstation->atom_table );
205 free( winstation->desktop_names );
208 /* retrieve the process window station, checking the handle access rights */
209 struct winstation *get_process_winstation( struct process *process, unsigned int access )
211 return (struct winstation *)get_handle_obj( process, process->winstation,
212 access, &winstation_ops );
215 /* retrieve the visible winstation */
216 struct winstation *get_visible_winstation(void)
218 struct winstation *winstation;
219 LIST_FOR_EACH_ENTRY( winstation, &winstation_list, struct winstation, entry )
220 if (winstation->flags & WSF_VISIBLE) return winstation;
221 return NULL;
224 /* retrieve the winstation current input desktop */
225 struct desktop *get_input_desktop( struct winstation *winstation )
227 struct desktop *desktop;
228 if (!(desktop = winstation->input_desktop)) return NULL;
229 return (struct desktop *)grab_object( desktop );
232 /* changes the winstation current input desktop and update its input time */
233 int set_input_desktop( struct winstation *winstation, struct desktop *new_desktop )
235 struct desktop *old_desktop = winstation->input_desktop;
236 struct thread *thread;
238 if (!(winstation->flags & WSF_VISIBLE)) return 0;
239 if (new_desktop) new_desktop->input_time = current_time;
240 if (old_desktop == new_desktop) return 1;
242 if (old_desktop)
244 /* disconnect every process of the old input desktop from rawinput */
245 LIST_FOR_EACH_ENTRY( thread, &old_desktop->threads, struct thread, desktop_entry )
246 set_rawinput_process( thread->process, 0 );
249 if ((winstation->input_desktop = new_desktop))
251 /* connect every process of the new input desktop to rawinput */
252 LIST_FOR_EACH_ENTRY( thread, &new_desktop->threads, struct thread, desktop_entry )
253 set_rawinput_process( thread->process, 1 );
256 return 1;
259 /* retrieve a pointer to a desktop object */
260 struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle, unsigned int access )
262 return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
265 /* create a desktop object */
266 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
267 unsigned int flags, struct winstation *winstation )
269 struct desktop *desktop, *current_desktop;
271 if ((desktop = create_named_object( &winstation->obj, &desktop_ops, name, attr, NULL )))
273 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
275 /* initialize it if it didn't already exist */
277 desktop->flags = flags;
279 /* inherit DF_WINE_*_DESKTOP flags if none of them are specified */
280 if (!(flags & (DF_WINE_ROOT_DESKTOP | DF_WINE_VIRTUAL_DESKTOP))
281 && (current_desktop = get_thread_desktop( current, 0 )))
283 desktop->flags |= current_desktop->flags & (DF_WINE_VIRTUAL_DESKTOP | DF_WINE_ROOT_DESKTOP);
284 release_object( current_desktop );
287 desktop->winstation = (struct winstation *)grab_object( winstation );
288 desktop->top_window = NULL;
289 desktop->msg_window = NULL;
290 desktop->global_hooks = NULL;
291 desktop->close_timeout = NULL;
292 desktop->foreground_input = NULL;
293 desktop->users = 0;
294 list_init( &desktop->threads );
295 desktop->clip_flags = 0;
296 desktop->cursor_win = 0;
297 desktop->alt_pressed = 0;
298 memset( &desktop->key_repeat, 0, sizeof(desktop->key_repeat) );
299 list_add_tail( &winstation->desktops, &desktop->entry );
300 list_init( &desktop->hotkeys );
301 list_init( &desktop->pointers );
303 if (!(desktop->shared = alloc_shared_object()))
305 release_object( desktop );
306 return NULL;
309 SHARED_WRITE_BEGIN( desktop->shared, desktop_shm_t )
311 shared->cursor.x = 0;
312 shared->cursor.y = 0;
313 shared->cursor.last_change = 0;
314 shared->cursor.clip.left = 0;
315 shared->cursor.clip.top = 0;
316 shared->cursor.clip.right = 0;
317 shared->cursor.clip.bottom = 0;
318 memset( (void *)shared->keystate, 0, sizeof(shared->keystate) );
320 SHARED_WRITE_END;
322 else
324 desktop->flags |= flags & (DF_WINE_VIRTUAL_DESKTOP | DF_WINE_ROOT_DESKTOP);
325 clear_error();
328 return desktop;
331 static void desktop_dump( struct object *obj, int verbose )
333 struct desktop *desktop = (struct desktop *)obj;
335 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p\n",
336 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
339 static int desktop_link_name( struct object *obj, struct object_name *name, struct object *parent )
341 struct winstation *winstation = (struct winstation *)parent;
343 if (parent->ops != &winstation_ops)
345 set_error( STATUS_OBJECT_TYPE_MISMATCH );
346 return 0;
348 if (get_path_element( name->name, name->len ) < name->len) /* no backslash allowed in name */
350 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
351 return 0;
353 namespace_add( winstation->desktop_names, name );
354 return 1;
357 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
359 struct thread *thread;
361 /* check if the handle is currently used by the process or one of its threads */
362 if (process->desktop == handle) return 0;
363 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
364 if (thread->desktop == handle) return 0;
365 return 1;
368 static void desktop_destroy( struct object *obj )
370 struct desktop *desktop = (struct desktop *)obj;
371 struct winstation *winstation = desktop->winstation;
373 list_remove( &desktop->entry );
375 if (desktop == winstation->input_desktop)
377 struct desktop *other, *found = NULL;
378 LIST_FOR_EACH_ENTRY(other, &winstation->desktops, struct desktop, entry)
379 if (!found || other->input_time > found->input_time) found = other;
380 set_input_desktop( winstation, found );
383 free_hotkeys( desktop, 0 );
384 free_pointers( desktop );
385 if (desktop->top_window) free_window_handle( desktop->top_window );
386 if (desktop->msg_window) free_window_handle( desktop->msg_window );
387 if (desktop->global_hooks) release_object( desktop->global_hooks );
388 if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
389 if (desktop->key_repeat.timeout) remove_timeout_user( desktop->key_repeat.timeout );
390 release_object( desktop->winstation );
391 if (desktop->shared) free_shared_object( desktop->shared );
394 /* retrieve the thread desktop, checking the handle access rights */
395 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
397 return get_desktop_obj( thread->process, thread->desktop, access );
400 static void close_desktop_timeout( void *private )
402 struct desktop *desktop = private;
404 desktop->close_timeout = NULL;
405 unlink_named_object( &desktop->obj ); /* make sure no other process can open it */
406 post_desktop_message( desktop, WM_CLOSE, 0, 0 ); /* and signal the owner to quit */
409 /* add a user of the desktop and cancel the close timeout */
410 static void add_desktop_thread( struct desktop *desktop, struct thread *thread )
412 list_add_tail( &desktop->threads, &thread->desktop_entry );
414 if (!thread->process->is_system)
416 desktop->users++;
417 if (desktop->close_timeout)
419 remove_timeout_user( desktop->close_timeout );
420 desktop->close_timeout = NULL;
424 /* if thread process is now connected to the input desktop, let it receive rawinput */
425 if (desktop == desktop->winstation->input_desktop) set_rawinput_process( thread->process, 1 );
428 /* remove a user of the desktop and start the close timeout if necessary */
429 static void remove_desktop_user( struct desktop *desktop, struct thread *thread )
431 struct process *process;
433 assert( desktop->users > 0 );
434 desktop->users--;
436 /* if we have one remaining user, it has to be the manager of the desktop window */
437 if ((process = get_top_window_owner( desktop )) && desktop->users == process->running_threads && !desktop->close_timeout)
438 desktop->close_timeout = add_timeout_user( -TICKS_PER_SEC, close_desktop_timeout, desktop );
441 /* remove a thread from the list of threads attached to a desktop */
442 static void remove_desktop_thread( struct desktop *desktop, struct thread *thread )
444 list_remove( &thread->desktop_entry );
446 if (!thread->process->is_system) remove_desktop_user( desktop, thread );
448 if (desktop == desktop->winstation->input_desktop)
450 /* thread process might still be connected the input desktop through another thread, update the full list */
451 LIST_FOR_EACH_ENTRY( thread, &desktop->threads, struct thread, desktop_entry )
452 set_rawinput_process( thread->process, 1 );
456 /* set the thread default desktop handle */
457 void set_thread_default_desktop( struct thread *thread, struct desktop *desktop, obj_handle_t handle )
459 if (thread->desktop) return; /* nothing to do */
461 thread->desktop = handle;
462 add_desktop_thread( desktop, thread );
465 /* set the process default desktop handle */
466 void set_process_default_desktop( struct process *process, struct desktop *desktop,
467 obj_handle_t handle )
469 struct thread *thread;
471 if (process->desktop == handle) return; /* nothing to do */
473 process->desktop = handle;
475 /* set desktop for threads that don't have one yet */
476 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
477 set_thread_default_desktop( thread, desktop, handle );
480 /* connect a process to its window station */
481 void connect_process_winstation( struct process *process, struct unicode_str *desktop_path,
482 struct thread *parent_thread, struct process *parent_process )
484 struct unicode_str desktop_name = *desktop_path, winstation_name = {0};
485 const int attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
486 struct winstation *winstation = NULL;
487 struct desktop *desktop = NULL;
488 const WCHAR *wch, *end;
489 obj_handle_t handle;
491 for (wch = desktop_name.str, end = wch + desktop_name.len / sizeof(WCHAR); wch != end; wch++)
493 if (*wch == '\\')
495 winstation_name.str = desktop_name.str;
496 winstation_name.len = (wch - winstation_name.str) * sizeof(WCHAR);
497 desktop_name.str = wch + 1;
498 desktop_name.len = (end - desktop_name.str) * sizeof(WCHAR);
499 break;
503 /* check for an inherited winstation handle (don't ask...) */
504 if ((handle = find_inherited_handle( process, &winstation_ops )))
506 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
508 else if (winstation_name.len && (winstation = open_named_object( NULL, &winstation_ops, &winstation_name, attributes )))
510 handle = alloc_handle( process, winstation, STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS, 0 );
512 else if (parent_process->winstation)
514 handle = duplicate_handle( parent_process, parent_process->winstation,
515 process, 0, 0, DUPLICATE_SAME_ACCESS );
516 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
518 if (!winstation) goto done;
519 process->winstation = handle;
521 if ((handle = find_inherited_handle( process, &desktop_ops )))
523 desktop = get_desktop_obj( process, handle, 0 );
524 if (!desktop || desktop->winstation != winstation) goto done;
526 else if (desktop_name.len && (desktop = open_named_object( &winstation->obj, &desktop_ops, &desktop_name, attributes )))
528 handle = alloc_handle( process, desktop, STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS, 0 );
530 else
532 if (parent_thread && parent_thread->desktop)
533 handle = parent_thread->desktop;
534 else if (parent_process->desktop)
535 handle = parent_process->desktop;
536 else
537 goto done;
539 desktop = get_desktop_obj( parent_process, handle, 0 );
541 if (!desktop || desktop->winstation != winstation) goto done;
543 handle = duplicate_handle( parent_process, handle, process, 0, 0, DUPLICATE_SAME_ACCESS );
545 if (handle) set_process_default_desktop( process, desktop, handle );
547 done:
548 if (desktop) release_object( desktop );
549 if (winstation) release_object( winstation );
550 clear_error();
553 /* close the desktop of a given process */
554 void close_process_desktop( struct process *process )
556 obj_handle_t handle;
558 if (!(handle = process->desktop)) return;
560 process->desktop = 0;
561 close_handle( process, handle );
564 /* release (and eventually close) the desktop of a given thread */
565 void release_thread_desktop( struct thread *thread, int close )
567 struct desktop *desktop;
568 obj_handle_t handle;
570 if (!(handle = thread->desktop)) return;
572 if (!(desktop = get_desktop_obj( thread->process, handle, 0 ))) clear_error(); /* ignore errors */
573 else
575 if (close) remove_desktop_thread( desktop, thread );
576 else remove_desktop_user( desktop, thread );
577 release_object( desktop );
580 if (close)
582 thread->desktop = 0;
583 close_handle( thread->process, handle );
587 /* create a window station */
588 DECL_HANDLER(create_winstation)
590 struct winstation *winstation;
591 struct unicode_str name = get_req_unicode_str();
592 struct object *root = NULL;
594 reply->handle = 0;
595 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir ))) return;
597 if ((winstation = create_winstation( root, &name, req->attributes, req->flags )))
599 reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
600 release_object( winstation );
602 if (root) release_object( root );
605 /* open a handle to a window station */
606 DECL_HANDLER(open_winstation)
608 struct unicode_str name = get_req_unicode_str();
610 reply->handle = open_object( current->process, req->rootdir, req->access,
611 &winstation_ops, &name, req->attributes );
615 /* close a window station */
616 DECL_HANDLER(close_winstation)
618 struct winstation *winstation;
620 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
621 0, &winstation_ops )))
623 if (close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
624 release_object( winstation );
629 /* get the process current window station */
630 DECL_HANDLER(get_process_winstation)
632 reply->handle = current->process->winstation;
636 /* set the process current window station */
637 DECL_HANDLER(set_process_winstation)
639 struct winstation *winstation;
641 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
642 0, &winstation_ops )))
644 /* FIXME: should we close the old one? */
645 current->process->winstation = req->handle;
646 release_object( winstation );
650 /* create a desktop */
651 DECL_HANDLER(create_desktop)
653 struct desktop *desktop;
654 struct winstation *winstation;
655 struct unicode_str name = get_req_unicode_str();
657 reply->handle = 0;
658 if (!name.len)
660 set_error( STATUS_INVALID_HANDLE );
661 return;
663 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
665 if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
667 if (!winstation->input_desktop) set_input_desktop( winstation, desktop );
668 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
669 release_object( desktop );
671 release_object( winstation );
675 /* open a handle to a desktop */
676 DECL_HANDLER(open_desktop)
678 struct winstation *winstation;
679 struct object *obj;
680 struct unicode_str name = get_req_unicode_str();
682 /* FIXME: check access rights */
683 if (!req->winsta)
684 winstation = get_process_winstation( current->process, 0 );
685 else
686 winstation = (struct winstation *)get_handle_obj( current->process, req->winsta, 0, &winstation_ops );
688 if (!winstation) return;
690 if ((obj = open_named_object( &winstation->obj, &desktop_ops, &name, req->attributes )))
692 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
693 release_object( obj );
696 release_object( winstation );
699 /* open a handle to current input desktop */
700 DECL_HANDLER(open_input_desktop)
702 /* FIXME: check access rights */
703 struct winstation *winstation = get_process_winstation( current->process, 0 );
704 struct desktop *desktop;
706 if (!winstation) return;
708 if (!(winstation->flags & WSF_VISIBLE))
710 set_error( STATUS_ILLEGAL_FUNCTION );
711 release_object( winstation );
712 return;
715 if ((desktop = get_input_desktop( winstation )))
717 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
718 release_object( desktop );
720 release_object( winstation );
723 /* changes the current input desktop */
724 DECL_HANDLER(set_input_desktop)
726 /* FIXME: check access rights */
727 struct winstation *winstation;
728 struct desktop *desktop;
730 if (!(winstation = get_process_winstation( current->process, 0 ))) return;
732 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle, 0, &desktop_ops )))
734 if (!set_input_desktop( winstation, desktop )) set_error( STATUS_ILLEGAL_FUNCTION );
735 release_object( desktop );
738 release_object( winstation );
741 /* close a desktop */
742 DECL_HANDLER(close_desktop)
744 struct desktop *desktop;
746 /* make sure it is a desktop handle */
747 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
748 0, &desktop_ops )))
750 if (close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
751 release_object( desktop );
756 /* get the thread current desktop */
757 DECL_HANDLER(get_thread_desktop)
759 struct desktop *desktop;
760 struct thread *thread;
762 if (!(thread = get_thread_from_id( req->tid ))) return;
763 reply->handle = thread->desktop;
765 if (!(desktop = get_thread_desktop( thread, 0 ))) clear_error();
766 else
768 if (desktop->shared) reply->locator = get_shared_object_locator( desktop->shared );
769 release_object( desktop );
772 release_object( thread );
776 /* set the thread current desktop */
777 DECL_HANDLER(set_thread_desktop)
779 struct desktop *old_desktop, *new_desktop;
780 struct winstation *winstation;
782 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
783 return;
785 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
787 release_object( winstation );
788 return;
790 if (new_desktop->winstation != winstation)
792 set_error( STATUS_ACCESS_DENIED );
793 release_object( new_desktop );
794 release_object( winstation );
795 return;
798 /* check if we are changing to a new desktop */
800 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
801 clear_error(); /* ignore error */
803 /* when changing desktop, we can't have any users on the current one */
804 if (old_desktop != new_desktop && current->desktop_users > 0)
805 set_error( STATUS_DEVICE_BUSY );
806 else
808 current->desktop = req->handle; /* FIXME: should we close the old one? */
809 if (old_desktop != new_desktop)
811 if (old_desktop) remove_desktop_thread( old_desktop, current );
812 add_desktop_thread( new_desktop, current );
814 reply->locator = get_shared_object_locator( new_desktop->shared );
817 if (!current->process->desktop)
818 set_process_default_desktop( current->process, new_desktop, req->handle );
820 if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
822 if (old_desktop) release_object( old_desktop );
823 release_object( new_desktop );
824 release_object( winstation );
828 /* get/set information about a user object (window station or desktop) */
829 DECL_HANDLER(set_user_object_info)
831 struct object *obj;
832 data_size_t len;
834 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
836 if (obj->ops == &desktop_ops)
838 struct desktop *desktop = (struct desktop *)obj;
839 reply->is_desktop = 1;
840 reply->old_obj_flags = desktop->flags;
841 if (req->flags & SET_USER_OBJECT_SET_FLAGS) desktop->flags = req->obj_flags;
843 else if (obj->ops == &winstation_ops)
845 struct winstation *winstation = (struct winstation *)obj;
846 reply->is_desktop = 0;
847 reply->old_obj_flags = winstation->flags;
848 if (req->flags & SET_USER_OBJECT_SET_FLAGS) winstation->flags = req->obj_flags;
850 else
852 set_error( STATUS_OBJECT_TYPE_MISMATCH );
853 release_object( obj );
854 return;
856 if (obj->ops == &desktop_ops && get_reply_max_size() && (req->flags & SET_USER_OBJECT_GET_FULL_NAME))
858 struct desktop *desktop = (struct desktop *)obj;
859 data_size_t winstation_len, desktop_len;
860 const WCHAR *winstation_name = get_object_name( &desktop->winstation->obj, &winstation_len );
861 const WCHAR *desktop_name = get_object_name( obj, &desktop_len );
862 WCHAR *full_name;
864 if (!winstation_name) winstation_len = 0;
865 if (!desktop_name) desktop_len = 0;
866 len = winstation_len + desktop_len + sizeof(WCHAR);
867 if ((full_name = mem_alloc( len )))
869 memcpy( full_name, winstation_name, winstation_len );
870 full_name[winstation_len / sizeof(WCHAR)] = '\\';
871 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, desktop_name, desktop_len );
872 set_reply_data_ptr( full_name, min( len, get_reply_max_size() ));
875 else
877 const WCHAR *name = get_object_name( obj, &len );
878 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
880 release_object( obj );
884 /* enumerate window stations */
885 DECL_HANDLER(enum_winstation)
887 unsigned int index = 0;
888 struct winstation *winsta;
889 const WCHAR *name;
890 data_size_t len;
892 LIST_FOR_EACH_ENTRY( winsta, &winstation_list, struct winstation, entry )
894 unsigned int access = WINSTA_ENUMERATE;
895 if (req->index > index++) continue;
896 if (!check_object_access( NULL, &winsta->obj, &access )) continue;
897 clear_error();
898 reply->next = index;
899 if ((name = get_object_name( &winsta->obj, &len )))
900 set_reply_data( name, min( len, get_reply_max_size() ));
901 return;
903 set_error( STATUS_NO_MORE_ENTRIES );
907 /* enumerate desktops */
908 DECL_HANDLER(enum_desktop)
910 struct winstation *winstation;
911 struct desktop *desktop;
912 unsigned int index = 0;
913 const WCHAR *name;
914 data_size_t len;
916 if (!(winstation = (struct winstation *)get_handle_obj( current->process, req->winstation,
917 WINSTA_ENUMDESKTOPS, &winstation_ops )))
918 return;
920 LIST_FOR_EACH_ENTRY( desktop, &winstation->desktops, struct desktop, entry )
922 unsigned int access = DESKTOP_ENUMERATE;
923 if (req->index > index++) continue;
924 if (!desktop->obj.name) continue;
925 if (!check_object_access( NULL, &desktop->obj, &access )) continue;
926 if ((name = get_object_name( &desktop->obj, &len )))
927 set_reply_data( name, min( len, get_reply_max_size() ));
928 release_object( winstation );
929 clear_error();
930 reply->next = index;
931 return;
934 release_object( winstation );
935 set_error( STATUS_NO_MORE_ENTRIES );