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