1 /* GTK - The GIMP Toolkit
2 * gdkasync.c: Utility functions using the Xlib asynchronous interfaces
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 /* Portions of code in this file are based on code from Xlib
23 Copyright 1986, 1998 The Open Group
25 Permission to use, copy, modify, distribute, and sell this software and its
26 documentation for any purpose is hereby granted without fee, provided that
27 the above copyright notice appear in all copies and that both that
28 copyright notice and this permission notice appear in supporting
31 The above copyright notice and this permission notice shall be included in
32 all copies or substantial portions of the Software.
34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
38 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 Except as contained in this notice, the name of The Open Group shall not be
42 used in advertising or otherwise to promote the sale, use or other dealings
43 in this Software without prior written authorization from The Open Group.
46 #include <X11/Xlibint.h>
50 typedef struct _SendEventState SendEventState;
51 typedef struct _SetInputFocusState SetInputFocusState;
53 struct _SendEventState
58 gulong send_event_req;
59 gulong get_input_focus_req;
61 GdkSendXEventCallback callback;
65 struct _SetInputFocusState
69 gulong set_input_focus_req;
70 gulong get_input_focus_req;
74 send_event_handler (Display *dpy,
80 SendEventState *state = (SendEventState *)data;
82 if (dpy->last_request_read == state->send_event_req)
84 if (rep->generic.type == X_Error)
86 if (rep->error.errorCode == BadWindow)
88 state->have_error = TRUE;
93 else if (dpy->last_request_read == state->get_input_focus_req)
95 xGetInputFocusReply replbuf;
96 xGetInputFocusReply *repl;
98 if (rep->generic.type != X_Error)
100 repl = (xGetInputFocusReply *)
101 _XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
102 (sizeof(xGetInputFocusReply) - sizeof(xReply)) >> 2,
107 state->callback (state->window, !state->have_error, state->data);
109 DeqAsyncHandler(state->dpy, &state->async);
120 _gdk_send_xevent_async (GdkDisplay *display,
125 GdkSendXEventCallback callback,
129 SendEventState *state;
132 dpy = GDK_DISPLAY_XDISPLAY (display);
134 state = g_new (SendEventState, 1);
137 state->window = window;
138 state->callback = callback;
140 state->have_error = FALSE;
144 state->async.next = dpy->async_handlers;
145 state->async.handler = send_event_handler;
146 state->async.data = (XPointer) state;
147 dpy->async_handlers = &state->async;
150 register xSendEventReq *req;
152 register Status (**fp)();
154 /* call through display to find proper conversion routine */
156 fp = &dpy->wire_vec[event_send->type & 0177];
157 if (*fp == NULL) *fp = _XEventToWire;
158 status = (**fp)(dpy, event_send, &ev);
162 g_warning ("Error converting event to wire");
163 DeqAsyncHandler(dpy, &state->async);
171 GetReq(SendEvent, req);
172 req->destination = window;
173 req->propagate = propagate;
174 req->eventMask = event_mask;
175 /* gross, matches Xproto.h */
177 memcpy ((char *) req->eventdata, (char *) &ev, SIZEOF(xEvent));
179 memcpy ((char *) &req->event, (char *) &ev, SIZEOF(xEvent));
182 state->send_event_req = dpy->request;
191 GetEmptyReq(GetInputFocus, req);
192 state->get_input_focus_req = dpy->request;
200 set_input_focus_handler (Display *dpy,
206 SetInputFocusState *state = (SetInputFocusState *)data;
208 if (dpy->last_request_read == state->set_input_focus_req)
210 if (rep->generic.type == X_Error &&
211 rep->error.errorCode == BadMatch)
213 /* Consume BadMatch errors, since we have no control
220 if (dpy->last_request_read == state->get_input_focus_req)
222 xGetInputFocusReply replbuf;
223 xGetInputFocusReply *repl;
225 if (rep->generic.type != X_Error)
227 /* Actually does nothing, since there are no additional bytes
228 * to read, but maintain good form.
230 repl = (xGetInputFocusReply *)
231 _XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
232 (sizeof(xGetInputFocusReply) - sizeof(xReply)) >> 2,
236 DeqAsyncHandler(state->dpy, &state->async);
240 return (rep->generic.type != X_Error);
247 _gdk_x11_set_input_focus_safe (GdkDisplay *display,
253 SetInputFocusState *state;
255 dpy = GDK_DISPLAY_XDISPLAY (display);
257 state = g_new (SetInputFocusState, 1);
263 state->async.next = dpy->async_handlers;
264 state->async.handler = set_input_focus_handler;
265 state->async.data = (XPointer) state;
266 dpy->async_handlers = &state->async;
269 xSetInputFocusReq *req;
271 GetReq(SetInputFocus, req);
273 req->revertTo = revert_to;
275 state->set_input_focus_req = dpy->request;
284 GetEmptyReq(GetInputFocus, req);
285 state->get_input_focus_req = dpy->request;