Skip to content

Commit f3bc90c

Browse files
A safe shutdown function has been added for the VGA, mouse, and keyboard drivers.
1 parent 5d7275f commit f3bc90c

6 files changed

Lines changed: 72 additions & 16 deletions

File tree

Drivers/Keyboard/keyboard.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#define KBD_STAT 0x64
66

77
static bool_t caps_lock = false;
8+
static bool_t kbd_enabled_driver = false;
9+
10+
extern void sleep(uint32_t seconds);
811

912
static const char kmap[128] __attribute__((section(".rodata"))) = {
1013
0, 27, '1','2','3','4','5','6','7','8','9','0','-','=', '\b',
@@ -19,6 +22,7 @@ static uint8_t buf_head = 0;
1922
static uint8_t buf_tail = 0;
2023

2124
static char to_upper(char c) {
25+
if (!kbd_enabled_driver) return;
2226
if (caps_lock && c >= 'a' && c <= 'z') {
2327
return c - 32;
2428
}
@@ -29,13 +33,16 @@ void kbd_init() {
2933
buf_head = 0;
3034
buf_tail = 0;
3135
caps_lock = false;
36+
kbd_enabled_driver = true;
3237
}
3338

3439
uint8_t kb_check() {
40+
if (!kbd_enabled_driver) return;
3541
return buf_head != buf_tail;
3642
}
3743

3844
char get_char() {
45+
if (!kbd_enabled_driver) return;
3946
uint8_t scancode;
4047

4148
while (1) {
@@ -64,7 +71,16 @@ char get_char() {
6471
}
6572

6673
void kbd_hndlr(uint8_t scancode) {
74+
if (!kbd_enabled_driver) return;
6775
if (scancode == 0x3A) {
6876
caps_lock = !caps_lock;
6977
}
78+
}
79+
80+
void kbd_close() {
81+
buf_head = 0;
82+
buf_tail = 0;
83+
sleep(1);
84+
caps_lock = false;
85+
kbd_enabled_driver = false;
7086
}

Drivers/Keyboard/keyboard.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ void kbd_init();
99
char get_char();
1010
uint8_t kb_check();
1111
void kbd_hndlr(uint8_t scancode);
12+
void kbd_close();
1213

1314
#define KBD_DRIVER_NAME "OpenKernel Keyboard Driver"
14-
#define KBD_DRIVER_VER "0.2"
15+
#define KBD_DRIVER_VER "0.3"
1516
#define KBD_DRIVER_DESC "A simple Keyboard driver for OpenKernel"
1617
#define KBD_DRIVER_AUTHOR "OpenSoftware-World"
17-
#define KBD_DRIVER_KRNL_VER "1.0"
18+
#define KBD_DRIVER_KRNL_VER "3.0"
1819

1920
#endif

Drivers/Mouse/mouse.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#define MOUSE_LEFT_LIMIT 0
1111
#define MOUSE_RIGHT_LIMIT (VGA_WIDTH - 1)
1212

13-
uint16_t* vga_buffer = (uint16_t*)0xB8000;
13+
extern uint16_t *vgabuffer = (uint16_t *)0xB8000;
1414
extern uint16_t screen_buffer[VGA_WIDTH * VGA_HEIGHT];
1515

16+
static bool_t mouse_enabled_driver = false;
17+
1618
static uint8_t mouse_cycle = 0;
1719
static int8_t mouse_byte[3];
1820

@@ -48,9 +50,11 @@ void mouse_init() {
4850
mouse_y = 12;
4951
prev_mouse_x = mouse_x;
5052
prev_mouse_y = mouse_y;
53+
mouse_enabled_driver = true;
5154
}
5255

5356
void mouse_poll() {
57+
if (!mouse_enabled_driver) return;
5458
uint8_t status = inb(0x64);
5559

5660
if (!(status & 1)) return;
@@ -99,18 +103,21 @@ void mouse_poll() {
99103
}
100104

101105
void mouse_draw() {
106+
if (!mouse_enabled_driver) return;
102107
uint16_t index = mouse_y * VGA_WIDTH + mouse_x;
103108

104-
vga_buffer[index] = (0x1F << 8) | 0xDB;
109+
vgabuffer[index] = (0x1F << 8) | 0xDB;
105110
}
106111

107112
void mouse_restore() {
113+
if (!mouse_enabled_driver) return;
108114
uint16_t index = prev_mouse_y * VGA_WIDTH + prev_mouse_x;
109115

110-
vga_buffer[index] = screen_buffer[index] ? screen_buffer[index] : (' ' | (0x1F << 8));
116+
vgabuffer[index] = screen_buffer[index] ? screen_buffer[index] : (' ' | (0x1F << 8));
111117
}
112118

113119
void mouse_update() {
120+
if (!mouse_enabled_driver) return;
114121
mouse_poll();
115122

116123
if (mouse_x != prev_mouse_x || mouse_y != prev_mouse_y) {
@@ -123,8 +130,13 @@ void mouse_update() {
123130
}
124131
}
125132

126-
void screen_init() {
127-
for (int i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++) {
128-
screen_buffer[i] = vga_buffer[i];
129-
}
133+
void mouse_close() {
134+
sleep(1);
135+
mouse_cycle = 0;
136+
mouse_x = 0;
137+
mouse_y = 0;
138+
sleep(1);
139+
prev_mouse_x = mouse_x;
140+
prev_mouse_y = mouse_y;
141+
mouse_enabled_driver = false;
130142
}

Drivers/Mouse/mouse.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ extern int mouse_x;
77
extern int mouse_y;
88
extern int prev_mouse_x;
99
extern int prev_mouse_y;
10-
extern uint16_t* vga_buffer;
1110
extern uint16_t prev_cell;
1211

1312
void mouse_init();
@@ -17,12 +16,12 @@ void mouse_clear();
1716
void mouse_put_char(int x, int y, char c, uint8_t color);
1817
void mouse_restore();
1918
void mouse_update();
20-
void screen_init();
19+
void mouse_close();
2120

2221
#define MOUSE_DRIVER_NAME "OpenKernel Mouse Driver"
23-
#define MOUSE_DRIVER_VER "0.1"
22+
#define MOUSE_DRIVER_VER "0.2"
2423
#define MOUSE_DRIVER_DESC "A simple mouse driver for OpenKernel"
2524
#define MOUSE_DRIVER_AUTHOR "OpenSoftware-World"
26-
#define MOUSE_DRIVER_KRNL_VER "2.0"
25+
#define MOUSE_DRIVER_KRNL_VER "3.0"
2726

2827
#endif

Drivers/Vga/vga.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
#define SCREEN_SIZE (VWIDTH * VHEIGHT)
55

6-
static uint16_t *vgabuffer = (uint16_t *)VBUFFER;
6+
uint16_t *vgabuffer = (uint16_t *)VBUFFER;
7+
static bool_t vga_enabled_driver = false;
78
uint16_t screen_buffer[SCREEN_SIZE];
89
static uint8_t txt_color = VGA_COLOR(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_GREY);
910
static uint8_t cx = 0; // cursor x
1011
static uint8_t cy = 0; // cursor y
1112

13+
extern void sleep(uint32_t seconds);
14+
1215
void vga_clear_screen(const char *mode) {
1316
if (mode[0] == 'F') {
1417
for (uint16_t y = 0; y < VHEIGHT; y++) {
@@ -57,6 +60,8 @@ void vga_set_color_scheme(uint8_t bg_color, uint8_t text_color) {
5760
}
5861

5962
void ptchar(char c) {
63+
if (!vga_enabled_driver) return;
64+
6065
if (c == '\n') {
6166
cx = 0;
6267
cy++;
@@ -83,6 +88,8 @@ void ptchar(char c) {
8388
}
8489

8590
void vga_print_scr(const char *str) {
91+
if (!vga_enabled_driver) return;
92+
8693
while (*str) {
8794
ptchar(*str++);
8895
}
@@ -186,6 +193,7 @@ void vga_newline() {
186193

187194
void vga_init() {
188195
vga_clear_screen("F");
196+
vga_enabled_driver = true;
189197
}
190198

191199
void vga_bckspc() {
@@ -204,4 +212,22 @@ void vga_bckspc() {
204212
screen_buffer[cy * VWIDTH + cx] = cell;
205213

206214
vga_set_cursor(cx, cy);
215+
}
216+
217+
void vga_close() {
218+
sleep(1);
219+
vga_clear_screen("F");
220+
cx = 0;
221+
cy = 0;
222+
sleep(1);
223+
vgabuffer = 0;
224+
screen_buffer = 0;
225+
sleep(1);
226+
vga_enabled_driver = false;
227+
}
228+
229+
void screen_init() {
230+
for (int i = 0; i < VWIDTH * VHEIGHT; i++) {
231+
screen_buffer[i] = vgabuffer[i];
232+
}
207233
}

Drivers/Vga/vga.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define VBUFFER 0xB8000
99

1010
void vga_init();
11+
void vga_close();
1112
void vga_clear_screen(const char *mode);
1213
void vga_set_bg_color(uint8_t color);
1314
void vga_set_text_color(uint8_t color);
@@ -22,6 +23,7 @@ void vga_print_hex(uint32_t n);
2223
void vga_print_dec(int num);
2324
void vga_print_bin(unsigned int num);
2425
void vga_cursor_mode(const char *mode, uint8_t start, uint8_t end);
26+
void screen_init();
2527

2628
#define VGA_COLOR_BLACK 0x0
2729
#define VGA_COLOR_BLUE 0x1
@@ -49,9 +51,9 @@ Example: VGA_COLOR_BLUE 0x1 -> 1: Blue background color
4951
#define VGA_ENTRY(ch, color) ((uint16_t)ch | (color << 8))
5052

5153
#define VGA_DRIVER_NAME "OpenKernel VGA Driver"
52-
#define VGA_DRIVER_VER "0.4"
54+
#define VGA_DRIVER_VER "0.5"
5355
#define VGA_DRIVER_DESC "A simple VGA text mode driver for OpenKernel"
5456
#define VGA_DRIVER_AUTHOR "OpenSoftware-World"
55-
#define VGA_DRIVER_KRNL_VER "2.0"
57+
#define VGA_DRIVER_KRNL_VER "3.0"
5658

5759
#endif

0 commit comments

Comments
 (0)