Add a few tests that fail under wine.
[wine/dibdrv.git] / server / winstation.c
bloba51315113a9b221b59890990179586c9d5be1889
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"
31 #include "object.h"
32 #include "handle.h"
33 #include "request.h"
34 #include "process.h"
35 #include "user.h"
36 #include "wine/unicode.h"
39 static struct list winstation_list = LIST_INIT(winstation_list);
40 static struct winstation *interactive_winstation;
41 static struct namespace *winstation_namespace;
43 static void winstation_dump( struct object *obj, int verbose );
44 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
45 static void winstation_destroy( struct object *obj );
46 static void desktop_dump( struct object *obj, int verbose );
47 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
48 static void desktop_destroy( struct object *obj );
50 static const struct object_ops winstation_ops =
52 sizeof(struct winstation), /* size */
53 winstation_dump, /* dump */
54 no_add_queue, /* add_queue */
55 NULL, /* remove_queue */
56 NULL, /* signaled */
57 NULL, /* satisfied */
58 no_signal, /* signal */
59 no_get_fd, /* get_fd */
60 winstation_close_handle, /* close_handle */
61 winstation_destroy /* destroy */
65 static const struct object_ops desktop_ops =
67 sizeof(struct desktop), /* size */
68 desktop_dump, /* dump */
69 no_add_queue, /* add_queue */
70 NULL, /* remove_queue */
71 NULL, /* signaled */
72 NULL, /* satisfied */
73 no_signal, /* signal */
74 no_get_fd, /* get_fd */
75 desktop_close_handle, /* close_handle */
76 desktop_destroy /* destroy */
79 #define DESKTOP_ALL_ACCESS 0x01ff
81 /* create a winstation object */
82 static struct winstation *create_winstation( const WCHAR *name, size_t len, unsigned int flags )
84 struct winstation *winstation;
86 if (!winstation_namespace && !(winstation_namespace = create_namespace( 7, FALSE )))
87 return NULL;
89 if (memchrW( name, '\\', len / sizeof(WCHAR) )) /* no backslash allowed in name */
91 set_error( STATUS_INVALID_PARAMETER );
92 return NULL;
95 if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, len )))
97 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
99 /* initialize it if it didn't already exist */
100 winstation->flags = flags;
101 winstation->clipboard = NULL;
102 winstation->atom_table = NULL;
103 list_add_tail( &winstation_list, &winstation->entry );
104 list_init( &winstation->desktops );
107 return winstation;
110 static void winstation_dump( struct object *obj, int verbose )
112 struct winstation *winstation = (struct winstation *)obj;
114 fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p ",
115 winstation->flags, winstation->clipboard, winstation->atom_table );
116 dump_object_name( &winstation->obj );
117 fputc( '\n', stderr );
120 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
122 return (process->winstation != handle);
125 static void winstation_destroy( struct object *obj )
127 struct winstation *winstation = (struct winstation *)obj;
129 if (winstation == interactive_winstation) interactive_winstation = NULL;
130 list_remove( &winstation->entry );
131 if (winstation->clipboard) release_object( winstation->clipboard );
132 if (winstation->atom_table) release_object( winstation->atom_table );
135 /* retrieve the process window station, checking the handle access rights */
136 struct winstation *get_process_winstation( struct process *process, unsigned int access )
138 return (struct winstation *)get_handle_obj( process, process->winstation,
139 access, &winstation_ops );
142 /* build the full name of a desktop object */
143 static WCHAR *build_desktop_name( const WCHAR *name, size_t len,
144 struct winstation *winstation, size_t *res_len )
146 const WCHAR *winstation_name;
147 WCHAR *full_name;
148 size_t winstation_len;
150 if (memchrW( name, '\\', len / sizeof(WCHAR) ))
152 set_error( STATUS_INVALID_PARAMETER );
153 return NULL;
156 if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
157 winstation_len = 0;
159 *res_len = winstation_len + len + sizeof(WCHAR);
160 if (!(full_name = mem_alloc( *res_len ))) return NULL;
161 memcpy( full_name, winstation_name, winstation_len );
162 full_name[winstation_len / sizeof(WCHAR)] = '\\';
163 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name, len );
164 return full_name;
167 /* retrieve a pointer to a desktop object */
168 inline static struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
169 unsigned int access )
171 return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
174 /* create a desktop object */
175 static struct desktop *create_desktop( const WCHAR *name, size_t len, unsigned int flags,
176 struct winstation *winstation )
178 struct desktop *desktop;
179 WCHAR *full_name;
180 size_t full_len;
182 if (!(full_name = build_desktop_name( name, len, winstation, &full_len ))) return NULL;
184 if ((desktop = create_named_object( winstation_namespace, &desktop_ops, full_name, full_len )))
186 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
188 /* initialize it if it didn't already exist */
189 desktop->flags = flags;
190 desktop->winstation = (struct winstation *)grab_object( winstation );
191 desktop->top_window = NULL;
192 desktop->global_hooks = NULL;
193 list_add_tail( &winstation->desktops, &desktop->entry );
196 free( full_name );
197 return desktop;
200 static void desktop_dump( struct object *obj, int verbose )
202 struct desktop *desktop = (struct desktop *)obj;
204 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
205 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
206 dump_object_name( &desktop->obj );
207 fputc( '\n', stderr );
210 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
212 struct thread *thread;
214 /* check if the handle is currently used by the process or one of its threads */
215 if (process->desktop == handle) return 0;
216 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
217 if (thread->desktop == handle) return 0;
218 return 1;
221 static void desktop_destroy( struct object *obj )
223 struct desktop *desktop = (struct desktop *)obj;
225 if (desktop->top_window) destroy_window( desktop->top_window );
226 if (desktop->global_hooks) release_object( desktop->global_hooks );
227 list_remove( &desktop->entry );
228 release_object( desktop->winstation );
231 /* retrieve the thread desktop, checking the handle access rights */
232 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
234 return get_desktop_obj( thread->process, thread->desktop, access );
237 /* connect a process to its window station */
238 void connect_process_winstation( struct process *process, const WCHAR *name, size_t len )
240 struct winstation *winstation;
242 if (process->winstation) return; /* already has one */
244 /* check for an inherited winstation handle (don't ask...) */
245 if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
247 if (name)
249 winstation = create_winstation( name, len, 0 );
251 else
253 if (!interactive_winstation)
255 static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
256 interactive_winstation = create_winstation( winsta0W, sizeof(winsta0W), 0 );
257 winstation = interactive_winstation;
259 else winstation = (struct winstation *)grab_object( interactive_winstation );
261 if (winstation)
263 process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE );
264 release_object( winstation );
266 clear_error(); /* ignore errors */
269 /* connect a process to its main desktop */
270 void connect_process_desktop( struct process *process, const WCHAR *name, size_t len )
272 struct desktop *desktop;
273 struct winstation *winstation;
275 if (process->desktop) return; /* already has one */
277 if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
279 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
281 if (!name)
283 name = defaultW;
284 len = sizeof(defaultW);
286 if ((desktop = create_desktop( name, len, 0, winstation )))
288 process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
289 release_object( desktop );
291 release_object( winstation );
293 clear_error(); /* ignore errors */
296 /* close the desktop of a given thread */
297 void close_thread_desktop( struct thread *thread )
299 obj_handle_t handle = thread->desktop;
301 thread->desktop = 0;
302 if (handle) close_handle( thread->process, handle, NULL );
303 clear_error(); /* ignore errors */
307 /* create a window station */
308 DECL_HANDLER(create_winstation)
310 struct winstation *winstation;
312 reply->handle = 0;
313 if ((winstation = create_winstation( get_req_data(), get_req_data_size(), req->flags )))
315 reply->handle = alloc_handle( current->process, winstation, req->access, req->inherit );
316 release_object( winstation );
320 /* open a handle to a window station */
321 DECL_HANDLER(open_winstation)
323 if (winstation_namespace)
324 reply->handle = open_object( winstation_namespace, get_req_data(), get_req_data_size(),
325 &winstation_ops, req->access, req->inherit );
326 else
327 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
331 /* close a window station */
332 DECL_HANDLER(close_winstation)
334 struct winstation *winstation;
336 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
337 0, &winstation_ops )))
339 if (!close_handle( current->process, req->handle, NULL )) 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;
372 reply->handle = 0;
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? */ )))
392 size_t full_len;
393 WCHAR *full_name;
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 );
400 free( full_name );
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,
414 0, &desktop_ops )))
416 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
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 *old_desktop, *new_desktop;
437 struct winstation *winstation;
439 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
440 return;
442 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
444 release_object( winstation );
445 return;
447 if (new_desktop->winstation != winstation)
449 set_error( STATUS_ACCESS_DENIED );
450 release_object( new_desktop );
451 release_object( winstation );
452 return;
455 /* check if we are changing to a new desktop */
457 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
458 clear_error(); /* ignore error */
460 /* when changing desktop, we can't have any users on the current one */
461 if (old_desktop != new_desktop && current->desktop_users > 0)
462 set_error( STATUS_DEVICE_BUSY );
463 else
464 current->desktop = req->handle; /* FIXME: should we close the old one? */
466 if (old_desktop != new_desktop) detach_thread_input( current );
468 if (old_desktop) release_object( old_desktop );
469 release_object( new_desktop );
470 release_object( winstation );
474 /* get/set information about a user object (window station or desktop) */
475 DECL_HANDLER(set_user_object_info)
477 struct object *obj;
479 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
481 if (obj->ops == &desktop_ops)
483 struct desktop *desktop = (struct desktop *)obj;
484 reply->is_desktop = 1;
485 reply->old_obj_flags = desktop->flags;
486 if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
488 else if (obj->ops == &winstation_ops)
490 struct winstation *winstation = (struct winstation *)obj;
491 reply->is_desktop = 0;
492 reply->old_obj_flags = winstation->flags;
493 if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
495 else
497 set_error( STATUS_OBJECT_TYPE_MISMATCH );
498 release_object( obj );
499 return;
501 if (get_reply_max_size())
503 size_t len;
504 const WCHAR *ptr, *name = get_object_name( obj, &len );
506 /* if there is a backslash return the part of the name after it */
507 if (name && (ptr = memchrW( name, '\\', len )))
509 len -= (ptr + 1 - name) * sizeof(WCHAR);
510 name = ptr + 1;
512 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
514 release_object( obj );