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