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 int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
48 static void winstation_destroy( struct object
*obj
);
49 static unsigned int winstation_map_access( struct object
*obj
, unsigned int access
);
50 static void desktop_dump( struct object
*obj
, int verbose
);
51 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
52 static void desktop_destroy( struct object
*obj
);
53 static unsigned int desktop_map_access( struct object
*obj
, unsigned int access
);
55 static const struct object_ops winstation_ops
=
57 sizeof(struct winstation
), /* size */
58 winstation_dump
, /* dump */
59 no_add_queue
, /* add_queue */
60 NULL
, /* remove_queue */
63 no_signal
, /* signal */
64 no_get_fd
, /* get_fd */
65 winstation_map_access
, /* map_access */
66 default_get_sd
, /* get_sd */
67 default_set_sd
, /* set_sd */
68 no_lookup_name
, /* lookup_name */
69 no_open_file
, /* open_file */
70 winstation_close_handle
, /* close_handle */
71 winstation_destroy
/* destroy */
75 static const struct object_ops desktop_ops
=
77 sizeof(struct desktop
), /* size */
78 desktop_dump
, /* dump */
79 no_add_queue
, /* add_queue */
80 NULL
, /* remove_queue */
83 no_signal
, /* signal */
84 no_get_fd
, /* get_fd */
85 desktop_map_access
, /* map_access */
86 default_get_sd
, /* get_sd */
87 default_set_sd
, /* set_sd */
88 no_lookup_name
, /* lookup_name */
89 no_open_file
, /* open_file */
90 desktop_close_handle
, /* close_handle */
91 desktop_destroy
/* destroy */
94 #define DESKTOP_ALL_ACCESS 0x01ff
96 /* create a winstation object */
97 static struct winstation
*create_winstation( const struct unicode_str
*name
, unsigned int attr
,
100 struct winstation
*winstation
;
102 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7 )))
105 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
107 set_error( STATUS_INVALID_PARAMETER
);
111 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, attr
)))
113 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
115 /* initialize it if it didn't already exist */
116 winstation
->flags
= flags
;
117 winstation
->clipboard
= NULL
;
118 winstation
->atom_table
= NULL
;
119 list_add_tail( &winstation_list
, &winstation
->entry
);
120 list_init( &winstation
->desktops
);
126 static void winstation_dump( struct object
*obj
, int verbose
)
128 struct winstation
*winstation
= (struct winstation
*)obj
;
130 fprintf( stderr
, "Winstation flags=%x clipboard=%p atoms=%p ",
131 winstation
->flags
, winstation
->clipboard
, winstation
->atom_table
);
132 dump_object_name( &winstation
->obj
);
133 fputc( '\n', stderr
);
136 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
138 return (process
->winstation
!= handle
);
141 static void winstation_destroy( struct object
*obj
)
143 struct winstation
*winstation
= (struct winstation
*)obj
;
145 list_remove( &winstation
->entry
);
146 if (winstation
->clipboard
) release_object( winstation
->clipboard
);
147 if (winstation
->atom_table
) release_object( winstation
->atom_table
);
150 static unsigned int winstation_map_access( struct object
*obj
, unsigned int access
)
152 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| WINSTA_ENUMDESKTOPS
| WINSTA_READATTRIBUTES
|
153 WINSTA_ENUMERATE
| WINSTA_READSCREEN
;
154 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| WINSTA_ACCESSCLIPBOARD
| WINSTA_CREATEDESKTOP
|
155 WINSTA_WRITEATTRIBUTES
;
156 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| WINSTA_ACCESSGLOBALATOMS
| WINSTA_EXITWINDOWS
;
157 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_REQUIRED
| WINSTA_ALL_ACCESS
;
158 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
161 /* retrieve the process window station, checking the handle access rights */
162 struct winstation
*get_process_winstation( struct process
*process
, unsigned int access
)
164 return (struct winstation
*)get_handle_obj( process
, process
->winstation
,
165 access
, &winstation_ops
);
168 /* build the full name of a desktop object */
169 static WCHAR
*build_desktop_name( const struct unicode_str
*name
,
170 struct winstation
*winstation
, struct unicode_str
*res
)
172 const WCHAR
*winstation_name
;
174 data_size_t winstation_len
;
176 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) ))
178 set_error( STATUS_INVALID_PARAMETER
);
182 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
185 res
->len
= winstation_len
+ name
->len
+ sizeof(WCHAR
);
186 if (!(full_name
= mem_alloc( res
->len
))) return NULL
;
187 memcpy( full_name
, winstation_name
, winstation_len
);
188 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
189 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
->str
, name
->len
);
190 res
->str
= full_name
;
194 /* retrieve a pointer to a desktop object */
195 static inline struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
,
196 unsigned int access
)
198 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
201 /* create a desktop object */
202 static struct desktop
*create_desktop( const struct unicode_str
*name
, unsigned int attr
,
203 unsigned int flags
, struct winstation
*winstation
)
205 struct desktop
*desktop
;
206 struct unicode_str full_str
;
209 if (!(full_name
= build_desktop_name( name
, winstation
, &full_str
))) return NULL
;
211 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, &full_str
, attr
)))
213 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
215 /* initialize it if it didn't already exist */
216 desktop
->flags
= flags
;
217 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
218 desktop
->top_window
= NULL
;
219 desktop
->global_hooks
= NULL
;
220 desktop
->close_timeout
= NULL
;
222 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
229 static void desktop_dump( struct object
*obj
, int verbose
)
231 struct desktop
*desktop
= (struct desktop
*)obj
;
233 fprintf( stderr
, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
234 desktop
->flags
, desktop
->winstation
, desktop
->top_window
, desktop
->global_hooks
);
235 dump_object_name( &desktop
->obj
);
236 fputc( '\n', stderr
);
239 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
241 struct thread
*thread
;
243 /* check if the handle is currently used by the process or one of its threads */
244 if (process
->desktop
== handle
) return 0;
245 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
246 if (thread
->desktop
== handle
) return 0;
250 static void desktop_destroy( struct object
*obj
)
252 struct desktop
*desktop
= (struct desktop
*)obj
;
254 if (desktop
->top_window
) destroy_window( desktop
->top_window
);
255 if (desktop
->global_hooks
) release_object( desktop
->global_hooks
);
256 if (desktop
->close_timeout
) remove_timeout_user( desktop
->close_timeout
);
257 list_remove( &desktop
->entry
);
258 release_object( desktop
->winstation
);
261 static unsigned int desktop_map_access( struct object
*obj
, unsigned int access
)
263 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| DESKTOP_READOBJECTS
| DESKTOP_ENUMERATE
;
264 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| DESKTOP_CREATEMENU
| DESKTOP_CREATEWINDOW
|
265 DESKTOP_HOOKCONTROL
| DESKTOP_JOURNALRECORD
| DESKTOP_JOURNALPLAYBACK
|
266 DESKTOP_WRITEOBJECTS
;
267 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| DESKTOP_SWITCHDESKTOP
;
268 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_REQUIRED
| DESKTOP_ALL_ACCESS
;
269 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
272 /* retrieve the thread desktop, checking the handle access rights */
273 struct desktop
*get_thread_desktop( struct thread
*thread
, unsigned int access
)
275 return get_desktop_obj( thread
->process
, thread
->desktop
, access
);
278 /* set the process default desktop handle */
279 void set_process_default_desktop( struct process
*process
, struct desktop
*desktop
,
280 obj_handle_t handle
)
282 struct thread
*thread
;
283 struct desktop
*old_desktop
;
285 if (process
->desktop
== handle
) return; /* nothing to do */
287 if (!(old_desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error();
288 process
->desktop
= handle
;
290 /* set desktop for threads that don't have one yet */
291 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
292 if (!thread
->desktop
) thread
->desktop
= handle
;
295 if (desktop
->close_timeout
)
297 remove_timeout_user( desktop
->close_timeout
);
298 desktop
->close_timeout
= NULL
;
302 old_desktop
->users
--;
303 release_object( old_desktop
);
307 /* connect a process to its window station */
308 void connect_process_winstation( struct process
*process
, struct thread
*parent
)
310 struct winstation
*winstation
= NULL
;
311 struct desktop
*desktop
= NULL
;
314 /* check for an inherited winstation handle (don't ask...) */
315 if ((handle
= find_inherited_handle( process
, &winstation_ops
)))
317 winstation
= (struct winstation
*)get_handle_obj( process
, handle
, 0, &winstation_ops
);
319 else if (parent
&& parent
->process
->winstation
)
321 handle
= duplicate_handle( parent
->process
, parent
->process
->winstation
,
322 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
323 winstation
= (struct winstation
*)get_handle_obj( process
, handle
, 0, &winstation_ops
);
325 if (!winstation
) goto done
;
326 process
->winstation
= handle
;
328 if ((handle
= find_inherited_handle( process
, &desktop_ops
)))
330 desktop
= get_desktop_obj( process
, handle
, 0 );
331 if (!desktop
|| desktop
->winstation
!= winstation
) goto done
;
333 else if (parent
&& parent
->desktop
)
335 desktop
= get_desktop_obj( parent
->process
, parent
->desktop
, 0 );
336 if (!desktop
|| desktop
->winstation
!= winstation
) goto done
;
337 handle
= duplicate_handle( parent
->process
, parent
->desktop
,
338 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
341 if (handle
) set_process_default_desktop( process
, desktop
, handle
);
344 if (desktop
) release_object( desktop
);
345 if (winstation
) release_object( winstation
);
349 static void close_desktop_timeout( void *private )
351 struct desktop
*desktop
= private;
353 desktop
->close_timeout
= NULL
;
354 unlink_named_object( &desktop
->obj
); /* make sure no other process can open it */
355 close_desktop_window( desktop
); /* and signal the owner to quit */
358 /* close the desktop of a given process */
359 void close_process_desktop( struct process
*process
)
361 struct desktop
*desktop
;
363 if (process
->desktop
&& (desktop
= get_desktop_obj( process
, process
->desktop
, 0 )))
365 assert( desktop
->users
> 0 );
367 /* if we have one remaining user, it has to be the manager of the desktop window */
368 if (desktop
->users
== 1 && get_top_window_owner( desktop
))
370 assert( !desktop
->close_timeout
);
371 desktop
->close_timeout
= add_timeout_user( -TICKS_PER_SEC
, close_desktop_timeout
, desktop
);
373 release_object( desktop
);
375 clear_error(); /* ignore errors */
378 /* close the desktop of a given thread */
379 void close_thread_desktop( struct thread
*thread
)
381 obj_handle_t handle
= thread
->desktop
;
384 if (handle
) close_handle( thread
->process
, handle
);
385 clear_error(); /* ignore errors */
389 /* create a window station */
390 DECL_HANDLER(create_winstation
)
392 struct winstation
*winstation
;
393 struct unicode_str name
;
396 get_req_unicode_str( &name
);
397 if ((winstation
= create_winstation( &name
, req
->attributes
, req
->flags
)))
399 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->attributes
);
400 release_object( winstation
);
404 /* open a handle to a window station */
405 DECL_HANDLER(open_winstation
)
407 struct unicode_str name
;
409 get_req_unicode_str( &name
);
410 if (winstation_namespace
)
411 reply
->handle
= open_object( winstation_namespace
, &name
, &winstation_ops
, req
->access
,
414 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
418 /* close a window station */
419 DECL_HANDLER(close_winstation
)
421 struct winstation
*winstation
;
423 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
424 0, &winstation_ops
)))
426 if (!close_handle( current
->process
, req
->handle
)) set_error( STATUS_ACCESS_DENIED
);
427 release_object( winstation
);
432 /* get the process current window station */
433 DECL_HANDLER(get_process_winstation
)
435 reply
->handle
= current
->process
->winstation
;
439 /* set the process current window station */
440 DECL_HANDLER(set_process_winstation
)
442 struct winstation
*winstation
;
444 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
445 0, &winstation_ops
)))
447 /* FIXME: should we close the old one? */
448 current
->process
->winstation
= req
->handle
;
449 release_object( winstation
);
453 /* create a desktop */
454 DECL_HANDLER(create_desktop
)
456 struct desktop
*desktop
;
457 struct winstation
*winstation
;
458 struct unicode_str name
;
461 get_req_unicode_str( &name
);
462 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
464 if ((desktop
= create_desktop( &name
, req
->attributes
, req
->flags
, winstation
)))
466 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->attributes
);
467 release_object( desktop
);
469 release_object( winstation
);
473 /* open a handle to a desktop */
474 DECL_HANDLER(open_desktop
)
476 struct winstation
*winstation
;
477 struct unicode_str name
;
479 get_req_unicode_str( &name
);
480 if ((winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
482 struct unicode_str full_str
;
485 if ((full_name
= build_desktop_name( &name
, winstation
, &full_str
)))
487 reply
->handle
= open_object( winstation_namespace
, &full_str
, &desktop_ops
, req
->access
,
491 release_object( winstation
);
496 /* close a desktop */
497 DECL_HANDLER(close_desktop
)
499 struct desktop
*desktop
;
501 /* make sure it is a desktop handle */
502 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
505 if (!close_handle( current
->process
, req
->handle
)) set_error( STATUS_DEVICE_BUSY
);
506 release_object( desktop
);
511 /* get the thread current desktop */
512 DECL_HANDLER(get_thread_desktop
)
514 struct thread
*thread
;
516 if (!(thread
= get_thread_from_id( req
->tid
))) return;
517 reply
->handle
= thread
->desktop
;
518 release_object( thread
);
522 /* set the thread current desktop */
523 DECL_HANDLER(set_thread_desktop
)
525 struct desktop
*old_desktop
, *new_desktop
;
526 struct winstation
*winstation
;
528 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
531 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
533 release_object( winstation
);
536 if (new_desktop
->winstation
!= winstation
)
538 set_error( STATUS_ACCESS_DENIED
);
539 release_object( new_desktop
);
540 release_object( winstation
);
544 /* check if we are changing to a new desktop */
546 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
547 clear_error(); /* ignore error */
549 /* when changing desktop, we can't have any users on the current one */
550 if (old_desktop
!= new_desktop
&& current
->desktop_users
> 0)
551 set_error( STATUS_DEVICE_BUSY
);
553 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
555 if (!current
->process
->desktop
)
556 set_process_default_desktop( current
->process
, new_desktop
, req
->handle
);
558 if (old_desktop
!= new_desktop
&& current
->queue
) detach_thread_input( current
);
560 if (old_desktop
) release_object( old_desktop
);
561 release_object( new_desktop
);
562 release_object( winstation
);
566 /* get/set information about a user object (window station or desktop) */
567 DECL_HANDLER(set_user_object_info
)
571 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
573 if (obj
->ops
== &desktop_ops
)
575 struct desktop
*desktop
= (struct desktop
*)obj
;
576 reply
->is_desktop
= 1;
577 reply
->old_obj_flags
= desktop
->flags
;
578 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
580 else if (obj
->ops
== &winstation_ops
)
582 struct winstation
*winstation
= (struct winstation
*)obj
;
583 reply
->is_desktop
= 0;
584 reply
->old_obj_flags
= winstation
->flags
;
585 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
589 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
590 release_object( obj
);
593 if (get_reply_max_size())
596 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
598 /* if there is a backslash return the part of the name after it */
599 if (name
&& (ptr
= memchrW( name
, '\\', len
)))
601 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
604 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
606 release_object( obj
);