view cos/kernel/keyboard.c @ 330:a79ac866732f

Added kernel recipe
author Windel Bouwman
date Fri, 07 Feb 2014 12:08:40 +0100
parents 8012221dd740
children
line wrap: on
line source

#include "kernel.h"

static int shiftstate = 0;
static volatile uint8_t charAvail = 0;
static volatile char kbdchar = ' ';

// TODO: move to user land:
static char keymap[128] = 
{
  '?','?','1','2', '3', '4', '5','6', '7', '8','9', '0', '-','=', 0xe, '?',
  'q','w','e','r', 't', 'y', 'u','i', 'o', 'p','[', ']', '\n','?', 'a', 's',
  'd','f','g','h', 'j', 'k', 'l',';', '\'', '?','?', '?', 'z','x', 'c', 'v',
  'b','n','m',',', '.', '/', '?','?', '?', ' ','?', '?', '?','?', '?', '?',

  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?'
};

static char keymapUPPER[128] = 
{
  '?','?','!','@', '#', '$', '%','^', '&', '*','(', ')', '_','+', '?', '?',
  'Q','W','E','R', 'T', 'Y', 'U','I', 'O', 'P','{', '}', '|','?', 'A', 'S',
  'D','F','G','H', 'J', 'K', 'L',':', '"', '?','?', '?', 'Z','X', 'C', 'V',
  'B','N','M','<', '>', '?', '?','?', '?', ' ','?', '?', '?','?', '?', '?',

  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?',
  '?','?','?','?', '?', '?', '?','?', '?', '?','?', '?', '?','?', '?', '?'
};

void keyboard_init()
{
   shiftstate = 0;
   charAvail = 0;
}

void keyboardDriverUpdate()
{
  unsigned char scancode = inb(0x60);

  switch(scancode) {
    case 0x2a:
      shiftstate = 1;
      break;
    case 0xaa:
      shiftstate = 0;
      break;
    default:
      if (scancode < 128) {
        if (charAvail == 0) {
          if (shiftstate == 0) {
            kbdchar = keymap[scancode];
          } else {
            kbdchar = keymapUPPER[scancode];
          }
          
          charAvail = 1;
        }
      } else {
        // Key release
        //printf("Unhandled scancode: 0x%x\n", scancode);
      }
      break;
  }
}

char getChar() 
{
  while (charAvail == 0);
  char c = kbdchar;
  charAvail = 0;
  return c;
}

// Move to user code:
void getline(char *buffer, int len) 
{
  char c;
  int i = 0;
  while (i < len-1) {
    c = getChar();
    //printf("%x", c);
    if (c == '\n') {
      // Enter
      break;
    }
    if (c == 0x0e) 
    {
       // Backspace
       if (i>0) 
       {
          int r, c;
          get_cursor(&r, &c);
          set_cursor(r, c - 1);
          printf(" ");
          set_cursor(r, c - 1);
          i--;
       }
       continue;
    }
    buffer[i] = c;
    printf("%c", c);
    i++;
  }
  buffer[i] = 0;
}