Search
lxdream.org :: lxdream/src/maple/kbd.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/kbd.c
changeset 1072:d82e04e6d497
prev1034:7044e01148f0
next1224:7762a347ca33
author nkeynes
date Fri Dec 04 18:06:12 2009 +1000 (14 years ago)
permissions -rw-r--r--
last change Fix crash on vmu device create introduced in r1072 - config was being
initialised in the wrong order
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Implements the standard dreamcast keyboard
     5  * Part No. HKT-7620
     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  */
    20 #include <stdlib.h>
    21 #include <X11/keysym.h>
    22 #include "dream.h"
    23 #include "dreamcast.h"
    24 #include "display.h"
    25 #include "maple.h"
    27 #define KEYBOARD_IDENT {  0x00, 0x00, 0x00, 0x40,  0x02, 0x05, 0x00, 0x80,  0x00, 0x00, 0x00, 0x00, \
    28     0x00, 0x00, 0x00, 0x00,  0x01, 0x00, 0x4b, 0x65,  0x79, 0x62, 0x6f, 0x61,  0x72, 0x64, 0x20, 0x20, \
    29     0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20, \
    30     0x20, 0x20, 0x20, 0x20,  0x50, 0x72, 0x6f, 0x64,  0x75, 0x63, 0x65, 0x64,  0x20, 0x42, 0x79, 0x20, \
    31     0x6f, 0x72, 0x20, 0x55,  0x6e, 0x64, 0x65, 0x72,  0x20, 0x4c, 0x69, 0x63,  0x65, 0x6e, 0x73, 0x65, \
    32     0x20, 0x46, 0x72, 0x6f,  0x6d, 0x20, 0x53, 0x45,  0x47, 0x41, 0x20, 0x45,  0x4e, 0x54, 0x45, 0x52, \
    33     0x50, 0x52, 0x49, 0x53,  0x45, 0x53, 0x2c, 0x4c,  0x54, 0x44, 0x2e, 0x20,  0x20, 0x20, 0x20, 0x20, \
    34     0x2c, 0x01, 0x90, 0x01 }
    36 #define KEYBOARD_VERSION {0x56, 0x65, 0x72, 0x73,  0x69, 0x6f, 0x6e, 0x20,  0x31, 0x2e, 0x30, 0x31, \
    37     0x30, 0x2c, 0x31, 0x39,  0x39, 0x39, 0x2f, 0x30,  0x34, 0x2f, 0x32, 0x37,  0x2c, 0x33, 0x31, 0x35, \
    38     0x2d, 0x36, 0x32, 0x31,  0x31, 0x2d, 0x41, 0x4d,  0x20, 0x20, 0x20, 0x2c,  0x4b, 0x65, 0x79, 0x20, \
    39     0x53, 0x63, 0x61, 0x6e,  0x20, 0x4d, 0x6f, 0x64,  0x75, 0x6c, 0x65, 0x20,  0x3a, 0x20, 0x54, 0x68, \
    40     0x65, 0x20, 0x32, 0x6e,  0x64, 0x20, 0x45, 0x64,  0x69, 0x74, 0x69, 0x6f,  0x6e, 0x2e, 0x20, 0x30, \
    41     0x34, 0x2f, 0x32, 0x35 }
    43 void keyboard_attach( maple_device_t dev );
    44 void keyboard_detach( maple_device_t dev );
    45 maple_device_t keyboard_clone( maple_device_t dev );
    46 maple_device_t keyboard_new();
    47 int keyboard_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
    48                        unsigned int *outlen );
    50 typedef struct keyboard_device {
    51     struct maple_device dev;
    52     uint8_t condition[8];
    53 } *keyboard_device_t; 
    55 struct maple_device_class keyboard_class = { "Sega Keyboard", MAPLE_GRAB_DONTCARE|MAPLE_TYPE_PRIMARY, keyboard_new };
    57 static struct keyboard_device base_keyboard = {
    58         { MAPLE_DEVICE_TAG, &keyboard_class,
    59                 KEYBOARD_IDENT, KEYBOARD_VERSION, 
    60                 NULL, keyboard_attach, keyboard_detach, maple_default_destroy,
    61                 keyboard_clone, NULL, NULL, keyboard_get_cond, NULL, NULL, NULL,
    62                 NULL, NULL, NULL},
    63                 {0,0,0,0,0,0,0,0}, 
    64 };
    66 #define KEYBOARD(x) ((keyboard_device_t)(x))
    68 maple_device_t keyboard_new( )
    69 {
    70     keyboard_device_t dev = malloc( sizeof(struct keyboard_device) );
    71     memcpy( dev, &base_keyboard, sizeof(base_keyboard) );
    72     return MAPLE_DEVICE(dev);
    73 }
    75 maple_device_t keyboard_clone( maple_device_t srcdevice )
    76 {
    77     keyboard_device_t src = (keyboard_device_t)srcdevice;
    78     keyboard_device_t dev = (keyboard_device_t)keyboard_new();
    79     memcpy( dev->condition, src->condition, sizeof(src->condition) );
    80     return MAPLE_DEVICE(dev);
    81 }
    83 void keyboard_key_down( keyboard_device_t dev, uint8_t key )
    84 {
    85     int i;
    86     for( i=2; i<8; i++ ) {
    87         if( dev->condition[i] == key ) {
    88             return; // key already down, missed event or repeat
    89         } else if( dev->condition[i] == 0 ) {
    90             dev->condition[i] = key;
    91             return;
    92         }
    93     }
    94     /* Key array is full - skip for the moment */
    95 }
    97 void keyboard_key_up( keyboard_device_t dev, uint8_t key )
    98 {
    99     int i;
   100     for( i=2; i<8; i++ ) {
   101         if( dev->condition[i] == key ) {
   102             for( ; i<7; i++ ) {
   103                 dev->condition[i] = dev->condition[i+1];
   104             }
   105             dev->condition[7] = 0;
   106             break;
   107         }
   108     }
   109 }
   111 void keyboard_input_hook( void *mdev, uint32_t keycode, uint32_t pressure, gboolean isKeyDown )
   112 {
   113     keyboard_device_t dev = (keyboard_device_t)mdev;
   114     uint16_t key = input_keycode_to_dckeysym( keycode );
   115     if( key != 0 ) {
   116         if( key >> 8 == 0xFF ) { // shift 
   117             if( isKeyDown ) {
   118                 dev->condition[0] |= (key&0xFF);
   119             } else {
   120                 dev->condition[0] &= ~(key&0xFF);
   121             }
   122         } else {
   123             if( isKeyDown ) {
   124                 keyboard_key_down( dev, (uint8_t)key );
   125             } else {
   126                 keyboard_key_up( dev, (uint8_t)key );
   127             }
   128         }
   129     }
   130     /*
   131     fprintf( stderr, "Key cond: %02X %02X  %02X %02X %02X %02X %02X %02X\n",
   132 	     dev->condition[0], dev->condition[1], dev->condition[2],
   133 	     dev->condition[3], dev->condition[4], dev->condition[5],
   134 	     dev->condition[6], dev->condition[7] );
   135      */     
   136 }
   138 /**
   139  * Device is being attached to the bus. Go through the config and reserve the
   140  * keys we need.
   141  */
   142 void keyboard_attach( maple_device_t mdev )
   143 {
   144     input_register_keyboard_hook( keyboard_input_hook, mdev );
   145 }
   147 void keyboard_detach( maple_device_t mdev )
   148 {
   149     input_unregister_keyboard_hook( keyboard_input_hook, mdev );
   150 }
   153 int keyboard_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
   154                        unsigned int *outlen )
   155 {
   156     keyboard_device_t dev = (keyboard_device_t)mdev;
   157     if( function == MAPLE_FUNC_KEYBOARD ) {
   158         *outlen = 2;
   159         memcpy( outbuf, dev->condition, 8 );
   160         return 0;
   161     } else {
   162         return MAPLE_ERR_FUNC_UNSUP;
   163     }
   164 }
.