/*******************************************************************************
;*******************************************************************************
;**                                                                           **
;**                  COPYRIGHT 2001-2012 NUANCE COMMUNICATIONS                **
;**                                                                           **
;**               NUANCE COMMUNICATIONS PROPRIETARY INFORMATION               **
;**                                                                           **
;**     This software is supplied under the terms of a license agreement      **
;**     or non-disclosure agreement with Nuance Communications and may not    **
;**     be copied or disclosed except in accordance with the terms of that    **
;**     agreement.                                                            **
;**                                                                           **
;*******************************************************************************
;**                                                                           **
;**     FileName: et9abackend.h                                               **
;**                                                                           **
;**  Description: Struct for linking SearchDB and IPAnalyzer together.        **
;**                                                                           **
;*******************************************************************************
;******************************************************************************/


#if !defined(_BACKEND_H_INCLUDED__)
#define      _BACKEND_H_INCLUDED__

/* don't mangle the function name if compile under C++ */
#if defined(__cplusplus)
extern "C" {
#endif

#include "et9abuildconfig.h"

#define SW_SPECIAL_MOUSE_EVENT      0xffffffff

typedef struct _SWCSearchDB_s _SWCSearchDB;
typedef struct SWCIPAnalyzer_s SWCIPAnalyzer;
typedef struct SWCIPTable_s SWCIPTable;
typedef struct SWDbm_s SWDbm;
typedef struct _SWPoint_s _SWPoint;
typedef struct _SWVector_s _SWVector;
typedef struct _SWWord_s _SWWord;

/*================================================================== */
/*  SWScreenGeometry Class Definition */
/*================================================================== */

typedef struct _SWScreenGeometry_s {

    /* Screen/key dimensions used in some calculations. */
    /* This is available to other classes who want direct-variable (faster) */
    /* access. */
    ET9INT keyHeight8;
    ET9INT keyHeight;
    ET9INT keyWidth8;
    ET9INT keyWidth;
    ET9INT keyRadius;
    ET9INT keyboardHeight;
    ET9INT keyboardWidth;
    ET9INT topRowMidY;
    ET9INT bottomRowMidY;
    ET9INT shiftMargin;
}
_SWScreenGeometry;

ET9PRIVATE void ET9FARCALL _SWScreenGeometry_setScreenGeometry(_SWScreenGeometry *pThis, ET9INT keyHeight8, ET9INT keyWidth8, ET9INT keyboardHeight, ET9INT keyboardWidth, ET9INT topRowMidY, ET9INT bottomRowMidY);

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* This is an abstraction of the backend. Some day we'll put more method around */
/* it to wrap the backend into a standalone entity and separate it out of the */
/* front end */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
typedef struct BackEnd_s
{
    ET9AWLingCmnPrivate *pContext;       /* < -IDR-                                                              */
    _SWCSearchDB        *m_pSearchDB;    /* search class */
    SWCIPAnalyzer       *pIPAnalyzer;    /* inflection point analyzer class (CK: Move to BaseStateMachine class?) */
    SWCIPTable          *pIPTable;       /* inflection point table class */
    SWDbm               *m_pDbm;
    _SWScreenGeometry   sScreenGeometry;
}
BackEnd;


/* et9aspath private */

#define _FALSE  ((ET9BOOL)0)
#define _TRUE   ((ET9BOOL)1)

typedef ET9AWLingCmnPrivate _ET9AWSPContext;             /* < -IDR-                                                     */

/* Provide easy access to __Swype_Globals from functions referencing structs SearchDB, IPAnalyzer and IPTable,
 * which all have an m_backend member variable. */
#define _ET9AWSP_DATA   ((__Swype_Globals *)(pThis->m_backend->pContext->pSpmGlobals))

#define __ET9AWSP_MEM   (pLingInfo->pLingCmnInfo->Private)
#define __DATA          ((__Swype_Globals *)(pContext->pSpmGlobals))
#define __SWYPE_DATA    ((__Swype_Globals *)(__ET9AWSP_MEM.pSwypeGlobals))
#define __CONTEXT       (pThis->m_backend->pContext)

typedef ET9U8   _ET9Key;

#define __MAX_SYMBOLS         300
#define __SWYPE_MAX_ROWS      ET9_KDB_MAX_ROWS
#define __SWYPE_MAX_SCORE     (65536.0f*65536.0f)
#define __SWYPE_MIN_SCORE     (1.175494351e-38f)

#define QVGA_MAX_X                  240                     /* QVGA is 240x160, this is the scale always used by the back-end */
#define QVGA_MAX_Y                  160
#define QVGA_MAX_Y_3ROW             120                     /* For 3 row keyboards, use a different vertical scaling */

/* Copied from zdbFormat.h:
   Structure containing key pair data */
typedef struct SWKEYPRDATA_s
{
    ET9U8   octant;     /* octant of slope vector (could reside in 4 most sig bits in lptIdx); */
    ET9U8   distance;   /* distance separating key pair centers (divided by 2 to make big keyboards fit. */
    ET9S8   slopeX;     /* slope connecting of key pair centers */
    ET9S8   slopeY;     /* slope connecting of key pair centers */
}
SWKEYPRDATA;

typedef struct __Swype_Globals_s
{
    /* All the Swype components */
    BackEnd                 sBackEnd;                                        /* < -IDR-                              */

    /* XT9 data */
    ET9AWLingInfo           *pLingInfo;                                     /* < -IDR-                      */
    const ET9KdbLayoutInfo  *pLayoutInfo;                                   /* < -IDR-                      */
    ET9U32                  dwSwypeWordLocale;                              /* < -IDR-                            */

    /* Saved language/keyboard state to know when it changes */
    ET9U32                  dwLdbNum;                                       /* < -IDR-      */
    ET9U32                  dwFirstLdbNum;                                  /* < -IDR-      */
    ET9U32                  dwSecondLdbNum;                                 /* < -IDR-      */
    ET9U32                  dwKdbNum;                                       /* < -IDR-      */
    ET9U16                  wPageNum;                                       /* < -IDR-      */

    /* Computed keyboard information (pertaining to the ET9 keyboard definition) */
    ET9S16                  wLayoutTop;                                     /* < -IDR-      */
    ET9S16                  wLayoutLeft;                                    /* < -IDR-      */
    ET9S16                  wComputedLayoutWidth;                           /* < -IDR-                                                   */
    ET9S16                  wComputedLayoutHeight;                          /* < -IDR-                                                    */

    /* Keyboard geometry info (Swype Classic processing) */
    ET9FLOAT                fKeyboardOffsetX;                               /* < -IDR-      */
    ET9FLOAT                fKeyboardOffsetY;                               /* < -IDR-      */
    ET9FLOAT                fKeyboardScaleX;                                /* < -IDR-      */
    ET9FLOAT                fKeyboardScaleY;                                /* < -IDR-      */
    ET9S16                  wKeyboardScaleXNumerator;                       /* < -IDR-      */
    ET9S16                  wKeyboardScaleXDenominator;                     /* < -IDR-      */
    ET9S16                  wKeyboardScaleYNumerator;                       /* < -IDR-      */
    ET9S16                  wKeyboardScaleYDenominator;                     /* < -IDR-      */
    ET9U16                  wKeyboardHeightQVGA;                            /* < -IDR-      */
    ET9U16                  wKeyboardRowCount;                              /* < -IDR-      */
    ET9S16                  pwKeyboardRowBottom[__SWYPE_MAX_ROWS + 1];      /* < -IDR-      */
    /* Indices into pKeyAreas, Swypable keys first, then the rest */
    ET9U16                  wLetterKeyCount;                                /* < -IDR-      */
    _ET9Key                 pbLetterKeyIndices[ET9_KDB_MAX_KEYS];           /* < -IDR-      */
    SWKEYPRDATA             psKeyDistances[ET9_KDB_MAX_KEYS * ET9_KDB_MAX_KEYS];    /* < -IDR-      */
    ET9SYMB                 pSortedSymbols[__MAX_SYMBOLS];                  /* < -IDR-      */
    _ET9Key                 pSortedSymbolKeys[__MAX_SYMBOLS];               /* < -IDR-      */
    ET9UINT                 nSortedSymbolCount;                             /* < -IDR-      */

    ET9BOOL                 bLowMemory;                                     /* < -IDR-                             */
    ET9BOOL                 bPanic;                                         /* < -IDR-                                          */
    ET9BOOL                 bInvalidContext;                                /* < -IDR-                             /                                         */
}
__Swype_Globals;

ET9PRIVATE _ET9Key ET9FARCALL __SymbolToKey(_ET9AWSPContext *pContext, ET9SYMB symbol);
ET9PRIVATE ET9BOOL ET9FARCALL __AppendSymbolToKeyMap(_ET9AWSPContext *pContext, ET9SYMB newSymbol, _ET9Key newKey, ET9BOOL bRemapOnConflict);
ET9PRIVATE ET9BOOL ET9FARCALL __InitSymbolToKeyMap(_ET9AWSPContext *pContext);
ET9PRIVATE void ET9FARCALL __CalculateKeyboardSize(_ET9AWSPContext *pContext);
ET9PRIVATE void ET9FARCALL __InitSwypeKeyboardLetterInfo(_ET9AWSPContext *pContext);
ET9PRIVATE void ET9FARCALL __CalculateKeyboardSwypableArea(_ET9AWSPContext *pContext);
ET9PRIVATE void ET9FARCALL __InitSwypeKeyboardRows(_ET9AWSPContext *pContext);
ET9PRIVATE void ET9FARCALL __CalculateKeyDistanceMatrix(_ET9AWSPContext *pContext);
ET9PRIVATE ET9BOOL ET9FARCALL __InitSwypeKeyboardInfo(ET9AWLingInfo * const pLingInfo, _ET9AWSPContext *pContext);
ET9PRIVATE ET9BOOL ET9FARCALL __EnsureLanguageAndKeyboardInitialized(ET9AWLingInfo * const pLingInfo, _ET9AWSPContext *pContext);
ET9PRIVATE void ET9FARCALL __ProcessMouseData(_ET9AWSPContext *pContext, ET9S16 wX, ET9S16 wY, ET9U32 dwTimeMS, ET9BOOL bIsSpecial);
ET9PRIVATE ET9BOOL ET9FARCALL __AnalyzePath(ET9AWLingInfo * const pLingInfo, _ET9AWSPContext *pContext);
ET9PRIVATE ET9BOOL ET9FARCALL __PrepareSwypeWord(_ET9AWSPContext *pContext, ET9AWPrivWordInfo * const pWord, _SWWord *dbWord, ET9BOOL bRemoveOptionalChars);
ET9PRIVATE void ET9FARCALL __ScoreWord(_ET9AWSPContext *pContext, ET9AWPrivWordInfo * const pWord);

/* End don't mangle the function name if compile under C++ */
#if defined (__cplusplus)
    }
#endif

#endif
