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