src/keyboard.c

changeset 84
0d903718da81
parent 83
7d2f720daed8
child 85
9883eb142f70
     1.1 --- a/src/keyboard.c	Wed Feb 09 15:43:48 2011 +0000
     1.2 +++ b/src/keyboard.c	Wed Feb 09 16:35:49 2011 +0000
     1.3 @@ -146,8 +146,12 @@
     1.4  
     1.5  	// scan the keymap
     1.6  	int keyidx = 0;
     1.7 +	bool found = false;
     1.8  	for (keyidx=0; keyidx < sizeof(keymap)/sizeof(keymap[0]); keyidx++) {
     1.9 -		if (keymap[keyidx].key == ev->key.keysym.sym) break;
    1.10 +		if (keymap[keyidx].key == ev->key.keysym.sym) {
    1.11 +			found = true;
    1.12 +			break;
    1.13 +		}
    1.14  	}
    1.15  
    1.16  	switch (ev->type) {
    1.17 @@ -157,23 +161,45 @@
    1.18  			break;
    1.19  		// key released
    1.20  		case SDL_KEYUP:
    1.21 -			ks->keystate[keymap[keyidx].scancode] = 1;
    1.22 +			ks->keystate[keymap[keyidx].scancode] = 0;
    1.23  			break;
    1.24  	}
    1.25  }
    1.26  
    1.27  void keyboard_scan(KEYBOARD_STATE *ks)
    1.28  {
    1.29 +	int nkeys = 0;
    1.30 +
    1.31  	// if buffer empty, do a keyboard scan
    1.32  	if (ks->buflen == 0) {
    1.33 -		// TODO: inject "keyboard data follows" chunk header
    1.34 +		// Keyboard Data Begins Here (BEGKBD)
    1.35 +		ks->buffer[ks->writep] = 0xDF;
    1.36 +		ks->writep = (ks->writep + 1) % KEYBOARD_BUFFER_SIZE;
    1.37 +		if (ks->buflen < KEYBOARD_BUFFER_SIZE) ks->buflen++;
    1.38 +
    1.39  		for (int i=0; i<(sizeof(ks->keystate)/sizeof(ks->keystate[0])); i++) {
    1.40  			if (ks->keystate[i]) {
    1.41 +				printf("\tKBC KEY DOWN: %d\n", i);
    1.42  				ks->buffer[ks->writep] = i;
    1.43  				ks->writep = (ks->writep + 1) % KEYBOARD_BUFFER_SIZE;
    1.44 +				if (ks->buflen < KEYBOARD_BUFFER_SIZE) ks->buflen++;
    1.45 +				nkeys++;
    1.46  			}
    1.47  		}
    1.48 +
    1.49 +		// If no keys down, then send All Keys Up byte
    1.50 +		if (nkeys == 0) {
    1.51 +			ks->buffer[ks->writep] = 0x40;
    1.52 +			ks->writep = (ks->writep + 1) % KEYBOARD_BUFFER_SIZE;
    1.53 +			if (ks->buflen < KEYBOARD_BUFFER_SIZE) ks->buflen++;
    1.54 +		}
    1.55 +
    1.56  		// TODO: inject "mouse data follows" chunk header and mouse movement info
    1.57 +
    1.58 +		// Last Entry In List
    1.59 +		ks->buffer[ks->writep] = 0x80;
    1.60 +		ks->writep = (ks->writep + 1) % KEYBOARD_BUFFER_SIZE;
    1.61 +		if (ks->buflen < KEYBOARD_BUFFER_SIZE) ks->buflen++;
    1.62  	}
    1.63  }
    1.64  
    1.65 @@ -200,19 +226,22 @@
    1.66  {
    1.67  	if ((addr & 1) == 0) {
    1.68  		// Status register -- RS=0, read
    1.69 -		return
    1.70 -			(ks->buflen > 0) ? 1 : 0 +		// SR0: a new character has been received
    1.71 -			0 +								// SR1: Transmitter Data Register Empty
    1.72 -			0 +								// SR2: Data Carrier Detect
    1.73 -			0 +								// SR3: Clear To Send
    1.74 -			0 +								// SR4: Framing Error
    1.75 -			0 +								// SR5: Receiver Overrun
    1.76 -			0 +								// SR6: Parity Error
    1.77 -			(keyboard_get_irq(ks)) ? 0x80 : 0;	// SR7: IRQ status
    1.78 +		uint8_t sr = 0;
    1.79 +		if (ks->buflen > 0) sr |= 1;			// SR0: a new character has been received
    1.80 +		sr |= 2;								// SR1: Transmitter Data Register Empty
    1.81 +//			0 +								// SR2: Data Carrier Detect
    1.82 +//			0 +								// SR3: Clear To Send
    1.83 +//			0 +								// SR4: Framing Error
    1.84 +//			0 +								// SR5: Receiver Overrun
    1.85 +//			0 +								// SR6: Parity Error
    1.86 +		if (keyboard_get_irq(ks)) sr |= 0x80;	// SR7: IRQ status
    1.87 +		printf("\tKBD DBG: sr=%02X\n", sr);
    1.88 +		return sr;
    1.89  	} else {
    1.90  		// return data, pop off the fifo
    1.91  		uint8_t x = ks->buffer[ks->readp];
    1.92  		ks->readp = (ks->readp + 1) % KEYBOARD_BUFFER_SIZE;
    1.93 +		if (ks->buflen > 0) ks->buflen--;
    1.94  		return x;
    1.95  	}
    1.96  }
    1.97 @@ -238,6 +267,7 @@
    1.98  		ks->rxie = (val & 0x80)==0x80;
    1.99  	} else {
   1.100  		// Write command to KBC -- TODO!
   1.101 +		printf("KBC TODO: write keyboard data 0x%02X\n", val);
   1.102  	}
   1.103  }
   1.104