Search
lxdream.org :: lxdream/src/maple/mouse.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/mouse.c
changeset 839:51f1c4195790
prev838:9abb2fa58934
next848:34dc33c05106
author nkeynes
date Tue Sep 02 03:34:00 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Implement absolute positioning mouse mode when not grabbed
enable/disable grab on dreamcast start/stop where it's requested
by the controllers
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 "display.h"
    22 #include "maple/maple.h"
    24 #define MOUSE_IDENT     { 0x00, 0x00, 0x02, 0x00,  0x02, 0x00, 0x07, 0x00,  0x00, 0x00, 0x00, 0x00,\
    25     0x00, 0x00, 0x00, 0x00,  0xff, 0x00, 0x44, 0x72,  0x65, 0x61, 0x6d, 0x63,  0x61, 0x73, 0x74, 0x20,\
    26     0x4d, 0x6f, 0x75, 0x73,  0x65, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,\
    27     0x20, 0x20, 0x20, 0x20,  0x50, 0x72, 0x6f, 0x64,  0x75, 0x63, 0x65, 0x64,  0x20, 0x42, 0x79, 0x20,\
    28     0x6f, 0x72, 0x20, 0x55,  0x6e, 0x64, 0x65, 0x72,  0x20, 0x4c, 0x69, 0x63,  0x65, 0x6e, 0x73, 0x65,\
    29     0x20, 0x46, 0x72, 0x6f,  0x6d, 0x20, 0x53, 0x45,  0x47, 0x41, 0x20, 0x45,  0x4e, 0x54, 0x45, 0x52,\
    30     0x50, 0x52, 0x49, 0x53,  0x45, 0x53, 0x2c, 0x4c,  0x54, 0x44, 0x2e, 0x20,  0x20, 0x20, 0x20, 0x20,\
    31     0x90, 0x01, 0xf4, 0x01 }
    32 #define MOUSE_VERSION   { 0x56, 0x65, 0x72, 0x73,  0x69, 0x6f, 0x6e, 0x20,  0x31, 0x2e, 0x30, 0x30,\
    33     0x30, 0x2c, 0x32, 0x30,  0x30, 0x30, 0x2f, 0x30,  0x32, 0x2f, 0x32, 0x35,  0x2c, 0x33, 0x31, 0x35,\
    34     0x2d, 0x36, 0x32, 0x31,  0x31, 0x2d, 0x41, 0x54,  0x20, 0x20, 0x20, 0x2c,  0x33, 0x20, 0x42, 0x75,\
    35     0x74, 0x74, 0x6f, 0x6e,  0x20, 0x26, 0x20, 0x58,  0x2d, 0x59, 0x20, 0x42,  0x61, 0x6c, 0x6c, 0x20,\
    36     0x26, 0x20, 0x5a, 0x20,  0x57, 0x68, 0x65, 0x65,  0x6c, 0x20, 0x2c, 0x34,  0x30, 0x30, 0x64, 0x70,\
    37     0x69, 0x20, 0x20, 0x20 }
    39 #define BUTTON_MIDDLE 0x01
    40 #define BUTTON_RIGHT  0x02
    41 #define BUTTON_LEFT   0x04
    42 #define BUTTON_THUMB  0x08
    44 void mouse_attach( maple_device_t dev );
    45 void mouse_detach( maple_device_t dev );
    46 maple_device_t mouse_clone( maple_device_t dev );
    47 maple_device_t mouse_new();
    48 int mouse_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
    49                     unsigned int *outlen );
    51 typedef struct mouse_device {
    52     struct maple_device dev;
    53     uint32_t buttons;
    54     int32_t axis[8];
    55 } *mouse_device_t; 
    57 struct maple_device_class mouse_class = { "Sega Mouse", mouse_new };
    59 static struct mouse_device base_mouse = {
    60         { MAPLE_DEVICE_TAG, &mouse_class, MAPLE_GRAB_YES,
    61                 MOUSE_IDENT, MOUSE_VERSION, 
    62                 NULL, NULL, mouse_attach, mouse_detach, maple_default_destroy,
    63                 mouse_clone, NULL, NULL, mouse_get_cond, NULL, NULL, NULL },
    64                 0, {0,0,0,0,0,0,0,0}, 
    65 };
    67 static int32_t mouse_axis_scale_factors[8] = { 10, 10, 1, 1, 1, 1, 1, 1 };
    69 #define MOUSE(x) ((mouse_device_t)(x))
    71 maple_device_t mouse_new( )
    72 {
    73     mouse_device_t dev = malloc( sizeof(struct mouse_device) );
    74     memcpy( dev, &base_mouse, sizeof(base_mouse) );
    75     return MAPLE_DEVICE(dev);
    76 }
    78 maple_device_t mouse_clone( maple_device_t srcdevice )
    79 {
    80     mouse_device_t src = (mouse_device_t)srcdevice;
    81     mouse_device_t dev = (mouse_device_t)mouse_new();
    82     dev->buttons = src->buttons;
    83     memcpy( dev->axis, src->axis, sizeof(src->axis) );
    84     return MAPLE_DEVICE(dev);
    85 }
    87 void mouse_input_callback( void *mdev, uint32_t buttons, int32_t x, int32_t y, gboolean absolute )
    88 {
    89     mouse_device_t dev = (mouse_device_t)mdev;
    90     if( !absolute ) {
    91         dev->buttons = 0xFF;
    92         if( buttons & 0x01 ) {
    93             dev->buttons &= ~BUTTON_LEFT;
    94         }
    95         if( buttons & 0x02 ) {
    96             dev->buttons &= ~BUTTON_MIDDLE;
    97         } 
    98         if( buttons & 0x04 ) {
    99             dev->buttons &= ~BUTTON_RIGHT;
   100         }
   101         if( buttons & 0x08 ) {
   102             dev->buttons &= ~BUTTON_THUMB;
   103         }
   104         dev->axis[0] += x;
   105         dev->axis[1] += y;
   106     }
   107 }
   109 void mouse_attach( maple_device_t dev )
   110 {
   111     input_register_mouse_hook( TRUE, mouse_input_callback, dev );
   112 }
   114 void mouse_detach( maple_device_t dev )
   115 {
   116     input_unregister_mouse_hook( mouse_input_callback, dev );
   117 }
   119 int mouse_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
   120                     unsigned int *outlen )
   121 {
   122     mouse_device_t dev = (mouse_device_t)mdev;
   123     if( function == MAPLE_FUNC_MOUSE ) {
   124         *outlen = 5;
   125         *(uint32_t *)outbuf = dev->buttons;
   126         uint16_t *p = (uint16_t *)(outbuf+4);
   127         int i;
   128         // Axis values are in the range 0..0x3FF, where 0x200 is zero movement
   129         for( i=0; i<8; i++ ) {
   130             int32_t value = dev->axis[i] / mouse_axis_scale_factors[i];
   131             if( value < -0x200 ) {
   132                 p[i] = 0;
   133             } else if( value > 0x1FF ) {
   134                 p[i] = 0x3FF;
   135             } else {
   136                 p[i] = 0x200 + value;
   137             }
   138             dev->axis[i] = 0; // clear after returning.
   139         }
   140         return 0;
   141     } else {
   142         return MAPLE_ERR_FUNC_UNSUP;
   143     }
   144 }
.