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 struct unicode_str
*name
, unsigned int attr
,
86 struct winstation
*winstation
;
88 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7 )))
91 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
93 set_error( STATUS_INVALID_PARAMETER
);
97 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, attr
)))
99 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
101 /* initialize it if it didn't already exist */
102 winstation
->flags
= flags
;
103 winstation
->clipboard
= NULL
;
104 winstation
->atom_table
= NULL
;
105 list_add_tail( &winstation_list
, &winstation
->entry
);
106 list_init( &winstation
->desktops
);
112 static void winstation_dump( struct object
*obj
, int verbose
)
114 struct winstation
*winstation
= (struct winstation
*)obj
;
116 fprintf( stderr
, "Winstation flags=%x clipboard=%p atoms=%p ",
117 winstation
->flags
, winstation
->clipboard
, winstation
->atom_table
);
118 dump_object_name( &winstation
->obj
);
119 fputc( '\n', stderr
);
122 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
124 return (process
->winstation
!= handle
);
127 static void winstation_destroy( struct object
*obj
)
129 struct winstation
*winstation
= (struct winstation
*)obj
;
131 if (winstation
== interactive_winstation
) interactive_winstation
= NULL
;
132 list_remove( &winstation
->entry
);
133 if (winstation
->clipboard
) release_object( winstation
->clipboard
);
134 if (winstation
->atom_table
) release_object( winstation
->atom_table
);
137 /* retrieve the process window station, checking the handle access rights */
138 struct winstation
*get_process_winstation( struct process
*process
, unsigned int access
)
140 return (struct winstation
*)get_handle_obj( process
, process
->winstation
,
141 access
, &winstation_ops
);
144 /* build the full name of a desktop object */
145 static WCHAR
*build_desktop_name( const struct unicode_str
*name
,
146 struct winstation
*winstation
, struct unicode_str
*res
)
148 const WCHAR
*winstation_name
;
150 size_t winstation_len
;
152 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) ))
154 set_error( STATUS_INVALID_PARAMETER
);
158 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
161 res
->len
= winstation_len
+ name
->len
+ sizeof(WCHAR
);
162 if (!(full_name
= mem_alloc( res
->len
))) return NULL
;
163 memcpy( full_name
, winstation_name
, winstation_len
);
164 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
165 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
->str
, name
->len
);
166 res
->str
= full_name
;
170 /* retrieve a pointer to a desktop object */
171 inline static struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
,
172 unsigned int access
)
174 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
177 /* create a desktop object */
178 static struct desktop
*create_desktop( const struct unicode_str
*name
, unsigned int attr
,
179 unsigned int flags
, struct winstation
*winstation
)
181 struct desktop
*desktop
;
182 struct unicode_str full_str
;
185 if (!(full_name
= build_desktop_name( name
, winstation
, &full_str
))) return NULL
;
187 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, &full_str
, attr
)))
189 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
191 /* initialize it if it didn't already exist */
192 desktop
->flags
= flags
;
193 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
194 desktop
->top_window
= NULL
;
195 desktop
->global_hooks
= NULL
;
196 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
203 static void desktop_dump( struct object
*obj
, int verbose
)
205 struct desktop
*desktop
= (struct desktop
*)obj
;
207 fprintf( stderr
, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
208 desktop
->flags
, desktop
->winstation
, desktop
->top_window
, desktop
->global_hooks
);
209 dump_object_name( &desktop
->obj
);
210 fputc( '\n', stderr
);
213 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
215 struct thread
*thread
;
217 /* check if the handle is currently used by the process or one of its threads */
218 if (process
->desktop
== handle
) return 0;
219 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
220 if (thread
->desktop
== handle
) return 0;
224 static void desktop_destroy( struct object
*obj
)
226 struct desktop
*desktop
= (struct desktop
*)obj
;
228 if (desktop
->top_window
) destroy_window( desktop
->top_window
);
229 if (desktop
->global_hooks
) release_object( desktop
->global_hooks
);
230 list_remove( &desktop
->entry
);
231 release_object( desktop
->winstation
);
234 /* retrieve the thread desktop, checking the handle access rights */
235 struct desktop
*get_thread_desktop( struct thread
*thread
, unsigned int access
)
237 return get_desktop_obj( thread
->process
, thread
->desktop
, access
);
240 /* connect a process to its window station */
241 void connect_process_winstation( struct process
*process
, const struct unicode_str
*name
)
243 struct winstation
*winstation
;
245 if (process
->winstation
) return; /* already has one */
247 /* check for an inherited winstation handle (don't ask...) */
248 if ((process
->winstation
= find_inherited_handle( process
, &winstation_ops
)) != 0) return;
252 winstation
= create_winstation( name
, OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
, 0 );
256 if (!interactive_winstation
)
258 static const WCHAR winsta0W
[] = {'W','i','n','S','t','a','0'};
259 static const struct unicode_str winsta0
= { winsta0W
, sizeof(winsta0W
) };
260 interactive_winstation
= create_winstation( &winsta0
, OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
, 0 );
261 winstation
= interactive_winstation
;
263 else winstation
= (struct winstation
*)grab_object( interactive_winstation
);
267 process
->winstation
= alloc_handle( process
, winstation
, WINSTA_ALL_ACCESS
, FALSE
);
268 release_object( winstation
);
270 clear_error(); /* ignore errors */
273 /* connect a process to its main desktop */
274 void connect_process_desktop( struct process
*process
, const struct unicode_str
*name
)
276 struct desktop
*desktop
;
277 struct winstation
*winstation
;
279 if (process
->desktop
) return; /* already has one */
281 if ((winstation
= get_process_winstation( process
, WINSTA_CREATEDESKTOP
)))
283 static const WCHAR defaultW
[] = {'D','e','f','a','u','l','t'};
284 static const struct unicode_str default_str
= { defaultW
, sizeof(defaultW
) };
286 if (!name
) name
= &default_str
;
287 if ((desktop
= create_desktop( name
, OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
, 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
;
312 struct unicode_str name
;
315 get_req_unicode_str( &name
);
316 if ((winstation
= create_winstation( &name
, req
->attributes
, req
->flags
)))
318 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
,
319 req
->attributes
& OBJ_INHERIT
);
320 release_object( winstation
);
324 /* open a handle to a window station */
325 DECL_HANDLER(open_winstation
)
327 struct unicode_str name
;
329 get_req_unicode_str( &name
);
330 if (winstation_namespace
)
331 reply
->handle
= open_object( winstation_namespace
, &name
, &winstation_ops
, req
->access
,
334 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
338 /* close a window station */
339 DECL_HANDLER(close_winstation
)
341 struct winstation
*winstation
;
343 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
344 0, &winstation_ops
)))
346 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_ACCESS_DENIED
);
347 release_object( winstation
);
352 /* get the process current window station */
353 DECL_HANDLER(get_process_winstation
)
355 reply
->handle
= current
->process
->winstation
;
359 /* set the process current window station */
360 DECL_HANDLER(set_process_winstation
)
362 struct winstation
*winstation
;
364 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
365 0, &winstation_ops
)))
367 /* FIXME: should we close the old one? */
368 current
->process
->winstation
= req
->handle
;
369 release_object( winstation
);
373 /* create a desktop */
374 DECL_HANDLER(create_desktop
)
376 struct desktop
*desktop
;
377 struct winstation
*winstation
;
378 struct unicode_str name
;
381 get_req_unicode_str( &name
);
382 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
384 if ((desktop
= create_desktop( &name
, req
->attributes
, req
->flags
, winstation
)))
386 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
,
387 req
->attributes
& OBJ_INHERIT
);
388 release_object( desktop
);
390 release_object( winstation
);
394 /* open a handle to a desktop */
395 DECL_HANDLER(open_desktop
)
397 struct winstation
*winstation
;
398 struct unicode_str name
;
400 get_req_unicode_str( &name
);
401 if ((winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
403 struct unicode_str full_str
;
406 if ((full_name
= build_desktop_name( &name
, winstation
, &full_str
)))
408 reply
->handle
= open_object( winstation_namespace
, &full_str
, &desktop_ops
, req
->access
,
412 release_object( winstation
);
417 /* close a desktop */
418 DECL_HANDLER(close_desktop
)
420 struct desktop
*desktop
;
422 /* make sure it is a desktop handle */
423 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
426 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_DEVICE_BUSY
);
427 release_object( desktop
);
432 /* get the thread current desktop */
433 DECL_HANDLER(get_thread_desktop
)
435 struct thread
*thread
;
437 if (!(thread
= get_thread_from_id( req
->tid
))) return;
438 reply
->handle
= thread
->desktop
;
439 release_object( thread
);
443 /* set the thread current desktop */
444 DECL_HANDLER(set_thread_desktop
)
446 struct desktop
*old_desktop
, *new_desktop
;
447 struct winstation
*winstation
;
449 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
452 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
454 release_object( winstation
);
457 if (new_desktop
->winstation
!= winstation
)
459 set_error( STATUS_ACCESS_DENIED
);
460 release_object( new_desktop
);
461 release_object( winstation
);
465 /* check if we are changing to a new desktop */
467 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
468 clear_error(); /* ignore error */
470 /* when changing desktop, we can't have any users on the current one */
471 if (old_desktop
!= new_desktop
&& current
->desktop_users
> 0)
472 set_error( STATUS_DEVICE_BUSY
);
474 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
476 if (old_desktop
!= new_desktop
) detach_thread_input( current
);
478 if (old_desktop
) release_object( old_desktop
);
479 release_object( new_desktop
);
480 release_object( winstation
);
484 /* get/set information about a user object (window station or desktop) */
485 DECL_HANDLER(set_user_object_info
)
489 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
491 if (obj
->ops
== &desktop_ops
)
493 struct desktop
*desktop
= (struct desktop
*)obj
;
494 reply
->is_desktop
= 1;
495 reply
->old_obj_flags
= desktop
->flags
;
496 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
498 else if (obj
->ops
== &winstation_ops
)
500 struct winstation
*winstation
= (struct winstation
*)obj
;
501 reply
->is_desktop
= 0;
502 reply
->old_obj_flags
= winstation
->flags
;
503 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
507 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
508 release_object( obj
);
511 if (get_reply_max_size())
514 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
516 /* if there is a backslash return the part of the name after it */
517 if (name
&& (ptr
= memchrW( name
, '\\', len
)))
519 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
522 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
524 release_object( obj
);