/* 
 * -- $Header: /cec/src/nestor/shX/shXlib/X11/XmuXstructs.h,v 1.2 90/11/29 10:06:22 altenhof Exp $
 */

#ifndef _XMUXSTRUCTS_H_
#define _XMUXSTRUCTS_H_

#include <X11/Xlib.h>

#ifndef MSKCNT
#include "Xlibos.h"            /* MSKCNT is defined here */
#endif

typedef struct _MUXToplevelInfo {
    Window        window ;
    long          mux_mask ;

} MUXToplevelInfo ;

/*
 * -- this is our central data structure; 
 */
typedef struct _MUXServer {
    Display *xdpy ;               /* the corresponding Display pointer */

    Bool     mustMux ,            /* do we have to multiplex requests from this
				     connection (used in XmuXGetDisplay) */ 
             storeResources ;     /* do we have to keep track of allocated 
				     resources ? */
    int      index ,              /* index to the Application context array */
             prim_server ;         /* index of the "primary" MUXServerRec 
                                      (contains the Display that is known 
				      to the client) in the connection 
				      translation table */
				 
    long     other_servers[ MSKCNT ] ;  /* bit array containing all 
					   indexes to other MUXServerRec 
					   in the connection trans-
				           lation table that are used for this 
					   application */
    long  all_depths ,                   /* all depths that are supported by
					    this connection */
          vis_depths ;                   /* all depths that supply visuals */
    unsigned char last_error_code ,
                  last_request_code ,
                  last_minor_code ;
    Atom  atoms[ MAXATOM ] ;
} MUXServerRec,
  * MUXServerPtr;

#ifdef NEED_RESOURCES

/* -- convenience definitions for null pointer casting -- */

#define NullWindowInfoPtr   ( MUXWindowInfoPtr ) ( 0 )
#define NullGCInfoPtr       ( MUXGCInfoPtr ) ( 0 )
#define NullPixmapInfoPtr   ( MUXPixmapInfoPtr ) ( 0 )
#define NullCursorInfoPtr   ( MUXCursorInfoPtr ) ( 0 )
#define NullFontInfoPtr     ( MUXFontInfoPtr ) ( 0 )
#define NullColormapInfoPtr ( MUXColormapInfoPtr ) ( 0 )
#define NullGCPixelsInfoPtr ( MUXGCPixelsInfoPtr ) ( 0 )
#define NullResourceInfoPtr ( MUXResourceInfoPtr ) ( 0 )

/* -- our six resource types -- */

#define RT_WINDOW    ( 1L << 0 )
#define RT_SUBWINDOW ( 1L << 1 )
#define RT_GC        ( 1L << 2 )
#define RT_PIXMAP    ( 1L << 3 )
#define RT_CURSOR    ( 1L << 4 )
#define RT_FONT      ( 1L << 5 )
#define RT_COLORMAP  ( 1L << 6 )

/*
 * -- artificial resource type; see MUXPixelInfo
 */
#define RT_GCPIXELS    ( 1L << 7 )   

#define RT_INVALID     ( 1L << 8 )

/* -- the two cursor types -- */

#define PIXMAP_CURSOR 0
#define GLYPH_CURSOR  1

#define ButtonGrabbed 1L
#define KeyGrabbed    2L

/*  
 * -- Now follow the definitions of our data structures that store
 * -- the information about each resource type we need to know "in advance"
 * -- (i.e. that cannot be retrieved from the server via Xlib routines)
 */

/*
 * -- two special structs holding parameters for distributing
 * -- passive Button/Key grabs (see comments below) 
 */

typedef struct _GrabButtonParams {
    /* the parameters XGrabButton is called with */
    unsigned int button ,     
                 modifiers ,
                 event_mask ;
    Bool         owner_events ;
    int          pointer_mode ,
                 keyboard_mode ;
    XID          confine_to ,
                 cursor ;
    struct  _GrabButtonParams *next ; /* a window may grab more than one */

} GrabButtonParams ;

