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 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 */
76 clipboard_dump
, /* dump */
77 no_add_queue
, /* add_queue */
78 NULL
, /* remove_queue */
81 no_signal
, /* signal */
82 no_get_fd
, /* get_fd */
83 default_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
;
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
;
120 list_add_tail( &clipboard
->formats
, &format
->entry
);
121 clipboard
->format_count
++;
122 if (id
< CF_MAX
) clipboard
->format_map
|= 1 << id
;
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
);
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
);
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
++;
214 format
->size
= sizeof(clipboard
->lcid
);
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];
225 if (!(format
= add_format( clipboard
, formats
[i
][0] ))) continue;
227 format
->seqno
= clipboard
->seqno
;
233 /* add a clipboard listener */
234 static void add_listener( struct clipboard
*clipboard
, user_handle_t window
)
238 for (i
= 0; i
< clipboard
->listen_count
; i
++)
240 if (clipboard
->listeners
[i
] != window
) continue;
241 set_error( STATUS_INVALID_PARAMETER
); /* already set */
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) );
250 set_error( STATUS_NO_MEMORY
);
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
)
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
--;
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
)
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
;
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
) continue;
307 /* format->from is earlier in the list and thus has already been
308 * removed if not available anymore (it is also < CF_MAX)
310 if (format
->from
&& HAS_FORMAT( clipboard
->format_map
, format
->from
)) continue;
311 list_remove( &format
->entry
);
312 if (format
->id
< CF_MAX
) clipboard
->format_map
&= ~(1 << format
->id
);
313 clipboard
->format_count
--;
318 if (!changed
) return 0;
320 return notify_listeners( clipboard
);
323 /* cleanup clipboard information upon window destruction */
324 void cleanup_clipboard_window( struct desktop
*desktop
, user_handle_t window
)
326 struct clipboard
*clipboard
= desktop
->winstation
->clipboard
;
328 if (!clipboard
) return;
330 remove_listener( clipboard
, window
);
331 if (clipboard
->viewer
== window
) clipboard
->viewer
= 0;
332 if (clipboard
->owner
== window
) release_clipboard( clipboard
);
333 if (clipboard
->open_win
== window
)
335 user_handle_t viewer
= close_clipboard( clipboard
);
336 if (viewer
) send_notify_message( viewer
, WM_DRAWCLIPBOARD
, clipboard
->owner
, 0 );
340 /* Called when thread terminates to allow release of clipboard */
341 void cleanup_clipboard_thread(struct thread
*thread
)
343 struct clipboard
*clipboard
;
344 struct winstation
*winstation
;
346 if (!thread
->process
->winstation
) return;
347 if (!(winstation
= get_process_winstation( thread
->process
, WINSTA_ACCESSCLIPBOARD
))) return;
349 if ((clipboard
= winstation
->clipboard
))
351 if (thread
== clipboard
->open_thread
)
353 user_handle_t viewer
= close_clipboard( clipboard
);
354 if (viewer
) send_notify_message( viewer
, WM_DRAWCLIPBOARD
, clipboard
->owner
, 0 );
357 release_object( winstation
);
360 /* open the clipboard */
361 DECL_HANDLER(open_clipboard
)
363 struct clipboard
*clipboard
= get_process_clipboard();
364 user_handle_t win
= 0;
366 if (!clipboard
) return;
367 if (req
->window
&& !(win
= get_valid_window_handle( req
->window
))) return;
369 if (clipboard
->open_thread
&& clipboard
->open_win
!= win
)
371 set_error( STATUS_INVALID_LOCK_SEQUENCE
);
375 if (!clipboard
->open_thread
) clipboard
->open_seqno
= clipboard
->seqno
; /* first open */
376 if (clipboard
->open_thread
!= current
) clipboard
->rendering
= 0;
377 clipboard
->open_win
= win
;
378 clipboard
->open_thread
= current
;
380 reply
->owner
= clipboard
->owner
;
384 /* close the clipboard */
385 DECL_HANDLER(close_clipboard
)
387 struct clipboard
*clipboard
= get_process_clipboard();
389 if (!clipboard
) return;
391 if (clipboard
->open_thread
!= current
)
393 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
396 reply
->viewer
= close_clipboard( clipboard
);
397 reply
->owner
= clipboard
->owner
;
401 /* add a data format to the clipboard */
402 DECL_HANDLER(set_clipboard_data
)
404 struct clip_format
*format
;
405 struct clipboard
*clipboard
= get_process_clipboard();
408 if (!clipboard
) return;
410 if (!req
->format
|| !clipboard
->open_thread
)
412 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
416 if (get_req_data_size() && !(data
= memdup( get_req_data(), get_req_data_size() ))) return;
418 if (!(format
= get_format( clipboard
, req
->format
)))
420 if (!(format
= add_format( clipboard
, req
->format
)))
427 free( format
->data
);
429 format
->seqno
= clipboard
->seqno
;
430 format
->size
= get_req_data_size();
432 if (!clipboard
->rendering
) clipboard
->seqno
++;
434 if (req
->format
== CF_TEXT
|| req
->format
== CF_OEMTEXT
|| req
->format
== CF_UNICODETEXT
)
435 clipboard
->lcid
= req
->lcid
;
437 reply
->seqno
= format
->seqno
;
441 /* fetch a data format from the clipboard */
442 DECL_HANDLER(get_clipboard_data
)
444 struct clip_format
*format
;
445 struct clipboard
*clipboard
= get_process_clipboard();
447 if (!clipboard
) return;
449 if (clipboard
->open_thread
!= current
)
451 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
454 if (!(format
= get_format( clipboard
, req
->format
)))
456 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
459 reply
->from
= format
->from
;
460 reply
->total
= format
->size
;
461 reply
->seqno
= format
->seqno
;
462 reply
->owner
= clipboard
->owner
;
464 if (!format
->data
&& req
->render
) /* try rendering it client-side */
466 if (format
->from
|| clipboard
->owner
) clipboard
->rendering
++;
470 if (req
->cached
&& req
->seqno
== format
->seqno
) goto done
; /* client-side cache still valid */
472 if (format
->size
> get_reply_max_size())
474 set_error( STATUS_BUFFER_OVERFLOW
);
477 set_reply_data( format
->data
, format
->size
);
480 if (!req
->render
) clipboard
->rendering
--;
484 /* retrieve a list of available formats */
485 DECL_HANDLER(get_clipboard_formats
)
487 struct clipboard
*clipboard
= get_process_clipboard();
489 if (!clipboard
) return;
493 struct clip_format
*format
;
494 unsigned int i
= 0, *ptr
;
495 data_size_t size
= clipboard
->format_count
* sizeof(unsigned int);
497 reply
->count
= clipboard
->format_count
;
498 if (size
<= get_reply_max_size())
500 if ((ptr
= mem_alloc( size
)))
502 LIST_FOR_EACH_ENTRY( format
, &clipboard
->formats
, struct clip_format
, entry
)
503 ptr
[i
++] = format
->id
;
504 assert( i
== clipboard
->format_count
);
505 set_reply_data_ptr( ptr
, size
);
508 else set_error( STATUS_BUFFER_TOO_SMALL
);
510 else reply
->count
= (get_format( clipboard
, req
->format
) != NULL
); /* query a single format */
514 /* retrieve the next available format */
515 DECL_HANDLER(enum_clipboard_formats
)
518 struct clipboard
*clipboard
= get_process_clipboard();
520 if (!clipboard
) return;
522 if (clipboard
->open_thread
!= current
)
524 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
528 ptr
= list_head( &clipboard
->formats
);
531 while (ptr
&& LIST_ENTRY( ptr
, struct clip_format
, entry
)->id
!= req
->previous
)
532 ptr
= list_next( &clipboard
->formats
, ptr
);
533 if (ptr
) ptr
= list_next( &clipboard
->formats
, ptr
);
535 if (ptr
) reply
->format
= LIST_ENTRY( ptr
, struct clip_format
, entry
)->id
;
539 /* empty the clipboard and grab ownership */
540 DECL_HANDLER(empty_clipboard
)
542 struct clipboard
*clipboard
= get_process_clipboard();
544 if (!clipboard
) return;
546 if (clipboard
->open_thread
!= current
)
548 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
552 free_clipboard_formats( clipboard
);
553 clipboard
->owner
= clipboard
->open_win
;
558 /* release ownership of the clipboard */
559 DECL_HANDLER(release_clipboard
)
561 struct clipboard
*clipboard
= get_process_clipboard();
564 if (!clipboard
) return;
566 if (!(owner
= get_valid_window_handle( req
->owner
))) return;
568 if (clipboard
->owner
== owner
)
570 reply
->viewer
= release_clipboard( clipboard
);
571 reply
->owner
= clipboard
->owner
;
573 else set_error( STATUS_INVALID_OWNER
);
577 /* get clipboard information */
578 DECL_HANDLER(get_clipboard_info
)
580 struct clipboard
*clipboard
= get_process_clipboard();
582 if (!clipboard
) return;
584 reply
->window
= clipboard
->open_win
;
585 reply
->owner
= clipboard
->owner
;
586 reply
->viewer
= clipboard
->viewer
;
587 reply
->seqno
= clipboard
->seqno
;
591 /* set the clipboard viewer window */
592 DECL_HANDLER(set_clipboard_viewer
)
594 struct clipboard
*clipboard
= get_process_clipboard();
595 user_handle_t viewer
= 0, previous
= 0;
597 if (!clipboard
) return;
598 if (req
->viewer
&& !(viewer
= get_valid_window_handle( req
->viewer
))) return;
599 if (req
->previous
&& !(previous
= get_valid_window_handle( req
->previous
))) return;
601 reply
->old_viewer
= clipboard
->viewer
;
602 reply
->owner
= clipboard
->owner
;
604 if (!previous
|| clipboard
->viewer
== previous
)
605 clipboard
->viewer
= viewer
;
607 set_error( STATUS_PENDING
); /* need to send message instead */
611 /* add a clipboard listener window */
612 DECL_HANDLER(add_clipboard_listener
)
614 struct clipboard
*clipboard
= get_process_clipboard();
617 if (!clipboard
) return;
618 if (!(win
= get_valid_window_handle( req
->window
))) return;
620 add_listener( clipboard
, win
);
624 /* remove a clipboard listener window */
625 DECL_HANDLER(remove_clipboard_listener
)
627 struct clipboard
*clipboard
= get_process_clipboard();
630 if (!clipboard
) return;
631 if (!(win
= get_valid_window_handle( req
->window
))) return;
633 if (!remove_listener( clipboard
, win
)) set_error( STATUS_INVALID_PARAMETER
);