/* 
 * (C) Copyright 1992, ..., 2003 the "DOSEMU-Development-Team".
 *
 * for details see file COPYING in the DOSEMU distribution
 */

/* XTERM MOUSE suport by M.Laak */
static void xtermmouse_get_event (void)
{
	int btn;
	static int last_btn = 0;
	int x_pos, y_pos;
    
	/* Decode Xterm mouse information to a GPM style event */

	if (keyb_state.kbcount >= 3) {

		x_pos = *(keyb_state.kbp+1) - 32;
		y_pos = *(keyb_state.kbp+2) - 32;
		mouse_move_absolute(x_pos-1, y_pos-1, co, li);
		m_printf("XTERM MOUSE: movement (click follows) detected to pos x=%d: y=%d\n", x_pos, y_pos);

		/* Variable btn has following meaning: */
		/* 0 = btn1 dn, 1 = btn2 dn, 2 = btn3 dn, 3 = btn up */
		btn = *keyb_state.kbp & 3;
    
		/* There seems to be no way of knowing which button was released */
		/* So we assume all the buttons were released */
		if (btn == 3){
			if (last_btn) {
                                mouse_move_buttons(0, 0, 0);
				m_printf("XTERM MOUSE: button release detected\n");
				last_btn = 0;
			}
		} else {
			switch (btn) {
			case 0:
				mouse_move_buttons(1, 0, 0);
				m_printf("XTERM MOUSE: left button click detected\n");
				last_btn = 1;
				break;
			case 1:
				mouse_move_buttons(0, 1, 0);
				m_printf("XTERM MOUSE: middle button click detected\n");
				last_btn = 2;
				break;
			case 2:
				mouse_move_buttons(0, 0, 1);
				m_printf("XTERM MOUSE: right button click detected\n");
				last_btn = 3;
				break;
			}
		}
		keyb_state.kbcount -= 3;	/* update count */
		keyb_state.kbp += 3;

		pic_request(PIC_IMOUSE);
	}
}

void gpm_handle_mouse(void)
{
          static unsigned char buttons;
	  Gpm_Event ev;
	  Gpm_GetEvent(&ev);
	  m_printf("MOUSE: Get GPM Event, %d\n", ev.type);
	  switch (GPM_BARE_EVENTS(ev.type)) {
	  case GPM_MOVE:
	  case GPM_DRAG:
	    mouse_move_absolute(ev.x - 1, ev.y - 1, co, li);
	    break;
	  case GPM_UP:
	    if (ev.buttons & GPM_B_LEFT) buttons &= ~GPM_B_LEFT;
	    if (ev.buttons & GPM_B_MIDDLE) buttons &= ~GPM_B_MIDDLE;
	    if (ev.buttons & GPM_B_RIGHT) buttons &= ~GPM_B_RIGHT;
	    ev.buttons = buttons;
            /* fall through */
	  case GPM_DOWN:
	    mouse_move_buttons(ev.buttons & GPM_B_LEFT,
			       ev.buttons & GPM_B_MIDDLE,
			       ev.buttons & GPM_B_RIGHT);
            buttons = ev.buttons;
            /* fall through */
	  default:
	    break;
	  }
	}
}
