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 int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
60 static void winstation_destroy( struct object *obj );
61 static void desktop_dump( struct object *obj, int verbose );
62 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
63 static void desktop_destroy( struct object *obj );
65 static const struct object_ops winstation_ops =
67 sizeof(struct winstation), /* size */
68 winstation_dump, /* dump */
69 no_add_queue, /* add_queue */
70 NULL, /* remove_queue */
73 no_signal, /* signal */
74 no_get_fd, /* get_fd */
75 winstation_close_handle, /* close_handle */
76 winstation_destroy /* destroy */
80 static const struct object_ops desktop_ops =
82 sizeof(struct desktop), /* size */
83 desktop_dump, /* dump */
84 no_add_queue, /* add_queue */
85 NULL, /* remove_queue */
88 no_signal, /* signal */
89 no_get_fd, /* get_fd */
90 desktop_close_handle, /* close_handle */
91 desktop_destroy /* destroy */
94 #define DESKTOP_ALL_ACCESS 0x01ff
96 /* create a winstation object */
97 static struct winstation *create_winstation( const WCHAR *name, size_t len, unsigned int flags )
99 struct winstation *winstation;
101 if (!winstation_namespace && !(winstation_namespace = create_namespace( 7, FALSE )))
104 if (memchrW( name, '\\', len / sizeof(WCHAR) )) /* no backslash allowed in name */
106 set_error( STATUS_INVALID_PARAMETER );
110 if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, len )))
112 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
114 /* initialize it if it didn't already exist */
115 winstation->flags = flags;
116 list_add_tail( &winstation_list, &winstation->entry );
117 list_init( &winstation->desktops );
123 static void winstation_dump( struct object *obj, int verbose )
125 struct winstation *winstation = (struct winstation *)obj;
127 fprintf( stderr, "Winstation flags=%x ", winstation->flags );
128 dump_object_name( &winstation->obj );
129 fputc( '\n', stderr );
132 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
134 return (process->winstation != handle);
137 static void winstation_destroy( struct object *obj )
139 struct winstation *winstation = (struct winstation *)obj;
141 if (winstation == interactive_winstation) interactive_winstation = NULL;
142 list_remove( &winstation->entry );
145 /* retrieve the process window station, checking the handle access rights */
146 inline static struct winstation *get_process_winstation( struct process *process,
147 unsigned int access )
149 return (struct winstation *)get_handle_obj( process, process->winstation,
150 access, &winstation_ops );
153 /* build the full name of a desktop object */
154 static WCHAR *build_desktop_name( const WCHAR *name, size_t len,
155 struct winstation *winstation, size_t *res_len )
157 const WCHAR *winstation_name;
159 size_t winstation_len;
161 if (memchrW( name, '\\', len / sizeof(WCHAR) ))
163 set_error( STATUS_INVALID_PARAMETER );
167 if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
170 *res_len = winstation_len + len + sizeof(WCHAR);
171 if (!(full_name = mem_alloc( *res_len ))) return NULL;
172 memcpy( full_name, winstation_name, winstation_len );
173 full_name[winstation_len / sizeof(WCHAR)] = '\\';
174 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name, len );
178 /* create a desktop object */
179 static struct desktop *create_desktop( const WCHAR *name, size_t len, unsigned int flags,
180 struct winstation *winstation )
182 struct desktop *desktop;
186 if (!(full_name = build_desktop_name( name, len, winstation, &full_len ))) return NULL;
188 if ((desktop = create_named_object( winstation_namespace, &desktop_ops, full_name, full_len )))
190 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
192 /* initialize it if it didn't already exist */
193 desktop->flags = flags;
194 desktop->winstation = (struct winstation *)grab_object( winstation );
195 list_add_tail( &winstation->desktops, &desktop->entry );
202 static void desktop_dump( struct object *obj, int verbose )
204 struct desktop *desktop = (struct desktop *)obj;
206 fprintf( stderr, "Desktop flags=%x winstation=%p ", desktop->flags, desktop->winstation );
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 list_remove( &desktop->entry );
227 release_object( desktop->winstation );
230 /* connect a process to its window station */
231 void connect_process_winstation( struct process *process, const WCHAR *name, size_t len )
233 struct winstation *winstation;
235 if (process->winstation) return; /* already has one */
237 /* check for an inherited winstation handle (don't ask...) */
238 if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
242 winstation = create_winstation( name, len, 0 );
246 if (!interactive_winstation)
248 static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
249 interactive_winstation = create_winstation( winsta0W, sizeof(winsta0W), 0 );
250 winstation = interactive_winstation;
252 else winstation = (struct winstation *)grab_object( interactive_winstation );
256 process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE );
257 release_object( winstation );
259 clear_error(); /* ignore errors */
262 /* connect a process to its main desktop */
263 void connect_process_desktop( struct process *process, const WCHAR *name, size_t len )
265 struct desktop *desktop;
266 struct winstation *winstation;
268 if (process->desktop) return; /* already has one */
270 if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
272 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
277 len = sizeof(defaultW);
279 if ((desktop = create_desktop( name, len, 0, winstation )))
281 process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
282 release_object( desktop );
284 release_object( winstation );
286 clear_error(); /* ignore errors */
289 /* close the desktop of a given thread */
290 void close_thread_desktop( struct thread *thread )
292 obj_handle_t handle = thread->desktop;
295 if (handle) close_handle( thread->process, handle, NULL );
296 clear_error(); /* ignore errors */
300 /* create a window station */
301 DECL_HANDLER(create_winstation)
303 struct winstation *winstation;
306 if ((winstation = create_winstation( get_req_data(), get_req_data_size(), req->flags )))
308 reply->handle = alloc_handle( current->process, winstation, req->access, req->inherit );
309 release_object( winstation );
313 /* open a handle to a window station */
314 DECL_HANDLER(open_winstation)
316 if (winstation_namespace)
317 reply->handle = open_object( winstation_namespace, get_req_data(), get_req_data_size(),
318 &winstation_ops, req->access, req->inherit );
320 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
324 /* close a window station */
325 DECL_HANDLER(close_winstation)
327 struct winstation *winstation;
329 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
330 0, &winstation_ops )))
332 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_ACCESS_DENIED );
333 release_object( winstation );
338 /* get the process current window station */
339 DECL_HANDLER(get_process_winstation)
341 reply->handle = current->process->winstation;
345 /* set the process current window station */
346 DECL_HANDLER(set_process_winstation)
348 struct winstation *winstation;
350 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
351 0, &winstation_ops )))
353 /* FIXME: should we close the old one? */
354 current->process->winstation = req->handle;
355 release_object( winstation );
359 /* create a desktop */
360 DECL_HANDLER(create_desktop)
362 struct desktop *desktop;
363 struct winstation *winstation;
366 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
368 if ((desktop = create_desktop( get_req_data(), get_req_data_size(),
369 req->flags, winstation )))
371 reply->handle = alloc_handle( current->process, desktop, req->access, req->inherit );
372 release_object( desktop );
374 release_object( winstation );
378 /* open a handle to a desktop */
379 DECL_HANDLER(open_desktop)
381 struct winstation *winstation;
383 if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
388 if ((full_name = build_desktop_name( get_req_data(), get_req_data_size(),
389 winstation, &full_len )))
391 reply->handle = open_object( winstation_namespace, full_name, full_len,
392 &desktop_ops, req->access, req->inherit );
395 release_object( winstation );
400 /* close a desktop */
401 DECL_HANDLER(close_desktop)
403 struct desktop *desktop;
405 /* make sure it is a desktop handle */
406 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
409 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
410 release_object( desktop );
415 /* get the thread current desktop */
416 DECL_HANDLER(get_thread_desktop)
418 struct thread *thread;
420 if (!(thread = get_thread_from_id( req->tid ))) return;
421 reply->handle = thread->desktop;
422 release_object( thread );
426 /* set the thread current desktop */
427 DECL_HANDLER(set_thread_desktop)
429 struct desktop *desktop;
431 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle, 0, &desktop_ops )))
433 /* FIXME: should we close the old one? */
434 current->desktop = req->handle;
435 release_object( desktop );
440 /* get/set information about a user object (window station or desktop) */
441 DECL_HANDLER(set_user_object_info)
445 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
447 if (obj->ops == &desktop_ops)
449 struct desktop *desktop = (struct desktop *)obj;
450 reply->is_desktop = 1;
451 reply->old_obj_flags = desktop->flags;
452 if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
454 else if (obj->ops == &winstation_ops)
456 struct winstation *winstation = (struct winstation *)obj;
457 reply->is_desktop = 0;
458 reply->old_obj_flags = winstation->flags;
459 if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
463 set_error( STATUS_OBJECT_TYPE_MISMATCH );
464 release_object( obj );
467 if (get_reply_max_size())
470 const WCHAR *ptr, *name = get_object_name( obj, &len );
472 /* if there is a backslash return the part of the name after it */
473 if (name && (ptr = memchrW( name, '\\', len )))
475 len -= (ptr + 1 - name) * sizeof(WCHAR);
478 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
480 release_object( obj );