2 * Server-side clipboard management
4 * Copyright (C) 2002 Ulrich Czekalla
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
41 struct object obj
; /* object header */
42 struct thread
*open_thread
; /* thread id that has clipboard open */
43 user_handle_t open_win
; /* window that has clipboard open */
44 struct thread
*owner_thread
; /* thread id that owns the clipboard */
45 user_handle_t owner_win
; /* window that owns the clipboard data */
46 user_handle_t viewer
; /* first window in clipboard viewer list */
47 unsigned int seqno
; /* clipboard change sequence number */
48 timeout_t seqno_timestamp
; /* time stamp of last seqno increment */
51 static void clipboard_dump( struct object
*obj
, int verbose
);
53 static const struct object_ops clipboard_ops
=
55 sizeof(struct clipboard
), /* size */
56 clipboard_dump
, /* dump */
57 no_get_type
, /* get_type */
58 no_add_queue
, /* add_queue */
59 NULL
, /* remove_queue */
62 no_signal
, /* signal */
63 no_get_fd
, /* get_fd */
64 no_map_access
, /* map_access */
65 default_get_sd
, /* get_sd */
66 default_set_sd
, /* set_sd */
67 no_lookup_name
, /* lookup_name */
68 no_link_name
, /* link_name */
69 NULL
, /* unlink_name */
70 no_open_file
, /* open_file */
71 no_close_handle
, /* close_handle */
72 no_destroy
/* destroy */
76 #define MINUPDATELAPSE (2 * TICKS_PER_SEC)
78 /* dump a clipboard object */
79 static void clipboard_dump( struct object
*obj
, int verbose
)
81 struct clipboard
*clipboard
= (struct clipboard
*)obj
;
83 fprintf( stderr
, "Clipboard open_thread=%p open_win=%08x owner_thread=%p owner_win=%08x viewer=%08x seq=%u\n",
84 clipboard
->open_thread
, clipboard
->open_win
, clipboard
->owner_thread
,
85 clipboard
->owner_win
, clipboard
->viewer
, clipboard
->seqno
);
88 /* retrieve the clipboard info for the current process, allocating it if needed */
89 static struct clipboard
*get_process_clipboard(void)
91 struct clipboard
*clipboard
;
92 struct winstation
*winstation
= get_process_winstation( current
->process
, WINSTA_ACCESSCLIPBOARD
);
94 if (!winstation
) return NULL
;
96 if (!(clipboard
= winstation
->clipboard
))
98 if ((clipboard
= alloc_object( &clipboard_ops
)))
100 clipboard
->open_thread
= NULL
;
101 clipboard
->open_win
= 0;
102 clipboard
->owner_thread
= NULL
;
103 clipboard
->owner_win
= 0;
104 clipboard
->viewer
= 0;
105 clipboard
->seqno
= 0;
106 clipboard
->seqno_timestamp
= 0;
107 winstation
->clipboard
= clipboard
;
110 release_object( winstation
);
114 /* cleanup clipboard information upon window destruction */
115 void cleanup_clipboard_window( struct desktop
*desktop
, user_handle_t window
)
117 struct clipboard
*clipboard
= desktop
->winstation
->clipboard
;
119 if (!clipboard
) return;
121 if (clipboard
->open_win
== window
)
123 clipboard
->open_win
= 0;
124 clipboard
->open_thread
= NULL
;
126 if (clipboard
->owner_win
== window
)
128 clipboard
->owner_win
= 0;
129 clipboard
->owner_thread
= NULL
;
131 if (clipboard
->viewer
== window
) clipboard
->viewer
= 0;
134 /* Called when thread terminates to allow release of clipboard */
135 void cleanup_clipboard_thread(struct thread
*thread
)
137 struct clipboard
*clipboard
;
138 struct winstation
*winstation
;
140 if (!thread
->process
->winstation
) return;
141 if (!(winstation
= get_process_winstation( thread
->process
, WINSTA_ACCESSCLIPBOARD
))) return;
143 if ((clipboard
= winstation
->clipboard
))
145 if (thread
== clipboard
->open_thread
)
147 clipboard
->open_win
= 0;
148 clipboard
->open_thread
= NULL
;
150 if (thread
== clipboard
->owner_thread
)
152 clipboard
->owner_win
= 0;
153 clipboard
->owner_thread
= NULL
;
156 release_object( winstation
);
159 static int release_clipboard_owner( struct clipboard
*clipboard
, user_handle_t win
)
161 if ((clipboard
->open_thread
&& clipboard
->open_thread
->process
!= current
->process
) ||
162 (win
&& clipboard
->owner_win
!= get_user_full_handle( win
)))
164 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
167 clipboard
->owner_win
= 0;
168 clipboard
->owner_thread
= NULL
;
173 static int get_seqno( struct clipboard
*clipboard
)
175 if (!clipboard
->owner_thread
&& (current_time
- clipboard
->seqno_timestamp
> MINUPDATELAPSE
))
177 clipboard
->seqno_timestamp
= current_time
;
180 return clipboard
->seqno
;
184 /* open the clipboard */
185 DECL_HANDLER(open_clipboard
)
187 struct clipboard
*clipboard
= get_process_clipboard();
190 if (!clipboard
) return;
191 win
= get_user_full_handle( req
->window
);
193 if (clipboard
->open_thread
&& clipboard
->open_win
!= win
)
195 set_error( STATUS_WAS_LOCKED
);
198 clipboard
->open_win
= win
;
199 clipboard
->open_thread
= current
;
201 reply
->owner
= (clipboard
->owner_thread
&& clipboard
->owner_thread
->process
== current
->process
);
205 /* close the clipboard */
206 DECL_HANDLER(close_clipboard
)
208 struct clipboard
*clipboard
= get_process_clipboard();
210 if (!clipboard
) return;
212 if (clipboard
->open_thread
!= current
)
214 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
217 if (req
->changed
) clipboard
->seqno
++;
218 clipboard
->open_thread
= NULL
;
219 clipboard
->open_win
= 0;
221 reply
->viewer
= clipboard
->viewer
;
222 reply
->owner
= (clipboard
->owner_thread
&& clipboard
->owner_thread
->process
== current
->process
);
226 DECL_HANDLER(set_clipboard_info
)
228 struct clipboard
*clipboard
= get_process_clipboard();
230 if (!clipboard
) return;
232 reply
->old_clipboard
= clipboard
->open_win
;
233 reply
->old_owner
= clipboard
->owner_win
;
234 reply
->old_viewer
= clipboard
->viewer
;
236 if (req
->flags
& SET_CB_RELOWNER
)
238 if (!release_clipboard_owner( clipboard
, req
->owner
)) return;
241 if (req
->flags
& SET_CB_SEQNO
) clipboard
->seqno
++;
243 reply
->seqno
= get_seqno( clipboard
);
245 if (clipboard
->open_thread
) reply
->flags
|= CB_OPEN_ANY
;
246 if (clipboard
->open_thread
== current
) reply
->flags
|= CB_OPEN
;
247 if (clipboard
->owner_thread
== current
) reply
->flags
|= CB_OWNER
;
248 if (clipboard
->owner_thread
&& clipboard
->owner_thread
->process
== current
->process
)
249 reply
->flags
|= CB_PROCESS
;
253 /* empty the clipboard and grab ownership */
254 DECL_HANDLER(empty_clipboard
)
256 struct clipboard
*clipboard
= get_process_clipboard();
258 if (!clipboard
) return;
260 if (clipboard
->open_thread
!= current
)
262 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN
);
265 clipboard
->owner_win
= clipboard
->open_win
;
266 clipboard
->owner_thread
= clipboard
->open_thread
;
271 /* Get clipboard information */
272 DECL_HANDLER(get_clipboard_info
)
274 struct clipboard
*clipboard
= get_process_clipboard();
276 if (!clipboard
) return;
278 reply
->window
= clipboard
->open_win
;
279 reply
->owner
= clipboard
->owner_win
;
280 reply
->viewer
= clipboard
->viewer
;
281 reply
->seqno
= get_seqno( clipboard
);
285 /* set the clipboard viewer window */
286 DECL_HANDLER(set_clipboard_viewer
)
288 struct clipboard
*clipboard
= get_process_clipboard();
290 if (!clipboard
) return;
292 reply
->old_viewer
= clipboard
->viewer
;
293 reply
->owner
= clipboard
->owner_win
;
295 if (!req
->previous
|| clipboard
->viewer
== get_user_full_handle( req
->previous
))
296 clipboard
->viewer
= get_user_full_handle( req
->viewer
);
298 set_error( STATUS_PENDING
); /* need to send message instead */