Added a real "window restore" glyph to Wine Marlett.
[wine/multimedia.git] / server / winstation.c
blob0d473cad506c85cc6e55239d8a19c1c291e51ea4
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"
38 struct winstation
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 */
44 struct clipboard *clipboard; /* clipboard information */
45 struct atom_table *atom_table; /* global atom table */
48 struct desktop
50 struct object obj; /* object header */
51 unsigned int flags; /* desktop flags */
52 struct winstation *winstation; /* winstation this desktop belongs to */
53 struct list entry; /* entry in winstation list of desktops */
56 static struct list winstation_list = LIST_INIT(winstation_list);
57 static struct winstation *interactive_winstation;
58 static struct namespace *winstation_namespace;
60 static void winstation_dump( struct object *obj, int verbose );
61 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
62 static void winstation_destroy( struct object *obj );
63 static void desktop_dump( struct object *obj, int verbose );
64 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
65 static void desktop_destroy( struct object *obj );
67 static const struct object_ops winstation_ops =
69 sizeof(struct winstation), /* size */
70 winstation_dump, /* dump */
71 no_add_queue, /* add_queue */
72 NULL, /* remove_queue */
73 NULL, /* signaled */
74 NULL, /* satisfied */
75 no_signal, /* signal */
76 no_get_fd, /* get_fd */
77 winstation_close_handle, /* close_handle */
78 winstation_destroy /* destroy */
82 static const struct object_ops desktop_ops =
84 sizeof(struct desktop), /* size */
85 desktop_dump, /* dump */
86 no_add_queue, /* add_queue */
87 NULL, /* remove_queue */
88 NULL, /* signaled */
89 NULL, /* satisfied */
90 no_signal, /* signal */
91 no_get_fd, /* get_fd */
92 desktop_close_handle, /* close_handle */
93 desktop_destroy /* destroy */
96 #define DESKTOP_ALL_ACCESS 0x01ff
98 /* create a winstation object */
99 static struct winstation *create_winstation( const WCHAR *name, size_t len, unsigned int flags )
101 struct winstation *winstation;
103 if (!winstation_namespace && !(winstation_namespace = create_namespace( 7, FALSE )))
104 return NULL;
106 if (memchrW( name, '\\', len / sizeof(WCHAR) )) /* no backslash allowed in name */
108 set_error( STATUS_INVALID_PARAMETER );
109 return NULL;
112 if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, len )))
114 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
116 /* initialize it if it didn't already exist */
117 winstation->flags = flags;
118 winstation->clipboard = NULL;
119 winstation->atom_table = NULL;
120 list_add_tail( &winstation_list, &winstation->entry );
121 list_init( &winstation->desktops );
124 return winstation;
127 static void winstation_dump( struct object *obj, int verbose )
129 struct winstation *winstation = (struct winstation *)obj;
131 fprintf( stderr, "Winstation flags=%x ", winstation->flags );
132 dump_object_name( &winstation->obj );
133 fputc( '\n', stderr );
136 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
138 return (process->winstation != handle);
141 static void winstation_destroy( struct object *obj )
143 struct winstation *winstation = (struct winstation *)obj;
145 if (winstation == interactive_winstation) interactive_winstation = NULL;
146 list_remove( &winstation->entry );
147 if (winstation->clipboard) release_object( winstation->clipboard );
148 if (winstation->atom_table) release_object( winstation->atom_table );
151 /* retrieve the process window station, checking the handle access rights */
152 struct winstation *get_process_winstation( struct process *process, unsigned int access )
154 return (struct winstation *)get_handle_obj( process, process->winstation,
155 access, &winstation_ops );
158 /* set the pointer to the (opaque) clipboard info */
159 void set_winstation_clipboard( struct winstation *winstation, struct clipboard *clipboard )
161 winstation->clipboard = clipboard;
164 /* retrieve the pointer to the (opaque) clipboard info */
165 struct clipboard *get_winstation_clipboard( struct winstation *winstation )
167 return winstation->clipboard;
170 /* set the pointer to the (opaque) atom table */
171 void set_winstation_atom_table( struct winstation *winstation, struct atom_table *table )
173 winstation->atom_table = table;
176 /* retrieve the pointer to the (opaque) clipboard info */
177 struct atom_table *get_winstation_atom_table( struct winstation *winstation )
179 return winstation->atom_table;
182 /* build the full name of a desktop object */
183 static WCHAR *build_desktop_name( const WCHAR *name, size_t len,
184 struct winstation *winstation, size_t *res_len )
186 const WCHAR *winstation_name;
187 WCHAR *full_name;
188 size_t winstation_len;
190 if (memchrW( name, '\\', len / sizeof(WCHAR) ))
192 set_error( STATUS_INVALID_PARAMETER );
193 return NULL;
196 if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
197 winstation_len = 0;
199 *res_len = winstation_len + len + sizeof(WCHAR);
200 if (!(full_name = mem_alloc( *res_len ))) return NULL;
201 memcpy( full_name, winstation_name, winstation_len );
202 full_name[winstation_len / sizeof(WCHAR)] = '\\';
203 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name, len );
204 return full_name;
207 /* retrieve a pointer to a desktop object */
208 inline static struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
209 unsigned int access )
211 return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
214 /* create a desktop object */
215 static struct desktop *create_desktop( const WCHAR *name, size_t len, unsigned int flags,
216 struct winstation *winstation )
218 struct desktop *desktop;
219 WCHAR *full_name;
220 size_t full_len;
222 if (!(full_name = build_desktop_name( name, len, winstation, &full_len ))) return NULL;
224 if ((desktop = create_named_object( winstation_namespace, &desktop_ops, full_name, full_len )))
226 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
228 /* initialize it if it didn't already exist */
229 desktop->flags = flags;
230 desktop->winstation = (struct winstation *)grab_object( winstation );
231 list_add_tail( &winstation->desktops, &desktop->entry );
234 free( full_name );
235 return desktop;
238 static void desktop_dump( struct object *obj, int verbose )
240 struct desktop *desktop = (struct desktop *)obj;
242 fprintf( stderr, "Desktop flags=%x winstation=%p ", desktop->flags, desktop->winstation );
243 dump_object_name( &desktop->obj );
244 fputc( '\n', stderr );
247 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
249 struct thread *thread;
251 /* check if the handle is currently used by the process or one of its threads */
252 if (process->desktop == handle) return 0;
253 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
254 if (thread->desktop == handle) return 0;
255 return 1;
258 static void desktop_destroy( struct object *obj )
260 struct desktop *desktop = (struct desktop *)obj;
262 list_remove( &desktop->entry );
263 release_object( desktop->winstation );
266 /* connect a process to its window station */
267 void connect_process_winstation( struct process *process, const WCHAR *name, size_t len )
269 struct winstation *winstation;
271 if (process->winstation) return; /* already has one */
273 /* check for an inherited winstation handle (don't ask...) */
274 if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
276 if (name)
278 winstation = create_winstation( name, len, 0 );
280 else
282 if (!interactive_winstation)
284 static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
285 interactive_winstation = create_winstation( winsta0W, sizeof(winsta0W), 0 );
286 winstation = interactive_winstation;
288 else winstation = (struct winstation *)grab_object( interactive_winstation );
290 if (winstation)
292 process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE );
293 release_object( winstation );
295 clear_error(); /* ignore errors */
298 /* connect a process to its main desktop */
299 void connect_process_desktop( struct process *process, const WCHAR *name, size_t len )
301 struct desktop *desktop;
302 struct winstation *winstation;
304 if (process->desktop) return; /* already has one */
306 if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
308 static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
310 if (!name)
312 name = defaultW;
313 len = sizeof(defaultW);
315 if ((desktop = create_desktop( name, len, 0, winstation )))
317 process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
318 release_object( desktop );
320 release_object( winstation );
322 clear_error(); /* ignore errors */
325 /* close the desktop of a given thread */
326 void close_thread_desktop( struct thread *thread )
328 obj_handle_t handle = thread->desktop;
330 thread->desktop = 0;
331 if (handle) close_handle( thread->process, handle, NULL );
332 clear_error(); /* ignore errors */
336 /* create a window station */
337 DECL_HANDLER(create_winstation)
339 struct winstation *winstation;
341 reply->handle = 0;
342 if ((winstation = create_winstation( get_req_data(), get_req_data_size(), req->flags )))
344 reply->handle = alloc_handle( current->process, winstation, req->access, req->inherit );
345 release_object( winstation );
349 /* open a handle to a window station */
350 DECL_HANDLER(open_winstation)
352 if (winstation_namespace)
353 reply->handle = open_object( winstation_namespace, get_req_data(), get_req_data_size(),
354 &winstation_ops, req->access, req->inherit );
355 else
356 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
360 /* close a window station */
361 DECL_HANDLER(close_winstation)
363 struct winstation *winstation;
365 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
366 0, &winstation_ops )))
368 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_ACCESS_DENIED );
369 release_object( winstation );
374 /* get the process current window station */
375 DECL_HANDLER(get_process_winstation)
377 reply->handle = current->process->winstation;
381 /* set the process current window station */
382 DECL_HANDLER(set_process_winstation)
384 struct winstation *winstation;
386 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
387 0, &winstation_ops )))
389 /* FIXME: should we close the old one? */
390 current->process->winstation = req->handle;
391 release_object( winstation );
395 /* create a desktop */
396 DECL_HANDLER(create_desktop)
398 struct desktop *desktop;
399 struct winstation *winstation;
401 reply->handle = 0;
402 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
404 if ((desktop = create_desktop( get_req_data(), get_req_data_size(),
405 req->flags, winstation )))
407 reply->handle = alloc_handle( current->process, desktop, req->access, req->inherit );
408 release_object( desktop );
410 release_object( winstation );
414 /* open a handle to a desktop */
415 DECL_HANDLER(open_desktop)
417 struct winstation *winstation;
419 if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
421 size_t full_len;
422 WCHAR *full_name;
424 if ((full_name = build_desktop_name( get_req_data(), get_req_data_size(),
425 winstation, &full_len )))
427 reply->handle = open_object( winstation_namespace, full_name, full_len,
428 &desktop_ops, req->access, req->inherit );
429 free( full_name );
431 release_object( winstation );
436 /* close a desktop */
437 DECL_HANDLER(close_desktop)
439 struct desktop *desktop;
441 /* make sure it is a desktop handle */
442 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
443 0, &desktop_ops )))
445 if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
446 release_object( desktop );
451 /* get the thread current desktop */
452 DECL_HANDLER(get_thread_desktop)
454 struct thread *thread;
456 if (!(thread = get_thread_from_id( req->tid ))) return;
457 reply->handle = thread->desktop;
458 release_object( thread );
462 /* set the thread current desktop */
463 DECL_HANDLER(set_thread_desktop)
465 struct desktop *old_desktop, *new_desktop;
466 struct winstation *winstation;
468 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
469 return;
471 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
473 release_object( winstation );
474 return;
476 if (new_desktop->winstation != winstation)
478 set_error( STATUS_ACCESS_DENIED );
479 release_object( new_desktop );
480 release_object( winstation );
481 return;
484 /* check if we are changing to a new desktop */
486 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
487 clear_error(); /* ignore error */
489 /* when changing desktop, we can't have any users on the current one */
490 if (old_desktop != new_desktop && current->desktop_users > 0)
491 set_error( STATUS_DEVICE_BUSY );
492 else
493 current->desktop = req->handle; /* FIXME: should we close the old one? */
495 if (old_desktop) release_object( old_desktop );
496 release_object( new_desktop );
497 release_object( winstation );
501 /* get/set information about a user object (window station or desktop) */
502 DECL_HANDLER(set_user_object_info)
504 struct object *obj;
506 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
508 if (obj->ops == &desktop_ops)
510 struct desktop *desktop = (struct desktop *)obj;
511 reply->is_desktop = 1;
512 reply->old_obj_flags = desktop->flags;
513 if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
515 else if (obj->ops == &winstation_ops)
517 struct winstation *winstation = (struct winstation *)obj;
518 reply->is_desktop = 0;
519 reply->old_obj_flags = winstation->flags;
520 if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
522 else
524 set_error( STATUS_OBJECT_TYPE_MISMATCH );
525 release_object( obj );
526 return;
528 if (get_reply_max_size())
530 size_t len;
531 const WCHAR *ptr, *name = get_object_name( obj, &len );
533 /* if there is a backslash return the part of the name after it */
534 if (name && (ptr = memchrW( name, '\\', len )))
536 len -= (ptr + 1 - name) * sizeof(WCHAR);
537 name = ptr + 1;
539 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
541 release_object( obj );