typedef struct _GrabKeyParams {
    /* the parameters XGrabKey is called with */
    int          key ;
    unsigned int modifiers ;
    Bool         owner_events ;
    int          pointer_mode ,
                 keyboard_mode ;
    struct _GrabKeyParams *next ; /* window may grab more than one */

} GrabKeyParams ;

/*
 * -- What we need to know about a WINDOW :
 * --  -> the window ID
 * --  -> the parent ID ( needed in XCreateWindow )
 * --  -> the visual id 
 * --  -> the background pixmap
 * --  -> the border pixmap 
 * --  -> the cursor 
 * --  -> the border pixel
 * --  -> the background pixel
 * --  -> the window depth 
 * --  All other window attributes can be retrieved via XGetWindowAttributes,
 * --  BUT :
 * --  Window may be involved in Pointer/Keyboard grabbing due to calls to
 * --  XGrabButton or XGrabKey; these functions establish so-called "passive"
 * --  grabs which turn to active grabs when certain key/button combinations
 * --  are pressed. In our context, these grabs deserve the name "hidden"
 * --  grabs, since there are no functions in Xlib that allow to determine
 * --  whether some passive grabs are in effect. So, we have some more "window
 * --  attributes" that we cannot retrieve from the server !
 * --  -> a grab flag field ( is this window involved in passive 
 * --     Pointer/Keyboard grabs 
 * --  -> two pointers to structs containing all parameters that we need to 
 * --     call XGrabButtonb/XGrabKey again ( In fact to lists of structs, 
 * --     since there are more than one button/key )
 * --  NOTE :
 * --  Windows are one resource type that reference other resources in their 
 * --  attribute list. Since this can disturb the "strict allocation history",
 * --  we use a flag field to indicate that other resources are referenced.
 * --  This flag field is used in the resource duplication step.
 * --  AND :
 * --  Windows are referenced by other resources on creation 
 * --  (GCs, pixmaps, colormaps) to determine the suitable screen/depth.
 * --  There may be applications that still use such "window-dependend" 
 * --  resources although the window that they reference is gone !
 * --  In this case we must try to find a "best-match" window, i.e. with 
 * --  the same depth on the same screen ! Same screen is no problem since
 * --  we duplicate all resources on the default screen, but the depth must
 * --  be stored.
 */
typedef struct _WindowInfo {

    XID            window ,            /* the window ID */
                   visualid ,          /* the visual id */
                   parent ,            /* the window's parent ID */
                   bg_pmap ,           /* the window's background pixmap */
                   border_pmap ,       /* the window's border pixmap */
                   cursor ,            /* the window's cursor */
                   colormap ; 
    unsigned long  flags ,             /* are other resources referenced ? */
                   grabs ,             /* does this window passively grabs
					  a button or a key */
                   border_pixel ,      /* the window's border_pixel */
                   bg_pixel ;          /* the window's bg_pixel */
    int            depth ;             /* the window's depth */
    int            map_state ;         /* we only map those windows in
					  UpdateResourceWindowAttributes whose
					  window attribute map_state is 
					  IsViewable (see multiplex.c) */
    int            hidden;             /* is this window tagged as unviewable
					  useful for selective window sharing */
    GrabButtonParams *button_grabs ;
    GrabKeyParams    *key_grabs ;
					  
} MUXWindowInfo , *MUXWindowInfoPtr ;

/*
 * -- What we need to know about a GC :
 * --  -> ALL GC attributes ! 
 * --  -> the ID of the drawable the gc was created on ( determines depth and
 * --      screen ) (watch out, may be gone! see comments about window info )
 * --  -> the depth 
 * -- There is no Xlib routine to get information about a graphics context !
 * -- We actually store the pointer to the GC structure.
 * --
 * --  NOTE :
 * --  GCs are one resource type that reference other resources in their 
 * --  attribute list. Since this can disturb the "strict allocation history",
 * --  we use a flag field to indicate that other resources are referenced.
 * --  This flag field is used in the resource duplication step.
 * --  Depth value is used to find the "best_matching" drawable, if the 
 * --  drawable we reference in the drawable field is gone.
 */

