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 + 1) / sizeof(WCHAR), 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 /* close a desktop handle if allowed */
211 static void close_desktop_handle( struct process *process, obj_handle_t handle )
213 struct thread *thread;
215 /* make sure it's not in use by any thread in the process */
216 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
218 if (thread->desktop == handle)
220 set_error( STATUS_DEVICE_BUSY );
224 close_handle( process, handle, NULL );
227 /* connect a process to its window station */
228 void connect_process_winstation( struct process *process, const WCHAR *name, size_t len )
230 struct winstation *winstation;
232 if (process->winstation) return; /* already has one */
234 /* check for an inherited winstation handle (don't ask...) */
235 if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
239 winstation = create_winstation( name, len, 0 );
243 if (!interactive_winstation)
245 static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
246 interactive_winstation = create_winstation( winsta0W, sizeof(winsta0W), 0 );
247 winstation = interactive_winstation;
249 else winstation = (struct winstation *)grab_object( interactive_winstation );
253 if ((process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE )))
256 /* FIXME: Windows doesn't do it this way */
257 set_handle_info( process, process->winstation, HANDLE_FLAG_PROTECT_FROM_CLOSE,
258 HANDLE_FLAG_PROTECT_FROM_CLOSE, &fd );
260 release_object( winstation );
262 clear_error(); /* ignore errors */
265 /* connect a thread to its desktop */
266 void connect_thread_desktop( struct thread *thread, const WCHAR *name, size_t len )
268 struct desktop *desktop;
269 struct winstation *winstation;
270 struct process *process = thread->process;
272 if (thread->desktop) return; /* already has one */
274 if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
276 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
281 len = sizeof(defaultW);
283 if ((desktop = create_desktop( name, len, 0, winstation )))
285 thread->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
286 release_object( desktop );
288 release_object( winstation );
290 clear_error(); /* ignore errors */
293 /* close the desktop of a given thread */
294 void close_thread_desktop( struct thread *thread )
296 obj_handle_t handle = thread->desktop;
299 if (handle) close_desktop_handle( thread->process, handle );
300 clear_error(); /* ignore errors */
304 /* create a window station */
305 DECL_HANDLER(create_winstation)
307 struct winstation *winstation;
310 if ((winstation = create_winstation( get_req_data(), get_req_data_size(), req->flags )))
312 reply->handle = alloc_handle( current->process, winstation, req->access, req->inherit );
313 release_object( winstation );
317 /* open a handle to a window station */
318 DECL_HANDLER(open_winstation)
320 if (winstation_namespace)
321 reply->handle = open_object( winstation_namespace, get_req_data(), get_req_data_size(),
322 &winstation_ops, req->access, req->inherit );
324 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
328 /* close a window station */
329 DECL_HANDLER(close_winstation)
331 struct winstation *winstation;
333 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
334 0, &winstation_ops )))
336 if (req->handle != current->process->winstation)
337 close_handle( current->process, req->handle, NULL );
339 set_error( STATUS_ACCESS_DENIED );
340 release_object( winstation );
345 /* get the process current window station */
346 DECL_HANDLER(get_process_winstation)
348 reply->handle = current->process->winstation;
352 /* set the process current window station */
353 DECL_HANDLER(set_process_winstation)
355 struct winstation *winstation;
357 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
358 0, &winstation_ops )))
360 /* FIXME: should we close the old one? */
361 current->process->winstation = req->handle;
362 release_object( winstation );
366 /* create a desktop */
367 DECL_HANDLER(create_desktop)
369 struct desktop *desktop;
370 struct winstation *winstation;
373 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
375 if ((desktop = create_desktop( get_req_data(), get_req_data_size(),
376 req->flags, winstation )))
378 reply->handle = alloc_handle( current->process, desktop, req->access, req->inherit );
379 release_object( desktop );
381 release_object( winstation );
385 /* open a handle to a desktop */
386 DECL_HANDLER(open_desktop)
388 struct winstation *winstation;
390 if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
395 if ((full_name = build_desktop_name( get_req_data(), get_req_data_size(),
396 winstation, &full_len )))
398 reply->handle = open_object( winstation_namespace, full_name, full_len,
399 &desktop_ops, req->access, req->inherit );
402 release_object( winstation );
407 /* close a desktop */
408 DECL_HANDLER(close_desktop)
410 struct desktop *desktop;
412 /* make sure it is a desktop handle */
413 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
416 close_desktop_handle( current->process, req->handle );
417 release_object( desktop );
422 /* get the thread current desktop */
423 DECL_HANDLER(get_thread_desktop)
425 struct thread *thread;
427 if (!(thread = get_thread_from_id( req->tid ))) return;
428 reply->handle = thread->desktop;
429 release_object( thread );
433 /* set the thread current desktop */
434 DECL_HANDLER(set_thread_desktop)
436 struct desktop *desktop;
438 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle, 0, &desktop_ops )))
440 /* FIXME: should we close the old one? */
441 current->desktop = req->handle;
442 release_object( desktop );
447 /* get/set information about a user object (window station or desktop) */
448 DECL_HANDLER(set_user_object_info)
452 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
454 if (obj->ops == &desktop_ops)
456 struct desktop *desktop = (struct desktop *)obj;
457 reply->is_desktop = 1;
458 reply->old_obj_flags = desktop->flags;
459 if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
461 else if (obj->ops == &winstation_ops)
463 struct winstation *winstation = (struct winstation *)obj;
464 reply->is_desktop = 0;
465 reply->old_obj_flags = winstation->flags;
466 if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
470 set_error( STATUS_OBJECT_TYPE_MISMATCH );
471 release_object( obj );
474 if (get_reply_max_size())
477 const WCHAR *ptr, *name = get_object_name( obj, &len );
479 /* if there is a backslash return the part of the name after it */
480 if (name && (ptr = memchrW( name, '\\', len )))
483 len -= (ptr - name) * sizeof(WCHAR);
485 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
487 release_object( obj );