ws2_32: s/#if/#ifdef/ so it still works if HAVE_XXX is not defined.
[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 static inline struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
207                                                unsigned int access )
208 {
209     return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
210 }
211
212 /* create a desktop object */
213 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
214                                        unsigned int flags, struct winstation *winstation )
215 {
216     struct desktop *desktop;
217     struct unicode_str full_str;
218     WCHAR *full_name;
219
220     if (!(full_name = build_desktop_name( name, winstation, &full_str ))) return NULL;
221
222     if ((desktop = create_named_object( winstation_namespace, &desktop_ops, &full_str, attr )))
223     {
224         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
225         {
226             /* initialize it if it didn't already exist */
227             desktop->flags = flags;
228             desktop->winstation = (struct winstation *)grab_object( winstation );
229             desktop->top_window = NULL;
230             desktop->global_hooks = NULL;
231             desktop->close_timeout = NULL;
232             desktop->users = 0;
233             list_add_tail( &winstation->desktops, &desktop->entry );
234         }
235     }
236     free( full_name );
237     return desktop;
238 }
239
240 static void desktop_dump( struct object *obj, int verbose )
241 {
242     struct desktop *desktop = (struct desktop *)obj;
243
244     fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
245              desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
246     dump_object_name( &desktop->obj );
247     fputc( '\n', stderr );
248 }
249
250 static struct object_type *desktop_get_type( struct object *obj )
251 {
252     static const WCHAR name[] = {'D','e','s','k','t','o','p'};
253     static const struct unicode_str str = { name, sizeof(name) };
254     return get_object_type( &str );
255 }
256
257 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
258 {
259     struct thread *thread;
260
261     /* check if the handle is currently used by the process or one of its threads */
262     if (process->desktop == handle) return 0;
263     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
264         if (thread->desktop == handle) return 0;
265     return 1;
266 }
267
268 static void desktop_destroy( struct object *obj )
269 {
270     struct desktop *desktop = (struct desktop *)obj;
271
272     if (desktop->top_window) destroy_window( desktop->top_window );
273     if (desktop->global_hooks) release_object( desktop->global_hooks );
274     if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
275     list_remove( &desktop->entry );
276     release_object( desktop->winstation );
277 }
278
279 static unsigned int desktop_map_access( struct object *obj, unsigned int access )
280 {
281     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | DESKTOP_READOBJECTS | DESKTOP_ENUMERATE;
282     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
283                                             DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
284                                             DESKTOP_WRITEOBJECTS;
285     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP;
286     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS;
287     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
288 }
289
290 /* retrieve the thread desktop, checking the handle access rights */
291 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
292 {
293     return get_desktop_obj( thread->process, thread->desktop, access );
294 }
295
296 /* set the process default desktop handle */
297 void set_process_default_desktop( struct process *process, struct desktop *desktop,
298                                   obj_handle_t handle )
299 {
300     struct thread *thread;
301     struct desktop *old_desktop;
302
303     if (process->desktop == handle) return;  /* nothing to do */
304
305     if (!(old_desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error();
306     process->desktop = handle;
307
308     /* set desktop for threads that don't have one yet */
309     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
310         if (!thread->desktop) thread->desktop = handle;
311
312     desktop->users++;
313     if (desktop->close_timeout)
314     {
315         remove_timeout_user( desktop->close_timeout );
316         desktop->close_timeout = NULL;
317     }
318     if (old_desktop)
319     {
320         old_desktop->users--;
321         release_object( old_desktop );
322     }
323 }
324
325 /* connect a process to its window station */
326 void connect_process_winstation( struct process *process, struct thread *parent )
327 {
328     struct winstation *winstation = NULL;
329     struct desktop *desktop = NULL;
330     obj_handle_t handle;
331
332     /* check for an inherited winstation handle (don't ask...) */
333     if ((handle = find_inherited_handle( process, &winstation_ops )))
334     {
335         winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
336     }
337     else if (parent && parent->process->winstation)
338     {
339         handle = duplicate_handle( parent->process, parent->process->winstation,
340                                    process, 0, 0, DUP_HANDLE_SAME_ACCESS );
341         winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
342     }
343     if (!winstation) goto done;
344     process->winstation = handle;
345
346     if ((handle = find_inherited_handle( process, &desktop_ops )))
347     {
348         desktop = get_desktop_obj( process, handle, 0 );
349         if (!desktop || desktop->winstation != winstation) goto done;
350     }
351     else if (parent && parent->desktop)
352     {
353         desktop = get_desktop_obj( parent->process, parent->desktop, 0 );
354         if (!desktop || desktop->winstation != winstation) goto done;
355         handle = duplicate_handle( parent->process, parent->desktop,
356                                    process, 0, 0, DUP_HANDLE_SAME_ACCESS );
357     }
358
359     if (handle) set_process_default_desktop( process, desktop, handle );
360
361 done:
362     if (desktop) release_object( desktop );
363     if (winstation) release_object( winstation );
364     clear_error();
365 }
366
367 static void close_desktop_timeout( void *private )
368 {
369     struct desktop *desktop = private;
370
371     desktop->close_timeout = NULL;
372     unlink_named_object( &desktop->obj );  /* make sure no other process can open it */
373     close_desktop_window( desktop );  /* and signal the owner to quit */
374 }
375
376 /* close the desktop of a given process */
377 void close_process_desktop( struct process *process )
378 {
379     struct desktop *desktop;
380
381     if (process->desktop && (desktop = get_desktop_obj( process, process->desktop, 0 )))
382     {
383         assert( desktop->users > 0 );
384         desktop->users--;
385         /* if we have one remaining user, it has to be the manager of the desktop window */
386         if (desktop->users == 1 && get_top_window_owner( desktop ))
387         {
388             assert( !desktop->close_timeout );
389             desktop->close_timeout = add_timeout_user( -TICKS_PER_SEC, close_desktop_timeout, desktop );
390         }
391         release_object( desktop );
392     }
393     clear_error();  /* ignore errors */
394 }
395
396 /* close the desktop of a given thread */
397 void close_thread_desktop( struct thread *thread )
398 {
399     obj_handle_t handle = thread->desktop;
400
401     thread->desktop = 0;
402     if (handle) close_handle( thread->process, handle );
403     clear_error();  /* ignore errors */
404 }
405
406 /* set the reply data from the object name */
407 static void set_reply_data_obj_name( struct object *obj )
408 {
409     data_size_t len;
410     const WCHAR *ptr, *name = get_object_name( obj, &len );
411
412     /* if there is a backslash return the part of the name after it */
413     if (name && (ptr = memchrW( name, '\\', len/sizeof(WCHAR) )))
414     {
415         len -= (ptr + 1 - name) * sizeof(WCHAR);
416         name = ptr + 1;
417     }
418     if (name) set_reply_data( name, min( len, get_reply_max_size() ));
419 }
420
421 /* create a window station */
422 DECL_HANDLER(create_winstation)
423 {
424     struct winstation *winstation;
425     struct unicode_str name;
426
427     reply->handle = 0;
428     get_req_unicode_str( &name );
429     if ((winstation = create_winstation( &name, req->attributes, req->flags )))
430     {
431         reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
432         release_object( winstation );
433     }
434 }
435
436 /* open a handle to a window station */
437 DECL_HANDLER(open_winstation)
438 {
439     struct unicode_str name;
440
441     get_req_unicode_str( &name );
442     if (winstation_namespace)
443         reply->handle = open_object( winstation_namespace, &name, &winstation_ops, req->access,
444                                      req->attributes );
445     else
446         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
447 }
448
449
450 /* close a window station */
451 DECL_HANDLER(close_winstation)
452 {
453     struct winstation *winstation;
454
455     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
456                                                            0, &winstation_ops )))
457     {
458         if (!close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
459         release_object( winstation );
460     }
461 }
462
463
464 /* get the process current window station */
465 DECL_HANDLER(get_process_winstation)
466 {
467     reply->handle = current->process->winstation;
468 }
469
470
471 /* set the process current window station */
472 DECL_HANDLER(set_process_winstation)
473 {
474     struct winstation *winstation;
475
476     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
477                                                            0, &winstation_ops )))
478     {
479         /* FIXME: should we close the old one? */
480         current->process->winstation = req->handle;
481         release_object( winstation );
482     }
483 }
484
485 /* create a desktop */
486 DECL_HANDLER(create_desktop)
487 {
488     struct desktop *desktop;
489     struct winstation *winstation;
490     struct unicode_str name;
491
492     reply->handle = 0;
493     get_req_unicode_str( &name );
494     if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
495     {
496         if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
497         {
498             reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
499             release_object( desktop );
500         }
501         release_object( winstation );
502     }
503 }
504
505 /* open a handle to a desktop */
506 DECL_HANDLER(open_desktop)
507 {
508     struct winstation *winstation;
509     struct unicode_str name;
510
511     get_req_unicode_str( &name );
512     if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
513     {
514         struct unicode_str full_str;
515         WCHAR *full_name;
516
517         if ((full_name = build_desktop_name( &name, winstation, &full_str )))
518         {
519             reply->handle = open_object( winstation_namespace, &full_str, &desktop_ops, req->access,
520                                          req->attributes );
521             free( full_name );
522         }
523         release_object( winstation );
524     }
525 }
526
527
528 /* close a desktop */
529 DECL_HANDLER(close_desktop)
530 {
531     struct desktop *desktop;
532
533     /* make sure it is a desktop handle */
534     if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
535                                                      0, &desktop_ops )))
536     {
537         if (!close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
538         release_object( desktop );
539     }
540 }
541
542
543 /* get the thread current desktop */
544 DECL_HANDLER(get_thread_desktop)
545 {
546     struct thread *thread;
547
548     if (!(thread = get_thread_from_id( req->tid ))) return;
549     reply->handle = thread->desktop;
550     release_object( thread );
551 }
552
553
554 /* set the thread current desktop */
555 DECL_HANDLER(set_thread_desktop)
556 {
557     struct desktop *old_desktop, *new_desktop;
558     struct winstation *winstation;
559
560     if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
561         return;
562
563     if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
564     {
565         release_object( winstation );
566         return;
567     }
568     if (new_desktop->winstation != winstation)
569     {
570         set_error( STATUS_ACCESS_DENIED );
571         release_object( new_desktop );
572         release_object( winstation );
573         return;
574     }
575
576     /* check if we are changing to a new desktop */
577
578     if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
579         clear_error();  /* ignore error */
580
581     /* when changing desktop, we can't have any users on the current one */
582     if (old_desktop != new_desktop && current->desktop_users > 0)
583         set_error( STATUS_DEVICE_BUSY );
584     else
585         current->desktop = req->handle;  /* FIXME: should we close the old one? */
586
587     if (!current->process->desktop)
588         set_process_default_desktop( current->process, new_desktop, req->handle );
589
590     if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
591
592     if (old_desktop) release_object( old_desktop );
593     release_object( new_desktop );
594     release_object( winstation );
595 }
596
597
598 /* get/set information about a user object (window station or desktop) */
599 DECL_HANDLER(set_user_object_info)
600 {
601     struct object *obj;
602
603     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
604
605     if (obj->ops == &desktop_ops)
606     {
607         struct desktop *desktop = (struct desktop *)obj;
608         reply->is_desktop = 1;
609         reply->old_obj_flags = desktop->flags;
610         if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
611     }
612     else if (obj->ops == &winstation_ops)
613     {
614         struct winstation *winstation = (struct winstation *)obj;
615         reply->is_desktop = 0;
616         reply->old_obj_flags = winstation->flags;
617         if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
618     }
619     else
620     {
621         set_error( STATUS_OBJECT_TYPE_MISMATCH );
622         release_object( obj );
623         return;
624     }
625     if (get_reply_max_size()) set_reply_data_obj_name( obj );
626     release_object( obj );
627 }
628
629
630 /* enumerate window stations */
631 DECL_HANDLER(enum_winstation)
632 {
633     unsigned int index = req->index;
634     obj_handle_t handle;
635     struct object *obj;
636
637     while ((handle = enumerate_handles( current->process, &winstation_ops, &index )))
638     {
639         if (!(obj = get_handle_obj( current->process, handle, WINSTA_ENUMERATE, &winstation_ops )))
640             continue;
641         set_reply_data_obj_name( obj );
642         release_object( obj );
643         clear_error();
644         reply->next = index;
645         return;
646     }
647     set_error( STATUS_NO_MORE_ENTRIES );
648 }
649
650
651 /* enumerate desktops */
652 DECL_HANDLER(enum_desktop)
653 {
654     struct winstation *winstation;
655     struct desktop *desktop;
656     unsigned int index = req->index;
657     obj_handle_t handle;
658
659     if (!(winstation = (struct winstation *)get_handle_obj( current->process, req->winstation,
660                                                             WINSTA_ENUMDESKTOPS, &winstation_ops )))
661         return;
662
663     while ((handle = enumerate_handles( current->process, &desktop_ops, &index )))
664     {
665         if (!(desktop = get_desktop_obj( current->process, handle, DESKTOP_ENUMERATE )))
666             continue;
667
668         if (desktop->winstation == winstation)
669         {
670             set_reply_data_obj_name( &desktop->obj );
671             release_object( desktop );
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 }