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