Search
lxdream.org :: lxdream/src/maple/mouse.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/mouse.c
changeset 608:4f588e52bce0
next611:0c5f53a87501
author nkeynes
date Sat Jan 26 02:45:27 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Bug #50: Implement mouse and keyboard
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/maple/mouse.c Sat Jan 26 02:45:27 2008 +0000
1.3 @@ -0,0 +1,141 @@
1.4 +/**
1.5 + * $Id: mouse.c 602 2008-01-15 20:50:23Z nkeynes $
1.6 + *
1.7 + * Implementation of the standard mouse device
1.8 + *
1.9 + * Copyright (c) 2005 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +#include <stdlib.h>
1.22 +#include <stdio.h>
1.23 +#include <string.h>
1.24 +#include "maple/maple.h"
1.25 +
1.26 +#define MOUSE_IDENT { 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,\
1.27 + 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x44, 0x72, 0x65, 0x61, 0x6d, 0x63, 0x61, 0x73, 0x74, 0x20,\
1.28 + 0x4d, 0x6f, 0x75, 0x73, 0x65, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,\
1.29 + 0x20, 0x20, 0x20, 0x20, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x42, 0x79, 0x20,\
1.30 + 0x6f, 0x72, 0x20, 0x55, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65,\
1.31 + 0x20, 0x46, 0x72, 0x6f, 0x6d, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x45, 0x4e, 0x54, 0x45, 0x52,\
1.32 + 0x50, 0x52, 0x49, 0x53, 0x45, 0x53, 0x2c, 0x4c, 0x54, 0x44, 0x2e, 0x20, 0x20, 0x20, 0x20, 0x20,\
1.33 + 0x90, 0x01, 0xf4, 0x01 }
1.34 +#define MOUSE_VERSION { 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x2e, 0x30, 0x30,\
1.35 + 0x30, 0x2c, 0x32, 0x30, 0x30, 0x30, 0x2f, 0x30, 0x32, 0x2f, 0x32, 0x35, 0x2c, 0x33, 0x31, 0x35,\
1.36 + 0x2d, 0x36, 0x32, 0x31, 0x31, 0x2d, 0x41, 0x54, 0x20, 0x20, 0x20, 0x2c, 0x33, 0x20, 0x42, 0x75,\
1.37 + 0x74, 0x74, 0x6f, 0x6e, 0x20, 0x26, 0x20, 0x58, 0x2d, 0x59, 0x20, 0x42, 0x61, 0x6c, 0x6c, 0x20,\
1.38 + 0x26, 0x20, 0x5a, 0x20, 0x57, 0x68, 0x65, 0x65, 0x6c, 0x20, 0x2c, 0x34, 0x30, 0x30, 0x64, 0x70,\
1.39 + 0x69, 0x20, 0x20, 0x20 }
1.40 +
1.41 +#define BUTTON_MIDDLE 0x01
1.42 +#define BUTTON_RIGHT 0x02
1.43 +#define BUTTON_LEFT 0x04
1.44 +#define BUTTON_THUMB 0x08
1.45 +
1.46 +void mouse_attach( maple_device_t dev );
1.47 +void mouse_detach( maple_device_t dev );
1.48 +maple_device_t mouse_clone( maple_device_t dev );
1.49 +maple_device_t mouse_new();
1.50 +int mouse_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
1.51 + unsigned int *outlen );
1.52 +
1.53 +typedef struct mouse_device {
1.54 + struct maple_device dev;
1.55 + uint32_t buttons;
1.56 + int32_t axis[8];
1.57 +} *mouse_device_t;
1.58 +
1.59 +struct maple_device_class mouse_class = { "Sega Mouse", mouse_new };
1.60 +
1.61 +static struct mouse_device base_mouse = {
1.62 + { MAPLE_DEVICE_TAG, &mouse_class, MOUSE_IDENT, MOUSE_VERSION,
1.63 + NULL, mouse_attach, mouse_detach, maple_default_destroy,
1.64 + mouse_clone, NULL, NULL, mouse_get_cond, NULL, NULL, NULL },
1.65 + 0, {0,0,0,0,0,0,0,0},
1.66 +};
1.67 +
1.68 +static int32_t mouse_axis_scale_factors[8] = { 10, 10, 1, 1, 1, 1, 1, 1 };
1.69 +
1.70 +#define MOUSE(x) ((mouse_device_t)(x))
1.71 +
1.72 +maple_device_t mouse_new( )
1.73 +{
1.74 + mouse_device_t dev = malloc( sizeof(struct mouse_device) );
1.75 + memcpy( dev, &base_mouse, sizeof(base_mouse) );
1.76 + return MAPLE_DEVICE(dev);
1.77 +}
1.78 +
1.79 +maple_device_t mouse_clone( maple_device_t srcdevice )
1.80 +{
1.81 + mouse_device_t src = (mouse_device_t)srcdevice;
1.82 + mouse_device_t dev = (mouse_device_t)mouse_new();
1.83 + dev->buttons = src->buttons;
1.84 + memcpy( dev->axis, src->axis, sizeof(src->axis) );
1.85 + return MAPLE_DEVICE(dev);
1.86 +}
1.87 +
1.88 +void mouse_input_callback( void *mdev, uint32_t buttons, int32_t x, int32_t y )
1.89 +{
1.90 + mouse_device_t dev = (mouse_device_t)mdev;
1.91 + dev->buttons = 0;
1.92 + if( buttons & 0x01 ) {
1.93 + dev->buttons |= BUTTON_LEFT;
1.94 + }
1.95 + if( buttons & 0x02 ) {
1.96 + dev->buttons |= BUTTON_MIDDLE;
1.97 + }
1.98 + if( buttons & 0x04 ) {
1.99 + dev->buttons |= BUTTON_RIGHT;
1.100 + }
1.101 + if( buttons & 0x08 ) {
1.102 + dev->buttons |= BUTTON_THUMB;
1.103 + }
1.104 + dev->axis[0] += x;
1.105 + dev->axis[1] += y;
1.106 +}
1.107 +
1.108 +void mouse_attach( maple_device_t dev )
1.109 +{
1.110 + input_register_mouse_hook( TRUE, mouse_input_callback, dev );
1.111 +}
1.112 +
1.113 +void mouse_detach( maple_device_t dev )
1.114 +{
1.115 + input_unregister_mouse_hook( mouse_input_callback, dev );
1.116 +}
1.117 +
1.118 +int mouse_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
1.119 + unsigned int *outlen )
1.120 +{
1.121 + mouse_device_t dev = (mouse_device_t)mdev;
1.122 + if( function == MAPLE_FUNC_MOUSE ) {
1.123 + *outlen = 5;
1.124 + *(uint32_t *)outbuf = dev->buttons;
1.125 + uint16_t *p = (uint16_t *)(outbuf+4);
1.126 + int i;
1.127 + // Axis values are in the range 0..0x3FF, where 0x200 is zero movement
1.128 + for( i=0; i<8; i++ ) {
1.129 + int32_t value = dev->axis[i] / mouse_axis_scale_factors[i];
1.130 + if( value < -0x200 ) {
1.131 + p[i] = 0;
1.132 + } else if( value > 0x1FF ) {
1.133 + p[i] = 0x3FF;
1.134 + } else {
1.135 + p[i] = 0x200 + value;
1.136 + }
1.137 + dev->axis[i] = 0; // clear after returning.
1.138 + }
1.139 + return 0;
1.140 + } else {
1.141 + return MAPLE_ERR_FUNC_UNSUP;
1.142 + }
1.143 +}
1.144 +
.