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