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   tb_clear();
 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 '" << inst.original_string << "'\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_WHITE;
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_change_cell(Display_column, Display_row, c, color, bg_color);
131   if (c == '\n' || c == '\r') {
132   ¦ if (Display_row < height-1) {
133   ¦ ¦ Display_column = 0;
134   ¦ ¦ ++Display_row;
135   ¦ ¦ tb_set_cursor(Display_column, Display_row);
136   ¦ }
137   ¦ break;
138   }
139   if (c == '\b') {
140   ¦ if (Display_column > 0) {
141   ¦ ¦ tb_change_cell(Display_column-1, Display_row, ' ', color, bg_color);
142   ¦ ¦ --Display_column;
143   ¦ ¦ tb_set_cursor(Display_column, Display_row);
144   ¦ }
145   ¦ break;
146   }
147   if (Display_column < width-1) {
148   ¦ ++Display_column;
149   ¦ tb_set_cursor(Display_column, Display_row);
150   }
151   break;
152 }
153 
154 :(before "End Primitive Recipe Declarations")
155 CURSOR_POSITION_ON_DISPLAY,
156 :(before "End Primitive Recipe Numbers")
157 put(Recipe_ordinal, "cursor-position-on-display", CURSOR_POSITION_ON_DISPLAY);
158 :(before "End Primitive Recipe Checks")
159 case CURSOR_POSITION_ON_DISPLAY: {
160   break;
161 }
162 :(before "End Primitive Recipe Implementations")
163 case CURSOR_POSITION_ON_DISPLAY: {
164   CHECK_SCREEN;
165   products.resize(2);
166   products.at(0).push_back(Display_row);
167   products.at(1).push_back(Display_column);
168   break;
169 }
170 
171 :(before "End Primitive Recipe Declarations")
172 MOVE_CURSOR_ON_DISPLAY,
173 :(before "End Primitive Recipe Numbers")
174 put(Recipe_ordinal, "move-cursor-on-display", MOVE_CURSOR_ON_DISPLAY);
175 :(before "End Primitive Recipe Checks")
176 case MOVE_CURSOR_ON_DISPLAY: {
177   if (SIZE(inst.ingredients) != 2) {
178   ¦ raise << maybe(get(Recipe, r).name) << "'move-cursor-on-display' requires two ingredients, but got '" << inst.original_string << "'\n" << end();
179   ¦ break;
180   }
181   if (!is_mu_number(inst.ingredients.at(0))) {
182   ¦ 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();
183   ¦ break;
184   }
185   if (!is_mu_number(inst.ingredients.at(1))) {
186   ¦ 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();
187   ¦ break;
188   }
189   break;
190 }
191 :(before "End Primitive Recipe Implementations")
192 case MOVE_CURSOR_ON_DISPLAY: {
193   CHECK_SCREEN;
194   Display_row = ingredients.at(0).at(0);
195   Display_column = ingredients.at(1).at(0);
196   tb_set_cursor(Display_column, Display_row);
197   break;
198 }
199 
200 :(before "End Primitive Recipe Declarations")
201 MOVE_CURSOR_DOWN_ON_DISPLAY,
202 :(before "End Primitive Recipe Numbers")
203 put(Recipe_ordinal, "move-cursor-down-on-display", MOVE_CURSOR_DOWN_ON_DISPLAY);
204 :(before "End Primitive Recipe Checks")
205 case MOVE_CURSOR_DOWN_ON_DISPLAY: {
206   break;
207 }
208 :(before "End Primitive Recipe Implementations")
209 case MOVE_CURSOR_DOWN_ON_DISPLAY: {
210   CHECK_SCREEN;
211   int h=tb_height();
212   int height = (h >= 0) ? h : 0;
213   if (Display_row < height-1) {
214   ¦ ++Display_row;
215   ¦ tb_set_cursor(Display_column, Display_row);
216   }
217   break;
218 }
219 
220 :(before "End Primitive Recipe Declarations")
221 MOVE_CURSOR_UP_ON_DISPLAY,
222 :(before "End Primitive Recipe Numbers")
223 put(Recipe_ordinal, "move-cursor-up-on-display", MOVE_CURSOR_UP_ON_DISPLAY);
224 :(before "End Primitive Recipe Checks")
225 case MOVE_CURSOR_UP_ON_DISPLAY: {
226   break;
227 }
228 :(before "End Primitive Recipe Implementations")
229 case MOVE_CURSOR_UP_ON_DISPLAY: {
230   CHECK_SCREEN;
231   if (Display_row > 0) {
232   ¦ --Display_row;
233   ¦ tb_set_cursor(Display_column, Display_row);
234   }
235   break;
236 }
237 
238 :(before "End Primitive Recipe Declarations")
239 MOVE_CURSOR_RIGHT_ON_DISPLAY,
240 :(before "End Primitive Recipe Numbers")
241 put(Recipe_ordinal, "move-cursor-right-on-display", MOVE_CURSOR_RIGHT_ON_DISPLAY);
242 :(before "End Primitive Recipe Checks")
243 case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
244   break;
245 }
246 :(before "End Primitive Recipe Implementations")
247 case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
248   CHECK_SCREEN;
249   int w=tb_width();
250   int width = (w >= 0) ? w : 0;
251   if (Display_column < width-1) {
252   ¦ ++Display_column;
253   ¦ tb_set_cursor(Display_column, Display_row);
254   }
255   break;
256 }
257 
258 :(before "End Primitive Recipe Declarations")
259 MOVE_CURSOR_LEFT_ON_DISPLAY,
260 :(before "End Primitive Recipe Numbers")
261 put(Recipe_ordinal, "move-cursor-left-on-display", MOVE_CURSOR_LEFT_ON_DISPLAY);
262 :(before "End Primitive Recipe Checks")
263 case MOVE_CURSOR_LEFT_ON_DISPLAY: {
264   break;
265 }
266 :(before "End Primitive Recipe Implementations")
267 case MOVE_CURSOR_LEFT_ON_DISPLAY: {
268   CHECK_SCREEN;
269   if (Display_column > 0) {
270   ¦ --Display_column;
271   ¦ tb_set_cursor(Display_column, Display_row);
272   }
273   break;
274 }
275 
276 //: as a convenience, make $print mostly work in console mode
277 :(before "End $print 10/newline Special-cases")
278 else if (tb_is_active()) {
279   move_cursor_to_start_of_next_line_on_display();
280 }
281 :(code)
282 void move_cursor_to_start_of_next_line_on_display() {
283   if (Display_row < tb_height()-1) ++Display_row;
284   else Display_row = 0;
285   Display_column = 0;
286   tb_set_cursor(Display_column, Display_row);
287 }
288 
289 :(before "End Primitive Recipe Declarations")
290 DISPLAY_WIDTH,
291 :(before "End Primitive Recipe Numbers")
292 put(Recipe_ordinal, "display-width", DISPLAY_WIDTH);
293 :(before "End Primitive Recipe Checks")
294 case DISPLAY_WIDTH: {
295   break;
296 }
297 :(before "End Primitive Recipe Implementations")
298 case DISPLAY_WIDTH: {
299   CHECK_SCREEN;
300   products.resize(1);
301   products.at(0).push_back(tb_width());
302   break;
303 }
304 
305 :(before "End Primitive Recipe Declarations")
306 DISPLAY_HEIGHT,
307 :(before "End Primitive Recipe Numbers")
308 put(Recipe_ordinal, "display-height", DISPLAY_HEIGHT);
309 :(before "End Primitive Recipe Checks")
310 case DISPLAY_HEIGHT: {
311   break;
312 }
313 :(before "End Primitive Recipe Implementations")
314 case DISPLAY_HEIGHT: {
315   CHECK_SCREEN;
316   products.resize(1);
317   products.at(0).push_back(tb_height());
318   break;
319 }
320 
321 //:: Keyboard/mouse management
322 
323 :(before "End Primitive Recipe Declarations")
324 WAIT_FOR_SOME_INTERACTION,
325 :(before "End Primitive Recipe Numbers")
326 put(Recipe_ordinal, "wait-for-some-interaction", WAIT_FOR_SOME_INTERACTION);
327 :(before "End Primitive Recipe Checks")
328 case WAIT_FOR_SOME_INTERACTION: {
329   break;
330 }
331 :(before "End Primitive Recipe Implementations")
332 case WAIT_FOR_SOME_INTERACTION: {
333   CHECK_SCREEN;
334   tb_event event;
335   tb_poll_event(&event);
336   break;
337 }
338 
339 :(before "End Primitive Recipe Declarations")
340 CHECK_FOR_INTERACTION,
341 :(before "End Primitive Recipe Numbers")
342 put(Recipe_ordinal, "check-for-interaction", CHECK_FOR_INTERACTION);
343 :(before "End Primitive Recipe Checks")
344 case CHECK_FOR_INTERACTION: {
345   break;
346 }
347 :(before "End Primitive Recipe Implementations")
348 case CHECK_FOR_INTERACTION: {
349   CHECK_CONSOLE;
350   products.resize(2);  // result and status
351   tb_event event;
352   int event_type = tb_peek_event(&event, 5/*ms*/);
353   if (event_type == TB_EVENT_KEY && event.ch) {
354   ¦ products.at(0).push_back(/*text event*/0);
355   ¦ products.at(0).push_back(event.ch);
356   ¦ products.at(0).push_back(0);
357   ¦ products.at(0).push_back(0);
358   ¦ products.at(1).push_back(/*found*/true);
359   ¦ break;
360   }
361   // treat keys within ascii as unicode characters
362   if (event_type == TB_EVENT_KEY && event.key < 0xff) {
363   ¦ products.at(0).push_back(/*text event*/0);
364   ¦ if (event.key == TB_KEY_CTRL_C) {
365   ¦ ¦ tb_shutdown();
366   ¦ ¦ exit(1);
367   ¦ }
368   ¦ if (event.key == TB_KEY_BACKSPACE2) event.key = TB_KEY_BACKSPACE;
369   ¦ if (event.key == TB_KEY_CARRIAGE_RETURN) event.key = TB_KEY_NEWLINE;
370   ¦ products.at(0).push_back(event.key);
371   ¦ products.at(0).push_back(0);
372   ¦ products.at(0).push_back(0);
373   ¦ products.at(1).push_back(/*found*/true);
374   ¦ break;
375   }
376   // keys outside ascii aren't unicode characters but arbitrary termbox inventions
377   if (event_type == TB_EVENT_KEY) {
378   ¦ products.at(0).push_back(/*keycode event*/1);
379   ¦ products.at(0).push_back(event.key);
380   ¦ products.at(0).push_back(0);
381   ¦ products.at(0).push_back(0);
382   ¦ products.at(1).push_back(/*found*/true);
383   ¦ break;
384   }
385   if (event_type == TB_EVENT_MOUSE) {
386   ¦ products.at(0).push_back(/*touch event*/2);
387   ¦ products.at(0).push_back(event.key);  // which button, etc.
388   ¦ products.at(0).push_back(event.y);  // row
389   ¦ products.at(0).push_back(event.x);  // column
390   ¦ products.at(1).push_back(/*found*/true);
391   ¦ break;
392   }
393   if (event_type == TB_EVENT_RESIZE) {
394   ¦ products.at(0).push_back(/*resize event*/3);
395   ¦ products.at(0).push_back(event.w);  // width
396   ¦ products.at(0).push_back(event.h);  // height
397   ¦ products.at(0).push_back(0);
398   ¦ products.at(1).push_back(/*found*/true);
399   ¦ break;
400   }
401   assert(event_type == 0);
402   products.at(0).push_back(0);
403   products.at(0).push_back(0);
404   products.at(0).push_back(0);
405   products.at(0).push_back(0);
406   products.at(1).push_back(/*found*/false);
407   break;
408 }
409 
410 :(before "End Primitive Recipe Declarations")
411 INTERACTIONS_LEFT,
412 :(before "End Primitive Recipe Numbers")
413 put(Recipe_ordinal, "interactions-left?", INTERACTIONS_LEFT);
414 :(before "End Primitive Recipe Checks")
415 case INTERACTIONS_LEFT: {
416   break;
417 }
418 :(before "End Primitive Recipe Implementations")
419 case INTERACTIONS_LEFT: {
420   CHECK_CONSOLE;
421   products.resize(1);
422   products.at(0).push_back(tb_event_ready());
423   break;
424 }
425 
426 //: hacks to make text-mode apps more responsive under Unix
427 
428 :(before "End Primitive Recipe Declarations")
429 CLEAR_LINE_ON_DISPLAY,
430 :(before "End Primitive Recipe Numbers")
431 put(Recipe_ordinal, "clear-line-on-display", CLEAR_LINE_ON_DISPLAY);
432 :(before "End Primitive Recipe Checks")
433 case CLEAR_LINE_ON_DISPLAY: {
434   break;
435 }
436 :(before "End Primitive Recipe Implementations")
437 case CLEAR_LINE_ON_DISPLAY: {
438   CHECK_SCREEN;
439   int width = tb_width();
440   for (int x = Display_column;  x < width;  ++x) {
441   ¦ tb_change_cell(x, Display_row, ' ', TB_WHITE, TB_BLACK);
442   }
443   tb_set_cursor(Display_column, Display_row);
444   break;
445 }
446 
447 :(before "End Primitive Recipe Declarations")
448 CLEAR_DISPLAY_FROM,
449 :(before "End Primitive Recipe Numbers")
450 put(Recipe_ordinal, "clear-display-from", CLEAR_DISPLAY_FROM);
451 :(before "End Primitive Recipe Checks")
452 case CLEAR_DISPLAY_FROM: {
453   break;
454 }
455 :(before "End Primitive Recipe Implementations")
456 case CLEAR_DISPLAY_FROM: {
457   CHECK_SCREEN;
458   // todo: error checking
459   int row = ingredients.at(0).at(0);
460   int column = ingredients.at(1).at(0);
461   int left = ingredients.at(2).at(0);
462   int right = ingredients.at(3).at(0);
463   int height=tb_height();
464   for (/*nada*/;  row < height;  ++row, column=left) {  // start column from left in every inner loop except first
465   ¦ for (/*nada*/;  column <= right;  ++column) {
466   ¦ ¦ tb_change_cell(column, row, ' ', TB_WHITE, TB_BLACK);
467   ¦ }
468   }
469   break;
470 }