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