From 3b24d5545035c79bc20a2c95b5b8fcb3c544544a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Janosch=20Gr=C3=A4f?= Date: Sun, 7 Dec 2008 01:37:56 +0100 Subject: [PATCH] console: Added escape sequences for screen --- apps/console/screen.c | 133 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 105 insertions(+), 28 deletions(-) diff --git a/apps/console/screen.c b/apps/console/screen.c index 50dad9d..c3c0f97 100644 --- a/apps/console/screen.c +++ b/apps/console/screen.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "console.h" @@ -32,8 +33,6 @@ #define VIDEOTEXT_STDCOLOR 0x07 // Default color: black background, lightgray foreground #define VIDEOTEXT_STDCOL 0 // Default col #define VIDEOTEXT_STDROW 0 // Default row -#define BELL_STDFREQ 440 // Tone A1 (440Hz) -#define BELL_STDDUR 100 // 100 msec #define SCREEN_DEFAULT_FONT "standard" @@ -42,20 +41,26 @@ // see http://files.osdev.org/mirrors/geezer/osd/graphics/modes.c // for changing fonts -struct { +static struct { int col; int row; +} cursor,saved_cursor; + +static struct { + int linewrap; + int bell_freq; + int bell_dur; int color; -} cursor; +} settings; -uint16_t *videomem; +static uint16_t *videomem; /** * Rings the bell * @param freq Frequency * @param dur Duration time */ -void bell(int freq,int dur) { +static void bell(int freq,int dur) { freq = 1193180/freq; outb(0x43,0xB6); outb(0x42,(uint8_t)freq); @@ -66,42 +71,112 @@ void bell(int freq,int dur) { } /** - * Sets cursor by offset - * @param off Offset - */ -void cursor_setbyoffset(int off) { - cursor.col = off%VIDEOTEXT_WIDTH; - cursor.row = off/VIDEOTEXT_WIDTH; -} - -/** * Clears screen */ -void clearscreen() { +static void clearscreen() { memset(videomem,0,VIDEOTEXT_SIZE); cursor.col = VIDEOTEXT_STDCOL; cursor.row = VIDEOTEXT_STDROW; - cursor.color = VIDEOTEXT_STDCOLOR; +} + +/** + * Sets default settings + */ +static void default_settings() { + settings.linewrap = 1; + settings.bell_freq = 440; + settings.bell_dur = 100; + settings.color = VIDEOTEXT_STDCOLOR; } /** * Prints a character on screen * @param chr Character */ -int printchar(char chr) { - int pos; - if (chr=='\a') bell(BELL_STDFREQ,BELL_STDDUR); - if (chr=='\b') cursor.col = (cursor.col-1>0)?(cursor.col-1):0; - if (chr=='\t') cursor.col = cursor.col+5; - if (chr=='\n') { +static int printchar(char chr) { + size_t pos = cursor_offset(); + static int escape = 0; + static char escape_buf[32]; + + if (escape) { + escape_buf[(escape++)-1] = chr; // put byte in escape buffer + + if (isalpha(chr)) { // escape code finished + escape = 0; + if (escape==1 && escape_buf[0]=='c') default_settings(); // reset device + else if (escape==3 && memcmp(escape_buf,"[7h",3)==0) settings.linewrap = 1; // enable linewrap + else if (escape==3 && memcmp(escape_buf,"[7l",3)==0) settings.linewrap = 0; // disable linewrap + else if (escape_buf[0]=='[' && (escape_buf[escape-2]=='H' || escape_buf[escape-2]=='f')) { // set cursor position + int row = -1; + int col = -1; + if (escape>3) sscanf(escape_buf,escape_buf[escape-2]=='H'?"[%d;%dH":"[%d;%df",&row,&col); + if (row!=-1 && col!=-1) { + cursor.col = col; + cursor.row = row; + } + else { + cursor.col = 0; + cursor.row = 0; + } + } + else if (escape_buf[0]=='[' && escape_buf[escape-2]=='A') { + int up = 1; + if (escape>3) sscanf(escape_buf,"[%dA",&up); + if (cursor.row>0) cursor.row -= up; + } + else if (escape_buf[0]=='[' && escape_buf[escape-2]=='B') { + int down = 1; + if (escape>3) sscanf(escape_buf,"[%dB",&down); + if (cursor.row3) sscanf(escape_buf,"[%dC",&left); + if (cursor.col>0) cursor.col -= left; + } + else if (escape_buf[0]=='[' && escape_buf[escape-2]=='D') { + int right = 1; + if (escape>3) sscanf(escape_buf,"[%dD",&right); + if (cursor.col0)?(cursor.col-1):0; + else if (chr=='\t') cursor.col = cursor.col+5; + else if (chr=='\n') { cursor.row++; cursor.col = VIDEOTEXT_STDCOL; } - if (chr=='\f') clearscreen(); - if (chr=='\r') cursor.col = VIDEOTEXT_STDCOL; - if (chr>=' ') { - pos = cursor_offset(); - *(videomem+pos) = (((uint16_t)cursor.color)<<8)|chr; + else if (chr=='\f') clearscreen(); + else if (chr=='\r') cursor.col = VIDEOTEXT_STDCOL; + else if (chr==0x1B) escape = 1; + else if (chr>=' ') { + *(videomem+pos) = (((uint16_t)settings.color)<<8)|chr; cursor.col++; } @@ -138,6 +213,8 @@ ssize_t onwrite(devfs_dev_t *dev,void *buffer,size_t count,off_t offset) { int init_screen() { videomem = mem_getvga(); if (videomem==NULL) return -1; + memset(&saved_cursor,0,sizeof(saved_cursor)); + default_settings(); clearscreen(); return 0; } -- 2.11.4.GIT