Page 1 of 1

Get_string()

Posted: Thu Feb 14, 2008 4:17 pm
by t6q4
Hello there,
I am making my own OS from scratch, as many of you here, and I have programmed my isr's, the gdt and the idt, irq's, etc. and I have come across a hurdle which I cant get past. I have a keyboard handler which is full of stuff to handle for the interface. I need to program a get string function, which basically adds the pressed letter from the keyboard buffer to a string until the user presses enter, when the keyboard handler exits. Then my kernel calls the get function which returns the string from get_string. I tried using flag variables to signal to the handler to add the letter to the get_string buffer, instead of going through the normal procedure or if the user pressed enter, disable the flag. However, I cannot have getsbuf=kblayout

Code: Select all

; because that will truncate the old value of the file. How can I have it so the old value does not get overwritten, but the the new key gets added?

Hope you understood the long post and thanks in advance,
t6q4

Posted: Thu Feb 14, 2008 4:38 pm
by t0xic
Hey,

Try making a global buffer where you can store keys as they are pressed. Something like this:

Code: Select all

char *buffer;	// we want to dynamically alloc this
uint32 idx = 0;	// keep track of where we are
int key_wait = 0;
extern int shell_running;

void keyboard_handler(regs_t *r)
{
	unsigned char scancode;
	unsigned char key;

    scancode = inportb(0x60);
    if (scancode & 0x80)
		;// Shift/Alt/Ctrl keys
    else 
	{
		key = keyboard[scancode];
		putc(key);

		if (key == '\n')	// line return
		{
			if (shell_running) parse_input(buffer);	// parse input for shell
			for(;idx; idx--)
				buffer[idx] = 0;	// clear the input buffer
		} 
		else if (key == '\b')	// backspace
		{
			if (idx)
				buffer[idx--] = 0;
		}
		else
			buffer[idx++] = key;	// add 'key' to the input buffer
	}

	if (key_wait == 1)
		key_wait = 0;
	updatecsr();
}
Note: I don't know if I should post this code for you or not, but I'm in a generous mood :roll:

--Michael

Posted: Thu Feb 14, 2008 9:20 pm
by Jef
The same method (global variable) i am also using for my keyboard driver.
When a key pressed, driver adds the key at the end of the buffer/queue.
When the OS handles the pressed keys, it removes characters from the queue.

Think about that your OS maybe multitasking in the future.
Let the driver only saves the characters (key presses).
Make a kernel part that handles these keys/characters with a way that can be dynamic (passes keys at the active window/process).

Posted: Fri Feb 15, 2008 10:08 am
by t6q4
@t0xic - Thanks, I didnt use your code, I made my own using your principles. Thanks again, for showing me how to append stuff to variables!

@Jef - I may add multitasking in the future, though thats quite far down on my todo list. Thanks for your ideas, I may use your method instead of cramming all my keyboard shortcut stuff into the keyboard handler.

Posted: Fri Feb 15, 2008 11:34 am
by bluecode
imho there are some volatiles missing (if the global variables are modified by a interrupt handler).

Posted: Fri Feb 15, 2008 1:49 pm
by t0xic
bluecode has a point. You should add the volatile's in there, but I've never had any problem.

edit: italics messed up

Posted: Fri Feb 15, 2008 3:32 pm
by astrocrep
I had an issue w/ a keyboard handler like that missing volatiles...

Also, as a good practice, you should keep the code in the ISR to a minimum...

For Example:

Code: Select all

void keyboard_isr()
{
 Get Raw Key Data...
 Add it to the Buffer
 Send a "wake up" message to the keyboard driver
}

The idea behind this is that the keyboard driver will get a notice saying that there are some keys pressed, and the Keyboard Driver, NOT THE ISR, will decode the state flags (ctrl/alt/shift/caps/num/etc etc) and send the key to the waiting app.

Granted I understand that this requires a multi-tasking model. But imho, I think you should have a simple round-robin task scheduling before trying to implement a driver.

Good luck either way!

-Rich