Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side window class management |
| 3 | * |
| 4 | * Copyright (C) 2002 Mike McCormack |
| 5 | * Copyright (C) 2003 Alexandre Julliard |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "wine/port.h" |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 30 | #include "ntstatus.h" |
| 31 | #define WIN32_NO_STATUS |
| 32 | |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 33 | #include "wine/list.h" |
| 34 | |
| 35 | #include "request.h" |
| 36 | #include "object.h" |
| 37 | #include "process.h" |
| 38 | #include "user.h" |
Alexandre Julliard | c3ac57d | 2005-07-08 14:23:27 +0000 | [diff] [blame] | 39 | #include "winuser.h" |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 40 | #include "winternl.h" |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 41 | |
| 42 | struct window_class |
| 43 | { |
| 44 | struct list entry; /* entry in process list */ |
| 45 | struct process *process; /* process owning the class */ |
| 46 | int count; /* reference count */ |
| 47 | int local; /* local class? */ |
| 48 | atom_t atom; /* class atom */ |
| 49 | void *instance; /* module instance */ |
| 50 | unsigned int style; /* class style */ |
| 51 | int win_extra; /* number of window extra bytes */ |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 52 | void *client_ptr; /* pointer to class in client address space */ |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 53 | int nb_extra_bytes; /* number of extra bytes */ |
| 54 | char extra_bytes[1]; /* extra bytes storage */ |
| 55 | }; |
| 56 | |
| 57 | static struct window_class *create_class( struct process *process, int extra_bytes, int local ) |
| 58 | { |
| 59 | struct window_class *class; |
| 60 | |
| 61 | if (!(class = mem_alloc( sizeof(*class) + extra_bytes - 1 ))) return NULL; |
| 62 | |
| 63 | class->process = (struct process *)grab_object( process ); |
| 64 | class->count = 0; |
| 65 | class->local = local; |
| 66 | class->nb_extra_bytes = extra_bytes; |
| 67 | memset( class->extra_bytes, 0, extra_bytes ); |
| 68 | /* other fields are initialized by caller */ |
| 69 | |
| 70 | /* local classes have priority so we put them first in the list */ |
| 71 | if (local) list_add_head( &process->classes, &class->entry ); |
| 72 | else list_add_tail( &process->classes, &class->entry ); |
| 73 | return class; |
| 74 | } |
| 75 | |
| 76 | static void destroy_class( struct window_class *class ) |
| 77 | { |
| 78 | list_remove( &class->entry ); |
| 79 | release_object( class->process ); |
| 80 | free( class ); |
| 81 | } |
| 82 | |
| 83 | void destroy_process_classes( struct process *process ) |
| 84 | { |
| 85 | struct list *ptr; |
| 86 | |
| 87 | while ((ptr = list_head( &process->classes ))) |
| 88 | { |
| 89 | struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry ); |
| 90 | destroy_class( class ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static struct window_class *find_class( struct process *process, atom_t atom, void *instance ) |
| 95 | { |
| 96 | struct list *ptr; |
| 97 | |
| 98 | LIST_FOR_EACH( ptr, &process->classes ) |
| 99 | { |
| 100 | struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry ); |
| 101 | if (class->atom != atom) continue; |
| 102 | if (!instance || !class->local || class->instance == instance) return class; |
| 103 | } |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 107 | struct window_class *grab_class( struct process *process, atom_t atom, |
| 108 | void *instance, int *extra_bytes ) |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 109 | { |
| 110 | struct window_class *class = find_class( process, atom, instance ); |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 111 | if (class) |
| 112 | { |
| 113 | class->count++; |
| 114 | *extra_bytes = class->win_extra; |
| 115 | } |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 116 | else set_error( STATUS_INVALID_HANDLE ); |
| 117 | return class; |
| 118 | } |
| 119 | |
| 120 | void release_class( struct window_class *class ) |
| 121 | { |
| 122 | assert( class->count > 0 ); |
| 123 | class->count--; |
| 124 | } |
| 125 | |
Alexandre Julliard | 251be54 | 2006-03-06 20:37:52 +0100 | [diff] [blame] | 126 | int is_desktop_class( struct window_class *class ) |
| 127 | { |
| 128 | return (class->atom == DESKTOP_ATOM && !class->local); |
| 129 | } |
| 130 | |
Alexandre Julliard | 81e6edb | 2008-06-25 14:43:39 +0200 | [diff] [blame] | 131 | int is_hwnd_message_class( struct window_class *class ) |
| 132 | { |
| 133 | static const WCHAR messageW[] = {'M','e','s','s','a','g','e'}; |
| 134 | static const struct unicode_str name = { messageW, sizeof(messageW) }; |
| 135 | |
| 136 | return (!class->local && class->atom == find_global_atom( NULL, &name )); |
| 137 | } |
| 138 | |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 139 | atom_t get_class_atom( struct window_class *class ) |
| 140 | { |
| 141 | return class->atom; |
| 142 | } |
| 143 | |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 144 | void *get_class_client_ptr( struct window_class *class ) |
| 145 | { |
| 146 | return class->client_ptr; |
| 147 | } |
| 148 | |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 149 | /* create a window class */ |
| 150 | DECL_HANDLER(create_class) |
| 151 | { |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 152 | struct window_class *class; |
Alexandre Julliard | 25e070c | 2008-06-25 14:03:08 +0200 | [diff] [blame] | 153 | struct unicode_str name; |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 154 | atom_t atom; |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 155 | |
Alexandre Julliard | 25e070c | 2008-06-25 14:03:08 +0200 | [diff] [blame] | 156 | get_req_unicode_str( &name ); |
| 157 | if (name.len) |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 158 | { |
Alexandre Julliard | 25e070c | 2008-06-25 14:03:08 +0200 | [diff] [blame] | 159 | atom = add_global_atom( NULL, &name ); |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 160 | if (!atom) return; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | atom = req->atom; |
| 165 | if (!grab_global_atom( NULL, atom )) return; |
| 166 | } |
| 167 | |
| 168 | class = find_class( current->process, atom, req->instance ); |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 169 | if (class && !class->local == !req->local) |
| 170 | { |
| 171 | set_win32_error( ERROR_CLASS_ALREADY_EXISTS ); |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 172 | release_global_atom( NULL, atom ); |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 173 | return; |
| 174 | } |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 175 | if (req->extra < 0 || req->extra > 4096 || req->win_extra < 0 || req->win_extra > 4096) |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 176 | { |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 177 | /* don't allow stupid values here */ |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 178 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 179 | release_global_atom( NULL, atom ); |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 180 | return; |
| 181 | } |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 182 | |
| 183 | if (!(class = create_class( current->process, req->extra, req->local ))) |
| 184 | { |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 185 | release_global_atom( NULL, atom ); |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 186 | return; |
| 187 | } |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 188 | class->atom = atom; |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 189 | class->instance = req->instance; |
| 190 | class->style = req->style; |
| 191 | class->win_extra = req->win_extra; |
| 192 | class->client_ptr = req->client_ptr; |
Alexandre Julliard | 0762d98 | 2007-11-01 13:02:01 +0100 | [diff] [blame] | 193 | reply->atom = atom; |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* destroy a window class */ |
| 197 | DECL_HANDLER(destroy_class) |
| 198 | { |
Alexandre Julliard | b9b940f | 2007-11-01 15:28:30 +0100 | [diff] [blame] | 199 | struct window_class *class; |
Alexandre Julliard | 25e070c | 2008-06-25 14:03:08 +0200 | [diff] [blame] | 200 | struct unicode_str name; |
Alexandre Julliard | b9b940f | 2007-11-01 15:28:30 +0100 | [diff] [blame] | 201 | atom_t atom = req->atom; |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 202 | |
Alexandre Julliard | 25e070c | 2008-06-25 14:03:08 +0200 | [diff] [blame] | 203 | get_req_unicode_str( &name ); |
| 204 | if (name.len) atom = find_global_atom( NULL, &name ); |
Alexandre Julliard | b9b940f | 2007-11-01 15:28:30 +0100 | [diff] [blame] | 205 | |
| 206 | if (!(class = find_class( current->process, atom, req->instance ))) |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 207 | set_win32_error( ERROR_CLASS_DOES_NOT_EXIST ); |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 208 | else if (class->count) |
| 209 | set_win32_error( ERROR_CLASS_HAS_WINDOWS ); |
| 210 | else |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 211 | { |
| 212 | reply->client_ptr = class->client_ptr; |
Alexandre Julliard | 251be54 | 2006-03-06 20:37:52 +0100 | [diff] [blame] | 213 | destroy_class( class ); |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 214 | } |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | /* set some information in a class */ |
| 219 | DECL_HANDLER(set_class_info) |
| 220 | { |
| 221 | struct window_class *class = get_window_class( req->window ); |
| 222 | |
| 223 | if (!class) return; |
| 224 | |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 225 | if (req->flags && class->process != current->process) |
| 226 | { |
| 227 | set_error( STATUS_ACCESS_DENIED ); |
| 228 | return; |
| 229 | } |
| 230 | |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 231 | if (req->extra_size > sizeof(req->extra_value) || |
| 232 | req->extra_offset < -1 || |
| 233 | req->extra_offset > class->nb_extra_bytes - (int)req->extra_size) |
| 234 | { |
| 235 | set_win32_error( ERROR_INVALID_INDEX ); |
| 236 | return; |
| 237 | } |
Alexandre Julliard | bd13ab8 | 2003-12-11 05:34:53 +0000 | [diff] [blame] | 238 | if ((req->flags & SET_CLASS_WINEXTRA) && (req->win_extra < 0 || req->win_extra > 4096)) |
| 239 | { |
| 240 | set_error( STATUS_INVALID_PARAMETER ); |
| 241 | return; |
| 242 | } |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 243 | if (req->extra_offset != -1) |
| 244 | { |
| 245 | memcpy( &reply->old_extra_value, class->extra_bytes + req->extra_offset, req->extra_size ); |
| 246 | } |
| 247 | else if (req->flags & SET_CLASS_EXTRA) |
| 248 | { |
| 249 | set_win32_error( ERROR_INVALID_INDEX ); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | reply->old_atom = class->atom; |
| 254 | reply->old_style = class->style; |
| 255 | reply->old_extra = class->nb_extra_bytes; |
| 256 | reply->old_win_extra = class->win_extra; |
| 257 | reply->old_instance = class->instance; |
| 258 | |
| 259 | if (req->flags & SET_CLASS_ATOM) |
| 260 | { |
Alexandre Julliard | 9873494 | 2006-03-22 22:13:40 +0100 | [diff] [blame] | 261 | if (!grab_global_atom( NULL, req->atom )) return; |
| 262 | release_global_atom( NULL, class->atom ); |
Alexandre Julliard | bfce151 | 2003-12-10 04:08:06 +0000 | [diff] [blame] | 263 | class->atom = req->atom; |
| 264 | } |
| 265 | if (req->flags & SET_CLASS_STYLE) class->style = req->style; |
| 266 | if (req->flags & SET_CLASS_WINEXTRA) class->win_extra = req->win_extra; |
| 267 | if (req->flags & SET_CLASS_INSTANCE) class->instance = req->instance; |
| 268 | if (req->flags & SET_CLASS_EXTRA) memcpy( class->extra_bytes + req->extra_offset, |
| 269 | &req->extra_value, req->extra_size ); |
| 270 | } |