/*
 * @(#)dga.h 1.9 91/09/14
 */

/*
 * Copyright (c) 1991 by Sun Microsystems, Inc.
 */

/*
 * dga.h - Sun Direct Graphics Access interface
 */


#ifndef _DGA_H
#define _DGA_H

#include <sys/types.h>

#define	DGA_X_EOL		(-32767)
#define	DGA_Y_EOL		(-32768)

#define DGA_DB_DEV_INFO_SZ 132

#define DGA_RTN_NOT_CACHED      0
#define DGA_RTN_SAME_DEV        1
#define DGA_RTN_NEW_DEV         2


typedef u_int   *Dga_lockp;

typedef u_int Dga_token;

typedef struct dga_curs_memimage {
	u_int		width;
	u_int		height;
	u_int		depth;
	u_int		linebytes;
	caddr_t		memptr;
} Dga_curs_memimage;

typedef struct dga_dbinfo {
	short		number_buffers;
	short		read_buffer;
	short		write_buffer;
	short		display_buffer;
	u_int		reserved_1;	/* for the future */
	u_int		reserved_2;	/* for the future */
	u_int		reserved_3;	/* for the future */
	u_int		reserved_4;	/* for the future */
	u_int		reserved_5;	/* for the future */
	u_int		wid;		/* db window id. */
	u_int		reserved_6;	/* for the future */
	u_char		device_info[DGA_DB_DEV_INFO_SZ];
} Dga_dbinfo;

typedef struct dga_widinfo {
	short		number_wids;	/* number of contiguous wids alloc */
	short		start_wid;	/* starting wid */
	short		wid;		/* current wid */
	short		reserved_1;	/* for the future */
	} Dga_widinfo;

typedef	struct dga_window {
	caddr_t		w_info;		/* pointer to shared memory */
	u_int		c_chngcnt;	/* last recorded global change count */
	u_int		*s_chngcnt_p;	/* ptr to server's global change cnt */
	int		w_lockcnt;	/* lock count for nested locks */
	Dga_lockp	w_lockp;	/* pointer to lock page */
	Dga_lockp	w_unlockp;	/* points to unlock page */
	int		(*w_lock_func)(); /* alternate function for locking */
	int		(*w_unlock_func)(); /* alternate func for unlocking */
	int		(*w_update_func)(); /* function to call for updates */
	int		w_modif;	/* cached result of update check */
	u_int		c_clipseq;	/* last recorded clip change count */
	u_int		*s_clipseq_p;	/* ptr to server's clip change count */
	short		*w_clipptr;	/* client virtual pointer to */
					/* clip array. */
	u_int		w_cliplen;	/* client's size of clip list */
					/* in bytes. */
	Dga_token	w_token;	/* token used to grab window */
	int		w_infofd;	/* fd of shared memory file */
	int		w_devfd;	/* fd of framebuffer */
	int		w_grab_count;	/* how many times we've grabbed it */
	struct dga_window *w_next;	/* used to link all grabbed windows */
	caddr_t		w_lib_private;	/* library private data */
	caddr_t		w_client;	/* for client use */

	/* cursor grabber info */
	caddr_t		curs_info;	/* pointer to client's cursor info */
	int		curs_fd;	/* Client side file descriptor */
	u_int		c_curseq;	/* last recorded cursor change count */
	u_int		*s_curseq_p;	/* ptr to server's cursor change cnt */

	/* double buffering info */
	u_int		db_enabled;	/* flag to tell if initialized */
	u_int		*db_vrtcntrp;	/* Client's ptr to VRT counter */
	u_int		db_lastvrtcntr;	/* VRT counter value at last flip */
	u_int		db_swapint;	/* number of intervals between flips */

	/* retained window grabber info */
	int		rtn_fd;		/* Client side fd for shared memory */
	u_int		rtn_size;	/* Client size of pixel block */
					/* in bytes */
	u_int		c_rtnseq;	/* Client rtn modification counter */
	u_int		*s_rtnseq_p;	/* Ptr to server's rtn mod counter */
	u_int		c_rtncachseq;	/* Client device cache */
					/* modification counter */
	u_int		*s_rtncachseq_p; /* Ptr to server's device cache */
					/* modification counter */
	caddr_t		rtn_info;	/* Ptr to Shared memory structure */
	caddr_t		rtn_pixels;	/* Client ptr to Client pixel mapping */
	u_int		rtn_flags;	/* to record state of retained grab */

} *Dga_window;


