class.c 8.9 KB

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