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