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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
37 #include "wine/unicode.h"
40 static struct list winstation_list
= LIST_INIT(winstation_list
);
41 static struct winstation
*interactive_winstation
;
42 static struct namespace *winstation_namespace
;
44 static void winstation_dump( struct object
*obj
, int verbose
);
45 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
46 static void winstation_destroy( struct object
*obj
);
47 static void desktop_dump( struct object
*obj
, int verbose
);
48 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
49 static void desktop_destroy( struct object
*obj
);
51 static const struct object_ops winstation_ops
=
53 sizeof(struct winstation
), /* size */
54 winstation_dump
, /* dump */
55 no_add_queue
, /* add_queue */
56 NULL
, /* remove_queue */
59 no_signal
, /* signal */
60 no_get_fd
, /* get_fd */
61 winstation_close_handle
, /* close_handle */
62 winstation_destroy
/* destroy */
66 static const struct object_ops desktop_ops
=
68 sizeof(struct desktop
), /* size */
69 desktop_dump
, /* dump */
70 no_add_queue
, /* add_queue */
71 NULL
, /* remove_queue */
74 no_signal
, /* signal */
75 no_get_fd
, /* get_fd */
76 desktop_close_handle
, /* close_handle */
77 desktop_destroy
/* destroy */
80 #define DESKTOP_ALL_ACCESS 0x01ff
82 /* create a winstation object */
83 static struct winstation
*create_winstation( const WCHAR
*name
, size_t len
, unsigned int flags
)
85 struct winstation
*winstation
;
87 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7, FALSE
)))
90 if (memchrW( name
, '\\', len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
92 set_error( STATUS_INVALID_PARAMETER
);
96 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, len
)))
98 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
100 /* initialize it if it didn't already exist */
101 winstation
->flags
= flags
;
102 winstation
->clipboard
= NULL
;
103 winstation
->atom_table
= NULL
;
104 list_add_tail( &winstation_list
, &winstation
->entry
);
105 list_init( &winstation
->desktops
);
111 static void winstation_dump( struct object
*obj
, int verbose
)
113 struct winstation
*winstation
= (struct winstation
*)obj
;
115 fprintf( stderr
, "Winstation flags=%x clipboard=%p atoms=%p ",
116 winstation
->flags
, winstation
->clipboard
, winstation
->atom_table
);
117 dump_object_name( &winstation
->obj
);
118 fputc( '\n', stderr
);
121 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
123 return (process
->winstation
!= handle
);
126 static void winstation_destroy( struct object
*obj
)
128 struct winstation
*winstation
= (struct winstation
*)obj
;
130 if (winstation
== interactive_winstation
) interactive_winstation
= NULL
;
131 list_remove( &winstation
->entry
);
132 if (winstation
->clipboard
) release_object( winstation
->clipboard
);
133 if (winstation
->atom_table
) release_object( winstation
->atom_table
);
136 /* retrieve the process window station, checking the handle access rights */
137 struct winstation
*get_process_winstation( struct process
*process
, unsigned int access
)
139 return (struct winstation
*)get_handle_obj( process
, process
->winstation
,
140 access
, &winstation_ops
);
143 /* build the full name of a desktop object */
144 static WCHAR
*build_desktop_name( const WCHAR
*name
, size_t len
,
145 struct winstation
*winstation
, size_t *res_len
)
147 const WCHAR
*winstation_name
;
149 size_t winstation_len
;
151 if (memchrW( name
, '\\', len
/ sizeof(WCHAR
) ))
153 set_error( STATUS_INVALID_PARAMETER
);
157 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
160 *res_len
= winstation_len
+ len
+ sizeof(WCHAR
);
161 if (!(full_name
= mem_alloc( *res_len
))) return NULL
;
162 memcpy( full_name
, winstation_name
, winstation_len
);
163 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
164 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
, len
);
168 /* retrieve a pointer to a desktop object */
169 inline static struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
,
170 unsigned int access
)
172 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
175 /* create a desktop object */
176 static struct desktop
*create_desktop( const WCHAR
*name
, size_t len
, unsigned int flags
,
177 struct winstation
*winstation
)
179 struct desktop
*desktop
;
183 if (!(full_name
= build_desktop_name( name
, len
, winstation
, &full_len
))) return NULL
;
185 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, full_name
, full_len
)))
187 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
189 /* initialize it if it didn't already exist */
190 desktop
->flags
= flags
;
191 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
192 desktop
->top_window
= NULL
;
193 desktop
->global_hooks
= NULL
;
194 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
201 static void desktop_dump( struct object
*obj
, int verbose
)
203 struct desktop
*desktop
= (struct desktop
*)obj
;
205 fprintf( stderr
, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
206 desktop
->flags
, desktop
->winstation
, desktop
->top_window
, desktop
->global_hooks
);
207 dump_object_name( &desktop
->obj
);
208 fputc( '\n', stderr
);
211 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
213 struct thread
*thread
;
215 /* check if the handle is currently used by the process or one of its threads */
216 if (process
->desktop
== handle
) return 0;
217 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
218 if (thread
->desktop
== handle
) return 0;
222 static void desktop_destroy( struct object
*obj
)
224 struct desktop
*desktop
= (struct desktop
*)obj
;
226 if (desktop
->top_window
) destroy_window( desktop
->top_window
);
227 if (desktop
->global_hooks
) release_object( desktop
->global_hooks
);
228 list_remove( &desktop
->entry
);
229 release_object( desktop
->winstation
);
232 /* retrieve the thread desktop, checking the handle access rights */
233 struct desktop
*get_thread_desktop( struct thread
*thread
, unsigned int access
)
235 return get_desktop_obj( thread
->process
, thread
->desktop
, access
);
238 /* connect a process to its window station */
239 void connect_process_winstation( struct process
*process
, const WCHAR
*name
, size_t len
)
241 struct winstation
*winstation
;
243 if (process
->winstation
) return; /* already has one */
245 /* check for an inherited winstation handle (don't ask...) */
246 if ((process
->winstation
= find_inherited_handle( process
, &winstation_ops
)) != 0) return;
250 winstation
= create_winstation( name
, len
, 0 );
254 if (!interactive_winstation
)
256 static const WCHAR winsta0W
[] = {'W','i','n','S','t','a','0'};
257 interactive_winstation
= create_winstation( winsta0W
, sizeof(winsta0W
), 0 );
258 winstation
= interactive_winstation
;
260 else winstation
= (struct winstation
*)grab_object( interactive_winstation
);
264 process
->winstation
= alloc_handle( process
, winstation
, WINSTA_ALL_ACCESS
, FALSE
);
265 release_object( winstation
);
267 clear_error(); /* ignore errors */
270 /* connect a process to its main desktop */
271 void connect_process_desktop( struct process
*process
, const WCHAR
*name
, size_t len
)
273 struct desktop
*desktop
;
274 struct winstation
*winstation
;
276 if (process
->desktop
) return; /* already has one */
278 if ((winstation
= get_process_winstation( process
, WINSTA_CREATEDESKTOP
)))
280 static const WCHAR defaultW
[] = {'D','e','f','a','u','l','t'};
285 len
= sizeof(defaultW
);
287 if ((desktop
= create_desktop( name
, len
, 0, winstation
)))
289 process
->desktop
= alloc_handle( process
, desktop
, DESKTOP_ALL_ACCESS
, FALSE
);
290 release_object( desktop
);
292 release_object( winstation
);
294 clear_error(); /* ignore errors */
297 /* close the desktop of a given thread */
298 void close_thread_desktop( struct thread
*thread
)
300 obj_handle_t handle
= thread
->desktop
;
303 if (handle
) close_handle( thread
->process
, handle
, NULL
);
304 clear_error(); /* ignore errors */
308 /* create a window station */
309 DECL_HANDLER(create_winstation
)
311 struct winstation
*winstation
;
314 if ((winstation
= create_winstation( get_req_data(), get_req_data_size(), req
->flags
)))
316 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->inherit
);
317 release_object( winstation
);
321 /* open a handle to a window station */
322 DECL_HANDLER(open_winstation
)
324 if (winstation_namespace
)
325 reply
->handle
= open_object( winstation_namespace
, get_req_data(), get_req_data_size(),
326 &winstation_ops
, req
->access
, (req
->inherit
) ? OBJ_INHERIT
:0 );
328 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
332 /* close a window station */
333 DECL_HANDLER(close_winstation
)
335 struct winstation
*winstation
;
337 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
338 0, &winstation_ops
)))
340 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_ACCESS_DENIED
);
341 release_object( winstation
);
346 /* get the process current window station */
347 DECL_HANDLER(get_process_winstation
)
349 reply
->handle
= current
->process
->winstation
;
353 /* set the process current window station */
354 DECL_HANDLER(set_process_winstation
)
356 struct winstation
*winstation
;
358 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
359 0, &winstation_ops
)))
361 /* FIXME: should we close the old one? */
362 current
->process
->winstation
= req
->handle
;
363 release_object( winstation
);
367 /* create a desktop */
368 DECL_HANDLER(create_desktop
)
370 struct desktop
*desktop
;
371 struct winstation
*winstation
;
374 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
376 if ((desktop
= create_desktop( get_req_data(), get_req_data_size(),
377 req
->flags
, winstation
)))
379 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->inherit
);
380 release_object( desktop
);
382 release_object( winstation
);
386 /* open a handle to a desktop */
387 DECL_HANDLER(open_desktop
)
389 struct winstation
*winstation
;
391 if ((winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
396 if ((full_name
= build_desktop_name( get_req_data(), get_req_data_size(),
397 winstation
, &full_len
)))
399 reply
->handle
= open_object( winstation_namespace
, full_name
, full_len
,
400 &desktop_ops
, req
->access
,
401 (req
->inherit
) ? OBJ_INHERIT
:0 );
404 release_object( winstation
);
409 /* close a desktop */
410 DECL_HANDLER(close_desktop
)
412 struct desktop
*desktop
;
414 /* make sure it is a desktop handle */
415 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
418 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_DEVICE_BUSY
);
419 release_object( desktop
);
424 /* get the thread current desktop */
425 DECL_HANDLER(get_thread_desktop
)
427 struct thread
*thread
;
429 if (!(thread
= get_thread_from_id( req
->tid
))) return;
430 reply
->handle
= thread
->desktop
;
431 release_object( thread
);
435 /* set the thread current desktop */
436 DECL_HANDLER(set_thread_desktop
)
438 struct desktop
*old_desktop
, *new_desktop
;
439 struct winstation
*winstation
;
441 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
444 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
446 release_object( winstation
);
449 if (new_desktop
->winstation
!= winstation
)
451 set_error( STATUS_ACCESS_DENIED
);
452 release_object( new_desktop
);
453 release_object( winstation
);
457 /* check if we are changing to a new desktop */
459 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
460 clear_error(); /* ignore error */
462 /* when changing desktop, we can't have any users on the current one */
463 if (old_desktop
!= new_desktop
&& current
->desktop_users
> 0)
464 set_error( STATUS_DEVICE_BUSY
);
466 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
468 if (old_desktop
!= new_desktop
) detach_thread_input( current
);
470 if (old_desktop
) release_object( old_desktop
);
471 release_object( new_desktop
);
472 release_object( winstation
);
476 /* get/set information about a user object (window station or desktop) */
477 DECL_HANDLER(set_user_object_info
)
481 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
483 if (obj
->ops
== &desktop_ops
)
485 struct desktop
*desktop
= (struct desktop
*)obj
;
486 reply
->is_desktop
= 1;
487 reply
->old_obj_flags
= desktop
->flags
;
488 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
490 else if (obj
->ops
== &winstation_ops
)
492 struct winstation
*winstation
= (struct winstation
*)obj
;
493 reply
->is_desktop
= 0;
494 reply
->old_obj_flags
= winstation
->flags
;
495 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
499 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
500 release_object( obj
);
503 if (get_reply_max_size())
506 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
508 /* if there is a backslash return the part of the name after it */
509 if (name
&& (ptr
= memchrW( name
, '\\', len
)))
511 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
514 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
516 release_object( obj
);