xref: /OK3568_Linux_fs/external/xserver/hw/xquartz/pbproxy/main.m (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun/* main.m
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person
6*4882a593Smuzhiyun * obtaining a copy of this software and associated documentation files
7*4882a593Smuzhiyun * (the "Software"), to deal in the Software without restriction,
8*4882a593Smuzhiyun * including without limitation the rights to use, copy, modify, merge,
9*4882a593Smuzhiyun * publish, distribute, sublicense, and/or sell copies of the Software,
10*4882a593Smuzhiyun * and to permit persons to whom the Software is furnished to do so,
11*4882a593Smuzhiyun * subject to the following conditions:
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be
14*4882a593Smuzhiyun * included in all copies or substantial portions of the Software.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*4882a593Smuzhiyun * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
20*4882a593Smuzhiyun * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21*4882a593Smuzhiyun * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*4882a593Smuzhiyun * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Except as contained in this notice, the name(s) of the above
26*4882a593Smuzhiyun * copyright holders shall not be used in advertising or otherwise to
27*4882a593Smuzhiyun * promote the sale, use or other dealings in this Software without
28*4882a593Smuzhiyun * prior written authorization.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun#include "pbproxy.h"
32*4882a593Smuzhiyun#import "x-selection.h"
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun#include <pthread.h>
35*4882a593Smuzhiyun#include <unistd.h>
36*4882a593Smuzhiyun#include <X11/extensions/applewm.h>
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunDisplay *xpbproxy_dpy;
39*4882a593Smuzhiyunint xpbproxy_apple_wm_event_base, xpbproxy_apple_wm_error_base;
40*4882a593Smuzhiyunint xpbproxy_xfixes_event_base, xpbproxy_xfixes_error_base;
41*4882a593SmuzhiyunBOOL xpbproxy_have_xfixes;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyunextern char *display;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun#ifdef STANDALONE_XPBPROXY
46*4882a593SmuzhiyunBOOL xpbproxy_is_standalone = NO;
47*4882a593Smuzhiyun#endif
48*4882a593Smuzhiyun
49*4882a593Smuzhiyunx_selection *_selection_object;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyunstatic int
52*4882a593Smuzhiyunx_io_error_handler(Display *dpy)
53*4882a593Smuzhiyun{
54*4882a593Smuzhiyun    /* We lost our connection to the server. */
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun    TRACE();
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun    /* trigger the thread to restart?
59*4882a593Smuzhiyun     *   NO - this would be to a "deeper" problem, and restarts would just
60*4882a593Smuzhiyun     *        make things worse...
61*4882a593Smuzhiyun     */
62*4882a593Smuzhiyun#ifdef STANDALONE_XPBPROXY
63*4882a593Smuzhiyun    if (xpbproxy_is_standalone)
64*4882a593Smuzhiyun        exit(EXIT_FAILURE);
65*4882a593Smuzhiyun#endif
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun    /* Prevent _XIOError from calling exit() */
68*4882a593Smuzhiyun    pthread_exit(NULL);
69*4882a593Smuzhiyun    return 0;
70*4882a593Smuzhiyun}
71*4882a593Smuzhiyun
72*4882a593Smuzhiyunstatic int
73*4882a593Smuzhiyunx_error_handler(Display *dpy, XErrorEvent *errevent)
74*4882a593Smuzhiyun{
75*4882a593Smuzhiyun    return 0;
76*4882a593Smuzhiyun}
77*4882a593Smuzhiyun
78*4882a593Smuzhiyunint
79*4882a593Smuzhiyunxpbproxy_run(void)
80*4882a593Smuzhiyun{
81*4882a593Smuzhiyun    @autoreleasepool {
82*4882a593Smuzhiyun        size_t i;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun        for (i = 0, xpbproxy_dpy = NULL; !xpbproxy_dpy && i < 5; i++) {
85*4882a593Smuzhiyun            xpbproxy_dpy = XOpenDisplay(NULL);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun            if (!xpbproxy_dpy && display) {
88*4882a593Smuzhiyun                char _display[32];
89*4882a593Smuzhiyun                snprintf(_display, sizeof(_display), ":%s", display);
90*4882a593Smuzhiyun                setenv("DISPLAY", _display, TRUE);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun                xpbproxy_dpy = XOpenDisplay(_display);
93*4882a593Smuzhiyun            }
94*4882a593Smuzhiyun            if (!xpbproxy_dpy)
95*4882a593Smuzhiyun                sleep(1);
96*4882a593Smuzhiyun        }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun        if (xpbproxy_dpy == NULL) {
99*4882a593Smuzhiyun            ErrorF("xpbproxy: can't open default display\n");
100*4882a593Smuzhiyun            return EXIT_FAILURE;
101*4882a593Smuzhiyun        }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun        XSetIOErrorHandler(x_io_error_handler);
104*4882a593Smuzhiyun        XSetErrorHandler(x_error_handler);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun        if (!XAppleWMQueryExtension(xpbproxy_dpy, &xpbproxy_apple_wm_event_base,
107*4882a593Smuzhiyun                                    &xpbproxy_apple_wm_error_base)) {
108*4882a593Smuzhiyun            ErrorF("xpbproxy: can't open AppleWM server extension\n");
109*4882a593Smuzhiyun            return EXIT_FAILURE;
110*4882a593Smuzhiyun        }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun        xpbproxy_have_xfixes = XFixesQueryExtension(xpbproxy_dpy, &xpbproxy_xfixes_event_base,
113*4882a593Smuzhiyun                                                    &xpbproxy_xfixes_error_base);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun        XAppleWMSelectInput(xpbproxy_dpy, AppleWMActivationNotifyMask | AppleWMPasteboardNotifyMask);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun        _selection_object = [x_selection new];
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun        if (!xpbproxy_input_register()) {
120*4882a593Smuzhiyun            return EXIT_FAILURE;
121*4882a593Smuzhiyun        }
122*4882a593Smuzhiyun    }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun    CFRunLoopRun();
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun    return EXIT_SUCCESS;
127*4882a593Smuzhiyun}
128*4882a593Smuzhiyun
129*4882a593Smuzhiyunid
130*4882a593Smuzhiyunxpbproxy_selection_object(void)
131*4882a593Smuzhiyun{
132*4882a593Smuzhiyun    return _selection_object;
133*4882a593Smuzhiyun}
134*4882a593Smuzhiyun
135*4882a593SmuzhiyunTime
136*4882a593Smuzhiyunxpbproxy_current_timestamp(void)
137*4882a593Smuzhiyun{
138*4882a593Smuzhiyun    /* FIXME: may want to fetch a timestamp from the server.. */
139*4882a593Smuzhiyun    return CurrentTime;
140*4882a593Smuzhiyun}
141