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