gitlab: Use --enable-werror for Clang builds.
[wine.git] / server / winstation.c
blob80126ad5d60ae096a76c232d4a5ab956ae6abf31
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 memset( &desktop->cursor, 0, sizeof(desktop->cursor) );
296 memset( desktop->keystate, 0, sizeof(desktop->keystate) );
297 list_add_tail( &winstation->desktops, &desktop->entry );
298 list_init( &desktop->hotkeys );
299 list_init( &desktop->pointers );
301 else
303 desktop->flags |= flags & (DF_WINE_VIRTUAL_DESKTOP | DF_WINE_ROOT_DESKTOP);
304 clear_error();
307 return desktop;
310 static void desktop_dump( struct object *obj, int verbose )
312 struct desktop *desktop = (struct desktop *)obj;
314 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p\n",
315 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
318 static int desktop_link_name( struct object *obj, struct object_name *name, struct object *parent )
320 struct winstation *winstation = (struct winstation *)parent;
322 if (parent->ops != &winstation_ops)
324 set_error( STATUS_OBJECT_TYPE_MISMATCH );
325 return 0;
327 if (get_path_element( name->name, name->len ) < name->len) /* no backslash allowed in name */
329 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
330 return 0;
332 namespace_add( winstation->desktop_names, name );
333 return 1;
336 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
338 struct thread *thread;
340 /* check if the handle is currently used by the process or one of its threads */
341 if (process->desktop == handle) return 0;
342 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
343 if (thread->desktop == handle) return 0;
344 return 1;
347 static void desktop_destroy( struct object *obj )
349 struct desktop *desktop = (struct desktop *)obj;
350 struct winstation *winstation = desktop->winstation;
352 list_remove( &desktop->entry );
354 if (desktop == winstation->input_desktop)
356 struct desktop *other, *found = NULL;
357 LIST_FOR_EACH_ENTRY(other, &winstation->desktops, struct desktop, entry)
358 if (!found || other->input_time > found->input_time) found = other;
359 set_input_desktop( winstation, found );
362 free_hotkeys( desktop, 0 );
363 free_pointers( desktop );
364 if (desktop->top_window) free_window_handle( desktop->top_window );
365 if (desktop->msg_window) free_window_handle( desktop->msg_window );
366 if (desktop->global_hooks) release_object( desktop->global_hooks );
367 if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
368 release_object( desktop->winstation );
371 /* retrieve the thread desktop, checking the handle access rights */
372 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
374 return get_desktop_obj( thread->process, thread->desktop, access );
377 static void close_desktop_timeout( void *private )
379 struct desktop *desktop = private;
381 desktop->close_timeout = NULL;
382 unlink_named_object( &desktop->obj ); /* make sure no other process can open it */
383 post_desktop_message( desktop, WM_CLOSE, 0, 0 ); /* and signal the owner to quit */
386 /* add a user of the desktop and cancel the close timeout */
387 static void add_desktop_thread( struct desktop *desktop, struct thread *thread )
389 list_add_tail( &desktop->threads, &thread->desktop_entry );
391 if (!thread->process->is_system)
393 desktop->users++;
394 if (desktop->close_timeout)
396 remove_timeout_user( desktop->close_timeout );
397 desktop->close_timeout = NULL;
401 /* if thread process is now connected to the input desktop, let it receive rawinput */
402 if (desktop == desktop->winstation->input_desktop) set_rawinput_process( thread->process, 1 );
405 /* remove a user of the desktop and start the close timeout if necessary */
406 static void remove_desktop_user( struct desktop *desktop, struct thread *thread )
408 struct process *process;
410 assert( desktop->users > 0 );
411 desktop->users--;
413 /* if we have one remaining user, it has to be the manager of the desktop window */
414 if ((process = get_top_window_owner( desktop )) && desktop->users == process->running_threads && !desktop->close_timeout)
415 desktop->close_timeout = add_timeout_user( -TICKS_PER_SEC, close_desktop_timeout, desktop );
418 /* remove a thread from the list of threads attached to a desktop */
419 static void remove_desktop_thread( struct desktop *desktop, struct thread *thread )
421 list_remove( &thread->desktop_entry );
423 if (!thread->process->is_system) remove_desktop_user( desktop, thread );
425 if (desktop == desktop->winstation->input_desktop)
427 /* thread process might still be connected the input desktop through another thread, update the full list */
428 LIST_FOR_EACH_ENTRY( thread, &desktop->threads, struct thread, desktop_entry )
429 set_rawinput_process( thread->process, 1 );
433 /* set the thread default desktop handle */
434 void set_thread_default_desktop( struct thread *thread, struct desktop *desktop, obj_handle_t handle )
436 if (thread->desktop) return; /* nothing to do */
438 thread->desktop = handle;
439 add_desktop_thread( desktop, thread );
442 /* set the process default desktop handle */
443 void set_process_default_desktop( struct process *process, struct desktop *desktop,
444 obj_handle_t handle )
446 struct thread *thread;
448 if (process->desktop == handle) return; /* nothing to do */
450 process->desktop = handle;
452 /* set desktop for threads that don't have one yet */
453 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
454 set_thread_default_desktop( thread, desktop, handle );
457 /* connect a process to its window station */
458 void connect_process_winstation( struct process *process, struct unicode_str *desktop_path,
459 struct thread *parent_thread, struct process *parent_process )
461 struct unicode_str desktop_name = *desktop_path, winstation_name = {0};
462 const int attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
463 struct winstation *winstation = NULL;
464 struct desktop *desktop = NULL;
465 const WCHAR *wch, *end;
466 obj_handle_t handle;
468 for (wch = desktop_name.str, end = wch + desktop_name.len / sizeof(WCHAR); wch != end; wch++)
470 if (*wch == '\\')
472 winstation_name.str = desktop_name.str;
473 winstation_name.len = (wch - winstation_name.str) * sizeof(WCHAR);
474 desktop_name.str = wch + 1;
475 desktop_name.len = (end - desktop_name.str) * sizeof(WCHAR);
476 break;
480 /* check for an inherited winstation handle (don't ask...) */
481 if ((handle = find_inherited_handle( process, &winstation_ops )))
483 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
485 else if (winstation_name.len && (winstation = open_named_object( NULL, &winstation_ops, &winstation_name, attributes )))
487 handle = alloc_handle( process, winstation, STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS, 0 );
489 else if (parent_process->winstation)
491 handle = duplicate_handle( parent_process, parent_process->winstation,
492 process, 0, 0, DUPLICATE_SAME_ACCESS );
493 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
495 if (!winstation) goto done;
496 process->winstation = handle;
498 if ((handle = find_inherited_handle( process, &desktop_ops )))
500 desktop = get_desktop_obj( process, handle, 0 );
501 if (!desktop || desktop->winstation != winstation) goto done;
503 else if (desktop_name.len && (desktop = open_named_object( &winstation->obj, &desktop_ops, &desktop_name, attributes )))
505 handle = alloc_handle( process, desktop, STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS, 0 );
507 else
509 if (parent_thread && parent_thread->desktop)
510 handle = parent_thread->desktop;
511 else if (parent_process->desktop)
512 handle = parent_process->desktop;
513 else
514 goto done;
516 desktop = get_desktop_obj( parent_process, handle, 0 );
518 if (!desktop || desktop->winstation != winstation) goto done;
520 handle = duplicate_handle( parent_process, handle, process, 0, 0, DUPLICATE_SAME_ACCESS );
522 if (handle) set_process_default_desktop( process, desktop, handle );
524 done:
525 if (desktop) release_object( desktop );
526 if (winstation) release_object( winstation );
527 clear_error();
530 /* close the desktop of a given process */
531 void close_process_desktop( struct process *process )
533 obj_handle_t handle;
535 if (!(handle = process->desktop)) return;
537 process->desktop = 0;
538 close_handle( process, handle );
541 /* release (and eventually close) the desktop of a given thread */
542 void release_thread_desktop( struct thread *thread, int close )
544 struct desktop *desktop;
545 obj_handle_t handle;
547 if (!(handle = thread->desktop)) return;
549 if (!(desktop = get_desktop_obj( thread->process, handle, 0 ))) clear_error(); /* ignore errors */
550 else
552 if (close) remove_desktop_thread( desktop, thread );
553 else remove_desktop_user( desktop, thread );
554 release_object( desktop );
557 if (close)
559 thread->desktop = 0;
560 close_handle( thread->process, handle );
564 /* create a window station */
565 DECL_HANDLER(create_winstation)
567 struct winstation *winstation;
568 struct unicode_str name = get_req_unicode_str();
569 struct object *root = NULL;
571 reply->handle = 0;
572 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir ))) return;
574 if ((winstation = create_winstation( root, &name, req->attributes, req->flags )))
576 reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
577 release_object( winstation );
579 if (root) release_object( root );
582 /* open a handle to a window station */
583 DECL_HANDLER(open_winstation)
585 struct unicode_str name = get_req_unicode_str();
587 reply->handle = open_object( current->process, req->rootdir, req->access,
588 &winstation_ops, &name, req->attributes );
592 /* close a window station */
593 DECL_HANDLER(close_winstation)
595 struct winstation *winstation;
597 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
598 0, &winstation_ops )))
600 if (close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
601 release_object( winstation );
606 /* get the process current window station */
607 DECL_HANDLER(get_process_winstation)
609 reply->handle = current->process->winstation;
613 /* set the process current window station */
614 DECL_HANDLER(set_process_winstation)
616 struct winstation *winstation;
618 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
619 0, &winstation_ops )))
621 /* FIXME: should we close the old one? */
622 current->process->winstation = req->handle;
623 release_object( winstation );
627 /* create a desktop */
628 DECL_HANDLER(create_desktop)
630 struct desktop *desktop;
631 struct winstation *winstation;
632 struct unicode_str name = get_req_unicode_str();
634 reply->handle = 0;
635 if (!name.len)
637 set_error( STATUS_INVALID_HANDLE );
638 return;
640 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
642 if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
644 if (!winstation->input_desktop) set_input_desktop( winstation, desktop );
645 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
646 release_object( desktop );
648 release_object( winstation );
652 /* open a handle to a desktop */
653 DECL_HANDLER(open_desktop)
655 struct winstation *winstation;
656 struct object *obj;
657 struct unicode_str name = get_req_unicode_str();
659 /* FIXME: check access rights */
660 if (!req->winsta)
661 winstation = get_process_winstation( current->process, 0 );
662 else
663 winstation = (struct winstation *)get_handle_obj( current->process, req->winsta, 0, &winstation_ops );
665 if (!winstation) return;
667 if ((obj = open_named_object( &winstation->obj, &desktop_ops, &name, req->attributes )))
669 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
670 release_object( obj );
673 release_object( winstation );
676 /* open a handle to current input desktop */
677 DECL_HANDLER(open_input_desktop)
679 /* FIXME: check access rights */
680 struct winstation *winstation = get_process_winstation( current->process, 0 );
681 struct desktop *desktop;
683 if (!winstation) return;
685 if (!(winstation->flags & WSF_VISIBLE))
687 set_error( STATUS_ILLEGAL_FUNCTION );
688 release_object( winstation );
689 return;
692 if ((desktop = get_input_desktop( winstation )))
694 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
695 release_object( desktop );
697 release_object( winstation );
700 /* changes the current input desktop */
701 DECL_HANDLER(set_input_desktop)
703 /* FIXME: check access rights */
704 struct winstation *winstation;
705 struct desktop *desktop;
707 if (!(winstation = get_process_winstation( current->process, 0 ))) return;
709 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle, 0, &desktop_ops )))
711 if (!set_input_desktop( winstation, desktop )) set_error( STATUS_ILLEGAL_FUNCTION );
712 release_object( desktop );
715 release_object( winstation );
718 /* close a desktop */
719 DECL_HANDLER(close_desktop)
721 struct desktop *desktop;
723 /* make sure it is a desktop handle */
724 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
725 0, &desktop_ops )))
727 if (close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
728 release_object( desktop );
733 /* get the thread current desktop */
734 DECL_HANDLER(get_thread_desktop)
736 struct thread *thread;
738 if (!(thread = get_thread_from_id( req->tid ))) return;
739 reply->handle = thread->desktop;
740 release_object( thread );
744 /* set the thread current desktop */
745 DECL_HANDLER(set_thread_desktop)
747 struct desktop *old_desktop, *new_desktop;
748 struct winstation *winstation;
750 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
751 return;
753 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
755 release_object( winstation );
756 return;
758 if (new_desktop->winstation != winstation)
760 set_error( STATUS_ACCESS_DENIED );
761 release_object( new_desktop );
762 release_object( winstation );
763 return;
766 /* check if we are changing to a new desktop */
768 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
769 clear_error(); /* ignore error */
771 /* when changing desktop, we can't have any users on the current one */
772 if (old_desktop != new_desktop && current->desktop_users > 0)
773 set_error( STATUS_DEVICE_BUSY );
774 else
776 current->desktop = req->handle; /* FIXME: should we close the old one? */
777 if (old_desktop != new_desktop)
779 if (old_desktop) remove_desktop_thread( old_desktop, current );
780 add_desktop_thread( new_desktop, current );
784 if (!current->process->desktop)
785 set_process_default_desktop( current->process, new_desktop, req->handle );
787 if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
789 if (old_desktop) release_object( old_desktop );
790 release_object( new_desktop );
791 release_object( winstation );
795 /* get/set information about a user object (window station or desktop) */
796 DECL_HANDLER(set_user_object_info)
798 struct object *obj;
799 data_size_t len;
801 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
803 if (obj->ops == &desktop_ops)
805 struct desktop *desktop = (struct desktop *)obj;
806 reply->is_desktop = 1;
807 reply->old_obj_flags = desktop->flags;
808 if (req->flags & SET_USER_OBJECT_SET_FLAGS) desktop->flags = req->obj_flags;
810 else if (obj->ops == &winstation_ops)
812 struct winstation *winstation = (struct winstation *)obj;
813 reply->is_desktop = 0;
814 reply->old_obj_flags = winstation->flags;
815 if (req->flags & SET_USER_OBJECT_SET_FLAGS) winstation->flags = req->obj_flags;
817 else
819 set_error( STATUS_OBJECT_TYPE_MISMATCH );
820 release_object( obj );
821 return;
823 if (obj->ops == &desktop_ops && get_reply_max_size() && (req->flags & SET_USER_OBJECT_GET_FULL_NAME))
825 struct desktop *desktop = (struct desktop *)obj;
826 data_size_t winstation_len, desktop_len;
827 const WCHAR *winstation_name = get_object_name( &desktop->winstation->obj, &winstation_len );
828 const WCHAR *desktop_name = get_object_name( obj, &desktop_len );
829 WCHAR *full_name;
831 if (!winstation_name) winstation_len = 0;
832 if (!desktop_name) desktop_len = 0;
833 len = winstation_len + desktop_len + sizeof(WCHAR);
834 if ((full_name = mem_alloc( len )))
836 memcpy( full_name, winstation_name, winstation_len );
837 full_name[winstation_len / sizeof(WCHAR)] = '\\';
838 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, desktop_name, desktop_len );
839 set_reply_data_ptr( full_name, min( len, get_reply_max_size() ));
842 else
844 const WCHAR *name = get_object_name( obj, &len );
845 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
847 release_object( obj );
851 /* enumerate window stations */
852 DECL_HANDLER(enum_winstation)
854 unsigned int index = 0;
855 struct winstation *winsta;
856 const WCHAR *name;
857 data_size_t len;
859 LIST_FOR_EACH_ENTRY( winsta, &winstation_list, struct winstation, entry )
861 unsigned int access = WINSTA_ENUMERATE;
862 if (req->index > index++) continue;
863 if (!check_object_access( NULL, &winsta->obj, &access )) continue;
864 clear_error();
865 reply->next = index;
866 if ((name = get_object_name( &winsta->obj, &len )))
867 set_reply_data( name, min( len, get_reply_max_size() ));
868 return;
870 set_error( STATUS_NO_MORE_ENTRIES );
874 /* enumerate desktops */
875 DECL_HANDLER(enum_desktop)
877 struct winstation *winstation;
878 struct desktop *desktop;
879 unsigned int index = 0;
880 const WCHAR *name;
881 data_size_t len;
883 if (!(winstation = (struct winstation *)get_handle_obj( current->process, req->winstation,
884 WINSTA_ENUMDESKTOPS, &winstation_ops )))
885 return;
887 LIST_FOR_EACH_ENTRY( desktop, &winstation->desktops, struct desktop, entry )
889 unsigned int access = DESKTOP_ENUMERATE;
890 if (req->index > index++) continue;
891 if (!desktop->obj.name) continue;
892 if (!check_object_access( NULL, &desktop->obj, &access )) continue;
893 if ((name = get_object_name( &desktop->obj, &len )))
894 set_reply_data( name, min( len, get_reply_max_size() ));
895 release_object( winstation );
896 clear_error();
897 reply->next = index;
898 return;
901 release_object( winstation );
902 set_error( STATUS_NO_MORE_ENTRIES );