Get_string()

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
t6q4
Member
Member
Posts: 25
Joined: Thu Feb 14, 2008 3:56 pm

Get_string()

Post 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
Last edited by t6q4 on Sat Apr 11, 2009 6:30 am, edited 1 time in total.
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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
User avatar
Jef
Member
Member
Posts: 112
Joined: Tue Jan 08, 2008 7:25 am
Location: Greece
Contact:

Post 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).
Keep coding...
...the sky is the limit

AsteriOS project: http://www.mindfields.gr/main/index.php ... &Itemid=27
User avatar
t6q4
Member
Member
Posts: 25
Joined: Thu Feb 14, 2008 3:56 pm

Post 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.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

imho there are some volatiles missing (if the global variables are modified by a interrupt handler).
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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
User avatar
astrocrep
Member
Member
Posts: 127
Joined: Sat Apr 21, 2007 7:21 pm

Post 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
Mouse Pad - Coming in the distant future...
Kernel: Indigo Kernel - v0.0.1

Thanks to JamesM and BrokenThorn for there tutorials!
Post Reply