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