2015-04-23 00:15:19 +00:00
|
|
|
//: Take charge of the text-mode display and keyboard.
|
2015-04-20 19:35:47 +00:00
|
|
|
|
2015-05-06 03:02:38 +00:00
|
|
|
// uncomment to debug console programs
|
|
|
|
:(before "End Globals")
|
|
|
|
//? ofstream LOG("log.txt");
|
|
|
|
|
2015-04-21 00:35:32 +00:00
|
|
|
//:: Display management
|
|
|
|
|
2015-04-23 00:15:19 +00:00
|
|
|
:(before "End Globals")
|
2015-05-17 09:22:41 +00:00
|
|
|
long long int Display_row = 0, Display_column = 0;
|
2015-04-23 00:15:19 +00:00
|
|
|
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Declarations")
|
2015-04-21 00:40:25 +00:00
|
|
|
SWITCH_TO_DISPLAY,
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Numbers")
|
2015-04-21 00:40:25 +00:00
|
|
|
Recipe_number["switch-to-display"] = SWITCH_TO_DISPLAY;
|
2015-06-01 06:44:52 +00:00
|
|
|
//? cerr << "switch-to-display: " << SWITCH_TO_DISPLAY << '\n'; //? 1
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Implementations")
|
2015-04-21 00:40:25 +00:00
|
|
|
case SWITCH_TO_DISPLAY: {
|
2015-04-23 00:15:19 +00:00
|
|
|
tb_init();
|
|
|
|
Display_row = Display_column = 0;
|
2015-04-20 19:35:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
2015-04-21 00:40:25 +00:00
|
|
|
RETURN_TO_CONSOLE,
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Numbers")
|
2015-04-21 00:40:25 +00:00
|
|
|
Recipe_number["return-to-console"] = RETURN_TO_CONSOLE;
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Implementations")
|
2015-04-21 00:40:25 +00:00
|
|
|
case RETURN_TO_CONSOLE: {
|
2015-04-23 00:15:19 +00:00
|
|
|
tb_shutdown();
|
2015-05-10 12:42:56 +00:00
|
|
|
//? Trace_stream->dump_layer = "all"; //? 1
|
2015-04-20 19:35:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-10 19:05:33 +00:00
|
|
|
:(before "End Teardown")
|
|
|
|
tb_shutdown();
|
|
|
|
|
2015-04-21 00:35:32 +00:00
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
CLEAR_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["clear-display"] = CLEAR_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case CLEAR_DISPLAY: {
|
2015-04-23 00:15:19 +00:00
|
|
|
tb_clear();
|
|
|
|
Display_row = Display_column = 0;
|
2015-04-21 00:35:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
CLEAR_LINE_ON_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["clear-line-on-display"] = CLEAR_LINE_ON_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case CLEAR_LINE_ON_DISPLAY: {
|
2015-05-17 09:22:41 +00:00
|
|
|
long long int width = tb_width();
|
|
|
|
for (long long int x = Display_column; x < width; ++x) {
|
2015-06-05 23:03:54 +00:00
|
|
|
tb_change_cell(x, Display_row, ' ', TB_WHITE, TB_BLACK);
|
2015-04-23 00:15:19 +00:00
|
|
|
}
|
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
2015-04-21 00:35:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
PRINT_CHARACTER_TO_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["print-character-to-display"] = PRINT_CHARACTER_TO_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case PRINT_CHARACTER_TO_DISPLAY: {
|
2015-04-23 00:15:19 +00:00
|
|
|
int h=tb_height(), w=tb_width();
|
2015-05-17 09:22:41 +00:00
|
|
|
long long int height = (h >= 0) ? h : 0;
|
|
|
|
long long int width = (w >= 0) ? w : 0;
|
|
|
|
assert(scalar(ingredients.at(0)));
|
2015-05-13 00:00:56 +00:00
|
|
|
long long int c = ingredients.at(0).at(0);
|
2015-05-27 22:57:38 +00:00
|
|
|
//? tb_shutdown(); //? 1
|
|
|
|
//? cerr << "AAA " << c << ' ' << (int)'\n' << ' ' << (int)'\r' << '\n'; //? 1
|
|
|
|
//? exit(1); //? 1
|
2015-05-10 15:34:12 +00:00
|
|
|
if (c == '\n' || c == '\r') {
|
|
|
|
if (Display_row < height-1) {
|
2015-04-23 00:15:19 +00:00
|
|
|
Display_column = 0;
|
|
|
|
++Display_row;
|
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-05-10 15:34:12 +00:00
|
|
|
if (c == '\b') {
|
|
|
|
if (Display_column > 0) {
|
2015-06-05 23:03:54 +00:00
|
|
|
tb_change_cell(Display_column-1, Display_row, ' ', TB_WHITE, TB_BLACK);
|
2015-05-10 15:34:12 +00:00
|
|
|
--Display_column;
|
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-06-05 23:03:54 +00:00
|
|
|
int color = TB_BLACK;
|
2015-05-26 01:08:18 +00:00
|
|
|
if (SIZE(ingredients) > 1) {
|
|
|
|
assert(scalar(ingredients.at(1)));
|
2015-06-05 23:03:54 +00:00
|
|
|
color = ingredients.at(1).at(0);
|
2015-05-27 22:57:38 +00:00
|
|
|
//? tb_shutdown(); //? 1
|
|
|
|
//? cerr << "AAA " << color << '\n'; //? 1
|
|
|
|
//? exit(1); //? 1
|
2015-05-26 01:08:18 +00:00
|
|
|
}
|
2015-06-05 23:03:54 +00:00
|
|
|
tb_change_cell(Display_column, Display_row, c, color, TB_BLACK);
|
2015-05-10 15:34:12 +00:00
|
|
|
if (Display_column < width-1) {
|
|
|
|
++Display_column;
|
2015-04-23 00:15:19 +00:00
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
}
|
|
|
|
tb_present();
|
2015-04-21 00:35:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
CURSOR_POSITION_ON_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["cursor-position-on-display"] = CURSOR_POSITION_ON_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case CURSOR_POSITION_ON_DISPLAY: {
|
2015-05-07 22:06:53 +00:00
|
|
|
products.resize(2);
|
|
|
|
products.at(0).push_back(Display_row);
|
|
|
|
products.at(1).push_back(Display_column);
|
2015-04-21 00:35:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
MOVE_CURSOR_ON_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["move-cursor-on-display"] = MOVE_CURSOR_ON_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case MOVE_CURSOR_ON_DISPLAY: {
|
2015-05-17 09:22:41 +00:00
|
|
|
assert(scalar(ingredients.at(0)));
|
2015-05-13 00:00:56 +00:00
|
|
|
Display_row = ingredients.at(0).at(0);
|
2015-05-17 09:22:41 +00:00
|
|
|
assert(scalar(ingredients.at(1)));
|
2015-05-13 00:00:56 +00:00
|
|
|
Display_column = ingredients.at(1).at(0);
|
2015-04-23 00:15:19 +00:00
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
2015-04-21 00:35:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-29 04:28:34 +00:00
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
MOVE_CURSOR_DOWN_ON_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["move-cursor-down-on-display"] = MOVE_CURSOR_DOWN_ON_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case MOVE_CURSOR_DOWN_ON_DISPLAY: {
|
2015-05-10 15:34:12 +00:00
|
|
|
int h=tb_height();
|
2015-05-17 09:22:41 +00:00
|
|
|
long long int height = (h >= 0) ? h : 0;
|
2015-05-10 15:34:12 +00:00
|
|
|
if (Display_row < height-1) {
|
|
|
|
Display_row++;
|
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
|
|
|
}
|
2015-04-29 04:28:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
MOVE_CURSOR_UP_ON_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["move-cursor-up-on-display"] = MOVE_CURSOR_UP_ON_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case MOVE_CURSOR_UP_ON_DISPLAY: {
|
2015-05-10 15:34:12 +00:00
|
|
|
if (Display_row > 0) {
|
|
|
|
Display_row--;
|
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
|
|
|
}
|
2015-04-29 04:28:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
MOVE_CURSOR_RIGHT_ON_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["move-cursor-right-on-display"] = MOVE_CURSOR_RIGHT_ON_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case MOVE_CURSOR_RIGHT_ON_DISPLAY: {
|
2015-05-10 15:34:12 +00:00
|
|
|
int w=tb_width();
|
2015-05-17 09:22:41 +00:00
|
|
|
long long int width = (w >= 0) ? w : 0;
|
2015-05-10 15:34:12 +00:00
|
|
|
if (Display_column < width-1) {
|
|
|
|
Display_column++;
|
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
|
|
|
}
|
2015-04-29 04:28:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
MOVE_CURSOR_LEFT_ON_DISPLAY,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["move-cursor-left-on-display"] = MOVE_CURSOR_LEFT_ON_DISPLAY;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case MOVE_CURSOR_LEFT_ON_DISPLAY: {
|
2015-05-10 15:34:12 +00:00
|
|
|
if (Display_column > 0) {
|
|
|
|
Display_column--;
|
|
|
|
tb_set_cursor(Display_column, Display_row);
|
|
|
|
tb_present();
|
|
|
|
}
|
2015-04-29 04:28:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-08 19:25:25 +00:00
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
DISPLAY_WIDTH,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["display-width"] = DISPLAY_WIDTH;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case DISPLAY_WIDTH: {
|
|
|
|
products.resize(1);
|
|
|
|
products.at(0).push_back(tb_width());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
DISPLAY_HEIGHT,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["display-height"] = DISPLAY_HEIGHT;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case DISPLAY_HEIGHT: {
|
|
|
|
products.resize(1);
|
|
|
|
products.at(0).push_back(tb_height());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-21 00:35:32 +00:00
|
|
|
//:: Keyboard management
|
|
|
|
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Declarations")
|
2015-04-20 23:42:35 +00:00
|
|
|
WAIT_FOR_KEY_FROM_KEYBOARD,
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Numbers")
|
2015-04-20 23:42:35 +00:00
|
|
|
Recipe_number["wait-for-key-from-keyboard"] = WAIT_FOR_KEY_FROM_KEYBOARD;
|
2015-04-20 19:35:47 +00:00
|
|
|
:(before "End Primitive Recipe Implementations")
|
2015-04-20 23:42:35 +00:00
|
|
|
case WAIT_FOR_KEY_FROM_KEYBOARD: {
|
2015-06-08 06:14:02 +00:00
|
|
|
tb_event event;
|
2015-05-04 20:59:56 +00:00
|
|
|
do {
|
|
|
|
tb_poll_event(&event);
|
|
|
|
} while (event.type != TB_EVENT_KEY);
|
2015-05-27 22:57:38 +00:00
|
|
|
long long int result = event.key ? event.key : event.ch;
|
|
|
|
if (result == TB_KEY_CTRL_C) tb_shutdown(), exit(1);
|
|
|
|
if (result == TB_KEY_BACKSPACE2) result = TB_KEY_BACKSPACE;
|
|
|
|
if (result == TB_KEY_CARRIAGE_RETURN) result = TB_KEY_NEWLINE;
|
2015-05-07 22:06:53 +00:00
|
|
|
products.resize(1);
|
2015-05-27 22:57:38 +00:00
|
|
|
products.at(0).push_back(result);
|
2015-05-04 20:59:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
READ_KEY_FROM_KEYBOARD,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
|
|
|
Recipe_number["read-key-from-keyboard"] = READ_KEY_FROM_KEYBOARD;
|
|
|
|
:(before "End Primitive Recipe Implementations")
|
|
|
|
case READ_KEY_FROM_KEYBOARD: {
|
2015-06-08 06:14:02 +00:00
|
|
|
tb_event event;
|
2015-05-04 20:59:56 +00:00
|
|
|
int event_type = tb_peek_event(&event, 5/*ms*/);
|
2015-05-07 22:06:53 +00:00
|
|
|
long long int result = 0;
|
|
|
|
long long int found = false;
|
2015-05-09 17:56:44 +00:00
|
|
|
//? cerr << event_type << '\n'; //? 1
|
2015-05-07 22:06:53 +00:00
|
|
|
if (event_type == TB_EVENT_KEY) {
|
2015-05-09 17:56:44 +00:00
|
|
|
result = event.key ? event.key : event.ch;
|
2015-05-14 19:06:16 +00:00
|
|
|
if (result == TB_KEY_CTRL_C) tb_shutdown(), exit(1);
|
|
|
|
if (result == TB_KEY_BACKSPACE2) result = TB_KEY_BACKSPACE;
|
|
|
|
if (result == TB_KEY_CARRIAGE_RETURN) result = TB_KEY_NEWLINE;
|
2015-05-07 22:06:53 +00:00
|
|
|
found = true;
|
2015-05-04 20:59:56 +00:00
|
|
|
}
|
2015-05-07 22:06:53 +00:00
|
|
|
products.resize(2);
|
|
|
|
products.at(0).push_back(result);
|
|
|
|
products.at(1).push_back(found);
|
2015-04-20 19:35:47 +00:00
|
|
|
break;
|
|
|
|
}
|