Be more strict and verbose while testing
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winternl.h"
31
32 #include "object.h"
33 #include "handle.h"
34 #include "request.h"
35 #include "process.h"
36 #include "user.h"
37 #include "wine/unicode.h"
38
39
40 static struct list winstation_list = LIST_INIT(winstation_list);
41 static struct winstation *interactive_winstation;
42 static struct namespace *winstation_namespace;
43
44 static void winstation_dump( struct object *obj, int verbose );
45 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
46 static void winstation_destroy( struct object *obj );
47 static void desktop_dump( struct object *obj, int verbose );
48 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
49 static void desktop_destroy( struct object *obj );
50
51 static const struct object_ops winstation_ops =
52 {
53     sizeof(struct winstation),    /* size */
54     winstation_dump,              /* dump */
55     no_add_queue,                 /* add_queue */
56     NULL,                         /* remove_queue */
57     NULL,                         /* signaled */
58     NULL,                         /* satisfied */
59     no_signal,                    /* signal */
60     no_get_fd,                    /* get_fd */
61     no_lookup_name,               /* lookup_name */
62     winstation_close_handle,      /* close_handle */
63     winstation_destroy            /* destroy */
64 };
65
66
67 static const struct object_ops desktop_ops =
68 {
69     sizeof(struct desktop),       /* size */
70     desktop_dump,                 /* dump */
71     no_add_queue,                 /* add_queue */
72     NULL,                         /* remove_queue */
73     NULL,                         /* signaled */
74     NULL,                         /* satisfied */
75     no_signal,                    /* signal */
76     no_get_fd,                    /* get_fd */
77     no_lookup_name,               /* lookup_name */
78     desktop_close_handle,         /* close_handle */
79     desktop_destroy               /* destroy */
80 };
81
82 #define DESKTOP_ALL_ACCESS 0x01ff
83
84 /* create a winstation object */
85 static struct winstation *create_winstation( const struct unicode_str *name, unsigned int attr,
86                                              unsigned int flags )
87 {
88     struct winstation *winstation;
89
90     if (!winstation_namespace && !(winstation_namespace = create_namespace( 7 )))
91         return NULL;
92
93     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))  /* no backslash allowed in name */
94     {
95         set_error( STATUS_INVALID_PARAMETER );
96         return NULL;
97     }
98
99     if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, attr )))
100     {
101         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
102         {
103             /* initialize it if it didn't already exist */
104             winstation->flags = flags;
105             winstation->clipboard = NULL;
106             winstation->atom_table = NULL;
107             list_add_tail( &winstation_list, &winstation->entry );
108             list_init( &winstation->desktops );
109         }
110     }
111     return winstation;
112 }
113
114 static void winstation_dump( struct object *obj, int verbose )
115 {
116     struct winstation *winstation = (struct winstation *)obj;
117
118     fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p ",
119              winstation->flags, winstation->clipboard, winstation->atom_table );
120     dump_object_name( &winstation->obj );
121     fputc( '\n', stderr );
122 }
123
124 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
125 {
126     return (process->winstation != handle);
127 }
128
129 static void winstation_destroy( struct object *obj )
130 {
131     struct winstation *winstation = (struct winstation *)obj;
132
133     if (winstation == interactive_winstation) interactive_winstation = NULL;
134     list_remove( &winstation->entry );
135     if (winstation->clipboard) release_object( winstation->clipboard );
136     if (winstation->atom_table) release_object( winstation->atom_table );
137 }
138
139 /* retrieve the process window station, checking the handle access rights */
140 struct winstation *get_process_winstation( struct process *process, unsigned int access )
141 {
142     return (struct winstation *)get_handle_obj( process, process->winstation,
143                                                 access, &winstation_ops );
144 }
145
146 /* build the full name of a desktop object */
147 static WCHAR *build_desktop_name( const struct unicode_str *name,
148                                   struct winstation *winstation, struct unicode_str *res )
149 {
150     const WCHAR *winstation_name;
151     WCHAR *full_name;
152     size_t winstation_len;
153
154     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))
155     {
156         set_error( STATUS_INVALID_PARAMETER );
157         return NULL;
158     }
159
160     if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
161         winstation_len = 0;
162
163     res->len = winstation_len + name->len + sizeof(WCHAR);
164     if (!(full_name = mem_alloc( res->len ))) return NULL;
165     memcpy( full_name, winstation_name, winstation_len );
166     full_name[winstation_len / sizeof(WCHAR)] = '\\';
167     memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name->str, name->len );
168     res->str = full_name;
169     return full_name;
170 }
171
172 /* retrieve a pointer to a desktop object */
173 inline static struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
174                                                unsigned int access )
175 {
176     return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
177 }
178
179 /* create a desktop object */
180 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
181                                        unsigned int flags, struct winstation *winstation )
182 {
183     struct desktop *desktop;
184     struct unicode_str full_str;
185     WCHAR *full_name;
186
187     if (!(full_name = build_desktop_name( name, winstation, &full_str ))) return NULL;
188
189     if ((desktop = create_named_object( winstation_namespace, &desktop_ops, &full_str, attr )))
190     {
191         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
192         {
193             /* initialize it if it didn't already exist */
194             desktop->flags = flags;
195             desktop->winstation = (struct winstation *)grab_object( winstation );
196             desktop->top_window = NULL;
197             desktop->global_hooks = NULL;
198             list_add_tail( &winstation->desktops, &desktop->entry );
199         }
200     }
201     free( full_name );
202     return desktop;
203 }
204
205 static void desktop_dump( struct object *obj, int verbose )
206 {
207     struct desktop *desktop = (struct desktop *)obj;
208
209     fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
210              desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
211     dump_object_name( &desktop->obj );
212     fputc( '\n', stderr );
213 }
214
215 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
216 {
217     struct thread *thread;
218
219     /* check if the handle is currently used by the process or one of its threads */
220     if (process->desktop == handle) return 0;
221     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
222         if (thread->desktop == handle) return 0;
223     return 1;
224 }
225
226 static void desktop_destroy( struct object *obj )
227 {
228     struct desktop *desktop = (struct desktop *)obj;
229
230     if (desktop->top_window) destroy_window( desktop->top_window );
231     if (desktop->global_hooks) release_object( desktop->global_hooks );
232     list_remove( &desktop->entry );
233     release_object( desktop->winstation );
234 }
235
236 /* retrieve the thread desktop, checking the handle access rights */
237 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
238 {
239     return get_desktop_obj( thread->process, thread->desktop, access );
240 }
241
242 /* connect a process to its window station */
243 void connect_process_winstation( struct process *process, const struct unicode_str *name )
244 {
245     struct winstation *winstation;
246
247     if (process->winstation) return;  /* already has one */
248
249     /* check for an inherited winstation handle (don't ask...) */
250     if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
251
252     if (name)
253     {
254         winstation = create_winstation( name, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0 );
255     }
256     else
257     {
258         if (!interactive_winstation)
259         {
260             static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
261             static const struct unicode_str winsta0 = { winsta0W, sizeof(winsta0W) };
262             interactive_winstation = create_winstation( &winsta0, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0 );
263             winstation = interactive_winstation;
264         }
265         else winstation = (struct winstation *)grab_object( interactive_winstation );
266     }
267     if (winstation)
268     {
269         process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, FALSE );
270         release_object( winstation );
271     }
272     clear_error();  /* ignore errors */
273 }
274
275 /* connect a process to its main desktop */
276 void connect_process_desktop( struct process *process, const struct unicode_str *name )
277 {
278     struct desktop *desktop;
279     struct winstation *winstation;
280
281     if (process->desktop) return;  /* already has one */
282
283     if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
284     {
285         static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
286         static const struct unicode_str default_str = { defaultW, sizeof(defaultW) };
287
288         if (!name) name = &default_str;
289         if ((desktop = create_desktop( name, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0, winstation )))
290         {
291             process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, FALSE );
292             release_object( desktop );
293         }
294         release_object( winstation );
295     }
296     clear_error();  /* ignore errors */
297 }
298
299 /* close the desktop of a given thread */
300 void close_thread_desktop( struct thread *thread )
301 {
302     obj_handle_t handle = thread->desktop;
303
304     thread->desktop = 0;
305     if (handle) close_handle( thread->process, handle, NULL );
306     clear_error();  /* ignore errors */
307 }
308
309
310 /* create a window station */
311 DECL_HANDLER(create_winstation)
312 {
313     struct winstation *winstation;
314     struct unicode_str name;
315
316     reply->handle = 0;
317     get_req_unicode_str( &name );
318     if ((winstation = create_winstation( &name, req->attributes, req->flags )))
319     {
320         reply->handle = alloc_handle( current->process, winstation, req->access,
321                                       req->attributes & OBJ_INHERIT );
322         release_object( winstation );
323     }
324 }
325
326 /* open a handle to a window station */
327 DECL_HANDLER(open_winstation)
328 {
329     struct unicode_str name;
330
331     get_req_unicode_str( &name );
332     if (winstation_namespace)
333         reply->handle = open_object( winstation_namespace, &name, &winstation_ops, req->access,
334                                      req->attributes );
335     else
336         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
337 }
338
339
340 /* close a window station */
341 DECL_HANDLER(close_winstation)
342 {
343     struct winstation *winstation;
344
345     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
346                                                            0, &winstation_ops )))
347     {
348         if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_ACCESS_DENIED );
349         release_object( winstation );
350     }
351 }
352
353
354 /* get the process current window station */
355 DECL_HANDLER(get_process_winstation)
356 {
357     reply->handle = current->process->winstation;
358 }
359
360
361 /* set the process current window station */
362 DECL_HANDLER(set_process_winstation)
363 {
364     struct winstation *winstation;
365
366     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
367                                                            0, &winstation_ops )))
368     {
369         /* FIXME: should we close the old one? */
370         current->process->winstation = req->handle;
371         release_object( winstation );
372     }
373 }
374
375 /* create a desktop */
376 DECL_HANDLER(create_desktop)
377 {
378     struct desktop *desktop;
379     struct winstation *winstation;
380     struct unicode_str name;
381
382     reply->handle = 0;
383     get_req_unicode_str( &name );
384     if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
385     {
386         if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
387         {
388             reply->handle = alloc_handle( current->process, desktop, req->access,
389                                           req->attributes & OBJ_INHERIT );
390             release_object( desktop );
391         }
392         release_object( winstation );
393     }
394 }
395
396 /* open a handle to a desktop */
397 DECL_HANDLER(open_desktop)
398 {
399     struct winstation *winstation;
400     struct unicode_str name;
401
402     get_req_unicode_str( &name );
403     if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
404     {
405         struct unicode_str full_str;
406         WCHAR *full_name;
407
408         if ((full_name = build_desktop_name( &name, winstation, &full_str )))
409         {
410             reply->handle = open_object( winstation_namespace, &full_str, &desktop_ops, req->access,
411                                          req->attributes );
412             free( full_name );
413         }
414         release_object( winstation );
415     }
416 }
417
418
419 /* close a desktop */
420 DECL_HANDLER(close_desktop)
421 {
422     struct desktop *desktop;
423
424     /* make sure it is a desktop handle */
425     if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
426                                                      0, &desktop_ops )))
427     {
428         if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
429         release_object( desktop );
430     }
431 }
432
433
434 /* get the thread current desktop */
435 DECL_HANDLER(get_thread_desktop)
436 {
437     struct thread *thread;
438
439     if (!(thread = get_thread_from_id( req->tid ))) return;
440     reply->handle = thread->desktop;
441     release_object( thread );
442 }
443
444
445 /* set the thread current desktop */
446 DECL_HANDLER(set_thread_desktop)
447 {
448     struct desktop *old_desktop, *new_desktop;
449     struct winstation *winstation;
450
451     if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
452         return;
453
454     if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
455     {
456         release_object( winstation );
457         return;
458     }
459     if (new_desktop->winstation != winstation)
460     {
461         set_error( STATUS_ACCESS_DENIED );
462         release_object( new_desktop );
463         release_object( winstation );
464         return;
465     }
466
467     /* check if we are changing to a new desktop */
468
469     if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
470         clear_error();  /* ignore error */
471
472     /* when changing desktop, we can't have any users on the current one */
473     if (old_desktop != new_desktop && current->desktop_users > 0)
474         set_error( STATUS_DEVICE_BUSY );
475     else
476         current->desktop = req->handle;  /* FIXME: should we close the old one? */
477
478     if (old_desktop != new_desktop) detach_thread_input( current );
479
480     if (old_desktop) release_object( old_desktop );
481     release_object( new_desktop );
482     release_object( winstation );
483 }
484
485
486 /* get/set information about a user object (window station or desktop) */
487 DECL_HANDLER(set_user_object_info)
488 {
489     struct object *obj;
490
491     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
492
493     if (obj->ops == &desktop_ops)
494     {
495         struct desktop *desktop = (struct desktop *)obj;
496         reply->is_desktop = 1;
497         reply->old_obj_flags = desktop->flags;
498         if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
499     }
500     else if (obj->ops == &winstation_ops)
501     {
502         struct winstation *winstation = (struct winstation *)obj;
503         reply->is_desktop = 0;
504         reply->old_obj_flags = winstation->flags;
505         if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
506     }
507     else
508     {
509         set_error( STATUS_OBJECT_TYPE_MISMATCH );
510         release_object( obj );
511         return;
512     }
513     if (get_reply_max_size())
514     {
515         size_t len;
516         const WCHAR *ptr, *name = get_object_name( obj, &len );
517
518         /* if there is a backslash return the part of the name after it */
519         if (name && (ptr = memchrW( name, '\\', len )))
520         {
521             len -= (ptr + 1 - name) * sizeof(WCHAR);
522             name = ptr + 1;
523         }
524         if (name) set_reply_data( name, min( len, get_reply_max_size() ));
525     }
526     release_object( obj );
527 }