I am trying to make an os in c and assembly in 32-bits protected mode. I am trying to create a scanf() type function which gets the keyboard input until the enter button is pressed. I have a basic keyboard IRQ handler setup which prints anything typed and i want to implement a scanf() function but i have problems to return the value from the keyboard Handler to the main kernel.
Heres the code for the keyboard handler.
unsigned int shift_key = 0;
unsigned int caps_key = 0;
int counter = 0;
char *sbuf;
int scan = 1;
void keyboard_handler(registers_t regs)
{
// monitor_write("handler called");
unsigned char scancode;
scancode = get_scancode();
if(scancode == 0x2A )
{
shift_key = 1;//Shift key is pressed
//monitor_write("Shift + ");
}
else if(scancode == 0xAA)
{
shift_key= 0;//Shift Key is not pressed
// monitor_write("Unshift");
}
else if(scancode == 0x3A && caps_key == 1)
{
caps_key = 0;//Caps key is pressed
// monitor_write("Shift + ");
}
else if(scancode == 0x3A && caps_key == 0)
{
caps_key = 1;//Caps key is pressed
// monitor_write("Shift + ");
}
//Left arrow
else if(scancode == 0x4B)
{
move_cursor_LR(1,0);
}
//right Arrow
else if(scancode == 0x4D)
{
move_cursor_LR(0,1);
}
else
{
if (shift_key == 1 && caps_key == 0)
{
// int shiftaltctrl = 1;//Put anything to see what special keys were pressed
monitor_put(Kkbdus[scancode]);
if (scan == 1 && Kkbdus[scancode] != '\n')
{
sbuf[counter] = Kkbdus[scancode];
counter++ ;
}
}
if (caps_key == 1 && shift_key == 0)
{
// int shiftaltctrl = 1;//Put anything to see what special keys were pressed
monitor_put(Kkbdus[scancode]);
if (scan == 1 && Kkbdus[scancode] != '\n')
{
sbuf[counter] = Kkbdus[scancode] ;
counter++ ;
}
}
if(shift_key == 0 && caps_key == 0)
{
monitor_put(kbdus[scancode]); //Prints the character which was pressed
if (scan == 1 && kbdus[scancode] != '\n')
{
sbuf[counter] = kbdus[scancode];
counter++ ;
}
}
if( caps_key == 1 && shift_key == 1)
{
monitor_put(kbdus[scancode]);
if (scan == 1 && kbdus[scancode] != '\n')
{
sbuf[counter] = kbdus[scancode];
counter++ ;
}
}
if(scan == 1 && kbdus[scancode] == '\n')
{
scan = 0;
sbuf[0] = '\0';
}
if(kbdus[scancode] == '\t')
{
monitor_write(sbuf);
}
}
}
I am using the scan variable as a bool to put the chars in an array when the irq is called. but i cant get a way to return it to the main file from which i call it.
I thought of this and tried a piece of code which worked. What i did was made a function in the keyboard driver which would keep the last char typed. and when i want to get the char i would just call
getch();
which would return the char. and i made the mainscanf();
in the main file which would collect the chars and first set them to 0 so that the same char does not get repeated and then it goes through a while loop which would verify for the enter key to be pressed. and would add the char to achar*
if it is not = 0 and if thebuffch
variable is set to 1 if it is set to 0 it will not print it.That was the theory.Here is the code:
in keyboard driver: added getch and getchter wich would get the char and set it to 0 respectively.
and here is the scanf() function which goes in the main kernel file.
I hope this helps other people :)