d3d8/tests: Use the available ARRAY_SIZE() macro.
[wine.git] / server / clipboard.c
blob160eb46959d4cd1f2a86ae604c43bb78d987ad85
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 unsigned int rendering; /* format rendering recursion counter */
61 struct list formats; /* list of data formats */
62 unsigned int format_count; /* count of data formats */
63 unsigned int format_map; /* existence bitmap for formats < CF_MAX */
64 unsigned int listen_size; /* size of listeners array */
65 unsigned int listen_count; /* count of listeners */
66 user_handle_t *listeners; /* array of listener windows */
69 static void clipboard_dump( struct object *obj, int verbose );
70 static void clipboard_destroy( struct object *obj );
72 static const struct object_ops clipboard_ops =
74 sizeof(struct clipboard), /* size */
75 clipboard_dump, /* dump */
76 no_get_type, /* get_type */
77 no_add_queue, /* add_queue */
78 NULL, /* remove_queue */
79 NULL, /* signaled */
80 NULL, /* satisfied */
81 no_signal, /* signal */
82 no_get_fd, /* get_fd */
83 no_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 no_link_name, /* link_name */
88 NULL, /* unlink_name */
89 no_open_file, /* open_file */
90 no_close_handle, /* close_handle */
91 clipboard_destroy /* destroy */
95 #define HAS_FORMAT(map,id) ((map) & (1 << (id))) /* only for formats < CF_MAX */
97 /* find a data format in the clipboard */
98 static struct clip_format *get_format( struct clipboard *clipboard, unsigned int id )
100 struct clip_format *format;
102 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
103 if (format->id == id) return format;
105 return NULL;
108 /* add a data format to the clipboard */
109 static struct clip_format *add_format( struct clipboard *clipboard, unsigned int id )
111 struct clip_format *format;
113 if (!(format = mem_alloc( sizeof(*format )))) return NULL;
114 format->id = id;
115 format->from = 0;
116 format->size = 0;
117 format->data = NULL;
118 list_add_tail( &clipboard->formats, &format->entry );
119 clipboard->format_count++;
120 if (id < CF_MAX) clipboard->format_map |= 1 << id;
121 return format;
124 /* free all clipboard formats */
125 static void free_clipboard_formats( struct clipboard *clipboard )
127 struct clip_format *format, *next;
129 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
131 list_remove( &format->entry );
132 free( format->data );
133 free( format );
135 clipboard->format_count = 0;
136 clipboard->format_map = 0;
139 /* dump a clipboard object */
140 static void clipboard_dump( struct object *obj, int verbose )
142 struct clipboard *clipboard = (struct clipboard *)obj;
144 fprintf( stderr, "Clipboard open_thread=%p open_win=%08x owner=%08x viewer=%08x seq=%u\n",
145 clipboard->open_thread, clipboard->open_win,
146 clipboard->owner, clipboard->viewer, clipboard->seqno );
149 static void clipboard_destroy( struct object *obj )
151 struct clipboard *clipboard = (struct clipboard *)obj;
153 free( clipboard->listeners );
154 free_clipboard_formats( clipboard );
157 /* retrieve the clipboard info for the current process, allocating it if needed */
158 static struct clipboard *get_process_clipboard(void)
160 struct clipboard *clipboard;
161 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
163 if (!winstation) return NULL;
165 if (!(clipboard = winstation->clipboard))
167 if ((clipboard = alloc_object( &clipboard_ops )))
169 clipboard->open_thread = NULL;
170 clipboard->open_win = 0;
171 clipboard->owner = 0;
172 clipboard->viewer = 0;
173 clipboard->seqno = 0;
174 clipboard->format_count = 0;
175 clipboard->format_map = 0;
176 clipboard->listen_size = 0;
177 clipboard->listen_count = 0;
178 clipboard->listeners = NULL;
179 list_init( &clipboard->formats );
180 winstation->clipboard = clipboard;
183 release_object( winstation );
184 return clipboard;
187 /* add synthesized formats upon clipboard close */
188 static int synthesize_formats( struct clipboard *clipboard )
190 static const unsigned int formats[][3] =
192 { CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT },
193 { CF_OEMTEXT, CF_UNICODETEXT, CF_TEXT },
194 { CF_UNICODETEXT, CF_TEXT, CF_OEMTEXT },
195 { CF_METAFILEPICT, CF_ENHMETAFILE },
196 { CF_ENHMETAFILE, CF_METAFILEPICT },
197 { CF_BITMAP, CF_DIB, CF_DIBV5 },
198 { CF_DIB, CF_BITMAP, CF_DIBV5 },
199 { CF_DIBV5, CF_BITMAP, CF_DIB }
201 unsigned int i, from, total = 0, map = clipboard->format_map;
202 struct clip_format *format;
204 if (!HAS_FORMAT( map, CF_LOCALE ) &&
205 (HAS_FORMAT( map, CF_TEXT ) || HAS_FORMAT( map, CF_OEMTEXT ) || HAS_FORMAT( map, CF_UNICODETEXT )))
207 void *data = memdup( &clipboard->lcid, sizeof(clipboard->lcid) );
208 if (data && (format = add_format( clipboard, CF_LOCALE )))
210 format->seqno = clipboard->seqno++;
211 format->data = data;
212 format->size = sizeof(clipboard->lcid);
214 else free( data );
217 for (i = 0; i < sizeof(formats) / sizeof(formats[0]); i++)
219 if (HAS_FORMAT( map, formats[i][0] )) continue;
220 if (HAS_FORMAT( map, formats[i][1] )) from = formats[i][1];
221 else if (HAS_FORMAT( map, formats[i][2] )) from = formats[i][2];
222 else continue;
223 if (!(format = add_format( clipboard, formats[i][0] ))) continue;
224 format->from = from;
225 format->seqno = clipboard->seqno;
226 total++;
228 return total;
231 /* add a clipboard listener */
232 static void add_listener( struct clipboard *clipboard, user_handle_t window )
234 unsigned int i;
236 for (i = 0; i < clipboard->listen_count; i++)
238 if (clipboard->listeners[i] != window) continue;
239 set_error( STATUS_INVALID_PARAMETER ); /* already set */
240 return;
242 if (clipboard->listen_size == clipboard->listen_count)
244 unsigned int new_size = max( 8, clipboard->listen_size * 2 );
245 user_handle_t *new = realloc( clipboard->listeners, new_size * sizeof(*new) );
246 if (!new)
248 set_error( STATUS_NO_MEMORY );
249 return;
251 clipboard->listeners = new;
252 clipboard->listen_size = new_size;
254 clipboard->listeners[clipboard->listen_count++] = window;
257 /* remove a clipboard listener */
258 static int remove_listener( struct clipboard *clipboard, user_handle_t window )
260 unsigned int i;
262 for (i = 0; i < clipboard->listen_count; i++)
264 if (clipboard->listeners[i] != window) continue;
265 memmove( clipboard->listeners + i, clipboard->listeners + i + 1,
266 (clipboard->listen_count - i - 1) * sizeof(*clipboard->listeners) );
267 clipboard->listen_count--;
268 return 1;
270 return 0;
273 /* notify all listeners, and return the viewer window that should be notified if any */
274 static user_handle_t notify_listeners( struct clipboard *clipboard )
276 unsigned int i;
278 for (i = 0; i < clipboard->listen_count; i++)
279 post_message( clipboard->listeners[i], WM_CLIPBOARDUPDATE, 0, 0 );
280 return clipboard->viewer;
283 /* close the clipboard, and return the viewer window that should be notified if any */
284 static user_handle_t close_clipboard( struct clipboard *clipboard )
286 clipboard->open_win = 0;
287 clipboard->open_thread = NULL;
288 if (clipboard->seqno == clipboard->open_seqno) return 0; /* unchanged */
289 if (synthesize_formats( clipboard )) clipboard->seqno++;
290 return notify_listeners( clipboard );
293 /* release the clipboard owner, and return the viewer window that should be notified if any */
294 static user_handle_t release_clipboard( struct clipboard *clipboard )
296 struct clip_format *format, *next;
297 int changed = 0;
299 clipboard->owner = 0;
301 /* free the delayed-rendered formats, since we no longer have an owner to render them */
302 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
304 if (format->data || format->from) continue;
305 list_remove( &format->entry );
306 if (format->id < CF_MAX) clipboard->format_map &= ~(1 << format->id);
307 clipboard->format_count--;
308 free( format );
309 changed = 1;
312 if (!changed) return 0;
313 clipboard->seqno++;
314 return notify_listeners( clipboard );
317 /* cleanup clipboard information upon window destruction */
318 void cleanup_clipboard_window( struct desktop *desktop, user_handle_t window )
320 struct clipboard *clipboard = desktop->winstation->clipboard;
322 if (!clipboard) return;
324 remove_listener( clipboard, window );
325 if (clipboard->viewer == window) clipboard->viewer = 0;
326 if (clipboard->owner == window) release_clipboard( clipboard );
327 if (clipboard->open_win == window)
329 user_handle_t viewer = close_clipboard( clipboard );
330 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
334 /* Called when thread terminates to allow release of clipboard */
335 void cleanup_clipboard_thread(struct thread *thread)
337 struct clipboard *clipboard;
338 struct winstation *winstation;
340 if (!thread->process->winstation) return;
341 if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
343 if ((clipboard = winstation->clipboard))
345 if (thread == clipboard->open_thread)
347 user_handle_t viewer = close_clipboard( clipboard );
348 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
351 release_object( winstation );
354 /* open the clipboard */
355 DECL_HANDLER(open_clipboard)
357 struct clipboard *clipboard = get_process_clipboard();
358 user_handle_t win = 0;
360 if (!clipboard) return;
361 if (req->window && !(win = get_valid_window_handle( req->window ))) return;
363 if (clipboard->open_thread && clipboard->open_win != win)
365 set_error( STATUS_INVALID_LOCK_SEQUENCE );
366 return;
369 if (!clipboard->open_thread) clipboard->open_seqno = clipboard->seqno; /* first open */
370 if (clipboard->open_thread != current) clipboard->rendering = 0;
371 clipboard->open_win = win;
372 clipboard->open_thread = current;
374 reply->owner = clipboard->owner;
378 /* close the clipboard */
379 DECL_HANDLER(close_clipboard)
381 struct clipboard *clipboard = get_process_clipboard();
383 if (!clipboard) return;
385 if (clipboard->open_thread != current)
387 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
388 return;
390 reply->viewer = close_clipboard( clipboard );
391 reply->owner = clipboard->owner;
395 /* add a data format to the clipboard */
396 DECL_HANDLER(set_clipboard_data)
398 struct clip_format *format;
399 struct clipboard *clipboard = get_process_clipboard();
400 void *data = NULL;
402 if (!clipboard) return;
404 if (!req->format || !clipboard->open_thread)
406 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
407 return;
410 if (get_req_data_size() && !(data = memdup( get_req_data(), get_req_data_size() ))) return;
412 if (!(format = get_format( clipboard, req->format )))
414 if (!(format = add_format( clipboard, req->format )))
416 free( data );
417 return;
421 free( format->data );
422 format->from = 0;
423 format->seqno = clipboard->seqno;
424 format->size = get_req_data_size();
425 format->data = data;
426 if (!clipboard->rendering) clipboard->seqno++;
428 if (req->format == CF_TEXT || req->format == CF_OEMTEXT || req->format == CF_UNICODETEXT)
429 clipboard->lcid = req->lcid;
431 reply->seqno = format->seqno;
435 /* fetch a data format from the clipboard */
436 DECL_HANDLER(get_clipboard_data)
438 struct clip_format *format;
439 struct clipboard *clipboard = get_process_clipboard();
441 if (!clipboard) return;
443 if (clipboard->open_thread != current)
445 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
446 return;
448 if (!(format = get_format( clipboard, req->format )))
450 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
451 goto done;
453 reply->from = format->from;
454 reply->total = format->size;
455 reply->seqno = format->seqno;
456 reply->owner = clipboard->owner;
458 if (!format->data && req->render) /* try rendering it client-side */
460 if (format->from || clipboard->owner) clipboard->rendering++;
461 return;
464 if (req->cached && req->seqno == format->seqno) goto done; /* client-side cache still valid */
466 if (format->size > get_reply_max_size())
468 set_error( STATUS_BUFFER_OVERFLOW );
469 return;
471 set_reply_data( format->data, format->size );
473 done:
474 if (!req->render) clipboard->rendering--;
478 /* retrieve a list of available formats */
479 DECL_HANDLER(get_clipboard_formats)
481 struct clipboard *clipboard = get_process_clipboard();
483 if (!clipboard) return;
485 if (!req->format)
487 struct clip_format *format;
488 unsigned int i = 0, *ptr;
489 data_size_t size = clipboard->format_count * sizeof(unsigned int);
491 reply->count = clipboard->format_count;
492 if (size <= get_reply_max_size())
494 if ((ptr = mem_alloc( size )))
496 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
497 ptr[i++] = format->id;
498 assert( i == clipboard->format_count );
499 set_reply_data_ptr( ptr, size );
502 else set_error( STATUS_BUFFER_TOO_SMALL );
504 else reply->count = (get_format( clipboard, req->format ) != NULL); /* query a single format */
508 /* retrieve the next available format */
509 DECL_HANDLER(enum_clipboard_formats)
511 struct list *ptr;
512 struct clipboard *clipboard = get_process_clipboard();
514 if (!clipboard) return;
516 if (clipboard->open_thread != current)
518 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
519 return;
522 ptr = list_head( &clipboard->formats );
523 if (req->previous)
525 while (ptr && LIST_ENTRY( ptr, struct clip_format, entry )->id != req->previous)
526 ptr = list_next( &clipboard->formats, ptr );
527 if (ptr) ptr = list_next( &clipboard->formats, ptr );
529 if (ptr) reply->format = LIST_ENTRY( ptr, struct clip_format, entry )->id;
533 /* empty the clipboard and grab ownership */
534 DECL_HANDLER(empty_clipboard)
536 struct clipboard *clipboard = get_process_clipboard();
538 if (!clipboard) return;
540 if (clipboard->open_thread != current)
542 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
543 return;
546 free_clipboard_formats( clipboard );
547 clipboard->owner = clipboard->open_win;
548 clipboard->seqno++;
552 /* release ownership of the clipboard */
553 DECL_HANDLER(release_clipboard)
555 struct clipboard *clipboard = get_process_clipboard();
556 user_handle_t owner;
558 if (!clipboard) return;
560 if (!(owner = get_valid_window_handle( req->owner ))) return;
562 if (clipboard->owner == owner)
564 reply->viewer = release_clipboard( clipboard );
565 reply->owner = clipboard->owner;
567 else set_error( STATUS_INVALID_OWNER );
571 /* get clipboard information */
572 DECL_HANDLER(get_clipboard_info)
574 struct clipboard *clipboard = get_process_clipboard();
576 if (!clipboard) return;
578 reply->window = clipboard->open_win;
579 reply->owner = clipboard->owner;
580 reply->viewer = clipboard->viewer;
581 reply->seqno = clipboard->seqno;
585 /* set the clipboard viewer window */
586 DECL_HANDLER(set_clipboard_viewer)
588 struct clipboard *clipboard = get_process_clipboard();
589 user_handle_t viewer = 0, previous = 0;
591 if (!clipboard) return;
592 if (req->viewer && !(viewer = get_valid_window_handle( req->viewer ))) return;
593 if (req->previous && !(previous = get_valid_window_handle( req->previous ))) return;
595 reply->old_viewer = clipboard->viewer;
596 reply->owner = clipboard->owner;
598 if (!previous || clipboard->viewer == previous)
599 clipboard->viewer = viewer;
600 else
601 set_error( STATUS_PENDING ); /* need to send message instead */
605 /* add a clipboard listener window */
606 DECL_HANDLER(add_clipboard_listener)
608 struct clipboard *clipboard = get_process_clipboard();
609 user_handle_t win;
611 if (!clipboard) return;
612 if (!(win = get_valid_window_handle( req->window ))) return;
614 add_listener( clipboard, win );
618 /* remove a clipboard listener window */
619 DECL_HANDLER(remove_clipboard_listener)
621 struct clipboard *clipboard = get_process_clipboard();
622 user_handle_t win;
624 if (!clipboard) return;
625 if (!(win = get_valid_window_handle( req->window ))) return;
627 if (!remove_listener( clipboard, win )) set_error( STATUS_INVALID_PARAMETER );