| 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/win32/wincon.cpp $ |
| 21 | * $Revision: 11 $ |
| 22 | * $Date: 4/07/99 10:56a $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/win32/wincon.cpp $ |
| 28 | * |
| 29 | * 11 4/07/99 10:56a Matt |
| 30 | * Fixed compile warning. |
| 31 | * |
| 32 | * 10 3/24/99 3:54p Jeff |
| 33 | * handle backspaces in the Printf |
| 34 | * |
| 35 | * 9 11/01/98 1:58a Jeff |
| 36 | * converted the vsprintf calls to use the Pvsprintf, which is a safe |
| 37 | * vsprintf, no buffer overflows allowed |
| 38 | * |
| 39 | * 8 10/30/98 5:52p Jeff |
| 40 | * implemented up arrow for last command, and esc to clear the line |
| 41 | * |
| 42 | * 7 10/24/98 2:18p Samir |
| 43 | * make close box inactive. |
| 44 | * |
| 45 | * 6 10/21/98 4:49p Jeff |
| 46 | * filter garbage out of strings to console |
| 47 | * |
| 48 | * 5 10/02/98 11:15a Jeff |
| 49 | * added HBITMAP, HFONT and HBRUSH type casts where needed to satisfy the |
| 50 | * compiler |
| 51 | * |
| 52 | * 4 9/16/98 6:39p Samir |
| 53 | * make sure window isn't off screen. |
| 54 | * |
| 55 | * 3 9/16/98 6:24p Samir |
| 56 | * slightly improved. |
| 57 | * |
| 58 | * 2 9/16/98 5:02p Samir |
| 59 | * reimplemented X style console. |
| 60 | * |
| 61 | * 1 9/14/98 4:02p Samir |
| 62 | * new console library. |
| 63 | * |
| 64 | * $NoKeywords: $ |
| 65 | */ |
| 66 | |
| 67 | #include <algorithm> |
| 68 | #include <cstdarg> |
| 69 | #include <cstdio> |
| 70 | #include <cstring> |
| 71 | #include <windows.h> |
| 72 | |
| 73 | #include "Application.h" |
| 74 | #include "AppConsole.h" |
| 75 | |
| 76 | #define CON_SCROLL_ROWS 25 |
| 77 | #define CON_SCROLL_COLS 80 |
| 78 | |
| 79 | static bool Con_init = false; |
| 80 | static bool Con_newline = false; |
| 81 | static char *Con_buffer = NULL; |
| 82 | static int Con_row, Con_col; |
| 83 | static char Con_read_buf[CON_SCROLL_COLS + 4]; |
| 84 | static char Con_inp_buf[CON_SCROLL_COLS + 4], Con_inp_pos = 0; |
| 85 | static char Con_last_command[CON_MAX_STRINGLEN]; |
| 86 | static int Con_ch_w, Con_ch_h; |
| 87 | static HWND hConWnd = NULL; |
| 88 | |
| 89 | void con_Create(HWND hWnd, LPCREATESTRUCT lpcs); |
| 90 | void con_Paint(HWND hWnd); |
| 91 | void con_Destroy(HWND hWnd); |
| 92 | void con_Puts(const char *str); |
| 93 | void con_Scroll(); |
| 94 | int con_KeyDown(HWND hWnd, UINT vkey); |
| 95 | int con_Char(HWND hWnd, UINT vkey); |
| 96 | |
| 97 | void con_Printf(const char *fmt, ...) { |
| 98 | char buf[CON_MAX_STRINGLEN]; |
| 99 | std::va_list args; |
| 100 | |
| 101 | // filter out messages |
| 102 | va_start(args, fmt); |
| 103 | std::vsnprintf(buf, CON_MAX_STRINGLEN, fmt, args); |
| 104 | |
| 105 | int len = strlen(buf); |
| 106 | if (len >= CON_MAX_STRINGLEN) { |
| 107 | // we overflowed our buffer!!! |
| 108 | // we need to do some sort of error here!!!! |
| 109 | buf[CON_MAX_STRINGLEN - 1] = '\0'; |
| 110 | } |
| 111 | |
| 112 | // filter out unprintable characters |
| 113 | char *p, *fp, filter_buf[CON_MAX_STRINGLEN]; |
| 114 | p = buf; |
| 115 | fp = filter_buf; |
| 116 | |
| 117 | while (*p) { |
| 118 | if (*p == 0x01) { |
| 119 | // this is a color, skip the next 3 |
| 120 | p += 4; |
| 121 | } else { |
| 122 | if (isalnum(*p) || ispunct(*p) || (*p == ' ') || (*p == '\n') || (*p == '\r') || (*p == '\b')) { |
| 123 | *fp = *p; |
| 124 | fp++; |
| 125 | } |
| 126 | p++; |
| 127 | } |
| 128 | } |
| 129 | *fp = '\0'; |
| 130 | |
| 131 | con_Puts(filter_buf); |
| 132 | |
| 133 | if (hConWnd) { |
| 134 | InvalidateRect(hConWnd, NULL, TRUE); |
| 135 | UpdateWindow(hConWnd); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | bool con_Input(char *buf, int buflen) { |
| 140 | if (Con_read_buf[0]) { |
| 141 | strncpy(buf, Con_read_buf, buflen - 1); |
| 142 | buf[buflen - 1] = 0; |
| 143 | Con_read_buf[0] = 0; |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | void con_Defer() { |
| 151 | if (Con_newline) { |
| 152 | Con_newline = false; |
| 153 | con_Puts("%"); |
| 154 | if (hConWnd) { |
| 155 | InvalidateRect(hConWnd, NULL, TRUE); |
| 156 | UpdateWindow(hConWnd); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // console window. |
| 162 | LRESULT WINAPI MyConProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { |
| 163 | HDC hdc; |
| 164 | |
| 165 | switch (msg) { |
| 166 | case WM_CREATE: { |
| 167 | LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam; |
| 168 | con_Create(hWnd, lpcs); |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | case WM_PAINT: |
| 173 | con_Paint(hWnd); |
| 174 | break; |
| 175 | |
| 176 | case WM_SYSCOMMAND: |
| 177 | if (wParam == SC_CLOSE) { |
| 178 | return 0; |
| 179 | } |
| 180 | break; |
| 181 | |
| 182 | case WM_SETFOCUS: { |
| 183 | // The window has the input focus. Load the |
| 184 | // application-defined caret resource. |
| 185 | HFONT oldfont; |
| 186 | |
| 187 | hdc = GetDC(hWnd); |
| 188 | |
| 189 | oldfont = (HFONT)SelectObject(hdc, (HFONT)GetStockObject(ANSI_FIXED_FONT)); |
| 190 | SelectObject(hdc, oldfont); |
| 191 | CreateCaret(hWnd, NULL, Con_ch_w, Con_ch_h); |
| 192 | SetCaretPos(Con_col * Con_ch_w, Con_row * Con_ch_h); |
| 193 | ShowCaret(hWnd); |
| 194 | |
| 195 | ReleaseDC(hWnd, hdc); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | case WM_KILLFOCUS: |
| 200 | // The window is losing the input focus, |
| 201 | // so destroy the caret. |
| 202 | DestroyCaret(); |
| 203 | break; |
| 204 | |
| 205 | case WM_KEYDOWN: |
| 206 | return con_KeyDown(hWnd, wParam); |
| 207 | |
| 208 | case WM_CHAR: |
| 209 | return con_Char(hWnd, wParam); |
| 210 | } |
| 211 | |
| 212 | return DefWindowProc(hWnd, msg, wParam, lParam); |
| 213 | } |
| 214 | |
| 215 | // console window helper functions |
| 216 | void con_Create(HWND hWnd, LPCREATESTRUCT lpcs) { |
| 217 | oeWin32Application *app = (oeWin32Application *)lpcs->lpCreateParams; |
| 218 | HDC hdc; |
| 219 | HFONT oldfont; |
| 220 | RECT rect; |
| 221 | TEXTMETRIC tm; |
| 222 | |
| 223 | // create console region |
| 224 | Con_row = 0; |
| 225 | Con_col = 0; |
| 226 | Con_buffer = new char[CON_SCROLL_ROWS * (CON_SCROLL_COLS + 1)]; |
| 227 | Con_read_buf[0] = 0; |
| 228 | Con_inp_pos = 0; |
| 229 | memset(Con_inp_buf, 0, sizeof(Con_inp_buf)); |
| 230 | memset(Con_buffer, 0, CON_SCROLL_ROWS * (CON_SCROLL_COLS + 1)); |
| 231 | memset(Con_last_command, 0, sizeof(Con_last_command)); |
| 232 | |
| 233 | con_Puts("Outrage PC Console v1.0\n"); |
| 234 | |
| 235 | Con_newline = true; |
| 236 | |
| 237 | // get font width and height |
| 238 | hdc = GetDC(hWnd); |
| 239 | oldfont = (HFONT)SelectObject(hdc, (HFONT)GetStockObject(ANSI_FIXED_FONT)); |
| 240 | GetTextMetrics(hdc, &tm); |
| 241 | Con_ch_w = tm.tmMaxCharWidth; |
| 242 | Con_ch_h = tm.tmHeight; |
| 243 | rect.left = lpcs->x; |
| 244 | rect.top = lpcs->y; |
| 245 | rect.right = rect.left + Con_ch_w * CON_SCROLL_COLS; |
| 246 | rect.bottom = rect.top + Con_ch_h * CON_SCROLL_ROWS; |
| 247 | |
| 248 | AdjustWindowRect(&rect, lpcs->style, FALSE); |
| 249 | MoveWindow(hWnd, lpcs->x, lpcs->y, rect.right - rect.left, rect.bottom - rect.top, TRUE); |
| 250 | |
| 251 | app->m_X = lpcs->x; |
| 252 | app->m_Y = lpcs->y; |
| 253 | app->m_W = rect.right - rect.left; |
| 254 | app->m_H = rect.bottom - rect.top; |
| 255 | |
| 256 | SelectObject(hdc, oldfont); |
| 257 | ReleaseDC(hWnd, hdc); |
| 258 | |
| 259 | EnableMenuItem(GetSystemMenu(hWnd, FALSE), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED); |
| 260 | |
| 261 | hConWnd = hWnd; |
| 262 | } |
| 263 | |
| 264 | void con_Paint(HWND hWnd) { |
| 265 | PAINTSTRUCT ps; |
| 266 | HDC hdc = BeginPaint(hWnd, &ps); |
| 267 | int i; |
| 268 | HFONT hfont; |
| 269 | |
| 270 | // use a fixed font. |
| 271 | hfont = (HFONT)SelectObject(hdc, (HFONT)GetStockObject(ANSI_FIXED_FONT)); |
| 272 | |
| 273 | // draw buffer at 0 to Con_row to end of buffer. |
| 274 | for (i = 0; i < CON_SCROLL_ROWS; i++) { |
| 275 | char *ptr = &Con_buffer[i * (CON_SCROLL_COLS + 1)]; |
| 276 | TextOut(hdc, 0, i * Con_ch_h, ptr, strlen(ptr)); |
| 277 | } |
| 278 | |
| 279 | // end |
| 280 | SelectObject(hdc, hfont); |
| 281 | EndPaint(hWnd, &ps); |
| 282 | } |
| 283 | |
| 284 | void con_Destroy(HWND hWnd) { |
| 285 | if (Con_buffer) |
| 286 | delete[] Con_buffer; |
| 287 | |
| 288 | hConWnd = NULL; |
| 289 | } |
| 290 | |
| 291 | void con_Puts(const char *str) { |
| 292 | int i, len = strlen(str); |
| 293 | char *row = &Con_buffer[Con_row * (CON_SCROLL_COLS + 1)]; |
| 294 | bool endline = false; |
| 295 | |
| 296 | for (i = 0; i < len; i++) { |
| 297 | switch (str[i]) { |
| 298 | case '\n': |
| 299 | endline = true; |
| 300 | break; |
| 301 | case '\b': |
| 302 | if (Con_col > 0) { |
| 303 | row[Con_col] = ' '; |
| 304 | Con_col--; |
| 305 | } |
| 306 | break; |
| 307 | default: |
| 308 | row[Con_col] = str[i]; |
| 309 | Con_col++; |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | if (Con_col == CON_SCROLL_COLS) { |
| 314 | endline = true; |
| 315 | } |
| 316 | |
| 317 | if (endline) { |
| 318 | Con_row++; |
| 319 | if (Con_row == CON_SCROLL_ROWS) { |
| 320 | con_Scroll(); |
| 321 | Con_row--; |
| 322 | } |
| 323 | row = &Con_buffer[Con_row * (CON_SCROLL_COLS + 1)]; |
| 324 | memset(row, 0, CON_SCROLL_COLS + 1); |
| 325 | Con_col = 0; |
| 326 | endline = false; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | SetCaretPos(Con_ch_w * Con_col, Con_ch_h * Con_row); |
| 331 | } |
| 332 | |
| 333 | void con_Scroll() { |
| 334 | int i; |
| 335 | |
| 336 | for (i = 1; i < CON_SCROLL_ROWS; i++) |
| 337 | memcpy(&Con_buffer[(i - 1) * (CON_SCROLL_COLS + 1)], &Con_buffer[i * (CON_SCROLL_COLS + 1)], CON_SCROLL_COLS + 1); |
| 338 | memset(&Con_buffer[(i - 1) * (CON_SCROLL_COLS + 1)], 0, CON_SCROLL_COLS + 1); |
| 339 | } |
| 340 | |
| 341 | int con_KeyDown(HWND hWnd, UINT vkey) { |
| 342 | // HDC hdc; |
| 343 | int x; |
| 344 | char *ptr = &Con_buffer[Con_row * (CON_SCROLL_COLS + 1)]; |
| 345 | |
| 346 | switch (vkey) { |
| 347 | case VK_LEFT: // Left arrow |
| 348 | Con_col = std::max(Con_col - 1, 0); |
| 349 | Con_inp_pos = std::max(Con_inp_pos - 1, 0); |
| 350 | break; |
| 351 | |
| 352 | case VK_RIGHT: // Right arrow |
| 353 | Con_col = std::min(Con_col + 1, CON_SCROLL_COLS - 1); |
| 354 | Con_inp_pos = std::min(Con_inp_pos + 1, CON_SCROLL_COLS - 1); |
| 355 | break; |
| 356 | |
| 357 | case VK_UP: // Up arrow |
| 358 | // Replace the current buffer that is being typed with the last completed command (if there was one) |
| 359 | if (Con_last_command[0] != 0) { |
| 360 | memset(Con_inp_buf, 0, sizeof(Con_inp_buf)); |
| 361 | |
| 362 | strcpy(Con_inp_buf, Con_last_command); |
| 363 | |
| 364 | ptr[0] = '%'; |
| 365 | strcpy(&ptr[1], Con_last_command); // add for % |
| 366 | |
| 367 | Con_inp_pos = strlen(Con_last_command); |
| 368 | Con_col = Con_inp_pos + 1; // add for % prompt |
| 369 | |
| 370 | // The application will draw outside the |
| 371 | // WM_PAINT message processing, so hide the caret. |
| 372 | HideCaret(hWnd); |
| 373 | |
| 374 | // Redraw the line, adjusted for the |
| 375 | // deleted character. |
| 376 | InvalidateRect(hWnd, NULL, TRUE); |
| 377 | UpdateWindow(hWnd); |
| 378 | |
| 379 | // Display the caret. |
| 380 | ShowCaret(hWnd); |
| 381 | } |
| 382 | break; |
| 383 | |
| 384 | case VK_DELETE: // Delete |
| 385 | // Move all the characters that followed the |
| 386 | // deleted character (on the same line) one |
| 387 | // space back (to the left) in the matrix. |
| 388 | for (x = Con_inp_pos; x < CON_SCROLL_COLS; x++) |
| 389 | Con_inp_buf[x] = Con_inp_buf[x + 1]; |
| 390 | |
| 391 | for (x = Con_col; x < CON_SCROLL_COLS; x++) |
| 392 | ptr[x] = ptr[x + 1]; |
| 393 | |
| 394 | // The application will draw outside the |
| 395 | // WM_PAINT message processing, so hide the caret. |
| 396 | HideCaret(hWnd); |
| 397 | |
| 398 | // Redraw the line, adjusted for the |
| 399 | // deleted character. |
| 400 | InvalidateRect(hWnd, NULL, TRUE); |
| 401 | UpdateWindow(hWnd); |
| 402 | |
| 403 | // Display the caret. |
| 404 | ShowCaret(hWnd); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | // Adjust the caret position based on the |
| 409 | // virtual-key processing. |
| 410 | SetCaretPos(Con_ch_w * Con_col, Con_ch_h * Con_row); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int con_Char(HWND hWnd, UINT vkey) { |
| 416 | HDC hdc; |
| 417 | char *ptr = &Con_buffer[Con_row * (CON_SCROLL_COLS + 1)]; |
| 418 | |
| 419 | switch (vkey) { |
| 420 | case 0x08: // Backspace |
| 421 | // Move the caret back one space, and then |
| 422 | // process this like the DEL key. |
| 423 | if (Con_inp_pos > 0) |
| 424 | Con_inp_pos--; |
| 425 | |
| 426 | if (Con_col > 0) { |
| 427 | Con_col--; |
| 428 | SendMessage(hWnd, WM_KEYDOWN, VK_DELETE, 1L); |
| 429 | } |
| 430 | break; |
| 431 | |
| 432 | case 0x0D: // Carriage return |
| 433 | { |
| 434 | // Go to the beginning of the next line. |
| 435 | // The bottom line wraps around to the top. |
| 436 | strcpy(Con_read_buf, Con_inp_buf); |
| 437 | Con_col = 0; |
| 438 | Con_row++; |
| 439 | if (Con_row == CON_SCROLL_ROWS) { |
| 440 | con_Scroll(); |
| 441 | Con_row--; |
| 442 | } |
| 443 | ptr = &Con_buffer[Con_row * (CON_SCROLL_COLS + 1)]; |
| 444 | memset(ptr, 0, CON_SCROLL_COLS + 1); |
| 445 | |
| 446 | // only save the buffer if there is text in the buffer |
| 447 | char *p = Con_inp_buf; |
| 448 | while (*p) { |
| 449 | if (isalnum(*p)) { |
| 450 | strcpy(Con_last_command, Con_inp_buf); |
| 451 | break; |
| 452 | } |
| 453 | p++; |
| 454 | } |
| 455 | |
| 456 | memset(Con_inp_buf, 0, sizeof(Con_inp_buf)); |
| 457 | Con_inp_pos = 0; |
| 458 | // con_Printf("Command is %s.\n", Con_read_buf); |
| 459 | Con_newline = true; |
| 460 | } break; |
| 461 | |
| 462 | case 0x1B: // Escape |
| 463 | { |
| 464 | memset(Con_inp_buf, 0, sizeof(Con_inp_buf)); |
| 465 | memset(ptr, 0, CON_SCROLL_COLS + 1); |
| 466 | |
| 467 | ptr[0] = '%'; |
| 468 | |
| 469 | Con_inp_pos = 0; |
| 470 | Con_col = 1; // for % sign |
| 471 | |
| 472 | // The application will draw outside the |
| 473 | // WM_PAINT message processing, so hide the caret. |
| 474 | HideCaret(hWnd); |
| 475 | |
| 476 | // Redraw the line, adjusted for the |
| 477 | // deleted character. |
| 478 | InvalidateRect(hWnd, NULL, TRUE); |
| 479 | UpdateWindow(hWnd); |
| 480 | |
| 481 | // Display the caret. |
| 482 | ShowCaret(hWnd); |
| 483 | |
| 484 | } break; |
| 485 | case 0x0A: // Linefeed |
| 486 | break; |
| 487 | |
| 488 | default: |
| 489 | if (Con_inp_pos < (CON_SCROLL_COLS - 2)) { |
| 490 | // Add the character to the text buffer. |
| 491 | int oldcol = Con_col, oldrow = Con_row; |
| 492 | char str[2]; |
| 493 | |
| 494 | str[0] = (char)vkey; |
| 495 | str[1] = 0; |
| 496 | |
| 497 | con_Puts(str); |
| 498 | Con_inp_buf[Con_inp_pos] = (char)vkey; |
| 499 | |
| 500 | // The application will draw outside the |
| 501 | // WM_PAINT message processing, so hide the caret. |
| 502 | HideCaret(hWnd); |
| 503 | |
| 504 | // Draw the character on the screen. |
| 505 | hdc = GetDC(hWnd); |
| 506 | SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT)); |
| 507 | TextOut(hdc, Con_ch_w * oldcol, Con_ch_h * oldrow, str, 1); |
| 508 | ReleaseDC(hWnd, hdc); |
| 509 | |
| 510 | // Display the caret. |
| 511 | ShowCaret(hWnd); |
| 512 | Con_inp_pos++; |
| 513 | } |
| 514 | |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | SetCaretPos(Con_col * Con_ch_w, Con_row * Con_ch_h); |
| 519 | |
| 520 | return 0; |
| 521 | } |