/*
 * mouse support for dosemu 0.48p2+
 *     Robert Sanders, gt8134b@prism.gatech.edu
 *
 * $Date: $
 * $Source: $
 * $Revision: $
 * $State: $
 *
 * $Log: $
 */

#ifndef _EMU_MOUSE_H
#define _EMU_MOUSE_H

#include <termios.h>
#include "emu_defs.h"

#define MOUSE_BASE_VERSION	0x0700	/* minimum driver version 7.00 */
#define MOUSE_EMU_VERSION	0x0000	/* my driver version 0.00 */
/* this is the version returned to DOS programs */
#define MOUSE_VERSION	  (MOUSE_BASE_VERSION + MOUSE_EMU_VERSION)

#define MOUSE_NONE -1
#define MOUSE_MICROSOFT 0
#define MOUSE_MOUSESYSTEMS 1
#define MOUSE_MMSERIES 2
#define MOUSE_LOGITECH 3
#define MOUSE_BUSMOUSE 4
#define MOUSE_MOUSEMAN 5
#define MOUSE_PS2 6
#define MOUSE_HITACHI 7
#define MOUSE_X 8

/* types of mouse events */
#define DELTA_CURSOR		1
#define DELTA_LEFTBDOWN		2	/* pressed  */
#define DELTA_LEFTBUP		4	/* released */
#define DELTA_RIGHTBDOWN	8
#define DELTA_RIGHTBUP		16
#define DELTA_MIDDLEBDOWN  32
#define DELTA_MIDDLEBUP    64

#define MICKEY			9	/* mickeys per move */
#define M_DELTA			8

#define MAX_MOUSE 1
#define HEIGHT 16
#define PLANES 4

typedef struct  {
  char dev[255];
  int fd;
  int type;
  int flags;
  Boolean add_to_io_select;
  Boolean intdrv;
  Boolean emulate3buttons;
  Boolean has3buttons;
  Boolean cleardtr;
  int baudRate;
  int sampleRate;
  int lastButtons;
  int chordMiddle;

  struct termios oldset;
} mouse_t;

/* this entire structure gets saved on a driver state save */
/* therefore we try to keep it small where appropriate */
EXTERN struct  {
  Bit8u lbutton, mbutton, rbutton;
  Bit8u oldlbutton, oldmbutton, oldrbutton;

  Bit16u lpcount, lrcount, mpcount, mrcount, rpcount, rrcount;

  /* positions for last press/release for each button */
  Bit16s lpx, lpy, mpx, mpy, rpx, rpy;
  Bit16s lrx, lry, mrx, mry, rrx, rry;

  /* TRUE if we're in a graphics mode */
  Boolean gfx_cursor;

  Bit8u xshift, yshift;

  /* text cursor definition (we only support software cursor) */
  Bit16u textscreenmask, textcursormask;

  /* graphics cursor defnition */
  Bit8u hotx, hoty;
  Bit16u graphscreenmask[16], graphcursormask[16];

  /* exclusion zone */
  Bit16s exc_ux, exc_uy, exc_lx, exc_ly;

  /* these are clipped to min and max x; they are *not* rounded. */
  Bit16s x, y;
  /* these are rounded-off versions of the above which are returned
  	to a client */
  Bit16s rx, ry;
  /* coordinates at which the cursor was last drawn */
  Bit16s oldrx, oldry;
  /* these are the cursor extents; they are rounded off. */
  Bit16s minx, maxx, miny, maxy;
 
  /* these are for sensitivity options */
  Bit8u speed_x, speed_y;
  Bit8u threshold;
  Bit8u language;

  /* accumulated motion counters */
  Bit8u mickeyx, mickeyy;

  /* zero if cursor is on, negative if it's off */
  Bit16s cursor_on;

  /* this is for the user-defined subroutine */
  Bit16u cs, ip;
  Bit16u *csp, *ipp;
  Bit16u mask;

  /* true if mouse has three buttons (third might be emulated) */
  Boolean threebuttons;
 
  Bit16u display_page;
 
  /* ignore application's x/y speed settings?  might not be necessary
  	anymore if I managed to get the speed settings correct. */
  Boolean ignorexy;

  struct {
    Boolean state;
    unsigned short pkg;
  } ps2;
} mouse;

EXTERN mouse_t mice[MAX_MOUSE] ;
EXTERN int     keyboard_mouse;

extern void mouse_init(void);
extern void mouse_close(void);

extern void mouse_keyboard(int);
extern void mouse_curtick(void);
extern void mouse_sethandler(void *, Bit16u *, Bit16u *);

extern void mouse_helper(void);
extern void mouse_int(void);
extern void int74(void);

extern void mouse_move(void);
extern void mouse_lb(void);
extern void mouse_mb(void);
extern void mouse_rb(void);

extern void mouse_event(void);
extern void update_cursor_reg(void);

extern void DOSEMUSetupMouse(void);
extern void DOSEMUMouseEvents(void);
extern void DOSEMUMouseProtocol(unsigned char *, int);
extern void DOSEMUSetMouseSpeed(int, int, unsigned);

#endif /* _EMU_MOUSE_H */
