1 //: Take raw control of the text-mode display and console, putting it in
  2 //: 'console' mode rather than the usual automatically-scrolling 'typewriter'
  3 //: mode.
  4 
  5 //:: Display management
  6 
  7 :(before "End Globals")
  8 int Display_row = 0;
  9 int Display_column = 0;
 10 
 11 :(before "End Includes")
 12 #define CHECK_SCREEN \
 13   ¦ if (!tb_is_active()) { \
 14   ¦ ¦ if (Run_tests) \
 15   ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to print to real screen in a test!\n" << end(); \
 16   ¦ ¦ else \
 17   ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to print to real screen before 'open-console' or after 'close-console'\n" << end(); \
 18   ¦ ¦ break; \
 19   ¦ }
 20 #define CHECK_CONSOLE \
 21   ¦ if (!tb_is_active()) { \
 22   ¦ ¦ if (Run_tests) \
 23   ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to read event from real keyboard/mouse in a test!\n" << end(); \
 24   ¦ ¦ else \
 25   ¦ ¦ ¦ raise << maybe(current_recipe_name()) << "tried to read event from real keyboard/mouse before 'open-console' or after 'close-console'\n" << end(); \
 26   ¦ ¦ break; \
 27   ¦ }
 28 
 29 :(before "End Primitive Recipe Declarations")
 30 OPEN_CONSOLE,
 31 :(before "End Primitive Recipe Numbers")
 32 put(Recipe_ordinal, "open-console", OPEN_CONSOLE);
 33 :(before "End Primitive Recipe Checks")
 34 case OPEN_CONSOLE: {
 35   break;
 36 }
 37 :(before "End Primitive Recipe Implementations")
 38 case OPEN_CONSOLE: {
 39   tb_init();
 40   std::setvbuf(stdout, NULL, _IONBF, 0);  // disable buffering in cout
 41   Display_row = Display_column = 0;
 42   int width = tb_width();
 43   int height = tb_height();
 44   if (width > 222 || height > 222) tb_shutdown();
 45   if (width > 222)
 46   ¦ raise << "sorry, Mu doesn't support windows wider than 222 characters in console mode. Please resize your window.\n" << end();
 47   if (height > 222)
 48   ¦ raise << "sorry, Mu doesn't support windows taller than 222 characters in console mode. Please resize your window.\n" << end();
 49   break;
 50 }
 51 
 52 :(before "End Primitive Recipe Declarations")
 53 CLOSE_CONSOLE,
 54 :(before "End Primitive Recipe Numbers")
 55 put(Recipe_ordinal, "close-console", CLOSE_CONSOLE);
 56 :(before "End Primitive Recipe Checks")
 57 case CLOSE_CONSOLE: {
 58   break;
 59 }
 60 :(before "End Primitive Recipe Implementations")
 61 case CLOSE_CONSOLE: {
 62   tb_clear();
 63   tb_shutdown();
 64   break;
 65 }
 66 
 67 :(before "End Teardown")
 68 tb_shutdown();
 69 
 70 :(before "End Primitive Recipe Declarations")
 71 CLEAR_DISPLAY,
 72 :(before "End Primitive Recipe Numbers")
 73 put(Recipe_ordinal, "clear-display", CLEAR_DISPLAY);
 74 :(before "End Primitive Recipe Checks")
 75 case CLEAR_DISPLAY: {
 76   break;
 77 }
 78 :(before "End Primitive Recipe Implementations")
 79 case CLEAR_DISPLAY: {
 80   CHECK_SCREEN;
 81   tb_clear();
 82   Display_row = Display_column = 0;
 83   break;
 84 }
 85 
 86 :(before "End Primitive Recipe Declarations")
 87 PRINT_CHARACTER_TO_DISPLAY,
 88 :(before "End Primitive Recipe Numbers")
 89 put(Recipe_ordinal, "print-character-to-display", PRINT_CHARACTER_TO_DISPLAY);
 90 :(before "End Primitive Recipe Checks")
 91 case PRINT_CHARACTER_TO_DISPLAY: {
 92   if (inst.ingredients.empty()) {
 93   ¦ raise << maybe(get(Recipe, r).name) << "'print-character-to-display' requires at least one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
 94   ¦ break;
 95   }
 96   if (!is_mu_number(inst.ingredients.at(0))) {
 97   ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'print-character-to-display' should be a character, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
 98   ¦ break;
 99   }
100   if (SIZE(inst.ingredients) > 1) {
101   ¦ if (!is_mu_number(inst.ingredients.at(1))) {
102   ¦ ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'print-character-to-display' should be a foreground color number, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
103   ¦ ¦ break;
104   ¦ }
105   }
106   if (SIZE(inst.ingredients) > 2) {
107   ¦ if (!is_mu_number(inst.ingredients.at(2))) {
108   ¦ ¦ raise << maybe(get(Recipe, r).name) << "third ingredient of 'print-character-to-display' should be a background color number, but got '" << inst.ingredients.at(2).original_string << "'\n" << end();
109   ¦ ¦ break;
110   ¦ }
111   }
112   break;
113 }
114 :(before "End Primitive Recipe Implementations")
115 case PRINT_CHARACTER_TO_DISPLAY: {
116   CHECK_SCREEN;
117   int h=tb_height(), w=tb_width();
118   int height = (h >= 0) ? h : 0;
119   int width = (w >= 0) ? w : 0;
120   int c = ingredients.at(0).at(0);
121   int color = TB_BLACK;
122   if (SIZE(ingredients) > 1) {
123   ¦ color = ingredients.at(1).at(0);
124   }
125   int bg_color = TB_BLACK;
126   if (SIZE(ingredients) > 2) {
127   ¦ bg_color = ingredients.at(2).at(0);
128   ¦ if (bg_color == 0) bg_color = TB_BLACK;
129   }
130   tb_print(c, color, bg_color);
131   // track row and column, mimicking what happens on screen
132   if (c == '\n') {
133   ¦ if (Display_row < height-1) ++Display_row;  // otherwise we scroll and Display_row remains unchanged
134   }
135   else if (c == '\r') {
136   ¦ Display_column = 0;
137   }
138   else if (c == '\b') {
139   ¦ if (Display_column > 0) --Display_column;
140   }
141   else {
142   ¦ ++Display_column;
143   ¦ if (Display_column >= width) {
144   ¦ ¦ Display_column = 0;
145   ¦ ¦ if (Display_row < height-1) ++Display_row;
146   ¦ }
147   }
148   break;
149 }
150 
151 :(before "End Primitive Recipe Declarations")
152 CURSOR_POSITION_ON_DISPLAY,
153 :(before "End Primitive Recipe Numbers")
154 put(Recipe_ordinal, "cursor-position-on-display", CURSOR_POSITION_ON_DISPLAY);
155 :(before "End Primitive Recipe Checks")
156 case CURSOR_POSITION_ON_DISPLAY: {
157   break;
158 }
159 :(before "End Primitive Recipe Implementations")
160 case CURSOR_POSITION_ON_DISPLAY: {
161   CHECK_SCREEN;
162   products.resize(2);
163   products.at(0).push_back(Display_row);
164   products.at(1).push_back(Display_column);
165   break;
166 }
167 
168 :(before "End Primitive Recipe Declarations")
169 MOVE_CURSOR_ON_DISPLAY,
170 :(before "End Primitive Recipe Numbers")
171 put(Recipe_ordinal, "move-cursor-on-display", MOVE_CURSOR_ON_DISPLAY);
172 :(before "End Primitive Recipe Checks")
173 case MOVE_CURSOR_ON_DISPLAY: {
174   if (SIZE(inst.ingredients) != 2) {
175   ¦ raise << maybe(get(Recipe, r).name) << "'move-cursor-on-display' requires two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
176   ¦ break;
177   }
178   if (!is_mu_number(inst.ingredients.at(0))) {
179   ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'move-cursor-on-display' should be a row number, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
180   ¦ break;
181   }
182   if (!is_mu_number(inst.ingredients.at(1))) {
183   ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'move-cursor-on-display' should be a column number, but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
184   ¦ break;
185   }
186   break;
187 }
188 :(before "End Primitive Recipe Implementations")
189 case MOVE_CURSOR_ON_DISPLAY: {
190   CHECK_SCREEN;
191   Display_row = ingredients.at(0).at(0);
192   Display_column = ingredients.at(1).at(0);
193   tb_set_cursor(Display_column, Display_row);
194   break;
195 }
196 
197 :(before "End Primitive Recipe Declarations")
198 MOVE_CURSOR_DOWN_ON_DISPLAY,
199 :(before "End Primitive Recipe Numbers")
200 put(Recipe_ordinal, "move-cursor-down-on-display", MOVE_CURSOR_DOWN_ON_DISPLAY);
201 :(before "End Primitive Recipe Checks")
202 case MOVE_CURSOR_DOWN_ON_DISPLAY: {
203   break;
204 }
205 :(before "End Primitive Recipe Implementations")
206 case MOVE_CURSOR_DOWN_ON_DISPLAY: {
207   CHECK_SCREEN;
208   int h=tb_height();
209   int height = (h >= 0) ? h : 0;
210   if (Display_row < height-1) {
211   ¦ ++Display_row;
212   ¦ tb_set_cursor(Display_column, Display_row);
213   }
214   break;
215 }
216 
217 :(before "End Primitive Recipe Declarations")
218 MOVE_CURSOR_UP_ON_DISPLAY,
219 :(before "End Primitive Recipe Numbers")
220 put(Recipe_ordinal, "move-cursor-up-on-display", MOVE_CURSOR_UP_ON_DISPLAY);
221 :(before "End Primitive Recipe Checks")
222 case MOVE_CURSOR_UP_ON_DISPLAY: {
223   break;
224 }
225 :(before "End Primitive Recipe Implementations")
226 case MOVE_CURSOR_UP_ON_DISPLAY: {
227   CHECK_SCREEN;
228   if (Display_row > 0) {
229   ¦ --Display_row;
230   ¦ tb_set_cursor(Display_column, Display_row);
231   }
232   break;
233 }
234 
235 :(before "End Primitive Recipe Declarations")
236 MOVE_CURSOR_RIGHT_ON_DISPLAY,
237 :(before "End Primitive Recipe Numbers")
238 put(Recipe_ordinal, "move-cursor-right-on-display", MOVE_CURSOR_RIGHT_ON_DISPLAY);
239 :(before "End Primitive Recipe Checks")
240 case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
241   break;
242 }
243 :(before "End Primitive Recipe Implementations")
244 case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
245   CHECK_SCREEN;
246   int w=tb_width();
247   int width = (w >= 0) ? w : 0;
248   if (Display_column < width-1) {
249   ¦ ++Display_column;
250   ¦ tb_set_cursor(Display_column, Display_row);
251   }
252   break;
253 }
254 
255 :(before "End Primitive Recipe Declarations")
256 MOVE_CURSOR_LEFT_ON_DISPLAY,
257 :(before "End Primitive Recipe Numbers")
258 put(Recipe_ordinal, "move-cursor-left-on-display", MOVE_CURSOR_LEFT_ON_DISPLAY);
259 :(before "End Primitive Recipe Checks")
260 case MOVE_CURSOR_LEFT_ON_DISPLAY: {
261   break;
262 }
263 :(before "End Primitive Recipe Implementations")
264 case MOVE_CURSOR_LEFT_ON_DISPLAY: {
265   CHECK_SCREEN;
266   if (Display_column > 0) {
267   ¦ --Display_column;
268   ¦ tb_set_cursor(Display_column, Display_row);
269   }
270   break;
271 }
272 
273 //: as a convenience, make $print mostly work in console mode
274 :(before "End $print 10/newline Special-cases")
275 else if (tb_is_active()) {
276   move_cursor_to_start_of_next_line_on_display();
277 }
278 :(code)
279 void move_cursor_to_start_of_next_line_on_display() {
280   if (Display_row < tb_height()-1) ++Display_row;
281   else Display_row = 0;
282   Display_column = 0;
283   tb_set_cursor(Display_column, Display_row);
284 }
285 
286 :(before "End Primitive Recipe Declarations")
287 DISPLAY_WIDTH,
288 :(before "End Primitive Recipe Numbers")
289 put(Recipe_ordinal, "display-width", DISPLAY_WIDTH);
290 :(before "End Primitive Recipe Checks")
291 case DISPLAY_WIDTH: {
292   break;
293 }
294 :(before "End Primitive Recipe Implementations")
295 case DISPLAY_WIDTH: {
296   CHECK_SCREEN;
297   products.resize(1);
298   products.at(0).push_back(tb_width());
299   break;
300 }
301 
302 :(before "End Primitive Recipe Declarations")
303 DISPLAY_HEIGHT,
304 :(before "End Primitive Recipe Numbers")
305 put(Recipe_ordinal, "display-height", DISPLAY_HEIGHT);
306 :(before "End Primitive Recipe Checks")
307 case DISPLAY_HEIGHT: {
308   break;
309 }
310 :(before "End Primitive Recipe Implementations")
311 case DISPLAY_HEIGHT: {
312   CHECK_SCREEN;
313   products.resize(1);
314   products.at(0).push_back(tb_height());
315   break;
316 }
317 
318 //:: Keyboard/mouse management
319 
320 :(before "End Primitive Recipe Declarations")
321 WAIT_FOR_SOME_INTERACTION,
322 :(before "End Primitive Recipe Numbers")
323 put(Recipe_ordinal, "wait-for-some-interaction", WAIT_FOR_SOME_INTERACTION);
324 :(before "End Primitive Recipe Checks")
325 case WAIT_FOR_SOME_INTERACTION: {
326   break;
327 }
328 :(before "End Primitive Recipe Implementations")
329 case WAIT_FOR_SOME_INTERACTION: {
330   CHECK_SCREEN;
331   tb_event event;
332   tb_poll_event(&event);
333   break;
334 }
335 
336 :(before "End Primitive Recipe Declarations")
337 CHECK_FOR_INTERACTION,
338 :(before "End Primitive Recipe Numbers")
339 put(Recipe_ordinal, "check-for-interaction", CHECK_FOR_INTERACTION);
340 :(before "End Primitive Recipe Checks")
341 case CHECK_FOR_INTERACTION: {
342   break;
343 }
344 :(before "End Primitive Recipe Implementations")
345 case CHECK_FOR_INTERACTION: {
346   CHECK_CONSOLE;
347   products.resize(2);  // result and status
348   tb_event event;
349   int event_type = tb_peek_event(&event, 5/*ms*/);
350   if (event_type == TB_EVENT_KEY && event.ch) {
351   ¦ products.at(0).push_back(/*text event*/0);
352   ¦ products.at(0).push_back(event.ch);
353   ¦ products.at(0).push_back(0);
354   ¦ products.at(0).push_back(0);
355   ¦ products.at(1).push_back(/*found*/true);
356   ¦ break;
357   }
358   // treat keys within ascii as unicode characters
359   if (event_type == TB_EVENT_KEY && event.key < 0xff) {
360   ¦ products.at(0).push_back(/*text event*/0);
361   ¦ if (event.key == TB_KEY_CTRL_C) {
362   ¦ ¦ tb_shutdown();
363   ¦ ¦ exit(1);
364   ¦ }
365   ¦ if (event.key == TB_KEY_BACKSPACE2) event.key = TB_KEY_BACKSPACE;
366   ¦ if (event.key == TB_KEY_CARRIAGE_RETURN) event.key = TB_KEY_NEWLINE;
367   ¦ products.at(0).push_back(event.key);
368   ¦ products.at(0).push_back(0);
369   ¦ products.at(0).push_back(0);
370   ¦ products.at(1).push_back(/*found*/true);
371   ¦ break;
372   }
373   // keys outside ascii aren't unicode characters but arbitrary termbox inventions
374   if (event_type == TB_EVENT_KEY) {
375   ¦ products.at(0).push_back(/*keycode event*/1);
376   ¦ products.at(0).push_back(event.key);
377   ¦ products.at(0).push_back(0);
378   ¦ products.at(0).push_back(0);
379   ¦ products.at(1).push_back(/*found*/true);
380   ¦ break;
381   }
382   if (event_type == TB_EVENT_MOUSE) {
383   ¦ products.at(0).push_back(/*touch event*/2);
384   ¦ products.at(0).push_back(event.key);  // which button, etc.
385   ¦ products.at(0).push_back(event.y);  // row
386   ¦ products.at(0).push_back(event.x);  // column
387   ¦ products.at(1).push_back(/*found*/true);
388   ¦ break;
389   }
390   if (event_type == TB_EVENT_RESIZE) {
391   ¦ products.at(0).push_back(/*resize event*/3);
392   ¦ products.at(0).push_back(event.w);  // width
393   ¦ products.at(0).push_back(event.h);  // height
394   ¦ products.at(0).push_back(0);
395   ¦ products.at(1).push_back(/*found*/true);
396   ¦ break;
397   }
398   assert(event_type == 0);
399   products.at(0).push_back(0);
400   products.at(0).push_back(0);
401   products.at(0).push_back(0);
402   products.at(0).push_back(0);
403   products.at(1).push_back(/*found*/false);
404   break;
405 }
406 
407 :(before "End Primitive Recipe Declarations")
408 INTERACTIONS_LEFT,
409 :(before "End Primitive Recipe Numbers")
410 put(Recipe_ordinal, "interactions-left?", INTERACTIONS_LEFT);
411 :(before "End Primitive Recipe Checks")
412 case INTERACTIONS_LEFT: {
413   break;
414 }
415 :(before "End Primitive Recipe Implementations")
416 case INTERACTIONS_LEFT: {
417   CHECK_CONSOLE;
418   products.resize(1);
419   products.at(0).push_back(tb_event_ready());
420   break;
421 }
422 
423 //: hacks to make text-mode apps more responsive under Unix
424 
425 :(before "End Primitive Recipe Declarations")
426 CLEAR_LINE_ON_DISPLAY,
427 :(before "End Primitive Recipe Numbers")
428 put(Recipe_ordinal, "clear-line-on-display", CLEAR_LINE_ON_DISPLAY);
429 :(before "End Primitive Recipe Checks")
430 case CLEAR_LINE_ON_DISPLAY: {
431   break;
432 }
433 :(before "End Primitive Recipe Implementations")
434 case CLEAR_LINE_ON_DISPLAY: {
435   CHECK_SCREEN;
436   int width = tb_width();
437   for (int x = Display_column;  x < width;  ++x)
438   ¦ tb_print(' ', TB_WHITE, TB_BLACK);
439   tb_set_cursor(Display_column, Display_row);
440   break;
441 }
442 
443 :(before "End Primitive Recipe Declarations")
444 CLEAR_DISPLAY_FROM,
445 :(before "End Primitive Recipe Numbers")
446 put(Recipe_ordinal, "clear-display-from", CLEAR_DISPLAY_FROM);
447 :(before "End Primitive Recipe Checks")
448 case CLEAR_DISPLAY_FROM: {
449   break;
450 }
451 :(before "End Primitive Recipe Implementations")
452 case CLEAR_DISPLAY_FROM: {
453   CHECK_SCREEN;
454   // todo: error checking
455   int row = ingredients.at(0).at(0);
456   int column = ingredients.at(1).at(0);
457   int left = ingredients.at(2).at(0);
458   int right = ingredients.at(3).at(0);
459   int height=tb_height();
460   for (/*nada*/;  row < height;  ++row, column=left) {  // start column from left in every inner loop except first
461   ¦ tb_set_cursor(column, row);
462   ¦ for (/*nada*/;  column <= right;  ++column)
463   ¦ ¦ tb_print(' ', TB_WHITE, TB_BLACK);
464   }
465   tb_set_cursor(Display_column, Display_row);
466   break;
467 }