1 /* x-input.m -- event handling
3 * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
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.
32 #import "x-selection.h"
34 #include <CoreFoundation/CFSocket.h>
35 #include <CoreFoundation/CFRunLoop.h>
37 #include <X11/Xatom.h>
38 #include <X11/keysym.h>
39 #include <X11/extensions/applewm.h>
43 static CFRunLoopSourceRef xpbproxy_dpy_source;
45 #ifdef STANDALONE_XPBPROXY
46 BOOL xpbproxy_prefs_reload = NO;
49 /* Timestamp when the X server last told us it's active */
50 static Time last_activation_time;
53 x_event_apple_wm_notify(XAppleWMNotifyEvent *e)
55 int type = e->type - xpbproxy_apple_wm_event_base;
58 /* We want to reload prefs even if we're not active */
59 if (type == AppleWMActivationNotify &&
60 kind == AppleWMReloadPreferences)
61 [xpbproxy_selection_object ()reload_preferences];
63 if (![xpbproxy_selection_object ()is_active])
67 case AppleWMActivationNotify:
70 last_activation_time = e->time;
71 [xpbproxy_selection_object () x_active:e->time];
74 case AppleWMIsInactive:
75 [xpbproxy_selection_object () x_inactive:e->time];
80 case AppleWMPasteboardNotify:
82 case AppleWMCopyToPasteboard:
83 [xpbproxy_selection_object () x_copy:e->time];
90 xpbproxy_process_xevents(void)
92 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
95 ErrorF("unable to allocate/init auto release pool!\n");
99 while (XPending(xpbproxy_dpy) != 0) {
102 XNextEvent(xpbproxy_dpy, &e);
106 if ([xpbproxy_selection_object ()is_active])
107 [xpbproxy_selection_object () clear_event:&e.xselectionclear];
110 case SelectionRequest:
111 [xpbproxy_selection_object () request_event:&e.xselectionrequest];
114 case SelectionNotify:
115 [xpbproxy_selection_object () notify_event:&e.xselection];
119 [xpbproxy_selection_object () property_event:&e.xproperty];
123 if (e.type >= xpbproxy_apple_wm_event_base &&
124 e.type < xpbproxy_apple_wm_event_base +
125 AppleWMNumberEvents) {
126 x_event_apple_wm_notify((XAppleWMNotifyEvent *)&e);
128 else if (e.type == xpbproxy_xfixes_event_base +
129 XFixesSelectionNotify) {
130 [xpbproxy_selection_object () xfixes_selection_notify:(
131 XFixesSelectionNotifyEvent *)&e];
136 XFlush(xpbproxy_dpy);
143 add_input_socket(int sock, CFOptionFlags callback_types,
144 CFSocketCallBack callback, const CFSocketContext *ctx,
145 CFRunLoopSourceRef *cf_source)
149 cf_sock = CFSocketCreateWithNative(kCFAllocatorDefault, sock,
150 callback_types, callback, ctx);
151 if (cf_sock == NULL) {
156 *cf_source = CFSocketCreateRunLoopSource(kCFAllocatorDefault,
160 if (*cf_source == NULL)
163 CFRunLoopAddSource(CFRunLoopGetCurrent(),
164 *cf_source, kCFRunLoopDefaultMode);
169 x_input_callback(CFSocketRef sock, CFSocketCallBackType type,
170 CFDataRef address, const void *data, void *info)
173 #ifdef STANDALONE_XPBPROXY
174 if (xpbproxy_prefs_reload) {
175 [xpbproxy_selection_object ()reload_preferences];
176 xpbproxy_prefs_reload = NO;
180 xpbproxy_process_xevents();
184 xpbproxy_input_register(void)
186 return add_input_socket(ConnectionNumber(
187 xpbproxy_dpy), kCFSocketReadCallBack,
188 x_input_callback, NULL, &xpbproxy_dpy_source);