shdocvw: Return correct error from WebBrowser::Quit.
[wine] / server / winstation.c
1 /*
2  * Server-side window stations and desktops handling
3  *
4  * Copyright (C) 2002, 2005 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winternl.h"
33
34 #include "object.h"
35 #include "handle.h"
36 #include "request.h"
37 #include "process.h"
38 #include "user.h"
39 #include "file.h"
40 #include "wine/unicode.h"
41
42
43 static struct list winstation_list = LIST_INIT(winstation_list);
44 static struct namespace *winstation_namespace;
45
46 static void winstation_dump( struct object *obj, int verbose );
47 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
48 static void winstation_destroy( struct object *obj );
49 static unsigned int winstation_map_access( struct object *obj, unsigned int access );
50 static void desktop_dump( struct object *obj, int verbose );
51 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
52 static void desktop_destroy( struct object *obj );
53 static unsigned int desktop_map_access( struct object *obj, unsigned int access );
54
55 static const struct object_ops winstation_ops =
56 {
57     sizeof(struct winstation),    /* size */
58     winstation_dump,              /* dump */
59     no_add_queue,                 /* add_queue */
60     NULL,                         /* remove_queue */
61     NULL,                         /* signaled */
62     NULL,                         /* satisfied */
63     no_signal,                    /* signal */
64     no_get_fd,                    /* get_fd */
65     winstation_map_access,        /* map_access */
66     default_get_sd,               /* get_sd */
67     default_set_sd,               /* set_sd */
68     no_lookup_name,               /* lookup_name */
69     no_open_file,                 /* open_file */
70     winstation_close_handle,      /* close_handle */
71     winstation_destroy            /* destroy */
72 };
73
74
75 static const struct object_ops desktop_ops =
76 {
77     sizeof(struct desktop),       /* size */
78     desktop_dump,                 /* dump */
79     no_add_queue,                 /* add_queue */
80     NULL,                         /* remove_queue */
81     NULL,                         /* signaled */
82     NULL,                         /* satisfied */
83     no_signal,                    /* signal */
84     no_get_fd,                    /* get_fd */
85     desktop_map_access,           /* map_access */
86     default_get_sd,               /* get_sd */
87     default_set_sd,               /* set_sd */
88     no_lookup_name,               /* lookup_name */
89     no_open_file,                 /* open_file */
90     desktop_close_handle,         /* close_handle */
91     desktop_destroy               /* destroy */
92 };
93
94 #define DESKTOP_ALL_ACCESS 0x01ff
95
96 /* create a winstation object */
97 static struct winstation *create_winstation( const struct unicode_str *name, unsigned int attr,
98                                              unsigned int flags )
99 {
100     struct winstation *winstation;
101
102     if (!winstation_namespace && !(winstation_namespace = create_namespace( 7 )))
103         return NULL;
104
105     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))  /* no backslash allowed in name */
106     {
107         set_error( STATUS_INVALID_PARAMETER );
108         return NULL;
109     }
110
111     if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, attr )))
112     {
113         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
114         {
115             /* initialize it if it didn't already exist */
116             winstation->flags = flags;
117             winstation->clipboard = NULL;
118             winstation->atom_table = NULL;
119             list_add_tail( &winstation_list, &winstation->entry );
120             list_init( &winstation->desktops );
121         }
122     }
123     return winstation;
124 }
125
126 static void winstation_dump( struct object *obj, int verbose )
127 {
128     struct winstation *winstation = (struct winstation *)obj;
129
130     fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p ",
131              winstation->flags, winstation->clipboard, winstation->atom_table );
132     dump_object_name( &winstation->obj );
133     fputc( '\n', stderr );
134 }
135
136 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
137 {
138     return (process->winstation != handle);
139 }
140
141 static void winstation_destroy( struct object *obj )
142 {
143     struct winstation *winstation = (struct winstation *)obj;
144
145     list_remove( &winstation->entry );
146     if (winstation->clipboard) release_object( winstation->clipboard );
147     if (winstation->atom_table) release_object( winstation->atom_table );
148 }
149
150 static unsigned int winstation_map_access( struct object *obj, unsigned int access )
151 {
152     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | WINSTA_ENUMDESKTOPS | WINSTA_READATTRIBUTES |
153                                             WINSTA_ENUMERATE | WINSTA_READSCREEN;
154     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | WINSTA_ACCESSCLIPBOARD | WINSTA_CREATEDESKTOP |
155                                             WINSTA_WRITEATTRIBUTES;
156     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS;
157     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS;
158     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
159 }
160
161 /* retrieve the process window station, checking the handle access rights */
162 struct winstation *get_process_winstation( struct process *process, unsigned int access )
163 {
164     return (struct winstation *)get_handle_obj( process, process->winstation,
165                                                 access, &winstation_ops );
166 }
167
168 /* build the full name of a desktop object */
169 static WCHAR *build_desktop_name( const struct unicode_str *name,
170                                   struct winstation *winstation, struct unicode_str *res )
171 {
172     const WCHAR *winstation_name;
173     WCHAR *full_name;
174     data_size_t winstation_len;
175
176     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))
177     {
178         set_error( STATUS_INVALID_PARAMETER );
179         return NULL;
180     }
181
182     if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
183         winstation_len = 0;
184
185     res->len = winstation_len + name->len + sizeof(WCHAR);
186     if (!(full_name = mem_alloc( res->len ))) return NULL;
187     memcpy( full_name, winstation_name, winstation_len );
188     full_name[winstation_len / sizeof(WCHAR)] = '\\';
189     memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name->str, name->len );
190     res->str = full_name;
191     return full_name;
192 }
193
194 /* retrieve a pointer to a desktop object */
195 static inline struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
196                                                unsigned int access )
197 {
198     return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
199 }
200
201 /* create a desktop object */
202 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
203                                        unsigned int flags, struct winstation *winstation )
204 {
205     struct desktop *desktop;
206     struct unicode_str full_str;
207     WCHAR *full_name;
208
209     if (!(full_name = build_desktop_name( name, winstation, &full_str ))) return NULL;
210
211     if ((desktop = create_named_object( winstation_namespace, &desktop_ops, &full_str, attr )))
212     {
213         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
214         {
215             /* initialize it if it didn't already exist */
216             desktop->flags = flags;
217             desktop->winstation = (struct winstation *)grab_object( winstation );
218             desktop->top_window = NULL;
219             desktop->global_hooks = NULL;
220             desktop->close_timeout = NULL;
221             desktop->users = 0;
222             list_add_tail( &winstation->desktops, &desktop->entry );
223         }
224     }
225     free( full_name );
226     return desktop;
227 }
228
229 static void desktop_dump( struct object *obj, int verbose )
230 {
231     struct desktop *desktop = (struct desktop *)obj;
232
233     fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
234              desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
235     dump_object_name( &desktop->obj );
236     fputc( '\n', stderr );
237 }
238
239 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
240 {
241     struct thread *thread;
242
243     /* check if the handle is currently used by the process or one of its threads */
244     if (process->desktop == handle) return 0;
245     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
246         if (thread->desktop == handle) return 0;
247     return 1;
248 }
249
250 static void desktop_destroy( struct object *obj )
251 {
252     struct desktop *desktop = (struct desktop *)obj;
253
254     if (desktop->top_window) destroy_window( desktop->top_window );
255     if (desktop->global_hooks) release_object( desktop->global_hooks );
256     if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
257     list_remove( &desktop->entry );
258     release_object( desktop->winstation );
259 }
260
261 static unsigned int desktop_map_access( struct object *obj, unsigned int access )
262 {
263     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | DESKTOP_READOBJECTS | DESKTOP_ENUMERATE;
264     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
265                                             DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
266                                             DESKTOP_WRITEOBJECTS;
267     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP;
268     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS;
269     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
270 }
271
272 /* retrieve the thread desktop, checking the handle access rights */
273 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
274 {
275     return get_desktop_obj( thread->process, thread->desktop, access );
276 }
277
278 /* set the process default desktop handle */
279 void set_process_default_desktop( struct process *process, struct desktop *desktop,
280                                   obj_handle_t handle )
281 {
282     struct thread *thread;
283     struct desktop *old_desktop;
284
285     if (process->desktop == handle) return;  /* nothing to do */
286
287     if (!(old_desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error();
288     process->desktop = handle;
289
290     /* set desktop for threads that don't have one yet */
291     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
292         if (!thread->desktop) thread->desktop = handle;
293
294     desktop->users++;
295     if (desktop->close_timeout)
296     {
297         remove_timeout_user( desktop->close_timeout );
298         desktop->close_timeout = NULL;
299     }
300     if (old_desktop)
301     {
302         old_desktop->users--;
303         release_object( old_desktop );
304     }
305 }
306
307 /* connect a process to its window station */
308 void connect_process_winstation( struct process *process, struct thread *parent )
309 {
310     struct winstation *winstation = NULL;
311     struct desktop *desktop = NULL;
312     obj_handle_t handle;
313
314     /* check for an inherited winstation handle (don't ask...) */
315     if ((handle = find_inherited_handle( process, &winstation_ops )))
316     {
317         winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
318     }
319     else if (parent && parent->process->winstation)
320     {
321         handle = duplicate_handle( parent->process, parent->process->winstation,
322                                    process, 0, 0, DUP_HANDLE_SAME_ACCESS );
323         winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
324     }
325     if (!winstation) goto done;
326     process->winstation = handle;
327
328     if ((handle = find_inherited_handle( process, &desktop_ops )))
329     {
330         desktop = get_desktop_obj( process, handle, 0 );
331         if (!desktop || desktop->winstation != winstation) goto done;
332     }
333     else if (parent && parent->desktop)
334     {
335         desktop = get_desktop_obj( parent->process, parent->desktop, 0 );
336         if (!desktop || desktop->winstation != winstation) goto done;
337         handle = duplicate_handle( parent->process, parent->desktop,
338                                    process, 0, 0, DUP_HANDLE_SAME_ACCESS );
339     }
340
341     if (handle) set_process_default_desktop( process, desktop, handle );
342
343 done:
344     if (desktop) release_object( desktop );
345     if (winstation) release_object( winstation );
346     clear_error();
347 }
348
349 static void close_desktop_timeout( void *private )
350 {
351     struct desktop *desktop = private;
352
353     desktop->close_timeout = NULL;
354     unlink_named_object( &desktop->obj );  /* make sure no other process can open it */
355     close_desktop_window( desktop );  /* and signal the owner to quit */
356 }
357
358 /* close the desktop of a given process */
359 void close_process_desktop( struct process *process )
360 {
361     struct desktop *desktop;
362
363     if (process->desktop && (desktop = get_desktop_obj( process, process->desktop, 0 )))
364     {
365         assert( desktop->users > 0 );
366         desktop->users--;
367         /* if we have one remaining user, it has to be the manager of the desktop window */
368         if (desktop->users == 1 && get_top_window_owner( desktop ))
369         {
370             assert( !desktop->close_timeout );
371             desktop->close_timeout = add_timeout_user( -TICKS_PER_SEC, close_desktop_timeout, desktop );
372         }
373         release_object( desktop );
374     }
375     clear_error();  /* ignore errors */
376 }
377
378 /* close the desktop of a given thread */
379 void close_thread_desktop( struct thread *thread )
380 {
381     obj_handle_t handle = thread->desktop;
382
383     thread->desktop = 0;
384     if (handle) close_handle( thread->process, handle );
385     clear_error();  /* ignore errors */
386 }
387
388
389 /* create a window station */
390 DECL_HANDLER(create_winstation)
391 {
392     struct winstation *winstation;
393     struct unicode_str name;
394
395     reply->handle = 0;
396     get_req_unicode_str( &name );
397     if ((winstation = create_winstation( &name, req->attributes, req->flags )))
398     {
399         reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
400         release_object( winstation );
401     }
402 }
403
404 /* open a handle to a window station */
405 DECL_HANDLER(open_winstation)
406 {
407     struct unicode_str name;
408
409     get_req_unicode_str( &name );
410     if (winstation_namespace)
411         reply->handle = open_object( winstation_namespace, &name, &winstation_ops, req->access,
412                                      req->attributes );
413     else
414         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
415 }
416
417
418 /* close a window station */
419 DECL_HANDLER(close_winstation)
420 {
421     struct winstation *winstation;
422
423     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
424                                                            0, &winstation_ops )))
425     {
426         if (!close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
427         release_object( winstation );
428     }
429 }
430
431
432 /* get the process current window station */
433 DECL_HANDLER(get_process_winstation)
434 {
435     reply->handle = current->process->winstation;
436 }
437
438
439 /* set the process current window station */
440 DECL_HANDLER(set_process_winstation)
441 {
442     struct winstation *winstation;
443
444     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
445                                                            0, &winstation_ops )))
446     {
447         /* FIXME: should we close the old one? */
448         current->process->winstation = req->handle;
449         release_object( winstation );
450     }
451 }
452
453 /* create a desktop */
454 DECL_HANDLER(create_desktop)
455 {
456     struct desktop *desktop;
457     struct winstation *winstation;
458     struct unicode_str name;
459
460     reply->handle = 0;
461     get_req_unicode_str( &name );
462     if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
463     {
464         if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
465         {
466             reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
467             release_object( desktop );
468         }
469         release_object( winstation );
470     }
471 }
472
473 /* open a handle to a desktop */
474 DECL_HANDLER(open_desktop)
475 {
476     struct winstation *winstation;
477     struct unicode_str name;
478
479     get_req_unicode_str( &name );
480     if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
481     {
482         struct unicode_str full_str;
483         WCHAR *full_name;
484
485         if ((full_name = build_desktop_name( &name, winstation, &full_str )))
486         {
487             reply->handle = open_object( winstation_namespace, &full_str, &desktop_ops, req->access,
488                                          req->attributes );
489             free( full_name );
490         }
491         release_object( winstation );
492     }
493 }
494
495
496 /* close a desktop */
497 DECL_HANDLER(close_desktop)
498 {
499     struct desktop *desktop;
500
501     /* make sure it is a desktop handle */
502     if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
503                                                      0, &desktop_ops )))
504     {
505         if (!close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
506         release_object( desktop );
507     }
508 }
509
510
511 /* get the thread current desktop */
512 DECL_HANDLER(get_thread_desktop)
513 {
514     struct thread *thread;
515
516     if (!(thread = get_thread_from_id( req->tid ))) return;
517     reply->handle = thread->desktop;
518     release_object( thread );
519 }
520
521
522 /* set the thread current desktop */
523 DECL_HANDLER(set_thread_desktop)
524 {
525     struct desktop *old_desktop, *new_desktop;
526     struct winstation *winstation;
527
528     if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
529         return;
530
531     if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
532     {
533         release_object( winstation );
534         return;
535     }
536     if (new_desktop->winstation != winstation)
537     {
538         set_error( STATUS_ACCESS_DENIED );
539         release_object( new_desktop );
540         release_object( winstation );
541         return;
542     }
543
544     /* check if we are changing to a new desktop */
545
546     if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
547         clear_error();  /* ignore error */
548
549     /* when changing desktop, we can't have any users on the current one */
550     if (old_desktop != new_desktop && current->desktop_users > 0)
551         set_error( STATUS_DEVICE_BUSY );
552     else
553         current->desktop = req->handle;  /* FIXME: should we close the old one? */
554
555     if (!current->process->desktop)
556         set_process_default_desktop( current->process, new_desktop, req->handle );
557
558     if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
559
560     if (old_desktop) release_object( old_desktop );
561     release_object( new_desktop );
562     release_object( winstation );
563 }
564
565
566 /* get/set information about a user object (window station or desktop) */
567 DECL_HANDLER(set_user_object_info)
568 {
569     struct object *obj;
570
571     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
572
573     if (obj->ops == &desktop_ops)
574     {
575         struct desktop *desktop = (struct desktop *)obj;
576         reply->is_desktop = 1;
577         reply->old_obj_flags = desktop->flags;
578         if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
579     }
580     else if (obj->ops == &winstation_ops)
581     {
582         struct winstation *winstation = (struct winstation *)obj;
583         reply->is_desktop = 0;
584         reply->old_obj_flags = winstation->flags;
585         if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
586     }
587     else
588     {
589         set_error( STATUS_OBJECT_TYPE_MISMATCH );
590         release_object( obj );
591         return;
592     }
593     if (get_reply_max_size())
594     {
595         data_size_t len;
596         const WCHAR *ptr, *name = get_object_name( obj, &len );
597
598         /* if there is a backslash return the part of the name after it */
599         if (name && (ptr = memchrW( name, '\\', len/sizeof(WCHAR) )))
600         {
601             len -= (ptr + 1 - name) * sizeof(WCHAR);
602             name = ptr + 1;
603         }
604         if (name) set_reply_data( name, min( len, get_reply_max_size() ));
605     }
606     release_object( obj );
607 }