2 *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3 *Copyright (C) Colin Harrison 2005-2008
5 *Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 *"Software"), to deal in the Software without restriction, including
8 *without limitation the rights to use, copy, modify, merge, publish,
9 *distribute, sublicense, and/or sell copies of the Software, and to
10 *permit persons to whom the Software is furnished to do so, subject to
11 *the following conditions:
13 *The above copyright notice and this permission notice shall be
14 *included in all copies or substantial portions of the Software.
16 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
20 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *Except as contained in this notice, the name of the copyright holder(s)
25 *and author(s) shall not be used in advertising or otherwise to promote
26 *the sale, use or other dealings in this Software without prior written
27 *authorization from the copyright holder(s) and author(s).
29 * Authors: Harold L Hunt II
33 #ifdef HAVE_XWIN_CONFIG_H
34 #include <xwin-config.h>
38 #include <sys/types.h>
40 #include "winclipboard.h"
47 * References to external symbols
50 extern Bool g_fUnicodeClipboard
;
51 extern Bool g_fClipboardStarted
;
52 extern Bool g_fClipboardLaunched
;
53 extern Bool g_fClipboard
;
54 extern HWND g_hwndClipboard
;
55 extern void *g_pClipboardDisplay
;
56 extern Window g_iClipboardWindow
;
62 static jmp_buf g_jmpEntry
;
63 static int clipboardRestarts
= 0;
64 static XIOErrorHandler g_winClipboardOldIOErrorHandler
;
65 static pthread_t g_winClipboardProcThread
;
67 Bool g_fUseUnicode
= FALSE
;
70 * Local function prototypes
74 winClipboardErrorHandler(Display
* pDisplay
, XErrorEvent
* pErr
);
77 winClipboardIOErrorHandler(Display
* pDisplay
);
80 * Main thread function
84 winClipboardProc(void *pvNotUsed
)
89 int iConnectionNumber
= 0;
92 int fdMessageQueue
= 0;
94 struct timeval tvTimeout
;
98 Display
*pDisplay
= NULL
;
99 Window iWindow
= None
;
105 winDebug("winClipboardProc - Hello\n");
108 /* Do we use Unicode clipboard? */
109 fUseUnicode
= g_fUnicodeClipboard
;
111 /* Save the Unicode support flag in a global */
112 g_fUseUnicode
= fUseUnicode
;
114 /* Allow multiple threads to access Xlib */
115 if (XInitThreads() == 0) {
116 ErrorF("winClipboardProc - XInitThreads failed.\n");
117 goto winClipboardProc_Exit
;
120 /* See if X supports the current locale */
121 if (XSupportsLocale() == False
) {
122 ErrorF("winClipboardProc - Warning: Locale not supported by X.\n");
125 /* Set error handler */
126 XSetErrorHandler(winClipboardErrorHandler
);
127 g_winClipboardProcThread
= pthread_self();
128 g_winClipboardOldIOErrorHandler
=
129 XSetIOErrorHandler(winClipboardIOErrorHandler
);
131 /* Set jump point for Error exits */
132 iReturn
= setjmp(g_jmpEntry
);
134 /* Check if we should continue operations */
135 if (iReturn
!= WIN_JMP_ERROR_IO
&& iReturn
!= WIN_JMP_OKAY
) {
136 /* setjmp returned an unknown value, exit */
137 ErrorF("winClipboardProc - setjmp returned: %d exiting\n", iReturn
);
138 goto winClipboardProc_Exit
;
140 else if (iReturn
== WIN_JMP_ERROR_IO
) {
141 /* TODO: Cleanup the Win32 window and free any allocated memory */
142 ErrorF("winClipboardProc - setjmp returned for IO Error Handler.\n");
146 /* Use our generated cookie for authentication */
147 winSetAuthorization();
149 /* Initialize retry count */
152 /* Setup the display connection string x */
154 * NOTE: Always connect to screen 0 since we require that screen
155 * numbers start at 0 and increase without gaps. We only need
156 * to connect to one screen on the display to get events
157 * for all screens on the display. That is why there is only
158 * one clipboard client thread.
160 snprintf(szDisplay
, 512, "127.0.0.1:%s.0", display
);
162 /* Print the display connection string */
163 ErrorF("winClipboardProc - DISPLAY=%s\n", szDisplay
);
165 /* Open the X display */
167 pDisplay
= XOpenDisplay(szDisplay
);
168 if (pDisplay
== NULL
) {
169 ErrorF("winClipboardProc - Could not open display, "
170 "try: %d, sleeping: %d\n", iRetries
+ 1, WIN_CONNECT_DELAY
);
172 sleep(WIN_CONNECT_DELAY
);
178 while (pDisplay
== NULL
&& iRetries
< WIN_CONNECT_RETRIES
);
180 /* Make sure that the display opened */
181 if (pDisplay
== NULL
) {
182 ErrorF("winClipboardProc - Failed opening the display, giving up\n");
183 goto winClipboardProc_Done
;
186 /* Save the display in the screen privates */
187 g_pClipboardDisplay
= pDisplay
;
189 ErrorF("winClipboardProc - XOpenDisplay () returned and "
190 "successfully opened the display.\n");
192 /* Get our connection number */
193 iConnectionNumber
= ConnectionNumber(pDisplay
);
195 #ifdef HAS_DEVWINDOWS
196 /* Open a file descriptor for the windows message queue */
197 fdMessageQueue
= open(WIN_MSG_QUEUE_FNAME
, O_RDONLY
);
198 if (fdMessageQueue
== -1) {
199 ErrorF("winClipboardProc - Failed opening %s\n", WIN_MSG_QUEUE_FNAME
);
200 goto winClipboardProc_Done
;
203 /* Find max of our file descriptors */
204 iMaxDescriptor
= max(fdMessageQueue
, iConnectionNumber
) + 1;
206 iMaxDescriptor
= iConnectionNumber
+ 1;
210 atomClipboard
= XInternAtom(pDisplay
, "CLIPBOARD", False
);
212 /* Create a messaging window */
213 iWindow
= XCreateSimpleWindow(pDisplay
,
214 DefaultRootWindow(pDisplay
),
218 BlackPixel(pDisplay
, 0),
219 BlackPixel(pDisplay
, 0));
221 ErrorF("winClipboardProc - Could not create an X window.\n");
222 goto winClipboardProc_Done
;
225 XStoreName(pDisplay
, iWindow
, "xwinclip");
227 /* Select event types to watch */
228 if (XSelectInput(pDisplay
, iWindow
, PropertyChangeMask
) == BadWindow
)
229 ErrorF("winClipboardProc - XSelectInput generated BadWindow "
230 "on messaging window\n");
232 /* Save the window in the screen privates */
233 g_iClipboardWindow
= iWindow
;
235 /* Create Windows messaging window */
236 hwnd
= winClipboardCreateMessagingWindow();
238 /* Save copy of HWND in screen privates */
239 g_hwndClipboard
= hwnd
;
241 /* Assert ownership of selections if Win32 clipboard is owned */
242 if (NULL
!= GetClipboardOwner()) {
244 iReturn
= XSetSelectionOwner(pDisplay
, XA_PRIMARY
,
245 iWindow
, CurrentTime
);
246 if (iReturn
== BadAtom
|| iReturn
== BadWindow
||
247 XGetSelectionOwner(pDisplay
, XA_PRIMARY
) != iWindow
) {
248 ErrorF("winClipboardProc - Could not set PRIMARY owner\n");
249 goto winClipboardProc_Done
;
253 iReturn
= XSetSelectionOwner(pDisplay
, atomClipboard
,
254 iWindow
, CurrentTime
);
255 if (iReturn
== BadAtom
|| iReturn
== BadWindow
||
256 XGetSelectionOwner(pDisplay
, atomClipboard
) != iWindow
) {
257 ErrorF("winClipboardProc - Could not set CLIPBOARD owner\n");
258 goto winClipboardProc_Done
;
262 /* Pre-flush X events */
264 * NOTE: Apparently you'll freeze if you don't do this,
265 * because there may be events in local data structures
268 winClipboardFlushXEvents(hwnd
, iWindow
, pDisplay
, fUseUnicode
);
270 /* Pre-flush Windows messages */
271 if (!winClipboardFlushWindowsMessageQueue(hwnd
))
274 /* Signal that the clipboard client has started */
275 g_fClipboardStarted
= TRUE
;
277 /* Loop for X events */
279 /* Setup the file descriptor set */
281 * NOTE: You have to do this before every call to select
282 * because select modifies the mask to indicate
283 * which descriptors are ready.
286 FD_SET(iConnectionNumber
, &fdsRead
);
287 #ifdef HAS_DEVWINDOWS
288 FD_SET(fdMessageQueue
, &fdsRead
);
290 tvTimeout
.tv_sec
= 0;
291 tvTimeout
.tv_usec
= 100;
294 /* Wait for a Windows event or an X event */
295 iReturn
= select(iMaxDescriptor
, /* Highest fds number */
296 &fdsRead
, /* Read mask */
297 NULL
, /* No write mask */
298 NULL
, /* No exception mask */
299 #ifdef HAS_DEVWINDOWS
300 NULL
/* No timeout */
302 &tvTimeout
/* Set timeout */
307 iSelectError
= errno
;
309 iSelectError
= WSAGetLastError();
314 if (iSelectError
== EINTR
)
316 if (iSelectError
== WSAEINTR
)
320 ErrorF("winClipboardProc - Call to select () failed: %d. "
321 "Bailing.\n", iReturn
);
325 /* Branch on which descriptor became active */
326 if (FD_ISSET(iConnectionNumber
, &fdsRead
)) {
327 /* Process X events */
328 winClipboardFlushXEvents(hwnd
, iWindow
, pDisplay
, fUseUnicode
);
331 #ifdef HAS_DEVWINDOWS
332 /* Check for Windows event ready */
333 if (FD_ISSET(fdMessageQueue
, &fdsRead
))
338 /* Process Windows messages */
339 if (!winClipboardFlushWindowsMessageQueue(hwnd
)) {
340 ErrorF("winClipboardProc - "
341 "winClipboardFlushWindowsMessageQueue trapped "
342 "WM_QUIT message, exiting main loop.\n");
348 winClipboardProc_Exit
:
349 /* disable the clipboard, which means the thread will die */
350 g_fClipboard
= FALSE
;
352 winClipboardProc_Done
:
353 /* Close our Windows window */
354 if (g_hwndClipboard
) {
355 /* Destroy the Window window (hwnd) */
356 winDebug("winClipboardProc - Destroy Windows window\n");
357 PostMessage(g_hwndClipboard
, WM_DESTROY
, 0, 0);
358 winClipboardFlushWindowsMessageQueue(g_hwndClipboard
);
361 /* Close our X window */
362 if (pDisplay
&& iWindow
) {
363 iReturn
= XDestroyWindow(pDisplay
, iWindow
);
364 if (iReturn
== BadWindow
)
365 ErrorF("winClipboardProc - XDestroyWindow returned BadWindow.\n");
367 ErrorF("winClipboardProc - XDestroyWindow succeeded.\n");
370 #ifdef HAS_DEVWINDOWS
371 /* Close our Win32 message handle */
373 close(fdMessageQueue
);
378 * FIXME: XCloseDisplay hangs if we call it, as of 2004/03/26. The
379 * XSync and XSelectInput calls did not help.
382 /* Discard any remaining events */
383 XSync(pDisplay
, TRUE
);
385 /* Select event types to watch */
386 XSelectInput(pDisplay
, DefaultRootWindow(pDisplay
), None
);
388 /* Close our X display */
390 XCloseDisplay(pDisplay
);
394 /* global clipboard variable reset */
395 g_fClipboardLaunched
= FALSE
;
396 g_fClipboardStarted
= FALSE
;
397 g_iClipboardWindow
= None
;
398 g_pClipboardDisplay
= NULL
;
399 g_hwndClipboard
= NULL
;
401 /* checking if we need to restart */
402 if (clipboardRestarts
>= WIN_CLIPBOARD_RETRIES
) {
403 /* terminates clipboard thread but the main server still lives */
405 ("winClipboardProc - the clipboard thread has restarted %d times and seems to be unstable, disabling clipboard integration\n",
407 g_fClipboard
= FALSE
;
412 sleep(WIN_CLIPBOARD_DELAY
);
413 ErrorF("winClipboardProc - trying to restart clipboard thread \n");
414 /* Create the clipboard client thread */
415 if (!winInitClipboard()) {
416 ErrorF("winClipboardProc - winClipboardInit failed.\n");
420 winDebug("winClipboardProc - winInitClipboard returned.\n");
421 /* Flag that clipboard client has been launched */
422 g_fClipboardLaunched
= TRUE
;
425 ErrorF("winClipboardProc - Clipboard disabled - Exit from server \n");
426 /* clipboard thread has exited, stop server as well */
434 * winClipboardErrorHandler - Our application specific error handler
438 winClipboardErrorHandler(Display
* pDisplay
, XErrorEvent
* pErr
)
440 char pszErrorMsg
[100];
442 XGetErrorText(pDisplay
, pErr
->error_code
, pszErrorMsg
, sizeof(pszErrorMsg
));
443 ErrorF("winClipboardErrorHandler - ERROR: \n\t%s\n"
444 "\tSerial: %lu, Request Code: %d, Minor Code: %d\n",
445 pszErrorMsg
, pErr
->serial
, pErr
->request_code
, pErr
->minor_code
);
450 * winClipboardIOErrorHandler - Our application specific IO error handler
454 winClipboardIOErrorHandler(Display
* pDisplay
)
456 ErrorF("winClipboardIOErrorHandler!\n");
458 if (pthread_equal(pthread_self(), g_winClipboardProcThread
)) {
459 /* Restart at the main entry point */
460 longjmp(g_jmpEntry
, WIN_JMP_ERROR_IO
);
463 if (g_winClipboardOldIOErrorHandler
)
464 g_winClipboardOldIOErrorHandler(pDisplay
);