typedef struct _GCInfo {

    XID gid ,                /* the GC ID */
        drawable ;           /* the drawable the gc was created on */
    int depth ;              /* the drawable's (and the GC's) depth */
    unsigned long flags ;    /* are other resources referenced ? */
    GC  gc ;                 /* pointer to GC structure
				( not only the GCValues ) */

} MUXGCInfo , *MUXGCInfoPtr ;

/*
 * -- What we need to know about a PIXMAP :
 * --  -> the pixmap ID
 * --  -> the drawable ID that was passed to XCreatePixmap
 * --  -> the depth 
 * --  The pixmap attributes width , height are looked up by 
 * --  XGetGeometry
 * --  The "contents" of the pixmap can be retrieved via XGetImage
 * --  ( pixmaps are drawables ! )
 * --  NOTE :
 * --  Same problem as with windows concerning depth
 * --  ( see comments there ! )
 */
typedef struct _PixmapInfo {

    XID           pixmap ,             /* the pixmap ID */
                  drawable ;           /* the drawable ID passed to 
					  XCreatePixmap */
    int           depth ;              /* pixmap depth */

} MUXPixmapInfo , *MUXPixmapInfoPtr ;

/*
 * -- What we need to know about a CURSOR :
 * --  -> the cursor ID
 * --  -> the cursor type ( pixmap or glyph cursor )
 * --  -> ALL other type specific cursor attributes !
 * --  Like for GC's there is no Xlib routine to get information about a cursor !
 */
typedef struct _CursorInfo {

    XID     cursor ;         /* the cursor ID */
    int     cursor_type ;    /* the cursor type : PIXMAP_CURSOR or GLYPHCURSOR */
    XColor  fg ,             /* both use a foreground and */
            bg ;             /* a background color */
    union {                  /* type specific data */
	
	struct {                 /* pixmap cursors are created from : */
	    XID  source_pmap ,   /* -> a source pixmap */ 
	         mask_pmap ;     /* -> a mask pixmap */
	    int  hot_x ,         /* they define a hotspot */
	         hot_y ;
	} pixmap_cursor ;
	
	struct {                 /* glyph cursors are created from : */

	    XID           source_font , /* -> a source font */
	                  mask_font ;   /* -> a mask font */
	    unsigned int  source_char , /* -> a source "glyph" (character) */
	                  mask_char ;   /* -> a mask "glyph" (character) */
	} glyph_cursor ;

    } u ;

} MUXCursorInfo , *MUXCursorInfoPtr ;

/* 
 * -- What we need to know about a FONT :
 * --  -> the font ID
 * --  -> the font name 
 * --  This is the only information we need to open a font 
 */
typedef struct _FontInfo {

    XID  font ;            /* the font ID */
    char *name ;           /* it's name */

} MUXFontInfo , *MUXFontInfoPtr ;

/*
 * -- Additional struct to hold information about PRIVATE allocated color cells
 */

typedef struct _PrivateColorCells {
    int alloc_cells ,     /* called Xlib routine == XAllocColorCells */
	n_pixels ,        /* number of pixels allocated in 
			     XAllocColorCells/Planes */
        n_planes ;        /* number of planes allocated in 
			     XAllocColorCells/Planes */
    unsigned long *pixels , /* the pixel values returned by 
			       XAllocColorCells/Planes */
                  *planes ; /* the plane_masks returned by 
			       XAllocColorCells/Planes */
    struct _PrivateColorCells *next ;
} MUXPrivateColorCells ;

/*
 * -- What we need to know about a COLORMAP :
 * -- -> the window ID the colormap was created with ( watch out! may be gone!)
 * -- -> the corresponding visual id
 * -- -> a flag field indicating whether this colormap is a private one
 * -- -> the SHARED color cells that have been allocated by the client
 * -- -> the list of PRIVATE color cells that have been allocated by the client
 * --  Colormaps are the third resource type that are "hidden" by the server !
 */ 
