| 1 | /* main.m |
| 2 | * |
| 3 | * Copyright (c) 2002-2012 Apple Inc. All rights reserved. |
| 4 | * |
| 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: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be |
| 14 | * included in all copies or substantial portions of the Software. |
| 15 | * |
| 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. |
| 24 | * |
| 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. |
| 29 | */ |
| 30 | |
| 31 | #include "pbproxy.h" |
| 32 | #import "x-selection.h" |
| 33 | |
| 34 | #include <pthread.h> |
| 35 | #include <unistd.h> |
| 36 | #include <X11/extensions/applewm.h> |
| 37 | |
| 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; |
| 42 | |
| 43 | extern char *display; |
| 44 | |
| 45 | #ifdef STANDALONE_XPBPROXY |
| 46 | BOOL xpbproxy_is_standalone = NO; |
| 47 | #endif |
| 48 | |
| 49 | x_selection *_selection_object; |
| 50 | |
| 51 | extern BOOL serverRunning; |
| 52 | extern pthread_mutex_t serverRunningMutex; |
| 53 | extern pthread_cond_t serverRunningCond; |
| 54 | |
| 55 | static inline void |
| 56 | wait_for_server_init(void) |
| 57 | { |
| 58 | /* If the server hasn't finished initializing, wait for it... */ |
| 59 | if (!serverRunning) { |
| 60 | pthread_mutex_lock(&serverRunningMutex); |
| 61 | while (!serverRunning) |
| 62 | pthread_cond_wait(&serverRunningCond, &serverRunningMutex); |
| 63 | pthread_mutex_unlock(&serverRunningMutex); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | x_io_error_handler(Display *dpy) |
| 69 | { |
| 70 | /* We lost our connection to the server. */ |
| 71 | |
| 72 | TRACE(); |
| 73 | |
| 74 | /* trigger the thread to restart? |
| 75 | * NO - this would be to a "deeper" problem, and restarts would just |
| 76 | * make things worse... |
| 77 | */ |
| 78 | #ifdef STANDALONE_XPBPROXY |
| 79 | if (xpbproxy_is_standalone) |
| 80 | exit(EXIT_FAILURE); |
| 81 | #endif |
| 82 | |
| 83 | /* Prevent _XIOError from calling exit() */ |
| 84 | pthread_exit(NULL); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int |
| 89 | x_error_handler(Display *dpy, XErrorEvent *errevent) |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | xpbproxy_run(void) |
| 96 | { |
| 97 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 98 | size_t i; |
| 99 | |
| 100 | wait_for_server_init(); |
| 101 | |
| 102 | for (i = 0, xpbproxy_dpy = NULL; !xpbproxy_dpy && i < 5; i++) { |
| 103 | xpbproxy_dpy = XOpenDisplay(NULL); |
| 104 | |
| 105 | if (!xpbproxy_dpy && display) { |
| 106 | char _display[32]; |
| 107 | snprintf(_display, sizeof(_display), ":%s", display); |
| 108 | setenv("DISPLAY", _display, TRUE); |
| 109 | |
| 110 | xpbproxy_dpy = XOpenDisplay(_display); |
| 111 | } |
| 112 | if (!xpbproxy_dpy) |
| 113 | sleep(1); |
| 114 | } |
| 115 | |
| 116 | if (xpbproxy_dpy == NULL) { |
| 117 | ErrorF("xpbproxy: can't open default display\n"); |
| 118 | [pool release]; |
| 119 | return EXIT_FAILURE; |
| 120 | } |
| 121 | |
| 122 | XSetIOErrorHandler(x_io_error_handler); |
| 123 | XSetErrorHandler(x_error_handler); |
| 124 | |
| 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"); |
| 128 | [pool release]; |
| 129 | return EXIT_FAILURE; |
| 130 | } |
| 131 | |
| 132 | xpbproxy_have_xfixes = |
| 133 | XFixesQueryExtension(xpbproxy_dpy, &xpbproxy_xfixes_event_base, |
| 134 | &xpbproxy_xfixes_error_base); |
| 135 | |
| 136 | XAppleWMSelectInput(xpbproxy_dpy, AppleWMActivationNotifyMask | |
| 137 | AppleWMPasteboardNotifyMask); |
| 138 | |
| 139 | _selection_object = [[x_selection alloc] init]; |
| 140 | |
| 141 | if (!xpbproxy_input_register()) { |
| 142 | [pool release]; |
| 143 | return EXIT_FAILURE; |
| 144 | } |
| 145 | |
| 146 | [pool release]; |
| 147 | |
| 148 | CFRunLoopRun(); |
| 149 | |
| 150 | return EXIT_SUCCESS; |
| 151 | } |
| 152 | |
| 153 | id |
| 154 | xpbproxy_selection_object(void) |
| 155 | { |
| 156 | return _selection_object; |
| 157 | } |
| 158 | |
| 159 | Time |
| 160 | xpbproxy_current_timestamp(void) |
| 161 | { |
| 162 | /* FIXME: may want to fetch a timestamp from the server.. */ |
| 163 | return CurrentTime; |
| 164 | } |