3 * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation files
7 * (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to 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 THE ABOVE LISTED COPYRIGHT
20 * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Except as contained in this notice, the name(s) of the above
26 * copyright holders shall not be used in advertising or otherwise to
27 * promote the sale, use or other dealings in this Software without
28 * prior written authorization.
32 #import "x-selection.h"
36 #include <X11/extensions/applewm.h>
38 Display *xpbproxy_dpy;
39 int xpbproxy_apple_wm_event_base, xpbproxy_apple_wm_error_base;
40 int xpbproxy_xfixes_event_base, xpbproxy_xfixes_error_base;
41 BOOL xpbproxy_have_xfixes;
45 #ifdef STANDALONE_XPBPROXY
46 BOOL xpbproxy_is_standalone = NO;
49 x_selection *_selection_object;
51 extern BOOL serverRunning;
52 extern pthread_mutex_t serverRunningMutex;
53 extern pthread_cond_t serverRunningCond;
56 wait_for_server_init(void)
58 /* If the server hasn't finished initializing, wait for it... */
60 pthread_mutex_lock(&serverRunningMutex);
61 while (!serverRunning)
62 pthread_cond_wait(&serverRunningCond, &serverRunningMutex);
63 pthread_mutex_unlock(&serverRunningMutex);
68 x_io_error_handler(Display *dpy)
70 /* We lost our connection to the server. */
74 /* trigger the thread to restart?
75 * NO - this would be to a "deeper" problem, and restarts would just
76 * make things worse...
78 #ifdef STANDALONE_XPBPROXY
79 if (xpbproxy_is_standalone)
83 /* Prevent _XIOError from calling exit() */
89 x_error_handler(Display *dpy, XErrorEvent *errevent)
97 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
100 wait_for_server_init();
102 for (i = 0, xpbproxy_dpy = NULL; !xpbproxy_dpy && i < 5; i++) {
103 xpbproxy_dpy = XOpenDisplay(NULL);
105 if (!xpbproxy_dpy && display) {
107 snprintf(_display, sizeof(_display), ":%s", display);
108 setenv("DISPLAY", _display, TRUE);
110 xpbproxy_dpy = XOpenDisplay(_display);
116 if (xpbproxy_dpy == NULL) {
117 ErrorF("xpbproxy: can't open default display\n");
122 XSetIOErrorHandler(x_io_error_handler);
123 XSetErrorHandler(x_error_handler);
125 if (!XAppleWMQueryExtension(xpbproxy_dpy, &xpbproxy_apple_wm_event_base,
126 &xpbproxy_apple_wm_error_base)) {
127 ErrorF("xpbproxy: can't open AppleWM server extension\n");
132 xpbproxy_have_xfixes =
133 XFixesQueryExtension(xpbproxy_dpy, &xpbproxy_xfixes_event_base,
134 &xpbproxy_xfixes_error_base);
136 XAppleWMSelectInput(xpbproxy_dpy, AppleWMActivationNotifyMask |
137 AppleWMPasteboardNotifyMask);
139 _selection_object = [[x_selection alloc] init];
141 if (!xpbproxy_input_register()) {
154 xpbproxy_selection_object(void)
156 return _selection_object;
160 xpbproxy_current_timestamp(void)
162 /* FIXME: may want to fetch a timestamp from the server.. */