2 * Server-side window stations and desktops handling
4 * Copyright (C) 2002, 2005 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
28 #define WIN32_NO_STATUS
39 #include "wine/unicode.h"
42 static struct list winstation_list
= LIST_INIT(winstation_list
);
43 static struct winstation
*interactive_winstation
;
44 static struct namespace *winstation_namespace
;
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 void desktop_dump( struct object
*obj
, int verbose
);
50 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
51 static void desktop_destroy( struct object
*obj
);
53 static const struct object_ops winstation_ops
=
55 sizeof(struct winstation
), /* size */
56 winstation_dump
, /* dump */
57 no_add_queue
, /* add_queue */
58 NULL
, /* remove_queue */
61 no_signal
, /* signal */
62 no_get_fd
, /* get_fd */
63 no_lookup_name
, /* lookup_name */
64 winstation_close_handle
, /* close_handle */
65 winstation_destroy
/* destroy */
69 static const struct object_ops desktop_ops
=
71 sizeof(struct desktop
), /* size */
72 desktop_dump
, /* dump */
73 no_add_queue
, /* add_queue */
74 NULL
, /* remove_queue */
77 no_signal
, /* signal */
78 no_get_fd
, /* get_fd */
79 no_lookup_name
, /* lookup_name */
80 desktop_close_handle
, /* close_handle */
81 desktop_destroy
/* destroy */
84 #define DESKTOP_ALL_ACCESS 0x01ff
86 /* create a winstation object */
87 static struct winstation
*create_winstation( const struct unicode_str
*name
, unsigned int attr
,
90 struct winstation
*winstation
;
92 if (!winstation_namespace
&& !(winstation_namespace
= create_namespace( 7 )))
95 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) )) /* no backslash allowed in name */
97 set_error( STATUS_INVALID_PARAMETER
);
101 if ((winstation
= create_named_object( winstation_namespace
, &winstation_ops
, name
, attr
)))
103 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
105 /* initialize it if it didn't already exist */
106 winstation
->flags
= flags
;
107 winstation
->clipboard
= NULL
;
108 winstation
->atom_table
= NULL
;
109 list_add_tail( &winstation_list
, &winstation
->entry
);
110 list_init( &winstation
->desktops
);
116 static void winstation_dump( struct object
*obj
, int verbose
)
118 struct winstation
*winstation
= (struct winstation
*)obj
;
120 fprintf( stderr
, "Winstation flags=%x clipboard=%p atoms=%p ",
121 winstation
->flags
, winstation
->clipboard
, winstation
->atom_table
);
122 dump_object_name( &winstation
->obj
);
123 fputc( '\n', stderr
);
126 static int winstation_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
128 return (process
->winstation
!= handle
);
131 static void winstation_destroy( struct object
*obj
)
133 struct winstation
*winstation
= (struct winstation
*)obj
;
135 if (winstation
== interactive_winstation
) interactive_winstation
= NULL
;
136 list_remove( &winstation
->entry
);
137 if (winstation
->clipboard
) release_object( winstation
->clipboard
);
138 if (winstation
->atom_table
) release_object( winstation
->atom_table
);
141 /* retrieve the process window station, checking the handle access rights */
142 struct winstation
*get_process_winstation( struct process
*process
, unsigned int access
)
144 return (struct winstation
*)get_handle_obj( process
, process
->winstation
,
145 access
, &winstation_ops
);
148 /* build the full name of a desktop object */
149 static WCHAR
*build_desktop_name( const struct unicode_str
*name
,
150 struct winstation
*winstation
, struct unicode_str
*res
)
152 const WCHAR
*winstation_name
;
154 size_t winstation_len
;
156 if (memchrW( name
->str
, '\\', name
->len
/ sizeof(WCHAR
) ))
158 set_error( STATUS_INVALID_PARAMETER
);
162 if (!(winstation_name
= get_object_name( &winstation
->obj
, &winstation_len
)))
165 res
->len
= winstation_len
+ name
->len
+ sizeof(WCHAR
);
166 if (!(full_name
= mem_alloc( res
->len
))) return NULL
;
167 memcpy( full_name
, winstation_name
, winstation_len
);
168 full_name
[winstation_len
/ sizeof(WCHAR
)] = '\\';
169 memcpy( full_name
+ winstation_len
/ sizeof(WCHAR
) + 1, name
->str
, name
->len
);
170 res
->str
= full_name
;
174 /* retrieve a pointer to a desktop object */
175 inline static struct desktop
*get_desktop_obj( struct process
*process
, obj_handle_t handle
,
176 unsigned int access
)
178 return (struct desktop
*)get_handle_obj( process
, handle
, access
, &desktop_ops
);
181 /* create a desktop object */
182 static struct desktop
*create_desktop( const struct unicode_str
*name
, unsigned int attr
,
183 unsigned int flags
, struct winstation
*winstation
)
185 struct desktop
*desktop
;
186 struct unicode_str full_str
;
189 if (!(full_name
= build_desktop_name( name
, winstation
, &full_str
))) return NULL
;
191 if ((desktop
= create_named_object( winstation_namespace
, &desktop_ops
, &full_str
, attr
)))
193 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
195 /* initialize it if it didn't already exist */
196 desktop
->flags
= flags
;
197 desktop
->winstation
= (struct winstation
*)grab_object( winstation
);
198 desktop
->top_window
= NULL
;
199 desktop
->global_hooks
= NULL
;
200 list_add_tail( &winstation
->desktops
, &desktop
->entry
);
207 static void desktop_dump( struct object
*obj
, int verbose
)
209 struct desktop
*desktop
= (struct desktop
*)obj
;
211 fprintf( stderr
, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
212 desktop
->flags
, desktop
->winstation
, desktop
->top_window
, desktop
->global_hooks
);
213 dump_object_name( &desktop
->obj
);
214 fputc( '\n', stderr
);
217 static int desktop_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
219 struct thread
*thread
;
221 /* check if the handle is currently used by the process or one of its threads */
222 if (process
->desktop
== handle
) return 0;
223 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
224 if (thread
->desktop
== handle
) return 0;
228 static void desktop_destroy( struct object
*obj
)
230 struct desktop
*desktop
= (struct desktop
*)obj
;
232 if (desktop
->top_window
) destroy_window( desktop
->top_window
);
233 if (desktop
->global_hooks
) release_object( desktop
->global_hooks
);
234 list_remove( &desktop
->entry
);
235 release_object( desktop
->winstation
);
238 /* retrieve the thread desktop, checking the handle access rights */
239 struct desktop
*get_thread_desktop( struct thread
*thread
, unsigned int access
)
241 return get_desktop_obj( thread
->process
, thread
->desktop
, access
);
244 /* connect a process to its window station */
245 void connect_process_winstation( struct process
*process
, const struct unicode_str
*name
)
247 struct winstation
*winstation
;
249 if (process
->winstation
) return; /* already has one */
251 /* check for an inherited winstation handle (don't ask...) */
252 if ((process
->winstation
= find_inherited_handle( process
, &winstation_ops
)) != 0) return;
256 winstation
= create_winstation( name
, OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
, 0 );
260 if (!interactive_winstation
)
262 static const WCHAR winsta0W
[] = {'W','i','n','S','t','a','0'};
263 static const struct unicode_str winsta0
= { winsta0W
, sizeof(winsta0W
) };
264 interactive_winstation
= create_winstation( &winsta0
, OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
, 0 );
265 winstation
= interactive_winstation
;
267 else winstation
= (struct winstation
*)grab_object( interactive_winstation
);
271 process
->winstation
= alloc_handle( process
, winstation
, WINSTA_ALL_ACCESS
, 0 );
272 release_object( winstation
);
274 clear_error(); /* ignore errors */
277 /* connect a process to its main desktop */
278 void connect_process_desktop( struct process
*process
, const struct unicode_str
*name
)
280 struct desktop
*desktop
;
281 struct winstation
*winstation
;
283 if (process
->desktop
) return; /* already has one */
285 if ((winstation
= get_process_winstation( process
, WINSTA_CREATEDESKTOP
)))
287 static const WCHAR defaultW
[] = {'D','e','f','a','u','l','t'};
288 static const struct unicode_str default_str
= { defaultW
, sizeof(defaultW
) };
290 if (!name
) name
= &default_str
;
291 if ((desktop
= create_desktop( name
, OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
, 0, winstation
)))
293 process
->desktop
= alloc_handle( process
, desktop
, DESKTOP_ALL_ACCESS
, 0 );
294 release_object( desktop
);
296 release_object( winstation
);
298 clear_error(); /* ignore errors */
301 /* close the desktop of a given thread */
302 void close_thread_desktop( struct thread
*thread
)
304 obj_handle_t handle
= thread
->desktop
;
307 if (handle
) close_handle( thread
->process
, handle
, NULL
);
308 clear_error(); /* ignore errors */
312 /* create a window station */
313 DECL_HANDLER(create_winstation
)
315 struct winstation
*winstation
;
316 struct unicode_str name
;
319 get_req_unicode_str( &name
);
320 if ((winstation
= create_winstation( &name
, req
->attributes
, req
->flags
)))
322 reply
->handle
= alloc_handle( current
->process
, winstation
, req
->access
, req
->attributes
);
323 release_object( winstation
);
327 /* open a handle to a window station */
328 DECL_HANDLER(open_winstation
)
330 struct unicode_str name
;
332 get_req_unicode_str( &name
);
333 if (winstation_namespace
)
334 reply
->handle
= open_object( winstation_namespace
, &name
, &winstation_ops
, req
->access
,
337 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
341 /* close a window station */
342 DECL_HANDLER(close_winstation
)
344 struct winstation
*winstation
;
346 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
347 0, &winstation_ops
)))
349 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_ACCESS_DENIED
);
350 release_object( winstation
);
355 /* get the process current window station */
356 DECL_HANDLER(get_process_winstation
)
358 reply
->handle
= current
->process
->winstation
;
362 /* set the process current window station */
363 DECL_HANDLER(set_process_winstation
)
365 struct winstation
*winstation
;
367 if ((winstation
= (struct winstation
*)get_handle_obj( current
->process
, req
->handle
,
368 0, &winstation_ops
)))
370 /* FIXME: should we close the old one? */
371 current
->process
->winstation
= req
->handle
;
372 release_object( winstation
);
376 /* create a desktop */
377 DECL_HANDLER(create_desktop
)
379 struct desktop
*desktop
;
380 struct winstation
*winstation
;
381 struct unicode_str name
;
384 get_req_unicode_str( &name
);
385 if ((winstation
= get_process_winstation( current
->process
, WINSTA_CREATEDESKTOP
)))
387 if ((desktop
= create_desktop( &name
, req
->attributes
, req
->flags
, winstation
)))
389 reply
->handle
= alloc_handle( current
->process
, desktop
, req
->access
, req
->attributes
);
390 release_object( desktop
);
392 release_object( winstation
);
396 /* open a handle to a desktop */
397 DECL_HANDLER(open_desktop
)
399 struct winstation
*winstation
;
400 struct unicode_str name
;
402 get_req_unicode_str( &name
);
403 if ((winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
405 struct unicode_str full_str
;
408 if ((full_name
= build_desktop_name( &name
, winstation
, &full_str
)))
410 reply
->handle
= open_object( winstation_namespace
, &full_str
, &desktop_ops
, req
->access
,
414 release_object( winstation
);
419 /* close a desktop */
420 DECL_HANDLER(close_desktop
)
422 struct desktop
*desktop
;
424 /* make sure it is a desktop handle */
425 if ((desktop
= (struct desktop
*)get_handle_obj( current
->process
, req
->handle
,
428 if (!close_handle( current
->process
, req
->handle
, NULL
)) set_error( STATUS_DEVICE_BUSY
);
429 release_object( desktop
);
434 /* get the thread current desktop */
435 DECL_HANDLER(get_thread_desktop
)
437 struct thread
*thread
;
439 if (!(thread
= get_thread_from_id( req
->tid
))) return;
440 reply
->handle
= thread
->desktop
;
441 release_object( thread
);
445 /* set the thread current desktop */
446 DECL_HANDLER(set_thread_desktop
)
448 struct desktop
*old_desktop
, *new_desktop
;
449 struct winstation
*winstation
;
451 if (!(winstation
= get_process_winstation( current
->process
, 0 /* FIXME: access rights? */ )))
454 if (!(new_desktop
= get_desktop_obj( current
->process
, req
->handle
, 0 )))
456 release_object( winstation
);
459 if (new_desktop
->winstation
!= winstation
)
461 set_error( STATUS_ACCESS_DENIED
);
462 release_object( new_desktop
);
463 release_object( winstation
);
467 /* check if we are changing to a new desktop */
469 if (!(old_desktop
= get_desktop_obj( current
->process
, current
->desktop
, 0)))
470 clear_error(); /* ignore error */
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
);
476 current
->desktop
= req
->handle
; /* FIXME: should we close the old one? */
478 if (old_desktop
!= new_desktop
) detach_thread_input( current
);
480 if (old_desktop
) release_object( old_desktop
);
481 release_object( new_desktop
);
482 release_object( winstation
);
486 /* get/set information about a user object (window station or desktop) */
487 DECL_HANDLER(set_user_object_info
)
491 if (!(obj
= get_handle_obj( current
->process
, req
->handle
, 0, NULL
))) return;
493 if (obj
->ops
== &desktop_ops
)
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
;
500 else if (obj
->ops
== &winstation_ops
)
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
;
509 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
510 release_object( obj
);
513 if (get_reply_max_size())
516 const WCHAR
*ptr
, *name
= get_object_name( obj
, &len
);
518 /* if there is a backslash return the part of the name after it */
519 if (name
&& (ptr
= memchrW( name
, '\\', len
)))
521 len
-= (ptr
+ 1 - name
) * sizeof(WCHAR
);
524 if (name
) set_reply_data( name
, min( len
, get_reply_max_size() ));
526 release_object( obj
);