/****************************************************************************

 Module:
 liblgkbm.h

 Description:
 Logitech USB mouse/keyboard handler library file for Sony PlayStation(tm)2

 COPYRIGHT (C) 2002-2004 LOGITECH, INC. ALL RIGHTS RESERVED.

 This program is a trade secret of LOGITECH, and it is not to be
 reproduced, published, disclosed to others, copied, adapted,
 distributed or displayed without the prior authorization of LOGITECH.

 Licensee agrees to attach or embed this notice on all copies of the
 program, including partial copies or modified versions thereof.

****************************************************************************/

#ifndef INCLUDE_LIBLGKBM_H
#define INCLUDE_LIBLGKBM_H

#if defined(_LANGUAGE_C_PLUS_PLUS)||defined(__cplusplus)||defined(c_plusplus)
extern "C" {
#endif

#include "lgkbmerr.h"


#define LGDEVINFO_WHEEL_MASK    (1<<14)  /* 0: No Wheel 1: Has Wheel         */
#define LGDEVINFO_REPORTID_MASK (1<<15)  /* 0: No report ID, 1: has reportId */


// Input parameters for the configuring the irx
#define LGKBM_PARAM_MAX_NUM_KBDS          "MaxKbd"
#define LGKBM_PARAM_MAX_NUM_MICE          "MaxMice"


#define LGKBM_MAX_PACKETS              5
#define LGKBM_HID_PACKET_SIZE          8


#define LGKBM_HINT_NOENUMNEEDED        0
#define LGKBM_HINT_ENUMNEEDED          1


// Following are used by the keyboard
#define LGKBM_MAX_KEY_EVENTS           10
#define LGKBM_MAX_NUM_KEY_CHARS        8
#define LGKBM_MAX_TRANSLATED           5

// Following defines define the event type.
#define LGKBM_KEY_DOWN                    1
#define LGKBM_KEY_UP                      2
#define LGKBM_KEY_REPEAT                  3
#define LGKBM_INVALID_EVENT               0

#define LGKBM_DEFAULT_REPEAT_INTERVAL     100      // repeat interval in ms
#define LGKBM_DEFAULT_INITIAL_DELAY       500      // inital repeat interval in ms
#define LGKBM_MINIMUM_INITIAL_DELAY       100
#define LGKBM_MAXIMUM_INITIAL_DELAY       3000
#define LGKBM_MINIMUM_REPEAT_INTERVAL     20
#define LGKBM_MAXIMUM_REPEAT_INTERVAL     2000


// This mask is used to extract the translated unicode charector from the unsigned int 
// xlate_key_info
#define LGKBD_XLATE_CHAR_MASK   0xffff

#define LGKBD_RAWHID_MASK       (1U<<31) /* Bit indicating raw hid code   */
#define LGKBD_KEYPAD_MASK       (1U<<30) /* Bit indicating the keypad key */
#define LGKBD_KEYREPEAT_MASK    (1U<<29) /* Bit indicating repeat key     */

#define LGKBD_NUMLOCK_MASK      (1<<0)  /* 0:OFF 1:ON */
#define LGKBD_CAPSLOCK_MASK     (1<<1)  /* 0:OFF 1:ON */
#define LGKBD_SCROLLLOCK_MASK   (1<<2)  /* 0:OFF 1:ON */
#define LGKBD_LED_COMPOSE_MASK  (1<<3)  /* 0:OFF 1:ON */
#define LGKBD_LED_KANA_MASK     (1<<4)  /* 0:OFF 1:ON */

#define LGKBD_MKEY_L_CTRL       (1<<0)  /* 0:Release 1:Push */
#define LGKBD_MKEY_L_SHIFT      (1<<1)  /* 0:Release 1:Push */
#define LGKBD_MKEY_L_ALT        (1<<2)  /* 0:Release 1:Push */
#define LGKBD_MKEY_L_WIN        (1<<3)  /* 0:Release 1:Push */
#define LGKBD_MKEY_R_CTRL       (1<<4)  /* 0:Release 1:Push */
#define LGKBD_MKEY_R_SHIFT      (1<<5)  /* 0:Release 1:Push */
#define LGKBD_MKEY_R_ALT        (1<<6)  /* 0:Release 1:Push */
#define LGKBD_MKEY_R_WIN        (1<<7)  /* 0:Release 1:Push */

#define LGKBM_MOUSE_BUTTON1           (1<<0)  /* Mouse Button 1 */
#define LGKBM_MOUSE_BUTTON2           (1<<1)  /* Mouse Button 1 */
#define LGKBM_MOUSE_BUTTON3           (1<<2)  /* Mouse Button 1 */
#define LGKBM_MOUSE_BUTTON4           (1<<3)  /* Mouse Button 1 */
#define LGKBM_MOUSE_BUTTON5           (1<<4)  /* Mouse Button 1 */
#define LGKBM_MOUSE_BUTTON6           (1<<5)  /* Mouse Button 1 */
#define LGKBM_MOUSE_BUTTON7           (1<<6)  /* Mouse Button 1 */
#define LGKBM_MOUSE_BUTTON8           (1<<7)  /* Mouse Button 1 */


// Macro definitions

// Pass the button BITMAP and the button no to be checked.
#define lgIsMouseButtonPressed(x, bno)  (x & (1<<(bno - 1)) )

// Pass the LED state to the following macros (lgKbdPacket.led_state)
#define lgIsNumLockPressed(x)         (x & LGKBD_NUMLOCK_MASK)
#define lgIsCapsLockPressed(x)        (x & LGKBD_CAPSLOCK_MASK)
#define lgIsScrollLockPressed(x)      (x & LGKBD_SCROLLLOCK_MASK)
#define lgIsComposePressed(x)         (x & LGKBD_LED_COMPOSE_MASK)
#define lgIsKanaPressed(x)            (x & LGKBD_LED_KANA_MASK)

// Pass the modifier state to the following macros (lgKbdPacket.modifier_state)
#define lgIsCtrlKeyPressed(x)         ((x & LGKBD_MKEY_L_CTRL)  ? 1: ((x & LGKBD_MKEY_R_CTRL)  ? 2 : 0) )
#define lgIsShiftKeyPressed(x)        ((x & LGKBD_MKEY_L_SHIFT) ? 1: ((x & LGKBD_MKEY_R_SHIFT) ? 2 : 0) )
#define lgIsAltKeyPressed(x)          ((x & LGKBD_MKEY_L_ALT)   ? 1: ((x & LGKBD_MKEY_R_ALT)   ? 2 : 0) )
#define lgIsWinKeyPressed(x)          ((x & LGKBD_MKEY_L_WIN)   ? 1: ((x & LGKBD_MKEY_R_WIN)   ? 2 : 0) )


typedef struct _lgKbdDeviceDesc {
    char            device_location[8];
    u_short         flag;
} lgKbdDeviceDesc;

typedef struct _lgMouseDeviceDesc {
    char            device_location[8];
    u_short         flag;
    u_char          nButtons;
    u_char          fHasWheel;
} lgMouseDeviceDesc;


typedef struct _lgKeyInfo {
    unsigned int    time_stamp;
    unsigned short  key_code;
} lgKeyInfo;

typedef struct _lgKbdEvent {
    lgKeyInfo       key_info;
    u_short         led_state; 
    u_short         modifier_state; 
    u_short         event_type;
} lgKbdEvent;


typedef struct _lgKbdXlatedList {
    int             num_keys;
    unsigned int    xlated_char[LGKBM_MAX_TRANSLATED];
} lgKbdXlatedList;



typedef struct _lgKbdEventList {
    u_short         flag;             // bit 0 : if 1 indicates buffer overflow. bit 1-14 : reserved.
                                      // bit 15 : reserved. if 1 indicates that the list has been translated into ASCII chars.
    u_short         num_events;
    lgKbdEvent      events[LGKBM_MAX_KEY_EVENTS];
} lgKbdEventList;


typedef struct _lgKbdPacket {
    unsigned int    time_stamp;
    u_short         led_state;
    u_short         modifier_state;
    u_short         len;
    u_char          key_code[LGKBM_MAX_NUM_KEY_CHARS];
} lgKbdPacket;



typedef struct _lgMouseSummaryPacket {
    unsigned int    time_stamp;
    u_char          button;
    int             X;
    int             Y;
    int             Wheel;
} lgMouseSummaryPacket;
 


typedef struct _lgMousePacket {
    unsigned int    time_stamp;
    u_char          button;
    char            X;
    char            Y;
    char            Wheel;
} lgMousePacket;
 



typedef struct lgMouseData {
  lgMouseSummaryPacket  summary_pkt;
  u_short               num_pkts;
  lgMousePacket         mouse_pkts[LGKBM_MAX_PACKETS];
} lgMouseData;


typedef struct _lgKbdStateInfo {
  u_char   KbdStateMap[32];
} lgKbdStateInfo;


typedef struct _lgKbdHidToAsciiTranslateTable {
   union { 
       unsigned char reserved;
       struct {
	   unsigned char fAlpha    :  1;
	   unsigned char fNumPad   :  1;
	   unsigned char reserved  :  6;
       } keyAttributes;
   } flags;
   unsigned char   HidKeyCode;
   unsigned short  KeyCode;
   unsigned short  ShiftKeyCode;
   unsigned short  AltGrKeyCode;
   unsigned short  ShiftAltGrKeyCode;
} lgKbdHidToAsciiTranslateTable;


typedef unsigned long (*lgLPTimerFunc)(void) ;


int lgKbMInit(int max_kbds, int max_mice, int thri_priority, 
                                              lgLPTimerFunc pfGetTime);
int lgKbMPrepareForRebootIOP(void);
int lgKbMUnloadIopModule(void);
int lgKbMChangeThreadPriority(int thri_priority);
int lgKbMResumeThread(void);
int lgKbMSuspendThread(void);

int lgKbdReInit(int device_handle);
int lgKbdEnumerate(int device_index, lgKbdDeviceDesc *description);
int lgKbdEnumHint(int *hint);
int lgKbdOpen(int device_index, int *device_handle);
int lgKbdClose(int device_handle);
int lgKbdReadState(int device_handle, lgKbdStateInfo *kbd_state);
int lgKbdRead(int device_handle, lgKbdEventList *event_list);
int lgIsBitSet(lgKbdStateInfo *kbd_state_map, int bitno);
int lgIsKeyPressed(lgKbdStateInfo *kbd_state_map, u_char hid_key_code);
int lgKbdGetLedState(int device_handle, u_short *ledState);
int lgKbdTranslate(int device_handle, lgKbdEventList *event_list, lgKbdXlatedList *XlateList);
int lgKbdSetupTranslationTable(lgKbdHidToAsciiTranslateTable *pTranslateTable, int szTable);
int lgKbdResetTranslationTable(void);
int lgKbdSetRepeatRate(unsigned int inital_delay, unsigned int repeat_interval);
int lgMouseEnumerate(int device_index, lgMouseDeviceDesc *description);
int lgMouseEnumHint(int *hint);
int lgMouseOpen(int device_index, int *devie_handle);
int lgMouseClose(int devie_handle);
int lgMouseRead(int device_handle, lgMouseData *mouse_data);

#if defined(_LANGUAGE_C_PLUS_PLUS)||defined(__cplusplus)||defined(c_plusplus)
}
#endif


#endif