typedef struct _ColormapInfo {

    XID           colormap ,   /* the colormap ID -> used to identify this
				  resource info */
                  window ,
                  visualid ;
    Bool          is_private ;
    int           sh_length ,     /* the current length of the shared cells
				     array */
                  sh_allocated ;  /* the number of allocated cells in the cell
				     array */
    unsigned long *sh_cells ;     /* shares color cells (pixels) allocated */
    MUXPrivateColorCells *pr_cells ; /* pointer to the list of private 
					color cells */

} MUXColormapInfo , *MUXColormapInfoPtr ;

/* 
 * -- Want we need to know about GC's on MULTIPLEX'ED CONNECTIONS :
 * -- We introduce an artificial resource info type here that keeps
 * -- track of changes in the foreground/background(/plane_mask) values
 * -- on multiplexed connections. These values are critical, since they
 * -- refer to a colormap which is unknown until we use the GC in the
 * -- next graphic request; normally (i.e. when using only one -the default-
 * -- colormap), this won't harm, but if clients install and use own
 * -- colormaps, we will have to prologue each graphic request with an
 * -- XmuXFlushGC. The point here is, that we cannot rely on the values
 * -- stored in the application's GC cache, since colors may have been reset
 * -- in the meantime (e.g. bitmap does this all the time: it's nice to 
 * -- look on a black on black bitmap!). So, every time we hear a "Change GC"
 * -- request on the wire, we store the changed color values in this
 * -- resource info.
 * -- We need the following informations :
 * --  -> the "mapped" GC resource id ( to identify the GC )
 * --  -> a flag field to indicate which values have to be considered
 * --  -> the foreground / background pixel values (from the ChangeGC request)
 * --  -> the function and plane_mask field 
 * --     ( we dream of multiplexing real PseudoColor clients)
 */

typedef struct _GCPixelInfo {

    XID           gid ;        /* -- the mapped GC resource id -- */
    long          flags ;      /* -- flag field -- */
    unsigned long function ,
                  fg ,
                  bg ,
                  plane_mask ;

} MUXGCPixelsInfo , *MUXGCPixelsInfoPtr ;


/*
 * -- our central data structure used to store resource information :
 * --  -> a type field tells us which resource specific data is stored in
 * --     this list entry
 * --  -> an resource ID ( see above ) to find this entry in the list 
 * --  -> a pointer to the type specific data 
 */
typedef struct _ResourceInfo  {

    long type ;                         /* which resource type */
                                        /* (union tag field) */
    XID id ;                            /* resource ID used as the key */
    int used ;                          /* this entry is in use */
    union {                             /* pointers to resource specific data */
	MUXWindowInfoPtr      win_info ;
	MUXGCInfoPtr          gc_info ;
	MUXPixmapInfoPtr      pmap_info ;
	MUXCursorInfoPtr      curs_info ;
	MUXFontInfoPtr        font_info ;
	MUXColormapInfoPtr    cmap_info ;
	MUXGCPixelsInfoPtr    gcpix_info ;
    } u ;
    struct _ResourceInfo *next ;

} MUXResourceInfo , * MUXResourceInfoPtr ;

/*
 * -- for every client we maintain a list about his allocated resources 
 * -- it is sorted with respect to the creation time by using the "last in"
 * -- mechanism (that's why we use a pointer to the tail of the list).
 */
typedef struct _ResourceList {

    MUXResourceInfoPtr  head ,
                        tail ;
    int update ,                   /* flag indicating that the resource list 
				      needs updating */
        privcmaps ;                 /* accelorator field to determine if
				       client uses private colormaps */
				     
} MUXResourceList ;

MUXResourceList  MUXClientResources[ MAXSOCKS ] ;

#endif

#endif
