]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkasync.c
Use asynchronously _gdk_x11_set_input_focus_safe to avoid having to trap
[~andy/gtk] / gdk / x11 / gdkasync.c
1 /* GTK - The GIMP Toolkit
2  * gdkasync.c: Utility functions using the Xlib asynchronous interfaces
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20 /* Portions of code in this file are based on code from Xlib
21  */
22 /*
23 Copyright 1986, 1998  The Open Group
24
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
29 documentation.
30
31 The above copyright notice and this permission notice shall be included in
32 all copies or substantial portions of the Software.
33
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.
40
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.
44
45 */
46 #include <X11/Xlibint.h>
47 #include "gdkasync.h"
48 #include "gdkx.h"
49
50 typedef struct _SetInputFocusState SetInputFocusState;
51
52 struct _SetInputFocusState
53 {
54   Display *dpy;
55   Window window;
56   _XAsyncHandler async;
57   gulong set_input_focus_req;
58   gulong get_input_focus_req;
59   gboolean have_error;
60   GdkSendXEventCallback callback;
61   gpointer data;
62 };
63
64 static Bool
65 set_input_focus_handler (Display *dpy,
66                          xReply  *rep,
67                          char    *buf,
68                          int      len,
69                          XPointer data)
70 {
71   SetInputFocusState *state = (SetInputFocusState *)data;  
72
73   if (dpy->last_request_read == state->set_input_focus_req)
74     {
75       if (rep->generic.type == X_Error &&
76           rep->error.errorCode == BadMatch)
77         {
78           /* Consume BadMatch errors, since we have no control
79            * over them.
80            */
81           return True;
82         }
83     }
84   
85   if (dpy->last_request_read == state->get_input_focus_req)
86     {
87       xGetInputFocusReply replbuf;
88       xGetInputFocusReply *repl;
89       
90       if (rep->generic.type != X_Error)
91         {
92           /* Actually does nothing, since there are no additional bytes
93            * to read, but maintain good form.
94            */
95           repl = (xGetInputFocusReply *)
96             _XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
97                             (sizeof(xGetInputFocusReply) - sizeof(xReply)) >> 2,
98                             True);
99         }
100
101       DeqAsyncHandler(state->dpy, &state->async);
102
103       g_free (state);
104       
105       return (rep->generic.type != X_Error);
106     }
107
108   return False;
109 }
110
111 void
112 _gdk_x11_set_input_focus_safe (GdkDisplay             *display,
113                                Window                  window,
114                                int                     revert_to,
115                                Time                    time)
116 {
117   Display *dpy;
118   SetInputFocusState *state;
119   
120   dpy = GDK_DISPLAY_XDISPLAY (display);
121
122   state = g_new (SetInputFocusState, 1);
123
124   state->dpy = dpy;
125   state->window = window;
126   
127   LockDisplay(dpy);
128
129   state->async.next = dpy->async_handlers;
130   state->async.handler = set_input_focus_handler;
131   state->async.data = (XPointer) state;
132   dpy->async_handlers = &state->async;
133
134   {
135     xSetInputFocusReq *req;
136     
137     GetReq(SetInputFocus, req);
138     req->focus = window;
139     req->revertTo = revert_to;
140     req->time = time;
141     state->set_input_focus_req = dpy->request;
142   }
143
144   /*
145    * XSync (dpy, 0)
146    */
147   {
148     xReq *req;
149     
150     GetEmptyReq(GetInputFocus, req);
151     state->get_input_focus_req = dpy->request;
152   }
153   
154   UnlockDisplay(dpy);
155   SyncHandle();
156 }