console.c

来自「从网上下载的一个自己编写的简单的操作系统源代码,对底层了解很有好处的」· C语言 代码 · 共 242 行

C
242
字号
/*
 * ExpOS
 *
 *     
 */

#include <drv/vga.h>

 /* 定义字符颜色和背景颜色 */
 int char_color = LIGHTGREEN;
 int back_color = BLACK;
 int pos;
 int cursor_x, cursor_y;  /* current x and y */
 extern char _graph;

void console_init()
{
	cursor_x = 0;
	cursor_y = 5;
}

/*
 * Procedure:     putchar ID:1
 * Purpose:       在当前位置显示一个字符
 * Input:         ch 要显示的字符
 *               x, y 坐标从默认全局值中更新
 * Output:		暂不支持汉字
 * Errors:        ?使用缓存一个字符以处理汉字双字节问题。
 */
void putchar(char ch)
{
  if(_graph){
    int x, y;
	x = cursor_x;
	y = cursor_y;

	if (ch == '\n') {
		//x = 0;
		if (++y >= ROWS) {
			scroll_up();
			y--;
		}
	} else if (ch == '\r') {
		x=0;
	} else {
		/*??? 使用 SPACING 间距使汉字底线同英文水平线平行 */
		video_ascii(x, y * LINE_HEIGHT + SPACING, ch, char_color);
		if (++x >= COLS) {
			x = 0;
			if (++y >= ROWS) {
				scroll_up();
				y--;
			}
		}
	}
	cursor_x = x;
	cursor_y = y;
	gotoxy(x, y);
  }else{

    	/* the default background color is black , so don't change */
    	/* now work out the memory pos of this x,y */
    	int x, y;
    	unsigned char *p = (unsigned char *)0xB8000;
    	x = cursor_x;
    	y = cursor_y;
    
    	if (ch == '\n') {
    		//x = 0;
    		if (++y >= ROWS) {
    			scroll_up();
    			y--;
    		}
    	}else if (ch == '\r') {
    		x=0;
    	} else{
    		p[(x + COLS * y) * 2] = ch;
    		if (++x >= COLS) {
    			x = 0;
    			if (++y >= ROWS) {
    				scroll_up();
    				y--;
    			}
    		}
    	}
    
    	cursor_x = x;
    	cursor_y = y;
    	gotoxy(x, y);

  	}
}




/*
 * Procedure:     puts ID:1
 * Purpose:       显示字符串
 * Input:         s 要显示的字符串
 *                x, y 坐标全局自动更新
 *                自动滚动屏幕
 * Output:
 * Errors:
 */
void puts(char *s)
{
  if(_graph)
  {
    	unsigned char c, c1, f;
    	int x, y;
    	int color;
    
    	x = cursor_x;
    	y = cursor_y;
    	color = char_color;
    
    	f = c1 = 0;
    	while (*s) {
    		c = *s++;
    		/* `~ 逃逸序列,后跟一个十六进制数 0-A 表示颜色。`~ 两个字符共用一个键通常在 Esc 键下面 */
    		/*??? 支持更多逃逸序列命令,比如是 RED 等表示颜色值? */
    		/*??? `逃逸字符显示,使用双字符 `` 表示单字符 ' ? */
    		if (c == '`') {
    			if (!*s)
    				break;
    			c = *s++;
    			if (!*s) {
    				putchar('`');
    				break;
    			}
    			if (c == '~') {
    				c = *s++;
    				if (c >= '0' && c <= '9')
    					char_color = c - '0';
    				else if (c >= 'a' && c <= 'f')
    					char_color = c - 'a' + 10;
    				else if (c >= 'A' && c <= 'F')
    					char_color = c - 'a' + 10;
    				else {
    					putchar('`');
    					putchar('~');
    					putchar(c);
    				}
    				continue;
    			} else {
    				putchar('`');
    				putchar(c);
    			}
    		}
    		if (c > 0xA0) {
    			if (!f) {
    				c1 = c;
    				f++;
    			} else {
    				f--;
    				/* 一个汉字占用两个字符位置,所以要小心 */
    				if (x >= COLS - 1) {
    					x = 0;
    					if (++y >= ROWS) {
    						scroll_up();
    						y--;
    					}
    				}
    				video_gb2312(x, y * LINE_HEIGHT, c1, c, char_color);
    				x += 2;
    				if (x >= COLS) {
    					x = 0;
    					if (++y >= ROWS) {
    						scroll_up();
    						y--;
    					}
    				}
    			}
    		} else {
    			if (f) {
    				f--;
    				cursor_x = x;
    				cursor_y = y;
    				putchar(c1);
    				x = cursor_x;
    				y = cursor_y;
    			}
    			cursor_x = x;
    			cursor_y = y;
    			putchar(c);
    			x = cursor_x;
    			y = cursor_y;
    		}
    	}
    	if (f) {
    		cursor_x = x;
    		cursor_y = y;
    		putchar(c1);
    		x = cursor_x;
    		y = cursor_y;
    	}
    	cursor_x = x;
    	cursor_y = y;
    	gotoxy(x, y);
    	char_color = color;
    }else{

    	unsigned char c;
    	int x, y;
    	unsigned char *p = (unsigned char *)VRAM_TEXT;
    
    	x = cursor_x;
    	y = cursor_y;
        while ((c = *s++) != '\0') {

			if (c == '\n') {
    			x = 0;
    			if (++y >= ROWS) {
    				/* has gone to bottom of screen */
    				scroll_up();
    				y--;
    			}
    		} else if (c == '\r') {
		           x=0;
		         }else {
    			       p[(x + COLS * y) * 2] = c;
					   p[(x + COLS * y) * 2+1] =(char) char_color;
    	
    			       if (++x >= COLS) {
    				   x = 0;
    				   if (++y >= ROWS) {
    				  	     scroll_up();
    					     y--;
    				   }
    			 }
    		}
    	}
    	cursor_x = x;
    	cursor_y = y;
    	gotoxy(x, y);
  }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?