1
2
3
4
5
6 #include <cmajor/rt/Screen.hpp>
7 #include <cmajor/rt/Environment.hpp>
8 #include <memory>
9 #include <unordered_map>
10 #include <fstream>
11 bool runningOnWsl = false;
12 int ksup = 0;
13 int ksdn = 0;
14 int clft = 0;
15 int crgt = 0;
16 int cup = 0;
17 int cdn = 0;
18 int cpgup = 0;
19 int cpgdn = 0;
20 int chome = 0;
21 int cend = 0;
22 #ifdef _WIN32
23 #include <cmajor/system/ext/pdcurs36/curses.h>
24
25
26
27
28
29
30
31
32
33
34
35
36
37 #else
38 #include <ncurses.h>
39 void SetKeys()
40 {
41 if (runningOnWsl)
42 {
43 ksup = 337;
44 ksdn = 336;
45 clft = 545;
46 crgt = 560;
47 cup = 566;
48 cdn = 525;
49 cpgup = 555;
50 cpgdn = 550;
51 chome = 535;
52 cend = 530;
53 }
54 else
55 {
56 ksup = 337;
57 ksdn = 336;
58 clft = 544;
59 crgt = 559;
60 cup = 565;
61 cdn = 524;
62 cpgup = 554;
63 cpgdn = 549;
64 chome = 262;
65 cend = 360;
66 }
67 }
68 #endif
69
70 namespace cmajor { namespace rt {
71
72 std::unordered_map<int, int> keyMap;
73
74 void InitScreen()
75 {
76 std::string osInfo = RtGetOsInfo();
77 if (osInfo.find("Microsoft") != std::string::npos)
78 {
79 runningOnWsl = true;
80 }
81 SetKeys();
82 keyMap['\r'] = keyEnter;
83 keyMap['\n'] = keyEnter;
84 keyMap[KEY_DOWN] = keyDown;
85 keyMap[KEY_UP] = keyUp;
86 keyMap[KEY_LEFT] = keyLeft;
87 keyMap[KEY_RIGHT] = keyRight;
88 keyMap[KEY_HOME] = keyHome;
89 keyMap[KEY_F(0)] = keyF0;
90 keyMap[KEY_F(1)] = keyF1;
91 keyMap[KEY_F(2)] = keyF2;
92 keyMap[KEY_F(3)] = keyF3;
93 keyMap[KEY_F(4)] = keyF4;
94 keyMap[KEY_F(5)] = keyF5;
95 keyMap[KEY_F(6)] = keyF6;
96 keyMap[KEY_F(7)] = keyF7;
97 keyMap[KEY_F(8)] = keyF8;
98 keyMap[KEY_F(9)] = keyF9;
99 keyMap[KEY_F(10)] = keyF10;
100 keyMap[KEY_F(11)] = keyF11;
101 keyMap[KEY_F(12)] = keyF12;
102 keyMap[KEY_DC] = keyDel;
103 keyMap[KEY_IC] = keyIns;
104 keyMap[KEY_NPAGE] = keyPgDown;
105 keyMap[KEY_PPAGE] = keyPgUp;
106 keyMap[KEY_PRINT] = keyPrint;
107 keyMap[KEY_END] = keyEnd;
108 keyMap[KEY_SDC] = keyShiftDel;
109 keyMap[KEY_SEND] = keyShiftEnd;
110 keyMap[KEY_SHOME] = keyShiftHome;
111 keyMap[KEY_SLEFT] = keyShiftLeft;
112 keyMap[KEY_SRIGHT] = keyShiftRight;
113 keyMap[KEY_RESIZE] = keyResize;
114 keyMap[ksup] = keyShiftUp;
115 keyMap[ksdn] = keyShiftDown;
116 keyMap[cup] = keyControlUp;
117 keyMap[cdn] = keyControlDown;
118 keyMap[clft] = keyControlLeft;
119 keyMap[crgt] = keyControlRight;
120 keyMap[cpgup] = keyControlPgUp;
121 keyMap[cpgdn] = keyControlPgDown;
122 keyMap[chome] = keyControlHome;
123 keyMap[cend] = keyControlEnd;
124 }
125
126 void DoneScreen()
127 {
128 }
129
130 } }
131
132 extern "C" void RtInitScreen()
133 {
134 initscr();
135 }
136
137 extern "C" void RtDoneScreen()
138 {
139 endwin();
140 }
141
142 extern "C" void RtRaw()
143 {
144 raw();
145 }
146
147 extern "C" void RtNoRaw()
148 {
149 noraw();
150 }
151
152 extern "C" void RtCBreak()
153 {
154 cbreak();
155 }
156
157 extern "C" void RtNoCBreak()
158 {
159 nocbreak();
160 }
161
162 extern "C" void RtKeyPad()
163 {
164 keypad(stdscr, true);
165 }
166
167 extern "C" void RtEcho()
168 {
169 echo();
170 }
171
172 extern "C" void RtNoEcho()
173 {
174 noecho();
175 }
176
177 extern "C" void RtCursSet(int visibility)
178 {
179 curs_set(visibility);
180 }
181
182 extern "C" void RtRefresh()
183 {
184 refresh();
185 }
186
187 extern "C" void RtGetMaxYX(int* rows, int* cols)
188 {
189 int r;
190 int c;
191 getmaxyx(stdscr, r, c);
192 *rows = r;
193 *cols = c;
194 }
195
196 extern "C" void RtErase()
197 {
198 erase();
199 }
200
201 extern "C" void RtClear()
202 {
203 clear();
204 }
205
206 extern "C" void RtClearToEol()
207 {
208 clrtoeol();
209 }
210
211 extern "C" void RtGetYX(int* row, int* col)
212 {
213 int r;
214 int c;
215 getyx(stdscr, r, c);
216 *row = r;
217 *col = c;
218 }
219
220 extern "C" void RtMove(int row, int col)
221 {
222 move(row, col);
223 }
224
225 extern "C" void RtAddCh(int ch)
226 {
227 addch(ch);
228 }
229
230 extern "C" void RtAddStr(const char* str)
231 {
232 addstr(str);
233 }
234
235 extern "C" int RtGetRawCh()
236 {
237 return getch();
238 }
239
240 extern "C" int RtTranslateCh(int ch)
241 {
242 using cmajor::rt::keyMap;
243 auto it = keyMap.find(ch);
244 if (it != keyMap.cend())
245 {
246 ch = it->second;
247 }
248 return ch;
249 }
250
251 extern "C" int RtGetCh()
252 {
253 int ch = RtGetRawCh();
254 ch = RtTranslateCh(ch);
255 return ch;
256 }
257
258 extern "C" void RtGetNStr(char* str, int size)
259 {
260 getnstr(str, size);
261 }
262
263 extern "C" void RtAttrOn(int attrs)
264 {
265 attron(attrs);
266 }
267
268 extern "C" void RtAttrOff(int attrs)
269 {
270 attroff(attrs);
271 }
272
273 extern "C" void RtAttrSet(int attrs)
274 {
275 attrset(attrs);
276 }
277
278 extern "C" void RtStartColor()
279 {
280 start_color();
281 }
282
283 extern "C" void RtInitPair(short n, short foregroundColor, short backgroundColor)
284 {
285 init_pair(n, foregroundColor, backgroundColor);
286 }
287
288 extern "C" int RtColorPair(short n)
289 {
290 return COLOR_PAIR(n);
291 }
292
293 extern "C" short RtWhite()
294 {
295 return COLOR_WHITE;
296 }
297
298 extern "C" short RtBlack()
299 {
300 return COLOR_BLACK;
301 }
302
303 extern "C" short RtBlue()
304 {
305 return COLOR_BLUE;
306 }
307
308 extern "C" short RtGreen()
309 {
310 return COLOR_GREEN;
311 }
312
313 extern "C" short RtRed()
314 {
315 return COLOR_RED;
316 }
317
318 extern "C" short RtCyan()
319 {
320 return COLOR_CYAN;
321 }
322
323 extern "C" short RtMagenta()
324 {
325 return COLOR_MAGENTA;
326 }
327
328 extern "C" short RtYellow()
329 {
330 return COLOR_YELLOW;
331 }
332
333 extern "C" bool RtRunningOnWsl()
334 {
335 return runningOnWsl;
336 }