/*****************************************************************************
 *
 *   ctxh264.h
 *
 *   H.264 v3 interface software fallback implementation.
 *
 *   $Id$
 *
 *   Copyright 2013-2016 Citrix Systems, Inc.  All Rights Reserved.
 *
 *****************************************************************************/


#ifndef _CTX_H264_H_
#define _CTX_H264_H_

#include <vector>
#include <map>

#ifdef X11_SUPPORT
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XShm.h>
#endif /* X11_SUPPORT */

#include "decoder.h"
#include "H264_decode.h"

#define MAX_DIRTY_RECT  31 /* Taken from tw2proto.h. */

#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b)) 
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif

#define REFERENCE_VAR(x) { x = x; }

typedef struct image_buf ImgBuf;

typedef struct _V3_FBCtxData
{
    unsigned int    CtxId;

    /* Decoder defined by source. */
    struct Decoder  dec;

    unsigned int    uEncFrameCurSize;
    unsigned int    uEncFrameSize;
    unsigned int    uEncMaxFrameSize;
    unsigned char   *pEncodedFrame;
    unsigned int    uCtxWidth;
    unsigned int    uCtxHeight;

#ifdef X11_SUPPORT
    Window          window;
    XImage         *fb;
    XShmSegmentInfo shm_info;
    int             shm_id;
#endif /* X11_SUPPORT */

    char           *obuf;
    unsigned int    obuf_stride;

    /* Dirty rects. */

    SIGNED_RECT     dirty_rects[MAX_DIRTY_RECT];
    int             num_rects;

    /* Some state variables to help improve decision making. */

    bool            has_text;
    bool            is_small_frame;

    std::vector<struct image_buf> ltdata; /* Lossless text data. */
    std::vector<struct image_buf> sfdata; /* Small frame data. */

#ifdef DUMP_OUTPUT
    /* For H.264 dump output. */
    FILE            *fp;
#endif

    /* Is this H.264 context associated with a canvas? If so,
     * x_off and y_off are the coordinates of the top-left pixel
     * of this context, relative to the canvas.
     */
    CANV_context    canvas;
    int             x_off;
    int             y_off;

    /* YUV bounding rectangle for conversion to RGB. */
    SIGNED_RECT     yuv_bounds;

} V3FBCtxData;

typedef struct _V2_CANVAS_STRUCT_
{
    /* Pointer to canvas pixels. */
    unsigned char *bits;
    int width, height;
    int stride;

    /* x, y offset relative to canvas at (0,0). */
    int x_off, y_off;

    /* Track dirty rectangle. */
    SIGNED_RECT dirty_rect;

#ifdef X11_SUPPORT
    XImage         *fb;
    XShmSegmentInfo shm_info;
    int             shm_id;
#endif /* X11_SUPPORT */

} V2_CANVAS_STRUCT;

extern struct H264_decoder H264_decoder; /* Starting point for plugin. */

/* All exported by the main process. */

extern "C" char        *getICARoot(void);
extern "C" struct mainloop_plugin Plug; /* FIXME */

/* Common functions, for X11 support. */

#ifdef X11_SUPPORT
extern "C" Display     *GetICADisplay();
extern "C" GC           GetICAGC();
extern "C" void         WaitForShmCompletion(); 
extern "C" UINT32       SHM_FramesPushed;
extern "C" int          Get_SHM_event_base();
#endif /* X11_SUPPORT */

#endif // _CTX_H264_H_
