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