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
23 #include "wine/port.h"
31 #define WIN32_NO_STATUS
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 */
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 struct thread
*owner_thread
; /* thread id that owns the clipboard */
56 user_handle_t owner_win
; /* window that owns the clipboard data */
57 user_handle_t viewer
; /* first window in clipboard viewer list */
58 unsigned int lcid
; /* locale id to use for synthesizing text formats */
59 unsigned int seqno
; /* clipboard change sequence number */
60 unsigned int open_seqno
; /* sequence number at open time */
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 */
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
;
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
;
118 list_add_tail( &clipboard
->formats
, &format
->entry
);
119 clipboard
->format_count
++;
120 if (id
< CF_MAX
) clipboard
->format_map
|= 1 << id
;
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
);
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_thread=%p owner_win=%08x viewer=%08x seq=%u\n",
145 clipboard
->open_thread
, clipboard
->open_win
, clipboard
->owner_thread
,
146 clipboard
->owner_win
, 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_thread
= NULL
;
172 clipboard
->owner_win
= 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
);
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 ((format
= add_format( clipboard
, CF_LOCALE
)))
211 format
->seqno
= clipboard
->seqno
++;
213 format
->size
= sizeof(clipboard
->lcid
);
218 for (i
= 0; i
< sizeof(formats
) / sizeof(formats
[0]); 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];
224 if (!(format
= add_format( clipboard
, formats
[i
][0] ))) continue;
226 format
->seqno
= clipboard
->seqno
;
232 /* add a clipboard listener */
233 static void add_listener( struct clipboard
*clipboard
, user_handle_t window
)
237 for (i
= 0; i
< clipboard
->listen_count
; i
++)
239 if (clipboard
->listeners
[i
] != window
) continue;
240 set_error( STATUS_INVALID_PARAMETER
); /* already set */
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) );
249 set_error( STATUS_NO_MEMORY
);
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
)
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
--;
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
)
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
;
300 clipboard
->owner_win
= 0;
301 clipboard
->owner_thread
= NULL
;
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
--;
314 if (!changed
) return 0;
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_win
== 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_win
, 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
->owner_thread
) clipboard
->owner_thread
= NULL
;
348 if (thread
== clipboard
->open_thread
)
350 user_handle_t viewer
= close_clipboard( clipboard
);
351 if (viewer
) send_notify_message( viewer
, WM_DRAWCLIPBOARD
, clipboard
->owner_win
, 0 );
354 release_object( winstation
);
357 static int release_clipboard_owner( struct clipboard
*clipboard
, user_handle_t win
)
359 if ((clipboard
->open_thread
&& clipboard
->open_thread
->process
!= current
->process
) ||
360 (win
&& clipboard
->owner_win
!= get_user_full_handle( win
)))
362 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
365 clipboard
->owner_win
= 0;
366 clipboard
->owner_thread
= NULL
;
371 /* open the clipboard */
372 DECL_HANDLER(open_clipboard
)
374 struct clipboard
*clipboard
= get_process_clipboard();
375 user_handle_t win
= 0;
377 if (!clipboard
) return;
378 if (req
->window
&& !(win
= get_valid_window_handle( req
->window
))) return;
380 if (clipboard
->open_thread
&& clipboard
->open_win
!= win
)
382 set_error( STATUS_INVALID_LOCK_SEQUENCE
);
386 if (!clipboard
->open_thread
) clipboard
->open_seqno
= clipboard
->seqno
; /* first open */
387 clipboard
->open_win
= win
;
388 clipboard
->open_thread
= current
;
390 reply
->owner
= clipboard
->owner_win
;
394 /* close the clipboard */
395 DECL_HANDLER(close_clipboard
)
397 struct clipboard
*clipboard
= get_process_clipboard();
399 if (!clipboard
) return;
401 if (clipboard
->open_thread
!= current
)
403 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
406 reply
->viewer
= close_clipboard( clipboard
);
407 reply
->owner
= clipboard
->owner_win
;
411 DECL_HANDLER(set_clipboard_info
)
413 struct clipboard
*clipboard
= get_process_clipboard();
415 if (!clipboard
) return;
417 reply
->old_clipboard
= clipboard
->open_win
;
418 reply
->old_owner
= clipboard
->owner_win
;
419 reply
->old_viewer
= clipboard
->viewer
;
421 if (req
->flags
& SET_CB_RELOWNER
)
423 if (!release_clipboard_owner( clipboard
, req
->owner
)) return;
426 if (req
->flags
& SET_CB_SEQNO
) clipboard
->seqno
++;
428 reply
->seqno
= clipboard
->seqno
;
430 if (clipboard
->open_thread
) reply
->flags
|= CB_OPEN_ANY
;
431 if (clipboard
->open_thread
== current
) reply
->flags
|= CB_OPEN
;
432 if (clipboard
->owner_thread
== current
) reply
->flags
|= CB_OWNER
;
433 if (clipboard
->owner_thread
&& clipboard
->owner_thread
->process
== current
->process
)
434 reply
->flags
|= CB_PROCESS
;
438 /* add a data format to the clipboard */
439 DECL_HANDLER(set_clipboard_data
)
441 struct clip_format
*format
;
442 struct clipboard
*clipboard
= get_process_clipboard();
445 if (!clipboard
) return;
447 if (!req
->format
|| !clipboard
->open_thread
)
449 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
453 if (get_req_data_size() && !(data
= memdup( get_req_data(), get_req_data_size() ))) return;
455 if (!(format
= get_format( clipboard
, req
->format
)))
457 if (!(format
= add_format( clipboard
, req
->format
)))
464 free( format
->data
);
466 format
->seqno
= clipboard
->seqno
++;
467 format
->size
= get_req_data_size();
470 if (req
->format
== CF_TEXT
|| req
->format
== CF_OEMTEXT
|| req
->format
== CF_UNICODETEXT
)
471 clipboard
->lcid
= req
->lcid
;
473 reply
->seqno
= format
->seqno
;
477 /* fetch a data format from the clipboard */
478 DECL_HANDLER(get_clipboard_data
)
480 struct clip_format
*format
;
481 struct clipboard
*clipboard
= get_process_clipboard();
483 if (!clipboard
) return;
485 if (clipboard
->open_thread
!= current
)
487 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
490 if (!(format
= get_format( clipboard
, req
->format
)))
492 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
495 reply
->from
= format
->from
;
496 reply
->total
= format
->size
;
497 reply
->seqno
= format
->seqno
;
498 if (!format
->data
&& !format
->from
) reply
->owner
= clipboard
->owner_win
;
499 if (req
->cached
&& req
->seqno
== format
->seqno
) return; /* client-side cache still valid */
500 if (format
->size
<= get_reply_max_size()) set_reply_data( format
->data
, format
->size
);
501 else set_error( STATUS_BUFFER_OVERFLOW
);
505 /* retrieve a list of available formats */
506 DECL_HANDLER(get_clipboard_formats
)
508 struct clipboard
*clipboard
= get_process_clipboard();
510 if (!clipboard
) return;
514 struct clip_format
*format
;
515 unsigned int i
= 0, *ptr
;
516 data_size_t size
= clipboard
->format_count
* sizeof(unsigned int);
518 reply
->count
= clipboard
->format_count
;
519 if (size
<= get_reply_max_size())
521 if ((ptr
= mem_alloc( size
)))
523 LIST_FOR_EACH_ENTRY( format
, &clipboard
->formats
, struct clip_format
, entry
)
524 ptr
[i
++] = format
->id
;
525 assert( i
== clipboard
->format_count
);
526 set_reply_data_ptr( ptr
, size
);
529 else set_error( STATUS_BUFFER_TOO_SMALL
);
531 else reply
->count
= (get_format( clipboard
, req
->format
) != NULL
); /* query a single format */
535 /* retrieve the next available format */
536 DECL_HANDLER(enum_clipboard_formats
)
539 struct clipboard
*clipboard
= get_process_clipboard();
541 if (!clipboard
) return;
543 if (clipboard
->open_thread
!= current
)
545 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
549 ptr
= list_head( &clipboard
->formats
);
552 while (ptr
&& LIST_ENTRY( ptr
, struct clip_format
, entry
)->id
!= req
->previous
)
553 ptr
= list_next( &clipboard
->formats
, ptr
);
554 if (ptr
) ptr
= list_next( &clipboard
->formats
, ptr
);
556 if (ptr
) reply
->format
= LIST_ENTRY( ptr
, struct clip_format
, entry
)->id
;
560 /* empty the clipboard and grab ownership */
561 DECL_HANDLER(empty_clipboard
)
563 struct clipboard
*clipboard
= get_process_clipboard();
565 if (!clipboard
) return;
567 if (clipboard
->open_thread
!= current
)
569 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
573 free_clipboard_formats( clipboard
);
574 clipboard
->owner_win
= clipboard
->open_win
;
575 clipboard
->owner_thread
= clipboard
->open_thread
;
580 /* release ownership of the clipboard */
581 DECL_HANDLER(release_clipboard
)
583 struct clipboard
*clipboard
= get_process_clipboard();
586 if (!clipboard
) return;
588 if (!(owner
= get_valid_window_handle( req
->owner
))) return;
590 if (clipboard
->owner_win
== owner
)
592 reply
->viewer
= release_clipboard( clipboard
);
593 reply
->owner
= clipboard
->owner_win
;
595 else set_error( STATUS_INVALID_OWNER
);
599 /* get clipboard information */
600 DECL_HANDLER(get_clipboard_info
)
602 struct clipboard
*clipboard
= get_process_clipboard();
604 if (!clipboard
) return;
606 reply
->window
= clipboard
->open_win
;
607 reply
->owner
= clipboard
->owner_win
;
608 reply
->viewer
= clipboard
->viewer
;
609 reply
->seqno
= clipboard
->seqno
;
613 /* set the clipboard viewer window */
614 DECL_HANDLER(set_clipboard_viewer
)
616 struct clipboard
*clipboard
= get_process_clipboard();
617 user_handle_t viewer
= 0, previous
= 0;
619 if (!clipboard
) return;
620 if (req
->viewer
&& !(viewer
= get_valid_window_handle( req
->viewer
))) return;
621 if (req
->previous
&& !(previous
= get_valid_window_handle( req
->previous
))) return;
623 reply
->old_viewer
= clipboard
->viewer
;
624 reply
->owner
= clipboard
->owner_win
;
626 if (!previous
|| clipboard
->viewer
== previous
)
627 clipboard
->viewer
= viewer
;
629 set_error( STATUS_PENDING
); /* need to send message instead */
633 /* add a clipboard listener window */
634 DECL_HANDLER(add_clipboard_listener
)
636 struct clipboard
*clipboard
= get_process_clipboard();
639 if (!clipboard
) return;
640 if (!(win
= get_valid_window_handle( req
->window
))) return;
642 add_listener( clipboard
, win
);
646 /* remove a clipboard listener window */
647 DECL_HANDLER(remove_clipboard_listener
)
649 struct clipboard
*clipboard
= get_process_clipboard();
652 if (!clipboard
) return;
653 if (!(win
= get_valid_window_handle( req
->window
))) return;
655 if (!remove_listener( clipboard
, win
)) set_error( STATUS_INVALID_PARAMETER
);