amiga_cyberspace

diff src/mouse.c @ 0:e6fd57053627

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Jul 2017 08:17:34 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/mouse.c	Tue Jul 25 08:17:34 2017 +0300
     1.3 @@ -0,0 +1,69 @@
     1.4 +#include <stdlib.h>
     1.5 +#include "mouse.h"
     1.6 +#include "inttypes.h"
     1.7 +
     1.8 +
     1.9 +#define REG_BASE_ADDR	0xdff000
    1.10 +#define REGNO_JOY0DAT	0x00a
    1.11 +#define REG_JOY0DAT *(volatile uint16_t*)(REG_BASE_ADDR | REGNO_JOY0DAT)
    1.12 +#define REG_CIAA_PORTA	*(volatile uint8_t*)0xbfe001
    1.13 +
    1.14 +#define CIAA_PA_FIR0	0x40
    1.15 +#define CIAA_PA_FIR1	0x80
    1.16 +
    1.17 +static int xrng[2] = {0, 320};
    1.18 +static int yrng[2] = {0, 240};
    1.19 +
    1.20 +void set_mouse_bounds(int x0, int y0, int x1, int y1)
    1.21 +{
    1.22 +	xrng[0] = x0;
    1.23 +	xrng[1] = x1;
    1.24 +	yrng[0] = y0;
    1.25 +	yrng[1] = y1;
    1.26 +}
    1.27 +
    1.28 +int mouse_state(int *x, int *y)
    1.29 +{
    1.30 +	mouse_pos(x, y);
    1.31 +	return mouse_bnstate();
    1.32 +}
    1.33 +
    1.34 +int mouse_bnstate(void)
    1.35 +{
    1.36 +	/* TODO: handle right mouse button */
    1.37 +	return (REG_CIAA_PORTA & CIAA_PA_FIR0) ? 0 : 1;
    1.38 +}
    1.39 +
    1.40 +/* TODO: for when I feel like making a proper mouse handler
    1.41 + * use vblank interrupt vector to read the mouse register once
    1.42 + * and have mouse_pos (and friends) simply access the global data
    1.43 + */
    1.44 +void mouse_pos(int *x, int *y)
    1.45 +{
    1.46 +	static int xpos, ypos;
    1.47 +	static int prev_xcount, prev_ycount;
    1.48 +	int xcount, ycount, dx, dy;
    1.49 +	uint16_t raw = REG_JOY0DAT;
    1.50 +
    1.51 +	xcount = raw & 0xff;
    1.52 +	ycount = raw >> 8;
    1.53 +
    1.54 +	dx = xcount - prev_xcount;
    1.55 +	dy = ycount - prev_ycount;
    1.56 +
    1.57 +	if(abs(dx) > 127) dx = 255 - (xcount - prev_xcount);
    1.58 +	if(abs(dy) > 127) dy = 255 - (ycount - prev_ycount);
    1.59 +	prev_xcount = xcount;
    1.60 +	prev_ycount = ycount;
    1.61 +
    1.62 +	xpos += dx;
    1.63 +	ypos += dy;
    1.64 +
    1.65 +	if(xpos < xrng[0]) xpos = xrng[0];
    1.66 +	if(ypos < yrng[0]) ypos = yrng[0];
    1.67 +	if(xpos >= xrng[1]) xpos = xrng[1] - 1;
    1.68 +	if(ypos >= yrng[1]) ypos = yrng[1] - 1;
    1.69 +
    1.70 +	*x = xpos;
    1.71 +	*y = ypos;
    1.72 +}