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 38Display *xpbproxy_dpy; 39int xpbproxy_apple_wm_event_base, xpbproxy_apple_wm_error_base; 40int xpbproxy_xfixes_event_base, xpbproxy_xfixes_error_base; 41BOOL xpbproxy_have_xfixes; 42 43extern char *display; 44 45#ifdef STANDALONE_XPBPROXY 46BOOL xpbproxy_is_standalone = NO; 47#endif 48 49x_selection *_selection_object; 50 51static int 52x_io_error_handler(Display *dpy) 53{ 54 /* We lost our connection to the server. */ 55 56 TRACE(); 57 58 /* trigger the thread to restart? 59 * NO - this would be to a "deeper" problem, and restarts would just 60 * make things worse... 61 */ 62#ifdef STANDALONE_XPBPROXY 63 if (xpbproxy_is_standalone) 64 exit(EXIT_FAILURE); 65#endif 66 67 /* Prevent _XIOError from calling exit() */ 68 pthread_exit(NULL); 69 return 0; 70} 71 72static int 73x_error_handler(Display *dpy, XErrorEvent *errevent) 74{ 75 return 0; 76} 77 78int 79xpbproxy_run(void) 80{ 81 @autoreleasepool { 82 size_t i; 83 84 for (i = 0, xpbproxy_dpy = NULL; !xpbproxy_dpy && i < 5; i++) { 85 xpbproxy_dpy = XOpenDisplay(NULL); 86 87 if (!xpbproxy_dpy && display) { 88 char _display[32]; 89 snprintf(_display, sizeof(_display), ":%s", display); 90 setenv("DISPLAY", _display, TRUE); 91 92 xpbproxy_dpy = XOpenDisplay(_display); 93 } 94 if (!xpbproxy_dpy) 95 sleep(1); 96 } 97 98 if (xpbproxy_dpy == NULL) { 99 ErrorF("xpbproxy: can't open default display\n"); 100 return EXIT_FAILURE; 101 } 102 103 XSetIOErrorHandler(x_io_error_handler); 104 XSetErrorHandler(x_error_handler); 105 106 if (!XAppleWMQueryExtension(xpbproxy_dpy, &xpbproxy_apple_wm_event_base, 107 &xpbproxy_apple_wm_error_base)) { 108 ErrorF("xpbproxy: can't open AppleWM server extension\n"); 109 return EXIT_FAILURE; 110 } 111 112 xpbproxy_have_xfixes = XFixesQueryExtension(xpbproxy_dpy, &xpbproxy_xfixes_event_base, 113 &xpbproxy_xfixes_error_base); 114 115 XAppleWMSelectInput(xpbproxy_dpy, AppleWMActivationNotifyMask | AppleWMPasteboardNotifyMask); 116 117 _selection_object = [x_selection new]; 118 119 if (!xpbproxy_input_register()) { 120 return EXIT_FAILURE; 121 } 122 } 123 124 CFRunLoopRun(); 125 126 return EXIT_SUCCESS; 127} 128 129id 130xpbproxy_selection_object(void) 131{ 132 return _selection_object; 133} 134 135Time 136xpbproxy_current_timestamp(void) 137{ 138 /* FIXME: may want to fetch a timestamp from the server.. */ 139 return CurrentTime; 140} 141