Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side clipboard management |
| 3 | * |
| 4 | * Copyright (C) 2002 Ulrich Czekalla |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
| 24 | #include <assert.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 29 | #include "ntstatus.h" |
| 30 | #define WIN32_NO_STATUS |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 31 | #include "request.h" |
| 32 | #include "object.h" |
Alexandre Julliard | e4faa12 | 2009-12-01 15:10:23 +0100 | [diff] [blame] | 33 | #include "process.h" |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 34 | #include "user.h" |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 35 | #include "winuser.h" |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 36 | #include "winternl.h" |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 37 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 38 | struct clipboard |
| 39 | { |
| 40 | struct object obj; /* object header */ |
| 41 | struct thread *open_thread; /* thread id that has clipboard open */ |
| 42 | user_handle_t open_win; /* window that has clipboard open */ |
| 43 | struct thread *owner_thread; /* thread id that owns the clipboard */ |
| 44 | user_handle_t owner_win; /* window that owns the clipboard data */ |
| 45 | user_handle_t viewer; /* first window in clipboard viewer list */ |
| 46 | unsigned int seqno; /* clipboard change sequence number */ |
| 47 | time_t seqno_timestamp; /* time stamp of last seqno increment */ |
| 48 | }; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 49 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 50 | static void clipboard_dump( struct object *obj, int verbose ); |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 51 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 52 | static const struct object_ops clipboard_ops = |
| 53 | { |
| 54 | sizeof(struct clipboard), /* size */ |
| 55 | clipboard_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 56 | no_get_type, /* get_type */ |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 57 | no_add_queue, /* add_queue */ |
| 58 | NULL, /* remove_queue */ |
| 59 | NULL, /* signaled */ |
| 60 | NULL, /* satisfied */ |
| 61 | no_signal, /* signal */ |
| 62 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 63 | no_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 64 | default_get_sd, /* get_sd */ |
| 65 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 66 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 67 | no_open_file, /* open_file */ |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 68 | no_close_handle, /* close_handle */ |
| 69 | no_destroy /* destroy */ |
| 70 | }; |
| 71 | |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 72 | |
| 73 | #define MINUPDATELAPSE 2 |
| 74 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 75 | /* dump a clipboard object */ |
| 76 | static void clipboard_dump( struct object *obj, int verbose ) |
| 77 | { |
| 78 | struct clipboard *clipboard = (struct clipboard *)obj; |
| 79 | |
Alexandre Julliard | d764107 | 2008-12-08 16:57:38 +0100 | [diff] [blame] | 80 | fprintf( stderr, "Clipboard open_thread=%p open_win=%08x owner_thread=%p owner_win=%08x viewer=%08x seq=%u\n", |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 81 | clipboard->open_thread, clipboard->open_win, clipboard->owner_thread, |
| 82 | clipboard->owner_win, clipboard->viewer, clipboard->seqno ); |
| 83 | } |
| 84 | |
| 85 | /* retrieve the clipboard info for the current process, allocating it if needed */ |
| 86 | static struct clipboard *get_process_clipboard(void) |
| 87 | { |
| 88 | struct clipboard *clipboard; |
| 89 | struct winstation *winstation = get_process_winstation( current->process, WINSTA_ACCESSCLIPBOARD ); |
| 90 | |
| 91 | if (!winstation) return NULL; |
| 92 | |
Alexandre Julliard | 499e343 | 2005-07-11 10:55:53 +0000 | [diff] [blame] | 93 | if (!(clipboard = winstation->clipboard)) |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 94 | { |
| 95 | if ((clipboard = alloc_object( &clipboard_ops ))) |
| 96 | { |
| 97 | clipboard->open_thread = NULL; |
| 98 | clipboard->open_win = 0; |
| 99 | clipboard->owner_thread = NULL; |
| 100 | clipboard->owner_win = 0; |
| 101 | clipboard->viewer = 0; |
| 102 | clipboard->seqno = 0; |
| 103 | clipboard->seqno_timestamp = 0; |
Alexandre Julliard | 499e343 | 2005-07-11 10:55:53 +0000 | [diff] [blame] | 104 | winstation->clipboard = clipboard; |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | release_object( winstation ); |
| 108 | return clipboard; |
| 109 | } |
| 110 | |
| 111 | |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 112 | /* Called when thread terminates to allow release of clipboard */ |
| 113 | void cleanup_clipboard_thread(struct thread *thread) |
| 114 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 115 | struct clipboard *clipboard; |
Alexandre Julliard | e4faa12 | 2009-12-01 15:10:23 +0100 | [diff] [blame] | 116 | struct winstation *winstation; |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 117 | |
Alexandre Julliard | e4faa12 | 2009-12-01 15:10:23 +0100 | [diff] [blame] | 118 | if (!thread->process->winstation) return; |
| 119 | if (!(winstation = get_process_winstation( thread->process, WINSTA_ACCESSCLIPBOARD ))) return; |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 120 | |
Alexandre Julliard | 499e343 | 2005-07-11 10:55:53 +0000 | [diff] [blame] | 121 | if ((clipboard = winstation->clipboard)) |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 122 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 123 | if (thread == clipboard->open_thread) |
| 124 | { |
| 125 | clipboard->open_win = 0; |
| 126 | clipboard->open_thread = NULL; |
| 127 | } |
| 128 | if (thread == clipboard->owner_thread) |
| 129 | { |
| 130 | clipboard->owner_win = 0; |
| 131 | clipboard->owner_thread = NULL; |
| 132 | } |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 133 | } |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 134 | release_object( winstation ); |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 137 | static int set_clipboard_window( struct clipboard *clipboard, user_handle_t win, int clear ) |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 138 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 139 | if (clipboard->open_thread && clipboard->open_thread != current) |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 140 | { |
| 141 | set_error(STATUS_WAS_LOCKED); |
| 142 | return 0; |
| 143 | } |
| 144 | else if (!clear) |
| 145 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 146 | clipboard->open_win = win; |
| 147 | clipboard->open_thread = current; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 148 | } |
| 149 | else |
| 150 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 151 | clipboard->open_thread = NULL; |
| 152 | clipboard->open_win = 0; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 153 | } |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 158 | static int set_clipboard_owner( struct clipboard *clipboard, user_handle_t win, int clear ) |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 159 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 160 | if (clipboard->open_thread && clipboard->open_thread->process != current->process) |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 161 | { |
| 162 | set_error(STATUS_WAS_LOCKED); |
| 163 | return 0; |
| 164 | } |
Dmitry Timoshkov | b3569e7 | 2004-06-25 02:55:37 +0000 | [diff] [blame] | 165 | else if (!clear) |
| 166 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 167 | clipboard->owner_win = win; |
| 168 | clipboard->owner_thread = current; |
Dmitry Timoshkov | b3569e7 | 2004-06-25 02:55:37 +0000 | [diff] [blame] | 169 | } |
| 170 | else |
| 171 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 172 | clipboard->owner_win = 0; |
| 173 | clipboard->owner_thread = NULL; |
Dmitry Timoshkov | b3569e7 | 2004-06-25 02:55:37 +0000 | [diff] [blame] | 174 | } |
| 175 | return 1; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 179 | static int get_seqno( struct clipboard *clipboard ) |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 180 | { |
| 181 | time_t tm = time(NULL); |
| 182 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 183 | if (!clipboard->owner_thread && (tm > (clipboard->seqno_timestamp + MINUPDATELAPSE))) |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 184 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 185 | clipboard->seqno_timestamp = tm; |
| 186 | clipboard->seqno++; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 187 | } |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 188 | return clipboard->seqno; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | DECL_HANDLER(set_clipboard_info) |
| 193 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 194 | struct clipboard *clipboard = get_process_clipboard(); |
| 195 | |
| 196 | if (!clipboard) return; |
| 197 | |
| 198 | reply->old_clipboard = clipboard->open_win; |
| 199 | reply->old_owner = clipboard->owner_win; |
| 200 | reply->old_viewer = clipboard->viewer; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 201 | |
| 202 | if (req->flags & SET_CB_OPEN) |
| 203 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 204 | if (clipboard->open_thread) |
Dmitry Timoshkov | b3569e7 | 2004-06-25 02:55:37 +0000 | [diff] [blame] | 205 | { |
| 206 | /* clipboard already opened */ |
| 207 | set_error(STATUS_WAS_LOCKED); |
| 208 | return; |
| 209 | } |
| 210 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 211 | if (!set_clipboard_window( clipboard, req->clipboard, 0 )) return; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 212 | } |
| 213 | else if (req->flags & SET_CB_CLOSE) |
| 214 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 215 | if (clipboard->open_thread != current) |
Dmitry Timoshkov | b3569e7 | 2004-06-25 02:55:37 +0000 | [diff] [blame] | 216 | { |
| 217 | set_win32_error(ERROR_CLIPBOARD_NOT_OPEN); |
| 218 | return; |
| 219 | } |
| 220 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 221 | if (!set_clipboard_window( clipboard, 0, 1 )) return; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | if (req->flags & SET_CB_OWNER) |
| 225 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 226 | if (!set_clipboard_owner( clipboard, req->owner, 0 )) return; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 227 | } |
| 228 | else if (req->flags & SET_CB_RELOWNER) |
| 229 | { |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 230 | if (!set_clipboard_owner( clipboard, 0, 1 )) return; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 233 | if (req->flags & SET_CB_VIEWER) clipboard->viewer = req->viewer; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 234 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 235 | if (req->flags & SET_CB_SEQNO) clipboard->seqno++; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 236 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 237 | reply->seqno = get_seqno( clipboard ); |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 238 | |
Alexandre Julliard | 45128bd | 2005-06-29 20:13:36 +0000 | [diff] [blame] | 239 | if (clipboard->open_thread == current) reply->flags |= CB_OPEN; |
| 240 | if (clipboard->owner_thread == current) reply->flags |= CB_OWNER; |
| 241 | if (clipboard->owner_thread && clipboard->owner_thread->process == current->process) |
Ulrich Czekalla | 5067909 | 2005-03-07 19:31:46 +0000 | [diff] [blame] | 242 | reply->flags |= CB_PROCESS; |
Ulrich Czekalla | b2df5f9 | 2003-06-23 23:02:02 +0000 | [diff] [blame] | 243 | } |