Partially implement and test the shelllink object's
[wine/multimedia.git] / server / winstation.c
blob486b2e228ed0cafb88e3ee8a99c809da763f2651
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 WCHAR *name, size_t len, unsigned int flags )
85 struct winstation *winstation;
87 if (!winstation_namespace && !(winstation_namespace = create_namespace( 7 )))
88 return NULL;
90 if (memchrW( 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, len,
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 WCHAR *name, size_t len,
146 struct winstation *winstation, size_t *res_len )
148 const WCHAR *winstation_name;
149 WCHAR *full_name;
150 size_t winstation_len;
152 if (memchrW( 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 + 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 );
166 return full_name;
169 /* retrieve a pointer to a desktop object */
170 inline static struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
171 unsigned int access )
173 return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
176 /* create a desktop object */
177 static struct desktop *create_desktop( const WCHAR *name, size_t len, unsigned int flags,
178 struct winstation *winstation )
180 struct desktop *desktop;
181 WCHAR *full_name;
182 size_t full_len;
184 if (!(full_name = build_desktop_name( name, len, winstation, &full_len ))) return NULL;
186 if ((desktop = create_named_object( winstation_namespace, &desktop_ops, full_name, full_len,
187 OBJ_CASE_INSENSITIVE )))
189 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
191 /* initialize it if it didn't already exist */
192 desktop->flags = flags;
193 desktop->winstation = (struct winstation *)grab_object( winstation );
194 desktop->top_window = NULL;
195 desktop->global_hooks = NULL;
196 list_add_tail( &winstation->desktops, &desktop->entry );
199 free( full_name );
200 return desktop;
203 static void desktop_dump( struct object *obj, int verbose )
205 struct desktop *desktop = (struct desktop *)obj;
207 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
208 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
209 dump_object_name( &desktop->obj );
210 fputc( '\n', stderr );
213 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
215 struct thread *thread;
217 /* check if the handle is currently used by the process or one of its threads */
218 if (process->desktop == handle) return 0;
219 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
220 if (thread->desktop == handle) return 0;
221 return 1;
224 static void desktop_destroy( struct object *obj )
226 struct desktop *desktop = (struct desktop *)obj;
228 if (desktop->top_window) destroy_window( desktop->top_window );
229 if (desktop->global_hooks) release_object( desktop->global_hooks );
230 list_remove( &desktop->entry );
231 release_object( desktop->winstation );
234 /* retrieve the thread desktop, checking the handle access rights */
235 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
237 return get_desktop_obj( thread->process, thread->desktop, access );
240 /* connect a process to its window station */
241 void connect_process_winstation( struct process *process, const WCHAR *name, size_t len )
243 struct winstation *winstation;
245 if (process->winstation) return; /* already has one */
247 /* check for an inherited winstation handle (don't ask...) */
248 if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
250 if (name)
252 winstation = create_winstation( name, len, 0 );
254 else
256 if (!interactive_winstation)
258 static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
259 interactive_winstation = create_winstation( winsta0W, sizeof(winsta0W), 0 );
260 winstation = interactive_winstation;
262 else winstation = (struct winstation *)grab_object( interactive_winstation );
264 if (winstation)
266 process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE );
267 release_object( winstation );
269 clear_error(); /* ignore errors */
272 /* connect a process to its main desktop */
273 void connect_process_desktop( struct process *process, const WCHAR *name, size_t len )
275 struct desktop *desktop;
276 struct winstation *winstation;
278 if (process->desktop) return; /* already has one */
280 if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
282 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
284 if (!name)
286 name = defaultW;
287 len = sizeof(defaultW);
289 if ((desktop = create_desktop( name, len, 0, winstation )))
291 process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
292 release_object( desktop );
294 release_object( winstation );
296 clear_error(); /* ignore errors */
299 /* close the desktop of a given thread */
300 void close_thread_desktop( struct thread *thread )
302 obj_handle_t handle = thread->desktop;
304 thread->desktop = 0;
305 if (handle) close_handle( thread->process, handle, NULL );
306 clear_error(); /* ignore errors */
310 /* create a window station */
311 DECL_HANDLER(create_winstation)
313 struct winstation *winstation;
315 reply->handle = 0;
316 if ((winstation = create_winstation( get_req_data(), get_req_data_size(), req->flags )))
318 reply->handle = alloc_handle( current->process, winstation, req->access, req->inherit );
319 release_object( winstation );
323 /* open a handle to a window station */
324 DECL_HANDLER(open_winstation)
326 if (winstation_namespace)
327 reply->handle = open_object( winstation_namespace, get_req_data(), get_req_data_size(),
328 &winstation_ops, req->access,
329 OBJ_CASE_INSENSITIVE | (req->inherit ? OBJ_INHERIT:0) );
330 else
331 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
335 /* close a window station */
336 DECL_HANDLER(close_winstation)
338 struct winstation *winstation;
340 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
341 0, &winstation_ops )))
343 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_ACCESS_DENIED );
344 release_object( winstation );
349 /* get the process current window station */
350 DECL_HANDLER(get_process_winstation)
352 reply->handle = current->process->winstation;
356 /* set the process current window station */
357 DECL_HANDLER(set_process_winstation)
359 struct winstation *winstation;
361 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
362 0, &winstation_ops )))
364 /* FIXME: should we close the old one? */
365 current->process->winstation = req->handle;
366 release_object( winstation );
370 /* create a desktop */
371 DECL_HANDLER(create_desktop)
373 struct desktop *desktop;
374 struct winstation *winstation;
376 reply->handle = 0;
377 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
379 if ((desktop = create_desktop( get_req_data(), get_req_data_size(),
380 req->flags, winstation )))
382 reply->handle = alloc_handle( current->process, desktop, req->access, req->inherit );
383 release_object( desktop );
385 release_object( winstation );
389 /* open a handle to a desktop */
390 DECL_HANDLER(open_desktop)
392 struct winstation *winstation;
394 if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
396 size_t full_len;
397 WCHAR *full_name;
399 if ((full_name = build_desktop_name( get_req_data(), get_req_data_size(),
400 winstation, &full_len )))
402 reply->handle = open_object( winstation_namespace, full_name, full_len,
403 &desktop_ops, req->access,
404 OBJ_CASE_INSENSITIVE | (req->inherit ? OBJ_INHERIT:0) );
405 free( full_name );
407 release_object( winstation );
412 /* close a desktop */
413 DECL_HANDLER(close_desktop)
415 struct desktop *desktop;
417 /* make sure it is a desktop handle */
418 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
419 0, &desktop_ops )))
421 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
422 release_object( desktop );
427 /* get the thread current desktop */
428 DECL_HANDLER(get_thread_desktop)
430 struct thread *thread;
432 if (!(thread = get_thread_from_id( req->tid ))) return;
433 reply->handle = thread->desktop;
434 release_object( thread );
438 /* set the thread current desktop */
439 DECL_HANDLER(set_thread_desktop)
441 struct desktop *old_desktop, *new_desktop;
442 struct winstation *winstation;
444 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
445 return;
447 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
449 release_object( winstation );
450 return;
452 if (new_desktop->winstation != winstation)
454 set_error( STATUS_ACCESS_DENIED );
455 release_object( new_desktop );
456 release_object( winstation );
457 return;
460 /* check if we are changing to a new desktop */
462 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
463 clear_error(); /* ignore error */
465 /* when changing desktop, we can't have any users on the current one */
466 if (old_desktop != new_desktop && current->desktop_users > 0)
467 set_error( STATUS_DEVICE_BUSY );
468 else
469 current->desktop = req->handle; /* FIXME: should we close the old one? */
471 if (old_desktop != new_desktop) detach_thread_input( current );
473 if (old_desktop) release_object( old_desktop );
474 release_object( new_desktop );
475 release_object( winstation );
479 /* get/set information about a user object (window station or desktop) */
480 DECL_HANDLER(set_user_object_info)
482 struct object *obj;
484 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
486 if (obj->ops == &desktop_ops)
488 struct desktop *desktop = (struct desktop *)obj;
489 reply->is_desktop = 1;
490 reply->old_obj_flags = desktop->flags;
491 if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
493 else if (obj->ops == &winstation_ops)
495 struct winstation *winstation = (struct winstation *)obj;
496 reply->is_desktop = 0;
497 reply->old_obj_flags = winstation->flags;
498 if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
500 else
502 set_error( STATUS_OBJECT_TYPE_MISMATCH );
503 release_object( obj );
504 return;
506 if (get_reply_max_size())
508 size_t len;
509 const WCHAR *ptr, *name = get_object_name( obj, &len );
511 /* if there is a backslash return the part of the name after it */
512 if (name && (ptr = memchrW( name, '\\', len )))
514 len -= (ptr + 1 - name) * sizeof(WCHAR);
515 name = ptr + 1;
517 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
519 release_object( obj );