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