Search
lxdream.org :: lxdream/src/maple/kbd.c :: diff
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 (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/kbd.c Sat Jan 26 02:45:27 2008 +0000
1.3 @@ -0,0 +1,162 @@
1.4 +/**
1.5 + * $Id: kbd.c 602 2008-01-15 20:50:23Z nkeynes $
1.6 + *
1.7 + * Implements the standard dreamcast keyboard
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 +
1.22 +#include <stdlib.h>
1.23 +#include <X11/keysym.h>
1.24 +#include "dream.h"
1.25 +#include "dreamcast.h"
1.26 +#include "display.h"
1.27 +#include "maple.h"
1.28 +
1.29 +#define KEYBOARD_IDENT { 0x00, 0x00, 0x00, 0x40, 0x02, 0x05, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, \
1.30 + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x4b, 0x65, 0x79, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x20, 0x20, \
1.31 + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, \
1.32 + 0x20, 0x20, 0x20, 0x20, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x42, 0x79, 0x20, \
1.33 + 0x6f, 0x72, 0x20, 0x55, 0x6e, 0x64, 0x65, 0x72, 0x20, 0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, \
1.34 + 0x20, 0x46, 0x72, 0x6f, 0x6d, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x45, 0x4e, 0x54, 0x45, 0x52, \
1.35 + 0x50, 0x52, 0x49, 0x53, 0x45, 0x53, 0x2c, 0x4c, 0x54, 0x44, 0x2e, 0x20, 0x20, 0x20, 0x20, 0x20, \
1.36 + 0x2c, 0x01, 0x90, 0x01 }
1.37 +
1.38 +#define KEYBOARD_VERSION {0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x2e, 0x30, 0x31, \
1.39 + 0x30, 0x2c, 0x31, 0x39, 0x39, 0x39, 0x2f, 0x30, 0x34, 0x2f, 0x32, 0x37, 0x2c, 0x33, 0x31, 0x35, \
1.40 + 0x2d, 0x36, 0x32, 0x31, 0x31, 0x2d, 0x41, 0x4d, 0x20, 0x20, 0x20, 0x2c, 0x4b, 0x65, 0x79, 0x20, \
1.41 + 0x53, 0x63, 0x61, 0x6e, 0x20, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x20, 0x3a, 0x20, 0x54, 0x68, \
1.42 + 0x65, 0x20, 0x32, 0x6e, 0x64, 0x20, 0x45, 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x20, 0x30, \
1.43 + 0x34, 0x2f, 0x32, 0x35 }
1.44 +
1.45 +void keyboard_attach( maple_device_t dev );
1.46 +void keyboard_detach( maple_device_t dev );
1.47 +maple_device_t keyboard_clone( maple_device_t dev );
1.48 +maple_device_t keyboard_new();
1.49 +int keyboard_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
1.50 + unsigned int *outlen );
1.51 +
1.52 +typedef struct keyboard_device {
1.53 + struct maple_device dev;
1.54 + uint8_t condition[8];
1.55 +} *keyboard_device_t;
1.56 +
1.57 +struct maple_device_class keyboard_class = { "Sega Keyboard", keyboard_new };
1.58 +
1.59 +static struct keyboard_device base_keyboard = {
1.60 + { MAPLE_DEVICE_TAG, &keyboard_class, KEYBOARD_IDENT, KEYBOARD_VERSION,
1.61 + NULL, keyboard_attach, keyboard_detach, maple_default_destroy,
1.62 + keyboard_clone, NULL, NULL, keyboard_get_cond, NULL, NULL, NULL },
1.63 + {0,0,0,0,0,0,0,0},
1.64 +};
1.65 +
1.66 +#define KEYBOARD(x) ((keyboard_device_t)(x))
1.67 +
1.68 +maple_device_t keyboard_new( )
1.69 +{
1.70 + keyboard_device_t dev = malloc( sizeof(struct keyboard_device) );
1.71 + memcpy( dev, &base_keyboard, sizeof(base_keyboard) );
1.72 + return MAPLE_DEVICE(dev);
1.73 +}
1.74 +
1.75 +maple_device_t keyboard_clone( maple_device_t srcdevice )
1.76 +{
1.77 + keyboard_device_t src = (keyboard_device_t)srcdevice;
1.78 + keyboard_device_t dev = (keyboard_device_t)keyboard_new();
1.79 + memcpy( dev->condition, src->condition, sizeof(src->condition) );
1.80 + return MAPLE_DEVICE(dev);
1.81 +}
1.82 +
1.83 +void keyboard_key_down( keyboard_device_t dev, uint8_t key )
1.84 +{
1.85 + int i;
1.86 + for( i=2; i<8; i++ ) {
1.87 + if( dev->condition[i] == key ) {
1.88 + return; // key already down, missed event or repeat
1.89 + } else if( dev->condition[i] == 0 ) {
1.90 + dev->condition[i] = key;
1.91 + return;
1.92 + }
1.93 + }
1.94 + /* Key array is full - skip for the moment */
1.95 +}
1.96 +
1.97 +void keyboard_key_up( keyboard_device_t dev, uint8_t key )
1.98 +{
1.99 + int i;
1.100 + for( i=2; i<8; i++ ) {
1.101 + if( dev->condition[i] == key ) {
1.102 + for( ; i<7; i++ ) {
1.103 + dev->condition[i] = dev->condition[i+1];
1.104 + }
1.105 + dev->condition[7] = 0;
1.106 + break;
1.107 + }
1.108 + }
1.109 +}
1.110 +
1.111 +void keyboard_input_hook( void *mdev, uint32_t keycode, gboolean isKeyDown )
1.112 +{
1.113 + keyboard_device_t dev = (keyboard_device_t)mdev;
1.114 + uint16_t key = input_keycode_to_dckeysym( keycode );
1.115 + if( key != 0 ) {
1.116 + if( key >> 8 == 0xFF ) { // shift
1.117 + if( isKeyDown ) {
1.118 + dev->condition[0] |= (key&0xFF);
1.119 + } else {
1.120 + dev->condition[0] &= ~(key&0xFF);
1.121 + }
1.122 + } else {
1.123 + if( isKeyDown ) {
1.124 + keyboard_key_down( dev, (uint8_t)key );
1.125 + } else {
1.126 + keyboard_key_up( dev, (uint8_t)key );
1.127 + }
1.128 + }
1.129 + }
1.130 + /*
1.131 + fprintf( stderr, "Key cond: %02X %02X %02X %02X %02X %02X %02X %02X\n",
1.132 + dev->condition[0], dev->condition[1], dev->condition[2],
1.133 + dev->condition[3], dev->condition[4], dev->condition[5],
1.134 + dev->condition[6], dev->condition[7] );
1.135 + */
1.136 +}
1.137 +
1.138 +/**
1.139 + * Device is being attached to the bus. Go through the config and reserve the
1.140 + * keys we need.
1.141 + */
1.142 +void keyboard_attach( maple_device_t mdev )
1.143 +{
1.144 + input_register_hook( keyboard_input_hook, mdev );
1.145 +}
1.146 +
1.147 +void keyboard_detach( maple_device_t mdev )
1.148 +{
1.149 + input_unregister_hook( keyboard_input_hook, mdev );
1.150 +}
1.151 +
1.152 +
1.153 +int keyboard_get_cond( maple_device_t mdev, int function, unsigned char *outbuf,
1.154 + unsigned int *outlen )
1.155 +{
1.156 + keyboard_device_t dev = (keyboard_device_t)mdev;
1.157 + if( function == MAPLE_FUNC_KEYBOARD ) {
1.158 + *outlen = 2;
1.159 + memcpy( outbuf, dev->condition, 8 );
1.160 + return 0;
1.161 + } else {
1.162 + return MAPLE_ERR_FUNC_UNSUP;
1.163 + }
1.164 +}
1.165 +
.