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 void desktop_dump( struct object
*obj
, int verbose
);
50 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
51 static void desktop_destroy( struct object
*obj
);
53 static const struct object_ops winstation_ops
=
55 sizeof(struct winstation
), /* size */
56 winstation_dump
, /* dump */
57 no_add_queue
, /* add_queue */
58 NULL
, /* remove_queue */
61 no_signal
, /* signal */
62 no_get_fd
, /* get_fd */
63 no_map_access
, /* map_access */
64 no_lookup_name
, /* lookup_name */
65 winstation_close_handle
, /* close_handle */
66 winstation_destroy
/* destroy */
70 static const struct object_ops desktop_ops
=
72 sizeof(struct desktop
), /* size */
73 desktop_dump
, /* dump */
74 no_add_queue
, /* add_queue */
75 NULL
, /* remove_queue */
78 no_signal
, /* signal */
79 no_get_fd
, /* get_fd */
80 no_map_access
, /* map_access */
81 no_lookup_name
, /* lookup_name */
82 desktop_close_handle
, /* close_handle */
83 desktop_destroy
/* destroy */
86 #define DESKTOP_ALL_ACCESS 0x01ff
88 /* create a winstation object */
89 static struct winstation
*create_winstation( const struct unicode_str
*name
, unsigned int attr
,
92 struct winstation
*winstation
;
94 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7 )))
97 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
99 set_error( STATUS_INVALID_PARAMETER
);
103 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, attr
)))
105 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
107 /* initialize it if it didn't already exist */
108 winstation
->flags
= flags
;
109 winstation
->clipboard
= NULL
;
110 winstation
->atom_table
= NULL
;
111 list_add_tail( &winstation_list
, &winstation
->entry
);
112 list_init( &winstation
->desktops
);
118 static void winstation_dump( struct object
*obj
, int verbose
)
120 struct winstation
*winstation
= (struct winstation
*)obj
;
122 fprintf( stderr
, "Winstation flags=%x clipboard=%p atoms=%p ",
123 winstation
->flags
, winstation
->clipboard
, winstation
->atom_table
);
124 dump_object_name( &winstation
->obj
);
125 fputc( '\n', stderr
);
128 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
130 return (process
->winstation
!= handle
);
133 static void winstation_destroy( struct object
*obj
)
135 struct winstation
*winstation
= (struct winstation
*)obj
;
137 list_remove( &winstation
->entry
);
138 if (winstation
->clipboard
) release_object( winstation
->clipboard
);
139 if (winstation
->atom_table
) release_object( winstation
->atom_table
);
142 /* retrieve the process window station, checking the handle access rights */
143 struct winstation
*get_process_winstation( struct process
*process
, unsigned int access
)
145 return (struct winstation
*)get_handle_obj( process
, process
->winstation
,
146 access
, &winstation_ops
);
149 /* build the full name of a desktop object */
150 static WCHAR
*build_desktop_name( const struct unicode_str
*name
,
151 struct winstation
*winstation
, struct unicode_str
*res
)
153 const WCHAR
*winstation_name
;
155 data_size_t winstation_len
;
157 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) ))
159 set_error( STATUS_INVALID_PARAMETER
);
163 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
166 res
->len
= winstation_len
+ name
->len
+ sizeof(WCHAR
);
167 if (!(full_name
= mem_alloc( res
->len
))) return NULL
;
168 memcpy( full_name
, winstation_name
, winstation_len
);
169 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
170 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
->str
, name
->len
);
171 res
->str
= full_name
;
175 /* retrieve a pointer to a desktop object */
176 inline static struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
,
177 unsigned int access
)
179 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
182 /* create a desktop object */
183 static struct desktop
*create_desktop( const struct unicode_str
*name
, unsigned int attr
,
184 unsigned int flags
, struct winstation
*winstation
)
186 struct desktop
*desktop
;
187 struct unicode_str full_str
;
190 if (!(full_name
= build_desktop_name( name
, winstation
, &full_str
))) return NULL
;
192 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, &full_str
, attr
)))
194 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
196 /* initialize it if it didn't already exist */
197 desktop
->flags
= flags
;
198 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
199 desktop
->top_window
= NULL
;
200 desktop
->global_hooks
= NULL
;
201 desktop
->close_timeout
= NULL
;
203 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
210 static void desktop_dump( struct object
*obj
, int verbose
)
212 struct desktop
*desktop
= (struct desktop
*)obj
;
214 fprintf( stderr
, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
215 desktop
->flags
, desktop
->winstation
, desktop
->top_window
, desktop
->global_hooks
);
216 dump_object_name( &desktop
->obj
);
217 fputc( '\n', stderr
);
220 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
222 struct thread
*thread
;
224 /* check if the handle is currently used by the process or one of its threads */
225 if (process
->desktop
== handle
) return 0;
226 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
227 if (thread
->desktop
== handle
) return 0;
231 static void desktop_destroy( struct object
*obj
)
233 struct desktop
*desktop
= (struct desktop
*)obj
;
235 if (desktop
->top_window
) destroy_window( desktop
->top_window
);
236 if (desktop
->global_hooks
) release_object( desktop
->global_hooks
);
237 if (desktop
->close_timeout
) remove_timeout_user( desktop
->close_timeout
);
238 list_remove( &desktop
->entry
);
239 release_object( desktop
->winstation
);
242 /* retrieve the thread desktop, checking the handle access rights */
243 struct desktop
*get_thread_desktop( struct thread
*thread
, unsigned int access
)
245 return get_desktop_obj( thread
->process
, thread
->desktop
, access
);
248 /* set the process default desktop handle */
249 void set_process_default_desktop( struct process
*process
, struct desktop
*desktop
,
250 obj_handle_t handle
)
252 struct thread
*thread
;
253 struct desktop
*old_desktop
;
255 if (process
->desktop
== handle
) return; /* nothing to do */
257 if (!(old_desktop
= get_desktop_obj( process
, process
->desktop
, 0 ))) clear_error();
258 process
->desktop
= handle
;
260 /* set desktop for threads that don't have one yet */
261 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
262 if (!thread
->desktop
) thread
->desktop
= handle
;
265 if (desktop
->close_timeout
)
267 remove_timeout_user( desktop
->close_timeout
);
268 desktop
->close_timeout
= NULL
;
272 old_desktop
->users
--;
273 release_object( old_desktop
);
277 /* connect a process to its window station */
278 void connect_process_winstation( struct process
*process
, struct thread
*parent
)
280 struct winstation
*winstation
= NULL
;
281 struct desktop
*desktop
= NULL
;
284 /* check for an inherited winstation handle (don't ask...) */
285 if ((handle
= find_inherited_handle( process
, &winstation_ops
)))
287 winstation
= (struct winstation
*)get_handle_obj( process
, handle
, 0, &winstation_ops
);
289 else if (parent
&& parent
->process
->winstation
)
291 handle
= duplicate_handle( parent
->process
, parent
->process
->winstation
,
292 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
293 winstation
= (struct winstation
*)get_handle_obj( process
, handle
, 0, &winstation_ops
);
295 if (!winstation
) goto done
;
296 process
->winstation
= handle
;
298 if ((handle
= find_inherited_handle( process
, &desktop_ops
)))
300 desktop
= get_desktop_obj( process
, handle
, 0 );
301 if (!desktop
|| desktop
->winstation
!= winstation
) goto done
;
303 else if (parent
&& parent
->desktop
)
305 desktop
= get_desktop_obj( parent
->process
, parent
->desktop
, 0 );
306 if (!desktop
|| desktop
->winstation
!= winstation
) goto done
;
307 handle
= duplicate_handle( parent
->process
, parent
->desktop
,
308 process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
311 if (handle
) set_process_default_desktop( process
, desktop
, handle
);
314 if (desktop
) release_object( desktop
);
315 if (winstation
) release_object( winstation
);
319 static void close_desktop_timeout( void *private )
321 struct desktop
*desktop
= private;
323 desktop
->close_timeout
= NULL
;
324 unlink_named_object( &desktop
->obj
); /* make sure no other process can open it */
325 close_desktop_window( desktop
); /* and signal the owner to quit */
328 /* close the desktop of a given process */
329 void close_process_desktop( struct process
*process
)
331 struct desktop
*desktop
;
333 if (process
->desktop
&& (desktop
= get_desktop_obj( process
, process
->desktop
, 0 )))
335 assert( desktop
->users
> 0 );
337 /* if we have one remaining user, it has to be the manager of the desktop window */
338 if (desktop
->users
== 1 && get_top_window_owner( desktop
))
340 struct timeval when
= current_time
;
341 add_timeout( &when
, 1000 );
342 assert( !desktop
->close_timeout
);
343 desktop
->close_timeout
= add_timeout_user( &when
, close_desktop_timeout
, desktop
);
345 release_object( desktop
);
347 clear_error(); /* ignore errors */
350 /* close the desktop of a given thread */
351 void close_thread_desktop( struct thread
*thread
)
353 obj_handle_t handle
= thread
->desktop
;
356 if (handle
) close_handle( thread
->process
, handle
);
357 clear_error(); /* ignore errors */
361 /* create a window station */
362 DECL_HANDLER(create_winstation
)
364 struct winstation
*winstation
;
365 struct unicode_str name
;
368 get_req_unicode_str( &name
);
369 if ((winstation
= create_winstation( &name
, req
->attributes
, req
->flags
)))
371 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->attributes
);
372 release_object( winstation
);
376 /* open a handle to a window station */
377 DECL_HANDLER(open_winstation
)
379 struct unicode_str name
;
381 get_req_unicode_str( &name
);
382 if (winstation_namespace
)
383 reply
->handle
= open_object( winstation_namespace
, &name
, &winstation_ops
, req
->access
,
386 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
390 /* close a window station */
391 DECL_HANDLER(close_winstation
)
393 struct winstation
*winstation
;
395 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
396 0, &winstation_ops
)))
398 if (!close_handle( current
->process
, req
->handle
)) set_error( STATUS_ACCESS_DENIED
);
399 release_object( winstation
);
404 /* get the process current window station */
405 DECL_HANDLER(get_process_winstation
)
407 reply
->handle
= current
->process
->winstation
;
411 /* set the process current window station */
412 DECL_HANDLER(set_process_winstation
)
414 struct winstation
*winstation
;
416 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
417 0, &winstation_ops
)))
419 /* FIXME: should we close the old one? */
420 current
->process
->winstation
= req
->handle
;
421 release_object( winstation
);
425 /* create a desktop */
426 DECL_HANDLER(create_desktop
)
428 struct desktop
*desktop
;
429 struct winstation
*winstation
;
430 struct unicode_str name
;
433 get_req_unicode_str( &name
);
434 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
436 if ((desktop
= create_desktop( &name
, req
->attributes
, req
->flags
, winstation
)))
438 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->attributes
);
439 release_object( desktop
);
441 release_object( winstation
);
445 /* open a handle to a desktop */
446 DECL_HANDLER(open_desktop
)
448 struct winstation
*winstation
;
449 struct unicode_str name
;
451 get_req_unicode_str( &name
);
452 if ((winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
454 struct unicode_str full_str
;
457 if ((full_name
= build_desktop_name( &name
, winstation
, &full_str
)))
459 reply
->handle
= open_object( winstation_namespace
, &full_str
, &desktop_ops
, req
->access
,
463 release_object( winstation
);
468 /* close a desktop */
469 DECL_HANDLER(close_desktop
)
471 struct desktop
*desktop
;
473 /* make sure it is a desktop handle */
474 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
477 if (!close_handle( current
->process
, req
->handle
)) set_error( STATUS_DEVICE_BUSY
);
478 release_object( desktop
);
483 /* get the thread current desktop */
484 DECL_HANDLER(get_thread_desktop
)
486 struct thread
*thread
;
488 if (!(thread
= get_thread_from_id( req
->tid
))) return;
489 reply
->handle
= thread
->desktop
;
490 release_object( thread
);
494 /* set the thread current desktop */
495 DECL_HANDLER(set_thread_desktop
)
497 struct desktop
*old_desktop
, *new_desktop
;
498 struct winstation
*winstation
;
500 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
503 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
505 release_object( winstation
);
508 if (new_desktop
->winstation
!= winstation
)
510 set_error( STATUS_ACCESS_DENIED
);
511 release_object( new_desktop
);
512 release_object( winstation
);
516 /* check if we are changing to a new desktop */
518 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
519 clear_error(); /* ignore error */
521 /* when changing desktop, we can't have any users on the current one */
522 if (old_desktop
!= new_desktop
&& current
->desktop_users
> 0)
523 set_error( STATUS_DEVICE_BUSY
);
525 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
527 if (!current
->process
->desktop
)
528 set_process_default_desktop( current
->process
, new_desktop
, req
->handle
);
530 if (old_desktop
!= new_desktop
&& current
->queue
) detach_thread_input( current
);
532 if (old_desktop
) release_object( old_desktop
);
533 release_object( new_desktop
);
534 release_object( winstation
);
538 /* get/set information about a user object (window station or desktop) */
539 DECL_HANDLER(set_user_object_info
)
543 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
545 if (obj
->ops
== &desktop_ops
)
547 struct desktop
*desktop
= (struct desktop
*)obj
;
548 reply
->is_desktop
= 1;
549 reply
->old_obj_flags
= desktop
->flags
;
550 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
552 else if (obj
->ops
== &winstation_ops
)
554 struct winstation
*winstation
= (struct winstation
*)obj
;
555 reply
->is_desktop
= 0;
556 reply
->old_obj_flags
= winstation
->flags
;
557 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
561 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
562 release_object( obj
);
565 if (get_reply_max_size())
568 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
570 /* if there is a backslash return the part of the name after it */
571 if (name
&& (ptr
= memchrW( name
, '\\', len
)))
573 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
576 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
578 release_object( obj
);