ws2_32/tests: Allow ESP and AH protocols to be missing on Windows.
[wine.git] / server / clipboard.c
blobe6884dd75faeb662a5227a7172430b7b47bb6424
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_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 || format->from) continue;
306 list_remove( &format->entry );
307 if (format->id < CF_MAX) clipboard->format_map &= ~(1 << format->id);
308 clipboard->format_count--;
309 free( format );
310 changed = 1;
313 if (!changed) return 0;
314 clipboard->seqno++;
315 return notify_listeners( clipboard );
318 /* cleanup clipboard information upon window destruction */
319 void cleanup_clipboard_window( struct desktop *desktop, user_handle_t window )
321 struct clipboard *clipboard = desktop->winstation->clipboard;
323 if (!clipboard) return;
325 remove_listener( clipboard, window );
326 if (clipboard->viewer == window) clipboard->viewer = 0;
327 if (clipboard->owner == window) release_clipboard( clipboard );
328 if (clipboard->open_win == window)
330 user_handle_t viewer = close_clipboard( clipboard );
331 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
335 /* Called when thread terminates to allow release of clipboard */
336 void cleanup_clipboard_thread(struct thread *thread)
338 struct clipboard *clipboard;
339 struct winstation *winstation;
341 if (!thread->process->winstation) return;
342 if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
344 if ((clipboard = winstation->clipboard))
346 if (thread == clipboard->open_thread)
348 user_handle_t viewer = close_clipboard( clipboard );
349 if (viewer) send_notify_message( viewer, WM_DRAWCLIPBOARD, clipboard->owner, 0 );
352 release_object( winstation );
355 /* open the clipboard */
356 DECL_HANDLER(open_clipboard)
358 struct clipboard *clipboard = get_process_clipboard();
359 user_handle_t win = 0;
361 if (!clipboard) return;
362 if (req->window && !(win = get_valid_window_handle( req->window ))) return;
364 if (clipboard->open_thread && clipboard->open_win != win)
366 set_error( STATUS_INVALID_LOCK_SEQUENCE );
367 return;
370 if (!clipboard->open_thread) clipboard->open_seqno = clipboard->seqno; /* first open */
371 if (clipboard->open_thread != current) clipboard->rendering = 0;
372 clipboard->open_win = win;
373 clipboard->open_thread = current;
375 reply->owner = clipboard->owner;
379 /* close the clipboard */
380 DECL_HANDLER(close_clipboard)
382 struct clipboard *clipboard = get_process_clipboard();
384 if (!clipboard) return;
386 if (clipboard->open_thread != current)
388 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
389 return;
391 reply->viewer = close_clipboard( clipboard );
392 reply->owner = clipboard->owner;
396 /* add a data format to the clipboard */
397 DECL_HANDLER(set_clipboard_data)
399 struct clip_format *format;
400 struct clipboard *clipboard = get_process_clipboard();
401 void *data = NULL;
403 if (!clipboard) return;
405 if (!req->format || !clipboard->open_thread)
407 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
408 return;
411 if (get_req_data_size() && !(data = memdup( get_req_data(), get_req_data_size() ))) return;
413 if (!(format = get_format( clipboard, req->format )))
415 if (!(format = add_format( clipboard, req->format )))
417 free( data );
418 return;
422 free( format->data );
423 format->from = 0;
424 format->seqno = clipboard->seqno;
425 format->size = get_req_data_size();
426 format->data = data;
427 if (!clipboard->rendering) clipboard->seqno++;
429 if (req->format == CF_TEXT || req->format == CF_OEMTEXT || req->format == CF_UNICODETEXT)
430 clipboard->lcid = req->lcid;
432 reply->seqno = format->seqno;
436 /* fetch a data format from the clipboard */
437 DECL_HANDLER(get_clipboard_data)
439 struct clip_format *format;
440 struct clipboard *clipboard = get_process_clipboard();
442 if (!clipboard) return;
444 if (clipboard->open_thread != current)
446 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
447 return;
449 if (!(format = get_format( clipboard, req->format )))
451 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
452 goto done;
454 reply->from = format->from;
455 reply->total = format->size;
456 reply->seqno = format->seqno;
457 reply->owner = clipboard->owner;
459 if (!format->data && req->render) /* try rendering it client-side */
461 if (format->from || clipboard->owner) clipboard->rendering++;
462 return;
465 if (req->cached && req->seqno == format->seqno) goto done; /* client-side cache still valid */
467 if (format->size > get_reply_max_size())
469 set_error( STATUS_BUFFER_OVERFLOW );
470 return;
472 set_reply_data( format->data, format->size );
474 done:
475 if (!req->render) clipboard->rendering--;
479 /* retrieve a list of available formats */
480 DECL_HANDLER(get_clipboard_formats)
482 struct clipboard *clipboard = get_process_clipboard();
484 if (!clipboard) return;
486 if (!req->format)
488 struct clip_format *format;
489 unsigned int i = 0, *ptr;
490 data_size_t size = clipboard->format_count * sizeof(unsigned int);
492 reply->count = clipboard->format_count;
493 if (size <= get_reply_max_size())
495 if ((ptr = mem_alloc( size )))
497 LIST_FOR_EACH_ENTRY( format, &clipboard->formats, struct clip_format, entry )
498 ptr[i++] = format->id;
499 assert( i == clipboard->format_count );
500 set_reply_data_ptr( ptr, size );
503 else set_error( STATUS_BUFFER_TOO_SMALL );
505 else reply->count = (get_format( clipboard, req->format ) != NULL); /* query a single format */
509 /* retrieve the next available format */
510 DECL_HANDLER(enum_clipboard_formats)
512 struct list *ptr;
513 struct clipboard *clipboard = get_process_clipboard();
515 if (!clipboard) return;
517 if (clipboard->open_thread != current)
519 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
520 return;
523 ptr = list_head( &clipboard->formats );
524 if (req->previous)
526 while (ptr && LIST_ENTRY( ptr, struct clip_format, entry )->id != req->previous)
527 ptr = list_next( &clipboard->formats, ptr );
528 if (ptr) ptr = list_next( &clipboard->formats, ptr );
530 if (ptr) reply->format = LIST_ENTRY( ptr, struct clip_format, entry )->id;
534 /* empty the clipboard and grab ownership */
535 DECL_HANDLER(empty_clipboard)
537 struct clipboard *clipboard = get_process_clipboard();
539 if (!clipboard) return;
541 if (clipboard->open_thread != current)
543 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
544 return;
547 free_clipboard_formats( clipboard );
548 clipboard->owner = clipboard->open_win;
549 clipboard->seqno++;
553 /* release ownership of the clipboard */
554 DECL_HANDLER(release_clipboard)
556 struct clipboard *clipboard = get_process_clipboard();
557 user_handle_t owner;
559 if (!clipboard) return;
561 if (!(owner = get_valid_window_handle( req->owner ))) return;
563 if (clipboard->owner == owner)
565 reply->viewer = release_clipboard( clipboard );
566 reply->owner = clipboard->owner;
568 else set_error( STATUS_INVALID_OWNER );
572 /* get clipboard information */
573 DECL_HANDLER(get_clipboard_info)
575 struct clipboard *clipboard = get_process_clipboard();
577 if (!clipboard) return;
579 reply->window = clipboard->open_win;
580 reply->owner = clipboard->owner;
581 reply->viewer = clipboard->viewer;
582 reply->seqno = clipboard->seqno;
586 /* set the clipboard viewer window */
587 DECL_HANDLER(set_clipboard_viewer)
589 struct clipboard *clipboard = get_process_clipboard();
590 user_handle_t viewer = 0, previous = 0;
592 if (!clipboard) return;
593 if (req->viewer && !(viewer = get_valid_window_handle( req->viewer ))) return;
594 if (req->previous && !(previous = get_valid_window_handle( req->previous ))) return;
596 reply->old_viewer = clipboard->viewer;
597 reply->owner = clipboard->owner;
599 if (!previous || clipboard->viewer == previous)
600 clipboard->viewer = viewer;
601 else
602 set_error( STATUS_PENDING ); /* need to send message instead */
606 /* add a clipboard listener window */
607 DECL_HANDLER(add_clipboard_listener)
609 struct clipboard *clipboard = get_process_clipboard();
610 user_handle_t win;
612 if (!clipboard) return;
613 if (!(win = get_valid_window_handle( req->window ))) return;
615 add_listener( clipboard, win );
619 /* remove a clipboard listener window */
620 DECL_HANDLER(remove_clipboard_listener)
622 struct clipboard *clipboard = get_process_clipboard();
623 user_handle_t win;
625 if (!clipboard) return;
626 if (!(win = get_valid_window_handle( req->window ))) return;
628 if (!remove_listener( clipboard, win )) set_error( STATUS_INVALID_PARAMETER );