class.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include <assert.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "ntstatus.h"
  27. #define WIN32_NO_STATUS
  28. #include "wine/list.h"
  29. #include "request.h"
  30. #include "object.h"
  31. #include "process.h"
  32. #include "user.h"
  33. #include "winuser.h"
  34. #include "winternl.h"
  35. struct window_class
  36. {
  37. struct list entry; /* entry in process list */
  38. struct process *process; /* process owning the class */
  39. int count; /* reference count */
  40. int local; /* local class? */
  41. atom_t atom; /* class atom */
  42. atom_t base_atom; /* base class atom for versioned class */
  43. mod_handle_t instance; /* module instance */
  44. unsigned int style; /* class style */
  45. int win_extra; /* number of window extra bytes */
  46. client_ptr_t client_ptr; /* pointer to class in client address space */
  47. int nb_extra_bytes; /* number of extra bytes */
  48. char extra_bytes[1]; /* extra bytes storage */
  49. };
  50. static struct window_class *create_class( struct process *process, int extra_bytes, int local )
  51. {
  52. struct window_class *class;
  53. if (!(class = mem_alloc( sizeof(*class) + extra_bytes - 1 ))) return NULL;
  54. class->process = (struct process *)grab_object( process );
  55. class->count = 0;
  56. class->local = local;
  57. class->nb_extra_bytes = extra_bytes;
  58. memset( class->extra_bytes, 0, extra_bytes );
  59. /* other fields are initialized by caller */
  60. /* local classes have priority so we put them first in the list */
  61. if (local) list_add_head( &process->classes, &class->entry );
  62. else list_add_tail( &process->classes, &class->entry );
  63. return class;
  64. }
  65. static void destroy_class( struct window_class *class )
  66. {
  67. release_global_atom( NULL, class->atom );
  68. release_global_atom( NULL, class->base_atom );
  69. list_remove( &class->entry );
  70. release_object( class->process );
  71. free( class );
  72. }
  73. void destroy_process_classes( struct process *process )
  74. {
  75. struct list *ptr;
  76. while ((ptr = list_head( &process->classes )))
  77. {
  78. struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
  79. destroy_class( class );
  80. }
  81. }
  82. static struct window_class *find_class( struct process *process, atom_t atom, mod_handle_t instance )
  83. {
  84. struct list *ptr;
  85. LIST_FOR_EACH( ptr, &process->classes )
  86. {
  87. int is_win16;
  88. struct window_class *class = LIST_ENTRY( ptr, struct window_class, entry );
  89. if (class->atom != atom) continue;
  90. is_win16 = !(class->instance >> 16);
  91. if (!instance || !class->local || class->instance == instance ||
  92. (!is_win16 && ((class->instance & ~0xffff) == (instance & ~0xffff)))) return class;
  93. }
  94. return NULL;
  95. }
  96. struct window_class *grab_class( struct process *process, atom_t atom,
  97. mod_handle_t instance, int *extra_bytes )
  98. {
  99. struct window_class *class = find_class( process, atom, instance );
  100. if (class)
  101. {
  102. class->count++;
  103. *extra_bytes = class->win_extra;
  104. }
  105. else set_error( STATUS_INVALID_HANDLE );
  106. return class;
  107. }
  108. void release_class( struct window_class *class )
  109. {
  110. assert( class->count > 0 );
  111. class->count--;
  112. }
  113. int is_desktop_class( struct window_class *class )
  114. {
  115. return (class->atom == DESKTOP_ATOM && !class->local);
  116. }
  117. int is_hwnd_message_class( struct window_class *class )
  118. {
  119. static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
  120. static const struct unicode_str name = { messageW, sizeof(messageW) };
  121. return (!class->local && class->atom == find_global_atom( NULL, &name ));
  122. }
  123. atom_t get_class_atom( struct window_class *class )
  124. {
  125. return class->base_atom;
  126. }
  127. client_ptr_t get_class_client_ptr( struct window_class *class )
  128. {
  129. return class->client_ptr;
  130. }
  131. /* create a window class */
  132. DECL_HANDLER(create_class)
  133. {
  134. struct window_class *class;
  135. struct unicode_str name = get_req_unicode_str();
  136. atom_t atom, base_atom;
  137. if (name.len)
  138. {
  139. atom = add_global_atom( NULL, &name );
  140. if (!atom) return;
  141. if (req->name_offset && req->name_offset < name.len / sizeof(WCHAR))
  142. {
  143. name.str += req->name_offset;
  144. name.len -= req->name_offset * sizeof(WCHAR);
  145. base_atom = add_global_atom( NULL, &name );
  146. if (!base_atom)
  147. {
  148. release_global_atom( NULL, atom );
  149. return;
  150. }
  151. }
  152. else
  153. {
  154. base_atom = atom;
  155. grab_global_atom( NULL, atom );
  156. }
  157. }
  158. else
  159. {
  160. base_atom = atom = req->atom;
  161. if (!grab_global_atom( NULL, atom )) return;
  162. grab_global_atom( NULL, base_atom );
  163. }
  164. class = find_class( current->process, atom, req->instance );
  165. if (class && !class->local == !req->local)
  166. {
  167. set_win32_error( ERROR_CLASS_ALREADY_EXISTS );
  168. release_global_atom( NULL, atom );
  169. release_global_atom( NULL, base_atom );
  170. return;
  171. }
  172. if (req->extra < 0 || req->extra > 4096 || req->win_extra < 0 || req->win_extra > 4096)
  173. {
  174. /* don't allow stupid values here */
  175. set_error( STATUS_INVALID_PARAMETER );
  176. release_global_atom( NULL, atom );
  177. release_global_atom( NULL, base_atom );
  178. return;
  179. }
  180. if (!(class = create_class( current->process, req->extra, req->local )))
  181. {
  182. release_global_atom( NULL, atom );
  183. release_global_atom( NULL, base_atom );
  184. return;
  185. }
  186. class->atom = atom;
  187. class->base_atom = base_atom;
  188. class->instance = req->instance;
  189. class->style = req->style;
  190. class->win_extra = req->win_extra;
  191. class->client_ptr = req->client_ptr;
  192. reply->atom = atom;
  193. }
  194. /* destroy a window class */
  195. DECL_HANDLER(destroy_class)
  196. {
  197. struct window_class *class;
  198. struct unicode_str name = get_req_unicode_str();
  199. atom_t atom = req->atom;
  200. if (name.len) atom = find_global_atom( NULL, &name );
  201. if (!(class = find_class( current->process, atom, req->instance )))
  202. set_win32_error( ERROR_CLASS_DOES_NOT_EXIST );
  203. else if (class->count)
  204. set_win32_error( ERROR_CLASS_HAS_WINDOWS );
  205. else
  206. {
  207. reply->client_ptr = class->client_ptr;
  208. destroy_class( class );
  209. }
  210. }
  211. /* set some information in a class */
  212. DECL_HANDLER(set_class_info)
  213. {
  214. struct window_class *class = get_window_class( req->window );
  215. if (!class) return;
  216. if (req->flags && class->process != current->process)
  217. {
  218. set_error( STATUS_ACCESS_DENIED );
  219. return;
  220. }
  221. if (req->extra_size > sizeof(req->extra_value) ||
  222. req->extra_offset < -1 ||
  223. req->extra_offset > class->nb_extra_bytes - (int)req->extra_size)
  224. {
  225. set_win32_error( ERROR_INVALID_INDEX );
  226. return;
  227. }
  228. if ((req->flags & SET_CLASS_WINEXTRA) && (req->win_extra < 0 || req->win_extra > 4096))
  229. {
  230. set_error( STATUS_INVALID_PARAMETER );
  231. return;
  232. }
  233. if (req->extra_offset != -1)
  234. {
  235. memcpy( &reply->old_extra_value, class->extra_bytes + req->extra_offset, req->extra_size );
  236. }
  237. else if (req->flags & SET_CLASS_EXTRA)
  238. {
  239. set_win32_error( ERROR_INVALID_INDEX );
  240. return;
  241. }
  242. reply->old_atom = class->atom;
  243. reply->old_style = class->style;
  244. reply->old_extra = class->nb_extra_bytes;
  245. reply->old_win_extra = class->win_extra;
  246. reply->old_instance = class->instance;
  247. reply->base_atom = class->base_atom;
  248. if (req->flags & SET_CLASS_ATOM)
  249. {
  250. if (!grab_global_atom( NULL, req->atom )) return;
  251. release_global_atom( NULL, class->atom );
  252. class->atom = req->atom;
  253. }
  254. if (req->flags & SET_CLASS_STYLE) class->style = req->style;
  255. if (req->flags & SET_CLASS_WINEXTRA) class->win_extra = req->win_extra;
  256. if (req->flags & SET_CLASS_INSTANCE) class->instance = req->instance;
  257. if (req->flags & SET_CLASS_EXTRA) memcpy( class->extra_bytes + req->extra_offset,
  258. &req->extra_value, req->extra_size );
  259. }