2006-11-26 11:13:41 -07:00
|
|
|
/*
|
|
|
|
*Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
|
2010-07-27 13:02:24 -06:00
|
|
|
*Copyright (C) Colin Harrison 2005-2008
|
2006-11-26 11:13:41 -07: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
|
|
|
|
*NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
|
|
|
|
*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.
|
|
|
|
*
|
2010-07-27 13:02:24 -06: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).
|
2006-11-26 11:13:41 -07:00
|
|
|
*
|
|
|
|
* Authors: Harold L Hunt II
|
2010-07-27 13:02:24 -06:00
|
|
|
* Colin Harrison
|
2006-11-26 11:13:41 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_XWIN_CONFIG_H
|
|
|
|
#include <xwin-config.h>
|
2012-06-10 07:21:05 -06:00
|
|
|
#else
|
|
|
|
#define HAS_WINSOCK 1
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2015-09-16 13:10:19 -06: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.
|
2006-11-26 11:13:41 -07:00
|
|
|
*/
|
2015-09-16 13:10:19 -06:00
|
|
|
#ifdef _XSERVER64
|
|
|
|
#undef _XSERVER64
|
|
|
|
#endif
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/param.h> // for MAX() macro
|
|
|
|
|
|
|
|
#ifdef HAS_WINSOCK
|
|
|
|
#include <X11/Xwinsock.h>
|
|
|
|
#else
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <X11/extensions/Xfixes.h>
|
|
|
|
#include "winclipboard.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#define WIN_CONNECT_RETRIES 40
|
|
|
|
#define WIN_CONNECT_DELAY 4
|
|
|
|
|
|
|
|
#define WIN_CLIPBOARD_WINDOW_CLASS "xwinclip"
|
|
|
|
#define WIN_CLIPBOARD_WINDOW_TITLE "xwinclip"
|
|
|
|
#ifdef HAS_DEVWINDOWS
|
|
|
|
#define WIN_MSG_QUEUE_FNAME "/dev/windows"
|
|
|
|
#endif
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
static HWND g_hwndClipboard = NULL;
|
2012-06-10 07:21:05 -06:00
|
|
|
static jmp_buf g_jmpEntry;
|
|
|
|
static XIOErrorHandler g_winClipboardOldIOErrorHandler;
|
|
|
|
static pthread_t g_winClipboardProcThread;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
int xfixes_event_base;
|
|
|
|
int xfixes_error_base;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2016-05-29 06:02:34 -06:00
|
|
|
Bool g_fHasModernClipboardApi = FALSE;
|
|
|
|
ADDCLIPBOARDFORMATLISTENERPROC g_fpAddClipboardFormatListener;
|
|
|
|
REMOVECLIPBOARDFORMATLISTENERPROC g_fpRemoveClipboardFormatListener;
|
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
/*
|
|
|
|
* Local function prototypes
|
|
|
|
*/
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
static HWND
|
|
|
|
winClipboardCreateMessagingWindow(Display *pDisplay, Window iWindow, ClipboardAtoms *atoms);
|
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
static int
|
2012-06-10 07:21:05 -06:00
|
|
|
winClipboardErrorHandler(Display * pDisplay, XErrorEvent * pErr);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
static int
|
2012-06-10 07:21:05 -06:00
|
|
|
winClipboardIOErrorHandler(Display * pDisplay);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
/*
|
2015-09-16 13:10:19 -06: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
|
2006-11-26 11:13:41 -07:00
|
|
|
*/
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
Bool
|
|
|
|
winClipboardProc(Bool fUseUnicode, char *szDisplay)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2015-09-16 13:10:19 -06:00
|
|
|
ClipboardAtoms atoms;
|
2012-06-10 07:21:05 -06:00
|
|
|
int iReturn;
|
|
|
|
HWND hwnd = NULL;
|
|
|
|
int iConnectionNumber = 0;
|
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-06-10 07:21:05 -06:00
|
|
|
int fdMessageQueue = 0;
|
2006-11-26 11:13:41 -07:00
|
|
|
#else
|
2012-06-10 07:21:05 -06:00
|
|
|
struct timeval tvTimeout;
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
2012-06-10 07:21:05 -06:00
|
|
|
fd_set fdsRead;
|
|
|
|
int iMaxDescriptor;
|
|
|
|
Display *pDisplay = NULL;
|
|
|
|
Window iWindow = None;
|
|
|
|
int iSelectError;
|
2015-09-16 13:10:19 -06:00
|
|
|
Bool fShutdown = FALSE;
|
|
|
|
static Bool fErrorHandlerSet = FALSE;
|
|
|
|
ClipboardConversionData data;
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2013-06-07 11:28:45 -06:00
|
|
|
winDebug("winClipboardProc - Hello\n");
|
2012-06-10 07:21:05 -06:00
|
|
|
|
|
|
|
/* Allow multiple threads to access Xlib */
|
|
|
|
if (XInitThreads() == 0) {
|
|
|
|
ErrorF("winClipboardProc - XInitThreads failed.\n");
|
|
|
|
goto winClipboardProc_Exit;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
/* See if X supports the current locale */
|
|
|
|
if (XSupportsLocale() == False) {
|
|
|
|
ErrorF("winClipboardProc - Warning: Locale not supported by X.\n");
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2016-05-29 06:02:34 -06:00
|
|
|
g_fpAddClipboardFormatListener = (ADDCLIPBOARDFORMATLISTENERPROC)GetProcAddress(GetModuleHandle("user32"),"AddClipboardFormatListener");
|
|
|
|
g_fpRemoveClipboardFormatListener = (REMOVECLIPBOARDFORMATLISTENERPROC)GetProcAddress(GetModuleHandle("user32"),"RemoveClipboardFormatListener");
|
|
|
|
g_fHasModernClipboardApi = g_fpAddClipboardFormatListener && g_fpRemoveClipboardFormatListener;
|
|
|
|
ErrorF("OS maintains clipboard viewer chain: %s\n", g_fHasModernClipboardApi ? "yes" : "no");
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
g_winClipboardProcThread = pthread_self();
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
/* Set error handler */
|
|
|
|
if (!fErrorHandlerSet) {
|
|
|
|
XSetErrorHandler(winClipboardErrorHandler);
|
|
|
|
g_winClipboardOldIOErrorHandler =
|
|
|
|
XSetIOErrorHandler(winClipboardIOErrorHandler);
|
|
|
|
fErrorHandlerSet = TRUE;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
/* Set jump point for Error exits */
|
|
|
|
if (setjmp(g_jmpEntry)) {
|
|
|
|
ErrorF("winClipboardProc - setjmp returned for IO Error Handler.\n");
|
|
|
|
goto winClipboardProc_Done;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
/* Make sure that the display opened */
|
2015-09-16 13:10:19 -06:00
|
|
|
pDisplay = XOpenDisplay(szDisplay);
|
2012-06-10 07:21:05 -06:00
|
|
|
if (pDisplay == NULL) {
|
|
|
|
ErrorF("winClipboardProc - Failed opening the display, giving up\n");
|
|
|
|
goto winClipboardProc_Done;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
ErrorF("winClipboardProc - XOpenDisplay () returned and "
|
|
|
|
"successfully opened the display.\n");
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
/* Get our connection number */
|
|
|
|
iConnectionNumber = ConnectionNumber(pDisplay);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-06-10 07:21:05 -06: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;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
/* Find max of our file descriptors */
|
2015-09-16 13:10:19 -06:00
|
|
|
iMaxDescriptor = MAX(fdMessageQueue, iConnectionNumber) + 1;
|
2006-11-26 11:13:41 -07:00
|
|
|
#else
|
2012-06-10 07:21:05 -06:00
|
|
|
iMaxDescriptor = iConnectionNumber + 1;
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
if (!XFixesQueryExtension(pDisplay, &xfixes_event_base, &xfixes_error_base))
|
|
|
|
ErrorF ("winClipboardProc - XFixes extension not present\n");
|
|
|
|
|
|
|
|
/* 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-06-10 07:21:05 -06: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;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
XStoreName(pDisplay, iWindow, "xwinclip");
|
|
|
|
|
|
|
|
/* Select event types to watch */
|
|
|
|
if (XSelectInput(pDisplay, iWindow, PropertyChangeMask) == BadWindow)
|
|
|
|
ErrorF("winClipboardProc - XSelectInput generated BadWindow "
|
|
|
|
"on messaging window\n");
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
XFixesSelectSelectionInput (pDisplay,
|
|
|
|
iWindow,
|
|
|
|
XA_PRIMARY,
|
|
|
|
XFixesSetSelectionOwnerNotifyMask |
|
|
|
|
XFixesSelectionWindowDestroyNotifyMask |
|
|
|
|
XFixesSelectionClientCloseNotifyMask);
|
|
|
|
|
|
|
|
XFixesSelectSelectionInput (pDisplay,
|
|
|
|
iWindow,
|
|
|
|
atoms.atomClipboard,
|
|
|
|
XFixesSetSelectionOwnerNotifyMask |
|
|
|
|
XFixesSelectionWindowDestroyNotifyMask |
|
|
|
|
XFixesSelectionClientCloseNotifyMask);
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
|
|
|
|
/* Initialize monitored selection state */
|
|
|
|
winClipboardInitMonitoredSelections();
|
2012-06-10 07:21:05 -06:00
|
|
|
/* Create Windows messaging window */
|
2015-09-16 13:10:19 -06:00
|
|
|
hwnd = winClipboardCreateMessagingWindow(pDisplay, iWindow, &atoms);
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
/* Save copy of HWND */
|
2012-06-10 07:21:05 -06: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 */
|
2015-09-16 13:10:19 -06:00
|
|
|
iReturn = XSetSelectionOwner(pDisplay, atoms.atomClipboard,
|
2012-06-10 07:21:05 -06:00
|
|
|
iWindow, CurrentTime);
|
|
|
|
if (iReturn == BadAtom || iReturn == BadWindow ||
|
2015-09-16 13:10:19 -06:00
|
|
|
XGetSelectionOwner(pDisplay, atoms.atomClipboard) != iWindow) {
|
2012-06-10 07:21:05 -06:00
|
|
|
ErrorF("winClipboardProc - Could not set CLIPBOARD owner\n");
|
|
|
|
goto winClipboardProc_Done;
|
|
|
|
}
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
data.fUseUnicode = fUseUnicode;
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
/* Loop for events */
|
|
|
|
while (1) {
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2015-09-16 13:10:19 -06: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-06-10 07:21:05 -06: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);
|
2006-11-26 11:13:41 -07:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-06-10 07:21:05 -06:00
|
|
|
FD_SET(fdMessageQueue, &fdsRead);
|
2006-11-26 11:13:41 -07:00
|
|
|
#else
|
2012-06-10 07:21:05 -06:00
|
|
|
tvTimeout.tv_sec = 0;
|
|
|
|
tvTimeout.tv_usec = 100;
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
|
|
|
|
2012-06-10 07:21:05 -06: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 */
|
2006-11-26 11:13:41 -07:00
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-06-10 07:21:05 -06:00
|
|
|
NULL /* No timeout */
|
2006-11-26 11:13:41 -07:00
|
|
|
#else
|
2012-06-10 07:21:05 -06:00
|
|
|
&tvTimeout /* Set timeout */
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
2012-06-10 07:21:05 -06:00
|
|
|
);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
#ifndef HAS_WINSOCK
|
2012-06-10 07:21:05 -06:00
|
|
|
iSelectError = errno;
|
2006-11-26 11:13:41 -07:00
|
|
|
#else
|
2012-06-10 07:21:05 -06:00
|
|
|
iSelectError = WSAGetLastError();
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
if (iReturn < 0) {
|
2006-11-26 11:13:41 -07:00
|
|
|
#ifndef HAS_WINSOCK
|
2012-06-10 07:21:05 -06:00
|
|
|
if (iSelectError == EINTR)
|
2006-11-26 11:13:41 -07:00
|
|
|
#else
|
2012-06-10 07:21:05 -06:00
|
|
|
if (iSelectError == WSAEINTR)
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
2012-06-10 07:21:05 -06:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ErrorF("winClipboardProc - Call to select () failed: %d. "
|
|
|
|
"Bailing.\n", iReturn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(iConnectionNumber, &fdsRead)) {
|
2015-09-16 13:10:19 -06:00
|
|
|
winDebug
|
|
|
|
("winClipboardProc - X connection ready, pumping X event queue\n");
|
2012-06-10 07:21:05 -06:00
|
|
|
}
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-06-10 07:21:05 -06:00
|
|
|
/* Check for Windows event ready */
|
|
|
|
if (FD_ISSET(fdMessageQueue, &fdsRead))
|
2006-11-26 11:13:41 -07:00
|
|
|
#else
|
2012-06-10 07:21:05 -06:00
|
|
|
if (1)
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
2012-06-10 07:21:05 -06:00
|
|
|
{
|
2015-09-16 13:10:19 -06: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-06-10 07:21:05 -06:00
|
|
|
}
|
2015-09-16 13:10:19 -06:00
|
|
|
#endif
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
winClipboardProc_Exit:
|
2015-09-16 13:10:19 -06:00
|
|
|
/* broke out of while loop on a shutdown message */
|
|
|
|
fShutdown = TRUE;
|
2012-06-10 07:21:05 -06:00
|
|
|
|
|
|
|
winClipboardProc_Done:
|
|
|
|
/* Close our Windows window */
|
|
|
|
if (g_hwndClipboard) {
|
2015-09-16 13:10:19 -06:00
|
|
|
DestroyWindow(g_hwndClipboard);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06: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");
|
|
|
|
}
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
#ifdef HAS_DEVWINDOWS
|
2012-06-10 07:21:05 -06:00
|
|
|
/* Close our Win32 message handle */
|
|
|
|
if (fdMessageQueue)
|
|
|
|
close(fdMessageQueue);
|
2006-11-26 11:13:41 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2012-06-10 07:21:05 -06:00
|
|
|
/*
|
2015-09-16 13:10:19 -06: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-06-10 07:21:05 -06: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);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
/* global clipboard variable reset */
|
|
|
|
g_hwndClipboard = NULL;
|
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
return fShutdown;
|
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
/*
|
|
|
|
* Create the Windows window that we use to receive Windows messages
|
|
|
|
*/
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2015-09-16 13:10:19 -06:00
|
|
|
static HWND
|
|
|
|
winClipboardCreateMessagingWindow(Display *pDisplay, Window iWindow, ClipboardAtoms *atoms)
|
|
|
|
{
|
|
|
|
WNDCLASSEX wc;
|
|
|
|
ClipboardWindowCreationParams cwcp;
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Information to be passed to WM_CREATE */
|
|
|
|
cwcp.pClipboardDisplay = pDisplay;
|
|
|
|
cwcp.iClipboardWindow = iWindow;
|
|
|
|
cwcp.atoms = atoms;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
&cwcp); /* Creation data */
|
|
|
|
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;
|
2012-06-10 07:21:05 -06:00
|
|
|
}
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* winClipboardErrorHandler - Our application specific error handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2012-06-10 07:21:05 -06:00
|
|
|
winClipboardErrorHandler(Display * pDisplay, XErrorEvent * pErr)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2012-06-10 07:21:05 -06:00
|
|
|
char pszErrorMsg[100];
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06: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;
|
|
|
|
}
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* winClipboardIOErrorHandler - Our application specific IO error handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2012-06-10 07:21:05 -06:00
|
|
|
winClipboardIOErrorHandler(Display * pDisplay)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2014-05-02 13:27:46 -06:00
|
|
|
ErrorF("winClipboardIOErrorHandler!\n");
|
2012-06-10 07:21:05 -06:00
|
|
|
|
|
|
|
if (pthread_equal(pthread_self(), g_winClipboardProcThread)) {
|
|
|
|
/* Restart at the main entry point */
|
2015-09-16 13:10:19 -06:00
|
|
|
longjmp(g_jmpEntry, 2);
|
2012-06-10 07:21:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (g_winClipboardOldIOErrorHandler)
|
|
|
|
g_winClipboardOldIOErrorHandler(pDisplay);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
return 0;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
2015-09-16 13:10:19 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
winClipboardWindowDestroy(void)
|
|
|
|
{
|
|
|
|
if (g_hwndClipboard) {
|
|
|
|
SendMessage(g_hwndClipboard, WM_WM_QUIT, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
winFixClipboardChain(void)
|
|
|
|
{
|
|
|
|
if (g_hwndClipboard) {
|
|
|
|
PostMessage(g_hwndClipboard, WM_WM_REINIT, 0, 0);
|
|
|
|
}
|
|
|
|
}
|