| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/linux/lnxcon_raw.cpp $ |
| 21 | * $Revision: 1.2 $ |
| 22 | * $Date: 2004/02/25 00:04:06 $ |
| 23 | * $Author: ryan $ |
| 24 | * |
| 25 | * stdout console driver |
| 26 | * |
| 27 | * $Log: lnxcon_raw.cpp,v $ |
| 28 | * Revision 1.2 2004/02/25 00:04:06 ryan |
| 29 | * Removed loki_utils dependency and ported to MacOS X (runs, but incomplete). |
| 30 | * |
| 31 | * Revision 1.1.1.1 2000/04/18 00:00:39 icculus |
| 32 | * initial checkin |
| 33 | * |
| 34 | * |
| 35 | * 2 7/19/99 12:54p Jeff |
| 36 | * created lnxcon_raw.cpp so ncurses is now only used for SVGALib, cuts |
| 37 | * down on redraw |
| 38 | * |
| 39 | * $NoKeywords: $ |
| 40 | */ |
| 41 | |
| 42 | #include <cstdlib> |
| 43 | #include <cstring> |
| 44 | #include <cstdio> |
| 45 | #include <cctype> |
| 46 | |
| 47 | #include "AppConsole.h" |
| 48 | #include "ddio_common.h" |
| 49 | |
| 50 | static char *Con_raw_read_buf = nullptr; // The next buffer of text from user input |
| 51 | static char *Con_raw_inp_buf = nullptr, |
| 52 | Con_raw_inp_pos = 0; // Currently updating input buffer of text (and it's position) |
| 53 | static char Con_raw_last_command[CON_MAX_STRINGLEN]; // The last command entered by the user |
| 54 | static int Con_raw_cols = 0, Con_raw_rows = 0; // The size of the main window (input window is (1 row, Con_cols)) |
| 55 | |
| 56 | // put some data up on the screen |
| 57 | void con_raw_Puts(int window, const char *str); |
| 58 | |
| 59 | bool con_raw_Input(char *buf, int buflen) { |
| 60 | if (!Con_raw_read_buf) { // there is no read buffer...yipes |
| 61 | *buf = '\0'; |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (Con_raw_read_buf[0]) { |
| 66 | // we have a new buffer of input...send it away |
| 67 | strncpy(buf, Con_raw_read_buf, buflen - 1); |
| 68 | buf[buflen - 1] = 0; |
| 69 | Con_raw_read_buf[0] = 0; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | void con_raw_Defer() { |
| 77 | int keypressed = ddio_KeyInKey(); |
| 78 | |
| 79 | while (keypressed != 0) { |
| 80 | // we got a key...handle it |
| 81 | switch (keypressed) { |
| 82 | case KEY_LEFT: |
| 83 | case KEY_RIGHT: |
| 84 | case KEY_UP: |
| 85 | case KEY_DELETE: |
| 86 | case KEY_ESC: |
| 87 | break; |
| 88 | case KEY_BACKSP: { |
| 89 | // Move the caret back one space, and then |
| 90 | // process this like the DEL key. |
| 91 | if (Con_raw_inp_pos > 0) { |
| 92 | Con_raw_inp_pos--; |
| 93 | |
| 94 | for (int x = Con_raw_inp_pos; x < Con_raw_cols; x++) |
| 95 | Con_raw_inp_buf[x] = Con_raw_inp_buf[x + 1]; |
| 96 | |
| 97 | con_raw_Puts(-1, "\b \b"); |
| 98 | } |
| 99 | } break; |
| 100 | case KEY_ENTER: { |
| 101 | // Go to the beginning of the next line. |
| 102 | // The bottom line wraps around to the top. |
| 103 | strcpy(Con_raw_read_buf, Con_raw_inp_buf); |
| 104 | strcat(Con_raw_inp_buf, "\n"); |
| 105 | |
| 106 | // go to the next line |
| 107 | con_raw_Puts(-1, "\n"); |
| 108 | |
| 109 | // only save the buffer if there is text in the buffer |
| 110 | char *p = Con_raw_read_buf; |
| 111 | while (*p) { |
| 112 | if (isalnum(*p)) { |
| 113 | strcpy(Con_raw_last_command, Con_raw_read_buf); |
| 114 | break; |
| 115 | } |
| 116 | p++; |
| 117 | } |
| 118 | |
| 119 | memset(Con_raw_inp_buf, 0, Con_raw_cols + 4); |
| 120 | Con_raw_inp_pos = 0; |
| 121 | } break; |
| 122 | default: |
| 123 | if (Con_raw_inp_pos < (Con_raw_cols - 2)) { |
| 124 | // Add the character to the text buffer. |
| 125 | uint8_t str[2]; |
| 126 | str[0] = ddio_KeyToAscii(keypressed); |
| 127 | str[1] = 0; |
| 128 | if (str[0] != 255) { |
| 129 | Con_raw_inp_buf[Con_raw_inp_pos] = str[0]; |
| 130 | Con_raw_inp_buf[Con_raw_inp_pos + 1] = '\0'; |
| 131 | Con_raw_inp_pos++; |
| 132 | con_raw_Puts(-1, (const char *)str); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | keypressed = ddio_KeyInKey(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | bool con_raw_Create() { |
| 142 | Con_raw_cols = 80; |
| 143 | Con_raw_rows = 24; // one less, since the bottom window takes up one row |
| 144 | |
| 145 | // allocate any memory needed for buffers |
| 146 | Con_raw_inp_buf = (char *)malloc(sizeof(char) * (Con_raw_cols + 4)); |
| 147 | Con_raw_read_buf = (char *)malloc(sizeof(char) * (Con_raw_cols + 4)); |
| 148 | if (!Con_raw_inp_buf || !Con_raw_read_buf) { |
| 149 | // error allocating memory |
| 150 | return false; |
| 151 | } |
| 152 | memset(Con_raw_inp_buf, 0, sizeof(char) * (Con_raw_cols + 4)); |
| 153 | memset(Con_raw_read_buf, 0, sizeof(char) * (Con_raw_cols + 4)); |
| 154 | Con_raw_last_command[0] = '\0'; |
| 155 | |
| 156 | Con_raw_inp_pos = 0; |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | void con_raw_Destroy() { |
| 162 | // free any allocated memory |
| 163 | if (Con_raw_inp_buf) { |
| 164 | free(Con_raw_inp_buf); |
| 165 | Con_raw_inp_buf = nullptr; |
| 166 | } |
| 167 | |
| 168 | if (Con_raw_read_buf) { |
| 169 | free(Con_raw_read_buf); |
| 170 | Con_raw_read_buf = nullptr; |
| 171 | } |
| 172 | Con_raw_cols = Con_raw_rows = 0; |
| 173 | } |
| 174 | |
| 175 | // put some data up on the screen |
| 176 | void con_raw_Puts(int window, const char *str) { |
| 177 | fprintf(stdout, str, strlen(str)); |
| 178 | fflush(stdout); |
| 179 | } |