oxytoxin

unix-like kernel
git clone git://git.evenfri.xyz/oxytoxin.git
Log | Files | Refs | README | LICENSE

tty.c (2080B)


      1 #include "oxytoxin.h"
      2 
      3 size_t tty_row;
      4 size_t tty_column;
      5 uint8_t tty_color;
      6 uint16_t* tty_buffer = (uint16_t*)vga_start;
      7 
      8 void tty_set_color(uint8_t color) {
      9   tty_color = color;
     10 }
     11 
     12 void tty_putentryat(char c, uint8_t color, size_t x, size_t y) {
     13   size_t index;
     14 
     15   index = y * vga_width + x;
     16   tty_buffer[index] = vga_entry(c, color);
     17 }
     18 
     19 void tty_clear(void) {
     20   size_t x;
     21   size_t y;
     22   size_t index;
     23 
     24   tty_row = 0;
     25   tty_column = 0;
     26 
     27   for (y = 0; y < vga_height; y++) {
     28     for (x = 0; x < vga_width; x++) {
     29       index = y * vga_width + x;
     30       tty_buffer[index] = vga_entry(' ', tty_color);
     31     }
     32   }
     33   vga_cursor_update(tty_column, tty_row);
     34 }
     35 
     36 void tty_init(void) {
     37   vga_cursor_enable(14, 15);
     38   tty_color = vga_entry_color(vga_color_light_grey, vga_color_black);
     39   tty_clear();
     40 }
     41 
     42 void tty_scroll(void) {
     43   int row;
     44   int col;
     45   size_t src_index;
     46   size_t dst_index;
     47   uint16_t blank;
     48   size_t index;
     49 
     50   /* move lines to line-1 */
     51   for (row = 1; row < vga_height; row++) {
     52     for (col = 0; col < vga_width; col++) {
     53       src_index = row * vga_width + col;
     54       dst_index = (row - 1) * vga_width + col;
     55       tty_buffer[dst_index] = tty_buffer[src_index];
     56     }
     57   }
     58   /* clear last line*/
     59   blank = ' ' | (tty_color << 8);
     60   for (col = 0; col < vga_width; col++) {
     61     index = (vga_height - 1) * vga_width + col;
     62     tty_buffer[index] = blank;
     63   }
     64 }
     65 
     66 void tty_putchar(char c) {
     67   if(c == '\n') {
     68     tty_row++;
     69     tty_column = 0;
     70 
     71     if (tty_row >= vga_height) {
     72       tty_scroll();
     73       tty_row = vga_height - 1;
     74     }
     75   } else if(c == '\b') {
     76     if (tty_column > 0) {
     77       tty_column--;
     78       tty_putentryat(' ', tty_color, tty_column, tty_row);
     79     }
     80   } else {
     81     tty_putentryat(c, tty_color, tty_column, tty_row);
     82     if (++tty_column == vga_width) {
     83       tty_column = 0;
     84       if (++tty_row == vga_height) {
     85         tty_scroll();
     86         tty_row = vga_height - 1;
     87       }
     88     }
     89   }
     90 
     91   vga_cursor_update(tty_column, tty_row);
     92 }
     93 
     94 void tty_write(const char* data, size_t size) {
     95   size_t i;
     96 
     97   for (i = 0; i < size; i++) {
     98     tty_putchar(data[i]);
     99   }
    100 }