Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/awt/multiVis.c
32287 views
/*1* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/** ------------------------------------------------------------------------25This file contains functions to create a list of regions which26tile a specified window. Each region contains all visible27portions of the window which are drawn with the same visual.28If the window consists of subwindows of two different visual types,29there will be two regions in the list. The list can be traversed30to correctly pull an image of the window using XGetImage or the31Image Library.3233This file is available under and governed by the GNU General Public34License version 2 only, as published by the Free Software Foundation.35However, the following notice accompanied the original version of this36file:3738Copyright 1994 Hewlett-Packard Co.39Copyright 1996, 1998 The Open Group4041Permission to use, copy, modify, distribute, and sell this software and its42documentation for any purpose is hereby granted without fee, provided that43the above copyright notice appear in all copies and that both that44copyright notice and this permission notice appear in supporting45documentation.4647The above copyright notice and this permission notice shall be included48in all copies or substantial portions of the Software.4950THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS51OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF52MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.53IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR54OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,55ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR56OTHER DEALINGS IN THE SOFTWARE.5758Except as contained in this notice, the name of The Open Group shall59not be used in advertising or otherwise to promote the sale, use or60other dealings in this Software without prior written authorization61from The Open Group.6263------------------------------------------------------------------------ **/6465#include <stdlib.h>66#include <X11/Xlib.h>67#include <X11/Xutil.h>68#include <X11/X.h>69#include <stdio.h>70#include "list.h"71#include "wsutils.h"72#include "multiVis.h"73/* These structures are copied from X11/region.h. For some reason74* they're invisible from the outside.75*/76typedef struct {77short x1, x2, y1, y2;78} myBox, myBOX, myBoxRec, *myBoxPtr;7980typedef struct my_XRegion {81long size;82long numRects;83myBOX *rects;84myBOX extents;85} myREGION;8687/* Items in long list of windows that have some part in the grabbed area */88typedef struct {89Window win;90Visual *vis;91Colormap cmap;92int x_rootrel, y_rootrel; /* root relative location of window */93int x_vis, y_vis; /* rt rel x,y of vis part, not parent clipped */94int width, height; /* width and height of visible part */95int border_width; /* border width of the window */96Window parent; /* id of parent (for debugging) */97} image_win_type;9899/* Items in short list of regions that tile the grabbed area. May have100multiple windows in the region.101*/102typedef struct {103Window win; /* lowest window of this visual */104Visual *vis;105Colormap cmap;106int x_rootrel, y_rootrel; /* root relative location of bottom window */107int x_vis, y_vis; /* rt rel x,y of vis part, not parent clipped */108int width, height; /* w & h of visible rect of bottom window */109int border; /* border width of the window */110Region visible_region;111} image_region_type;112113/** ------------------------------------------------------------------------114Returns TRUE if the two structs pointed to have the same "vis" &115"cmap" fields and s2 lies completely within s1. s1 and s2 can116point to structs of image_win_type or image_region_type.117------------------------------------------------------------------------ **/118#define SAME_REGIONS( s1, s2) \119((s1)->vis == (s2)->vis && (s1)->cmap == (s2)->cmap && \120(s1)->x_vis <= (s2)->x_vis && \121(s1)->y_vis <= (s2)->y_vis && \122(s1)->x_vis + (s1)->width >= (s2)->x_vis + (s2)->width && \123(s1)->y_vis + (s1)->height >= (s2)->y_vis + (s2)->height)124125#ifndef MIN126#define MIN( a, b) ((a) < (b) ? a : b)127#define MAX( a, b) ((a) > (b) ? a : b)128#endif129130#define RED_SHIFT 16131#define GREEN_SHIFT 8132#define BLUE_SHIFT 0133134/*135extern list_ptr new_list();136extern list_ptr dup_list_head();137extern void * first_in_list();138extern void * next_in_list();139extern int add_to_list();140extern void zero_list();141extern void delete_list();142extern void delete_list_destroying();143extern unsigned int list_length();144*/145146/* Prototype Declarations for Static Functions */147static void QueryColorMap(148Display *, Colormap , Visual *,149XColor **, int *, int *, int *150);151static void TransferImage(152Display *, XImage *,int, int , image_region_type*,153XImage *,int ,int154);155static XImage * ReadRegionsInList(156Display *, Visual *, int, int, unsigned int,157unsigned int, XRectangle, list_ptr158);159160static list_ptr make_region_list(161Display*, Window, XRectangle*,162int*, int, XVisualInfo**, int *163);164165static void destroy_region_list(166list_ptr167) ;168static void subtr_rect_from_image_region(169image_region_type *, int , int , int , int170);171static void add_rect_to_image_region(172image_region_type *,173int , int , int , int174);175static int src_in_region_list(176image_win_type *, list_ptr177);178static void add_window_to_list(179list_ptr, Window, int, int ,180int , int , int , int, int,181Visual*, Colormap, Window182);183static int src_in_image(184image_win_type *, int , XVisualInfo**185);186static int src_in_overlay(187image_region_type *, int, OverlayInfo *, int*, int*188);189static void make_src_list(190Display *, list_ptr, XRectangle *, Window,191int, int, XWindowAttributes *, XRectangle *192);193static void destroy_image_region(194image_region_type *195);196197/* End of Prototype Declarations */198199void initFakeVisual(Visual *Vis)200{201Vis->ext_data=NULL;202Vis->class = DirectColor ;203Vis->red_mask = 0x00FF0000;204Vis->green_mask = 0x0000FF00 ;205Vis->blue_mask = 0x000000FF ;206Vis->map_entries = 256 ;207Vis->bits_per_rgb = 8 ;208}209210static void211QueryColorMap(Display *disp, Colormap src_cmap, Visual *src_vis,212XColor **src_colors, int *rShift, int *gShift, int *bShift)213{214unsigned int ncolors,i ;215unsigned long redMask, greenMask, blueMask;216int redShift, greenShift, blueShift;217XColor *colors ;218219ncolors = (unsigned) src_vis->map_entries ;220/* JDK modification.221* use calloc instead of malloc to initialize allocated memory222* *src_colors = colors = (XColor *)malloc(ncolors * sizeof(XColor) ) ;223*/224*src_colors = colors = (XColor *)calloc(ncolors, sizeof(XColor));225226if(src_vis->class != TrueColor && src_vis->class != DirectColor)227{228for(i=0 ; i < ncolors ; i++)229{230colors[i].pixel = i ;231colors[i].pad = 0;232colors[i].flags = DoRed|DoGreen|DoBlue;233}234}235else /** src is decomposed rgb ***/236{237/* Get the X colormap */238redMask = src_vis->red_mask;239greenMask = src_vis->green_mask;240blueMask = src_vis->blue_mask;241redShift = 0; while (!(redMask&0x1)) {242redShift++;243redMask = redMask>>1;244}245greenShift = 0; while (!(greenMask&0x1)) {246greenShift++;247greenMask = greenMask>>1;248}249blueShift = 0; while (!(blueMask&0x1)) {250blueShift++;251blueMask = blueMask>>1;252}253*rShift = redShift ;254*gShift = greenShift ;255*bShift = blueShift ;256for (i=0; i<ncolors; i++) {257if( i <= redMask)colors[i].pixel = (i<<redShift) ;258if( i <= greenMask)colors[i].pixel |= (i<<greenShift) ;259if( i <= blueMask)colors[i].pixel |= (i<<blueShift) ;260/***** example :for gecko's 3-3-2 map, blue index should be <= 3.261colors[i].pixel = (i<<redShift)|(i<<greenShift)|(i<<blueShift);262*****/263colors[i].pad = 0;264colors[i].flags = DoRed|DoGreen|DoBlue;265}266}267268XQueryColors(disp, src_cmap, colors, (int) ncolors);269}270271int272GetMultiVisualRegions(Display *disp,273/* root win on which grab was done */274Window srcRootWinid,275/* root rel UL corner of bounding box of grab */276int x, int y,277/* size of bounding box of grab */278unsigned int width, unsigned int height,279int *transparentOverlays, int *numVisuals,280XVisualInfo **pVisuals, int *numOverlayVisuals,281OverlayInfo **pOverlayVisuals,282int *numImageVisuals, XVisualInfo ***pImageVisuals,283/* list of regions to read from */284list_ptr *vis_regions,285list_ptr *vis_image_regions, int *allImage)286{287int hasNonDefault;288XRectangle bbox; /* bounding box of grabbed area */289290291bbox.x = x; /* init X rect for bounding box */292bbox.y = y;293bbox.width = width;294bbox.height = height;295296GetXVisualInfo(disp,DefaultScreen(disp),297transparentOverlays,298numVisuals, pVisuals,299numOverlayVisuals, pOverlayVisuals,300numImageVisuals, pImageVisuals);301302*vis_regions = *vis_image_regions = NULL ;303if ((*vis_regions = make_region_list( disp, srcRootWinid, &bbox,304&hasNonDefault, *numImageVisuals,305*pImageVisuals, allImage)) == NULL)306return 0 ;307308if (*transparentOverlays)309{310*allImage = 1; /* until proven otherwise,311this flags that it to be an image only list */312*vis_image_regions =313make_region_list( disp, srcRootWinid, &bbox, &hasNonDefault,314*numImageVisuals, *pImageVisuals, allImage);315}316317/* if there is a second region in any of the two lists return 1 **/318if ( ( *vis_regions && (*vis_regions)->next && (*vis_regions)->next->next ) ||319( *vis_image_regions && (*vis_image_regions)->next &&320(*vis_image_regions)->next->next ) ) return 1 ;321else return 0 ;322323}324325static void TransferImage(Display *disp, XImage *reg_image,326int srcw, int srch,327image_region_type *reg, XImage *target_image,328int dst_x, int dst_y)329{330int i,j,old_pixel,new_pixel,red_ind,green_ind,blue_ind ;331XColor *colors;332int rShift = 0, gShift = 0, bShift = 0;333334QueryColorMap(disp,reg->cmap,reg->vis,&colors,335&rShift,&gShift,&bShift) ;336337switch (reg->vis->class) {338case TrueColor :339for(i=0 ; i < srch ; i++)340{341for(j=0 ; j < srcw ; j++)342{343old_pixel = XGetPixel(reg_image,j,i) ;344345/*346* JDK modification.347* commented out since not using server RGB masks in all true color modes348* causes the R and B values to be swapped around on some X servers349* - robi.khan@eng 9/7/1999350* if( reg->vis->map_entries == 16) {351*/352red_ind = (old_pixel & reg->vis->red_mask) >> rShift ;353green_ind = (old_pixel & reg->vis->green_mask) >> gShift ;354blue_ind = (old_pixel & reg->vis->blue_mask) >> bShift ;355356new_pixel = (357((colors[red_ind].red >> 8) << RED_SHIFT)358|((colors[green_ind].green >> 8) << GREEN_SHIFT)359|((colors[blue_ind].blue >> 8) << BLUE_SHIFT)360);361/* JDK modification.362* else part of above modification363*364* }365* else366* new_pixel = old_pixel;367*/368369XPutPixel(target_image,dst_x+j, dst_y+i,new_pixel);370371}372}373break;374case DirectColor :375for(i=0 ; i < srch ; i++)376{377378for(j=0 ; j < srcw ; j++)379{380old_pixel = XGetPixel(reg_image,j,i) ;381red_ind = (old_pixel & reg->vis->red_mask) >> rShift ;382green_ind = (old_pixel & reg->vis->green_mask) >> gShift ;383blue_ind = (old_pixel & reg->vis->blue_mask) >> bShift ;384385new_pixel = (386((colors[red_ind].red >> 8) << RED_SHIFT)387|((colors[green_ind].green >> 8) << GREEN_SHIFT)388|((colors[blue_ind].blue >> 8) << BLUE_SHIFT)389);390XPutPixel(target_image,dst_x+j, dst_y+i,new_pixel);391392}393}394break;395default :396for(i=0 ; i < srch ; i++)397{398for(j=0 ; j < srcw ; j++)399{400old_pixel = XGetPixel(reg_image,j,i) ;401402new_pixel = (403((colors[old_pixel].red >> 8) << RED_SHIFT)404|((colors[old_pixel].green >> 8) << GREEN_SHIFT)405|((colors[old_pixel].blue >> 8) << BLUE_SHIFT)406);407XPutPixel(target_image,dst_x+j, dst_y+i,new_pixel);408409}410}411break;412}413/* JDK modification414* Fix memory leak by freeing colors415* - robi.khan@eng 9/22/1999416*/417free(colors);418}419420static XImage *421ReadRegionsInList(Display *disp, Visual *fakeVis, int depth, int format,422unsigned int width, unsigned int height,423XRectangle bbox, /* bounding box of grabbed area */424list_ptr regions) /* list of regions to read from */425{426image_region_type *reg;427int dst_x, dst_y; /* where in pixmap to write (UL) */428int diff;429430XImage *reg_image,*ximage ;431int srcRect_x,srcRect_y,srcRect_width,srcRect_height ;432int bytes_per_line;433434ximage = XCreateImage(disp,fakeVis,depth,format,0,NULL,width,height,4358,0) ;436bytes_per_line = ximage->bytes_per_line;437438if (format == ZPixmap)439ximage->data = malloc((size_t) height * bytes_per_line);440else441ximage->data = malloc((size_t) height * bytes_per_line * depth);442443ximage->bits_per_pixel = depth; /** Valid only if format is ZPixmap ***/444445for (reg = (image_region_type *) first_in_list( regions); reg;446reg = (image_region_type *) next_in_list( regions))447{448int rect;449struct my_XRegion *vis_reg;450vis_reg = (struct my_XRegion *)(reg->visible_region);451for (rect = 0;452rect < vis_reg->numRects;453rect++)454{455/** ------------------------------------------------------------------------456Intersect bbox with visible part of region giving src rect & output457location. Width is the min right side minus the max left side.458Similar for height. Offset src rect so x,y are relative to459origin of win, not the root-relative visible rect of win.460------------------------------------------------------------------------ **/461srcRect_width = MIN( vis_reg->rects[rect].x2, bbox.width + bbox.x) -462MAX( vis_reg->rects[rect].x1, bbox.x);463srcRect_height = MIN( vis_reg->rects[rect].y2, bbox.height + bbox.y) -464MAX( vis_reg->rects[rect].y1, bbox.y);465diff = bbox.x - vis_reg->rects[rect].x1;466srcRect_x = MAX( 0, diff) + (vis_reg->rects[rect].x1 - reg->x_rootrel - reg->border);467dst_x = MAX( 0, -diff) ;468diff = bbox.y - vis_reg->rects[rect].y1;469srcRect_y = MAX( 0, diff) + (vis_reg->rects[rect].y1 - reg->y_rootrel - reg->border);470dst_y = MAX( 0, -diff) ;471reg_image = XGetImage(disp,reg->win,srcRect_x,srcRect_y,472srcRect_width,srcRect_height,AllPlanes,format) ;473474/* JDK Modification475* Enclose in if test and also call XDestroyImage476*/477if (reg_image) {478TransferImage(disp,reg_image,srcRect_width,479srcRect_height,reg,ximage,dst_x,dst_y) ;480XDestroyImage(reg_image);481}482}483}484return ximage ;485}486487488/** ------------------------------------------------------------------------489------------------------------------------------------------------------ **/490491XImage *ReadAreaToImage(Display *disp,492/* root win on which grab was done */493Window srcRootWinid,494/* root rel UL corner of bounding box of grab */495int x, int y,496/* size of bounding box of grab */497unsigned int width, unsigned int height,498int numVisuals, XVisualInfo *pVisuals,499int numOverlayVisuals, OverlayInfo *pOverlayVisuals,500int numImageVisuals, XVisualInfo **pImageVisuals,501/* list of regions to read from */502list_ptr vis_regions,503/* list of regions to read from */504list_ptr vis_image_regions,505int format, int allImage)506{507image_region_type *reg;508XRectangle bbox; /* bounding box of grabbed area */509int depth ;510XImage *ximage, *ximage_ipm = NULL;511Visual fakeVis ;512int x1, y1;513XImage *image;514#if 0515unsigned char *pmData , *ipmData ;516#endif517int transparentColor, transparentType;518int srcRect_x,srcRect_y,srcRect_width,srcRect_height ;519int diff ;520int dst_x, dst_y; /* where in pixmap to write (UL) */521int pixel;522523bbox.x = x; /* init X rect for bounding box */524bbox.y = y;525bbox.width = width;526bbox.height = height;527528529initFakeVisual(&fakeVis) ;530531depth = 24 ;532ximage = ReadRegionsInList(disp,&fakeVis,depth,format,width,height,533bbox,vis_regions) ;534#if 0535pmData = (unsigned char *)ximage -> data ;536#endif537538/* if transparency possible do it again, but this time for image planes only */539if (vis_image_regions && (vis_image_regions->next) && !allImage)540{541ximage_ipm = ReadRegionsInList(disp,&fakeVis,depth,format,width,height,542bbox,vis_image_regions) ;543#if 0544ipmData = (unsigned char *)ximage_ipm -> data ;545#endif546}547/* Now tranverse the overlay visual windows and test for transparency index. */548/* If you find one, subsitute the value from the matching image plane pixmap. */549550for (reg = (image_region_type *) first_in_list( vis_regions); reg;551reg = (image_region_type *) next_in_list( vis_regions))552{553554if (src_in_overlay( reg, numOverlayVisuals, pOverlayVisuals,555&transparentColor, &transparentType))556{557int test = 0 ;558srcRect_width = MIN( reg->width + reg->x_vis, bbox.width + bbox.x)559- MAX( reg->x_vis, bbox.x);560srcRect_height = MIN( reg->height + reg->y_vis, bbox.height561+ bbox.y) - MAX( reg->y_vis, bbox.y);562diff = bbox.x - reg->x_vis;563srcRect_x = MAX( 0, diff) + (reg->x_vis - reg->x_rootrel - reg->border);564dst_x = MAX( 0, -diff) ;565diff = bbox.y - reg->y_vis;566srcRect_y = MAX( 0, diff) + (reg->y_vis - reg->y_rootrel - reg->border);567dst_y = MAX( 0, -diff) ;568/* let's test some pixels for transparency */569image = XGetImage(disp, reg->win, srcRect_x, srcRect_y,570srcRect_width, srcRect_height, 0xffffffff, ZPixmap);571572/* let's assume byte per pixel for overlay image for now */573if ((image->depth == 8) && (transparentType == TransparentPixel))574{575unsigned char *pixel_ptr;576unsigned char *start_of_line = (unsigned char *) image->data;577578for (y1 = 0; y1 < srcRect_height; y1++) {579pixel_ptr = start_of_line;580for (x1 = 0; x1 < srcRect_width; x1++)581{582if (*pixel_ptr++ == transparentColor)583{584#if 0585*pmData++ = *ipmData++;586*pmData++ = *ipmData++;587*pmData++ = *ipmData++;588#endif589pixel = XGetPixel(ximage_ipm,dst_x+x1,dst_y+y1) ;590XPutPixel(ximage,dst_x+x1, dst_y+y1,pixel);591592if(!test){593test = 1 ;594}595}596#if 0597else {598pmData +=3;599ipmData +=3;600}601#endif602}603start_of_line += image->bytes_per_line;604}605} else {606if (transparentType == TransparentPixel) {607for (y1 = 0; y1 < srcRect_height; y1++) {608for (x1 = 0; x1 < srcRect_width; x1++)609{610int pixel_value = XGetPixel(image, x1, y1);611if (pixel_value == transparentColor)612{613#if 0614*pmData++ = *ipmData++;615*pmData++ = *ipmData++;616*pmData++ = *ipmData++;617#endif618pixel = XGetPixel(ximage_ipm,dst_x+x1,dst_y+y1) ;619XPutPixel(ximage,dst_x+x1, dst_y+y1,pixel);620if(!test){621test = 1 ;622}623}624#if 0625else {626pmData +=3;627ipmData +=3;628}629#endif630}631}632} else {633for (y1 = 0; y1 < srcRect_height; y1++) {634for (x1 = 0; x1 < srcRect_width; x1++)635{636int pixel_value = XGetPixel(image, x1, y1);637if (pixel_value & transparentColor)638{639#if 0640*pmData++ = *ipmData++;641*pmData++ = *ipmData++;642*pmData++ = *ipmData++;643#endif644pixel = XGetPixel(ximage_ipm,dst_x+x1,dst_y+y1) ;645XPutPixel(ximage,dst_x+x1, dst_y+y1,pixel);646if(!test){647test = 1 ;648}649}650#if 0651else {652pmData +=3;653ipmData +=3;654}655#endif656}657}658}659}660XDestroyImage (image);661} /* end of src_in_overlay */662} /** end transparency **/663/* JDK modification - call XDestroyImage if non-null */664if (ximage_ipm != NULL) {665XDestroyImage(ximage_ipm);666}667destroy_region_list( vis_regions);668if (vis_image_regions) destroy_region_list( vis_image_regions );669FreeXVisualInfo(pVisuals, pOverlayVisuals, pImageVisuals);670XSync(disp, 0);671672return ximage;673}674675/** ------------------------------------------------------------------------676Creates a list of the subwindows of a given window which have a677different visual than their parents. The function is recursive.678This list is used in make_region_list(), which coalesces the679windows with the same visual into a region.680image_wins must point to an existing list struct that's already681been zeroed (zero_list()).682------------------------------------------------------------------------ **/683static void make_src_list(Display *disp, list_ptr image_wins,684/* bnding box of area we want */685XRectangle *bbox,686Window curr,687/* pos of curr WRT root */688int x_rootrel, int y_rootrel,689XWindowAttributes *curr_attrs,690/* visible part of curr, not obscurred by ancestors */691XRectangle *pclip)692{693XWindowAttributes child_attrs;694Window root, parent, *child; /* variables for XQueryTree() */695Window *save_child_list; /* variables for XQueryTree() */696unsigned int nchild; /* variables for XQueryTree() */697XRectangle child_clip; /* vis part of child */698int curr_clipX, curr_clipY, curr_clipRt, curr_clipBt;699700/* check that win is mapped & not outside bounding box */701if (curr_attrs->map_state == IsViewable &&702curr_attrs->class == InputOutput &&703!( pclip->x >= (int) (bbox->x + bbox->width) ||704pclip->y >= (int) (bbox->y + bbox->height) ||705(int) (pclip->x + pclip->width) <= bbox->x ||706(int) (pclip->y + pclip->height) <= bbox->y)) {707708XQueryTree( disp, curr, &root, &parent, &child, &nchild );709save_child_list = child; /* so we can free list when we're done */710add_window_to_list( image_wins, curr, x_rootrel, y_rootrel,711pclip->x, pclip->y,712pclip->width, pclip->height,713curr_attrs->border_width,curr_attrs->visual,714curr_attrs->colormap, parent);715716717/** ------------------------------------------------------------------------718set RR coords of right (Rt), left (X), bottom (Bt) and top (Y)719of rect we clip all children by. This is our own clip rect (pclip)720inflicted on us by our parent plus our own borders. Within the721child loop, we figure the clip rect for each child by adding in722it's rectangle (not taking into account the child's borders).723------------------------------------------------------------------------ **/724curr_clipX = MAX( pclip->x, x_rootrel + (int) curr_attrs->border_width);725curr_clipY = MAX( pclip->y, y_rootrel + (int) curr_attrs->border_width);726curr_clipRt = MIN( pclip->x + (int) pclip->width,727x_rootrel + (int) curr_attrs->width +7282 * (int) curr_attrs->border_width);729curr_clipBt = MIN( pclip->y + (int) pclip->height,730y_rootrel + (int) curr_attrs->height +7312 * (int) curr_attrs->border_width);732733while (nchild--) {734int new_width, new_height;735int child_xrr, child_yrr; /* root relative x & y of child */736737XGetWindowAttributes( disp, *child, &child_attrs);738739/* intersect parent & child clip rects */740child_xrr = x_rootrel + child_attrs.x + curr_attrs->border_width;741child_clip.x = MAX( curr_clipX, child_xrr);742new_width = MIN( curr_clipRt, child_xrr + (int) child_attrs.width743+ 2 * child_attrs.border_width)744- child_clip.x;745if (new_width >= 0) {746child_clip.width = new_width;747748child_yrr = y_rootrel + child_attrs.y +749curr_attrs->border_width;750child_clip.y = MAX( curr_clipY, child_yrr);751new_height = MIN( curr_clipBt,752child_yrr + (int) child_attrs.height +7532 * child_attrs.border_width)754- child_clip.y;755if (new_height >= 0) {756child_clip.height = new_height;757make_src_list( disp, image_wins, bbox, *child,758child_xrr, child_yrr,759&child_attrs, &child_clip);760}761}762child++;763}764XFree( save_child_list);765}766}767768769/** ------------------------------------------------------------------------770This function creates a list of regions which tile a specified771window. Each region contains all visible portions of the window772which are drawn with the same visual. For example, if the773window consists of subwindows of two different visual types,774there will be two regions in the list.775Returns a pointer to the list.776------------------------------------------------------------------------ **/777static list_ptr make_region_list(Display *disp, Window win, XRectangle *bbox,778int *hasNonDefault, int numImageVisuals,779XVisualInfo **pImageVisuals, int *allImage)780{781XWindowAttributes win_attrs;782list image_wins;783list_ptr image_regions;784list_ptr srcs_left;785image_region_type *new_reg;786image_win_type *base_src, *src;787Region bbox_region = XCreateRegion();788XRectangle clip;789int image_only;790791int count=0 ;792793*hasNonDefault = False;794XUnionRectWithRegion( bbox, bbox_region, bbox_region);795XGetWindowAttributes( disp, win, &win_attrs);796797zero_list( &image_wins);798clip.x = 0;799clip.y = 0;800clip.width = win_attrs.width;801clip.height = win_attrs.height;802make_src_list( disp, &image_wins, bbox, win,8030 /* x_rootrel */, 0 /* y_rootrel */, &win_attrs, &clip);804805image_regions = new_list();806image_only = (*allImage) ? True:False;807808for (base_src = (image_win_type *) first_in_list( &image_wins); base_src;809base_src = (image_win_type *) next_in_list( &image_wins))810{811/* test for image visual */812if (!image_only || src_in_image(base_src, numImageVisuals, pImageVisuals))813{814/* find a window whose visual hasn't been put in list yet */815if (!src_in_region_list( base_src, image_regions))816{817if (! (new_reg = (image_region_type *)818malloc( sizeof( image_region_type)))) {819return (list_ptr) NULL;820}821count++;822823new_reg->visible_region = XCreateRegion();824new_reg->win = base_src->win;825new_reg->vis = base_src->vis;826new_reg->cmap = base_src->cmap;827new_reg->x_rootrel = base_src->x_rootrel;828new_reg->y_rootrel = base_src->y_rootrel;829new_reg->x_vis = base_src->x_vis;830new_reg->y_vis = base_src->y_vis;831new_reg->width = base_src->width;832new_reg->height = base_src->height;833new_reg->border = base_src->border_width;834835srcs_left = (list_ptr) dup_list_head( &image_wins, START_AT_CURR);836for (src = (image_win_type *) first_in_list( srcs_left); src;837src = (image_win_type *) next_in_list( srcs_left)) {838if (SAME_REGIONS( base_src, src)) {839add_rect_to_image_region( new_reg, src->x_vis, src->y_vis,840src->width, src->height);841}842else {843if (!image_only || src_in_image(src, numImageVisuals, pImageVisuals))844{845subtr_rect_from_image_region( new_reg, src->x_vis,846src->y_vis, src->width, src->height);847}848}849}850XIntersectRegion( bbox_region, new_reg->visible_region,851new_reg->visible_region);852if (! XEmptyRegion( new_reg->visible_region)) {853add_to_list( image_regions, new_reg);854if (new_reg->vis != DefaultVisualOfScreen( win_attrs.screen) ||855new_reg->cmap != DefaultColormapOfScreen(856win_attrs.screen)) {857*hasNonDefault = True;858}859}860else {861XDestroyRegion( new_reg->visible_region);862free( (void *) new_reg);863}864}865} else *allImage = 0;866}867delete_list( &image_wins, True);868XDestroyRegion( bbox_region);869return image_regions;870}871/** ------------------------------------------------------------------------872Destructor called from destroy_region_list().873------------------------------------------------------------------------ **/874static void destroy_image_region(image_region_type *image_region)875{876XDestroyRegion( image_region->visible_region);877free( (void *) image_region);878}879880/** ------------------------------------------------------------------------881Destroys the region list, destroying all the regions contained in it.882------------------------------------------------------------------------ **/883static void destroy_region_list(list_ptr rlist)884{885delete_list_destroying( rlist, (DESTRUCT_FUNC_PTR)destroy_image_region);886}887888889/** ------------------------------------------------------------------------890Subtracts the specified rectangle from the region in image_region.891First converts the rectangle to a region of its own, since X892only provides a way to subtract one region from another, not a893rectangle from a region.894------------------------------------------------------------------------ **/895static void subtr_rect_from_image_region(image_region_type *image_region,896int x, int y, int width, int height)897{898XRectangle rect;899Region rect_region;900901rect_region = XCreateRegion();902rect.x = x;903rect.y = y;904rect.width = width;905rect.height = height;906XUnionRectWithRegion( &rect, rect_region, rect_region);907XSubtractRegion( image_region->visible_region, rect_region,908image_region->visible_region);909XDestroyRegion( rect_region);910}911912913/** ------------------------------------------------------------------------914Adds the specified rectangle to the region in image_region.915------------------------------------------------------------------------ **/916static void add_rect_to_image_region(image_region_type *image_region,917int x, int y, int width, int height)918{919XRectangle rect;920921rect.x = x;922rect.y = y;923rect.width = width;924rect.height = height;925XUnionRectWithRegion( &rect, image_region->visible_region,926image_region->visible_region);927}928929930/** ------------------------------------------------------------------------931Returns TRUE if the given src's visual is already represented in932the image_regions list, FALSE otherwise.933------------------------------------------------------------------------ **/934static int src_in_region_list(image_win_type *src, list_ptr image_regions)935{936image_region_type *ir;937938for (ir = (image_region_type *) first_in_list( image_regions); ir;939ir = (image_region_type *) next_in_list( image_regions)) {940if (SAME_REGIONS( ir, src)) {941942return 1;943}944}945946return 0;947}948949950/** ------------------------------------------------------------------------951Makes a new entry in image_wins with the given fields filled in.952------------------------------------------------------------------------ **/953static void add_window_to_list(list_ptr image_wins, Window w,954int xrr, int yrr, int x_vis, int y_vis,955int width, int height, int border_width,956Visual *vis, Colormap cmap, Window parent)957{958image_win_type *new_src;959960if ((new_src = (image_win_type *) malloc( sizeof( image_win_type))) == NULL)961962return;963964new_src->win = w;965new_src->x_rootrel = xrr;966new_src->y_rootrel = yrr;967new_src->x_vis = x_vis;968new_src->y_vis = y_vis;969new_src->width = width;970new_src->height = height;971new_src->border_width = border_width;972new_src->vis = vis;973new_src->cmap = cmap;974new_src->parent = parent;975add_to_list( image_wins, new_src);976}977978/** ------------------------------------------------------------------------979Returns TRUE if the given src's visual is in the image planes,980FALSE otherwise.981------------------------------------------------------------------------ **/982static int src_in_image(image_win_type *src, int numImageVisuals,983XVisualInfo **pImageVisuals)984{985int i;986987for (i = 0 ; i < numImageVisuals ; i++)988{989if (pImageVisuals[i]->visual == src->vis)990return 1;991}992return 0;993}994995996/** ------------------------------------------------------------------------997Returns TRUE if the given src's visual is in the overlay planes998and transparency is possible, FALSE otherwise.999------------------------------------------------------------------------ **/1000static int src_in_overlay(image_region_type *src, int numOverlayVisuals,1001OverlayInfo *pOverlayVisuals,1002int *transparentColor, int *transparentType)1003{1004int i;10051006for (i = 0 ; i < numOverlayVisuals ; i++)1007{1008if (((pOverlayVisuals[i].pOverlayVisualInfo)->visual == src->vis)1009&& (pOverlayVisuals[i].transparentType != None))1010{1011*transparentColor = pOverlayVisuals[i].value;1012*transparentType = pOverlayVisuals[i].transparentType;1013return 1;1014}10151016else {1017}10181019}1020return 0;1021}102210231024/********************** from wsutils.c ******************************/10251026/******************************************************************************1027*1028* This file contains a set of example utility procedures; procedures that can1029* help a "window-smart" Starbase or PHIGS program determine information about1030* a device, and create image and overlay plane windows. To use these1031* utilities, #include "wsutils.h" and compile this file and link the results1032* with your program.1033*1034******************************************************************************/1035103610371038#define STATIC_GRAY 0x011039#define GRAY_SCALE 0x021040#define PSEUDO_COLOR 0x041041#define TRUE_COLOR 0x101042#define DIRECT_COLOR 0x11104310441045static int weCreateServerOverlayVisualsProperty = False;104610471048/******************************************************************************1049*1050* GetXVisualInfo()1051*1052* This routine takes an X11 Display, screen number, and returns whether the1053* screen supports transparent overlays and three arrays:1054*1055* 1) All of the XVisualInfo struct's for the screen.1056* 2) All of the OverlayInfo struct's for the screen.1057* 3) An array of pointers to the screen's image plane XVisualInfo1058* structs.1059*1060* The code below obtains the array of all the screen's visuals, and obtains1061* the array of all the screen's overlay visual information. It then processes1062* the array of the screen's visuals, determining whether the visual is an1063* overlay or image visual.1064*1065* If the routine sucessfully obtained the visual information, it returns zero.1066* If the routine didn't obtain the visual information, it returns non-zero.1067*1068******************************************************************************/10691070int GetXVisualInfo(/* Which X server (aka "display"). */1071Display *display,1072/* Which screen of the "display". */1073int screen,1074/* Non-zero if there's at least one overlay visual and1075* if at least one of those supports a transparent pixel. */1076int *transparentOverlays,1077/* Number of XVisualInfo struct's pointed to by pVisuals. */1078int *numVisuals,1079/* All of the device's visuals. */1080XVisualInfo **pVisuals,1081/* Number of OverlayInfo's pointed to by pOverlayVisuals.1082* If this number is zero, the device does not have1083* overlay planes. */1084int *numOverlayVisuals,1085/* The device's overlay plane visual information. */1086OverlayInfo **pOverlayVisuals,1087/* Number of XVisualInfo's pointed to by pImageVisuals. */1088int *numImageVisuals,1089/* The device's image visuals. */1090XVisualInfo ***pImageVisuals)1091{1092XVisualInfo getVisInfo; /* Paramters of XGetVisualInfo */1093int mask;1094XVisualInfo *pVis, **pIVis; /* Faster, local copies */1095OverlayInfo *pOVis;1096OverlayVisualPropertyRec *pOOldVis;1097int nVisuals, nOVisuals;1098Atom overlayVisualsAtom; /* Parameters for XGetWindowProperty */1099Atom actualType;1100unsigned long numLongs, bytesAfter;1101int actualFormat;1102int nImageVisualsAlloced; /* Values to process the XVisualInfo */1103int imageVisual; /* array */110411051106/* First, get the list of visuals for this screen. */1107getVisInfo.screen = screen;1108mask = VisualScreenMask;11091110*pVisuals = XGetVisualInfo(display, mask, &getVisInfo, numVisuals);1111if ((nVisuals = *numVisuals) <= 0)1112{1113/* Return that the information wasn't sucessfully obtained: */1114return(1);1115}1116pVis = *pVisuals;111711181119/* Now, get the overlay visual information for this screen. To obtain1120* this information, get the SERVER_OVERLAY_VISUALS property.1121*/1122overlayVisualsAtom = XInternAtom(display, "SERVER_OVERLAY_VISUALS", True);1123if (overlayVisualsAtom != None)1124{1125/* Since the Atom exists, we can request the property's contents. The1126* do-while loop makes sure we get the entire list from the X server.1127*/1128bytesAfter = 0;1129numLongs = sizeof(OverlayVisualPropertyRec) / sizeof(long);1130do1131{1132numLongs += bytesAfter * sizeof(long);1133XGetWindowProperty(display, RootWindow(display, screen),1134overlayVisualsAtom, 0, numLongs, False,1135overlayVisualsAtom, &actualType, &actualFormat,1136&numLongs, &bytesAfter, (unsigned char**) pOverlayVisuals);1137} while (bytesAfter > 0);113811391140/* Calculate the number of overlay visuals in the list. */1141*numOverlayVisuals = numLongs / (sizeof(OverlayVisualPropertyRec) / sizeof(long));1142}1143else1144{1145/* This screen doesn't have overlay planes. */1146*numOverlayVisuals = 0;1147*pOverlayVisuals = NULL;1148*transparentOverlays = 0;1149}115011511152/* Process the pVisuals array. */1153*numImageVisuals = 0;1154nImageVisualsAlloced = 1;1155pIVis = *pImageVisuals = (XVisualInfo **) malloc(sizeof(XVisualInfo *));1156while (--nVisuals >= 0)1157{1158nOVisuals = *numOverlayVisuals;1159pOVis = *pOverlayVisuals;1160imageVisual = True;1161while (--nOVisuals >= 0)1162{1163pOOldVis = (OverlayVisualPropertyRec *) pOVis;1164if (pVis->visualid == pOOldVis->visualID)1165{1166imageVisual = False;1167pOVis->pOverlayVisualInfo = pVis;1168if (pOVis->transparentType == TransparentPixel)1169*transparentOverlays = 1;1170}1171pOVis++;1172}1173if (imageVisual)1174{1175if ((*numImageVisuals += 1) > nImageVisualsAlloced)1176{1177nImageVisualsAlloced++;1178*pImageVisuals = (XVisualInfo **)1179realloc(*pImageVisuals, (nImageVisualsAlloced * sizeof(XVisualInfo *)));1180pIVis = *pImageVisuals + (*numImageVisuals - 1);1181}1182*pIVis++ = pVis;1183}1184pVis++;1185}118611871188/* Return that the information was sucessfully obtained: */1189return(0);11901191} /* GetXVisualInfo() */119211931194/******************************************************************************1195*1196* FreeXVisualInfo()1197*1198* This routine frees the data that was allocated by GetXVisualInfo().1199*1200******************************************************************************/12011202void FreeXVisualInfo(XVisualInfo *pVisuals, OverlayInfo *pOverlayVisuals,1203XVisualInfo **pImageVisuals)1204{1205XFree(pVisuals);1206if (weCreateServerOverlayVisualsProperty)1207free(pOverlayVisuals);1208else1209XFree(pOverlayVisuals);1210free(pImageVisuals);12111212} /* FreeXVisualInfo() */121312141215