HariboteOS/day18/bootpack.c
2021-04-19 01:42:02 +08:00

463 lines
14 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <string.h>
#include "bootpack.h"
#include "console.h"
#include "desctbl.h"
#include "fifo.h"
#include "fs.h"
#include "graphic.h"
#include "int.h"
#include "io.h"
#include "keyboard.h"
#include "memory.h"
#include "mouse.h"
#include "sheet.h"
#include "task.h"
#include "timer.h"
#include "window.h"
void console_task(struct Sheet *sheet, unsigned int memtotal) {
struct Task *task = task_now();
int fifobuf[128], cursor_x = 16, cursor_y = 28, cursor_c = -1;
char s[30], cmdline[30];
struct MemMan *memman = (struct MemMan *)MEMMAN_ADDR;
struct FileInfo *finfo = (struct FileInfo *) (ADR_DISKIMG + 0x002600);
fifo32_init(&task->fifo, 128, fifobuf, task);
struct Timer *timer = timer_alloc();
timer_init(timer, &task->fifo, 1);
timer_set_timer(timer, 50);
put_fonts8_asc_sht(sheet, 8, 28, COL8_FFFFFF, COL8_000000, ">", 1);
for (;;) {
io_cli();
if (!fifo32_status(&task->fifo)) {
task_sleep(task);
io_sti();
} else {
int data = fifo32_get(&task->fifo);
io_sti();
if (data <= 1) {
if (data) {
timer_init(timer, &task->fifo, 0);
if (cursor_c >= 0) {
cursor_c = COL8_FFFFFF;
}
} else {
timer_init(timer, &task->fifo, 1);
if (cursor_c >= 0) {
cursor_c = COL8_000000;
}
}
timer_set_timer(timer, 50);
}
// 光标ON
if (data == 2) {
cursor_c = COL8_FFFFFF;
}
// 光标OFF
if (data == 3) {
box_fill8(sheet->buf, sheet->bxsize, COL8_000000, cursor_x, 28,
cursor_x + 7, 43);
cursor_c = -1;
}
if (data >= 256 && data <= 511) {
if (data == 8 + 256) {
// 退格键
if (cursor_x > 16) {
// 用空白擦除光标后将光标前移一位
put_fonts8_asc_sht(sheet, cursor_x, cursor_y, COL8_FFFFFF,
COL8_000000, " ", 1);
cursor_x -= 8;
}
} else if (data == 10 + 256) {
// 回车键
// 用空格将光标擦除
put_fonts8_asc_sht(sheet, cursor_x, cursor_y, COL8_FFFFFF,
COL8_000000, " ", 1);
cmdline[cursor_x / 8 - 2] = '\0';
cursor_y = cons_newline(cursor_y, sheet);
if (!strcmp(cmdline, "mem")) {
// mem命令
sprintf(s, "total %dMB", memtotal / (1024 * 1024));
put_fonts8_asc_sht(sheet, 8, cursor_y, COL8_FFFFFF, COL8_000000, s,
30);
cursor_y = cons_newline(cursor_y, sheet);
sprintf(s, "free %dKB", memman_total(memman) / 1024);
put_fonts8_asc_sht(sheet, 8, cursor_y, COL8_FFFFFF, COL8_000000, s,
30);
cursor_y = cons_newline(cursor_y, sheet);
cursor_y = cons_newline(cursor_y, sheet);
} else if (!strcmp(cmdline, "cls")) {
// cls命令
for (int y = 28; y < 28 + 128; y++) {
for (int x = 8; x < 8 + 240; x++) {
sheet->buf[x + y * sheet->bxsize] = COL8_000000;
}
}
sheet_refresh(sheet, 8, 28, 8 + 240, 28 + 128);
cursor_y = 28;
} else if (!strcmp(cmdline, "dir")) {
// dir命令
for (int x = 0; x < 224; x++) {
if (finfo[x].name[0] == '\0') {
break;
}
if (finfo[x].name[0] != 0xe5) {
if (!(finfo[x].type & 0x18)) {
sprintf(s, "filename.ext %7d", finfo[x].size);
for (int y = 0; y < 8; y++) {
s[y] = finfo[x].name[y];
}
s[9] = finfo[x].ext[0];
s[10] = finfo[x].ext[1];
s[11] = finfo[x].ext[2];
put_fonts8_asc_sht(sheet, 8, cursor_y, COL8_FFFFFF, COL8_000000, s, 30);
cursor_y = cons_newline(cursor_y, sheet);
}
}
}
cursor_y = cons_newline(cursor_y, sheet);
} else if (strcmp(cmdline, "")) {
put_fonts8_asc_sht(sheet, 8, cursor_y, COL8_FFFFFF, COL8_000000,
"Bad command.", 12);
cursor_y = cons_newline(cursor_y, sheet);
cursor_y = cons_newline(cursor_y, sheet);
}
// 显示提示符
put_fonts8_asc_sht(sheet, 8, cursor_y, COL8_FFFFFF, COL8_000000, ">",
1);
cursor_x = 16;
} else {
if (cursor_x < 240) {
s[0] = data - 256;
s[1] = '\0';
cmdline[cursor_x / 8 - 2] = data - 256;
put_fonts8_asc_sht(sheet, cursor_x, cursor_y, COL8_FFFFFF,
COL8_000000, s, 1);
cursor_x += 8;
}
}
}
if (cursor_c >= 0) {
box_fill8(sheet->buf, sheet->bxsize, cursor_c, cursor_x, cursor_y,
cursor_x + 7, cursor_y + 15);
}
sheet_refresh(sheet, cursor_x, cursor_y, cursor_x + 8, cursor_y + 16);
}
}
}
int main(void) {
struct BootInfo *binfo = (struct BootInfo *)ADR_BOOTINFO;
struct MemMan *memman = (struct MemMan *)MEMMAN_ADDR;
struct MouseDec mdec;
char s[40], mcursor[256];
unsigned int memtotal;
struct Shtctl *shtctl;
struct Sheet *sht_back, *sht_mouse, *sht_win, *sht_cons;
unsigned char *buf_back, buf_mouse[256], *buf_win, *buf_cons;
struct Timer *timer;
struct FIFO32 fifo, keycmd;
int fifobuf[128], keycmd_buf[32];
int data, key_to = 0, key_shift = 0, key_leds = (binfo->leds >> 4) & 7,
keycmd_wait = -1;
init_gdtidt();
init_pic(); // GDT/IDT完成初始化开放CPU中断
io_sti();
fifo32_init(&fifo, 128, fifobuf, NULL);
fifo32_init(&keycmd, 32, keycmd_buf, NULL);
init_keyboard(&fifo, 256);
enable_mouse(&fifo, 512, &mdec);
init_pit();
io_out8(PIC0_IMR, 0xf8); // 开放PIT、PIC1以及键盘中断
io_out8(PIC1_IMR, 0xef); // 开放鼠标中断
memtotal = memtest(0x00400000, 0xbfffffff);
memman_init(memman);
// 书中为0x00001000 ~ 0x0009e000
// 注1: 测试时发现会造成错误原因未知所以改为由0x00010000开始
// 注2: 在640*480模式下free该段内存后会导致屏幕画面错误
// memman_free(memman, 0x00010000, 0x0009e000); // 0x00010000 ~ 0x0009efff
memman_free(memman, 0x00400000, memtotal - 0x00400000);
init_palette();
shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny);
struct Task *task_a = task_init(memman);
fifo.task = task_a;
task_run(task_a, 1, 0);
// sht_back
sht_back = sheet_alloc(shtctl);
buf_back =
(unsigned char *)memman_alloc_4k(memman, binfo->scrnx * binfo->scrny);
sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny,
-1); // 没有透明色
init_screen8(buf_back, binfo->scrnx, binfo->scrny);
// sht_cons
sht_cons = sheet_alloc(shtctl);
buf_cons = (unsigned char *)memman_alloc_4k(memman, 256 * 165);
sheet_setbuf(sht_cons, buf_cons, 256, 165, -1); // 无透明色
make_window8(buf_cons, 256, 165, "console", 0);
make_textbox8(sht_cons, 8, 28, 240, 128, COL8_000000);
struct Task *task_cons = task_alloc();
task_cons->tss.esp = memman_alloc_4k(memman, 64 * 1024) + 64 * 1024 - 12;
task_cons->tss.eip = (int)&console_task;
task_cons->tss.es = 1 * 8;
task_cons->tss.cs = 2 * 8;
task_cons->tss.ss = 1 * 8;
task_cons->tss.ds = 1 * 8;
task_cons->tss.fs = 1 * 8;
task_cons->tss.gs = 1 * 8;
*((int *)(task_cons->tss.esp + 4)) = (int)sht_cons;
*((int *)(task_cons->tss.esp + 8)) = memtotal;
task_run(task_cons, 2, 2);
// sht_win
sht_win = sheet_alloc(shtctl);
buf_win = (unsigned char *)memman_alloc_4k(memman, 160 * 52);
sheet_setbuf(sht_win, buf_win, 160, 52, -1); // 没有透明色
make_window8(buf_win, 160, 52, "task_a", 1);
make_textbox8(sht_win, 8, 28, 144, 16, COL8_FFFFFF);
int cursor_x = 8;
int cursor_c = COL8_FFFFFF;
timer = timer_alloc();
timer_init(timer, &fifo, 1);
timer_set_timer(timer, 50);
// sht_mouse
sht_mouse = sheet_alloc(shtctl);
sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99); // 透明色号99
init_mouse_cursor8(buf_mouse, 99); // 背景色号99
int mx = (binfo->scrnx - 16) / 2; // 按在画面中央来计算坐标
int my = (binfo->scrny - 28 - 16) / 2;
sheet_slide(sht_back, 0, 0);
sheet_slide(sht_cons, 32, 4);
sheet_slide(sht_win, 64, 56);
sheet_slide(sht_mouse, mx, my);
sheet_updown(sht_back, 0);
sheet_updown(sht_cons, 1);
sheet_updown(sht_win, 2);
sheet_updown(sht_mouse, 3);
// 避免与键盘状态冲突
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
for (;;) {
if (fifo32_status(&keycmd) > 0 && keycmd_wait < 0) {
// 如果存在向键盘控制器发送的数据
keycmd_wait = fifo32_get(&keycmd);
wait_KBC_sendready();
io_out8(PORT_KEYCMD, keycmd_wait);
}
io_cli();
if (fifo32_status(&fifo) == 0) {
task_sleep(task_a);
io_sti();
} else {
data = fifo32_get(&fifo);
io_sti();
if (256 <= data && data <= 511) {
// 键盘数据
if (data < 0x80 + 256) {
if (key_shift == 0) {
s[0] = keytable0[data - 256];
} else {
s[0] = keytable1[data - 256];
}
} else {
s[0] = 0;
}
if ('A' <= s[0] && s[0] <= 'Z') {
if ((!(key_leds & 4) && !key_shift) ||
((key_leds & 4) && key_shift)) {
s[0] += 0x20;
}
}
if (s[0]) {
if (!key_to) {
if (cursor_x < 128) {
// 显示一个字符之后将光标后移一位
s[1] = '\0';
put_fonts8_asc_sht(sht_win, cursor_x, 28, COL8_000000,
COL8_FFFFFF, s, 1);
cursor_x += 8;
}
} else {
fifo32_put(&task_cons->fifo, s[0] + 256);
}
}
if (data == 256 + 0x0e) {
// 退格键
if (!key_to) {
if (cursor_x > 8) {
// 用空格键把光标消去后,后移一次光标
put_fonts8_asc_sht(sht_win, cursor_x, 28, COL8_000000,
COL8_FFFFFF, " ", 1);
cursor_x -= 8;
}
} else {
fifo32_put(&task_cons->fifo, 8 + 256);
}
}
// Tab键
if (data == 256 + 0x0f) {
if (!key_to) {
key_to = 1;
make_window_title8(buf_win, sht_win->bxsize, "task_a", 0);
make_window_title8(buf_cons, sht_cons->bxsize, "console", 1);
cursor_c = -1; // 不显示光标
box_fill8(sht_win->buf, sht_win->bxsize, COL8_FFFFFF, cursor_x, 28,
cursor_x + 7, 43);
fifo32_put(&task_cons->fifo, 2); // 命令行窗口光标ON
} else {
key_to = 0;
make_window_title8(buf_win, sht_win->bxsize, "task_a", 1);
make_window_title8(buf_cons, sht_cons->bxsize, "console", 0);
cursor_c = COL8_000000; // 显示光标
fifo32_put(&task_cons->fifo, 3); // 命令行窗口光标OFF
}
sheet_refresh(sht_win, 0, 0, sht_win->bxsize, 21);
sheet_refresh(sht_cons, 0, 0, sht_cons->bxsize, 21);
}
// 回车键
if (data == 256 + 0x1c) {
if (key_to) {
fifo32_put(&task_cons->fifo, 10 + 256);
}
}
// 左Shift按下
if (data == 256 + 0x2a) {
key_shift |= 1;
}
// 右Shift按下
if (data == 256 + 0x36) {
key_shift |= 2;
}
// 左Shift释放
if (data == 256 + 0xaa) {
key_shift &= ~1;
}
// 右Shift释放
if (data == 256 + 0xb6) {
key_shift &= ~2;
}
// CapsLock
if (data == 256 + 0x3a) {
key_leds ^= 4;
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
}
// NumLock
if (data == 256 + 0x45) {
key_leds ^= 2;
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
}
// ScrollLock
if (data == 256 + 0x46) {
key_leds ^= 1;
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
}
// 键盘成功接收到数据
if (data == 256 + 0xfa) {
keycmd_wait = -1;
}
if (data == 256 + 0xfe) {
wait_KBC_sendready();
io_out8(PORT_KEYDAT, keycmd_wait);
}
if (cursor_c >= 0) {
box_fill8(sht_win->buf, sht_win->bxsize, cursor_c, cursor_x, 28,
cursor_x + 7, 43);
}
sheet_refresh(sht_win, cursor_x, 28, cursor_x + 8, 44);
} else if (512 <= data && data <= 767) {
// 鼠标数据
if (mouse_decode(&mdec, data - 512)) {
// 移动光标
mx += mdec.x;
my += mdec.y;
if (mx < 0) {
mx = 0;
}
if (my < 0) {
my = 0;
}
if (mx > binfo->scrnx - 1) {
mx = binfo->scrnx - 1;
}
if (my > binfo->scrny - 1) {
my = binfo->scrny - 1;
}
sheet_slide(sht_mouse, mx, my);
if (mdec.btn & 0x01) {
// 按下左键移动sht_win
sheet_slide(sht_win, mx - 80, my - 8);
}
}
} else if (data <= 1) {
if (data) {
timer_init(timer, &fifo, 0);
if (cursor_c >= 0) {
cursor_c = COL8_000000;
}
} else {
timer_init(timer, &fifo, 1);
if (cursor_c >= 0) {
cursor_c = COL8_FFFFFF;
}
}
timer_set_timer(timer, 50);
if (cursor_c >= 0) {
box_fill8(sht_win->buf, sht_win->bxsize, cursor_c, cursor_x, 28,
cursor_x + 7, 43);
sheet_refresh(sht_win, cursor_x, 28, cursor_x + 8, 44);
}
}
}
}
return 0;
}