1 /* $TOG: XPanoramiX.c /main/2 1997/11/16 08:45:41 kaleb $ */
2 /*****************************************************************
4 Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software.
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
19 BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
21 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of Digital Equipment Corporation
24 shall not be used in advertising or otherwise to promote the sale, use or other
25 dealings in this Software without prior written authorization from Digital
26 Equipment Corporation.
28 ******************************************************************/
29 /* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
53 #include <X11/Xlibint.h>
54 #include <X11/Xutil.h>
55 #include <X11/Xresource.h>
56 #include <X11/extensions/Xext.h>
57 #include <X11/extensions/extutil.h>
58 #include <X11/extensions/panoramiXext.h>
59 #include <X11/extensions/panoramiXproto.h>
60 #include <X11/extensions/xinerama.h>
62 Bool
XGetXineramaInfo(Display
*dpy
, int screen_number
, XID VirtualWID
, XineramaInfo
*info
);
64 static XExtensionInfo _panoramiX_ext_info_data
;
65 static XExtensionInfo
*panoramiX_ext_info
= &_panoramiX_ext_info_data
;
66 static /* const */ char *panoramiX_extension_name
= PANORAMIX_PROTOCOL_NAME
;
68 #define PanoramiXCheckExtension(dpy,i,val) \
69 XextCheckExtension (dpy, i, panoramiX_extension_name, val)
70 #define PanoramiXSimpleCheckExtension(dpy,i) \
71 XextSimpleCheckExtension (dpy, i, panoramiX_extension_name)
73 static int close_display(Display
*dpy
, XExtCodes
*codes
);
74 static Bool
wire_to_event(Display
*dpy
, XEvent
*libevent
, xEvent
*netevent
);
75 static Status
event_to_wire(Display
*dpy
, XEvent
*libevent
, xEvent
*netevent
);
76 static /* const */ XExtensionHooks panoramiX_extension_hooks
= {
81 NULL
, /* create_font */
83 close_display
, /* close_display */
84 NULL
, /* wire_to_event */
85 NULL
, /* event_to_wire */
87 NULL
, /* error_string */
90 static XEXT_GENERATE_FIND_DISPLAY (find_display
, panoramiX_ext_info
,
91 panoramiX_extension_name
,
92 &panoramiX_extension_hooks
,
95 static XEXT_GENERATE_CLOSE_DISPLAY (close_display
, panoramiX_ext_info
)
100 struct XineramaExtVersionInfo
102 int major_vers
, minor_vers
;
105 /**** Code taken from xc/include/extensions/Xinerama.h in Xorg for
106 compatibility with XFree86 & Xorg protocols ****/
108 static Bool
XineramaIsActive(Display
*dpy
);
111 Returns the number of heads and a pointer to an array of
112 structures describing the position and size of the individual
113 heads. Returns NULL and number = 0 if Xinerama is not active.
115 Returned array should be freed with XFree().
123 } XineramaScreenInfo
;
125 static XineramaScreenInfo
*
126 XineramaQueryScreens(
133 /****************************************************************************
135 * PanoramiX public interfaces *
137 ****************************************************************************/
139 Bool
XPanoramiXQueryExtension (
144 XExtDisplayInfo
*info
= find_display (dpy
);
146 if (XextHasExtension(info
)) {
147 *event_basep
= info
->codes
->first_event
;
148 *error_basep
= info
->codes
->first_error
;
156 Status
XPanoramiXQueryVersion(
161 XExtDisplayInfo
*info
= find_display (dpy
);
162 xPanoramiXQueryVersionReply rep
;
163 register xPanoramiXQueryVersionReq
*req
;
166 struct XineramaExtVersionInfo
*vinfo
;
168 if (info
->data
!= NULL
) {
169 vinfo
= (struct XineramaExtVersionInfo
*) info
->data
;
171 *major_versionp
= vinfo
->major_vers
;
172 *minor_versionp
= vinfo
->minor_vers
;
177 PanoramiXCheckExtension (dpy
, info
, 0);
180 GetReq (PanoramiXQueryVersion
, req
);
181 req
->reqType
= info
->codes
->major_opcode
;
182 req
->panoramiXReqType
= X_PanoramiXQueryVersion
;
183 req
->clientMajor
= PANORAMIX_MAJOR_VERSION
;
184 req
->clientMinor
= PANORAMIX_MINOR_VERSION
;
185 if (!_XReply (dpy
, (xReply
*) &rep
, 0, xTrue
)) {
190 *major_versionp
= rep
.majorVersion
;
191 *minor_versionp
= rep
.minorVersion
;
192 #ifdef SUNSOFT /* Cache version info */
193 vinfo
= Xmalloc(sizeof(struct XineramaExtVersionInfo
));
196 vinfo
->major_vers
= rep
.majorVersion
;
197 vinfo
->minor_vers
= rep
.minorVersion
;
198 info
->data
= (XPointer
) vinfo
;
206 XPanoramiXInfo
*XPanoramiXAllocInfo(void)
208 return (XPanoramiXInfo
*) Xmalloc (sizeof (XPanoramiXInfo
));
211 Status
XPanoramiXGetState (
214 XPanoramiXInfo
*panoramiX_info
)
216 XExtDisplayInfo
*info
= find_display (dpy
);
217 xPanoramiXGetStateReply rep
;
218 register xPanoramiXGetStateReq
*req
;
220 PanoramiXCheckExtension (dpy
, info
, 0);
223 GetReq (PanoramiXGetState
, req
);
224 req
->reqType
= info
->codes
->major_opcode
;
225 req
->panoramiXReqType
= X_PanoramiXGetState
;
226 req
->window
= drawable
;
227 if (!_XReply (dpy
, (xReply
*) &rep
, 0, xTrue
)) {
234 panoramiX_info
->window
= rep
.window
;
235 panoramiX_info
->State
= rep
.state
;
239 Status
XPanoramiXGetScreenCount (
242 XPanoramiXInfo
*panoramiX_info
)
244 XExtDisplayInfo
*info
= find_display (dpy
);
245 xPanoramiXGetScreenCountReply rep
;
246 register xPanoramiXGetScreenCountReq
*req
;
248 PanoramiXCheckExtension (dpy
, info
, 0);
251 GetReq (PanoramiXGetScreenCount
, req
);
252 req
->reqType
= info
->codes
->major_opcode
;
253 req
->panoramiXReqType
= X_PanoramiXGetScreenCount
;
254 req
->window
= drawable
;
255 if (!_XReply (dpy
, (xReply
*) &rep
, 0, xTrue
)) {
262 panoramiX_info
->window
= rep
.window
;
263 panoramiX_info
->ScreenCount
= rep
.ScreenCount
;
267 Status
XPanoramiXGetScreenSize (
271 XPanoramiXInfo
*panoramiX_info
)
273 XExtDisplayInfo
*info
= find_display (dpy
);
274 xPanoramiXGetScreenSizeReply rep
;
275 register xPanoramiXGetScreenSizeReq
*req
;
277 PanoramiXCheckExtension (dpy
, info
, 0);
280 GetReq (PanoramiXGetScreenSize
, req
);
281 req
->reqType
= info
->codes
->major_opcode
;
282 req
->panoramiXReqType
= X_PanoramiXGetScreenSize
;
283 req
->window
= drawable
;
284 req
->screen
= screen_num
; /* need to define */
285 if (!_XReply (dpy
, (xReply
*) &rep
, 0, xTrue
)) {
292 panoramiX_info
->window
= rep
.window
;
293 panoramiX_info
->screen
= rep
.screen
;
294 panoramiX_info
->width
= rep
.width
;
295 panoramiX_info
->height
= rep
.height
;
300 /* new api for xinerama */
302 Bool
XineramaGetState(Display
* display
, int screen_number
)
304 XExtDisplayInfo
*info
= find_display (display
);
306 /* screen_number may seem like hyper-bogus stuff, but:
307 it may be possible to have 2 xinerama screen sets on
311 return False
; /* no no, must say zero */
313 if(!XextHasExtension(info
))
319 if (XPanoramiXQueryVersion(display
, &maj
, &min
) == 0) {
322 if ((maj
== 1) && (min
>= 0)) {
323 return XineramaIsActive(display
);
325 return True
; /* can't check in 1.0 protocol */
330 Status
XineramaGetInfo(Display
* display
, int screen_number
, XRectangle
*
331 framebuffer_rects
, unsigned char * framebuffer_hints
, int *
335 int framebuffercount
; /* number of fb's we know about */
341 if(!XineramaGetState(display
, screen_number
))
344 if (XPanoramiXQueryVersion(display
, &maj
, &min
) == 0) {
347 if ((maj
== 1) && (min
>= 0)) {
348 /* Use XFree86/Xorg extended protocol */
349 XineramaScreenInfo
*xsi
;
351 xsi
= XineramaQueryScreens(display
, &framebuffercount
);
352 if (xsi
== NULL
|| framebuffercount
== 0) {
355 for (i
= 0; i
< framebuffercount
; i
++) {
356 framebuffer_rects
[i
].x
= xsi
[i
].x_org
;
357 framebuffer_rects
[i
].y
= xsi
[i
].y_org
;
358 framebuffer_rects
[i
].width
= xsi
[i
].width
;
359 framebuffer_rects
[i
].height
= xsi
[i
].height
;
363 /* Use Sun extended protocol */
364 XPanoramiXInfo panoramiX_info
;
365 XineramaInfo
*info
; /* list of screen x,y etc */
367 /* get the count of screens that we need to know about
368 NOTE: screen_number implies a set of fb's for this
373 XPanoramiXGetScreenCount(display
, win
, &panoramiX_info
);
375 if(!panoramiX_info
.ScreenCount
) {
378 framebuffercount
= panoramiX_info
.ScreenCount
;
380 /* Make sure not to overflow buffers if X server has more screens
382 if (framebuffercount
> MAXSCREEN
)
383 framebuffercount
= MAXSCREEN
;
385 info
= malloc(sizeof(XineramaInfo
));
390 /* now read the servers list of frame buffers */
392 if(!XGetXineramaInfo(display
,screen_number
,
393 (XID
) RootWindow(display
,screen_number
),info
))
400 for(i
= 0; i
< framebuffercount
; i
++)
402 framebuffer_rects
[i
].x
= info
->subs
[i
].dx
;
403 framebuffer_rects
[i
].y
= info
->subs
[i
].dy
;
404 framebuffer_rects
[i
].width
= info
->subs
[i
].wdx
;
405 framebuffer_rects
[i
].height
= info
->subs
[i
].wdy
;
411 *num_framebuffers
= framebuffercount
;
413 /* assume the start up and list are in the same order */
415 for(i
= 0; i
< framebuffercount
; i
++)
417 framebuffer_hints
[i
] = 0;
418 if (i
> 0) /* fb0 never gets a hint */
420 if (framebuffer_rects
[i
].x
> x
) { /* right */
421 if (framebuffer_rects
[i
].y
== y
)
422 framebuffer_hints
[i
] = XINERAMA_PLACE_RIGHT
;
424 else if (framebuffer_rects
[i
].x
== x
) { /* up/down */
425 if (framebuffer_rects
[i
].y
> y
)
426 framebuffer_hints
[i
] = XINERAMA_PLACE_TOP
;
427 else if (framebuffer_rects
[i
].y
< y
)
428 framebuffer_hints
[i
] = XINERAMA_PLACE_BOTTOM
;
429 } else if (framebuffer_rects
[i
].x
< x
) { /* left */
430 if (framebuffer_rects
[i
].y
== y
)
431 framebuffer_hints
[i
] = XINERAMA_PLACE_LEFT
;
434 x
= framebuffer_rects
[i
].x
;
435 y
= framebuffer_rects
[i
].y
;
438 return framebuffercount
;
442 XGetXineramaInfo(Display
*dpy
,int screen_number
,XID VirtualWID
,XineramaInfo
*info
)
444 xXineramaInfoReq
*req
;
445 xXineramaInfoReply
*rep
;
446 XExtDisplayInfo
*Extinfo
= find_display (dpy
);
448 if(!XineramaGetState(dpy
, screen_number
))
451 rep
= malloc(sizeof(xXineramaInfoReply
));
454 GetReq(XineramaInfo
, req
);
455 req
->reqType
= Extinfo
->codes
->major_opcode
;
456 req
->xXineramaReqType
= X_XineramaInfo
;
457 req
->visual
= VirtualWID
;
459 if (!_XReply(dpy
, (xReply
*)rep
, (sizeof(xXineramaInfoReply
)-32) >> 2
468 info
->wid
= VirtualWID
;
469 memcpy(&info
->subs
[0],&rep
->subs
[0],(MAXSCREEN
-1) * sizeof(SubWID
));
476 Status
XineramaGetCenterHint(Display
* display
, int screen_number
,
477 int* xret
, int* yret
)
487 XRectangle rects
[MAXSCREEN
];
488 unsigned char hints
[MAXSCREEN
];
489 Bool xiGetState
, xiGetInfo
;
494 return 0; /* don't yet support multiple Xinerama screens */
496 xiGetState
= XineramaGetState(display
, screen_number
);
497 xiGetInfo
= XineramaGetInfo(display
, screen_number
, rects
, hints
, &fbs
);
499 if (xrdb
= XrmGetDatabase(display
))
501 if (XrmGetResource(xrdb
, "xineramaDefaultFramebuffer",
502 "XineramaDefaultFramebuffer", &vtype
, &value
))
504 if (value
.size
< sizeof(buffer
)) {
505 strncpy(buffer
, value
.addr
, value
.size
);
506 buffer
[value
.size
] = 0;
508 n
= strtol(buffer
, (char **)NULL
, 10);
509 if ( ! ((n
== 0) && (errno
== EINVAL
)) )
514 if (!xiGetState
|| !xiGetInfo
)
516 if ((fb
< 0) || (fb
>= fbs
)) /* fb value not valid */
518 if (XrmGetResource(xrdb
, "xineramaCenterHintX", "XineramaCenterHintX",
521 if (value
.size
< sizeof(buffer
)) {
522 strncpy(buffer
, value
.addr
, value
.size
);
523 buffer
[value
.size
] = 0;
525 n
= strtol(buffer
, (char **)NULL
, 10);
526 if ( ! ((n
== 0) && (errno
== EINVAL
)) )
530 if (XrmGetResource(xrdb
, "xineramaCenterHintY", "XineramaCenterHintY",
533 if (value
.size
< sizeof(buffer
)) {
534 strncpy(buffer
, value
.addr
, value
.size
);
535 buffer
[value
.size
] = 0;
537 n
= strtol(buffer
, (char **)NULL
, 10);
538 if ( ! ((n
== 0) && (errno
== EINVAL
)) )
547 /* Xinerama is not active, so there's only one framebuffer */
548 /* (for screen 0). Return appropriate values anyway. */
550 if (fb
== 0) /* assume screen == fb == 0. Code only supports one screen.*/
552 *xret
= (WidthOfScreen(ScreenOfDisplay(display
, 0))) / 2;
553 *yret
= (HeightOfScreen(ScreenOfDisplay(display
, 0))) / 2;
557 if ((x
>= 0) && (x
< WidthOfScreen(ScreenOfDisplay(display
, 0))))
560 *xret
= (WidthOfScreen(ScreenOfDisplay(display
, 0))) / 2;
562 if ((y
>= 0) && (y
< HeightOfScreen(ScreenOfDisplay(display
, 0))))
565 *yret
= (HeightOfScreen(ScreenOfDisplay(display
, 0))) / 2;
570 /* Xinerama is active, so do the right thing. */
575 if ((fb
>= 0) && (fb
< fbs
) && (fb
< MAXSCREEN
)) /* if fb has a legal value */
577 *xret
= rects
[fb
].x
+ (rects
[fb
].width
/ 2);
578 *yret
= rects
[fb
].y
+ (rects
[fb
].height
/ 2);
582 if ((x
>= 0) && (x
< WidthOfScreen(ScreenOfDisplay(display
, 0))))
585 *xret
= (WidthOfScreen(ScreenOfDisplay(display
, 0))) / 2;
587 if ((y
>= 0) && (y
< HeightOfScreen(ScreenOfDisplay(display
, 0))))
590 *yret
= (HeightOfScreen(ScreenOfDisplay(display
, 0))) / 2;
596 /**** Code taken from xc/lib/Xinerama/Xinerama.c in Xorg for compatibility with
597 XFree86 & Xorg Xinerama 1.1 protocol ****/
599 /*******************************************************************\
600 Alternate interface to make up for shortcomings in the original,
601 namely, the omission of the screen origin. The new interface is
602 in the "Xinerama" namespace instead of "PanoramiX".
603 \*******************************************************************/
606 Bool
XineramaIsActive(Display
*dpy
)
608 xXineramaIsActiveReply rep
;
609 xXineramaIsActiveReq
*req
;
610 XExtDisplayInfo
*info
= find_display (dpy
);
612 if(!XextHasExtension(info
))
613 return False
; /* server doesn't even have the extension */
616 GetReq (XineramaIsActive
, req
);
617 req
->reqType
= info
->codes
->major_opcode
;
618 req
->panoramiXReqType
= X_XineramaIsActive
;
619 if (!_XReply (dpy
, (xReply
*) &rep
, 0, xTrue
)) {
631 XineramaQueryScreens(
636 XExtDisplayInfo
*info
= find_display (dpy
);
637 xXineramaQueryScreensReply rep
;
638 xXineramaQueryScreensReq
*req
;
639 XineramaScreenInfo
*scrnInfo
= NULL
;
641 PanoramiXCheckExtension (dpy
, info
, 0);
644 GetReq (XineramaQueryScreens
, req
);
645 req
->reqType
= info
->codes
->major_opcode
;
646 req
->panoramiXReqType
= X_XineramaQueryScreens
;
647 if (!_XReply (dpy
, (xReply
*) &rep
, 0, xFalse
)) {
654 * rep.number is a CARD32 so could be as large as 2^32
655 * The X11 protocol limits the total screen size to 64k x 64k,
656 * and no screen can be smaller than a pixel. While technically
657 * that means we could theoretically reach 2^32 screens, and that's
658 * not even taking overlap into account, Xorg is currently limited
659 * to 16 screens, and few known servers have a much higher limit,
660 * so 1024 seems more than enough to prevent both integer overflow
661 * and insane X server responses causing massive memory allocation.
663 if ((rep
.number
> 0) && (rep
.number
<= 1024))
664 scrnInfo
= Xmalloc(sizeof(XineramaScreenInfo
) * rep
.number
);
665 if (scrnInfo
!= NULL
) {
668 for (i
= 0; i
< rep
.number
; i
++) {
669 xXineramaScreenInfo scratch
;
671 _XRead(dpy
, (char*)(&scratch
), sz_XineramaScreenInfo
);
673 scrnInfo
[i
].screen_number
= i
;
674 scrnInfo
[i
].x_org
= scratch
.x_org
;
675 scrnInfo
[i
].y_org
= scratch
.y_org
;
676 scrnInfo
[i
].width
= scratch
.width
;
677 scrnInfo
[i
].height
= scratch
.height
;
680 *number
= rep
.number
;
682 _XEatDataWords(dpy
, rep
.length
);