//      Holds system header includes, prototypes of xwin driver
//      utility functions, and definition of the state structure.
//

#ifndef __PLXWD_H__
#define __PLXWD_H__

#include "plplot.h"
#include "plstrm.h"
#ifdef PL_HAVE_PTHREAD
#include <pthread.h>
#endif

// System headers

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>

// Specify max number of displays in use

#define PLXDISPLAYS    100

// Set constants for dealing with colormap.  In brief:
//
// ccmap		When set, turns on custom color map
//
// See Init_CustomCmap() and  Init_DefaultCmap() for more info.
// Set ccmap at your own risk -- still under development.
//

static int plplot_ccmap = 0;

// One of these holds the display info, shared by all streams on a given
// display

typedef struct
{
    int      nstreams;                  // Number of streams using display
    int      ixwd;                      // Specifies xwDisplay number
    char     *displayName;              // Name of X display
    int      screen;                    // X screen
    Display  *display;                  // X display
    Visual   *visual;                   // X Visual
    GC       gcXor;                     // Graphics context for XOR draws
    Colormap map;                       // Colormap
    unsigned depth;                     // display depth
    int      color;                     // Set to 1 if a color output device
    int      ncol0;                     // Number of cmap 0 colors
    int      ncol0_alloc;               // Keeps track of storage for *cmap0
    int      ncol1;                     // Number of cmap 1 colors
    int      ncol1_alloc;               // Keeps track of storage for *cmap1
    XColor   *cmap0;                    // Color entries for cmap 0
    XColor   *cmap1;                    // Color entries for cmap 1
    XColor   fgcolor;                   // Foreground color (if grayscale)
    Cursor   xhair_cursor;              // Crosshair cursor
    int      rw_cmap;                   // Can we allocate r/w color cells?
} XwDisplay;

// One of these holds the X driver state information

typedef struct
{
    XwDisplay    *xwd;                             // Pointer to display info

    int          is_main;                          // Set if the toplevel X window
    Window       window;                           // X window id
    Pixmap       pixmap;                           // Off-screen pixmap
    GC           gc;                               // Graphics context
    XColor       curcolor;                         // Current pen color

    long         event_mask;                       // Event mask
    int          exit_eventloop;                   // Breaks the event loop when set
    long         init_width;                       // Initial window width
    long         init_height;                      // Initial window height

    unsigned     width, height, border;            // Current window dimensions

    double       xscale_init;                      // initial pixels/lx (virt. coords)
    double       yscale_init;                      // initial pixels/ly (virt. coords)
    double       xscale;                           // as above, but current value
    double       yscale;                           //  (after possible resizing)

    short        xlen, ylen;                       // Lengths of device coord space

    int          write_to_window;                  // Set if plotting direct to window
    int          write_to_pixmap;                  // Set if plotting to pixmap

    int          instr;                            // Instruction timer
    int          max_instr;                        // Limit before X server is queried

    PLGraphicsIn gin;                              // Graphics input structure

    int          locate_mode;                      // Set while in locate mode
    int          drawing_xhairs;                   // Set during xhair draws
    XPoint       xhair_x[2], xhair_y[2];           // Crosshair lines

    void ( *MasterEH )( PLStream *, XEvent * );    // Master X event handler
#ifdef PL_HAVE_PTHREAD
    pthread_t updater;                             // The X events updater thread id
#endif
    XColor    bgcolor;                             // Background color
} XwDev;

#endif  // __PLXWD_H__
