Pastey mouse.c

/*****************************************************************************/
/*      (c) Copyright - See License.txt       */
/*****************************************************************************/

/* compile as
 * gcc -o mouse.so -lgpm -shared mouse.c
 */

/* config starts */

#define EUNIX 1
//#undef EUNIX
//#define EBSD 1
#undef EBSD
//#define EWINDOWS 1
#undef EWINDOWS
//#define EMINGW 1
#undef EMINGW
//#define EWATCOM 1
#undef EWATCOM

/* config ends */

#define EGPM 1

#include 
#include 

#ifdef EUNIX

#include 
#define stricmp strcasecmp

#ifdef EBSD
#define NAME_MAX 255
#else
#include 
#endif
#ifdef EGPM
#include 
#endif
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#ifndef LOCK_SH
#define LOCK_SH  1 /* shared lock */
#define LOCK_EX  2 /* exclusive lock */
#define LOCK_NB  4 /* don't block when locking */
#define LOCK_UN  8 /* unlock */
#endif

#ifdef ESUNOS
#include 

int emul_flock(fd, cmd)
	int fd, cmd;
{
	struct flock f;

	memset(&f, 0, sizeof(f));
	
	if (cmd & LOCK_UN)
		f.l_type = F_UNLCK;
	if (cmd & LOCK_SH)
		f.l_type = F_RDLCK;
	if (cmd & LOCK_EX)
		f.l_type = F_WRLCK;

	return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
}

#define flock(f,c) emul_flock(f,c)
#endif // ESUNOS

#else // EUNIX

#include 

#ifdef EWATCOM
#  include 
#  include 
#endif

#include 
#endif  // not EUNIX

#include 
#include 

#ifdef EXTRA_CHECK
#include 
#endif

#ifdef EWINDOWS
#include 
#endif

#ifdef EMINGW
#include 
#include 
#endif

#include 

int first_mouse = 1;  /* indicates if mouse function has been set up yet */

#ifdef EXTRA_STATS
int mouse_ints = 0;  /* number of unused mouse interrupts */
#endif

/* In DOS, this mouse data is locked in memory */
static struct locked_data {
        int lock;  /* = 0  semaphore */
        int code;
        int x;
        int y;
} mouse = {0,0,0,0};

#define ATOM_1 1

extern int MousePointer(int x);
extern int MouseEvents(int interrupts);
extern int GetMouse(int * result);

int MousePointer(int x)
{
	return ATOM_1;
}

int MouseEvents(int interrupts)
{
	return ATOM_1;
}
int mouse_installed()
{
	return 0;
}
static void show_mouse_cursor(int x)
{
}
static void lock_mouse_pages()
{
}

#ifdef EUNIX
#ifdef EGPM  // if GPM package is desired, - not available on FreeBSD
/* Text Mode Mouse in Linux
 Codes:
	buttons:
		left = 4
		middle = 2
		right = 1

	clicks:
		0: single click
		1: double click
		2: triple click

	vc always 1?

	type:
		move = 1
		drag = 2   146?
		down = 4   20?
		up = 8     152?

	xterm:
		left-down: 409 32 x y
		middle-down: 409 33 x y
		right-down: 409 34 x y
		*any* button-up: 409 35 x y
		(can I report all 3 buttons went up?)

		no move events are reported

	x and y are origin (33,33) at top left of window
*/

int Mouse_Handler(Gpm_Event *event, void *clientdata) {
// handles mouse events
	mouse.lock = 1;
	mouse.code = event->buttons; //FOR NOW
	if (mouse.code == 32)
		mouse.code = 4;
	else if (mouse.code == 33)
		mouse.code = 2;
	else if (mouse.code == 34)
		mouse.code = 1;
	mouse.x = event->x;
	mouse.y = event->y;
	return -1;
}

static Gpm_Connect conn;

#define ATOM_M1 -1
#define FALSE 0
int GetMouse(int * result)
// Return the next mouse event, or -1
// Linux version using GPM
{
	int final_result;
	int key, action;

	if (first_mouse) {
		/* initialize GPM */
		first_mouse = FALSE;
		conn.eventMask = 0xFFFF; // unsigned short
		conn.defaultMask = 0;
		conn.minMod = 0;
		conn.maxMod = ~0;
		gpm_visiblepointer = 1;
		//gpm_zerobased = 1;
		gpm_handler = Mouse_Handler;
		gpm_data = NULL;
		Gpm_Open(&conn, 0);
		final_result = ATOM_M1;
	}
	else if (mouse.lock) {
		/* we picked up a mouse event via Mouse_Handler() */
		mouse.lock = 0;

		result[0] = mouse.code;
		result[1] = ((mouse.y > 32) ? mouse.x-32 : mouse.x);
		result[2] = ((mouse.y > 32) ? mouse.y-32 : mouse.y);

#ifdef EXTRA_STATS
		mouse_ints--;
#endif
		mouse.code = 0;
		final_result = 3;
	}
	#if 0
	else {
		/* check for a mouse event (or a key) */
		key = Gpm_Getchar();
		if (key == -1) {
			final_result = ATOM_M1;
		}
		else if (key == 409) {
			action = Gpm_Getchar();
			if (action == 32)
				action = 4;
			else if (action == 33)
				action = 2;
			else if (action == 34)
				action = 1;
			result[0] = action;
			result[1] = (Gpm_Getchar()-32);
			result[2] = (Gpm_Getchar()-32);
#ifdef EXTRA_STATS
			mouse_ints--;
#endif
			mouse.code = 0;
			final_result = 3;
		}
		else if (key == 27) {
			final_result = ATOM_M1;
		}
		else {
			/* we picked up a character */
			final_result = ATOM_M1;
		}
	}
	#endif
	return final_result;
}
#endif // EGPM

#else
// not LINUX
int GetMouse(int * result)
/* GET_MOUSE event built-in WIN32 */
{
	if (first_mouse) {
		lock_mouse_pages();
		first_mouse = 0;
		if (mouse_installed()) {
			show_mouse_cursor(0x1);
			MouseEvents(0x0000FFFF);
		}
	}
	if (mouse.lock) {
		/* there's something in the queue */

		mouse.lock = 2; /* critical section */
		result[0] = mouse.code;
		result[1] = mouse.x;
		result[2] = mouse.y;
		mouse.lock = 0;

#ifdef EXTRA_STATS
		mouse_ints--;
#endif
		return 3;
	}
	else
		return ATOM_M1;
}
#endif