python-27: make tkinter-27 dependency actually conditional
[unleashed-userland.git] / components / x11 / libXext / srcs / src / XPanoramiX.c
blobb535cba980e04894589a7d4b4fe0feac3daa98a8
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
40 * Software.
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.
51 #define NEED_EVENTS
52 #define NEED_REPLIES
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 = {
77 NULL, /* create_gc */
78 NULL, /* copy_gc */
79 NULL, /* flush_gc */
80 NULL, /* free_gc */
81 NULL, /* create_font */
82 NULL, /* free_font */
83 close_display, /* close_display */
84 NULL, /* wire_to_event */
85 NULL, /* event_to_wire */
86 NULL, /* error */
87 NULL, /* error_string */
90 static XEXT_GENERATE_FIND_DISPLAY (find_display, panoramiX_ext_info,
91 panoramiX_extension_name,
92 &panoramiX_extension_hooks,
93 0, NULL)
95 static XEXT_GENERATE_CLOSE_DISPLAY (close_display, panoramiX_ext_info)
99 #ifdef SUNSOFT
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().
117 typedef struct {
118 int screen_number;
119 short x_org;
120 short y_org;
121 short width;
122 short height;
123 } XineramaScreenInfo;
125 static XineramaScreenInfo *
126 XineramaQueryScreens(
127 Display *dpy,
128 int *number
131 #endif
133 /****************************************************************************
135 * PanoramiX public interfaces *
137 ****************************************************************************/
139 Bool XPanoramiXQueryExtension (
140 Display *dpy,
141 int *event_basep,
142 int *error_basep)
144 XExtDisplayInfo *info = find_display (dpy);
146 if (XextHasExtension(info)) {
147 *event_basep = info->codes->first_event;
148 *error_basep = info->codes->first_error;
149 return True;
150 } else {
151 return False;
156 Status XPanoramiXQueryVersion(
157 Display *dpy,
158 int *major_versionp,
159 int *minor_versionp)
161 XExtDisplayInfo *info = find_display (dpy);
162 xPanoramiXQueryVersionReply rep;
163 register xPanoramiXQueryVersionReq *req;
165 #ifdef SUNSOFT
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;
173 return 1;
175 #endif
177 PanoramiXCheckExtension (dpy, info, 0);
179 LockDisplay (dpy);
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)) {
186 UnlockDisplay (dpy);
187 SyncHandle ();
188 return 0;
190 *major_versionp = rep.majorVersion;
191 *minor_versionp = rep.minorVersion;
192 #ifdef SUNSOFT /* Cache version info */
193 vinfo = Xmalloc(sizeof(struct XineramaExtVersionInfo));
195 if (vinfo != NULL) {
196 vinfo->major_vers = rep.majorVersion;
197 vinfo->minor_vers = rep.minorVersion;
198 info->data = (XPointer) vinfo;
200 #endif
201 UnlockDisplay (dpy);
202 SyncHandle ();
203 return 1;
206 XPanoramiXInfo *XPanoramiXAllocInfo(void)
208 return (XPanoramiXInfo *) Xmalloc (sizeof (XPanoramiXInfo));
211 Status XPanoramiXGetState (
212 Display *dpy,
213 Drawable drawable,
214 XPanoramiXInfo *panoramiX_info)
216 XExtDisplayInfo *info = find_display (dpy);
217 xPanoramiXGetStateReply rep;
218 register xPanoramiXGetStateReq *req;
220 PanoramiXCheckExtension (dpy, info, 0);
222 LockDisplay (dpy);
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)) {
228 UnlockDisplay (dpy);
229 SyncHandle ();
230 return 0;
232 UnlockDisplay (dpy);
233 SyncHandle ();
234 panoramiX_info->window = rep.window;
235 panoramiX_info->State = rep.state;
236 return 1;
239 Status XPanoramiXGetScreenCount (
240 Display *dpy,
241 Drawable drawable,
242 XPanoramiXInfo *panoramiX_info)
244 XExtDisplayInfo *info = find_display (dpy);
245 xPanoramiXGetScreenCountReply rep;
246 register xPanoramiXGetScreenCountReq *req;
248 PanoramiXCheckExtension (dpy, info, 0);
250 LockDisplay (dpy);
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)) {
256 UnlockDisplay (dpy);
257 SyncHandle ();
258 return 0;
260 UnlockDisplay (dpy);
261 SyncHandle ();
262 panoramiX_info->window = rep.window;
263 panoramiX_info->ScreenCount = rep.ScreenCount;
264 return 1;
267 Status XPanoramiXGetScreenSize (
268 Display *dpy,
269 Drawable drawable,
270 int screen_num,
271 XPanoramiXInfo *panoramiX_info)
273 XExtDisplayInfo *info = find_display (dpy);
274 xPanoramiXGetScreenSizeReply rep;
275 register xPanoramiXGetScreenSizeReq *req;
277 PanoramiXCheckExtension (dpy, info, 0);
279 LockDisplay (dpy);
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)) {
286 UnlockDisplay (dpy);
287 SyncHandle ();
288 return 0;
290 UnlockDisplay (dpy);
291 SyncHandle ();
292 panoramiX_info->window = rep.window;
293 panoramiX_info->screen = rep.screen;
294 panoramiX_info->width = rep.width;
295 panoramiX_info->height = rep.height;
296 return 1;
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
308 one server */
310 if(screen_number)
311 return False; /* no no, must say zero */
313 if(!XextHasExtension(info))
314 return False;
315 else
317 int maj, min;
319 if (XPanoramiXQueryVersion(display, &maj, &min) == 0) {
320 return False;
322 if ((maj == 1) && (min >= 0)) {
323 return XineramaIsActive(display);
324 } else {
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 *
332 num_framebuffers )
335 int framebuffercount; /* number of fb's we know about */
336 int i; /* fortran */
337 Window win;
338 int x = 0, y = 0;
339 int maj, min;
341 if(!XineramaGetState(display, screen_number))
342 return 0;
344 if (XPanoramiXQueryVersion(display, &maj, &min) == 0) {
345 return 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) {
353 return 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;
361 XFree(xsi);
362 } else {
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
369 working surface */
371 win = 0;
373 XPanoramiXGetScreenCount(display, win, &panoramiX_info);
375 if(!panoramiX_info.ScreenCount) {
376 return 0;
378 framebuffercount = panoramiX_info.ScreenCount;
380 /* Make sure not to overflow buffers if X server has more screens
381 than we expect */
382 if (framebuffercount > MAXSCREEN)
383 framebuffercount = MAXSCREEN;
385 info = malloc(sizeof(XineramaInfo));
387 if(!info)
388 return 0;
390 /* now read the servers list of frame buffers */
392 if(!XGetXineramaInfo(display,screen_number,
393 (XID) RootWindow(display,screen_number),info))
395 free(info);
396 return 0;
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;
408 free(info);
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;
441 Bool
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))
449 return False;
451 rep = malloc(sizeof(xXineramaInfoReply));
453 LockDisplay(dpy);
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
461 xFalse))
463 UnlockDisplay(dpy);
464 SyncHandle();
465 Xfree(rep);
466 return NULL;
468 info->wid = VirtualWID;
469 memcpy(&info->subs[0],&rep->subs[0],(MAXSCREEN-1) * sizeof(SubWID));
470 UnlockDisplay(dpy);
471 SyncHandle();
472 free(rep);
473 return 1;
476 Status XineramaGetCenterHint(Display* display, int screen_number,
477 int* xret, int* yret)
479 int x = -1;
480 int y = -1;
481 int fb = -1;
482 int fbs;
483 XrmDatabase xrdb;
484 XrmValue value;
485 char* vtype;
486 char buffer[20];
487 XRectangle rects[MAXSCREEN];
488 unsigned char hints[MAXSCREEN];
489 Bool xiGetState, xiGetInfo;
490 long n;
493 if (screen_number)
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;
507 errno = 0;
508 n = strtol(buffer, (char **)NULL, 10);
509 if ( ! ((n == 0) && (errno == EINVAL)) )
510 fb = n;
514 if (!xiGetState || !xiGetInfo)
515 fbs = 1;
516 if ((fb < 0) || (fb >= fbs)) /* fb value not valid */
518 if (XrmGetResource(xrdb, "xineramaCenterHintX", "XineramaCenterHintX",
519 &vtype, &value))
521 if (value.size < sizeof(buffer)) {
522 strncpy(buffer, value.addr, value.size);
523 buffer[value.size] = 0;
524 errno = 0;
525 n = strtol(buffer, (char **)NULL, 10);
526 if ( ! ((n == 0) && (errno == EINVAL)) )
527 x = n;
530 if (XrmGetResource(xrdb, "xineramaCenterHintY", "XineramaCenterHintY",
531 &vtype, &value))
533 if (value.size < sizeof(buffer)) {
534 strncpy(buffer, value.addr, value.size);
535 buffer[value.size] = 0;
536 errno = 0;
537 n = strtol(buffer, (char **)NULL, 10);
538 if ( ! ((n == 0) && (errno == EINVAL)) )
539 y = n;
545 if (!xiGetState)
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;
555 else
557 if ((x >= 0) && (x < WidthOfScreen(ScreenOfDisplay(display, 0))))
558 *xret = x;
559 else
560 *xret = (WidthOfScreen(ScreenOfDisplay(display, 0))) / 2;
562 if ((y >= 0) && (y < HeightOfScreen(ScreenOfDisplay(display, 0))))
563 *yret = y;
564 else
565 *yret = (HeightOfScreen(ScreenOfDisplay(display, 0))) / 2;
568 else
570 /* Xinerama is active, so do the right thing. */
572 if (!xiGetInfo)
573 return 0;
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);
580 else
582 if ((x >= 0) && (x < WidthOfScreen(ScreenOfDisplay(display, 0))))
583 *xret = x;
584 else
585 *xret = (WidthOfScreen(ScreenOfDisplay(display, 0))) / 2;
587 if ((y >= 0) && (y < HeightOfScreen(ScreenOfDisplay(display, 0))))
588 *yret = y;
589 else
590 *yret = (HeightOfScreen(ScreenOfDisplay(display, 0))) / 2;
593 return 1;
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 \*******************************************************************/
605 static
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 */
615 LockDisplay (dpy);
616 GetReq (XineramaIsActive, req);
617 req->reqType = info->codes->major_opcode;
618 req->panoramiXReqType = X_XineramaIsActive;
619 if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
620 UnlockDisplay (dpy);
621 SyncHandle ();
622 return False;
624 UnlockDisplay (dpy);
625 SyncHandle ();
626 return rep.state;
629 static
630 XineramaScreenInfo *
631 XineramaQueryScreens(
632 Display *dpy,
633 int *number
636 XExtDisplayInfo *info = find_display (dpy);
637 xXineramaQueryScreensReply rep;
638 xXineramaQueryScreensReq *req;
639 XineramaScreenInfo *scrnInfo = NULL;
641 PanoramiXCheckExtension (dpy, info, 0);
643 LockDisplay (dpy);
644 GetReq (XineramaQueryScreens, req);
645 req->reqType = info->codes->major_opcode;
646 req->panoramiXReqType = X_XineramaQueryScreens;
647 if (!_XReply (dpy, (xReply *) &rep, 0, xFalse)) {
648 UnlockDisplay (dpy);
649 SyncHandle ();
650 return NULL;
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) {
666 int i;
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;
681 } else {
682 _XEatDataWords(dpy, rep.length);
683 *number = 0;
686 UnlockDisplay (dpy);
687 SyncHandle ();
688 return scrnInfo;