2003-11-14 17:48:57 +01:00
|
|
|
/*
|
2004-06-21 15:19:32 +02:00
|
|
|
*Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
|
2009-02-03 16:48:04 +01:00
|
|
|
*Copyright (C) Colin Harrison 2005-2008
|
2003-11-14 17:48:57 +01:00
|
|
|
*
|
|
|
|
*Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
*"Software"), to deal in the Software without restriction, including
|
|
|
|
*without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
*distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
*permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
*the following conditions:
|
|
|
|
*
|
|
|
|
*The above copyright notice and this permission notice shall be
|
|
|
|
*included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
*EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
*MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
2004-06-21 15:19:32 +02:00
|
|
|
*NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
|
2003-11-14 17:48:57 +01:00
|
|
|
*ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
|
|
*CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
*WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
2009-02-03 16:48:04 +01:00
|
|
|
*Except as contained in this notice, the name of the copyright holder(s)
|
|
|
|
*and author(s) shall not be used in advertising or otherwise to promote
|
|
|
|
*the sale, use or other dealings in this Software without prior written
|
|
|
|
*authorization from the copyright holder(s) and author(s).
|
2003-11-14 17:48:57 +01:00
|
|
|
*
|
|
|
|
* Authors: Harold L Hunt II
|
2009-02-03 16:48:04 +01:00
|
|
|
* Colin Harrison
|
2003-11-14 17:48:57 +01:00
|
|
|
*/
|
|
|
|
|
2005-07-05 00:10:43 +02:00
|
|
|
#ifdef HAVE_XWIN_CONFIG_H
|
|
|
|
#include <xwin-config.h>
|
2011-11-04 19:17:50 +01:00
|
|
|
#else
|
|
|
|
#define HAS_WINSOCK 1
|
2005-07-05 00:10:43 +02:00
|
|
|
#endif
|
2013-06-17 19:27:56 +02:00
|
|
|
|
2013-06-19 14:09:35 +02:00
|
|
|
/*
|
|
|
|
* Including any server header might define the macro _XSERVER64 on 64 bit machines.
|
|
|
|
* That macro must _NOT_ be defined for Xlib client code, otherwise bad things happen.
|
|
|
|
* So let's undef that macro if necessary.
|
|
|
|
*/
|
|
|
|
#ifdef _XSERVER64
|
|
|
|
#undef _XSERVER64
|
|
|
|
#endif
|
|
|
|
|
2013-06-19 19:09:07 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <pthread.h>
|
2013-06-20 13:50:18 +02:00
|
|
|
#include <sys/param.h> // for MAX() macro
|
2013-06-19 19:09:07 +02:00
|
|
|
|
|
|
|
#ifdef HAS_WINSOCK
|
|
|
|
#include <X11/Xwinsock.h>
|
|
|
|
#else
|
2004-11-15 16:06:51 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2013-06-17 19:27:56 +02:00
|
|
|
|
2013-06-20 13:50:18 +02:00
|
|
|
#include <X11/Xatom.h>
|
2012-02-13 20:58:37 +01:00
|
|
|
#include <X11/extensions/Xfixes.h>
|
2013-06-17 19:27:56 +02:00
|
|
|
#include "winclipboard.h"
|
|
|
|
#include "internal.h"
|
2004-06-21 15:19:32 +02:00
|
|
|
|
2013-06-17 00:57:17 +02:00
|
|
|
#define WIN_CONNECT_RETRIES 40
|
|
|
|
#define WIN_CONNECT_DELAY 4
|
|
|
|
|
2013-06-16 23:13:26 +02:00
|
|
|
#define WIN_CLIPBOARD_WINDOW_CLASS "xwinclip"
|
|
|
|
#define WIN_CLIPBOARD_WINDOW_TITLE "xwinclip"
|
2013-06-20 01:04:16 +02:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
|
|
|
#define WIN_MSG_QUEUE_FNAME "/dev/windows"
|
|
|
|
#endif
|
2013-06-16 23:13:26 +02:00
|
|
|
|
2003-11-14 17:48:57 +01:00
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
|
|
|
|
2013-06-17 14:18:16 +02:00
|
|
|
static HWND g_hwndClipboard = NULL;
|
2012-03-21 20:55:09 +01:00
|
|
|
static jmp_buf g_jmpEntry;
|
2010-04-05 14:57:24 +02:00
|
|
|
static XIOErrorHandler g_winClipboardOldIOErrorHandler;
|
|
|
|
static pthread_t g_winClipboardProcThread;
|
2010-10-31 15:53:02 +01:00
|
|
|
|
2012-02-13 20:58:37 +01:00
|
|
|
int xfixes_event_base;
|
|
|
|
int xfixes_error_base;
|
2003-11-14 17:48:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local function prototypes
|
|
|
|
*/
|
|
|
|
|
2013-06-16 23:13:26 +02:00
|
|
|
static HWND
|
2013-06-17 20:58:20 +02:00
|
|
|
winClipboardCreateMessagingWindow(Display *pDisplay, Window iWindow, ClipboardAtoms *atoms);
|
2013-06-16 23:13:26 +02:00
|
|
|
|
2003-11-14 17:48:57 +01:00
|
|
|
static int
|
2012-03-21 20:55:09 +01:00
|
|
|
winClipboardErrorHandler(Display * pDisplay, XErrorEvent * pErr);
|
2003-11-14 17:48:57 +01:00
|
|
|
|
|
|
|
static int
|
2012-03-21 20:55:09 +01:00
|
|
|
winClipboardIOErrorHandler(Display * pDisplay);
|
2003-11-14 17:48:57 +01:00
|
|
|
|
|
|
|
/*
|
2013-06-18 01:32:53 +02:00
|
|
|
* Create X11 and Win32 messaging windows, and run message processing loop
|
|
|
|
*
|
|
|
|
* returns TRUE if shutdown was signalled to loop, FALSE if some error occurred
|
2003-11-14 17:48:57 +01:00
|
|
|
*/
|
|
|
|
|
2013-06-18 01:32:53 +02:00
|
|
|
Bool
|
2013-06-17 20:07:05 +02:00
|
|
|
winClipboardProc(Bool fUseUnicode, char *szDisplay)
|
2003-11-14 17:48:57 +01:00
|
|
|
{
|
2013-06-17 20:58:20 +02:00
|
|
|
ClipboardAtoms atoms;
|
2012-03-21 20:55:09 +01:00
|
|
|
int iReturn;
|
|
|
|
HWND hwnd = NULL;
|
|
|
|
int iConnectionNumber = 0;
|
|
|
|
|
2004-11-15 16:06:51 +01:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-03-21 20:55:09 +01:00
|
|
|
int fdMessageQueue = 0;
|
2004-11-15 16:06:51 +01:00
|
|
|
#else
|
2012-03-21 20:55:09 +01:00
|
|
|
struct timeval tvTimeout;
|
2004-11-15 16:06:51 +01:00
|
|
|
#endif
|
2012-03-21 20:55:09 +01:00
|
|
|
fd_set fdsRead;
|
|
|
|
int iMaxDescriptor;
|
|
|
|
Display *pDisplay = NULL;
|
|
|
|
Window iWindow = None;
|
|
|
|
int iSelectError;
|
2013-06-18 01:32:53 +02:00
|
|
|
Bool fShutdown = FALSE;
|
2013-06-18 20:53:52 +02:00
|
|
|
static Bool fErrorHandlerSet = FALSE;
|
hw/xwin: Retrieve TARGETS to avoid unnecessary failing conversion attempts
See http://cygwin.com/ml/cygwin-xfree/2013-07/msg00016.html
It looks like the change in a9aca218f557c723e637287272819a7c17174e1e had some
unforseen consequences.
If the X11 selection contents are not convertable to COMPOUND_TEXT, UTF8_STRING
or STRING format (for example, if it is an image), after those conversion
attempts have failed, we sit in winProcessXEventsTimeout() until the timeout
expires.
It also seems that maybe gnuplot doesn't respond correctly to this sequence of
conversion requests and doesn't reply to some of them, which also causes us to
sit in winProcessXEventsTimeout() until the timeout expires.
The Windows application which has requested the clipboard contents via
GetClipboardContents() is blocked until we return from WM_RENDERFORMAT, so
sitting waiting for this timeout to expire should be avoided.
So instead, explicitly request conversion to the TARGETS target, choose
the most preferred format, and request conversion to that.
Also: if there is no owned selection, there is nothing to paste, so don't bother
trying to convert it.
v2: Fix compilation with -Werror=declaration-after-statement
Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
Reviewed-by: Colin Harrison <colin.harrison@virgin.net>
2013-09-24 17:02:37 +02:00
|
|
|
ClipboardConversionData data;
|
2012-03-21 20:55:09 +01:00
|
|
|
|
2012-07-05 14:19:02 +02:00
|
|
|
winDebug("winClipboardProc - Hello\n");
|
2012-03-21 20:55:09 +01:00
|
|
|
|
|
|
|
/* Allow multiple threads to access Xlib */
|
|
|
|
if (XInitThreads() == 0) {
|
|
|
|
ErrorF("winClipboardProc - XInitThreads failed.\n");
|
|
|
|
goto winClipboardProc_Exit;
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* See if X supports the current locale */
|
|
|
|
if (XSupportsLocale() == False) {
|
|
|
|
ErrorF("winClipboardProc - Warning: Locale not supported by X.\n");
|
2004-06-21 15:19:32 +02:00
|
|
|
}
|
2003-11-25 20:29:01 +01:00
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
g_winClipboardProcThread = pthread_self();
|
2013-06-18 20:53:52 +02:00
|
|
|
|
|
|
|
/* Set error handler */
|
|
|
|
if (!fErrorHandlerSet) {
|
|
|
|
XSetErrorHandler(winClipboardErrorHandler);
|
|
|
|
g_winClipboardOldIOErrorHandler =
|
|
|
|
XSetIOErrorHandler(winClipboardIOErrorHandler);
|
|
|
|
fErrorHandlerSet = TRUE;
|
|
|
|
}
|
2012-03-21 20:55:09 +01:00
|
|
|
|
|
|
|
/* Set jump point for Error exits */
|
2013-06-19 14:59:40 +02:00
|
|
|
if (setjmp(g_jmpEntry)) {
|
2012-03-21 20:55:09 +01:00
|
|
|
ErrorF("winClipboardProc - setjmp returned for IO Error Handler.\n");
|
2014-01-01 18:54:48 +01:00
|
|
|
goto winClipboardProc_Done;
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Make sure that the display opened */
|
2013-06-18 20:18:08 +02:00
|
|
|
pDisplay = XOpenDisplay(szDisplay);
|
2012-03-21 20:55:09 +01:00
|
|
|
if (pDisplay == NULL) {
|
|
|
|
ErrorF("winClipboardProc - Failed opening the display, giving up\n");
|
|
|
|
goto winClipboardProc_Done;
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
ErrorF("winClipboardProc - XOpenDisplay () returned and "
|
|
|
|
"successfully opened the display.\n");
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Get our connection number */
|
|
|
|
iConnectionNumber = ConnectionNumber(pDisplay);
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2004-11-15 16:06:51 +01:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Open a file descriptor for the windows message queue */
|
|
|
|
fdMessageQueue = open(WIN_MSG_QUEUE_FNAME, O_RDONLY);
|
|
|
|
if (fdMessageQueue == -1) {
|
|
|
|
ErrorF("winClipboardProc - Failed opening %s\n", WIN_MSG_QUEUE_FNAME);
|
|
|
|
goto winClipboardProc_Done;
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Find max of our file descriptors */
|
2013-06-20 13:50:18 +02:00
|
|
|
iMaxDescriptor = MAX(fdMessageQueue, iConnectionNumber) + 1;
|
2004-11-15 16:06:51 +01:00
|
|
|
#else
|
2012-03-21 20:55:09 +01:00
|
|
|
iMaxDescriptor = iConnectionNumber + 1;
|
2004-11-15 16:06:51 +01:00
|
|
|
#endif
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2012-02-13 20:58:37 +01:00
|
|
|
if (!XFixesQueryExtension(pDisplay, &xfixes_event_base, &xfixes_error_base))
|
|
|
|
ErrorF ("winClipboardProc - XFixes extension not present\n");
|
|
|
|
|
2013-06-17 20:58:20 +02:00
|
|
|
/* Create atoms */
|
|
|
|
atoms.atomClipboard = XInternAtom(pDisplay, "CLIPBOARD", False);
|
|
|
|
atoms.atomLocalProperty = XInternAtom (pDisplay, "CYGX_CUT_BUFFER", False);
|
|
|
|
atoms.atomUTF8String = XInternAtom (pDisplay, "UTF8_STRING", False);
|
|
|
|
atoms.atomCompoundText = XInternAtom (pDisplay, "COMPOUND_TEXT", False);
|
|
|
|
atoms.atomTargets = XInternAtom (pDisplay, "TARGETS", False);
|
2012-03-21 20:55:09 +01:00
|
|
|
|
|
|
|
/* Create a messaging window */
|
|
|
|
iWindow = XCreateSimpleWindow(pDisplay,
|
|
|
|
DefaultRootWindow(pDisplay),
|
|
|
|
1, 1,
|
|
|
|
500, 500,
|
|
|
|
0,
|
|
|
|
BlackPixel(pDisplay, 0),
|
|
|
|
BlackPixel(pDisplay, 0));
|
|
|
|
if (iWindow == 0) {
|
|
|
|
ErrorF("winClipboardProc - Could not create an X window.\n");
|
|
|
|
goto winClipboardProc_Done;
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
XStoreName(pDisplay, iWindow, "xwinclip");
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Select event types to watch */
|
|
|
|
if (XSelectInput(pDisplay, iWindow, PropertyChangeMask) == BadWindow)
|
|
|
|
ErrorF("winClipboardProc - XSelectInput generated BadWindow "
|
|
|
|
"on messaging window\n");
|
|
|
|
|
2012-02-13 20:58:37 +01:00
|
|
|
XFixesSelectSelectionInput (pDisplay,
|
|
|
|
iWindow,
|
|
|
|
XA_PRIMARY,
|
|
|
|
XFixesSetSelectionOwnerNotifyMask |
|
|
|
|
XFixesSelectionWindowDestroyNotifyMask |
|
|
|
|
XFixesSelectionClientCloseNotifyMask);
|
|
|
|
|
|
|
|
XFixesSelectSelectionInput (pDisplay,
|
|
|
|
iWindow,
|
2013-06-17 20:58:20 +02:00
|
|
|
atoms.atomClipboard,
|
2012-02-13 20:58:37 +01:00
|
|
|
XFixesSetSelectionOwnerNotifyMask |
|
|
|
|
XFixesSelectionWindowDestroyNotifyMask |
|
|
|
|
XFixesSelectionClientCloseNotifyMask);
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
|
2012-02-13 20:58:37 +01:00
|
|
|
/* Initialize monitored selection state */
|
|
|
|
winClipboardInitMonitoredSelections();
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Create Windows messaging window */
|
2013-06-17 20:58:20 +02:00
|
|
|
hwnd = winClipboardCreateMessagingWindow(pDisplay, iWindow, &atoms);
|
2012-03-21 20:55:09 +01:00
|
|
|
|
2013-06-17 14:18:16 +02:00
|
|
|
/* Save copy of HWND */
|
2012-03-21 20:55:09 +01:00
|
|
|
g_hwndClipboard = hwnd;
|
|
|
|
|
|
|
|
/* Assert ownership of selections if Win32 clipboard is owned */
|
|
|
|
if (NULL != GetClipboardOwner()) {
|
|
|
|
/* PRIMARY */
|
|
|
|
iReturn = XSetSelectionOwner(pDisplay, XA_PRIMARY,
|
|
|
|
iWindow, CurrentTime);
|
|
|
|
if (iReturn == BadAtom || iReturn == BadWindow ||
|
|
|
|
XGetSelectionOwner(pDisplay, XA_PRIMARY) != iWindow) {
|
|
|
|
ErrorF("winClipboardProc - Could not set PRIMARY owner\n");
|
|
|
|
goto winClipboardProc_Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CLIPBOARD */
|
2013-06-17 20:58:20 +02:00
|
|
|
iReturn = XSetSelectionOwner(pDisplay, atoms.atomClipboard,
|
2012-03-21 20:55:09 +01:00
|
|
|
iWindow, CurrentTime);
|
|
|
|
if (iReturn == BadAtom || iReturn == BadWindow ||
|
2013-06-17 20:58:20 +02:00
|
|
|
XGetSelectionOwner(pDisplay, atoms.atomClipboard) != iWindow) {
|
2012-03-21 20:55:09 +01:00
|
|
|
ErrorF("winClipboardProc - Could not set CLIPBOARD owner\n");
|
|
|
|
goto winClipboardProc_Done;
|
|
|
|
}
|
|
|
|
}
|
2003-11-14 17:48:57 +01:00
|
|
|
|
hw/xwin: Retrieve TARGETS to avoid unnecessary failing conversion attempts
See http://cygwin.com/ml/cygwin-xfree/2013-07/msg00016.html
It looks like the change in a9aca218f557c723e637287272819a7c17174e1e had some
unforseen consequences.
If the X11 selection contents are not convertable to COMPOUND_TEXT, UTF8_STRING
or STRING format (for example, if it is an image), after those conversion
attempts have failed, we sit in winProcessXEventsTimeout() until the timeout
expires.
It also seems that maybe gnuplot doesn't respond correctly to this sequence of
conversion requests and doesn't reply to some of them, which also causes us to
sit in winProcessXEventsTimeout() until the timeout expires.
The Windows application which has requested the clipboard contents via
GetClipboardContents() is blocked until we return from WM_RENDERFORMAT, so
sitting waiting for this timeout to expire should be avoided.
So instead, explicitly request conversion to the TARGETS target, choose
the most preferred format, and request conversion to that.
Also: if there is no owned selection, there is nothing to paste, so don't bother
trying to convert it.
v2: Fix compilation with -Werror=declaration-after-statement
Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
Reviewed-by: Colin Harrison <colin.harrison@virgin.net>
2013-09-24 17:02:37 +02:00
|
|
|
data.fUseUnicode = fUseUnicode;
|
2012-03-21 20:55:09 +01:00
|
|
|
|
2013-11-21 16:18:48 +01:00
|
|
|
/* Loop for events */
|
2012-03-21 20:55:09 +01:00
|
|
|
while (1) {
|
2013-11-21 16:18:48 +01:00
|
|
|
|
|
|
|
/* Process X events */
|
|
|
|
winClipboardFlushXEvents(hwnd,
|
|
|
|
iWindow, pDisplay, &data, &atoms);
|
|
|
|
|
|
|
|
/* Process Windows messages */
|
|
|
|
if (!winClipboardFlushWindowsMessageQueue(hwnd)) {
|
|
|
|
ErrorF("winClipboardProc - winClipboardFlushWindowsMessageQueue trapped "
|
|
|
|
"WM_QUIT message, exiting main loop.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We need to ensure that all pending requests are sent */
|
|
|
|
XFlush(pDisplay);
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Setup the file descriptor set */
|
|
|
|
/*
|
|
|
|
* NOTE: You have to do this before every call to select
|
|
|
|
* because select modifies the mask to indicate
|
|
|
|
* which descriptors are ready.
|
|
|
|
*/
|
|
|
|
FD_ZERO(&fdsRead);
|
|
|
|
FD_SET(iConnectionNumber, &fdsRead);
|
2004-11-15 16:06:51 +01:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-03-21 20:55:09 +01:00
|
|
|
FD_SET(fdMessageQueue, &fdsRead);
|
2004-11-15 16:06:51 +01:00
|
|
|
#else
|
2012-03-21 20:55:09 +01:00
|
|
|
tvTimeout.tv_sec = 0;
|
|
|
|
tvTimeout.tv_usec = 100;
|
2004-11-15 16:06:51 +01:00
|
|
|
#endif
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Wait for a Windows event or an X event */
|
|
|
|
iReturn = select(iMaxDescriptor, /* Highest fds number */
|
|
|
|
&fdsRead, /* Read mask */
|
|
|
|
NULL, /* No write mask */
|
|
|
|
NULL, /* No exception mask */
|
2004-11-15 16:06:51 +01:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-03-21 20:55:09 +01:00
|
|
|
NULL /* No timeout */
|
2004-11-15 16:06:51 +01:00
|
|
|
#else
|
2012-03-21 20:55:09 +01:00
|
|
|
&tvTimeout /* Set timeout */
|
2004-11-15 16:06:51 +01:00
|
|
|
#endif
|
2012-03-21 20:55:09 +01:00
|
|
|
);
|
2004-11-15 16:06:51 +01:00
|
|
|
|
|
|
|
#ifndef HAS_WINSOCK
|
2012-03-21 20:55:09 +01:00
|
|
|
iSelectError = errno;
|
2004-11-15 16:06:51 +01:00
|
|
|
#else
|
2012-03-21 20:55:09 +01:00
|
|
|
iSelectError = WSAGetLastError();
|
2004-11-15 16:06:51 +01:00
|
|
|
#endif
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
if (iReturn < 0) {
|
2004-11-15 16:06:51 +01:00
|
|
|
#ifndef HAS_WINSOCK
|
2012-03-21 20:55:09 +01:00
|
|
|
if (iSelectError == EINTR)
|
2004-11-15 16:06:51 +01:00
|
|
|
#else
|
2012-03-21 20:55:09 +01:00
|
|
|
if (iSelectError == WSAEINTR)
|
2004-11-15 16:06:51 +01:00
|
|
|
#endif
|
2012-03-21 20:55:09 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ErrorF("winClipboardProc - Call to select () failed: %d. "
|
|
|
|
"Bailing.\n", iReturn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(iConnectionNumber, &fdsRead)) {
|
2013-11-21 16:18:48 +01:00
|
|
|
winDebug
|
|
|
|
("winClipboardProc - X connection ready, pumping X event queue\n");
|
2012-03-21 20:55:09 +01:00
|
|
|
}
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2004-11-15 16:06:51 +01:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Check for Windows event ready */
|
|
|
|
if (FD_ISSET(fdMessageQueue, &fdsRead))
|
2004-11-15 16:06:51 +01:00
|
|
|
#else
|
2012-03-21 20:55:09 +01:00
|
|
|
if (1)
|
2003-11-14 17:48:57 +01:00
|
|
|
#endif
|
2012-03-21 20:55:09 +01:00
|
|
|
{
|
2013-11-21 16:18:48 +01:00
|
|
|
winDebug
|
|
|
|
("winClipboardProc - /dev/windows ready, pumping Windows message queue\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_DEVWINDOWS
|
|
|
|
if (!(FD_ISSET(iConnectionNumber, &fdsRead)) &&
|
|
|
|
!(FD_ISSET(fdMessageQueue, &fdsRead))) {
|
|
|
|
winDebug("winClipboardProc - Spurious wake, select() returned %d\n", iReturn);
|
2012-03-21 20:55:09 +01:00
|
|
|
}
|
2013-11-21 16:18:48 +01:00
|
|
|
#endif
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
winClipboardProc_Exit:
|
2013-06-18 01:32:53 +02:00
|
|
|
/* broke out of while loop on a shutdown message */
|
|
|
|
fShutdown = TRUE;
|
2012-03-21 20:55:09 +01:00
|
|
|
|
|
|
|
winClipboardProc_Done:
|
|
|
|
/* Close our Windows window */
|
|
|
|
if (g_hwndClipboard) {
|
hw/xwin: Fix clipboard thread restart
It seems that the clipboard thread restart mechanism has been broken for a
while, which can be demonstrated using XDMCP with KDM (e.g. to a Kubutunu 12.04
host)
KDM kills all attached clients, including the clipboard integration client,
which restarts, but then exits on WM_QUIT.
Using PostQuitMessage() in WM_DESTROY is unhelpful, as we may not actually be
quitting the thread, if we just destroyed the window because the clipboard
thread is about to retry, because he WM_QUIT message sticks around, and is
noticed the next time we look at the window message queue and confuses us into
thinking we need to quit.
Sending a WM_DESTROY is apparently never correct anyhow, see [1]
So:
1/ Use DestroyWindow() to destroy the clipboard messaging window when cleaning
up for retry or exit in winClipboardProc (the clipboard thread main proc)
2/ Send a special WM_WM_QUIT message in winClipboardWindowDestroy() from the X
server thread when the X server is resetting.
3/ When processing that WM_WM_QUIT message in the clipboard thread, cause the
clipboard window to PostQuitMessage(), which causes the clipboard thread to
exit.
[1] http://blogs.msdn.com/b/oldnewthing/archive/2011/09/26/10216420.aspx
Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
Reviewed-by: Colin Harrison <colin.harrison@virgin.net>
2014-02-22 00:20:00 +01:00
|
|
|
DestroyWindow(g_hwndClipboard);
|
2010-10-31 15:53:02 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Close our X window */
|
|
|
|
if (pDisplay && iWindow) {
|
|
|
|
iReturn = XDestroyWindow(pDisplay, iWindow);
|
|
|
|
if (iReturn == BadWindow)
|
|
|
|
ErrorF("winClipboardProc - XDestroyWindow returned BadWindow.\n");
|
|
|
|
else
|
|
|
|
ErrorF("winClipboardProc - XDestroyWindow succeeded.\n");
|
2004-06-21 15:19:32 +02:00
|
|
|
}
|
|
|
|
|
2004-11-15 16:06:51 +01:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-03-21 20:55:09 +01:00
|
|
|
/* Close our Win32 message handle */
|
|
|
|
if (fdMessageQueue)
|
|
|
|
close(fdMessageQueue);
|
2004-11-15 16:06:51 +01:00
|
|
|
#endif
|
2004-06-21 15:19:32 +02:00
|
|
|
|
|
|
|
#if 0
|
2012-03-21 20:55:09 +01:00
|
|
|
/*
|
2013-06-19 15:00:28 +02:00
|
|
|
* FIXME: XCloseDisplay hangs if we call it
|
|
|
|
*
|
|
|
|
* XCloseDisplay() calls XSync(), so any outstanding errors are reported.
|
|
|
|
* If we are built into the server, this can deadlock if the server is
|
|
|
|
* in the process of exiting and waiting for this thread to exit.
|
2012-03-21 20:55:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Discard any remaining events */
|
|
|
|
XSync(pDisplay, TRUE);
|
|
|
|
|
|
|
|
/* Select event types to watch */
|
|
|
|
XSelectInput(pDisplay, DefaultRootWindow(pDisplay), None);
|
|
|
|
|
|
|
|
/* Close our X display */
|
|
|
|
if (pDisplay) {
|
|
|
|
XCloseDisplay(pDisplay);
|
2004-06-21 15:19:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
/* global clipboard variable reset */
|
|
|
|
g_hwndClipboard = NULL;
|
|
|
|
|
2013-06-18 01:32:53 +02:00
|
|
|
return fShutdown;
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
|
|
|
|
2013-06-16 23:13:26 +02:00
|
|
|
/*
|
|
|
|
* Create the Windows window that we use to receive Windows messages
|
|
|
|
*/
|
|
|
|
|
|
|
|
static HWND
|
2013-06-17 20:58:20 +02:00
|
|
|
winClipboardCreateMessagingWindow(Display *pDisplay, Window iWindow, ClipboardAtoms *atoms)
|
2013-06-16 23:13:26 +02:00
|
|
|
{
|
|
|
|
WNDCLASSEX wc;
|
2013-06-16 23:35:22 +02:00
|
|
|
ClipboardWindowCreationParams cwcp;
|
2013-06-16 23:13:26 +02:00
|
|
|
HWND hwnd;
|
|
|
|
|
|
|
|
/* Setup our window class */
|
|
|
|
wc.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wc.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
wc.lpfnWndProc = winClipboardWindowProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = GetModuleHandle(NULL);
|
|
|
|
wc.hIcon = 0;
|
|
|
|
wc.hCursor = 0;
|
|
|
|
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = WIN_CLIPBOARD_WINDOW_CLASS;
|
|
|
|
wc.hIconSm = 0;
|
|
|
|
RegisterClassEx(&wc);
|
|
|
|
|
2013-06-16 23:35:22 +02:00
|
|
|
/* Information to be passed to WM_CREATE */
|
|
|
|
cwcp.pClipboardDisplay = pDisplay;
|
|
|
|
cwcp.iClipboardWindow = iWindow;
|
2013-06-17 20:58:20 +02:00
|
|
|
cwcp.atoms = atoms;
|
2013-06-16 23:35:22 +02:00
|
|
|
|
2013-06-16 23:13:26 +02:00
|
|
|
/* Create the window */
|
|
|
|
hwnd = CreateWindowExA(0, /* Extended styles */
|
|
|
|
WIN_CLIPBOARD_WINDOW_CLASS, /* Class name */
|
|
|
|
WIN_CLIPBOARD_WINDOW_TITLE, /* Window name */
|
|
|
|
WS_OVERLAPPED, /* Not visible anyway */
|
|
|
|
CW_USEDEFAULT, /* Horizontal position */
|
|
|
|
CW_USEDEFAULT, /* Vertical position */
|
|
|
|
CW_USEDEFAULT, /* Right edge */
|
|
|
|
CW_USEDEFAULT, /* Bottom edge */
|
|
|
|
(HWND) NULL, /* No parent or owner window */
|
|
|
|
(HMENU) NULL, /* No menu */
|
|
|
|
GetModuleHandle(NULL), /* Instance handle */
|
2013-06-16 23:35:22 +02:00
|
|
|
&cwcp); /* Creation data */
|
2013-06-16 23:13:26 +02:00
|
|
|
assert(hwnd != NULL);
|
|
|
|
|
|
|
|
/* I'm not sure, but we may need to call this to start message processing */
|
|
|
|
ShowWindow(hwnd, SW_HIDE);
|
|
|
|
|
|
|
|
/* Similarly, we may need a call to this even though we don't paint */
|
|
|
|
UpdateWindow(hwnd);
|
|
|
|
|
|
|
|
return hwnd;
|
|
|
|
}
|
|
|
|
|
2003-11-14 17:48:57 +01:00
|
|
|
/*
|
|
|
|
* winClipboardErrorHandler - Our application specific error handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2012-03-21 20:55:09 +01:00
|
|
|
winClipboardErrorHandler(Display * pDisplay, XErrorEvent * pErr)
|
2003-11-14 17:48:57 +01:00
|
|
|
{
|
2012-03-21 20:55:09 +01:00
|
|
|
char pszErrorMsg[100];
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
XGetErrorText(pDisplay, pErr->error_code, pszErrorMsg, sizeof(pszErrorMsg));
|
|
|
|
ErrorF("winClipboardErrorHandler - ERROR: \n\t%s\n"
|
|
|
|
"\tSerial: %lu, Request Code: %d, Minor Code: %d\n",
|
|
|
|
pszErrorMsg, pErr->serial, pErr->request_code, pErr->minor_code);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-11-14 17:48:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* winClipboardIOErrorHandler - Our application specific IO error handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2012-03-21 20:55:09 +01:00
|
|
|
winClipboardIOErrorHandler(Display * pDisplay)
|
2003-11-14 17:48:57 +01:00
|
|
|
{
|
2010-04-05 15:22:39 +02:00
|
|
|
ErrorF("winClipboardIOErrorHandler!\n");
|
2003-11-14 17:48:57 +01:00
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
if (pthread_equal(pthread_self(), g_winClipboardProcThread)) {
|
|
|
|
/* Restart at the main entry point */
|
2013-06-19 14:59:40 +02:00
|
|
|
longjmp(g_jmpEntry, 2);
|
2010-04-05 14:57:24 +02:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
if (g_winClipboardOldIOErrorHandler)
|
|
|
|
g_winClipboardOldIOErrorHandler(pDisplay);
|
2010-04-05 14:57:24 +02:00
|
|
|
|
2012-03-21 20:55:09 +01:00
|
|
|
return 0;
|
2003-11-14 17:48:57 +01:00
|
|
|
}
|
2013-06-17 14:18:16 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
winClipboardWindowDestroy(void)
|
|
|
|
{
|
|
|
|
if (g_hwndClipboard) {
|
hw/xwin: Fix clipboard thread restart
It seems that the clipboard thread restart mechanism has been broken for a
while, which can be demonstrated using XDMCP with KDM (e.g. to a Kubutunu 12.04
host)
KDM kills all attached clients, including the clipboard integration client,
which restarts, but then exits on WM_QUIT.
Using PostQuitMessage() in WM_DESTROY is unhelpful, as we may not actually be
quitting the thread, if we just destroyed the window because the clipboard
thread is about to retry, because he WM_QUIT message sticks around, and is
noticed the next time we look at the window message queue and confuses us into
thinking we need to quit.
Sending a WM_DESTROY is apparently never correct anyhow, see [1]
So:
1/ Use DestroyWindow() to destroy the clipboard messaging window when cleaning
up for retry or exit in winClipboardProc (the clipboard thread main proc)
2/ Send a special WM_WM_QUIT message in winClipboardWindowDestroy() from the X
server thread when the X server is resetting.
3/ When processing that WM_WM_QUIT message in the clipboard thread, cause the
clipboard window to PostQuitMessage(), which causes the clipboard thread to
exit.
[1] http://blogs.msdn.com/b/oldnewthing/archive/2011/09/26/10216420.aspx
Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
Reviewed-by: Colin Harrison <colin.harrison@virgin.net>
2014-02-22 00:20:00 +01:00
|
|
|
SendMessage(g_hwndClipboard, WM_WM_QUIT, 0, 0);
|
2013-06-17 14:18:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
winFixClipboardChain(void)
|
|
|
|
{
|
|
|
|
if (g_hwndClipboard) {
|
|
|
|
PostMessage(g_hwndClipboard, WM_WM_REINIT, 0, 0);
|
|
|
|
}
|
|
|
|
}
|