-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.c
More file actions
82 lines (74 loc) · 1.97 KB
/
terminal.c
File metadata and controls
82 lines (74 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <emscripten.h>
#include <stdlib.h>
#include <string.h>
#define TERM_WIDTH 80
#define TERM_HEIGHT 24
#define CHAR_WIDTH 10
#define CHAR_HEIGHT 20
// Terminal state
static char terminal_buffer[TERM_HEIGHT][TERM_WIDTH];
static int cursor_x = 0;
static int cursor_y = 0;
EMSCRIPTEN_KEEPALIVE
void init_terminal() {
// Initialize terminal buffer with spaces
for (int y = 0; y < TERM_HEIGHT; y++) {
for (int x = 0; x < TERM_WIDTH; x++) {
terminal_buffer[y][x] = ' ';
}
}
}
EMSCRIPTEN_KEEPALIVE
void put_char(char c) {
if (c == '\n') {
cursor_x = 0;
cursor_y++;
if (cursor_y >= TERM_HEIGHT) {
// Scroll up
for (int y = 0; y < TERM_HEIGHT - 1; y++) {
memcpy(terminal_buffer[y], terminal_buffer[y + 1], TERM_WIDTH);
}
// Clear last line
memset(terminal_buffer[TERM_HEIGHT - 1], ' ', TERM_WIDTH);
cursor_y = TERM_HEIGHT - 1;
}
} else {
if (cursor_x >= TERM_WIDTH) {
cursor_x = 0;
cursor_y++;
if (cursor_y >= TERM_HEIGHT) {
// Scroll up
for (int y = 0; y < TERM_HEIGHT - 1; y++) {
memcpy(terminal_buffer[y], terminal_buffer[y + 1], TERM_WIDTH);
}
// Clear last line
memset(terminal_buffer[TERM_HEIGHT - 1], ' ', TERM_WIDTH);
cursor_y = TERM_HEIGHT - 1;
}
}
terminal_buffer[cursor_y][cursor_x] = c;
cursor_x++;
}
}
EMSCRIPTEN_KEEPALIVE
char* get_line(int y) {
if (y >= 0 && y < TERM_HEIGHT) {
return terminal_buffer[y];
}
return NULL;
}
EMSCRIPTEN_KEEPALIVE
int get_cursor_x() {
return cursor_x;
}
EMSCRIPTEN_KEEPALIVE
int get_cursor_y() {
return cursor_y;
}
EMSCRIPTEN_KEEPALIVE
void write_text(const char* text) {
int len = strlen(text);
for (int i = 0; i < len; i++) {
put_char(text[i]);
}
}