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
22 #include "wine/port.h"
28 #define WIN32_NO_STATUS
40 #include "wine/unicode.h"
43 static struct list winstation_list
= LIST_INIT(winstation_list
);
44 static struct namespace *winstation_namespace
;
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 */
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 */
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( const struct unicode_str
*name
, unsigned int attr
,
104 struct winstation
*winstation
;
106 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7 )))
109 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
111 set_error( STATUS_INVALID_PARAMETER
);
115 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, attr
)))
117 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
119 /* initialize it if it didn't already exist */
120 winstation
->flags
= flags
;
121 winstation
->clipboard
= NULL
;
122 winstation
->atom_table
= NULL
;
123 list_add_tail( &winstation_list
, &winstation
->entry
);
124 list_init( &winstation
->desktops
);
130 static void winstation_dump( struct object
*obj
, int verbose
)
132 struct winstation
*winstation
= (struct winstation
*)obj
;
134 fprintf( stderr
, "Winstation flags=%x clipboard=%p atoms=%p ",
135 winstation
->flags
, winstation
->clipboard
, winstation
->atom_table
);
136 dump_object_name( &winstation
->obj
);
137 fputc( '\n', stderr
);
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
);
161 static unsigned int winstation_map_access( struct object
*obj
, unsigned int access
)
163 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| WINSTA_ENUMDESKTOPS
| WINSTA_READATTRIBUTES
|
164 WINSTA_ENUMERATE
| WINSTA_READSCREEN
;
165 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| WINSTA_ACCESSCLIPBOARD
| WINSTA_CREATEDESKTOP
|
166 WINSTA_WRITEATTRIBUTES
;
167 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| WINSTA_ACCESSGLOBALATOMS
| WINSTA_EXITWINDOWS
;
168 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_REQUIRED
| WINSTA_ALL_ACCESS
;
169 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
172 /* retrieve the process window station, checking the handle access rights */
173 struct winstation
*get_process_winstation( struct process
*process
, unsigned int access
)
175 return (struct winstation
*)get_handle_obj( process
, process
->winstation
,
176 access
, &winstation_ops
);
179 /* build the full name of a desktop object */
180 static WCHAR
*build_desktop_name( const struct unicode_str
*name
,
181 struct winstation
*winstation
, struct unicode_str
*res
)
183 const WCHAR
*winstation_name
;
185 data_size_t winstation_len
;
187 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) ))
189 set_error( STATUS_INVALID_PARAMETER
);
193 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
196 res
->len
= winstation_len
+ name
->len
+ sizeof(WCHAR
);
197 if (!(full_name
= mem_alloc( res
->len
))) return NULL
;
198 memcpy( full_name
, winstation_name
, winstation_len
);
199 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
200 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
->str
, name
->len
);
201 res
->str
= full_name
;
205 /* retrieve a pointer to a desktop object */
206 struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
208 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
211 /* create a desktop object */
212 static struct desktop
*create_desktop( const struct unicode_str
*name
, unsigned int attr
,
213 unsigned int flags
, struct winstation
*winstation
)
215 struct desktop
*desktop
;
216 struct unicode_str full_str
;
219 if (!(full_name
= build_desktop_name( name
, winstation
, &full_str
))) return NULL
;
221 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, &full_str
, attr
)))
223 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
225 /* initialize it if it didn't already exist */
226 desktop
->flags
= flags
;
227 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
228 desktop
->top_window
= NULL
;
229 desktop
->global_hooks
= NULL
;
230 desktop
->close_timeout
= NULL
;
232 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
239 static void desktop_dump( struct object
*obj
, int verbose
)
241 struct desktop
*desktop
= (struct desktop
*)obj
;
243 fprintf( stderr
, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
244 desktop
->flags
, desktop
->winstation
, desktop
->top_window
, desktop
->global_hooks
);
245 dump_object_name( &desktop
->obj
);
246 fputc( '\n', stderr
);
249 static struct object_type
*desktop_get_type( struct object
*obj
)
251 static const WCHAR name
[] = {'D','e','s','k','t','o','p'};
252 static const struct unicode_str str
= { name
, sizeof(name
) };
253 return get_object_type( &str
);
256 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
258 struct thread
*thread
;
260 /* check if the handle is currently used by the process or one of its threads */
261 if (process
->desktop
== handle
) return 0;
262 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
263 if (thread
->desktop
== handle
) return 0;
267 static void desktop_destroy( struct object
*obj
)
269 struct desktop
*desktop
= (struct desktop
*)obj
;
271 if (desktop
->top_window
) destroy_window( desktop
->top_window
);
272 if (desktop
->global_hooks
) release_object( desktop
->global_hooks
);
273 if (desktop
->close_timeout
) remove_timeout_user( desktop
->close_timeout
);
274 list_remove( &desktop
->entry
);
275 release_object( desktop
->winstation
);
278 static unsigned int desktop_map_access( struct object
*obj
, unsigned int access
)
280 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| DESKTOP_READOBJECTS
| DESKTOP_ENUMERATE
;
281 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| DESKTOP_CREATEMENU
| DESKTOP_CREATEWINDOW
|
282 DESKTOP_HOOKCONTROL
| DESKTOP_JOURNALRECORD
| DESKTOP_JOURNALPLAYBACK
|
283 DESKTOP_WRITEOBJECTS
;
284 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| DESKTOP_SWITCHDESKTOP
;
285 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_REQUIRED
| DESKTOP_ALL_ACCESS
;
286 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
289 /* retrieve the thread desktop, checking the handle access rights */
290 struct desktop
*get_thread_desktop( struct thread
*thread
, unsigned int access
)
292 return get_desktop_obj( thread
->process
, thread
->desktop
, access
);
295 /* set the process default desktop handle */
296 void set_process_default_desktop( struct process
*process
, struct desktop
*desktop
,
297 obj_handle_t handle
)
299 struct thread
*thread
;
300 struct desktop
*old_desktop
;
302 if (process
->desktop
== handle
) return; /* nothing to do */
304 if (!(old_desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error();
305 process
->desktop
= handle
;
307 /* set desktop for threads that don't have one yet */
308 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
309 if (!thread
->desktop
) thread
->desktop
= handle
;
312 if (desktop
->close_timeout
)
314 remove_timeout_user( desktop
->close_timeout
);
315 desktop
->close_timeout
= NULL
;
319 old_desktop
->users
--;
320 release_object( old_desktop
);
324 /* connect a process to its window station */
325 void connect_process_winstation( struct process
*process
, struct thread
*parent
)
327 struct winstation
*winstation
= NULL
;
328 struct desktop
*desktop
= NULL
;
331 /* check for an inherited winstation handle (don't ask...) */
332 if ((handle
= find_inherited_handle( process
, &winstation_ops
)))
334 winstation
= (struct winstation
*)get_handle_obj( process
, handle
, 0, &winstation_ops
);
336 else if (parent
&& parent
->process
->winstation
)
338 handle
= duplicate_handle( parent
->process
, parent
->process
->winstation
,
339 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
340 winstation
= (struct winstation
*)get_handle_obj( process
, handle
, 0, &winstation_ops
);
342 if (!winstation
) goto done
;
343 process
->winstation
= handle
;
345 if ((handle
= find_inherited_handle( process
, &desktop_ops
)))
347 desktop
= get_desktop_obj( process
, handle
, 0 );
348 if (!desktop
|| desktop
->winstation
!= winstation
) goto done
;
350 else if (parent
&& parent
->desktop
)
352 desktop
= get_desktop_obj( parent
->process
, parent
->desktop
, 0 );
353 if (!desktop
|| desktop
->winstation
!= winstation
) goto done
;
354 handle
= duplicate_handle( parent
->process
, parent
->desktop
,
355 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
358 if (handle
) set_process_default_desktop( process
, desktop
, handle
);
361 if (desktop
) release_object( desktop
);
362 if (winstation
) release_object( winstation
);
366 static void close_desktop_timeout( void *private )
368 struct desktop
*desktop
= private;
370 desktop
->close_timeout
= NULL
;
371 unlink_named_object( &desktop
->obj
); /* make sure no other process can open it */
372 close_desktop_window( desktop
); /* and signal the owner to quit */
375 /* close the desktop of a given process */
376 void close_process_desktop( struct process
*process
)
378 struct desktop
*desktop
;
380 if (process
->desktop
&& (desktop
= get_desktop_obj( process
, process
->desktop
, 0 )))
382 assert( desktop
->users
> 0 );
384 /* if we have one remaining user, it has to be the manager of the desktop window */
385 if (desktop
->users
== 1 && get_top_window_owner( desktop
))
387 assert( !desktop
->close_timeout
);
388 desktop
->close_timeout
= add_timeout_user( -TICKS_PER_SEC
, close_desktop_timeout
, desktop
);
390 release_object( desktop
);
392 clear_error(); /* ignore errors */
395 /* close the desktop of a given thread */
396 void close_thread_desktop( struct thread
*thread
)
398 obj_handle_t handle
= thread
->desktop
;
401 if (handle
) close_handle( thread
->process
, handle
);
402 clear_error(); /* ignore errors */
405 /* set the reply data from the object name */
406 static void set_reply_data_obj_name( struct object
*obj
)
409 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
411 /* if there is a backslash return the part of the name after it */
412 if (name
&& (ptr
= memchrW( name
, '\\', len
/sizeof(WCHAR
) )))
414 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
417 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
420 /* create a window station */
421 DECL_HANDLER(create_winstation
)
423 struct winstation
*winstation
;
424 struct unicode_str name
;
427 get_req_unicode_str( &name
);
428 if ((winstation
= create_winstation( &name
, req
->attributes
, req
->flags
)))
430 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->attributes
);
431 release_object( winstation
);
435 /* open a handle to a window station */
436 DECL_HANDLER(open_winstation
)
438 struct unicode_str name
;
440 get_req_unicode_str( &name
);
441 if (winstation_namespace
)
442 reply
->handle
= open_object( winstation_namespace
, &name
, &winstation_ops
, req
->access
,
445 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
449 /* close a window station */
450 DECL_HANDLER(close_winstation
)
452 struct winstation
*winstation
;
454 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
455 0, &winstation_ops
)))
457 if (!close_handle( current
->process
, req
->handle
)) set_error( STATUS_ACCESS_DENIED
);
458 release_object( winstation
);
463 /* get the process current window station */
464 DECL_HANDLER(get_process_winstation
)
466 reply
->handle
= current
->process
->winstation
;
470 /* set the process current window station */
471 DECL_HANDLER(set_process_winstation
)
473 struct winstation
*winstation
;
475 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
476 0, &winstation_ops
)))
478 /* FIXME: should we close the old one? */
479 current
->process
->winstation
= req
->handle
;
480 release_object( winstation
);
484 /* create a desktop */
485 DECL_HANDLER(create_desktop
)
487 struct desktop
*desktop
;
488 struct winstation
*winstation
;
489 struct unicode_str name
;
492 get_req_unicode_str( &name
);
493 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
495 if ((desktop
= create_desktop( &name
, req
->attributes
, req
->flags
, winstation
)))
497 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->attributes
);
498 release_object( desktop
);
500 release_object( winstation
);
504 /* open a handle to a desktop */
505 DECL_HANDLER(open_desktop
)
507 struct winstation
*winstation
;
508 struct unicode_str name
;
510 get_req_unicode_str( &name
);
512 /* FIXME: check access rights */
514 winstation
= get_process_winstation( current
->process
, 0 );
516 winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->winsta
, 0, &winstation_ops
);
520 struct unicode_str full_str
;
523 if ((full_name
= build_desktop_name( &name
, winstation
, &full_str
)))
525 reply
->handle
= open_object( winstation_namespace
, &full_str
, &desktop_ops
, req
->access
,
529 release_object( winstation
);
534 /* close a desktop */
535 DECL_HANDLER(close_desktop
)
537 struct desktop
*desktop
;
539 /* make sure it is a desktop handle */
540 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
543 if (!close_handle( current
->process
, req
->handle
)) set_error( STATUS_DEVICE_BUSY
);
544 release_object( desktop
);
549 /* get the thread current desktop */
550 DECL_HANDLER(get_thread_desktop
)
552 struct thread
*thread
;
554 if (!(thread
= get_thread_from_id( req
->tid
))) return;
555 reply
->handle
= thread
->desktop
;
556 release_object( thread
);
560 /* set the thread current desktop */
561 DECL_HANDLER(set_thread_desktop
)
563 struct desktop
*old_desktop
, *new_desktop
;
564 struct winstation
*winstation
;
566 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
569 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
571 release_object( winstation
);
574 if (new_desktop
->winstation
!= winstation
)
576 set_error( STATUS_ACCESS_DENIED
);
577 release_object( new_desktop
);
578 release_object( winstation
);
582 /* check if we are changing to a new desktop */
584 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
585 clear_error(); /* ignore error */
587 /* when changing desktop, we can't have any users on the current one */
588 if (old_desktop
!= new_desktop
&& current
->desktop_users
> 0)
589 set_error( STATUS_DEVICE_BUSY
);
591 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
593 if (!current
->process
->desktop
)
594 set_process_default_desktop( current
->process
, new_desktop
, req
->handle
);
596 if (old_desktop
!= new_desktop
&& current
->queue
) detach_thread_input( current
);
598 if (old_desktop
) release_object( old_desktop
);
599 release_object( new_desktop
);
600 release_object( winstation
);
604 /* get/set information about a user object (window station or desktop) */
605 DECL_HANDLER(set_user_object_info
)
609 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
611 if (obj
->ops
== &desktop_ops
)
613 struct desktop
*desktop
= (struct desktop
*)obj
;
614 reply
->is_desktop
= 1;
615 reply
->old_obj_flags
= desktop
->flags
;
616 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
618 else if (obj
->ops
== &winstation_ops
)
620 struct winstation
*winstation
= (struct winstation
*)obj
;
621 reply
->is_desktop
= 0;
622 reply
->old_obj_flags
= winstation
->flags
;
623 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
627 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
628 release_object( obj
);
631 if (get_reply_max_size()) set_reply_data_obj_name( obj
);
632 release_object( obj
);
636 /* enumerate window stations */
637 DECL_HANDLER(enum_winstation
)
639 unsigned int index
= req
->index
;
643 while ((handle
= enumerate_handles( current
->process
, &winstation_ops
, &index
)))
645 if (!(obj
= get_handle_obj( current
->process
, handle
, WINSTA_ENUMERATE
, &winstation_ops
)))
647 set_reply_data_obj_name( obj
);
648 release_object( obj
);
653 set_error( STATUS_NO_MORE_ENTRIES
);
657 /* enumerate desktops */
658 DECL_HANDLER(enum_desktop
)
660 struct winstation
*winstation
;
661 struct desktop
*desktop
;
662 unsigned int index
= req
->index
;
665 if (!(winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->winstation
,
666 WINSTA_ENUMDESKTOPS
, &winstation_ops
)))
669 while ((handle
= enumerate_handles( current
->process
, &desktop_ops
, &index
)))
671 if (!(desktop
= get_desktop_obj( current
->process
, handle
, DESKTOP_ENUMERATE
)))
674 if (desktop
->winstation
== winstation
)
676 set_reply_data_obj_name( &desktop
->obj
);
677 release_object( desktop
);
678 release_object( winstation
);
683 release_object( desktop
);
686 release_object( winstation
);
687 set_error( STATUS_NO_MORE_ENTRIES
);