typedef	struct dga_cmap {
	caddr_t  	cm_info;	/* pointer to shared memory */
	u_int		c_chngcnt;	/* last recorded change count */
	u_int		*s_chngcnt_p;	/* last recorded change count */
	int		cm_lockcnt;	/* lock count for nested locks */
	u_int		*cm_lockp;	/* pointer to lock page */
	u_int		*cm_unlockp;	/* pointer to unlock page */
	int		cm_use_new_ioctl; /* use new hardware ioctl? */
	Dga_token	cm_token;	/* token used to grab cmap */
	int		cm_infofd;	/* fd of shared memory file */
	int		cm_devfd;	/* fd of framebuffer */
	int		cm_lockfd;	/* fd of lock device
					   (if not framebuffer) */
	int		cm_sunwindows_def_fd; /* client fd for server's
					   SunWindows default cmap window */
	int		cm_sunwindows_inst_fd; /* client fd for server's
					   SunWindows installed cmap window */
	int		cm_grab_count;	/* how many times we've grabbed it */
	struct dga_cmap *cm_next;	/* used to link all grabbed cmaps */
	caddr_t		cm_lib_private;	/* library private data */
	caddr_t		cm_client;	/* for client use */
} *Dga_cmap;


/*
 *  Window grabber info macros
 */

#define DGA_LOCK(x)	(*((x)->w_lockp)) = 1
#define DGA_UNLOCK(x)	(*((x)->w_unlockp)) = 0

#define	DGA_WIN_LOCK(win)						\
    do {								\
    if (((win)->w_lockcnt)++ == 0) {					\
	DGA_LOCK(win);							\
	win->w_modif = ((((win)->c_chngcnt) == (*((win)->s_chngcnt_p))) ? \
			   0 : (*((win)->w_update_func))(win));		\
	}								\
    } while (0)

#define DGA_WIN_UNLOCK(win)			\
    do {					\
    if (--((win)->w_lockcnt) == 0) { 		\
	DGA_UNLOCK(win);			\
	if ((win)->w_unlock_func)		\
		(*((win)->w_unlock_func))(win);	\
	}					\
    } while (0)

#define	DGA_WIN_MODIF(win)	(win->w_modif)



/*
 *  X protocol extension functions
 */

extern	Dga_token	XDgaGrabWindow();
extern	int		XDgaUnGrabWindow();
extern	Dga_token	XDgaGrabColormap();
extern	int		XDgaUnGrabColormap();
extern	int		XDgaGrabWids();
extern	int		XDgaGrabFCS();
extern	int		XDgaGrabZbuf();
extern	int		XDgaGrabStereo();
extern	int		XDgaGrabBuffers();
extern	int		XDgaUnGrabBuffers();
extern	int		XDgaGrabRetainedWindow();
extern	int		XDgaUnGrabRetainedWindow();
extern	int		XDgaGetRetainedPath();
extern	int		XDgaQueryVersion();


/*
 *  Window grabber functions
 */

extern	Dga_window	dga_win_grab();
extern	void		dga_win_ungrab();
extern	char		*dga_win_fbname();
extern	int		dga_win_devfd();
extern	int		dga_win_clipchg();
extern	int		dga_win_curschg();
extern	int		dga_win_rtnchg();
extern	short		*dga_win_clipinfo();
extern	void		dga_win_bbox();
extern	int		dga_win_singlerect();
extern	int		dga_win_empty();
extern	int		dga_win_obscured();
extern	int		dga_win_cursactive();
extern	void		dga_win_cursupdate();
extern	Dga_dbinfo	*dga_win_dbinfop();
extern	Dga_widinfo	*dga_win_widinfop();


/*
 *  Colormap grabber functions
 */

extern	Dga_cmap	dga_cm_grab();
extern	void		dga_cm_ungrab();
extern	void		dga_cm_get();
extern	void		dga_cm_put();


/*
 *  double buffering functions
 */

extern	int		dga_db_access();
extern	void		dga_db_release();
extern	void		dga_db_write();
extern	void		dga_db_read();
extern	void		dga_db_display();
extern	void		dga_db_interval();
extern	void		dga_db_interval_wait();
extern	int		dga_db_interval_check();
extern	int		dga_db_write_inquire();
extern	int		dga_db_read_inquire();
extern	int		dga_db_display_inquire();
extern	int		dga_db_display_complete();


/*
 *  Retained window grabber functions
 */

extern	int		dga_rtn_grab();
extern	int		dga_rtn_ungrab();
extern	int		dga_rtn_active();
extern	int		dga_rtn_cached();
extern	caddr_t		dga_rtn_devinfo();
extern	void		dga_rtn_devtype();
extern	void		dga_rtn_dimensions();
extern	caddr_t		dga_rtn_pixels();


#endif /* _DGA_H */
