main.m revision 1b5d61b8
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    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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        [pool release];
101        return EXIT_FAILURE;
102    }
103
104    XSetIOErrorHandler(x_io_error_handler);
105    XSetErrorHandler(x_error_handler);
106
107    if (!XAppleWMQueryExtension(xpbproxy_dpy, &xpbproxy_apple_wm_event_base,
108                                &xpbproxy_apple_wm_error_base)) {
109        ErrorF("xpbproxy: can't open AppleWM server extension\n");
110        [pool release];
111        return EXIT_FAILURE;
112    }
113
114    xpbproxy_have_xfixes =
115        XFixesQueryExtension(xpbproxy_dpy, &xpbproxy_xfixes_event_base,
116                             &xpbproxy_xfixes_error_base);
117
118    XAppleWMSelectInput(xpbproxy_dpy, AppleWMActivationNotifyMask |
119                        AppleWMPasteboardNotifyMask);
120
121    _selection_object = [[x_selection alloc] init];
122
123    if (!xpbproxy_input_register()) {
124        [pool release];
125        return EXIT_FAILURE;
126    }
127
128    [pool release];
129
130    CFRunLoopRun();
131
132    return EXIT_SUCCESS;
133}
134
135id
136xpbproxy_selection_object(void)
137{
138    return _selection_object;
139}
140
141Time
142xpbproxy_current_timestamp(void)
143{
144    /* FIXME: may want to fetch a timestamp from the server.. */
145    return CurrentTime;
146}
147