Page 1 of 1

[Solved] Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 3:06 pm
by psimonson1988
My keyboard handler and my translation table seem to be wrong.. It keeps typing multiple characters and the 'enter/return' key is mapped to '9' on the keyboard not sure how to proceed. I have two questions so here they are. How do you build a complete keyboard translation table? Why does the handler for the keyboard not responding properly (as far as only typing one character at a time etc.)?

Edit: I found Translation Sets
But how do I write one of those sets, I do NOT understand them. The keys and what not aren't in any particular order. They are just listed.

Here's the code: boot32-barebones
Note: The keyboard handler code is in kernel/drivers/keyboard.c. Along with the translation table.
Thanks in advance for all possible answer(s).

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 3:19 pm
by Octocontrabass
You're only handling key-down scan codes. There are also key-up scan codes.

Some scan codes are more than one byte long, but you can only receive one byte at a time. Your IRQ handler will need to keep track of when it receives the first byte of a multi-byte scan code in order to correctly respond to the subsequent byte(s).

It's a good idea to separate the "keyboard" and "keyboard controller" drivers. However, since most keyboards are USB nowadays, I wouldn't worry too much about that until after you have working USB drivers.

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 3:31 pm
by psimonson1988
How do I handle key up and down events?

kernel/drivers/keyboard.c:

Code: Select all

/*
 * keyboard.c - Source file for simple keyboard input.
 *
 * Author: Philip R. Simonson
 * Date  : 07/04/2020
 *
 ******************************************************************
 */

#include "keyboard.h"
#include "kernel.h"
#include "helper.h"
#include "io.h"
#include "ports.h"
#include "isr.h"
#include "util.h"

static char key_buffer[256]; // Save the keys pressed

char kbdus_table[128] = {
	0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
	'-', '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u',
	'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's', 'd', 'f',
	'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z',
	'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
	' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-',
	0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

/* Handle keyboard input from user.
 */
static void keyboard_callback(registers_t *regs)
{
	char scancode = inb(0x60);

	if(scancode == '\b') {
		backspace(key_buffer);
		print_bkspc();
	} else if(scancode == '\n') { // Enter/Return
		user_input(key_buffer);
		key_buffer[0] = '\0';
	} else {
		char letter = kbdus_table[(int)scancode];
		char str[2] = {letter, '\0'};
		append(key_buffer, letter);
		print(str);
	}
	(void)regs;
}
/* Initialize the keyboard.
 */
void install_keyboard(void)
{
	register_interrupt_handler(IRQ1, keyboard_callback);
}

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 4:00 pm
by Octocontrabass

Code: Select all

static uint8_t multibyte = 0;

if( multibyte == 0xe0 )
{
	if( scancode >= 0x80 )
	{
		/* handle two-byte key release scan code */
	}
	else
	{
		/* handle two-byte key press scan code */
	}
	multibyte = 0;
}
else if( multibyte == 0xe1 )
{
	switch( scancode )
	{
		case 0x1d:
		case 0x45:
		case 0x9d:
		case 0xe1:
			/* you may want to verify you receive these in the right order */
			break;
		case 0xc5:
			/* handle pause key here */
			multibyte = 0;
			break;
		default:
			/* shouldn't happen */
			/* perhaps warn the user with an error */
			multibyte = 0;
			break;
	}
}
else
{
	if( scancode == 0xe0 || scancode == 0xe1 )
	{
		multibyte = scancode;
	}
	else if( scancode >= 0x80 )
	{
		/* handle one-byte key release scan code */
	}
	else
	{
		/* handle one-byte key press scan code */
		/* most of the code you've already written could go here */
	}
}
Something like this?

edit: forgot a couple very important lines of code

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 6:48 pm
by psimonson1988
Thanks for the example, but one problem with my new code is it doesn't append to the buffer. Not sure why not though it should. Why isn't it appending?

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 7:15 pm
by Octocontrabass
Well, I forgot a couple very important lines in the example, so perhaps check it again. :oops:

I don't see any reason why it wouldn't be appending. Have you stepped through the function with a debugger to see if control flow is going in an odd direction?

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 8:33 pm
by psimonson1988
Okay checked in the debugger and the user_input function shows char *input as empty. I'm not sure why there isn't anything wrong with my C code. Or at least I don't know, my C code should be passing key_buffer into input in the user_input(char *input) function shouldn't it? Unless there is something I don't know about at this low of level of code in C.

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 8:47 pm
by Octocontrabass
It sounds like key_buffer is actually empty, and what you should be looking at in the debugger are the calls to append() that are supposed to fill it.

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 9:49 pm
by psimonson1988
Yes, the call to append is supposed to fill the key_buffer. The append and backspace functions are located in kernel/common/helper.c.

Re: Keyboard handler and translation table problem.

Posted: Mon Jul 06, 2020 10:27 pm
by Octocontrabass
But did you step through it in the debugger to see if it actually does fill key_buffer?

Your strlen() and strcmp() functions don't look quite right...

Re: Keyboard handler and translation table problem.

Posted: Tue Jul 07, 2020 3:01 am
by psimonson1988
Yep that was it, glad you notice strlen and strcmp not looking right. I corrected them and it works perfect now. Thanks marking as solved.