vbscript: Use wide-char string literals.
[wine.git] / server / clipboard.c
blobe45468326204f9b8acdc58c14b466726e5f5d859
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_get_full_name, /* get_full_name */
87 no_lookup_name, /* lookup_name */
88 no_link_name, /* link_name */
89 NULL, /* unlink_name */
90 no_open_file, /* open_file */
91 no_kernel_obj_list, /* get_kernel_obj_list */
92 no_close_handle, /* close_handle */
93 clipboard_destroy /* destroy */
97 #define HAS_FORMAT(map,id) ((map) & (1 << (id))) /* only for formats < CF_MAX */
99 /* find a data format in the clipboard */
100 static struct clip_format *get_format( struct clipboard *clipboard, unsigned int id )
102 struct clip_format *format;
104 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
105 if (format->id == id) return format;
107 return NULL;
110 /* add a data format to the clipboard */
111 static struct clip_format *add_format( struct clipboard *clipboard, unsigned int id )
113 struct clip_format *format;
115 if (!(format = mem_alloc( sizeof(*format )))) return NULL;
116 format->id = id;
117 format->from = 0;
118 format->size = 0;
119 format->data = NULL;
120 list_add_tail( &clipboard->formats, &format->entry );
121 clipboard->format_count++;
122 if (id < CF_MAX) clipboard->format_map |= 1 << id;
123 return format;
126 /* free all clipboard formats */
127 static void free_clipboard_formats( struct clipboard *clipboard )
129 struct clip_format *format, *next;
131 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
133 list_remove( &format->entry );
134 free( format->data );
135 free( format );
137 clipboard->format_count = 0;
138 clipboard->format_map = 0;
141 /* dump a clipboard object */
142 static void clipboard_dump( struct object *obj, int verbose )
144 struct clipboard *clipboard = (struct clipboard *)obj;
146 fprintf( stderr, "Clipboard open_thread=%p open_win=%08x owner=%08x viewer=%08x seq=%u\n",
147 clipboard->open_thread, clipboard->open_win,
148 clipboard->owner, clipboard->viewer, clipboard->seqno );
151 static void clipboard_destroy( struct object *obj )
153 struct clipboard *clipboard = (struct clipboard *)obj;
155 free( clipboard->listeners );
156 free_clipboard_formats( clipboard );
159 /* retrieve the clipboard info for the current process, allocating it if needed */
160 static struct clipboard *get_process_clipboard(void)
162 struct clipboard *clipboard;
163 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
165 if (!winstation) return NULL;
167 if (!(clipboard = winstation->clipboard))
169 if ((clipboard = alloc_object( &clipboard_ops )))
171 clipboard->open_thread = NULL;
172 clipboard->open_win = 0;
173 clipboard->owner = 0;
174 clipboard->viewer = 0;
175 clipboard->seqno = 0;
176 clipboard->format_count = 0;
177 clipboard->format_map = 0;
178 clipboard->listen_size = 0;
179 clipboard->listen_count = 0;
180 clipboard->listeners = NULL;
181 list_init( &clipboard->formats );
182 winstation->clipboard = clipboard;
185 release_object( winstation );
186 return clipboard;
189 /* add synthesized formats upon clipboard close */
190 static int synthesize_formats( struct clipboard *clipboard )
192 static const unsigned int formats[][3] =
194 { CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT },
195 { CF_OEMTEXT, CF_UNICODETEXT, CF_TEXT },
196 { CF_UNICODETEXT, CF_TEXT, CF_OEMTEXT },
197 { CF_METAFILEPICT, CF_ENHMETAFILE },
198 { CF_ENHMETAFILE, CF_METAFILEPICT },
199 { CF_BITMAP, CF_DIB, CF_DIBV5 },
200 { CF_DIB, CF_BITMAP, CF_DIBV5 },
201 { CF_DIBV5, CF_BITMAP, CF_DIB }
203 unsigned int i, from, total = 0, map = clipboard->format_map;
204 struct clip_format *format;
206 if (!HAS_FORMAT( map, CF_LOCALE ) &&
207 (HAS_FORMAT( map, CF_TEXT ) || HAS_FORMAT( map, CF_OEMTEXT ) || HAS_FORMAT( map, CF_UNICODETEXT )))
209 void *data = memdup( &clipboard->lcid, sizeof(clipboard->lcid) );
210 if (data && (format = add_format( clipboard, CF_LOCALE )))
212 format->seqno = clipboard->seqno++;
213 format->data = data;
214 format->size = sizeof(clipboard->lcid);
216 else free( data );
219 for (i = 0; i < ARRAY_SIZE( formats ); i++)
221 if (HAS_FORMAT( map, formats[i][0] )) continue;
222 if (HAS_FORMAT( map, formats[i][1] )) from = formats[i][1];
223 else if (HAS_FORMAT( map, formats[i][2] )) from = formats[i][2];
224 else continue;
225 if (!(format = add_format( clipboard, formats[i][0] ))) continue;
226 format->from = from;
227 format->seqno = clipboard->seqno;
228 total++;
230 return total;
233 /* add a clipboard listener */
234 static void add_listener( struct clipboard *clipboard, user_handle_t window )
236 unsigned int i;
238 for (i = 0; i < clipboard->listen_count; i++)
240 if (clipboard->listeners[i] != window) continue;
241 set_error( STATUS_INVALID_PARAMETER ); /* already set */
242 return;
244 if (clipboard->listen_size == clipboard->listen_count)
246 unsigned int new_size = max( 8, clipboard->listen_size * 2 );
247 user_handle_t *new = realloc( clipboard->listeners, new_size * sizeof(*new) );
248 if (!new)
250 set_error( STATUS_NO_MEMORY );
251 return;
253 clipboard->listeners = new;
254 clipboard->listen_size = new_size;
256 clipboard->listeners[clipboard->listen_count++] = window;
259 /* remove a clipboard listener */
260 static int remove_listener( struct clipboard *clipboard, user_handle_t window )
262 unsigned int i;
264 for (i = 0; i < clipboard->listen_count; i++)
266 if (clipboard->listeners[i] != window) continue;
267 memmove( clipboard->listeners + i, clipboard->listeners + i + 1,
268 (clipboard->listen_count - i - 1) * sizeof(*clipboard->listeners) );
269 clipboard->listen_count--;
270 return 1;
272 return 0;
275 /* notify all listeners, and return the viewer window that should be notified if any */
276 static user_handle_t notify_listeners( struct clipboard *clipboard )
278 unsigned int i;
280 for (i = 0; i < clipboard->listen_count; i++)
281 post_message( clipboard->listeners[i], WM_CLIPBOARDUPDATE, 0, 0 );
282 return clipboard->viewer;
285 /* close the clipboard, and return the viewer window that should be notified if any */
286 static user_handle_t close_clipboard( struct clipboard *clipboard )
288 clipboard->open_win = 0;
289 clipboard->open_thread = NULL;
290 if (clipboard->seqno == clipboard->open_seqno) return 0; /* unchanged */
291 if (synthesize_formats( clipboard )) clipboard->seqno++;
292 return notify_listeners( clipboard );
295 /* release the clipboard owner, and return the viewer window that should be notified if any */
296 static user_handle_t release_clipboard( struct clipboard *clipboard )
298 struct clip_format *format, *next;
299 int changed = 0;
301 clipboard->owner = 0;
303 /* free the delayed-rendered formats, since we no longer have an owner to render them */
304 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
306 if (format->data || format->from) continue;
307 list_remove( &format->entry );
308 if (format->id < CF_MAX) clipboard->format_map &= ~(1 << format->id);
309 clipboard->format_count--;
310 free( format );
311 changed = 1;
314 if (!changed) return 0;
315 clipboard->seqno++;
316 return notify_listeners( clipboard );
319 /* cleanup clipboard information upon window destruction */
320 void cleanup_clipboard_window( struct desktop *desktop, user_handle_t window )
322 struct clipboard *clipboard = desktop->winstation->clipboard;
324 if (!clipboard) return;
326 remove_listener( clipboard, window );
327 if (clipboard->viewer == window) clipboard->viewer = 0;
328 if (clipboard->owner == window) release_clipboard( clipboard );
329 if (clipboard->open_win == window)
331 user_handle_t viewer = close_clipboard( clipboard );
332 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
336 /* Called when thread terminates to allow release of clipboard */
337 void cleanup_clipboard_thread(struct thread *thread)
339 struct clipboard *clipboard;
340 struct winstation *winstation;
342 if (!thread->process->winstation) return;
343 if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
345 if ((clipboard = winstation->clipboard))
347 if (thread == clipboard->open_thread)
349 user_handle_t viewer = close_clipboard( clipboard );
350 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
353 release_object( winstation );
356 /* open the clipboard */
357 DECL_HANDLER(open_clipboard)
359 struct clipboard *clipboard = get_process_clipboard();
360 user_handle_t win = 0;
362 if (!clipboard) return;
363 if (req->window && !(win = get_valid_window_handle( req->window ))) return;
365 if (clipboard->open_thread && clipboard->open_win != win)
367 set_error( STATUS_INVALID_LOCK_SEQUENCE );
368 return;
371 if (!clipboard->open_thread) clipboard->open_seqno = clipboard->seqno; /* first open */
372 if (clipboard->open_thread != current) clipboard->rendering = 0;
373 clipboard->open_win = win;
374 clipboard->open_thread = current;
376 reply->owner = clipboard->owner;
380 /* close the clipboard */
381 DECL_HANDLER(close_clipboard)
383 struct clipboard *clipboard = get_process_clipboard();
385 if (!clipboard) return;
387 if (clipboard->open_thread != current)
389 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
390 return;
392 reply->viewer = close_clipboard( clipboard );
393 reply->owner = clipboard->owner;
397 /* add a data format to the clipboard */
398 DECL_HANDLER(set_clipboard_data)
400 struct clip_format *format;
401 struct clipboard *clipboard = get_process_clipboard();
402 void *data = NULL;
404 if (!clipboard) return;
406 if (!req->format || !clipboard->open_thread)
408 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
409 return;
412 if (get_req_data_size() && !(data = memdup( get_req_data(), get_req_data_size() ))) return;
414 if (!(format = get_format( clipboard, req->format )))
416 if (!(format = add_format( clipboard, req->format )))
418 free( data );
419 return;
423 free( format->data );
424 format->from = 0;
425 format->seqno = clipboard->seqno;
426 format->size = get_req_data_size();
427 format->data = data;
428 if (!clipboard->rendering) clipboard->seqno++;
430 if (req->format == CF_TEXT || req->format == CF_OEMTEXT || req->format == CF_UNICODETEXT)
431 clipboard->lcid = req->lcid;
433 reply->seqno = format->seqno;
437 /* fetch a data format from the clipboard */
438 DECL_HANDLER(get_clipboard_data)
440 struct clip_format *format;
441 struct clipboard *clipboard = get_process_clipboard();
443 if (!clipboard) return;
445 if (clipboard->open_thread != current)
447 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
448 return;
450 if (!(format = get_format( clipboard, req->format )))
452 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
453 goto done;
455 reply->from = format->from;
456 reply->total = format->size;
457 reply->seqno = format->seqno;
458 reply->owner = clipboard->owner;
460 if (!format->data && req->render) /* try rendering it client-side */
462 if (format->from || clipboard->owner) clipboard->rendering++;
463 return;
466 if (req->cached && req->seqno == format->seqno) goto done; /* client-side cache still valid */
468 if (format->size > get_reply_max_size())
470 set_error( STATUS_BUFFER_OVERFLOW );
471 return;
473 set_reply_data( format->data, format->size );
475 done:
476 if (!req->render) clipboard->rendering--;
480 /* retrieve a list of available formats */
481 DECL_HANDLER(get_clipboard_formats)
483 struct clipboard *clipboard = get_process_clipboard();
485 if (!clipboard) return;
487 if (!req->format)
489 struct clip_format *format;
490 unsigned int i = 0, *ptr;
491 data_size_t size = clipboard->format_count * sizeof(unsigned int);
493 reply->count = clipboard->format_count;
494 if (size <= get_reply_max_size())
496 if ((ptr = mem_alloc( size )))
498 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
499 ptr[i++] = format->id;
500 assert( i == clipboard->format_count );
501 set_reply_data_ptr( ptr, size );
504 else set_error( STATUS_BUFFER_TOO_SMALL );
506 else reply->count = (get_format( clipboard, req->format ) != NULL); /* query a single format */
510 /* retrieve the next available format */
511 DECL_HANDLER(enum_clipboard_formats)
513 struct list *ptr;
514 struct clipboard *clipboard = get_process_clipboard();
516 if (!clipboard) return;
518 if (clipboard->open_thread != current)
520 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
521 return;
524 ptr = list_head( &clipboard->formats );
525 if (req->previous)
527 while (ptr && LIST_ENTRY( ptr, struct clip_format, entry )->id != req->previous)
528 ptr = list_next( &clipboard->formats, ptr );
529 if (ptr) ptr = list_next( &clipboard->formats, ptr );
531 if (ptr) reply->format = LIST_ENTRY( ptr, struct clip_format, entry )->id;
535 /* empty the clipboard and grab ownership */
536 DECL_HANDLER(empty_clipboard)
538 struct clipboard *clipboard = get_process_clipboard();
540 if (!clipboard) return;
542 if (clipboard->open_thread != current)
544 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
545 return;
548 free_clipboard_formats( clipboard );
549 clipboard->owner = clipboard->open_win;
550 clipboard->seqno++;
554 /* release ownership of the clipboard */
555 DECL_HANDLER(release_clipboard)
557 struct clipboard *clipboard = get_process_clipboard();
558 user_handle_t owner;
560 if (!clipboard) return;
562 if (!(owner = get_valid_window_handle( req->owner ))) return;
564 if (clipboard->owner == owner)
566 reply->viewer = release_clipboard( clipboard );
567 reply->owner = clipboard->owner;
569 else set_error( STATUS_INVALID_OWNER );
573 /* get clipboard information */
574 DECL_HANDLER(get_clipboard_info)
576 struct clipboard *clipboard = get_process_clipboard();
578 if (!clipboard) return;
580 reply->window = clipboard->open_win;
581 reply->owner = clipboard->owner;
582 reply->viewer = clipboard->viewer;
583 reply->seqno = clipboard->seqno;
587 /* set the clipboard viewer window */
588 DECL_HANDLER(set_clipboard_viewer)
590 struct clipboard *clipboard = get_process_clipboard();
591 user_handle_t viewer = 0, previous = 0;
593 if (!clipboard) return;
594 if (req->viewer && !(viewer = get_valid_window_handle( req->viewer ))) return;
595 if (req->previous && !(previous = get_valid_window_handle( req->previous ))) return;
597 reply->old_viewer = clipboard->viewer;
598 reply->owner = clipboard->owner;
600 if (!previous || clipboard->viewer == previous)
601 clipboard->viewer = viewer;
602 else
603 set_error( STATUS_PENDING ); /* need to send message instead */
607 /* add a clipboard listener window */
608 DECL_HANDLER(add_clipboard_listener)
610 struct clipboard *clipboard = get_process_clipboard();
611 user_handle_t win;
613 if (!clipboard) return;
614 if (!(win = get_valid_window_handle( req->window ))) return;
616 add_listener( clipboard, win );
620 /* remove a clipboard listener window */
621 DECL_HANDLER(remove_clipboard_listener)
623 struct clipboard *clipboard = get_process_clipboard();
624 user_handle_t win;
626 if (!clipboard) return;
627 if (!(win = get_valid_window_handle( req->window ))) return;
629 if (!remove_listener( clipboard, win )) set_error( STATUS_INVALID_PARAMETER );