Search
lxdream.org :: lxdream/src/maple/kbd.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/maple/kbd.c
changeset 608:4f588e52bce0
next611:0c5f53a87501
author nkeynes
date Sat Jan 26 02:45:27 2008 +0000 (16 years ago)
permissions -rw-r--r--
last change Bug #50: Implement mouse and keyboard
view annotate diff log raw
     1 /**
     2  * $Id: kbd.c 602 2008-01-15 20:50:23Z nkeynes $
     3  *
     4  * Implements the standard dreamcast keyboard
     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  */
    19 #include <stdlib.h>
    20 #include <X11/keysym.h>
    21 #include "dream.h"
    22 #include "dreamcast.h"
    23 #include "display.h"
    24 #include "maple.h"
    26 #define KEYBOARD_IDENT {  0x00, 0x00, 0x00, 0x40,  0x02, 0x05, 0x00, 0x80,  0x00, 0x00, 0x00, 0x00, \
    27  0x00, 0x00, 0x00, 0x00,  0x01, 0x00, 0x4b, 0x65,  0x79, 0x62, 0x6f, 0x61,  0x72, 0x64, 0x20, 0x20, \
    28  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20,  0x20, 0x20, 0x20, 0x20, \
    29  0x20, 0x20, 0x20, 0x20,  0x50, 0x72, 0x6f, 0x64,  0x75, 0x63, 0x65, 0x64,  0x20, 0x42, 0x79, 0x20, \
    30  0x6f, 0x72, 0x20, 0x55,  0x6e, 0x64, 0x65, 0x72,  0x20, 0x4c, 0x69, 0x63,  0x65, 0x6e, 0x73, 0x65, \
    31  0x20, 0x46, 0x72, 0x6f,  0x6d, 0x20, 0x53, 0x45,  0x47, 0x41, 0x20, 0x45,  0x4e, 0x54, 0x45, 0x52, \
    32  0x50, 0x52, 0x49, 0x53,  0x45, 0x53, 0x2c, 0x4c,  0x54, 0x44, 0x2e, 0x20,  0x20, 0x20, 0x20, 0x20, \
    33  0x2c, 0x01, 0x90, 0x01 }
    35 #define KEYBOARD_VERSION {0x56, 0x65, 0x72, 0x73,  0x69, 0x6f, 0x6e, 0x20,  0x31, 0x2e, 0x30, 0x31, \
    36  0x30, 0x2c, 0x31, 0x39,  0x39, 0x39, 0x2f, 0x30,  0x34, 0x2f, 0x32, 0x37,  0x2c, 0x33, 0x31, 0x35, \
    37  0x2d, 0x36, 0x32, 0x31,  0x31, 0x2d, 0x41, 0x4d,  0x20, 0x20, 0x20, 0x2c,  0x4b, 0x65, 0x79, 0x20, \
    38  0x53, 0x63, 0x61, 0x6e,  0x20, 0x4d, 0x6f, 0x64,  0x75, 0x6c, 0x65, 0x20,  0x3a, 0x20, 0x54, 0x68, \
    39  0x65, 0x20, 0x32, 0x6e,  0x64, 0x20, 0x45, 0x64,  0x69, 0x74, 0x69, 0x6f,  0x6e, 0x2e, 0x20, 0x30, \
    40  0x34, 0x2f, 0x32, 0x35 }
    42 void keyboard_attach( maple_device_t dev );
    43 void keyboard_detach( maple_device_t dev );
    44 maple_device_t keyboard_clone( maple_device_t dev );
    45 maple_device_t keyboard_new();
    46 int keyboard_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
    47                          unsigned int *outlen );
    49 typedef struct keyboard_device {
    50     struct maple_device dev;
    51     uint8_t condition[8];
    52 } *keyboard_device_t; 
    54 struct maple_device_class keyboard_class = { "Sega Keyboard", keyboard_new };
    56 static struct keyboard_device base_keyboard = {
    57     { MAPLE_DEVICE_TAG, &keyboard_class, KEYBOARD_IDENT, KEYBOARD_VERSION, 
    58       NULL, keyboard_attach, keyboard_detach, maple_default_destroy,
    59       keyboard_clone, NULL, NULL, keyboard_get_cond, NULL, NULL, NULL },
    60     {0,0,0,0,0,0,0,0}, 
    61 };
    63 #define KEYBOARD(x) ((keyboard_device_t)(x))
    65 maple_device_t keyboard_new( )
    66 {
    67     keyboard_device_t dev = malloc( sizeof(struct keyboard_device) );
    68     memcpy( dev, &base_keyboard, sizeof(base_keyboard) );
    69     return MAPLE_DEVICE(dev);
    70 }
    72 maple_device_t keyboard_clone( maple_device_t srcdevice )
    73 {
    74     keyboard_device_t src = (keyboard_device_t)srcdevice;
    75     keyboard_device_t dev = (keyboard_device_t)keyboard_new();
    76     memcpy( dev->condition, src->condition, sizeof(src->condition) );
    77     return MAPLE_DEVICE(dev);
    78 }
    80 void keyboard_key_down( keyboard_device_t dev, uint8_t key )
    81 {
    82     int i;
    83     for( i=2; i<8; i++ ) {
    84 	if( dev->condition[i] == key ) {
    85 	    return; // key already down, missed event or repeat
    86 	} else if( dev->condition[i] == 0 ) {
    87 	    dev->condition[i] = key;
    88 	    return;
    89 	}
    90     }
    91     /* Key array is full - skip for the moment */
    92 }
    94 void keyboard_key_up( keyboard_device_t dev, uint8_t key )
    95 {
    96     int i;
    97     for( i=2; i<8; i++ ) {
    98 	if( dev->condition[i] == key ) {
    99 	    for( ; i<7; i++ ) {
   100 		dev->condition[i] = dev->condition[i+1];
   101 	    }
   102 	    dev->condition[7] = 0;
   103 	    break;
   104 	}
   105     }
   106 }
   108 void keyboard_input_hook( void *mdev, uint32_t keycode, gboolean isKeyDown )
   109 {
   110     keyboard_device_t dev = (keyboard_device_t)mdev;
   111     uint16_t key = input_keycode_to_dckeysym( keycode );
   112     if( key != 0 ) {
   113 	if( key >> 8 == 0xFF ) { // shift 
   114 	    if( isKeyDown ) {
   115 		dev->condition[0] |= (key&0xFF);
   116 	    } else {
   117 		dev->condition[0] &= ~(key&0xFF);
   118 	    }
   119 	} else {
   120 	    if( isKeyDown ) {
   121 		keyboard_key_down( dev, (uint8_t)key );
   122 	    } else {
   123 		keyboard_key_up( dev, (uint8_t)key );
   124 	    }
   125 	}
   126     }
   127     /*
   128     fprintf( stderr, "Key cond: %02X %02X  %02X %02X %02X %02X %02X %02X\n",
   129 	     dev->condition[0], dev->condition[1], dev->condition[2],
   130 	     dev->condition[3], dev->condition[4], dev->condition[5],
   131 	     dev->condition[6], dev->condition[7] );
   132     */     
   133 }
   135 /**
   136  * Device is being attached to the bus. Go through the config and reserve the
   137  * keys we need.
   138  */
   139 void keyboard_attach( maple_device_t mdev )
   140 {
   141     input_register_hook( keyboard_input_hook, mdev );
   142 }
   144 void keyboard_detach( maple_device_t mdev )
   145 {
   146     input_unregister_hook( keyboard_input_hook, mdev );
   147 }
   150 int keyboard_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
   151 		       unsigned int *outlen )
   152 {
   153     keyboard_device_t dev = (keyboard_device_t)mdev;
   154     if( function == MAPLE_FUNC_KEYBOARD ) {
   155         *outlen = 2;
   156         memcpy( outbuf, dev->condition, 8 );
   157         return 0;
   158     } else {
   159         return MAPLE_ERR_FUNC_UNSUP;
   160     }
   161 }
.