Added a struct unicode_str to encapsulate object names.
[wine/wine-kai.git] / server / winstation.c
blobe461afb3d569e5f49eba63a191bb61fc1de421b1
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winternl.h"
32 #include "object.h"
33 #include "handle.h"
34 #include "request.h"
35 #include "process.h"
36 #include "user.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 */
57 NULL, /* signaled */
58 NULL, /* satisfied */
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 */
72 NULL, /* signaled */
73 NULL, /* satisfied */
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 flags )
85 struct winstation *winstation;
87 if (!winstation_namespace && !(winstation_namespace = create_namespace( 7 )))
88 return NULL;
90 if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) )) /* no backslash allowed in name */
92 set_error( STATUS_INVALID_PARAMETER );
93 return NULL;
96 if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name,
97 OBJ_CASE_INSENSITIVE )))
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 );
109 return winstation;
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;
149 WCHAR *full_name;
150 size_t winstation_len;
152 if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))
154 set_error( STATUS_INVALID_PARAMETER );
155 return NULL;
158 if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
159 winstation_len = 0;
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;
167 return 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 flags,
179 struct winstation *winstation )
181 struct desktop *desktop;
182 struct unicode_str full_str;
183 WCHAR *full_name;
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,
188 OBJ_CASE_INSENSITIVE )))
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 desktop->top_window = NULL;
196 desktop->global_hooks = NULL;
197 list_add_tail( &winstation->desktops, &desktop->entry );
200 free( full_name );
201 return desktop;
204 static void desktop_dump( struct object *obj, int verbose )
206 struct desktop *desktop = (struct desktop *)obj;
208 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
209 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
210 dump_object_name( &desktop->obj );
211 fputc( '\n', stderr );
214 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
216 struct thread *thread;
218 /* check if the handle is currently used by the process or one of its threads */
219 if (process->desktop == handle) return 0;
220 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
221 if (thread->desktop == handle) return 0;
222 return 1;
225 static void desktop_destroy( struct object *obj )
227 struct desktop *desktop = (struct desktop *)obj;
229 if (desktop->top_window) destroy_window( desktop->top_window );
230 if (desktop->global_hooks) release_object( desktop->global_hooks );
231 list_remove( &desktop->entry );
232 release_object( desktop->winstation );
235 /* retrieve the thread desktop, checking the handle access rights */
236 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
238 return get_desktop_obj( thread->process, thread->desktop, access );
241 /* connect a process to its window station */
242 void connect_process_winstation( struct process *process, const struct unicode_str *name )
244 struct winstation *winstation;
246 if (process->winstation) return; /* already has one */
248 /* check for an inherited winstation handle (don't ask...) */
249 if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
251 if (name)
253 winstation = create_winstation( name, 0 );
255 else
257 if (!interactive_winstation)
259 static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
260 static const struct unicode_str winsta0 = { winsta0W, sizeof(winsta0W) };
261 interactive_winstation = create_winstation( &winsta0, 0 );
262 winstation = interactive_winstation;
264 else winstation = (struct winstation *)grab_object( interactive_winstation );
266 if (winstation)
268 process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE );
269 release_object( winstation );
271 clear_error(); /* ignore errors */
274 /* connect a process to its main desktop */
275 void connect_process_desktop( struct process *process, const struct unicode_str *name )
277 struct desktop *desktop;
278 struct winstation *winstation;
280 if (process->desktop) return; /* already has one */
282 if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
284 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
285 static const struct unicode_str default_str = { defaultW, sizeof(defaultW) };
287 if (!name) name = &default_str;
288 if ((desktop = create_desktop( name, 0, winstation )))
290 process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
291 release_object( desktop );
293 release_object( winstation );
295 clear_error(); /* ignore errors */
298 /* close the desktop of a given thread */
299 void close_thread_desktop( struct thread *thread )
301 obj_handle_t handle = thread->desktop;
303 thread->desktop = 0;
304 if (handle) close_handle( thread->process, handle, NULL );
305 clear_error(); /* ignore errors */
309 /* create a window station */
310 DECL_HANDLER(create_winstation)
312 struct winstation *winstation;
313 struct unicode_str name;
315 reply->handle = 0;
316 get_req_unicode_str( &name );
317 if ((winstation = create_winstation( &name, req->flags )))
319 reply->handle = alloc_handle( current->process, winstation, req->access, req->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,
332 OBJ_CASE_INSENSITIVE | (req->inherit ? OBJ_INHERIT:0) );
333 else
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;
380 reply->handle = 0;
381 get_req_unicode_str( &name );
382 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
384 if ((desktop = create_desktop( &name, req->flags, winstation )))
386 reply->handle = alloc_handle( current->process, desktop, req->access, req->inherit );
387 release_object( desktop );
389 release_object( winstation );
393 /* open a handle to a desktop */
394 DECL_HANDLER(open_desktop)
396 struct winstation *winstation;
397 struct unicode_str name;
399 get_req_unicode_str( &name );
400 if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
402 struct unicode_str full_str;
403 WCHAR *full_name;
405 if ((full_name = build_desktop_name( &name, winstation, &full_str )))
407 reply->handle = open_object( winstation_namespace, &full_str, &desktop_ops, req->access,
408 OBJ_CASE_INSENSITIVE | (req->inherit ? OBJ_INHERIT:0) );
409 free( full_name );
411 release_object( winstation );
416 /* close a desktop */
417 DECL_HANDLER(close_desktop)
419 struct desktop *desktop;
421 /* make sure it is a desktop handle */
422 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
423 0, &desktop_ops )))
425 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
426 release_object( desktop );
431 /* get the thread current desktop */
432 DECL_HANDLER(get_thread_desktop)
434 struct thread *thread;
436 if (!(thread = get_thread_from_id( req->tid ))) return;
437 reply->handle = thread->desktop;
438 release_object( thread );
442 /* set the thread current desktop */
443 DECL_HANDLER(set_thread_desktop)
445 struct desktop *old_desktop, *new_desktop;
446 struct winstation *winstation;
448 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
449 return;
451 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
453 release_object( winstation );
454 return;
456 if (new_desktop->winstation != winstation)
458 set_error( STATUS_ACCESS_DENIED );
459 release_object( new_desktop );
460 release_object( winstation );
461 return;
464 /* check if we are changing to a new desktop */
466 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
467 clear_error(); /* ignore error */
469 /* when changing desktop, we can't have any users on the current one */
470 if (old_desktop != new_desktop && current->desktop_users > 0)
471 set_error( STATUS_DEVICE_BUSY );
472 else
473 current->desktop = req->handle; /* FIXME: should we close the old one? */
475 if (old_desktop != new_desktop) detach_thread_input( current );
477 if (old_desktop) release_object( old_desktop );
478 release_object( new_desktop );
479 release_object( winstation );
483 /* get/set information about a user object (window station or desktop) */
484 DECL_HANDLER(set_user_object_info)
486 struct object *obj;
488 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
490 if (obj->ops == &desktop_ops)
492 struct desktop *desktop = (struct desktop *)obj;
493 reply->is_desktop = 1;
494 reply->old_obj_flags = desktop->flags;
495 if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
497 else if (obj->ops == &winstation_ops)
499 struct winstation *winstation = (struct winstation *)obj;
500 reply->is_desktop = 0;
501 reply->old_obj_flags = winstation->flags;
502 if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
504 else
506 set_error( STATUS_OBJECT_TYPE_MISMATCH );
507 release_object( obj );
508 return;
510 if (get_reply_max_size())
512 size_t len;
513 const WCHAR *ptr, *name = get_object_name( obj, &len );
515 /* if there is a backslash return the part of the name after it */
516 if (name && (ptr = memchrW( name, '\\', len )))
518 len -= (ptr + 1 - name) * sizeof(WCHAR);
519 name = ptr + 1;
521 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
523 release_object( obj );