Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1 | /* |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 2 | * X11 clipboard windows driver |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright 1994 Martin Ayotte |
| 5 | * 1996 Alex Korobka |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 6 | * 1999 Noel Borthwick |
| 7 | * |
| 8 | * NOTES: |
| 9 | * This file contains the X specific implementation for the windows |
| 10 | * Clipboard API. |
| 11 | * |
| 12 | * Wine's internal clipboard is exposed to external apps via the X |
| 13 | * selection mechanism. |
| 14 | * Currently the driver asserts ownership via two selection atoms: |
| 15 | * 1. PRIMARY(XA_PRIMARY) |
| 16 | * 2. CLIPBOARD |
| 17 | * |
| 18 | * In our implementation, the CLIPBOARD selection takes precedence over PRIMARY. |
| 19 | * i.e. if a CLIPBOARD selection is available, it is used instead of PRIMARY. |
| 20 | * When Wine taks ownership of the clipboard, it takes ownership of BOTH selections. |
| 21 | * While giving up selection ownership, if the CLIPBOARD selection is lost, |
| 22 | * it will lose both PRIMARY and CLIPBOARD and empty the clipboard. |
| 23 | * However if only PRIMARY is lost, it will continue to hold the CLIPBOARD selection |
| 24 | * (leaving the clipboard cache content unaffected). |
| 25 | * |
| 26 | * Every format exposed via a windows clipboard format is also exposed through |
| 27 | * a corresponding X selection target. A selection target atom is synthesized |
| 28 | * whenever a new Windows clipboard format is registered via RegisterClipboardFormat, |
| 29 | * or when a built in format is used for the first time. |
| 30 | * Windows native format are exposed by prefixing the format name with "<WCF>" |
| 31 | * This allows us to uniquely identify windows native formats exposed by other |
| 32 | * running WINE apps. |
| 33 | * |
| 34 | * In order to allow external applications to query WINE for supported formats, |
| 35 | * we respond to the "TARGETS" selection target. (See EVENT_SelectionRequest |
| 36 | * for implementation) We use the same mechanism to query external clients for |
| 37 | * availability of a particular format, by cacheing the list of available targets |
| 38 | * by using the clipboard cache's "delayed render" mechanism. If a selection client |
| 39 | * does not support the "TARGETS" selection target, we actually attempt to retrieve |
| 40 | * the format requested as a fallback mechanism. |
| 41 | * |
| 42 | * Certain Windows native formats are automatically converted to X native formats |
| 43 | * and vice versa. If a native format is available in the selection, it takes |
| 44 | * precedence, in order to avoid unnecessary conversions. |
| 45 | * |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
Patrik Stridvall | 3d51161 | 2000-04-25 19:55:35 +0000 | [diff] [blame] | 48 | #include "ts_xlib.h" |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 49 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 50 | #include <string.h> |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 51 | #include <stdio.h> |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 52 | #include <stdlib.h> |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 53 | #include <unistd.h> |
Juergen Lock | 7cc0e27 | 1999-11-07 05:12:43 +0000 | [diff] [blame] | 54 | #include <fcntl.h> |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 55 | |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 56 | #include "clipboard.h" |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 57 | #include "win.h" |
| 58 | #include "x11drv.h" |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 59 | #include "options.h" |
| 60 | #include "debugtools.h" |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 61 | |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 62 | DEFAULT_DEBUG_CHANNEL(clipboard); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 63 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 64 | /* Selection masks */ |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 65 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 66 | #define S_NOSELECTION 0 |
| 67 | #define S_PRIMARY 1 |
| 68 | #define S_CLIPBOARD 2 |
| 69 | |
| 70 | /* X selection context info */ |
| 71 | |
| 72 | static char _CLIPBOARD[] = "CLIPBOARD"; /* CLIPBOARD atom name */ |
| 73 | static char FMT_PREFIX[] = "<WCF>"; /* Prefix for windows specific formats */ |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 74 | static int selectionAcquired = 0; /* Contains the current selection masks */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 75 | static Window selectionWindow = None; /* The top level X window which owns the selection */ |
| 76 | static Window selectionPrevWindow = None; /* The last X window that owned the selection */ |
| 77 | static Window PrimarySelectionOwner = None; /* The window which owns the primary selection */ |
| 78 | static Window ClipboardSelectionOwner = None; /* The window which owns the clipboard selection */ |
| 79 | static unsigned long cSelectionTargets = 0; /* Number of target formats reported by TARGETS selection */ |
| 80 | static Atom selectionCacheSrc = XA_PRIMARY; /* The selection source from which the clipboard cache was filled */ |
Niels Kristian Bech Jensen | c69a80c | 1999-11-28 20:31:04 +0000 | [diff] [blame] | 81 | static HANDLE selectionClearEvent = 0;/* Synchronization object used to block until server is started */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 82 | |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 83 | typedef struct tagPROPERTY |
| 84 | { |
| 85 | struct tagPROPERTY *next; |
| 86 | Atom atom; |
| 87 | Pixmap pixmap; |
| 88 | } PROPERTY; |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 89 | |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 90 | static PROPERTY *prop_head; |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 91 | |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 92 | |
| 93 | /************************************************************************** |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 94 | * X11DRV_CLIPBOARD_MapPropertyToFormat |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 95 | * |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 96 | * Map an X selection property type atom name to a windows clipboard format ID |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 97 | */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 98 | UINT X11DRV_CLIPBOARD_MapPropertyToFormat(char *itemFmtName) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 99 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 100 | /* |
| 101 | * If the property name starts with FMT_PREFIX strip this off and |
| 102 | * get the ID for a custom Windows registered format with this name. |
| 103 | * We can also understand STRING, PIXMAP and BITMAP. |
| 104 | */ |
| 105 | if ( NULL == itemFmtName ) |
| 106 | return 0; |
| 107 | else if ( 0 == strncmp(itemFmtName, FMT_PREFIX, strlen(FMT_PREFIX)) ) |
| 108 | return RegisterClipboardFormatA(itemFmtName + strlen(FMT_PREFIX)); |
| 109 | else if ( 0 == strcmp(itemFmtName, "STRING") ) |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 110 | return CF_UNICODETEXT; |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 111 | else if ( 0 == strcmp(itemFmtName, "PIXMAP") |
| 112 | || 0 == strcmp(itemFmtName, "BITMAP") ) |
| 113 | { |
| 114 | /* |
| 115 | * Return CF_DIB as first preference, if WINE is the selection owner |
| 116 | * and if CF_DIB exists in the cache. |
| 117 | * If wine dowsn't own the selection we always return CF_DIB |
| 118 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 119 | if ( !X11DRV_IsSelectionOwner() ) |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 120 | return CF_DIB; |
| 121 | else if ( CLIPBOARD_IsPresent(CF_DIB) ) |
| 122 | return CF_DIB; |
| 123 | else |
| 124 | return CF_BITMAP; |
| 125 | } |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 126 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 127 | WARN("\tNo mapping to Windows clipboard format for property %s\n", itemFmtName); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /************************************************************************** |
| 132 | * X11DRV_CLIPBOARD_MapFormatToProperty |
| 133 | * |
| 134 | * Map a windows clipboard format ID to an X selection property atom |
| 135 | */ |
| 136 | Atom X11DRV_CLIPBOARD_MapFormatToProperty(UINT wFormat) |
| 137 | { |
| 138 | Atom prop = None; |
| 139 | |
| 140 | switch (wFormat) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 141 | { |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 142 | /* We support only CF_UNICODETEXT, other formats are synthesized */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 143 | case CF_OEMTEXT: |
| 144 | case CF_TEXT: |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 145 | return None; |
| 146 | |
| 147 | case CF_UNICODETEXT: |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 148 | prop = XA_STRING; |
| 149 | break; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 150 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 151 | case CF_DIB: |
| 152 | case CF_BITMAP: |
| 153 | { |
| 154 | /* |
| 155 | * Request a PIXMAP, only if WINE is NOT the selection owner, |
| 156 | * AND the requested format is not in the cache. |
| 157 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 158 | if ( !X11DRV_IsSelectionOwner() && !CLIPBOARD_IsPresent(wFormat) ) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 159 | { |
| 160 | prop = XA_PIXMAP; |
| 161 | break; |
| 162 | } |
| 163 | /* Fall thru to the default case in order to use the native format */ |
| 164 | } |
| 165 | |
| 166 | default: |
| 167 | { |
| 168 | /* |
| 169 | * If an X atom is registered for this format, return that |
| 170 | * Otherwise register a new atom. |
| 171 | */ |
| 172 | char str[256]; |
| 173 | char *fmtName = CLIPBOARD_GetFormatName(wFormat); |
| 174 | strcpy(str, FMT_PREFIX); |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 175 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 176 | if (fmtName) |
| 177 | { |
| 178 | strncat(str, fmtName, sizeof(str) - strlen(FMT_PREFIX)); |
| 179 | prop = TSXInternAtom(display, str, False); |
| 180 | } |
| 181 | break; |
| 182 | } |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 183 | } |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 184 | |
| 185 | if (prop == None) |
| 186 | TRACE("\tNo mapping to X property for Windows clipboard format %d(%s)\n", |
| 187 | wFormat, CLIPBOARD_GetFormatName(wFormat)); |
| 188 | |
| 189 | return prop; |
| 190 | } |
| 191 | |
| 192 | /************************************************************************** |
| 193 | * X11DRV_CLIPBOARD_IsNativeProperty |
| 194 | * |
| 195 | * Checks if a property is a native property type |
| 196 | */ |
| 197 | BOOL X11DRV_CLIPBOARD_IsNativeProperty(Atom prop) |
| 198 | { |
| 199 | char *itemFmtName = TSXGetAtomName(display, prop); |
| 200 | BOOL bRet = FALSE; |
| 201 | |
| 202 | if ( 0 == strncmp(itemFmtName, FMT_PREFIX, strlen(FMT_PREFIX)) ) |
| 203 | bRet = TRUE; |
| 204 | |
| 205 | TSXFree(itemFmtName); |
| 206 | return bRet; |
| 207 | } |
| 208 | |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 209 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 210 | /************************************************************************** |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 211 | * X11DRV_CLIPBOARD_LaunchServer |
| 212 | * Launches the clipboard server. This is called from X11DRV_CLIPBOARD_ResetOwner |
| 213 | * when the selection can no longer be recyled to another top level window. |
| 214 | * In order to make the selection persist after Wine shuts down a server |
| 215 | * process is launched which services subsequent selection requests. |
| 216 | */ |
| 217 | BOOL X11DRV_CLIPBOARD_LaunchServer() |
| 218 | { |
| 219 | int iWndsLocks; |
| 220 | |
| 221 | /* If persistant selection has been disabled in the .winerc Clipboard section, |
| 222 | * don't launch the server |
| 223 | */ |
| 224 | if ( !PROFILE_GetWineIniInt("Clipboard", "PersistentSelection", 1) ) |
| 225 | return FALSE; |
| 226 | |
| 227 | /* Start up persistant WINE X clipboard server process which will |
| 228 | * take ownership of the X selection and continue to service selection |
| 229 | * requests from other apps. |
| 230 | */ |
| 231 | selectionWindow = selectionPrevWindow; |
| 232 | if ( !fork() ) |
| 233 | { |
| 234 | /* NOTE: This code only executes in the context of the child process |
| 235 | * Do note make any Wine specific calls here. |
| 236 | */ |
| 237 | |
| 238 | int dbgClasses = 0; |
| 239 | char selMask[8], dbgClassMask[8], clearSelection[8]; |
Juergen Lock | 7cc0e27 | 1999-11-07 05:12:43 +0000 | [diff] [blame] | 240 | int i; |
| 241 | |
| 242 | /* Don't inherit wine's X sockets to the wineclipsrv, otherwise |
| 243 | * windows stay around when you have to kill a hanging wine... |
| 244 | */ |
| 245 | for (i = 3; i < 256; ++i) |
| 246 | fcntl(i, F_SETFD, 1); |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 247 | |
| 248 | sprintf(selMask, "%d", selectionAcquired); |
| 249 | |
| 250 | /* Build the debug class mask to pass to the server, by inheriting |
| 251 | * the settings for the clipboard debug channel. |
| 252 | */ |
Jeremy White | d3e22d9 | 2000-02-10 19:03:02 +0000 | [diff] [blame] | 253 | dbgClasses |= FIXME_ON(clipboard) ? 1 : 0; |
| 254 | dbgClasses |= ERR_ON(clipboard) ? 2 : 0; |
| 255 | dbgClasses |= WARN_ON(clipboard) ? 4 : 0; |
| 256 | dbgClasses |= TRACE_ON(clipboard) ? 8 : 0; |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 257 | sprintf(dbgClassMask, "%d", dbgClasses); |
| 258 | |
| 259 | /* Get the clear selection preference */ |
| 260 | sprintf(clearSelection, "%d", |
| 261 | PROFILE_GetWineIniInt("Clipboard", "ClearAllSelections", 0)); |
Juergen Lock | 7cc0e27 | 1999-11-07 05:12:43 +0000 | [diff] [blame] | 262 | |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 263 | /* Exec the clipboard server passing it the selection and debug class masks */ |
Juergen Lock | 7cc0e27 | 1999-11-07 05:12:43 +0000 | [diff] [blame] | 264 | execl( BINDIR "/wineclipsrv", "wineclipsrv", |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 265 | selMask, dbgClassMask, clearSelection, NULL ); |
Juergen Lock | 7cc0e27 | 1999-11-07 05:12:43 +0000 | [diff] [blame] | 266 | execlp( "wineclipsrv", "wineclipsrv", selMask, dbgClassMask, clearSelection, NULL ); |
| 267 | execl( "./windows/x11drv/wineclipsrv", "wineclipsrv", |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 268 | selMask, dbgClassMask, clearSelection, NULL ); |
| 269 | |
| 270 | /* Exec Failed! */ |
| 271 | perror("Could not start Wine clipboard server"); |
| 272 | exit( 1 ); /* Exit the child process */ |
| 273 | } |
| 274 | |
| 275 | /* Wait until the clipboard server acquires the selection. |
| 276 | * We must release the windows lock to enable Wine to process |
| 277 | * selection messages in response to the servers requests. |
| 278 | */ |
| 279 | |
| 280 | iWndsLocks = WIN_SuspendWndsLock(); |
| 281 | |
| 282 | /* We must wait until the server finishes acquiring the selection, |
| 283 | * before proceeding, otherwise the window which owns the selection |
| 284 | * will be destroyed prematurely! |
| 285 | * Create a non-signalled, auto-reset event which will be set by |
| 286 | * X11DRV_CLIPBOARD_ReleaseSelection, and wait until this gets |
| 287 | * signalled before proceeding. |
| 288 | */ |
| 289 | |
| 290 | if ( !(selectionClearEvent = CreateEventA(NULL, FALSE, FALSE, NULL)) ) |
| 291 | ERR("Could not create wait object. Clipboard server won't start!\n"); |
| 292 | else |
| 293 | { |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 294 | /* Wait until we lose the selection, timing out after a minute */ |
| 295 | |
| 296 | TRACE("Waiting for clipboard server to acquire selection\n"); |
| 297 | |
| 298 | if ( WaitForSingleObject( selectionClearEvent, 60000 ) != WAIT_OBJECT_0 ) |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 299 | TRACE("Server could not acquire selection, or a timeout occurred!\n"); |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 300 | else |
| 301 | TRACE("Server successfully acquired selection\n"); |
| 302 | |
| 303 | /* Release the event */ |
| 304 | CloseHandle(selectionClearEvent); |
Niels Kristian Bech Jensen | c69a80c | 1999-11-28 20:31:04 +0000 | [diff] [blame] | 305 | selectionClearEvent = 0; |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | WIN_RestoreWndsLock(iWndsLocks); |
| 309 | |
| 310 | return TRUE; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /************************************************************************** |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 315 | * X11DRV_CLIPBOARD_CacheDataFormats |
| 316 | * |
| 317 | * Caches the list of data formats available from the current selection. |
| 318 | * This queries the selection owner for the TARGETS property and saves all |
| 319 | * reported property types. |
| 320 | */ |
| 321 | int X11DRV_CLIPBOARD_CacheDataFormats( Atom SelectionName ) |
| 322 | { |
Niels Kristian Bech Jensen | c69a80c | 1999-11-28 20:31:04 +0000 | [diff] [blame] | 323 | HWND hWnd = 0; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 324 | HWND hWndClipWindow = GetOpenClipboardWindow(); |
| 325 | WND* wnd = NULL; |
| 326 | XEvent xe; |
| 327 | Atom aTargets; |
| 328 | Atom atype=AnyPropertyType; |
| 329 | int aformat; |
| 330 | unsigned long remain; |
| 331 | Atom* targetList=NULL; |
| 332 | Window w; |
Niels Kristian Bech Jensen | c69a80c | 1999-11-28 20:31:04 +0000 | [diff] [blame] | 333 | Window ownerSelection = 0; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * Empty the clipboard cache |
| 337 | */ |
| 338 | CLIPBOARD_EmptyCache(TRUE); |
| 339 | |
| 340 | cSelectionTargets = 0; |
| 341 | selectionCacheSrc = SelectionName; |
| 342 | |
| 343 | hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow(); |
| 344 | |
| 345 | ownerSelection = TSXGetSelectionOwner(display, SelectionName); |
| 346 | if ( !hWnd || (ownerSelection == None) ) |
| 347 | return cSelectionTargets; |
| 348 | |
| 349 | /* |
| 350 | * Query the selection owner for the TARGETS property |
| 351 | */ |
| 352 | wnd = WIN_FindWndPtr(hWnd); |
| 353 | w = X11DRV_WND_FindXWindow(wnd); |
| 354 | WIN_ReleaseWndPtr(wnd); |
| 355 | wnd = NULL; |
| 356 | |
| 357 | aTargets = TSXInternAtom(display, "TARGETS", False); |
| 358 | |
| 359 | TRACE("Requesting TARGETS selection for '%s' (owner=%08x)...\n", |
| 360 | TSXGetAtomName(display, selectionCacheSrc), (unsigned)ownerSelection ); |
| 361 | |
| 362 | EnterCriticalSection( &X11DRV_CritSection ); |
| 363 | XConvertSelection(display, selectionCacheSrc, aTargets, |
| 364 | TSXInternAtom(display, "SELECTION_DATA", False), |
| 365 | w, CurrentTime); |
| 366 | |
| 367 | /* |
| 368 | * Wait until SelectionNotify is received |
| 369 | */ |
| 370 | while( TRUE ) |
| 371 | { |
| 372 | if( XCheckTypedWindowEvent(display, w, SelectionNotify, &xe) ) |
| 373 | if( xe.xselection.selection == selectionCacheSrc ) |
| 374 | break; |
| 375 | } |
| 376 | LeaveCriticalSection( &X11DRV_CritSection ); |
| 377 | |
| 378 | /* Verify that the selection returned a valid TARGETS property */ |
| 379 | if ( (xe.xselection.target != aTargets) |
| 380 | || (xe.xselection.property == None) ) |
| 381 | { |
| 382 | TRACE("\tCould not retrieve TARGETS\n"); |
| 383 | return cSelectionTargets; |
| 384 | } |
| 385 | |
| 386 | /* Read the TARGETS property contents */ |
| 387 | if(TSXGetWindowProperty(display, xe.xselection.requestor, xe.xselection.property, |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 388 | 0, 0x3FFF, True, AnyPropertyType/*XA_ATOM*/, &atype, &aformat, |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 389 | &cSelectionTargets, &remain, (unsigned char**)&targetList) != Success) |
| 390 | TRACE("\tCouldn't read TARGETS property\n"); |
| 391 | else |
| 392 | { |
| 393 | TRACE("\tType %s,Format %d,nItems %ld, Remain %ld\n", |
| 394 | TSXGetAtomName(display,atype),aformat,cSelectionTargets, remain); |
| 395 | /* |
| 396 | * The TARGETS property should have returned us a list of atoms |
| 397 | * corresponding to each selection target format supported. |
| 398 | */ |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 399 | if( (atype == XA_ATOM || atype == aTargets) && aformat == 32 ) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 400 | { |
| 401 | int i; |
| 402 | LPWINE_CLIPFORMAT lpFormat; |
| 403 | |
| 404 | /* Cache these formats in the clipboard cache */ |
| 405 | |
| 406 | for (i = 0; i < cSelectionTargets; i++) |
| 407 | { |
| 408 | char *itemFmtName = TSXGetAtomName(display, targetList[i]); |
| 409 | UINT wFormat = X11DRV_CLIPBOARD_MapPropertyToFormat(itemFmtName); |
| 410 | |
| 411 | /* |
| 412 | * If the clipboard format maps to a Windows format, simply store |
| 413 | * the atom identifier and record its availablity status |
| 414 | * in the clipboard cache. |
| 415 | */ |
| 416 | if (wFormat) |
| 417 | { |
| 418 | lpFormat = CLIPBOARD_LookupFormat( wFormat ); |
| 419 | |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 420 | /* Don't replace if the property already cached is a native format, |
| 421 | * or if a PIXMAP is being replaced by a BITMAP. |
| 422 | */ |
| 423 | if (lpFormat->wDataPresent && |
| 424 | ( X11DRV_CLIPBOARD_IsNativeProperty(lpFormat->drvData) |
| 425 | || (lpFormat->drvData == XA_PIXMAP && targetList[i] == XA_BITMAP) ) |
| 426 | ) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 427 | { |
| 428 | TRACE("\tAtom# %d: '%s' --> FormatID(%d) %s (Skipped)\n", |
| 429 | i, itemFmtName, wFormat, lpFormat->Name); |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | lpFormat->wDataPresent = 1; |
| 434 | lpFormat->drvData = targetList[i]; |
| 435 | TRACE("\tAtom# %d: '%s' --> FormatID(%d) %s\n", |
| 436 | i, itemFmtName, wFormat, lpFormat->Name); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | TSXFree(itemFmtName); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* Free the list of targets */ |
| 445 | TSXFree(targetList); |
| 446 | } |
| 447 | |
| 448 | return cSelectionTargets; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /************************************************************************** |
| 452 | * X11DRV_CLIPBOARD_ReadSelection |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 453 | * Reads the contents of the X selection property into the WINE clipboard cache |
| 454 | * converting the selection into a format compatible with the windows clipboard |
| 455 | * if possible. |
| 456 | * This method is invoked only to read the contents of a the selection owned |
| 457 | * by an external application. i.e. when we do not own the X selection. |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 458 | */ |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 459 | static BOOL X11DRV_CLIPBOARD_ReadSelection(UINT wFormat, Window w, Atom prop, Atom reqType) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 460 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 461 | Atom atype=AnyPropertyType; |
| 462 | int aformat; |
| 463 | unsigned long nitems,remain,itemSize; |
| 464 | long lRequestLength; |
| 465 | unsigned char* val=NULL; |
| 466 | LPWINE_CLIPFORMAT lpFormat; |
| 467 | BOOL bRet = FALSE; |
| 468 | HWND hWndClipWindow = GetOpenClipboardWindow(); |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 469 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 470 | |
| 471 | if(prop == None) |
| 472 | return bRet; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 473 | |
Alexandre Julliard | 359f497e | 1999-07-04 16:02:24 +0000 | [diff] [blame] | 474 | TRACE("Reading X selection...\n"); |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 475 | |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 476 | TRACE("\tretrieving property %s from window %ld into %s\n", |
| 477 | TSXGetAtomName(display,reqType), (long)w, TSXGetAtomName(display,prop) ); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 478 | |
| 479 | /* |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 480 | * First request a zero length in order to figure out the request size. |
| 481 | */ |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 482 | if(TSXGetWindowProperty(display,w,prop,0,0,False, AnyPropertyType/*reqType*/, |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 483 | &atype, &aformat, &nitems, &itemSize, &val) != Success) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 484 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 485 | WARN("\tcouldn't get property size\n"); |
| 486 | return bRet; |
| 487 | } |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 488 | |
| 489 | /* Free zero length return data if any */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 490 | if ( val ) |
| 491 | { |
| 492 | TSXFree(val); |
| 493 | val = NULL; |
| 494 | } |
| 495 | |
| 496 | TRACE("\tretrieving %ld bytes...\n", itemSize * aformat/8); |
| 497 | lRequestLength = (itemSize * aformat/8)/4 + 1; |
| 498 | |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 499 | /* |
| 500 | * Retrieve the actual property in the required X format. |
| 501 | */ |
| 502 | if(TSXGetWindowProperty(display,w,prop,0,lRequestLength,False,AnyPropertyType/*reqType*/, |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 503 | &atype, &aformat, &nitems, &remain, &val) != Success) |
| 504 | { |
| 505 | WARN("\tcouldn't read property\n"); |
| 506 | return bRet; |
| 507 | } |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 508 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 509 | TRACE("\tType %s,Format %d,nitems %ld,remain %ld,value %s\n", |
| 510 | atype ? TSXGetAtomName(display,atype) : NULL, aformat,nitems,remain,val); |
| 511 | |
| 512 | if (remain) |
| 513 | { |
| 514 | WARN("\tCouldn't read entire property- selection may be too large! Remain=%ld\n", remain); |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 515 | goto END; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Translate the X property into the appropriate Windows clipboard |
| 520 | * format, if possible. |
| 521 | */ |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 522 | if ( (reqType == XA_STRING) |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 523 | && (atype == XA_STRING) && (aformat == 8) ) |
| 524 | /* convert Unix text to CF_UNICODETEXT */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 525 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 526 | int i,inlcount = 0; |
| 527 | char* lpstr; |
| 528 | |
| 529 | TRACE("\tselection is '%s'\n",val); |
| 530 | |
| 531 | for(i=0; i <= nitems; i++) |
| 532 | if( val[i] == '\n' ) inlcount++; |
| 533 | |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 534 | if( (lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nitems + inlcount + 1)) ) |
| 535 | { |
| 536 | static UINT text_cp = (UINT)-1; |
| 537 | UINT count; |
| 538 | HANDLE hUnicodeText; |
| 539 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 540 | for(i=0,inlcount=0; i <= nitems; i++) |
| 541 | { |
| 542 | if( val[i] == '\n' ) lpstr[inlcount++]='\r'; |
| 543 | lpstr[inlcount++]=val[i]; |
| 544 | } |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 545 | |
| 546 | if(text_cp == (UINT)-1) |
| 547 | text_cp = PROFILE_GetWineIniInt("x11drv", "TextCP", CP_ACP); |
| 548 | |
| 549 | count = MultiByteToWideChar(text_cp, 0, lpstr, -1, NULL, 0); |
| 550 | hUnicodeText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, count * sizeof(WCHAR)); |
| 551 | if(hUnicodeText) |
| 552 | { |
| 553 | WCHAR *textW = GlobalLock(hUnicodeText); |
| 554 | MultiByteToWideChar(text_cp, 0, lpstr, -1, textW, count); |
| 555 | GlobalUnlock(hUnicodeText); |
| 556 | SetClipboardData(CF_UNICODETEXT, hUnicodeText); |
| 557 | bRet = TRUE; |
| 558 | } |
| 559 | HeapFree(GetProcessHeap(), 0, lpstr); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 562 | else if ( reqType == XA_PIXMAP || reqType == XA_BITMAP ) /* treat PIXMAP as CF_DIB or CF_BITMAP */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 563 | { |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 564 | /* Get the first pixmap handle passed to us */ |
| 565 | Pixmap *pPixmap = (Pixmap *)val; |
Niels Kristian Bech Jensen | c69a80c | 1999-11-28 20:31:04 +0000 | [diff] [blame] | 566 | HANDLE hTargetImage = 0; /* Handle to store the converted bitmap or DIB */ |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 567 | |
| 568 | if (aformat != 32 || nitems < 1 || atype != XA_PIXMAP |
| 569 | || (wFormat != CF_BITMAP && wFormat != CF_DIB)) |
| 570 | { |
| 571 | WARN("\tUnimplemented format conversion request\n"); |
| 572 | goto END; |
| 573 | } |
| 574 | |
| 575 | if ( wFormat == CF_BITMAP ) |
| 576 | { |
| 577 | /* For CF_BITMAP requests we must return an HBITMAP */ |
| 578 | hTargetImage = X11DRV_BITMAP_CreateBitmapFromPixmap(*pPixmap, TRUE); |
| 579 | } |
| 580 | else if (wFormat == CF_DIB) |
| 581 | { |
| 582 | HWND hwnd = GetOpenClipboardWindow(); |
| 583 | HDC hdc = GetDC(hwnd); |
| 584 | |
| 585 | /* For CF_DIB requests we must return an HGLOBAL storing a packed DIB */ |
| 586 | hTargetImage = X11DRV_DIB_CreateDIBFromPixmap(*pPixmap, hdc, TRUE); |
| 587 | |
| 588 | ReleaseDC(hdc, hwnd); |
| 589 | } |
| 590 | |
| 591 | if (!hTargetImage) |
| 592 | { |
| 593 | WARN("PIXMAP conversion failed!\n" ); |
| 594 | goto END; |
| 595 | } |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 596 | |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 597 | /* Delete previous clipboard data */ |
| 598 | lpFormat = CLIPBOARD_LookupFormat(wFormat); |
| 599 | if (lpFormat->wDataPresent && (lpFormat->hData16 || lpFormat->hData32)) |
| 600 | CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow)); |
| 601 | |
| 602 | /* Update the clipboard record */ |
| 603 | lpFormat->wDataPresent = 1; |
| 604 | lpFormat->hData32 = hTargetImage; |
| 605 | lpFormat->hData16 = 0; |
| 606 | |
| 607 | bRet = TRUE; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 610 | /* For native properties simply copy the X data without conversion */ |
| 611 | else if (X11DRV_CLIPBOARD_IsNativeProperty(reqType)) /* <WCF>* */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 612 | { |
| 613 | HANDLE hClipData = 0; |
| 614 | void* lpClipData; |
| 615 | int cBytes = nitems * aformat/8; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 616 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 617 | if( cBytes ) |
| 618 | { |
| 619 | /* Turn on the DDESHARE flag to enable shared 32 bit memory */ |
| 620 | hClipData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cBytes ); |
| 621 | if( (lpClipData = GlobalLock(hClipData)) ) |
| 622 | { |
| 623 | memcpy(lpClipData, val, cBytes); |
| 624 | GlobalUnlock(hClipData); |
| 625 | } |
| 626 | else |
| 627 | hClipData = 0; |
| 628 | } |
| 629 | |
| 630 | if( hClipData ) |
| 631 | { |
| 632 | /* delete previous clipboard record if any */ |
| 633 | lpFormat = CLIPBOARD_LookupFormat(wFormat); |
| 634 | if (lpFormat->wDataPresent || lpFormat->hData16 || lpFormat->hData32) |
| 635 | CLIPBOARD_DeleteRecord(lpFormat, !(hWndClipWindow)); |
| 636 | |
| 637 | /* Update the clipboard record */ |
| 638 | lpFormat->wDataPresent = 1; |
| 639 | lpFormat->hData32 = hClipData; |
| 640 | lpFormat->hData16 = 0; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 641 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 642 | bRet = TRUE; |
| 643 | } |
| 644 | } |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 645 | else |
| 646 | { |
| 647 | WARN("\tUnimplemented format conversion request\n"); |
| 648 | goto END; |
| 649 | } |
| 650 | |
| 651 | END: |
| 652 | /* Delete the property on the window now that we are done |
| 653 | * This will send a PropertyNotify event to the selection owner. */ |
| 654 | TSXDeleteProperty(display,w,prop); |
| 655 | |
| 656 | /* Free the retrieved property data */ |
| 657 | if (val) |
| 658 | TSXFree(val); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 659 | |
| 660 | return bRet; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /************************************************************************** |
| 664 | * X11DRV_CLIPBOARD_ReleaseSelection |
| 665 | * |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 666 | * Release an XA_PRIMARY or XA_CLIPBOARD selection that we own, in response |
| 667 | * to a SelectionClear event. |
| 668 | * This can occur in response to another client grabbing the X selection. |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 669 | * If the XA_CLIPBOARD selection is lost, we relinquish XA_PRIMARY as well. |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 670 | */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 671 | void X11DRV_CLIPBOARD_ReleaseSelection(Atom selType, Window w, HWND hwnd) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 672 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 673 | Atom xaClipboard = TSXInternAtom(display, "CLIPBOARD", False); |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 674 | int clearAllSelections = PROFILE_GetWineIniInt("Clipboard", "ClearAllSelections", 0); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 675 | |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 676 | /* w is the window that lost the selection |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 677 | * selectionPrevWindow is nonzero if CheckSelection() was called. |
| 678 | */ |
| 679 | |
Alexandre Julliard | 359f497e | 1999-07-04 16:02:24 +0000 | [diff] [blame] | 680 | TRACE("\tevent->window = %08x (sw = %08x, spw=%08x)\n", |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 681 | (unsigned)w, (unsigned)selectionWindow, (unsigned)selectionPrevWindow ); |
| 682 | |
| 683 | if( selectionAcquired ) |
| 684 | { |
| 685 | if( w == selectionWindow || selectionPrevWindow == None) |
| 686 | { |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 687 | /* If we're losing the CLIPBOARD selection, or if the preferences in .winerc |
| 688 | * dictate that *all* selections should be cleared on loss of a selection, |
| 689 | * we must give up all the selections we own. |
| 690 | */ |
| 691 | if ( clearAllSelections || (selType == xaClipboard) ) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 692 | { |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 693 | /* completely give up the selection */ |
| 694 | TRACE("Lost CLIPBOARD (+PRIMARY) selection\n"); |
| 695 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 696 | /* We are completely giving up the selection. |
| 697 | * Make sure we can open the windows clipboard first. */ |
| 698 | |
| 699 | if ( !OpenClipboard(hwnd) ) |
| 700 | { |
| 701 | /* |
| 702 | * We can't empty the clipboard if we cant open it so abandon. |
| 703 | * Wine will think that it still owns the selection but this is |
| 704 | * safer than losing the selection without properly emptying |
| 705 | * the clipboard. Perhaps we should forcibly re-assert ownership |
| 706 | * of the CLIPBOARD selection in this case... |
| 707 | */ |
| 708 | ERR("\tClipboard is busy. Could not give up selection!\n"); |
| 709 | return; |
| 710 | } |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 711 | |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 712 | /* We really lost CLIPBOARD but want to voluntarily lose PRIMARY */ |
| 713 | if ( (selType == xaClipboard) |
| 714 | && (selectionAcquired & S_PRIMARY) ) |
| 715 | { |
| 716 | XSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime); |
| 717 | } |
| 718 | |
| 719 | /* We really lost PRIMARY but want to voluntarily lose CLIPBOARD */ |
| 720 | if ( (selType == XA_PRIMARY) |
| 721 | && (selectionAcquired & S_CLIPBOARD) ) |
| 722 | { |
| 723 | XSetSelectionOwner(display, xaClipboard, None, CurrentTime); |
| 724 | } |
| 725 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 726 | selectionWindow = None; |
| 727 | PrimarySelectionOwner = ClipboardSelectionOwner = 0; |
| 728 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 729 | /* Empty the windows clipboard. |
| 730 | * We should pretend that we still own the selection BEFORE calling |
| 731 | * EmptyClipboard() since otherwise this has the side effect of |
| 732 | * triggering X11DRV_CLIPBOARD_Acquire() and causing the X selection |
| 733 | * to be re-acquired by us! |
| 734 | */ |
| 735 | selectionAcquired = (S_PRIMARY | S_CLIPBOARD); |
| 736 | EmptyClipboard(); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 737 | CloseClipboard(); |
| 738 | |
| 739 | /* Give up ownership of the windows clipboard */ |
| 740 | CLIPBOARD_ReleaseOwner(); |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 741 | |
| 742 | /* Reset the selection flags now that we are done */ |
| 743 | selectionAcquired = S_NOSELECTION; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 744 | } |
| 745 | else if ( selType == XA_PRIMARY ) /* Give up only PRIMARY selection */ |
| 746 | { |
| 747 | TRACE("Lost PRIMARY selection\n"); |
| 748 | PrimarySelectionOwner = 0; |
| 749 | selectionAcquired &= ~S_PRIMARY; /* clear S_PRIMARY mask */ |
| 750 | } |
| 751 | |
| 752 | cSelectionTargets = 0; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 753 | } |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 754 | /* but we'll keep existing data for internal use */ |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 755 | else if( w == selectionPrevWindow ) |
| 756 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 757 | Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False); |
| 758 | |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 759 | w = TSXGetSelectionOwner(display, XA_PRIMARY); |
| 760 | if( w == None ) |
| 761 | TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 762 | |
| 763 | w = TSXGetSelectionOwner(display, xaClipboard); |
| 764 | if( w == None ) |
| 765 | TSXSetSelectionOwner(display, xaClipboard, selectionWindow, CurrentTime); |
| 766 | } |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 769 | /* Signal to a selectionClearEvent listener if the selection is completely lost */ |
| 770 | if (selectionClearEvent && !selectionAcquired) |
| 771 | { |
| 772 | TRACE("Lost all selections, signalling to selectionClearEvent listener\n"); |
| 773 | SetEvent(selectionClearEvent); |
| 774 | } |
| 775 | |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 776 | selectionPrevWindow = None; |
| 777 | } |
| 778 | |
| 779 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 780 | * X11DRV_ReleaseClipboard (X11DRV.@) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 781 | * Voluntarily release all currently owned X selections |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 782 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 783 | void X11DRV_ReleaseClipboard(void) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 784 | { |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 785 | if( selectionAcquired ) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 786 | { |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 787 | XEvent xe; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 788 | Window savePrevWindow = selectionWindow; |
| 789 | Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False); |
| 790 | BOOL bHasPrimarySelection = selectionAcquired & S_PRIMARY; |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 791 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 792 | selectionAcquired = S_NOSELECTION; |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 793 | selectionPrevWindow = selectionWindow; |
| 794 | selectionWindow = None; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 795 | |
Alexandre Julliard | 359f497e | 1999-07-04 16:02:24 +0000 | [diff] [blame] | 796 | TRACE("\tgiving up selection (spw = %08x)\n", |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 797 | (unsigned)selectionPrevWindow); |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 798 | |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 799 | EnterCriticalSection(&X11DRV_CritSection); |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 800 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 801 | TRACE("Releasing CLIPBOARD selection\n"); |
| 802 | XSetSelectionOwner(display, xaClipboard, None, CurrentTime); |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 803 | if( selectionPrevWindow ) |
| 804 | while( !XCheckTypedWindowEvent( display, selectionPrevWindow, |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 805 | SelectionClear, &xe ) ); |
| 806 | |
| 807 | if ( bHasPrimarySelection ) |
| 808 | { |
| 809 | TRACE("Releasing XA_PRIMARY selection\n"); |
| 810 | selectionPrevWindow = savePrevWindow; /* May be cleared in X11DRV_CLIPBOARD_ReleaseSelection */ |
| 811 | XSetSelectionOwner(display, XA_PRIMARY, None, CurrentTime); |
| 812 | |
| 813 | if( selectionPrevWindow ) |
| 814 | while( !XCheckTypedWindowEvent( display, selectionPrevWindow, |
| 815 | SelectionClear, &xe ) ); |
| 816 | } |
| 817 | |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 818 | LeaveCriticalSection(&X11DRV_CritSection); |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 819 | } |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 820 | |
| 821 | /* Get rid of any Pixmap resources we may still have */ |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 822 | while (prop_head) |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 823 | { |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 824 | PROPERTY *prop = prop_head; |
| 825 | prop_head = prop->next; |
| 826 | XFreePixmap( display, prop->pixmap ); |
| 827 | HeapFree( GetProcessHeap(), 0, prop ); |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 828 | } |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 832 | * X11DRV_AcquireClipboard (X11DRV.@) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 833 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 834 | void X11DRV_AcquireClipboard(void) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 835 | { |
| 836 | Window owner; |
| 837 | HWND hWndClipWindow = GetOpenClipboardWindow(); |
| 838 | |
| 839 | /* |
| 840 | * Acquire X selection if we don't already own it. |
| 841 | * Note that we only acquire the selection if it hasn't been already |
| 842 | * acquired by us, and ignore the fact that another X window may be |
| 843 | * asserting ownership. The reason for this is we need *any* top level |
| 844 | * X window to hold selection ownership. The actual clipboard data requests |
| 845 | * are made via GetClipboardData from EVENT_SelectionRequest and this |
| 846 | * ensures that the real HWND owner services the request. |
| 847 | * If the owning X window gets destroyed the selection ownership is |
| 848 | * re-cycled to another top level X window in X11DRV_CLIPBOARD_ResetOwner. |
| 849 | * |
| 850 | */ |
| 851 | |
| 852 | if ( !(selectionAcquired == (S_PRIMARY | S_CLIPBOARD)) ) |
| 853 | { |
| 854 | Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False); |
| 855 | WND *tmpWnd = WIN_FindWndPtr( hWndClipWindow ? hWndClipWindow : AnyPopup() ); |
| 856 | owner = X11DRV_WND_FindXWindow(tmpWnd ); |
| 857 | WIN_ReleaseWndPtr(tmpWnd); |
| 858 | |
| 859 | /* Grab PRIMARY selection if not owned */ |
| 860 | if ( !(selectionAcquired & S_PRIMARY) ) |
| 861 | TSXSetSelectionOwner(display, XA_PRIMARY, owner, CurrentTime); |
| 862 | |
| 863 | /* Grab CLIPBOARD selection if not owned */ |
| 864 | if ( !(selectionAcquired & S_CLIPBOARD) ) |
| 865 | TSXSetSelectionOwner(display, xaClipboard, owner, CurrentTime); |
| 866 | |
| 867 | if( TSXGetSelectionOwner(display,XA_PRIMARY) == owner ) |
| 868 | selectionAcquired |= S_PRIMARY; |
| 869 | |
| 870 | if( TSXGetSelectionOwner(display,xaClipboard) == owner) |
| 871 | selectionAcquired |= S_CLIPBOARD; |
| 872 | |
| 873 | if (selectionAcquired) |
| 874 | { |
| 875 | selectionWindow = owner; |
| 876 | TRACE("Grabbed X selection, owner=(%08x)\n", (unsigned) owner); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 882 | * X11DRV_IsClipboardFormatAvailable (X11DRV.@) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 883 | * |
| 884 | * Checks if the specified format is available in the current selection |
| 885 | * Only invoked when WINE is not the selection owner |
| 886 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 887 | BOOL X11DRV_IsClipboardFormatAvailable(UINT wFormat) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 888 | { |
| 889 | Atom xaClipboard = TSXInternAtom(display, _CLIPBOARD, False); |
| 890 | Window ownerPrimary = TSXGetSelectionOwner(display,XA_PRIMARY); |
| 891 | Window ownerClipboard = TSXGetSelectionOwner(display,xaClipboard); |
| 892 | |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 893 | TRACE("%d\n", wFormat); |
| 894 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 895 | /* |
| 896 | * If the selection has not been previously cached, or the selection has changed, |
| 897 | * try and cache the list of available selection targets from the current selection. |
| 898 | */ |
| 899 | if ( !cSelectionTargets || (PrimarySelectionOwner != ownerPrimary) |
| 900 | || (ClipboardSelectionOwner != ownerClipboard) ) |
| 901 | { |
| 902 | /* |
| 903 | * First try cacheing the CLIPBOARD selection. |
| 904 | * If unavailable try PRIMARY. |
| 905 | */ |
| 906 | if ( X11DRV_CLIPBOARD_CacheDataFormats(xaClipboard) == 0 ) |
| 907 | { |
| 908 | X11DRV_CLIPBOARD_CacheDataFormats(XA_PRIMARY); |
| 909 | } |
| 910 | |
| 911 | ClipboardSelectionOwner = ownerClipboard; |
| 912 | PrimarySelectionOwner = ownerPrimary; |
| 913 | } |
| 914 | |
| 915 | /* Exit if there is no selection */ |
| 916 | if ( !ownerClipboard && !ownerPrimary ) |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 917 | { |
| 918 | TRACE("There is no selection\n"); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 919 | return FALSE; |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 920 | } |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 921 | |
| 922 | /* |
| 923 | * Many X client apps (such as XTerminal) don't support being queried |
| 924 | * for the "TARGETS" target atom. To handle such clients we must actually |
| 925 | * try to convert the selection to the requested type. |
| 926 | */ |
| 927 | if ( !cSelectionTargets ) |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 928 | return X11DRV_GetClipboardData( wFormat ); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 929 | |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 930 | TRACE("There is no selection\n"); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 931 | return FALSE; |
| 932 | } |
| 933 | |
| 934 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 935 | * X11DRV_RegisterClipboardFormat (X11DRV.@) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 936 | * |
| 937 | * Registers a custom X clipboard format |
| 938 | * Returns: TRUE - success, FALSE - failure |
| 939 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 940 | BOOL X11DRV_RegisterClipboardFormat( LPCSTR FormatName ) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 941 | { |
| 942 | Atom prop = None; |
| 943 | char str[256]; |
| 944 | |
| 945 | /* |
| 946 | * If an X atom is registered for this format, return that |
| 947 | * Otherwise register a new atom. |
| 948 | */ |
| 949 | if (FormatName) |
| 950 | { |
| 951 | /* Add a WINE specific prefix to the format */ |
| 952 | strcpy(str, FMT_PREFIX); |
| 953 | strncat(str, FormatName, sizeof(str) - strlen(FMT_PREFIX)); |
| 954 | prop = TSXInternAtom(display, str, False); |
| 955 | } |
| 956 | |
| 957 | return (prop) ? TRUE : FALSE; |
| 958 | } |
| 959 | |
| 960 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 961 | * X11DRV_IsSelectionOwner (X11DRV.@) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 962 | * |
| 963 | * Returns: TRUE - We(WINE) own the selection, FALSE - Selection not owned by us |
| 964 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 965 | BOOL X11DRV_IsSelectionOwner(void) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 966 | { |
| 967 | return selectionAcquired; |
| 968 | } |
| 969 | |
| 970 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 971 | * X11DRV_SetClipboardData (X11DRV.@) |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 972 | * |
| 973 | * We don't need to do anything special here since the clipboard code |
| 974 | * maintains the cache. |
| 975 | * |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 976 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 977 | void X11DRV_SetClipboardData(UINT wFormat) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 978 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 979 | /* Make sure we have acquired the X selection */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 980 | X11DRV_AcquireClipboard(); |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 984 | * X11DRV_GetClipboardData (X11DRV.@) |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 985 | * |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 986 | * This method is invoked only when we DO NOT own the X selection |
| 987 | * |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 988 | * NOTE: Clipboard driver get requests only for CF_UNICODETEXT data. |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 989 | * We always get the data from the selection client each time, |
| 990 | * since we have no way of determining if the data in our cache is stale. |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 991 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 992 | BOOL X11DRV_GetClipboardData(UINT wFormat) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 993 | { |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 994 | BOOL bRet = selectionAcquired; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 995 | HWND hWndClipWindow = GetOpenClipboardWindow(); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 996 | HWND hWnd = (hWndClipWindow) ? hWndClipWindow : GetActiveWindow(); |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 997 | WND* wnd = NULL; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 998 | LPWINE_CLIPFORMAT lpFormat; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 999 | |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 1000 | TRACE("%d\n", wFormat); |
| 1001 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1002 | if( !selectionAcquired && (wnd = WIN_FindWndPtr(hWnd)) ) |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 1003 | { |
| 1004 | XEvent xe; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1005 | Atom propRequest; |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 1006 | Window w = X11DRV_WND_FindXWindow(wnd); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1007 | WIN_ReleaseWndPtr(wnd); |
| 1008 | wnd = NULL; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1009 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1010 | /* Map the format ID requested to an X selection property. |
| 1011 | * If the format is in the cache, use the atom associated |
| 1012 | * with it. |
| 1013 | */ |
| 1014 | |
| 1015 | lpFormat = CLIPBOARD_LookupFormat( wFormat ); |
| 1016 | if (lpFormat && lpFormat->wDataPresent && lpFormat->drvData) |
| 1017 | propRequest = (Atom)lpFormat->drvData; |
| 1018 | else |
| 1019 | propRequest = X11DRV_CLIPBOARD_MapFormatToProperty(wFormat); |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1020 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1021 | if (propRequest) |
| 1022 | { |
| 1023 | TRACE("Requesting %s selection from %s...\n", |
| 1024 | TSXGetAtomName(display, propRequest), |
| 1025 | TSXGetAtomName(display, selectionCacheSrc) ); |
Francois Boisvert | 6b1b41c | 1999-03-14 17:25:32 +0000 | [diff] [blame] | 1026 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1027 | EnterCriticalSection( &X11DRV_CritSection ); |
| 1028 | XConvertSelection(display, selectionCacheSrc, propRequest, |
| 1029 | TSXInternAtom(display, "SELECTION_DATA", False), |
| 1030 | w, CurrentTime); |
| 1031 | |
| 1032 | /* wait until SelectionNotify is received */ |
| 1033 | |
| 1034 | while( TRUE ) |
| 1035 | { |
| 1036 | if( XCheckTypedWindowEvent(display, w, SelectionNotify, &xe) ) |
| 1037 | if( xe.xselection.selection == selectionCacheSrc ) |
| 1038 | break; |
| 1039 | } |
| 1040 | LeaveCriticalSection( &X11DRV_CritSection ); |
| 1041 | |
| 1042 | /* |
| 1043 | * Read the contents of the X selection property into WINE's |
| 1044 | * clipboard cache converting the selection to be compatible if possible. |
| 1045 | */ |
| 1046 | bRet = X11DRV_CLIPBOARD_ReadSelection( wFormat, |
| 1047 | xe.xselection.requestor, |
| 1048 | xe.xselection.property, |
| 1049 | xe.xselection.target); |
| 1050 | } |
| 1051 | else |
| 1052 | bRet = FALSE; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1053 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1054 | TRACE("\tpresent %s = %i\n", CLIPBOARD_GetFormatName(wFormat), bRet ); |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 1055 | } |
Dmitry Timoshkov | 1df0d36 | 2000-12-11 01:09:56 +0000 | [diff] [blame] | 1056 | |
| 1057 | TRACE("Returning %d\n", bRet); |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1058 | |
Alex Korobka | 44a1b59 | 1999-04-01 12:03:52 +0000 | [diff] [blame] | 1059 | return bRet; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /************************************************************************** |
Patrik Stridvall | 2ece70e | 2000-12-22 01:38:01 +0000 | [diff] [blame] | 1063 | * X11DRV_ResetSelectionOwner (X11DRV.@) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1064 | * |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1065 | * Called from DestroyWindow() to prevent X selection from being lost when |
| 1066 | * a top level window is destroyed, by switching ownership to another top |
| 1067 | * level window. |
| 1068 | * Any top level window can own the selection. See X11DRV_CLIPBOARD_Acquire |
| 1069 | * for a more detailed description of this. |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1070 | */ |
Alexandre Julliard | 42d20f9 | 2000-08-10 01:16:19 +0000 | [diff] [blame] | 1071 | void X11DRV_ResetSelectionOwner(WND *pWnd, BOOL bFooBar) |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1072 | { |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1073 | HWND hWndClipOwner = 0; |
| 1074 | Window XWnd = X11DRV_WND_GetXWindow(pWnd); |
| 1075 | Atom xaClipboard; |
| 1076 | BOOL bLostSelection = FALSE; |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1077 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1078 | /* There is nothing to do if we don't own the selection, |
| 1079 | * or if the X window which currently owns the selecion is different |
| 1080 | * from the one passed in. |
| 1081 | */ |
| 1082 | if ( !selectionAcquired || XWnd != selectionWindow |
| 1083 | || selectionWindow == None ) |
| 1084 | return; |
Patrik Stridvall | 151170c | 1998-12-26 12:00:43 +0000 | [diff] [blame] | 1085 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1086 | if ( (bFooBar && XWnd) || (!bFooBar && !XWnd) ) |
| 1087 | return; |
Patrik Stridvall | 151170c | 1998-12-26 12:00:43 +0000 | [diff] [blame] | 1088 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1089 | hWndClipOwner = GetClipboardOwner(); |
| 1090 | xaClipboard = TSXInternAtom(display, _CLIPBOARD, False); |
| 1091 | |
| 1092 | TRACE("clipboard owner = %04x, selection window = %08x\n", |
| 1093 | hWndClipOwner, (unsigned)selectionWindow); |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1094 | |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1095 | /* now try to salvage current selection from being destroyed by X */ |
| 1096 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1097 | TRACE("\tchecking %08x\n", (unsigned) XWnd); |
| 1098 | |
| 1099 | selectionPrevWindow = selectionWindow; |
| 1100 | selectionWindow = None; |
| 1101 | |
| 1102 | if( pWnd->next ) |
| 1103 | selectionWindow = X11DRV_WND_GetXWindow(pWnd->next); |
| 1104 | else if( pWnd->parent ) |
| 1105 | if( pWnd->parent->child != pWnd ) |
| 1106 | selectionWindow = X11DRV_WND_GetXWindow(pWnd->parent->child); |
| 1107 | |
| 1108 | if( selectionWindow != None ) |
| 1109 | { |
| 1110 | /* We must pretend that we don't own the selection while making the switch |
| 1111 | * since a SelectionClear event will be sent to the last owner. |
| 1112 | * If there is no owner X11DRV_CLIPBOARD_ReleaseSelection will do nothing. |
| 1113 | */ |
| 1114 | int saveSelectionState = selectionAcquired; |
| 1115 | selectionAcquired = False; |
| 1116 | |
| 1117 | TRACE("\tswitching selection from %08x to %08x\n", |
| 1118 | (unsigned)selectionPrevWindow, (unsigned)selectionWindow); |
| 1119 | |
| 1120 | /* Assume ownership for the PRIMARY and CLIPBOARD selection */ |
| 1121 | if ( saveSelectionState & S_PRIMARY ) |
| 1122 | TSXSetSelectionOwner(display, XA_PRIMARY, selectionWindow, CurrentTime); |
| 1123 | |
| 1124 | TSXSetSelectionOwner(display, xaClipboard, selectionWindow, CurrentTime); |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 1125 | |
| 1126 | /* Restore the selection masks */ |
| 1127 | selectionAcquired = saveSelectionState; |
| 1128 | |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1129 | /* Lose the selection if something went wrong */ |
| 1130 | if ( ( (saveSelectionState & S_PRIMARY) && |
| 1131 | (TSXGetSelectionOwner(display, XA_PRIMARY) != selectionWindow) ) |
| 1132 | || (TSXGetSelectionOwner(display, xaClipboard) != selectionWindow) ) |
| 1133 | { |
| 1134 | bLostSelection = TRUE; |
| 1135 | goto END; |
| 1136 | } |
| 1137 | else |
| 1138 | { |
| 1139 | /* Update selection state */ |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1140 | if (saveSelectionState & S_PRIMARY) |
| 1141 | PrimarySelectionOwner = selectionWindow; |
| 1142 | |
| 1143 | ClipboardSelectionOwner = selectionWindow; |
| 1144 | } |
| 1145 | } |
| 1146 | else |
| 1147 | { |
| 1148 | bLostSelection = TRUE; |
| 1149 | goto END; |
| 1150 | } |
| 1151 | |
| 1152 | END: |
| 1153 | if (bLostSelection) |
| 1154 | { |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 1155 | /* Launch the clipboard server if the selection can no longer be recyled |
| 1156 | * to another top level window. */ |
| 1157 | |
| 1158 | if ( !X11DRV_CLIPBOARD_LaunchServer() ) |
| 1159 | { |
| 1160 | /* Empty the windows clipboard if the server was not launched. |
| 1161 | * We should pretend that we still own the selection BEFORE calling |
| 1162 | * EmptyClipboard() since otherwise this has the side effect of |
| 1163 | * triggering X11DRV_CLIPBOARD_Acquire() and causing the X selection |
| 1164 | * to be re-acquired by us! |
| 1165 | */ |
| 1166 | |
| 1167 | TRACE("\tLost the selection! Emptying the clipboard...\n"); |
| 1168 | |
Niels Kristian Bech Jensen | c69a80c | 1999-11-28 20:31:04 +0000 | [diff] [blame] | 1169 | OpenClipboard( 0 ); |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 1170 | selectionAcquired = (S_PRIMARY | S_CLIPBOARD); |
| 1171 | EmptyClipboard(); |
| 1172 | |
| 1173 | CloseClipboard(); |
| 1174 | |
| 1175 | /* Give up ownership of the windows clipboard */ |
| 1176 | CLIPBOARD_ReleaseOwner(); |
| 1177 | } |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1178 | |
Francis Beaudet | 56ab55d | 1999-10-24 20:22:24 +0000 | [diff] [blame] | 1179 | selectionAcquired = S_NOSELECTION; |
| 1180 | ClipboardSelectionOwner = PrimarySelectionOwner = 0; |
| 1181 | selectionWindow = 0; |
Noel Borthwick | 2970067 | 1999-09-03 15:17:57 +0000 | [diff] [blame] | 1182 | } |
Patrik Stridvall | e35d636 | 1998-12-07 09:13:40 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 1185 | /************************************************************************** |
| 1186 | * X11DRV_CLIPBOARD_RegisterPixmapResource |
| 1187 | * Registers a Pixmap resource which is to be associated with a property Atom. |
| 1188 | * When the property is destroyed we also destroy the Pixmap through the |
| 1189 | * PropertyNotify event. |
| 1190 | */ |
| 1191 | BOOL X11DRV_CLIPBOARD_RegisterPixmapResource( Atom property, Pixmap pixmap ) |
| 1192 | { |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 1193 | PROPERTY *prop = HeapAlloc( GetProcessHeap(), 0, sizeof(*prop) ); |
| 1194 | if (!prop) return FALSE; |
| 1195 | prop->atom = property; |
| 1196 | prop->pixmap = pixmap; |
| 1197 | prop->next = prop_head; |
| 1198 | prop_head = prop; |
| 1199 | return TRUE; |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /************************************************************************** |
| 1203 | * X11DRV_CLIPBOARD_FreeResources |
| 1204 | * |
| 1205 | * Called from EVENT_PropertyNotify() to give us a chance to destroy |
| 1206 | * any resources associated with this property. |
| 1207 | */ |
| 1208 | void X11DRV_CLIPBOARD_FreeResources( Atom property ) |
| 1209 | { |
| 1210 | /* Do a simple linear search to see if we have a Pixmap resource |
| 1211 | * associated with this property and release it. |
| 1212 | */ |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 1213 | PROPERTY **prop = &prop_head; |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 1214 | |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 1215 | while (*prop) |
| 1216 | { |
| 1217 | if ((*prop)->atom == property) |
| 1218 | { |
| 1219 | PROPERTY *next = (*prop)->next; |
| 1220 | XFreePixmap( display, (*prop)->pixmap ); |
| 1221 | HeapFree( GetProcessHeap(), 0, *prop ); |
| 1222 | *prop = next; |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 1223 | } |
Alexandre Julliard | 96ebf15 | 2000-01-26 02:21:30 +0000 | [diff] [blame] | 1224 | else prop = &(*prop)->next; |
Noel Borthwick | d05b7be | 1999-09-20 15:42:47 +0000 | [diff] [blame] | 1225 | } |
| 1226 | } |