1/* x-input.m -- event handling 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 <CoreFoundation/CFSocket.h> 34#include <CoreFoundation/CFRunLoop.h> 35 36#include <X11/Xatom.h> 37#include <X11/keysym.h> 38#include <X11/extensions/applewm.h> 39 40#include <unistd.h> 41 42static CFRunLoopSourceRef xpbproxy_dpy_source; 43 44#ifdef STANDALONE_XPBPROXY 45BOOL xpbproxy_prefs_reload = NO; 46#endif 47 48/* Timestamp when the X server last told us it's active */ 49static Time last_activation_time; 50 51static void x_event_apple_wm_notify(XAppleWMNotifyEvent *e) { 52 int type = e->type - xpbproxy_apple_wm_event_base; 53 int kind = e->kind; 54 55 /* We want to reload prefs even if we're not active */ 56 if(type == AppleWMActivationNotify && 57 kind == AppleWMReloadPreferences) 58 [xpbproxy_selection_object() reload_preferences]; 59 60 if(![xpbproxy_selection_object() is_active]) 61 return; 62 63 switch (type) { 64 case AppleWMActivationNotify: 65 switch (kind) { 66 case AppleWMIsActive: 67 last_activation_time = e->time; 68 [xpbproxy_selection_object() x_active:e->time]; 69 break; 70 71 case AppleWMIsInactive: 72 [xpbproxy_selection_object() x_inactive:e->time]; 73 break; 74 } 75 break; 76 77 case AppleWMPasteboardNotify: 78 switch (kind) { 79 case AppleWMCopyToPasteboard: 80 [xpbproxy_selection_object() x_copy:e->time]; 81 } 82 break; 83 } 84} 85 86static void xpbproxy_process_xevents(void) { 87 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 88 89 if(pool == nil) { 90 fprintf(stderr, "unable to allocate/init auto release pool!\n"); 91 return; 92 } 93 94 while (XPending(xpbproxy_dpy) != 0) { 95 XEvent e; 96 97 XNextEvent (xpbproxy_dpy, &e); 98 99 switch (e.type) { 100 case SelectionClear: 101 if([xpbproxy_selection_object() is_active]) 102 [xpbproxy_selection_object () clear_event:&e.xselectionclear]; 103 break; 104 105 case SelectionRequest: 106 [xpbproxy_selection_object () request_event:&e.xselectionrequest]; 107 break; 108 109 case SelectionNotify: 110 [xpbproxy_selection_object () notify_event:&e.xselection]; 111 break; 112 113 case PropertyNotify: 114 [xpbproxy_selection_object () property_event:&e.xproperty]; 115 break; 116 117 default: 118 if(e.type >= xpbproxy_apple_wm_event_base && 119 e.type < xpbproxy_apple_wm_event_base + AppleWMNumberEvents) { 120 x_event_apple_wm_notify((XAppleWMNotifyEvent *) &e); 121 } else if(e.type == xpbproxy_xfixes_event_base + XFixesSelectionNotify) { 122 [xpbproxy_selection_object() xfixes_selection_notify:(XFixesSelectionNotifyEvent *)&e]; 123 } 124 break; 125 } 126 127 XFlush(xpbproxy_dpy); 128 } 129 130 [pool release]; 131} 132 133static BOOL add_input_socket (int sock, CFOptionFlags callback_types, 134 CFSocketCallBack callback, const CFSocketContext *ctx, 135 CFRunLoopSourceRef *cf_source) { 136 CFSocketRef cf_sock; 137 138 cf_sock = CFSocketCreateWithNative (kCFAllocatorDefault, sock, 139 callback_types, callback, ctx); 140 if (cf_sock == NULL) { 141 close (sock); 142 return FALSE; 143 } 144 145 *cf_source = CFSocketCreateRunLoopSource (kCFAllocatorDefault, 146 cf_sock, 0); 147 CFRelease (cf_sock); 148 149 if (*cf_source == NULL) 150 return FALSE; 151 152 CFRunLoopAddSource (CFRunLoopGetCurrent (), 153 *cf_source, kCFRunLoopDefaultMode); 154 return TRUE; 155} 156 157static void x_input_callback (CFSocketRef sock, CFSocketCallBackType type, 158 CFDataRef address, const void *data, void *info) { 159 160#ifdef STANDALONE_XPBPROXY 161 if(xpbproxy_prefs_reload) { 162 [xpbproxy_selection_object() reload_preferences]; 163 xpbproxy_prefs_reload = NO; 164 } 165#endif 166 167 xpbproxy_process_xevents(); 168} 169 170BOOL xpbproxy_input_register(void) { 171 return add_input_socket(ConnectionNumber(xpbproxy_dpy), kCFSocketReadCallBack, 172 x_input_callback, NULL, &xpbproxy_dpy_source); 173} 174