1 /* Copyright (c) 1990, 2015, Oracle and/or its affiliates. All rights reserved.
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice (including the next
11 * paragraph) shall be included in all copies or substantial portions of the
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
24 *-----------------------------------------------------------------------
25 * XGrabWin.c - X11 Client Side interface to Window Grabber.
27 * This code uses the standard R3 extension mechanism for sending the grab or
28 * ungrab window requests. If the extension isn't present, it uses the un-used
29 * protocol if the server is from Sun.
31 * The global state is only relevant to one display. Multiple displays
32 * will have to be implemented via arrays of global data.
33 *-----------------------------------------------------------------------
41 #include <X11/Xlibint.h> /* from usr.lib/libX11 */
42 #include <X11/Xproto.h> /* from usr.lib/libX11/include */
43 #include <X11/extensions/dgast.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <sys/types.h>
49 #define X_GrabWindow 125 /* should be in Xproto.h */
50 #define X_UnGrabWindow 126 /* just before X_NoOperation */
54 static int X_WxExtensionCode
;
56 static int WxError (Display
*dpy
,int mc
);
63 } WxInitialized
= NOT_INITIALIZED
;
66 Initialize(Display
*dpy
)
70 if (dpy
->display_name
[0] != ':') {
73 gethostname(hostname
,64);
74 if (strncmp("unix",dpy
->display_name
,4) &&
75 strncmp("localhost",dpy
->display_name
,9) &&
76 strncmp(hostname
,dpy
->display_name
,strlen(hostname
))) {
77 WxInitialized
= NOT_LOCAL_HOST
;
82 if (XQueryExtension(dpy
, "SunWindowGrabber",&X_WxExtensionCode
,
84 WxInitialized
= USE_EXTENSION
;
85 else if (!strcmp(dpy
->vendor
,"X11/NeWS - Sun Microsystems Inc."))
86 WxInitialized
= USE_EXTRA_PROTOCOL
;
97 if (WxInitialized
== NOT_INITIALIZED
)
100 switch (WxInitialized
) {
103 GetResReq(WxExtensionCode
, win
, req
);
105 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
109 case USE_EXTRA_PROTOCOL
:
111 GetResReq(GrabWindow
, win
, req
);
112 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
115 return (rep
.data00
); /* GrabToken */
116 case NOT_INITIALIZED
:
117 WxError(dpy
,X_WxGrab
);
121 return BadImplementation
;
132 if (WxInitialized
== NOT_INITIALIZED
)
135 switch (WxInitialized
) {
138 GetResReq(WxExtensionCode
, win
, req
);
139 req
->pad
= X_WxUnGrab
;
140 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
143 return rep
.data00
; /* Status */
144 case USE_EXTRA_PROTOCOL
:
146 GetResReq(UnGrabWindow
, win
, req
);
147 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
150 return (rep
.data00
); /* Status */
151 case NOT_INITIALIZED
:
152 WxError(dpy
,X_WxUnGrab
);
156 return BadImplementation
;
168 if (WxInitialized
== NOT_INITIALIZED
)
171 switch (WxInitialized
) {
174 GetResReq(WxExtensionCode
, cmap
, req
);
175 req
->pad
= X_WxGrabColormap
;
176 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
180 case USE_EXTRA_PROTOCOL
:
181 case NOT_INITIALIZED
:
182 WxError(dpy
,X_WxGrabColormap
);
186 return BadImplementation
;
197 if (WxInitialized
== NOT_INITIALIZED
)
200 switch (WxInitialized
) {
203 GetResReq(WxExtensionCode
, cmap
, req
);
204 req
->pad
= X_WxUnGrabColormap
;
205 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
208 return rep
.data00
; /* Status */
209 case USE_EXTRA_PROTOCOL
:
210 case NOT_INITIALIZED
:
211 WxError(dpy
,X_WxGrabColormap
);
215 return BadImplementation
;
227 if (WxInitialized
== NOT_INITIALIZED
)
230 switch (WxInitialized
) {
233 GetResReq(WxExtensionCode
, win
, req
);
234 req
->pad
= X_WxGrabRetained
;
235 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
239 case USE_EXTRA_PROTOCOL
:
240 case NOT_INITIALIZED
:
241 WxError(dpy
,X_WxGrabRetained
);
245 return BadImplementation
;
249 XUnGrabRetainedWindow(
256 if (WxInitialized
== NOT_INITIALIZED
)
259 switch (WxInitialized
) {
262 GetResReq(WxExtensionCode
, win
, req
);
263 req
->pad
= X_WxUnGrabRetained
;
264 (void) _XReply(dpy
, (xReply
*) &rep
, 0, xFalse
);
267 return rep
.data00
; /* Status */
268 case USE_EXTRA_PROTOCOL
:
269 case NOT_INITIALIZED
:
270 WxError(dpy
,X_WxUnGrabRetained
);
274 return BadImplementation
;
285 xOWGXRtndPathReply rep
;
287 if (WxInitialized
== NOT_INITIALIZED
)
290 switch (WxInitialized
) {
293 GetResReq(WxExtensionCode
, win
, req
);
294 req
->pad
= X_WxGetRetainedPath
;
295 (void) _XReply(dpy
, (xReply
*) &rep
,
296 (SIZEOF(xOWGXRtndPathReply
) - SIZEOF(xReply
)) >> 2, xFalse
);
299 strcpy(path
, rep
.path
);
301 case USE_EXTRA_PROTOCOL
:
302 case NOT_INITIALIZED
:
303 WxError(dpy
,X_WxGetRetainedPath
);
307 return BadImplementation
;
322 event
.type
= X_Error
;
323 event
.error_code
= BadImplementation
;
324 event
.request_code
= 0xff; /* Means that we were requesting an extension*/
325 event
.minor_code
= mc
;
326 event
.serial
= dpy
->request
;
327 if (_XErrorFunction
!= NULL
) {
328 return ((*_XErrorFunction
)(dpy
, &event
));