-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcurses.c
More file actions
221 lines (184 loc) · 3.19 KB
/
webcurses.c
File metadata and controls
221 lines (184 loc) · 3.19 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <emscripten/emscripten.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "webcurses.h"
typedef struct {
short pair;
short f;
short b;
void* next;
} _ColorPair;
/*******************
* LOCAL GLOBAL VARIABLES
*******************/
_ColorPair* _color_pairs = NULL;
int _timeout = -1;
/**********
* FUNCTIONS
***********/
void _init_em_params() {
EM_ASM(
_stack_arg = [];
);
}
void _add_int_em_param(int arg) {
EM_ASM({
_stack_arg.push($0);
}, arg);
}
void _add_char_em_param(char arg) {
EM_ASM({
_stack_arg.push($0);
}, arg);
}
_ColorPair* _get_color_pair(short pair) {
_ColorPair* p;
for (p=_color_pairs; p != NULL; p = (_ColorPair*)p->next) {
if (p->pair == pair) {
return p;
}
}
return NULL;
}
/*******************
* PARTIALLY IMPLEMENTED
*******************/
void initscr() {
EM_ASM(
window.startTerminal();
);
}
/*******************
* PARTIALLY IMPLEMENTED
*******************/
void noecho() {
EM_ASM(
window.term.clear();
window.term.lock = true;
window.term.insert = false;
);
}
/*******************
* PARTIALLY IMPLEMENTED
*******************/
void curs_set(int vis) {
switch (vis) {
case 0:
EM_ASM(
window.term.cursorOff();
);
break;
case 2:
case 1:
EM_ASM(
window.term.cursorOn();
);
break;
}
}
/************
* IMPLEMENTED
*************/
void endwin() {
EM_ASM(
window.term.close();
);
}
/************
* PARTIALLY IMPLEMENTED
*************/
EM_ASYNC_JS(int, _get_ch, (), {
return (await waitForKeyPress(_stack_arg[0])).charCode;
});
int getch() {
_init_em_params();
_add_int_em_param(_timeout);
return _get_ch();
}
/************
* IMPLEMENTED
*************/
int mvaddch(int y, int x, int ch) {
int pair = PAIR_NUMBER(ch);
if (pair > 0) {
short f, b;
f = 0;
b = 0;
pair_content(PAIR_NUMBER(ch), &f, &b);
EM_ASM({
window.term.setChar($2, $0, $1, ncursesColorToStyle($3));
}, y, x, (char)ch, f, b);
} else {
EM_ASM({
window.term.setChar($2, $0, $1);
}, y, x, (char)ch);
}
}
/*************
* IMPLEMENTED
**************/
int start_color(){
return 1;
}
/*************
* IMPLEMENTED
**************/
bool has_colors() {
return true;
}
/*************
* IMPLEMENTED
**************/
int init_pair(short pair, short f, short b) {
_ColorPair* new_color_pair = malloc(sizeof(_ColorPair));
new_color_pair->pair = pair;
new_color_pair->f = f;
new_color_pair->b = b;
new_color_pair->next = NULL;
if (!_color_pairs) {
_color_pairs = malloc(sizeof(_ColorPair*));
_color_pairs = new_color_pair;
return 1;
}
_ColorPair* p;
for (p=_color_pairs; p->next!=NULL; p = (_ColorPair*)p->next);
p->next = new_color_pair;
return 1;
}
/********************
* NOT IMPLEMENTED YET
********************/
int use_default_colors(void) {
return 0;
}
/*************
* IMPLEMENTED
**************/
int clear(void) {
EM_ASM(
window.term.clear();
);
return 1;
}
/********************
* NOT IMPLEMENTED YET
********************/
int refresh(void) {
return 0;
}
/*************
* IMPLEMENTED
**************/
int pair_content(short pair, short *f, short *b) {
_ColorPair* p = _get_color_pair(pair);
if (!p) {
return 0;
}
*f=p->f;
*b=p->b;
return 1;
}
void timeout(int delay) {
_timeout = delay;
}