server: Add a separate request to set the clipboard viewer.
[wine.git] / server / clipboard.c
blobc906e70232463be51f89d0f3f443f383601bfdf3
1 /*
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
21 #include "config.h"
22 #include "wine/port.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 clipboard
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 */
60 NULL, /* signaled */
61 NULL, /* satisfied */
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 );
111 return clipboard;
115 /* Called when thread terminates to allow release of clipboard */
116 void cleanup_clipboard_thread(struct thread *thread)
118 struct clipboard *clipboard;
119 struct winstation *winstation;
121 if (!thread->process->winstation) return;
122 if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return;
124 if ((clipboard = winstation->clipboard))
126 if (thread == clipboard->open_thread)
128 clipboard->open_win = 0;
129 clipboard->open_thread = NULL;
131 if (thread == clipboard->owner_thread)
133 clipboard->owner_win = 0;
134 clipboard->owner_thread = NULL;
137 release_object( winstation );
140 static int release_clipboard_owner( struct clipboard *clipboard, user_handle_t win )
142 if ((clipboard->open_thread && clipboard->open_thread->process != current->process) ||
143 (win && clipboard->owner_win != get_user_full_handle( win )))
145 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
146 return 0;
148 clipboard->owner_win = 0;
149 clipboard->owner_thread = NULL;
150 return 1;
154 static int get_seqno( struct clipboard *clipboard )
156 if (!clipboard->owner_thread && (current_time - clipboard->seqno_timestamp > MINUPDATELAPSE))
158 clipboard->seqno_timestamp = current_time;
159 clipboard->seqno++;
161 return clipboard->seqno;
165 /* open the clipboard */
166 DECL_HANDLER(open_clipboard)
168 struct clipboard *clipboard = get_process_clipboard();
169 user_handle_t win;
171 if (!clipboard) return;
172 win = get_user_full_handle( req->window );
174 if (clipboard->open_thread && clipboard->open_win != win)
176 set_error( STATUS_WAS_LOCKED );
177 return;
179 clipboard->open_win = win;
180 clipboard->open_thread = current;
182 reply->owner = (clipboard->owner_thread && clipboard->owner_thread->process == current->process);
186 /* close the clipboard */
187 DECL_HANDLER(close_clipboard)
189 struct clipboard *clipboard = get_process_clipboard();
191 if (!clipboard) return;
193 if (clipboard->open_thread != current)
195 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
196 return;
198 if (req->changed) clipboard->seqno++;
199 clipboard->open_thread = NULL;
200 clipboard->open_win = 0;
202 reply->viewer = clipboard->viewer;
203 reply->owner = (clipboard->owner_thread && clipboard->owner_thread->process == current->process);
207 DECL_HANDLER(set_clipboard_info)
209 struct clipboard *clipboard = get_process_clipboard();
211 if (!clipboard) return;
213 reply->old_clipboard = clipboard->open_win;
214 reply->old_owner = clipboard->owner_win;
215 reply->old_viewer = clipboard->viewer;
217 if (req->flags & SET_CB_RELOWNER)
219 if (!release_clipboard_owner( clipboard, req->owner )) return;
222 if (req->flags & SET_CB_SEQNO) clipboard->seqno++;
224 reply->seqno = get_seqno( clipboard );
226 if (clipboard->open_thread == current) reply->flags |= CB_OPEN;
227 if (clipboard->owner_thread == current) reply->flags |= CB_OWNER;
228 if (clipboard->owner_thread && clipboard->owner_thread->process == current->process)
229 reply->flags |= CB_PROCESS;
233 /* empty the clipboard and grab ownership */
234 DECL_HANDLER(empty_clipboard)
236 struct clipboard *clipboard = get_process_clipboard();
238 if (!clipboard) return;
240 if (clipboard->open_thread != current)
242 set_win32_error( ERROR_CLIPBOARD_NOT_OPEN );
243 return;
245 clipboard->owner_win = clipboard->open_win;
246 clipboard->owner_thread = clipboard->open_thread;
247 clipboard->seqno++;
251 /* set the clipboard viewer window */
252 DECL_HANDLER(set_clipboard_viewer)
254 struct clipboard *clipboard = get_process_clipboard();
256 if (!clipboard) return;
258 reply->old_viewer = clipboard->viewer;
259 reply->owner = clipboard->owner_win;
261 if (!req->previous || clipboard->viewer == get_user_full_handle( req->previous ))
262 clipboard->viewer = get_user_full_handle( req->viewer );
263 else
264 set_error( STATUS_PENDING ); /* need to send message instead */