1*4882a593Smuzhiyun/* app-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> /*for getpid*/ 36*4882a593Smuzhiyun#include <Cocoa/Cocoa.h> 37*4882a593Smuzhiyun 38*4882a593Smuzhiyunstatic const char *app_prefs_domain = BUNDLE_ID_PREFIX ".xpbproxy"; 39*4882a593SmuzhiyunCFStringRef app_prefs_domain_cfstr; 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun/* Stubs */ 42*4882a593Smuzhiyunchar *display = NULL; 43*4882a593Smuzhiyun 44*4882a593Smuzhiyunstatic void 45*4882a593Smuzhiyunsignal_handler(int sig) 46*4882a593Smuzhiyun{ 47*4882a593Smuzhiyun switch (sig) { 48*4882a593Smuzhiyun case SIGHUP: 49*4882a593Smuzhiyun xpbproxy_prefs_reload = YES; 50*4882a593Smuzhiyun break; 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun default: 53*4882a593Smuzhiyun _exit(EXIT_SUCCESS); 54*4882a593Smuzhiyun } 55*4882a593Smuzhiyun} 56*4882a593Smuzhiyun 57*4882a593Smuzhiyunvoid 58*4882a593SmuzhiyunErrorF(const char * f, ...) 59*4882a593Smuzhiyun{ 60*4882a593Smuzhiyun va_list args; 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun va_start(args, f); 63*4882a593Smuzhiyun vfprintf(stderr, f, args); 64*4882a593Smuzhiyun va_end(args); 65*4882a593Smuzhiyun} 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun/* TODO: Have this actually log to ASL */ 68*4882a593Smuzhiyunvoid 69*4882a593Smuzhiyunxq_asl_log(int level, const char *subsystem, const char *file, 70*4882a593Smuzhiyun const char *function, int line, const char *fmt, 71*4882a593Smuzhiyun ...) 72*4882a593Smuzhiyun{ 73*4882a593Smuzhiyun#ifdef DEBUG 74*4882a593Smuzhiyun va_list args; 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun va_start(args, fmt); 77*4882a593Smuzhiyun vfprintf(stderr, fmt, args); 78*4882a593Smuzhiyun va_end(args); 79*4882a593Smuzhiyun#endif 80*4882a593Smuzhiyun} 81*4882a593Smuzhiyun 82*4882a593Smuzhiyunint 83*4882a593Smuzhiyunmain(int argc, const char *argv[]) 84*4882a593Smuzhiyun{ 85*4882a593Smuzhiyun const char *s; 86*4882a593Smuzhiyun int i; 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun#ifdef DEBUG 89*4882a593Smuzhiyun ErrorF("pid: %u\n", getpid()); 90*4882a593Smuzhiyun#endif 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun xpbproxy_is_standalone = YES; 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun if ((s = getenv("X11_PREFS_DOMAIN"))) 95*4882a593Smuzhiyun app_prefs_domain = s; 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun for (i = 1; i < argc; i++) { 98*4882a593Smuzhiyun if (strcmp(argv[i], "--prefs-domain") == 0 && i + 1 < argc) { 99*4882a593Smuzhiyun app_prefs_domain = argv[++i]; 100*4882a593Smuzhiyun } 101*4882a593Smuzhiyun else if (strcmp(argv[i], "--help") == 0) { 102*4882a593Smuzhiyun ErrorF( 103*4882a593Smuzhiyun "usage: xpbproxy OPTIONS\n" 104*4882a593Smuzhiyun "Pasteboard proxying for X11.\n\n" 105*4882a593Smuzhiyun "--prefs-domain <domain> Change the domain used for reading preferences\n" 106*4882a593Smuzhiyun " (default: %s)\n", 107*4882a593Smuzhiyun app_prefs_domain); 108*4882a593Smuzhiyun return 0; 109*4882a593Smuzhiyun } 110*4882a593Smuzhiyun else { 111*4882a593Smuzhiyun ErrorF("usage: xpbproxy OPTIONS...\n" 112*4882a593Smuzhiyun "Try 'xpbproxy --help' for more information.\n"); 113*4882a593Smuzhiyun return 1; 114*4882a593Smuzhiyun } 115*4882a593Smuzhiyun } 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun app_prefs_domain_cfstr = CFStringCreateWithCString(NULL, app_prefs_domain, 118*4882a593Smuzhiyun kCFStringEncodingUTF8); 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun signal(SIGINT, signal_handler); 121*4882a593Smuzhiyun signal(SIGTERM, signal_handler); 122*4882a593Smuzhiyun signal(SIGHUP, signal_handler); 123*4882a593Smuzhiyun signal(SIGPIPE, SIG_IGN); 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun return xpbproxy_run(); 126*4882a593Smuzhiyun} 127