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 */
48 struct object obj
; /* object header */
49 unsigned int flags
; /* desktop flags */
50 struct winstation
*winstation
; /* winstation this desktop belongs to */
51 struct list entry
; /* entry in winstation list of desktops */
54 static struct list winstation_list
= LIST_INIT(winstation_list
);
55 static struct winstation
*interactive_winstation
;
56 static struct namespace *winstation_namespace
;
58 static void winstation_dump( struct object
*obj
, int verbose
);
59 static void winstation_destroy( struct object
*obj
);
60 static void desktop_dump( struct object
*obj
, int verbose
);
61 static void desktop_destroy( struct object
*obj
);
63 static const struct object_ops winstation_ops
=
65 sizeof(struct winstation
), /* size */
66 winstation_dump
, /* dump */
67 no_add_queue
, /* add_queue */
68 NULL
, /* remove_queue */
71 no_signal
, /* signal */
72 no_get_fd
, /* get_fd */
73 winstation_destroy
/* destroy */
77 static const struct object_ops desktop_ops
=
79 sizeof(struct desktop
), /* size */
80 desktop_dump
, /* dump */
81 no_add_queue
, /* add_queue */
82 NULL
, /* remove_queue */
85 no_signal
, /* signal */
86 no_get_fd
, /* get_fd */
87 desktop_destroy
/* destroy */
90 #define DESKTOP_ALL_ACCESS 0x01ff
92 /* create a winstation object */
93 static struct winstation
*create_winstation( const WCHAR
*name
, size_t len
, unsigned int flags
)
95 struct winstation
*winstation
;
97 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7, FALSE
)))
100 if (memchrW( name
, '\\', len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
102 set_error( STATUS_INVALID_PARAMETER
);
106 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, len
)))
108 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
110 /* initialize it if it didn't already exist */
111 winstation
->flags
= flags
;
112 list_add_tail( &winstation_list
, &winstation
->entry
);
113 list_init( &winstation
->desktops
);
119 static void winstation_dump( struct object
*obj
, int verbose
)
121 struct winstation
*winstation
= (struct winstation
*)obj
;
123 fprintf( stderr
, "Winstation flags=%x ", winstation
->flags
);
124 dump_object_name( &winstation
->obj
);
125 fputc( '\n', stderr
);
128 static void winstation_destroy( struct object
*obj
)
130 struct winstation
*winstation
= (struct winstation
*)obj
;
132 if (winstation
== interactive_winstation
) interactive_winstation
= NULL
;
133 list_remove( &winstation
->entry
);
136 /* retrieve the process window station, checking the handle access rights */
137 inline static struct winstation
*get_process_winstation( struct process
*process
,
138 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 WCHAR
*name
, size_t len
,
146 struct winstation
*winstation
, size_t *res_len
)
148 const WCHAR
*winstation_name
;
150 size_t winstation_len
;
152 if (memchrW( 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
+ 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
, len
);
169 /* create a desktop object */
170 static struct desktop
*create_desktop( const WCHAR
*name
, size_t len
, unsigned int flags
,
171 struct winstation
*winstation
)
173 struct desktop
*desktop
;
177 if (!(full_name
= build_desktop_name( name
, len
, winstation
, &full_len
))) return NULL
;
179 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, full_name
, full_len
)))
181 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
183 /* initialize it if it didn't already exist */
184 desktop
->flags
= flags
;
185 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
186 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
193 static void desktop_dump( struct object
*obj
, int verbose
)
195 struct desktop
*desktop
= (struct desktop
*)obj
;
197 fprintf( stderr
, "Desktop flags=%x winstation=%p ", desktop
->flags
, desktop
->winstation
);
198 dump_object_name( &desktop
->obj
);
199 fputc( '\n', stderr
);
202 static void desktop_destroy( struct object
*obj
)
204 struct desktop
*desktop
= (struct desktop
*)obj
;
206 list_remove( &desktop
->entry
);
207 release_object( desktop
->winstation
);
210 /* check if a desktop handle is currently used by the process */
211 static int is_desktop_in_use( struct process
*process
, obj_handle_t handle
)
213 struct thread
*thread
;
215 if (process
->desktop
== handle
) return 1;
217 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
218 if (thread
->desktop
== handle
) return 1;
223 /* connect a process to its window station */
224 void connect_process_winstation( struct process
*process
, const WCHAR
*name
, size_t len
)
226 struct winstation
*winstation
;
228 if (process
->winstation
) return; /* already has one */
230 /* check for an inherited winstation handle (don't ask...) */
231 if ((process
->winstation
= find_inherited_handle( process
, &winstation_ops
)) != 0) return;
235 winstation
= create_winstation( name
, len
, 0 );
239 if (!interactive_winstation
)
241 static const WCHAR winsta0W
[] = {'W','i','n','S','t','a','0'};
242 interactive_winstation
= create_winstation( winsta0W
, sizeof(winsta0W
), 0 );
243 winstation
= interactive_winstation
;
245 else winstation
= (struct winstation
*)grab_object( interactive_winstation
);
249 if ((process
->winstation
= alloc_handle( process
, winstation
, WINSTA_ALL_ACCESS
, FALSE
)))
252 /* FIXME: Windows doesn't do it this way */
253 set_handle_info( process
, process
->winstation
, HANDLE_FLAG_PROTECT_FROM_CLOSE
,
254 HANDLE_FLAG_PROTECT_FROM_CLOSE
, &fd
);
256 release_object( winstation
);
258 clear_error(); /* ignore errors */
261 /* connect a process to its main desktop */
262 void connect_process_desktop( struct process
*process
, const WCHAR
*name
, size_t len
)
264 struct desktop
*desktop
;
265 struct winstation
*winstation
;
267 if (process
->desktop
) return; /* already has one */
269 if ((winstation
= get_process_winstation( process
, WINSTA_CREATEDESKTOP
)))
271 static const WCHAR defaultW
[] = {'D','e','f','a','u','l','t'};
276 len
= sizeof(defaultW
);
278 if ((desktop
= create_desktop( name
, len
, 0, winstation
)))
280 process
->desktop
= alloc_handle( process
, desktop
, DESKTOP_ALL_ACCESS
, FALSE
);
281 release_object( desktop
);
283 release_object( winstation
);
285 clear_error(); /* ignore errors */
288 /* close the desktop of a given thread */
289 void close_thread_desktop( struct thread
*thread
)
291 obj_handle_t handle
= thread
->desktop
;
294 if (handle
&& !is_desktop_in_use( thread
->process
, handle
))
295 close_handle( thread
->process
, handle
, NULL
);
299 /* create a window station */
300 DECL_HANDLER(create_winstation
)
302 struct winstation
*winstation
;
305 if ((winstation
= create_winstation( get_req_data(), get_req_data_size(), req
->flags
)))
307 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->inherit
);
308 release_object( winstation
);
312 /* open a handle to a window station */
313 DECL_HANDLER(open_winstation
)
315 if (winstation_namespace
)
316 reply
->handle
= open_object( winstation_namespace
, get_req_data(), get_req_data_size(),
317 &winstation_ops
, req
->access
, req
->inherit
);
319 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
323 /* close a window station */
324 DECL_HANDLER(close_winstation
)
326 struct winstation
*winstation
;
328 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
329 0, &winstation_ops
)))
331 if (req
->handle
!= current
->process
->winstation
)
332 close_handle( current
->process
, req
->handle
, NULL
);
334 set_error( STATUS_ACCESS_DENIED
);
335 release_object( winstation
);
340 /* get the process current window station */
341 DECL_HANDLER(get_process_winstation
)
343 reply
->handle
= current
->process
->winstation
;
347 /* set the process current window station */
348 DECL_HANDLER(set_process_winstation
)
350 struct winstation
*winstation
;
352 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
353 0, &winstation_ops
)))
355 /* FIXME: should we close the old one? */
356 current
->process
->winstation
= req
->handle
;
357 release_object( winstation
);
361 /* create a desktop */
362 DECL_HANDLER(create_desktop
)
364 struct desktop
*desktop
;
365 struct winstation
*winstation
;
368 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
370 if ((desktop
= create_desktop( get_req_data(), get_req_data_size(),
371 req
->flags
, winstation
)))
373 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->inherit
);
374 release_object( desktop
);
376 release_object( winstation
);
380 /* open a handle to a desktop */
381 DECL_HANDLER(open_desktop
)
383 struct winstation
*winstation
;
385 if ((winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
390 if ((full_name
= build_desktop_name( get_req_data(), get_req_data_size(),
391 winstation
, &full_len
)))
393 reply
->handle
= open_object( winstation_namespace
, full_name
, full_len
,
394 &desktop_ops
, req
->access
, req
->inherit
);
397 release_object( winstation
);
402 /* close a desktop */
403 DECL_HANDLER(close_desktop
)
405 struct desktop
*desktop
;
407 /* make sure it is a desktop handle */
408 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
411 if (!is_desktop_in_use( current
->process
, req
->handle
))
412 close_handle( current
->process
, req
->handle
, NULL
);
414 set_error( STATUS_DEVICE_BUSY
);
415 release_object( desktop
);
420 /* get the thread current desktop */
421 DECL_HANDLER(get_thread_desktop
)
423 struct thread
*thread
;
425 if (!(thread
= get_thread_from_id( req
->tid
))) return;
426 reply
->handle
= thread
->desktop
;
427 release_object( thread
);
431 /* set the thread current desktop */
432 DECL_HANDLER(set_thread_desktop
)
434 struct desktop
*desktop
;
436 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
, 0, &desktop_ops
)))
438 /* FIXME: should we close the old one? */
439 current
->desktop
= req
->handle
;
440 release_object( desktop
);
445 /* get/set information about a user object (window station or desktop) */
446 DECL_HANDLER(set_user_object_info
)
450 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
452 if (obj
->ops
== &desktop_ops
)
454 struct desktop
*desktop
= (struct desktop
*)obj
;
455 reply
->is_desktop
= 1;
456 reply
->old_obj_flags
= desktop
->flags
;
457 if (req
->flags
& SET_USER_OBJECT_FLAGS
) desktop
->flags
= req
->obj_flags
;
459 else if (obj
->ops
== &winstation_ops
)
461 struct winstation
*winstation
= (struct winstation
*)obj
;
462 reply
->is_desktop
= 0;
463 reply
->old_obj_flags
= winstation
->flags
;
464 if (req
->flags
& SET_USER_OBJECT_FLAGS
) winstation
->flags
= req
->obj_flags
;
468 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
469 release_object( obj
);
472 if (get_reply_max_size())
475 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
477 /* if there is a backslash return the part of the name after it */
478 if (name
&& (ptr
= memchrW( name
, '\\', len
)))
480 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
483 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
485 release_object( obj
);