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
41 #include "wine/unicode.h"
44 static struct list winstation_list
= LIST_INIT(winstation_list
);
45 static struct namespace *winstation_namespace
;
47 static void winstation_dump( struct object
*obj
, int verbose
);
48 static struct object_type
*winstation_get_type( struct object
*obj
);
49 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
50 static void winstation_destroy( struct object
*obj
);
51 static unsigned int winstation_map_access( struct object
*obj
, unsigned int access
);
52 static void desktop_dump( struct object
*obj
, int verbose
);
53 static struct object_type
*desktop_get_type( struct object
*obj
);
54 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
55 static void desktop_destroy( struct object
*obj
);
56 static unsigned int desktop_map_access( struct object
*obj
, unsigned int access
);
58 static const struct object_ops winstation_ops
=
60 sizeof(struct winstation
), /* size */
61 winstation_dump
, /* dump */
62 winstation_get_type
, /* get_type */
63 no_add_queue
, /* add_queue */
64 NULL
, /* remove_queue */
67 no_signal
, /* signal */
68 no_get_fd
, /* get_fd */
69 winstation_map_access
, /* map_access */
70 default_get_sd
, /* get_sd */
71 default_set_sd
, /* set_sd */
72 no_lookup_name
, /* lookup_name */
73 no_open_file
, /* open_file */
74 winstation_close_handle
, /* close_handle */
75 winstation_destroy
/* destroy */
79 static const struct object_ops desktop_ops
=
81 sizeof(struct desktop
), /* size */
82 desktop_dump
, /* dump */
83 desktop_get_type
, /* get_type */
84 no_add_queue
, /* add_queue */
85 NULL
, /* remove_queue */
88 no_signal
, /* signal */
89 no_get_fd
, /* get_fd */
90 desktop_map_access
, /* map_access */
91 default_get_sd
, /* get_sd */
92 default_set_sd
, /* set_sd */
93 no_lookup_name
, /* lookup_name */
94 no_open_file
, /* open_file */
95 desktop_close_handle
, /* close_handle */
96 desktop_destroy
/* destroy */
99 #define DESKTOP_ALL_ACCESS 0x01ff
101 /* create a winstation object */
102 static struct winstation
*create_winstation( const struct unicode_str
*name
, unsigned int attr
,
105 struct winstation
*winstation
;
107 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7 )))
110 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
112 set_error( STATUS_INVALID_PARAMETER
);
116 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, attr
)))
118 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
120 /* initialize it if it didn't already exist */
121 winstation
->flags
= flags
;
122 winstation
->clipboard
= NULL
;
123 winstation
->atom_table
= NULL
;
124 list_add_tail( &winstation_list
, &winstation
->entry
);
125 list_init( &winstation
->desktops
);
131 static void winstation_dump( struct object
*obj
, int verbose
)
133 struct winstation
*winstation
= (struct winstation
*)obj
;
135 fprintf( stderr
, "Winstation flags=%x clipboard=%p atoms=%p ",
136 winstation
->flags
, winstation
->clipboard
, winstation
->atom_table
);
137 dump_object_name( &winstation
->obj
);
138 fputc( '\n', stderr
);
141 static struct object_type
*winstation_get_type( struct object
*obj
)
143 static const WCHAR name
[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n'};
144 static const struct unicode_str str
= { name
, sizeof(name
) };
145 return get_object_type( &str
);
148 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
150 return (process
->winstation
!= handle
);
153 static void winstation_destroy( struct object
*obj
)
155 struct winstation
*winstation
= (struct winstation
*)obj
;
157 list_remove( &winstation
->entry
);
158 if (winstation
->clipboard
) release_object( winstation
->clipboard
);
159 if (winstation
->atom_table
) release_object( winstation
->atom_table
);
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 /* build the full name of a desktop object */
181 static WCHAR
*build_desktop_name( const struct unicode_str
*name
,
182 struct winstation
*winstation
, struct unicode_str
*res
)
184 const WCHAR
*winstation_name
;
186 data_size_t winstation_len
;
188 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) ))
190 set_error( STATUS_INVALID_PARAMETER
);
194 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
197 res
->len
= winstation_len
+ name
->len
+ sizeof(WCHAR
);
198 if (!(full_name
= mem_alloc( res
->len
))) return NULL
;
199 memcpy( full_name
, winstation_name
, winstation_len
);
200 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
201 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
->str
, name
->len
);
202 res
->str
= full_name
;
206 /* retrieve a pointer to a desktop object */
207 struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
209 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
212 /* create a desktop object */
213 static struct desktop
*create_desktop( const struct unicode_str
*name
, unsigned int attr
,
214 unsigned int flags
, struct winstation
*winstation
)
216 struct desktop
*desktop
;
217 struct unicode_str full_str
;
220 if (!(full_name
= build_desktop_name( name
, winstation
, &full_str
))) return NULL
;
222 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, &full_str
, attr
)))
224 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
226 /* initialize it if it didn't already exist */
227 desktop
->flags
= flags
;
228 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
229 desktop
->top_window
= NULL
;
230 desktop
->msg_window
= NULL
;
231 desktop
->global_hooks
= NULL
;
232 desktop
->close_timeout
= NULL
;
233 desktop
->foreground_input
= NULL
;
235 desktop
->cursor_x
= 0;
236 desktop
->cursor_y
= 0;
237 desktop
->cursor_clip
.left
= 0;
238 desktop
->cursor_clip
.top
= 0;
239 desktop
->cursor_clip
.right
= 0;
240 desktop
->cursor_clip
.bottom
= 0;
241 memset( desktop
->keystate
, 0, sizeof(desktop
->keystate
) );
242 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
249 static void desktop_dump( struct object
*obj
, int verbose
)
251 struct desktop
*desktop
= (struct desktop
*)obj
;
253 fprintf( stderr
, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
254 desktop
->flags
, desktop
->winstation
, desktop
->top_window
, desktop
->global_hooks
);
255 dump_object_name( &desktop
->obj
);
256 fputc( '\n', stderr
);
259 static struct object_type
*desktop_get_type( struct object
*obj
)
261 static const WCHAR name
[] = {'D','e','s','k','t','o','p'};
262 static const struct unicode_str str
= { name
, sizeof(name
) };
263 return get_object_type( &str
);
266 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
268 struct thread
*thread
;
270 /* check if the handle is currently used by the process or one of its threads */
271 if (process
->desktop
== handle
) return 0;
272 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
273 if (thread
->desktop
== handle
) return 0;
277 static void desktop_destroy( struct object
*obj
)
279 struct desktop
*desktop
= (struct desktop
*)obj
;
281 if (desktop
->top_window
) destroy_window( desktop
->top_window
);
282 if (desktop
->msg_window
) destroy_window( desktop
->msg_window
);
283 if (desktop
->global_hooks
) release_object( desktop
->global_hooks
);
284 if (desktop
->close_timeout
) remove_timeout_user( desktop
->close_timeout
);
285 list_remove( &desktop
->entry
);
286 release_object( desktop
->winstation
);
289 static unsigned int desktop_map_access( struct object
*obj
, unsigned int access
)
291 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| DESKTOP_READOBJECTS
| DESKTOP_ENUMERATE
;
292 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| DESKTOP_CREATEMENU
| DESKTOP_CREATEWINDOW
|
293 DESKTOP_HOOKCONTROL
| DESKTOP_JOURNALRECORD
| DESKTOP_JOURNALPLAYBACK
|
294 DESKTOP_WRITEOBJECTS
;
295 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| DESKTOP_SWITCHDESKTOP
;
296 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_REQUIRED
| DESKTOP_ALL_ACCESS
;
297 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
300 /* retrieve the thread desktop, checking the handle access rights */
301 struct desktop
*get_thread_desktop( struct thread
*thread
, unsigned int access
)
303 return get_desktop_obj( thread
->process
, thread
->desktop
, access
);
306 /* set the process default desktop handle */
307 void set_process_default_desktop( struct process
*process
, struct desktop
*desktop
,
308 obj_handle_t handle
)
310 struct thread
*thread
;
311 struct desktop
*old_desktop
;
313 if (process
->desktop
== handle
) return; /* nothing to do */
315 if (!(old_desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error();
316 process
->desktop
= handle
;
318 /* set desktop for threads that don't have one yet */
319 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
320 if (!thread
->desktop
) thread
->desktop
= handle
;
322 if (!process
->is_system
)
325 if (desktop
->close_timeout
)
327 remove_timeout_user( desktop
->close_timeout
);
328 desktop
->close_timeout
= NULL
;
330 if (old_desktop
) old_desktop
->users
--;
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
;
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
);
373 if (desktop
) release_object( desktop
);
374 if (winstation
) release_object( winstation
);
378 static void close_desktop_timeout( void *private )
380 struct desktop
*desktop
= private;
382 desktop
->close_timeout
= NULL
;
383 unlink_named_object( &desktop
->obj
); /* make sure no other process can open it */
384 close_desktop_window( desktop
); /* and signal the owner to quit */
387 /* close the desktop of a given process */
388 void close_process_desktop( struct process
*process
)
390 struct desktop
*desktop
;
392 if (process
->desktop
&& (desktop
= get_desktop_obj( process
, process
->desktop
, 0 )))
394 assert( desktop
->users
> 0 );
396 /* if we have one remaining user, it has to be the manager of the desktop window */
397 if (desktop
->users
== 1 && get_top_window_owner( desktop
))
399 assert( !desktop
->close_timeout
);
400 desktop
->close_timeout
= add_timeout_user( -TICKS_PER_SEC
, close_desktop_timeout
, desktop
);
402 release_object( desktop
);
404 clear_error(); /* ignore errors */
407 /* close the desktop of a given thread */
408 void close_thread_desktop( struct thread
*thread
)
410 obj_handle_t handle
= thread
->desktop
;
413 if (handle
) close_handle( thread
->process
, handle
);
416 /* set the reply data from the object name */
417 static void set_reply_data_obj_name( struct object
*obj
)
420 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
422 /* if there is a backslash return the part of the name after it */
423 if (name
&& (ptr
= memchrW( name
, '\\', len
/sizeof(WCHAR
) )))
425 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
428 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
431 /* create a window station */
432 DECL_HANDLER(create_winstation
)
434 struct winstation
*winstation
;
435 struct unicode_str name
;
438 get_req_unicode_str( &name
);
439 if ((winstation
= create_winstation( &name
, req
->attributes
, req
->flags
)))
441 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->attributes
);
442 release_object( winstation
);
446 /* open a handle to a window station */
447 DECL_HANDLER(open_winstation
)
449 struct unicode_str name
;
451 get_req_unicode_str( &name
);
452 if (winstation_namespace
)
453 reply
->handle
= open_object( winstation_namespace
, &name
, &winstation_ops
, req
->access
,
456 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
460 /* close a window station */
461 DECL_HANDLER(close_winstation
)
463 struct winstation
*winstation
;
465 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
466 0, &winstation_ops
)))
468 if (close_handle( current
->process
, req
->handle
)) set_error( STATUS_ACCESS_DENIED
);
469 release_object( winstation
);
474 /* get the process current window station */
475 DECL_HANDLER(get_process_winstation
)
477 reply
->handle
= current
->process
->winstation
;
481 /* set the process current window station */
482 DECL_HANDLER(set_process_winstation
)
484 struct winstation
*winstation
;
486 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
487 0, &winstation_ops
)))
489 /* FIXME: should we close the old one? */
490 current
->process
->winstation
= req
->handle
;
491 release_object( winstation
);
495 /* create a desktop */
496 DECL_HANDLER(create_desktop
)
498 struct desktop
*desktop
;
499 struct winstation
*winstation
;
500 struct unicode_str name
;
503 get_req_unicode_str( &name
);
504 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
506 if ((desktop
= create_desktop( &name
, req
->attributes
, req
->flags
, winstation
)))
508 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->attributes
);
509 release_object( desktop
);
511 release_object( winstation
);
515 /* open a handle to a desktop */
516 DECL_HANDLER(open_desktop
)
518 struct winstation
*winstation
;
519 struct unicode_str name
;
521 get_req_unicode_str( &name
);
523 /* FIXME: check access rights */
525 winstation
= get_process_winstation( current
->process
, 0 );
527 winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->winsta
, 0, &winstation_ops
);
531 struct unicode_str full_str
;
534 if ((full_name
= build_desktop_name( &name
, winstation
, &full_str
)))
536 reply
->handle
= open_object( winstation_namespace
, &full_str
, &desktop_ops
, req
->access
,
540 release_object( winstation
);
545 /* close a desktop */
546 DECL_HANDLER(close_desktop
)
548 struct desktop
*desktop
;
550 /* make sure it is a desktop handle */
551 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
554 if (close_handle( current
->process
, req
->handle
)) set_error( STATUS_DEVICE_BUSY
);
555 release_object( desktop
);
560 /* get the thread current desktop */
561 DECL_HANDLER(get_thread_desktop
)
563 struct thread
*thread
;
565 if (!(thread
= get_thread_from_id( req
->tid
))) return;
566 reply
->handle
= thread
->desktop
;
567 release_object( thread
);
571 /* set the thread current desktop */
572 DECL_HANDLER(set_thread_desktop
)
574 struct desktop
*old_desktop
, *new_desktop
;
575 struct winstation
*winstation
;
577 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
580 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
582 release_object( winstation
);
585 if (new_desktop
->winstation
!= winstation
)
587 set_error( STATUS_ACCESS_DENIED
);
588 release_object( new_desktop
);
589 release_object( winstation
);
593 /* check if we are changing to a new desktop */
595 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
596 clear_error(); /* ignore error */
598 /* when changing desktop, we can't have any users on the current one */
599 if (old_desktop
!= new_desktop
&& current
->desktop_users
> 0)
600 set_error( STATUS_DEVICE_BUSY
);
602 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
604 if (!current
->process
->desktop
)
605 set_process_default_desktop( current
->process
, new_desktop
, req
->handle
);
607 if (old_desktop
!= new_desktop
&& current
->queue
) detach_thread_input( current
);
609 if (old_desktop
) release_object( old_desktop
);
610 release_object( new_desktop
);
611 release_object( winstation
);
615 /* get/set information about a user object (window station or desktop) */
616 DECL_HANDLER(set_user_object_info
)
620 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
622 if (obj
->ops
== &desktop_ops
)
624 struct desktop
*desktop
= (struct desktop
*)obj
;
625 reply
->is_desktop
= 1;
626 reply
->old_obj_flags
= desktop
->flags
;
627 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
629 else if (obj
->ops
== &winstation_ops
)
631 struct winstation
*winstation
= (struct winstation
*)obj
;
632 reply
->is_desktop
= 0;
633 reply
->old_obj_flags
= winstation
->flags
;
634 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
638 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
639 release_object( obj
);
642 if (get_reply_max_size()) set_reply_data_obj_name( obj
);
643 release_object( obj
);
647 /* enumerate window stations */
648 DECL_HANDLER(enum_winstation
)
650 unsigned int index
= 0;
651 struct winstation
*winsta
;
653 LIST_FOR_EACH_ENTRY( winsta
, &winstation_list
, struct winstation
, entry
)
655 unsigned int access
= WINSTA_ENUMERATE
;
656 if (req
->index
> index
++) continue;
657 if (!check_object_access( &winsta
->obj
, &access
)) continue;
658 set_reply_data_obj_name( &winsta
->obj
);
663 set_error( STATUS_NO_MORE_ENTRIES
);
667 /* enumerate desktops */
668 DECL_HANDLER(enum_desktop
)
670 struct winstation
*winstation
;
671 struct desktop
*desktop
;
672 unsigned int index
= 0;
674 if (!(winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->winstation
,
675 WINSTA_ENUMDESKTOPS
, &winstation_ops
)))
678 LIST_FOR_EACH_ENTRY( desktop
, &winstation
->desktops
, struct desktop
, entry
)
680 unsigned int access
= DESKTOP_ENUMERATE
;
681 if (req
->index
> index
++) continue;
682 if (!desktop
->obj
.name
) continue;
683 if (!check_object_access( &desktop
->obj
, &access
)) continue;
684 set_reply_data_obj_name( &desktop
->obj
);
685 release_object( winstation
);
691 release_object( winstation
);
692 set_error( STATUS_NO_MORE_ENTRIES
);