amiga_imgv

annotate src/amiga/mouse.c @ 9:01bd8bbc46d4

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Oct 2017 21:56:28 +0200
parents
children
rev   line source
nuclear@0 1 #include <stdlib.h>
nuclear@0 2 #include "mouse.h"
nuclear@0 3 #include "inttypes.h"
nuclear@0 4
nuclear@0 5
nuclear@0 6 #define REG_BASE_ADDR 0xdff000
nuclear@0 7 #define REGNO_JOY0DAT 0x00a
nuclear@0 8 #define REG_JOY0DAT *(volatile uint16_t*)(REG_BASE_ADDR | REGNO_JOY0DAT)
nuclear@0 9 #define REG_CIAA_PORTA *(volatile uint8_t*)0xbfe001
nuclear@0 10
nuclear@0 11 #define CIAA_PA_FIR0 0x40
nuclear@0 12 #define CIAA_PA_FIR1 0x80
nuclear@0 13
nuclear@0 14 static int xrng[2] = {0, 320};
nuclear@0 15 static int yrng[2] = {0, 240};
nuclear@0 16
nuclear@0 17 void set_mouse_bounds(int x0, int y0, int x1, int y1)
nuclear@0 18 {
nuclear@0 19 xrng[0] = x0;
nuclear@0 20 xrng[1] = x1;
nuclear@0 21 yrng[0] = y0;
nuclear@0 22 yrng[1] = y1;
nuclear@0 23 }
nuclear@0 24
nuclear@0 25 int mouse_state(int *x, int *y)
nuclear@0 26 {
nuclear@0 27 mouse_pos(x, y);
nuclear@0 28 return mouse_bnstate();
nuclear@0 29 }
nuclear@0 30
nuclear@0 31 int mouse_bnstate(void)
nuclear@0 32 {
nuclear@0 33 /* TODO: handle right mouse button */
nuclear@0 34 return (REG_CIAA_PORTA & CIAA_PA_FIR0) ? 0 : 1;
nuclear@0 35 }
nuclear@0 36
nuclear@0 37 /* TODO: for when I feel like making a proper mouse handler
nuclear@0 38 * use vblank interrupt vector to read the mouse register once
nuclear@0 39 * and have mouse_pos (and friends) simply access the global data
nuclear@0 40 */
nuclear@0 41 void mouse_pos(int *x, int *y)
nuclear@0 42 {
nuclear@0 43 static int xpos, ypos;
nuclear@0 44 static int prev_xcount, prev_ycount;
nuclear@0 45 int xcount, ycount, dx, dy;
nuclear@0 46 uint16_t raw = REG_JOY0DAT;
nuclear@0 47
nuclear@0 48 xcount = raw & 0xff;
nuclear@0 49 ycount = raw >> 8;
nuclear@0 50
nuclear@0 51 dx = xcount - prev_xcount;
nuclear@0 52 dy = ycount - prev_ycount;
nuclear@0 53
nuclear@0 54 if(abs(dx) > 127) dx = 255 - (xcount - prev_xcount);
nuclear@0 55 if(abs(dy) > 127) dy = 255 - (ycount - prev_ycount);
nuclear@0 56 prev_xcount = xcount;
nuclear@0 57 prev_ycount = ycount;
nuclear@0 58
nuclear@0 59 xpos += dx;
nuclear@0 60 ypos += dy;
nuclear@0 61
nuclear@0 62 if(xpos < xrng[0]) xpos = xrng[0];
nuclear@0 63 if(ypos < yrng[0]) ypos = yrng[0];
nuclear@0 64 if(xpos >= xrng[1]) xpos = xrng[1] - 1;
nuclear@0 65 if(ypos >= yrng[1]) ypos = yrng[1] - 1;
nuclear@0 66
nuclear@0 67 *x = xpos;
nuclear@0 68 *y = ypos;
nuclear@0 69 }