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"
36 #include "wine/unicode.h"
40 struct object obj
; /* object header */
41 unsigned int flags
; /* winstation flags */
42 struct list entry
; /* entry in global winstation list */
43 struct list desktops
; /* list of desktops of this winstation */
44 struct clipboard
*clipboard
; /* clipboard information */
49 struct object obj
; /* object header */
50 unsigned int flags
; /* desktop flags */
51 struct winstation
*winstation
; /* winstation this desktop belongs to */
52 struct list entry
; /* entry in winstation list of desktops */
55 static struct list winstation_list
= LIST_INIT(winstation_list
);
56 static struct winstation
*interactive_winstation
;
57 static struct namespace *winstation_namespace
;
59 static void winstation_dump( struct object
*obj
, int verbose
);
60 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
61 static void winstation_destroy( struct object
*obj
);
62 static void desktop_dump( struct object
*obj
, int verbose
);
63 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
64 static void desktop_destroy( struct object
*obj
);
66 static const struct object_ops winstation_ops
=
68 sizeof(struct winstation
), /* size */
69 winstation_dump
, /* dump */
70 no_add_queue
, /* add_queue */
71 NULL
, /* remove_queue */
74 no_signal
, /* signal */
75 no_get_fd
, /* get_fd */
76 winstation_close_handle
, /* close_handle */
77 winstation_destroy
/* destroy */
81 static const struct object_ops desktop_ops
=
83 sizeof(struct desktop
), /* size */
84 desktop_dump
, /* dump */
85 no_add_queue
, /* add_queue */
86 NULL
, /* remove_queue */
89 no_signal
, /* signal */
90 no_get_fd
, /* get_fd */
91 desktop_close_handle
, /* close_handle */
92 desktop_destroy
/* destroy */
95 #define DESKTOP_ALL_ACCESS 0x01ff
97 /* create a winstation object */
98 static struct winstation
*create_winstation( const WCHAR
*name
, size_t len
, unsigned int flags
)
100 struct winstation
*winstation
;
102 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7, FALSE
)))
105 if (memchrW( 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
, len
)))
113 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
115 /* initialize it if it didn't already exist */
116 winstation
->flags
= flags
;
117 winstation
->clipboard
= NULL
;
118 list_add_tail( &winstation_list
, &winstation
->entry
);
119 list_init( &winstation
->desktops
);
125 static void winstation_dump( struct object
*obj
, int verbose
)
127 struct winstation
*winstation
= (struct winstation
*)obj
;
129 fprintf( stderr
, "Winstation flags=%x ", winstation
->flags
);
130 dump_object_name( &winstation
->obj
);
131 fputc( '\n', stderr
);
134 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
136 return (process
->winstation
!= handle
);
139 static void winstation_destroy( struct object
*obj
)
141 struct winstation
*winstation
= (struct winstation
*)obj
;
143 if (winstation
== interactive_winstation
) interactive_winstation
= NULL
;
144 list_remove( &winstation
->entry
);
145 if (winstation
->clipboard
) release_object( winstation
->clipboard
);
148 /* retrieve the process window station, checking the handle access rights */
149 struct winstation
*get_process_winstation( struct process
*process
, unsigned int access
)
151 return (struct winstation
*)get_handle_obj( process
, process
->winstation
,
152 access
, &winstation_ops
);
155 /* set the pointer to the (opaque) clipboard info */
156 void set_winstation_clipboard( struct winstation
*winstation
, struct clipboard
*clipboard
)
158 winstation
->clipboard
= clipboard
;
161 /* retrieve the pointer to the (opaque) clipboard info */
162 struct clipboard
*get_winstation_clipboard( struct winstation
*winstation
)
164 return winstation
->clipboard
;
167 /* build the full name of a desktop object */
168 static WCHAR
*build_desktop_name( const WCHAR
*name
, size_t len
,
169 struct winstation
*winstation
, size_t *res_len
)
171 const WCHAR
*winstation_name
;
173 size_t winstation_len
;
175 if (memchrW( name
, '\\', len
/ sizeof(WCHAR
) ))
177 set_error( STATUS_INVALID_PARAMETER
);
181 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
184 *res_len
= winstation_len
+ len
+ sizeof(WCHAR
);
185 if (!(full_name
= mem_alloc( *res_len
))) return NULL
;
186 memcpy( full_name
, winstation_name
, winstation_len
);
187 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
188 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
, len
);
192 /* retrieve a pointer to a desktop object */
193 inline static struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
,
194 unsigned int access
)
196 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
199 /* create a desktop object */
200 static struct desktop
*create_desktop( const WCHAR
*name
, size_t len
, unsigned int flags
,
201 struct winstation
*winstation
)
203 struct desktop
*desktop
;
207 if (!(full_name
= build_desktop_name( name
, len
, winstation
, &full_len
))) return NULL
;
209 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, full_name
, full_len
)))
211 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
213 /* initialize it if it didn't already exist */
214 desktop
->flags
= flags
;
215 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
216 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
223 static void desktop_dump( struct object
*obj
, int verbose
)
225 struct desktop
*desktop
= (struct desktop
*)obj
;
227 fprintf( stderr
, "Desktop flags=%x winstation=%p ", desktop
->flags
, desktop
->winstation
);
228 dump_object_name( &desktop
->obj
);
229 fputc( '\n', stderr
);
232 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
234 struct thread
*thread
;
236 /* check if the handle is currently used by the process or one of its threads */
237 if (process
->desktop
== handle
) return 0;
238 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
239 if (thread
->desktop
== handle
) return 0;
243 static void desktop_destroy( struct object
*obj
)
245 struct desktop
*desktop
= (struct desktop
*)obj
;
247 list_remove( &desktop
->entry
);
248 release_object( desktop
->winstation
);
251 /* connect a process to its window station */
252 void connect_process_winstation( struct process
*process
, const WCHAR
*name
, size_t len
)
254 struct winstation
*winstation
;
256 if (process
->winstation
) return; /* already has one */
258 /* check for an inherited winstation handle (don't ask...) */
259 if ((process
->winstation
= find_inherited_handle( process
, &winstation_ops
)) != 0) return;
263 winstation
= create_winstation( name
, len
, 0 );
267 if (!interactive_winstation
)
269 static const WCHAR winsta0W
[] = {'W','i','n','S','t','a','0'};
270 interactive_winstation
= create_winstation( winsta0W
, sizeof(winsta0W
), 0 );
271 winstation
= interactive_winstation
;
273 else winstation
= (struct winstation
*)grab_object( interactive_winstation
);
277 process
->winstation
= alloc_handle( process
, winstation
, WINSTA_ALL_ACCESS
, FALSE
);
278 release_object( winstation
);
280 clear_error(); /* ignore errors */
283 /* connect a process to its main desktop */
284 void connect_process_desktop( struct process
*process
, const WCHAR
*name
, size_t len
)
286 struct desktop
*desktop
;
287 struct winstation
*winstation
;
289 if (process
->desktop
) return; /* already has one */
291 if ((winstation
= get_process_winstation( process
, WINSTA_CREATEDESKTOP
)))
293 static const WCHAR defaultW
[] = {'D','e','f','a','u','l','t'};
298 len
= sizeof(defaultW
);
300 if ((desktop
= create_desktop( name
, len
, 0, winstation
)))
302 process
->desktop
= alloc_handle( process
, desktop
, DESKTOP_ALL_ACCESS
, FALSE
);
303 release_object( desktop
);
305 release_object( winstation
);
307 clear_error(); /* ignore errors */
310 /* close the desktop of a given thread */
311 void close_thread_desktop( struct thread
*thread
)
313 obj_handle_t handle
= thread
->desktop
;
316 if (handle
) close_handle( thread
->process
, handle
, NULL
);
317 clear_error(); /* ignore errors */
321 /* create a window station */
322 DECL_HANDLER(create_winstation
)
324 struct winstation
*winstation
;
327 if ((winstation
= create_winstation( get_req_data(), get_req_data_size(), req
->flags
)))
329 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->inherit
);
330 release_object( winstation
);
334 /* open a handle to a window station */
335 DECL_HANDLER(open_winstation
)
337 if (winstation_namespace
)
338 reply
->handle
= open_object( winstation_namespace
, get_req_data(), get_req_data_size(),
339 &winstation_ops
, req
->access
, req
->inherit
);
341 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
345 /* close a window station */
346 DECL_HANDLER(close_winstation
)
348 struct winstation
*winstation
;
350 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
351 0, &winstation_ops
)))
353 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_ACCESS_DENIED
);
354 release_object( winstation
);
359 /* get the process current window station */
360 DECL_HANDLER(get_process_winstation
)
362 reply
->handle
= current
->process
->winstation
;
366 /* set the process current window station */
367 DECL_HANDLER(set_process_winstation
)
369 struct winstation
*winstation
;
371 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
372 0, &winstation_ops
)))
374 /* FIXME: should we close the old one? */
375 current
->process
->winstation
= req
->handle
;
376 release_object( winstation
);
380 /* create a desktop */
381 DECL_HANDLER(create_desktop
)
383 struct desktop
*desktop
;
384 struct winstation
*winstation
;
387 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
389 if ((desktop
= create_desktop( get_req_data(), get_req_data_size(),
390 req
->flags
, winstation
)))
392 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->inherit
);
393 release_object( desktop
);
395 release_object( winstation
);
399 /* open a handle to a desktop */
400 DECL_HANDLER(open_desktop
)
402 struct winstation
*winstation
;
404 if ((winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
409 if ((full_name
= build_desktop_name( get_req_data(), get_req_data_size(),
410 winstation
, &full_len
)))
412 reply
->handle
= open_object( winstation_namespace
, full_name
, full_len
,
413 &desktop_ops
, req
->access
, req
->inherit
);
416 release_object( winstation
);
421 /* close a desktop */
422 DECL_HANDLER(close_desktop
)
424 struct desktop
*desktop
;
426 /* make sure it is a desktop handle */
427 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
430 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_DEVICE_BUSY
);
431 release_object( desktop
);
436 /* get the thread current desktop */
437 DECL_HANDLER(get_thread_desktop
)
439 struct thread
*thread
;
441 if (!(thread
= get_thread_from_id( req
->tid
))) return;
442 reply
->handle
= thread
->desktop
;
443 release_object( thread
);
447 /* set the thread current desktop */
448 DECL_HANDLER(set_thread_desktop
)
450 struct desktop
*old_desktop
, *new_desktop
;
451 struct winstation
*winstation
;
453 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
456 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
458 release_object( winstation
);
461 if (new_desktop
->winstation
!= winstation
)
463 set_error( STATUS_ACCESS_DENIED
);
464 release_object( new_desktop
);
465 release_object( winstation
);
469 /* check if we are changing to a new desktop */
471 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
472 clear_error(); /* ignore error */
474 /* when changing desktop, we can't have any users on the current one */
475 if (old_desktop
!= new_desktop
&& current
->desktop_users
> 0)
476 set_error( STATUS_DEVICE_BUSY
);
478 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
480 if (old_desktop
) release_object( old_desktop
);
481 release_object( new_desktop
);
482 release_object( winstation
);
486 /* get/set information about a user object (window station or desktop) */
487 DECL_HANDLER(set_user_object_info
)
491 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
493 if (obj
->ops
== &desktop_ops
)
495 struct desktop
*desktop
= (struct desktop
*)obj
;
496 reply
->is_desktop
= 1;
497 reply
->old_obj_flags
= desktop
->flags
;
498 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
500 else if (obj
->ops
== &winstation_ops
)
502 struct winstation
*winstation
= (struct winstation
*)obj
;
503 reply
->is_desktop
= 0;
504 reply
->old_obj_flags
= winstation
->flags
;
505 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
509 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
510 release_object( obj
);
513 if (get_reply_max_size())
516 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
518 /* if there is a backslash return the part of the name after it */
519 if (name
&& (ptr
= memchrW( name
, '\\', len
)))
521 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
524 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
526 release_object( obj
);