Search
lxdream.org :: lxdream/src/maple/mouse.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/mouse.c
changeset 669:ab344e42bca9
prev634:b7d5592495e9
next736:a02d1475ccfd
author nkeynes
date Mon May 12 10:00:13 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Cleanup most of the -Wall warnings (getting a bit sloppy...)
Convert FP code to use fixed banks rather than indirect pointer
(3-4% faster this way now)
file annotate diff log raw
nkeynes@608
     1
/**
nkeynes@611
     2
 * $Id$
nkeynes@608
     3
 *
nkeynes@608
     4
 * Implementation of the standard mouse device
nkeynes@608
     5
 *
nkeynes@608
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@608
     7
 *
nkeynes@608
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@608
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@608
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@608
    11
 * (at your option) any later version.
nkeynes@608
    12
 *
nkeynes@608
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@608
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@608
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@608
    16
 * GNU General Public License for more details.
nkeynes@608
    17
 */
nkeynes@608
    18
#include <stdlib.h>
nkeynes@608
    19
#include <stdio.h>
nkeynes@608
    20
#include <string.h>
nkeynes@669
    21
#include "display.h"
nkeynes@608
    22
#include "maple/maple.h"
nkeynes@608
    23
nkeynes@608
    24
#define MOUSE_IDENT     { 0x00, 0x00, 0x02, 0x00,  0x02, 0x00, 0x07, 0x00,  0x00, 0x00, 0x00, 0x00,\
nkeynes@608
    25
 0x00, 0x00, 0x00, 0x00,  0xff, 0x00, 0x44, 0x72,  0x65, 0x61, 0x6d, 0x63,  0x61, 0x73, 0x74, 0x20,\
nkeynes@608
    26
 0x4d, 0x6f, 0x75, 0x73,  0x65, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,\
nkeynes@608
    27
 0x20, 0x20, 0x20, 0x20,  0x50, 0x72, 0x6f, 0x64,  0x75, 0x63, 0x65, 0x64,  0x20, 0x42, 0x79, 0x20,\
nkeynes@608
    28
 0x6f, 0x72, 0x20, 0x55,  0x6e, 0x64, 0x65, 0x72,  0x20, 0x4c, 0x69, 0x63,  0x65, 0x6e, 0x73, 0x65,\
nkeynes@608
    29
 0x20, 0x46, 0x72, 0x6f,  0x6d, 0x20, 0x53, 0x45,  0x47, 0x41, 0x20, 0x45,  0x4e, 0x54, 0x45, 0x52,\
nkeynes@608
    30
 0x50, 0x52, 0x49, 0x53,  0x45, 0x53, 0x2c, 0x4c,  0x54, 0x44, 0x2e, 0x20,  0x20, 0x20, 0x20, 0x20,\
nkeynes@608
    31
 0x90, 0x01, 0xf4, 0x01 }
nkeynes@608
    32
#define MOUSE_VERSION   { 0x56, 0x65, 0x72, 0x73,  0x69, 0x6f, 0x6e, 0x20,  0x31, 0x2e, 0x30, 0x30,\
nkeynes@608
    33
 0x30, 0x2c, 0x32, 0x30,  0x30, 0x30, 0x2f, 0x30,  0x32, 0x2f, 0x32, 0x35,  0x2c, 0x33, 0x31, 0x35,\
nkeynes@608
    34
 0x2d, 0x36, 0x32, 0x31,  0x31, 0x2d, 0x41, 0x54,  0x20, 0x20, 0x20, 0x2c,  0x33, 0x20, 0x42, 0x75,\
nkeynes@608
    35
 0x74, 0x74, 0x6f, 0x6e,  0x20, 0x26, 0x20, 0x58,  0x2d, 0x59, 0x20, 0x42,  0x61, 0x6c, 0x6c, 0x20,\
nkeynes@608
    36
 0x26, 0x20, 0x5a, 0x20,  0x57, 0x68, 0x65, 0x65,  0x6c, 0x20, 0x2c, 0x34,  0x30, 0x30, 0x64, 0x70,\
nkeynes@608
    37
 0x69, 0x20, 0x20, 0x20 }
nkeynes@608
    38
nkeynes@608
    39
#define BUTTON_MIDDLE 0x01
nkeynes@608
    40
#define BUTTON_RIGHT  0x02
nkeynes@608
    41
#define BUTTON_LEFT   0x04
nkeynes@608
    42
#define BUTTON_THUMB  0x08
nkeynes@608
    43
nkeynes@608
    44
void mouse_attach( maple_device_t dev );
nkeynes@608
    45
void mouse_detach( maple_device_t dev );
nkeynes@608
    46
maple_device_t mouse_clone( maple_device_t dev );
nkeynes@608
    47
maple_device_t mouse_new();
nkeynes@608
    48
int mouse_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
nkeynes@608
    49
                         unsigned int *outlen );
nkeynes@608
    50
nkeynes@608
    51
typedef struct mouse_device {
nkeynes@608
    52
    struct maple_device dev;
nkeynes@608
    53
    uint32_t buttons;
nkeynes@608
    54
    int32_t axis[8];
nkeynes@608
    55
} *mouse_device_t; 
nkeynes@608
    56
nkeynes@608
    57
struct maple_device_class mouse_class = { "Sega Mouse", mouse_new };
nkeynes@608
    58
nkeynes@608
    59
