1706f2543Smrg/* app-main.m
2706f2543Smrg   Copyright (c) 2002, 2008 Apple Computer, Inc. All rights reserved.
3706f2543Smrg
4706f2543Smrg   Permission is hereby granted, free of charge, to any person
5706f2543Smrg   obtaining a copy of this software and associated documentation files
6706f2543Smrg   (the "Software"), to deal in the Software without restriction,
7706f2543Smrg   including without limitation the rights to use, copy, modify, merge,
8706f2543Smrg   publish, distribute, sublicense, and/or sell copies of the Software,
9706f2543Smrg   and to permit persons to whom the Software is furnished to do so,
10706f2543Smrg   subject to the following conditions:
11706f2543Smrg
12706f2543Smrg   The above copyright notice and this permission notice shall be
13706f2543Smrg   included in all copies or substantial portions of the Software.
14706f2543Smrg
15706f2543Smrg   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16706f2543Smrg   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17706f2543Smrg   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18706f2543Smrg   NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19706f2543Smrg   HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20706f2543Smrg   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21706f2543Smrg   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22706f2543Smrg   DEALINGS IN THE SOFTWARE.
23706f2543Smrg
24706f2543Smrg   Except as contained in this notice, the name(s) of the above
25706f2543Smrg   copyright holders shall not be used in advertising or otherwise to
26706f2543Smrg   promote the sale, use or other dealings in this Software without
27706f2543Smrg   prior written authorization.
28706f2543Smrg */
29706f2543Smrg
30706f2543Smrg#include "pbproxy.h"
31706f2543Smrg#import "x-selection.h"
32706f2543Smrg
33706f2543Smrg#include <pthread.h>
34706f2543Smrg#include <unistd.h> /*for getpid*/
35706f2543Smrg#include <Cocoa/Cocoa.h>
36706f2543Smrg
37706f2543Smrgstatic const char *app_prefs_domain = 	LAUNCHD_ID_PREFIX".xpbproxy";
38706f2543SmrgCFStringRef app_prefs_domain_cfstr;
39706f2543Smrg
40706f2543Smrg/* Stubs */
41706f2543Smrgchar *display = NULL;
42706f2543SmrgBOOL serverRunning = YES;
43706f2543Smrgpthread_mutex_t serverRunningMutex = PTHREAD_MUTEX_INITIALIZER;
44706f2543Smrgpthread_cond_t serverRunningCond = PTHREAD_COND_INITIALIZER;
45706f2543Smrg
46706f2543Smrgstatic void signal_handler (int sig) {
47706f2543Smrg    switch(sig) {
48706f2543Smrg        case SIGHUP:
49706f2543Smrg            xpbproxy_prefs_reload = YES;
50706f2543Smrg            break;
51706f2543Smrg        default:
52706f2543Smrg            _exit(EXIT_SUCCESS);
53706f2543Smrg    }
54706f2543Smrg}
55706f2543Smrg
56706f2543Smrgint main (int argc, const char *argv[]) {
57706f2543Smrg    const char *s;
58706f2543Smrg    int i;
59706f2543Smrg
60706f2543Smrg#ifdef DEBUG
61706f2543Smrg    printf("pid: %u\n", getpid());
62706f2543Smrg#endif
63706f2543Smrg
64706f2543Smrg    xpbproxy_is_standalone = YES;
65706f2543Smrg
66706f2543Smrg    if((s = getenv("X11_PREFS_DOMAIN")))
67706f2543Smrg        app_prefs_domain = s;
68706f2543Smrg
69706f2543Smrg    for (i = 1; i < argc; i++) {
70706f2543Smrg        if(strcmp (argv[i], "--prefs-domain") == 0 && i+1 < argc) {
71706f2543Smrg            app_prefs_domain = argv[++i];
72706f2543Smrg        } else if (strcmp (argv[i], "--help") == 0) {
73706f2543Smrg            printf("usage: xpbproxy OPTIONS\n"
74706f2543Smrg                   "Pasteboard proxying for X11.\n\n"
75706f2543Smrg                   "--prefs-domain <domain>   Change the domain used for reading preferences\n"
76706f2543Smrg                   "                          (default: %s)\n", app_prefs_domain);
77706f2543Smrg            return 0;
78706f2543Smrg        } else {
79706f2543Smrg            fprintf(stderr, "usage: xpbproxy OPTIONS...\n"
80706f2543Smrg                    "Try 'xpbproxy --help' for more information.\n");
81706f2543Smrg            return 1;
82706f2543Smrg        }
83706f2543Smrg    }
84706f2543Smrg    
85706f2543Smrg    app_prefs_domain_cfstr = CFStringCreateWithCString(NULL, app_prefs_domain, kCFStringEncodingUTF8);
86706f2543Smrg    
87706f2543Smrg    signal (SIGINT, signal_handler);
88706f2543Smrg    signal (SIGTERM, signal_handler);
89706f2543Smrg    signal (SIGHUP, signal_handler);
90706f2543Smrg    signal (SIGPIPE, SIG_IGN);
91706f2543Smrg
92706f2543Smrg    return xpbproxy_run();
93706f2543Smrg}
94