wined3d: Validate the SM4 token count (AFL).
[wine.git] / server / clipboard.c
bloba93319a3aec3b9efe677d05f63f6939481ffee61
1 /*
2 * Server-side clipboard management
4 * Copyright 2002 Ulrich Czekalla
5 * Copyright 2016 Alexandre Julliard
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "request.h"
33 #include "object.h"
34 #include "file.h"
35 #include "process.h"
36 #include "user.h"
37 #include "winuser.h"
38 #include "winternl.h"
40 struct clip_format
42 struct list entry; /* entry in format list */
43 unsigned int id; /* format id */
44 unsigned int from; /* for synthesized data, format to generate it from */
45 unsigned int seqno; /* sequence number when the data was set */
46 data_size_t size; /* size of the data block */
47 void *data; /* data contents, or NULL for delay-rendered */
50 struct clipboard
52 struct object obj; /* object header */
53 struct thread *open_thread; /* thread id that has clipboard open */
54 user_handle_t open_win; /* window that has clipboard open */
55 user_handle_t owner; /* window that owns the clipboard */
56 user_handle_t viewer; /* first window in clipboard viewer list */
57 unsigned int lcid; /* locale id to use for synthesizing text formats */
58 unsigned int seqno; /* clipboard change sequence number */
59 unsigned int open_seqno; /* sequence number at open time */
60 struct list formats; /* list of data formats */
61 unsigned int format_count; /* count of data formats */
62 unsigned int format_map; /* existence bitmap for formats < CF_MAX */
63 unsigned int listen_size; /* size of listeners array */
64 unsigned int listen_count; /* count of listeners */
65 user_handle_t *listeners; /* array of listener windows */
68 static void clipboard_dump( struct object *obj, int verbose );
69 static void clipboard_destroy( struct object *obj );
71 static const struct object_ops clipboard_ops =
73 sizeof(struct clipboard), /* size */
74 clipboard_dump, /* dump */
75 no_get_type, /* get_type */
76 no_add_queue, /* add_queue */
77 NULL, /* remove_queue */
78 NULL, /* signaled */
79 NULL, /* satisfied */
80 no_signal, /* signal */
81 no_get_fd, /* get_fd */
82 no_map_access, /* map_access */
83 default_get_sd, /* get_sd */
84 default_set_sd, /* set_sd */
85 no_lookup_name, /* lookup_name */
86 no_link_name, /* link_name */
87 NULL, /* unlink_name */
88 no_open_file, /* open_file */
89 no_close_handle, /* close_handle */
90 clipboard_destroy /* destroy */
94 #define HAS_FORMAT(map,id) ((map) & (1 << (id))) /* only for formats < CF_MAX */
96 /* find a data format in the clipboard */
97 static struct clip_format *get_format( struct clipboard *clipboard, unsigned int id )
99 struct clip_format *format;
101 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
102 if (format->id == id) return format;
104 return NULL;
107 /* add a data format to the clipboard */
108 static struct clip_format *add_format( struct clipboard *clipboard, unsigned int id )
110 struct clip_format *format;
112 if (!(format = mem_alloc( sizeof(*format )))) return NULL;
113 format->id = id;
114 format->from = 0;
115 format->size = 0;
116 format->data = NULL;
117 list_add_tail( &clipboard->formats, &format->entry );
118 clipboard->format_count++;
119 if (id < CF_MAX) clipboard->format_map |= 1 << id;
120 return format;
123 /* free all clipboard formats */
124 static void free_clipboard_formats( struct clipboard *clipboard )
126 struct clip_format *format, *next;
128 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
130 list_remove( &format->entry );
131 free( format->data );
132 free( format );
134 clipboard->format_count = 0;
135 clipboard->format_map = 0;
138 /* dump a clipboard object */
139 static void clipboard_dump( struct object *obj, int verbose )
141 struct clipboard *clipboard = (struct clipboard *)obj;
143 fprintf( stderr, "Clipboard open_thread=%p open_win=%08x owner=%08x viewer=%08x seq=%u\n",
144 clipboard->open_thread, clipboard->open_win,
145 clipboard->owner, clipboard->viewer, clipboard->seqno );
148 static void clipboard_destroy( struct object *obj )
150 struct clipboard *clipboard = (struct clipboard *)obj;
152 free( clipboard->listeners );
153 free_clipboard_formats( clipboard );
156 /* retrieve the clipboard info for the current process, allocating it if needed */
157 static struct clipboard *get_process_clipboard(void)
159 struct clipboard *clipboard;
160 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
162 if (!winstation) return NULL;
164 if (!(clipboard = winstation->clipboard))
166 if ((clipboard = alloc_object( &clipboard_ops )))
168 clipboard->open_thread = NULL;
169 clipboard->open_win = 0;
170 clipboard->owner = 0;
171 clipboard->viewer = 0;
172 clipboard->seqno = 0;
173 clipboard->format_count = 0;
174 clipboard->format_map = 0;
175 clipboard->listen_size = 0;
176 clipboard->listen_count = 0;
177 clipboard->listeners = NULL;
178 list_init( &clipboard->formats );
179 winstation->clipboard = clipboard;
182 release_object( winstation );
183 return clipboard;
186 /* add synthesized formats upon clipboard close */
187 static int synthesize_formats( struct clipboard *clipboard )
189 static const unsigned int formats[][3] =
191 { CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT },
192 { CF_OEMTEXT, CF_UNICODETEXT, CF_TEXT },
193 { CF_UNICODETEXT, CF_TEXT, CF_OEMTEXT },
194 { CF_METAFILEPICT, CF_ENHMETAFILE },
195 { CF_ENHMETAFILE, CF_METAFILEPICT },
196 { CF_BITMAP, CF_DIB, CF_DIBV5 },
197 { CF_DIB, CF_BITMAP, CF_DIBV5 },
198 { CF_DIBV5, CF_BITMAP, CF_DIB }
200 unsigned int i, from, total = 0, map = clipboard->format_map;
201 struct clip_format *format;
203 if (!HAS_FORMAT( map, CF_LOCALE ) &&
204 (HAS_FORMAT( map, CF_TEXT ) || HAS_FORMAT( map, CF_OEMTEXT ) || HAS_FORMAT( map, CF_UNICODETEXT )))
206 void *data = memdup( &clipboard->lcid, sizeof(clipboard->lcid) );
207 if (data && (format = add_format( clipboard, CF_LOCALE )))
209 format->seqno = clipboard->seqno++;
210 format->data = data;
211 format->size = sizeof(clipboard->lcid);
213 else free( data );
216 for (i = 0; i < sizeof(formats) / sizeof(formats[0]); i++)
218 if (HAS_FORMAT( map, formats[i][0] )) continue;
219 if (HAS_FORMAT( map, formats[i][1] )) from = formats[i][1];
220 else if (HAS_FORMAT( map, formats[i][2] )) from = formats[i][2];
221 else continue;
222 if (!(format = add_format( clipboard, formats[i][0] ))) continue;
223 format->from = from;
224 format->seqno = clipboard->seqno;
225 total++;
227 return total;
230 /* add a clipboard listener */
231 static void add_listener( struct clipboard *clipboard, user_handle_t window )
233 unsigned int i;
235 for (i = 0; i < clipboard->listen_count; i++)
237 if (clipboard->listeners[i] != window) continue;
238 set_error( STATUS_INVALID_PARAMETER ); /* already set */
239 return;
241 if (clipboard->listen_size == clipboard->listen_count)
243 unsigned int new_size = max( 8, clipboard->listen_size * 2 );
244 user_handle_t *new = realloc( clipboard->listeners, new_size * sizeof(*new) );
245 if (!new)
247 set_error( STATUS_NO_MEMORY );
248 return;
250 clipboard->listeners = new;
251 clipboard->listen_size = new_size;
253 clipboard->listeners[clipboard->listen_count++] = window;
256 /* remove a clipboard listener */
257 static int remove_listener( struct clipboard *clipboard, user_handle_t window )
259 unsigned int i;
261 for (i = 0; i < clipboard->listen_count; i++)
263 if (clipboard->listeners[i] != window) continue;
264 memmove( clipboard->listeners + i, clipboard->listeners + i + 1,
265 (clipboard->listen_count - i - 1) * sizeof(*clipboard->listeners) );
266 clipboard->listen_count--;
267 return 1;
269 return 0;
272 /* notify all listeners, and return the viewer window that should be notified if any */
273 static user_handle_t notify_listeners( struct clipboard *clipboard )
275 unsigned int i;
277 for (i = 0; i < clipboard->listen_count; i++)
278 post_message( clipboard->listeners[i], WM_CLIPBOARDUPDATE, 0, 0 );
279 return clipboard->viewer;
282 /* close the clipboard, and return the viewer window that should be notified if any */
283 static user_handle_t close_clipboard( struct clipboard *clipboard )
285 clipboard->open_win = 0;
286 clipboard->open_thread = NULL;
287 if (clipboard->seqno == clipboard->open_seqno) return 0; /* unchanged */
288 if (synthesize_formats( clipboard )) clipboard->seqno++;
289 return notify_listeners( clipboard );
292 /* release the clipboard owner, and return the viewer window that should be notified if any */
293 static user_handle_t release_clipboard( struct clipboard *clipboard )
295 struct clip_format *format, *next;
296 int changed = 0;
298 clipboard->owner = 0;
300 /* free the delayed-rendered formats, since we no longer have an owner to render them */
301 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
303 if (format->data || format->from) continue;
304 list_remove( &format->entry );
305 if (format->id < CF_MAX) clipboard->format_map &= ~(1 << format->id);
306 clipboard->format_count--;
307 free( format );
308 changed = 1;
311 if (!changed) return 0;
312 clipboard->seqno++;
313 return notify_listeners( clipboard );
316 /* cleanup clipboard information upon window destruction */
317 void cleanup_clipboard_window( struct desktop *desktop, user_handle_t window )
319 struct clipboard *clipboard = desktop->winstation->clipboard;
321 if (!clipboard) return;
323 remove_listener( clipboard, window );
324 if (clipboard->viewer == window) clipboard->viewer = 0;
325 if (clipboard->owner == window) release_clipboard( clipboard );
326 if (clipboard->open_win == window)
328 user_handle_t viewer = close_clipboard( clipboard );
329 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
333 /* Called when thread terminates to allow release of clipboard */
334 void cleanup_clipboard_thread(struct thread *thread)
336 struct clipboard *clipboard;
337 struct winstation *winstation;
339 if (!thread->process->winstation) return;
340 if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
342 if ((clipboard = winstation->clipboard))
344 if (thread == clipboard->open_thread)
346 user_handle_t viewer = close_clipboard( clipboard );
347 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
350 release_object( winstation );
353 /* open the clipboard */
354 DECL_HANDLER(open_clipboard)
356 struct clipboard *clipboard = get_process_clipboard();
357 user_handle_t win = 0;
359 if (!clipboard) return;
360 if (req->window && !(win = get_valid_window_handle( req->window ))) return;
362 if (clipboard->open_thread && clipboard->open_win != win)
364 set_error( STATUS_INVALID_LOCK_SEQUENCE );
365 return;
368 if (!clipboard->open_thread) clipboard->open_seqno = clipboard->seqno; /* first open */
369 clipboard->open_win = win;
370 clipboard->open_thread = current;
372 reply->owner = clipboard->owner;
376 /* close the clipboard */
377 DECL_HANDLER(close_clipboard)
379 struct clipboard *clipboard = get_process_clipboard();
381 if (!clipboard) return;
383 if (clipboard->open_thread != current)
385 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
386 return;
388 reply->viewer = close_clipboard( clipboard );
389 reply->owner = clipboard->owner;
393 /* add a data format to the clipboard */
394 DECL_HANDLER(set_clipboard_data)
396 struct clip_format *format;
397 struct clipboard *clipboard = get_process_clipboard();
398 void *data = NULL;
400 if (!clipboard) return;
402 if (!req->format || !clipboard->open_thread)
404 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
405 return;
408 if (get_req_data_size() && !(data = memdup( get_req_data(), get_req_data_size() ))) return;
410 if (!(format = get_format( clipboard, req->format )))
412 if (!(format = add_format( clipboard, req->format )))
414 free( data );
415 return;
419 free( format->data );
420 format->from = 0;
421 format->seqno = clipboard->seqno++;
422 format->size = get_req_data_size();
423 format->data = data;
425 if (req->format == CF_TEXT || req->format == CF_OEMTEXT || req->format == CF_UNICODETEXT)
426 clipboard->lcid = req->lcid;
428 reply->seqno = format->seqno;
432 /* fetch a data format from the clipboard */
433 DECL_HANDLER(get_clipboard_data)
435 struct clip_format *format;
436 struct clipboard *clipboard = get_process_clipboard();
438 if (!clipboard) return;
440 if (clipboard->open_thread != current)
442 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
443 return;
445 if (!(format = get_format( clipboard, req->format )))
447 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
448 return;
450 reply->from = format->from;
451 reply->total = format->size;
452 reply->seqno = format->seqno;
453 if (!format->data && !format->from) reply->owner = clipboard->owner;
454 if (req->cached && req->seqno == format->seqno) return; /* client-side cache still valid */
455 if (format->size <= get_reply_max_size()) set_reply_data( format->data, format->size );
456 else set_error( STATUS_BUFFER_OVERFLOW );
460 /* retrieve a list of available formats */
461 DECL_HANDLER(get_clipboard_formats)
463 struct clipboard *clipboard = get_process_clipboard();
465 if (!clipboard) return;
467 if (!req->format)
469 struct clip_format *format;
470 unsigned int i = 0, *ptr;
471 data_size_t size = clipboard->format_count * sizeof(unsigned int);
473 reply->count = clipboard->format_count;
474 if (size <= get_reply_max_size())
476 if ((ptr = mem_alloc( size )))
478 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
479 ptr[i++] = format->id;
480 assert( i == clipboard->format_count );
481 set_reply_data_ptr( ptr, size );
484 else set_error( STATUS_BUFFER_TOO_SMALL );
486 else reply->count = (get_format( clipboard, req->format ) != NULL); /* query a single format */
490 /* retrieve the next available format */
491 DECL_HANDLER(enum_clipboard_formats)
493 struct list *ptr;
494 struct clipboard *clipboard = get_process_clipboard();
496 if (!clipboard) return;
498 if (clipboard->open_thread != current)
500 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
501 return;
504 ptr = list_head( &clipboard->formats );
505 if (req->previous)
507 while (ptr && LIST_ENTRY( ptr, struct clip_format, entry )->id != req->previous)
508 ptr = list_next( &clipboard->formats, ptr );
509 if (ptr) ptr = list_next( &clipboard->formats, ptr );
511 if (ptr) reply->format = LIST_ENTRY( ptr, struct clip_format, entry )->id;
515 /* empty the clipboard and grab ownership */
516 DECL_HANDLER(empty_clipboard)
518 struct clipboard *clipboard = get_process_clipboard();
520 if (!clipboard) return;
522 if (clipboard->open_thread != current)
524 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
525 return;
528 free_clipboard_formats( clipboard );
529 clipboard->owner = clipboard->open_win;
530 clipboard->seqno++;
534 /* release ownership of the clipboard */
535 DECL_HANDLER(release_clipboard)
537 struct clipboard *clipboard = get_process_clipboard();
538 user_handle_t owner;
540 if (!clipboard) return;
542 if (!(owner = get_valid_window_handle( req->owner ))) return;
544 if (clipboard->owner == owner)
546 reply->viewer = release_clipboard( clipboard );
547 reply->owner = clipboard->owner;
549 else set_error( STATUS_INVALID_OWNER );
553 /* get clipboard information */
554 DECL_HANDLER(get_clipboard_info)
556 struct clipboard *clipboard = get_process_clipboard();
558 if (!clipboard) return;
560 reply->window = clipboard->open_win;
561 reply->owner = clipboard->owner;
562 reply->viewer = clipboard->viewer;
563 reply->seqno = clipboard->seqno;
567 /* set the clipboard viewer window */
568 DECL_HANDLER(set_clipboard_viewer)
570 struct clipboard *clipboard = get_process_clipboard();
571 user_handle_t viewer = 0, previous = 0;
573 if (!clipboard) return;
574 if (req->viewer && !(viewer = get_valid_window_handle( req->viewer ))) return;
575 if (req->previous && !(previous = get_valid_window_handle( req->previous ))) return;
577 reply->old_viewer = clipboard->viewer;
578 reply->owner = clipboard->owner;
580 if (!previous || clipboard->viewer == previous)
581 clipboard->viewer = viewer;
582 else
583 set_error( STATUS_PENDING ); /* need to send message instead */
587 /* add a clipboard listener window */
588 DECL_HANDLER(add_clipboard_listener)
590 struct clipboard *clipboard = get_process_clipboard();
591 user_handle_t win;
593 if (!clipboard) return;
594 if (!(win = get_valid_window_handle( req->window ))) return;
596 add_listener( clipboard, win );
600 /* remove a clipboard listener window */
601 DECL_HANDLER(remove_clipboard_listener)
603 struct clipboard *clipboard = get_process_clipboard();
604 user_handle_t win;
606 if (!clipboard) return;
607 if (!(win = get_valid_window_handle( req->window ))) return;
609 if (!remove_listener( clipboard, win )) set_error( STATUS_INVALID_PARAMETER );