static struct mouse_device base_mouse = {
nkeynes@608
    60
    { MAPLE_DEVICE_TAG, &mouse_class, MOUSE_IDENT, MOUSE_VERSION, 
nkeynes@608
    61
      NULL, mouse_attach, mouse_detach, maple_default_destroy,
nkeynes@608
    62
      mouse_clone, NULL, NULL, mouse_get_cond, NULL, NULL, NULL },
nkeynes@608
    63
    0, {0,0,0,0,0,0,0,0}, 
nkeynes@608
    64
};
nkeynes@608
    65
nkeynes@608
    66
static int32_t mouse_axis_scale_factors[8] = { 10, 10, 1, 1, 1, 1, 1, 1 };
nkeynes@608
    67
nkeynes@608
    68
#define MOUSE(x) ((mouse_device_t)(x))
nkeynes@608
    69
nkeynes@608
    70
maple_device_t mouse_new( )
nkeynes@608
    71
{
nkeynes@608
    72
    mouse_device_t dev = malloc( sizeof(struct mouse_device) );
nkeynes@608
    73
    memcpy( dev, &base_mouse, sizeof(base_mouse) );
nkeynes@608
    74
    return MAPLE_DEVICE(dev);
nkeynes@608
    75
}
nkeynes@608
    76
nkeynes@608
    77
maple_device_t mouse_clone( maple_device_t srcdevice )
nkeynes@608
    78
{
nkeynes@608
    79
    mouse_device_t src = (mouse_device_t)srcdevice;
nkeynes@608
    80
    mouse_device_t dev = (mouse_device_t)mouse_new();
nkeynes@608
    81
    dev->buttons = src->buttons;
nkeynes@608
    82
    memcpy( dev->axis, src->axis, sizeof(src->axis) );
nkeynes@608
    83
    return MAPLE_DEVICE(dev);
nkeynes@608
    84
}
nkeynes@608
    85
nkeynes@608
    86
void mouse_input_callback( void *mdev, uint32_t buttons, int32_t x, int32_t y )
nkeynes@608
    87
{
nkeynes@608
    88
    mouse_device_t dev = (mouse_device_t)mdev;
nkeynes@634
    89
    dev->buttons = 0xFF;
nkeynes@608
    90
    if( buttons & 0x01 ) {
nkeynes@634
    91
	dev->buttons &= ~BUTTON_LEFT;
nkeynes@608
    92
    }
nkeynes@608
    93
    if( buttons & 0x02 ) {
nkeynes@634
    94
	dev->buttons &= ~BUTTON_MIDDLE;
nkeynes@608
    95
    } 
nkeynes@608
    96
    if( buttons & 0x04 ) {
nkeynes@634
    97
	dev->buttons &= ~BUTTON_RIGHT;
nkeynes@608
    98
    }
nkeynes@608
    99
    if( buttons & 0x08 ) {
nkeynes@634
   100
	dev->buttons &= ~BUTTON_THUMB;
nkeynes@608
   101
    }
nkeynes@608
   102
    dev->axis[0] += x;
nkeynes@608
   103
    dev->axis[1] += y;
nkeynes@608
   104
}
nkeynes@608
   105
nkeynes@608
   106
void mouse_attach( maple_device_t dev )
nkeynes@608
   107
{
nkeynes@608
   108
    input_register_mouse_hook( TRUE, mouse_input_callback, dev );
nkeynes@608
   109
}
nkeynes@608
   110
nkeynes@608
   111
void mouse_detach( maple_device_t dev )
nkeynes@608
   112
{
nkeynes@608
   113
    input_unregister_mouse_hook( mouse_input_callback, dev );
nkeynes@608
   114
}
nkeynes@608
   115
nkeynes@608
   116
int mouse_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
nkeynes@608
   117
		       unsigned int *outlen )
nkeynes@608
   118
{
nkeynes@608
   119
    mouse_device_t dev = (mouse_device_t)mdev;
nkeynes@608
   120
    if( function == MAPLE_FUNC_MOUSE ) {
nkeynes@608
   121
        *outlen = 5;
nkeynes@608
   122
	*(uint32_t *)outbuf = dev->buttons;
nkeynes@608
   123
	uint16_t *p = (uint16_t *)(outbuf+4);
nkeynes@608
   124
	int i;
nkeynes@608
   125
	// Axis values are in the range 0..0x3FF, where 0x200 is zero movement
nkeynes@608
   126
	for( i=0; i<8; i++ ) {
nkeynes@608
   127
	    int32_t value = dev->axis[i] / mouse_axis_scale_factors[i];
nkeynes@608
   128
	    if( value < -0x200 ) {
nkeynes@608
   129
		p[i] = 0;
nkeynes@608
   130
	    } else if( value > 0x1FF ) {
nkeynes@608
   131
		p[i] = 0x3FF;
nkeynes@608
   132
	    } else {
nkeynes@608
   133
		p[i] = 0x200 + value;
nkeynes@608
   134
	    }
nkeynes@608
   135
	    dev->axis[i] = 0; // clear after returning.
nkeynes@608
   136
	}
nkeynes@608
   137
        return 0;
nkeynes@608
   138
    } else {
nkeynes@608
   139
        return MAPLE_ERR_FUNC_UNSUP;
nkeynes@608
   140
    }
nkeynes@608
   141
}
nkeynes@608
   142
.