windows.media.mediacontrol: Add stub DLL.
[wine.git] / server / clipboard.c
blob8118a467dd8717d69ba323bfe3d26ee0479cda12
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"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "request.h"
32 #include "object.h"
33 #include "file.h"
34 #include "process.h"
35 #include "user.h"
36 #include "winuser.h"
37 #include "winternl.h"
39 struct clip_format
41 struct list entry; /* entry in format list */
42 unsigned int id; /* format id */
43 unsigned int from; /* for synthesized data, format to generate it from */
44 unsigned int seqno; /* sequence number when the data was set */
45 data_size_t size; /* size of the data block */
46 void *data; /* data contents, or NULL for delay-rendered */
49 struct clipboard
51 struct object obj; /* object header */
52 struct thread *open_thread; /* thread id that has clipboard open */
53 user_handle_t open_win; /* window that has clipboard open */
54 user_handle_t owner; /* window that owns the clipboard */
55 user_handle_t viewer; /* first window in clipboard viewer list */
56 unsigned int lcid; /* locale id to use for synthesizing text formats */
57 unsigned int seqno; /* clipboard change sequence number */
58 unsigned int open_seqno; /* sequence number at open time */
59 unsigned int rendering; /* format rendering recursion counter */
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 &no_type, /* type */
75 clipboard_dump, /* dump */
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 default_map_access, /* map_access */
83 default_get_sd, /* get_sd */
84 default_set_sd, /* set_sd */
85 no_get_full_name, /* get_full_name */
86 no_lookup_name, /* lookup_name */
87 no_link_name, /* link_name */
88 NULL, /* unlink_name */
89 no_open_file, /* open_file */
90 no_kernel_obj_list, /* get_kernel_obj_list */
91 no_close_handle, /* close_handle */
92 clipboard_destroy /* destroy */
96 #define HAS_FORMAT(map,id) ((map) & (1 << (id))) /* only for formats < CF_MAX */
98 /* find a data format in the clipboard */
99 static struct clip_format *get_format( struct clipboard *clipboard, unsigned int id )
101 struct clip_format *format;
103 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
104 if (format->id == id) return format;
106 return NULL;
109 /* add a data format to the clipboard */
110 static struct clip_format *add_format( struct clipboard *clipboard, unsigned int id )
112 struct clip_format *format;
114 if (!(format = mem_alloc( sizeof(*format )))) return NULL;
115 format->id = id;
116 format->from = 0;
117 format->size = 0;
118 format->data = NULL;
119 list_add_tail( &clipboard->formats, &format->entry );
120 clipboard->format_count++;
121 if (id < CF_MAX) clipboard->format_map |= 1 << id;
122 return format;
125 /* free all clipboard formats */
126 static void free_clipboard_formats( struct clipboard *clipboard )
128 struct clip_format *format, *next;
130 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
132 list_remove( &format->entry );
133 free( format->data );
134 free( format );
136 clipboard->format_count = 0;
137 clipboard->format_map = 0;
140 /* dump a clipboard object */
141 static void clipboard_dump( struct object *obj, int verbose )
143 struct clipboard *clipboard = (struct clipboard *)obj;
145 fprintf( stderr, "Clipboard open_thread=%p open_win=%08x owner=%08x viewer=%08x seq=%u\n",
146 clipboard->open_thread, clipboard->open_win,
147 clipboard->owner, clipboard->viewer, clipboard->seqno );
150 static void clipboard_destroy( struct object *obj )
152 struct clipboard *clipboard = (struct clipboard *)obj;
154 free( clipboard->listeners );
155 free_clipboard_formats( clipboard );
158 /* retrieve the clipboard info for the current process, allocating it if needed */
159 static struct clipboard *get_process_clipboard(void)
161 struct clipboard *clipboard;
162 struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD );
164 if (!winstation) return NULL;
166 if (!(clipboard = winstation->clipboard))
168 if ((clipboard = alloc_object( &clipboard_ops )))
170 clipboard->open_thread = NULL;
171 clipboard->open_win = 0;
172 clipboard->owner = 0;
173 clipboard->viewer = 0;
174 clipboard->seqno = 0;
175 clipboard->format_count = 0;
176 clipboard->format_map = 0;
177 clipboard->listen_size = 0;
178 clipboard->listen_count = 0;
179 clipboard->listeners = NULL;
180 list_init( &clipboard->formats );
181 winstation->clipboard = clipboard;
184 release_object( winstation );
185 return clipboard;
188 /* add synthesized formats upon clipboard close */
189 static int synthesize_formats( struct clipboard *clipboard )
191 static const unsigned int formats[][3] =
193 { CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT },
194 { CF_OEMTEXT, CF_UNICODETEXT, CF_TEXT },
195 { CF_UNICODETEXT, CF_TEXT, CF_OEMTEXT },
196 { CF_METAFILEPICT, CF_ENHMETAFILE },
197 { CF_ENHMETAFILE, CF_METAFILEPICT },
198 { CF_BITMAP, CF_DIB, CF_DIBV5 },
199 { CF_DIB, CF_BITMAP, CF_DIBV5 },
200 { CF_DIBV5, CF_BITMAP, CF_DIB }
202 unsigned int i, from, total = 0, map = clipboard->format_map;
203 struct clip_format *format;
205 if (!HAS_FORMAT( map, CF_LOCALE ) &&
206 (HAS_FORMAT( map, CF_TEXT ) || HAS_FORMAT( map, CF_OEMTEXT ) || HAS_FORMAT( map, CF_UNICODETEXT )))
208 void *data = memdup( &clipboard->lcid, sizeof(clipboard->lcid) );
209 if (data && (format = add_format( clipboard, CF_LOCALE )))
211 format->seqno = clipboard->seqno++;
212 format->data = data;
213 format->size = sizeof(clipboard->lcid);
215 else free( data );
218 for (i = 0; i < ARRAY_SIZE( formats ); i++)
220 if (HAS_FORMAT( map, formats[i][0] )) continue;
221 if (HAS_FORMAT( map, formats[i][1] )) from = formats[i][1];
222 else if (HAS_FORMAT( map, formats[i][2] )) from = formats[i][2];
223 else continue;
224 if (!(format = add_format( clipboard, formats[i][0] ))) continue;
225 format->from = from;
226 format->seqno = clipboard->seqno;
227 total++;
229 return total;
232 /* add a clipboard listener */
233 static void add_listener( struct clipboard *clipboard, user_handle_t window )
235 unsigned int i;
237 for (i = 0; i < clipboard->listen_count; i++)
239 if (clipboard->listeners[i] != window) continue;
240 set_error( STATUS_INVALID_PARAMETER ); /* already set */
241 return;
243 if (clipboard->listen_size == clipboard->listen_count)
245 unsigned int new_size = max( 8, clipboard->listen_size * 2 );
246 user_handle_t *new = realloc( clipboard->listeners, new_size * sizeof(*new) );
247 if (!new)
249 set_error( STATUS_NO_MEMORY );
250 return;
252 clipboard->listeners = new;
253 clipboard->listen_size = new_size;
255 clipboard->listeners[clipboard->listen_count++] = window;
258 /* remove a clipboard listener */
259 static int remove_listener( struct clipboard *clipboard, user_handle_t window )
261 unsigned int i;
263 for (i = 0; i < clipboard->listen_count; i++)
265 if (clipboard->listeners[i] != window) continue;
266 memmove( clipboard->listeners + i, clipboard->listeners + i + 1,
267 (clipboard->listen_count - i - 1) * sizeof(*clipboard->listeners) );
268 clipboard->listen_count--;
269 return 1;
271 return 0;
274 /* notify all listeners, and return the viewer window that should be notified if any */
275 static user_handle_t notify_listeners( struct clipboard *clipboard )
277 unsigned int i;
279 for (i = 0; i < clipboard->listen_count; i++)
280 post_message( clipboard->listeners[i], WM_CLIPBOARDUPDATE, 0, 0 );
281 return clipboard->viewer;
284 /* close the clipboard, and return the viewer window that should be notified if any */
285 static user_handle_t close_clipboard( struct clipboard *clipboard )
287 clipboard->open_win = 0;
288 clipboard->open_thread = NULL;
289 if (clipboard->seqno == clipboard->open_seqno) return 0; /* unchanged */
290 if (synthesize_formats( clipboard )) clipboard->seqno++;
291 return notify_listeners( clipboard );
294 /* release the clipboard owner, and return the viewer window that should be notified if any */
295 static user_handle_t release_clipboard( struct clipboard *clipboard )
297 struct clip_format *format, *next;
298 int changed = 0;
300 clipboard->owner = 0;
302 /* free the delayed-rendered formats, since we no longer have an owner to render them */
303 LIST_FOR_EACH_ENTRY_SAFE( format, next, &clipboard->formats, struct clip_format, entry )
305 if (format->data) continue;
306 /* format->from is earlier in the list and thus has already been
307 * removed if not available anymore (it is also < CF_MAX)
309 if (format->from && HAS_FORMAT( clipboard->format_map, format->from )) continue;
310 list_remove( &format->entry );
311 if (format->id < CF_MAX) clipboard->format_map &= ~(1 << format->id);
312 clipboard->format_count--;
313 free( format );
314 changed = 1;
317 if (!changed) return 0;
318 clipboard->seqno++;
319 return notify_listeners( clipboard );
322 /* cleanup clipboard information upon window destruction */
323 void cleanup_clipboard_window( struct desktop *desktop, user_handle_t window )
325 struct clipboard *clipboard = desktop->winstation->clipboard;
327 if (!clipboard) return;
329 remove_listener( clipboard, window );
330 if (clipboard->viewer == window) clipboard->viewer = 0;
331 if (clipboard->owner == window) release_clipboard( clipboard );
332 if (clipboard->open_win == window)
334 user_handle_t viewer = close_clipboard( clipboard );
335 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
339 /* Called when thread terminates to allow release of clipboard */
340 void cleanup_clipboard_thread(struct thread *thread)
342 struct clipboard *clipboard;
343 struct winstation *winstation;
345 if (!thread->process->winstation) return;
346 if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
348 if ((clipboard = winstation->clipboard))
350 if (thread == clipboard->open_thread)
352 user_handle_t viewer = close_clipboard( clipboard );
353 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
356 release_object( winstation );
359 /* open the clipboard */
360 DECL_HANDLER(open_clipboard)
362 struct clipboard *clipboard = get_process_clipboard();
363 user_handle_t win = 0;
365 if (!clipboard) return;
366 if (req->window && !(win = get_valid_window_handle( req->window ))) return;
368 if (clipboard->open_thread && clipboard->open_win != win)
370 set_error( STATUS_INVALID_LOCK_SEQUENCE );
371 return;
374 if (!clipboard->open_thread) clipboard->open_seqno = clipboard->seqno; /* first open */
375 if (clipboard->open_thread != current) clipboard->rendering = 0;
376 clipboard->open_win = win;
377 clipboard->open_thread = current;
379 reply->owner = clipboard->owner;
383 /* close the clipboard */
384 DECL_HANDLER(close_clipboard)
386 struct clipboard *clipboard = get_process_clipboard();
388 if (!clipboard) return;
390 if (clipboard->open_thread != current)
392 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
393 return;
395 reply->viewer = close_clipboard( clipboard );
396 reply->owner = clipboard->owner;
400 /* add a data format to the clipboard */
401 DECL_HANDLER(set_clipboard_data)
403 struct clip_format *format;
404 struct clipboard *clipboard = get_process_clipboard();
405 void *data = NULL;
407 if (!clipboard) return;
409 if (!req->format || !clipboard->open_thread)
411 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
412 return;
415 if (get_req_data_size() && !(data = memdup( get_req_data(), get_req_data_size() ))) return;
417 if (!(format = get_format( clipboard, req->format )))
419 if (!(format = add_format( clipboard, req->format )))
421 free( data );
422 return;
426 free( format->data );
427 format->from = 0;
428 format->seqno = clipboard->seqno;
429 format->size = get_req_data_size();
430 format->data = data;
431 if (!clipboard->rendering) clipboard->seqno++;
433 if (req->format == CF_TEXT || req->format == CF_OEMTEXT || req->format == CF_UNICODETEXT)
434 clipboard->lcid = req->lcid;
436 reply->seqno = format->seqno;
440 /* fetch a data format from the clipboard */
441 DECL_HANDLER(get_clipboard_data)
443 struct clip_format *format;
444 struct clipboard *clipboard = get_process_clipboard();
446 if (!clipboard) return;
448 if (clipboard->open_thread != current)
450 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
451 return;
453 if (!(format = get_format( clipboard, req->format )))
455 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
456 goto done;
458 reply->from = format->from;
459 reply->total = format->size;
460 reply->seqno = format->seqno;
461 reply->owner = clipboard->owner;
463 if (!format->data && req->render) /* try rendering it client-side */
465 if (format->from || clipboard->owner) clipboard->rendering++;
466 return;
469 if (req->cached && req->seqno == format->seqno) goto done; /* client-side cache still valid */
471 if (format->size > get_reply_max_size())
473 set_error( STATUS_BUFFER_OVERFLOW );
474 return;
476 set_reply_data( format->data, format->size );
478 done:
479 if (!req->render) clipboard->rendering--;
483 /* retrieve a list of available formats */
484 DECL_HANDLER(get_clipboard_formats)
486 struct clipboard *clipboard = get_process_clipboard();
488 if (!clipboard) return;
490 if (!req->format)
492 struct clip_format *format;
493 unsigned int i = 0, *ptr;
494 data_size_t size = clipboard->format_count * sizeof(unsigned int);
496 reply->count = clipboard->format_count;
497 if (size <= get_reply_max_size())
499 if ((ptr = mem_alloc( size )))
501 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
502 ptr[i++] = format->id;
503 assert( i == clipboard->format_count );
504 set_reply_data_ptr( ptr, size );
507 else set_error( STATUS_BUFFER_TOO_SMALL );
509 else reply->count = (get_format( clipboard, req->format ) != NULL); /* query a single format */
513 /* retrieve the next available format */
514 DECL_HANDLER(enum_clipboard_formats)
516 struct list *ptr;
517 struct clipboard *clipboard = get_process_clipboard();
519 if (!clipboard) return;
521 if (clipboard->open_thread != current)
523 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
524 return;
527 ptr = list_head( &clipboard->formats );
528 if (req->previous)
530 while (ptr && LIST_ENTRY( ptr, struct clip_format, entry )->id != req->previous)
531 ptr = list_next( &clipboard->formats, ptr );
532 if (ptr) ptr = list_next( &clipboard->formats, ptr );
534 if (ptr) reply->format = LIST_ENTRY( ptr, struct clip_format, entry )->id;
538 /* empty the clipboard and grab ownership */
539 DECL_HANDLER(empty_clipboard)
541 struct clipboard *clipboard = get_process_clipboard();
543 if (!clipboard) return;
545 if (clipboard->open_thread != current)
547 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
548 return;
551 free_clipboard_formats( clipboard );
552 clipboard->owner = clipboard->open_win;
553 clipboard->seqno++;
557 /* release ownership of the clipboard */
558 DECL_HANDLER(release_clipboard)
560 struct clipboard *clipboard = get_process_clipboard();
561 user_handle_t owner;
563 if (!clipboard) return;
565 if (!(owner = get_valid_window_handle( req->owner ))) return;
567 if (clipboard->owner == owner)
569 reply->viewer = release_clipboard( clipboard );
570 reply->owner = clipboard->owner;
572 else set_error( STATUS_INVALID_OWNER );
576 /* get clipboard information */
577 DECL_HANDLER(get_clipboard_info)
579 struct clipboard *clipboard = get_process_clipboard();
581 if (!clipboard) return;
583 reply->window = clipboard->open_win;
584 reply->owner = clipboard->owner;
585 reply->viewer = clipboard->viewer;
586 reply->seqno = clipboard->seqno;
590 /* set the clipboard viewer window */
591 DECL_HANDLER(set_clipboard_viewer)
593 struct clipboard *clipboard = get_process_clipboard();
594 user_handle_t viewer = 0, previous = 0;
596 if (!clipboard) return;
597 if (req->viewer && !(viewer = get_valid_window_handle( req->viewer ))) return;
598 if (req->previous && !(previous = get_valid_window_handle( req->previous ))) return;
600 reply->old_viewer = clipboard->viewer;
601 reply->owner = clipboard->owner;
603 if (!previous || clipboard->viewer == previous)
604 clipboard->viewer = viewer;
605 else
606 set_error( STATUS_PENDING ); /* need to send message instead */
610 /* add a clipboard listener window */
611 DECL_HANDLER(add_clipboard_listener)
613 struct clipboard *clipboard = get_process_clipboard();
614 user_handle_t win;
616 if (!clipboard) return;
617 if (!(win = get_valid_window_handle( req->window ))) return;
619 add_listener( clipboard, win );
623 /* remove a clipboard listener window */
624 DECL_HANDLER(remove_clipboard_listener)
626 struct clipboard *clipboard = get_process_clipboard();
627 user_handle_t win;
629 if (!clipboard) return;
630 if (!(win = get_valid_window_handle( req->window ))) return;
632 if (!remove_listener( clipboard, win )) set_error( STATUS_INVALID_PARAMETER );