keyboard - How to make a scanf() type function in a 32bit os in c? -
i trying make os in c , assembly in 32-bits protected mode. trying create scanf() type function gets keyboard input until enter button pressed. have basic keyboard irq handler setup prints typed , want implement scanf() function have problems return value keyboard handler main kernel.
heres code 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 pressed //monitor_write("shift + "); } else if(scancode == 0xaa) { shift_key= 0;//shift key not pressed // monitor_write("unshift"); } else if(scancode == 0x3a && caps_key == 1) { caps_key = 0;//caps key pressed // monitor_write("shift + "); } else if(scancode == 0x3a && caps_key == 0) { caps_key = 1;//caps key 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 see special keys 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 see special keys 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 character 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 using scan variable bool put chars in array when irq called. cant way return main file call it.
i thought of , tried piece of code worked. did made function in keyboard driver keep last char typed. , when want char call getch();
return char. , made main scanf();
in main file collect chars , first set them 0 same char not repeated , goes through while loop verify enter key pressed. , add char char*
if not = 0 , if buffch
variable set 1 if set 0 not print it.that theory.
here code:
in keyboard driver: added getch , getchter wich char , set 0 respectively.
char getch() { char buf = sbuf; // clone buffer getchter(); // set sbuf 0 same char not repeated return buf; // return buff. } void getchter() { sbuf = 0; // setting sbuf 0 }
and here scanf() function goes in main kernel file.
char* scanf() //scanf() called { char* buff; //make buffer char key = '/0'; // make single char buffer int = 0; //counter int n = 100; // counter number limit of chars input int buffch = 1; while(i < n) //until max char limit reached { buffch = 1; // buffch used bool buffer or not buffer char key = getch(); // last char typed if(key == '\n') // if enter pressed stop { break; } if(key == '\b') if key backspace { //! dont buffer char buffch = 0; if (i > 0) // if count more 0 { //! go 1 char in buffer i--; } } //! add char if buffered if (buffch == 1) { if (key != 0) // insure not 0 ie if repeated = 0 { buff[i++] = key; // add char buffer } } } buff[i] = '/0'; in last set string null terminated return buff; // return buff }
i hope helps other people :)
Comments
Post a Comment