| 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.cpp $ |
| 21 | * $Revision: 1.2 $ |
| 22 | * $Date: 2004/02/25 00:04:06 $ |
| 23 | * $Author: ryan $ |
| 24 | * |
| 25 | * <insert description of file here> |
| 26 | * |
| 27 | * $Log: lnxcon.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 | * 7 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 | * 6 6/24/99 4:43p Jeff |
| 40 | * removed ncurses dependency |
| 41 | * |
| 42 | * 5 4/22/99 2:04a Jeff |
| 43 | * support for null console |
| 44 | * |
| 45 | * 4 4/19/99 4:52p Jeff |
| 46 | * removed useless calls to curses lib for keyboard setup |
| 47 | * |
| 48 | * 3 4/19/99 3:58a Jeff |
| 49 | * got console working again (keyboard based off ddio) |
| 50 | * |
| 51 | * 2 4/14/99 1:59a Jeff |
| 52 | * fixed case mismatched #includes |
| 53 | * |
| 54 | * 1 1/12/99 3:42a Jeff |
| 55 | * |
| 56 | * $NoKeywords: $ |
| 57 | */ |
| 58 | |
| 59 | #include <cstdarg> |
| 60 | #include <cstdio> |
| 61 | #include <cctype> |
| 62 | |
| 63 | #include "application.h"cxx . . $ `<file>/lib/application.h`/ |
| 64 | #include "AppConsole.h"cxx . . $ `<file>/lib/AppConsole.h`/ |
| 65 | |
| 66 | enumcxx . . $ $anonymous_type_8fb88b4b132fd3f8_0# { |
| 67 | Console_nullcxx . . $ Console_null., |
| 68 | Console_rawcxx . . $ Console_raw., |
| 69 | Console_totalcxx . . $ Console_total., |
| 70 | } Console_modecxx . . $ Console_mode.; |
| 71 | |
| 72 | typedef bool (*Input_fpcxx . . $ Input_fp#)(char *buf, int buflen); |
| 73 | typedef void (*Defer_fpcxx . . $ Defer_fp#)(); |
| 74 | typedef bool (*Create_fpcxx . . $ Create_fp#)(); |
| 75 | typedef void (*Destroy_fpcxx . . $ Destroy_fp#)(); |
| 76 | typedef void (*Puts_fpcxx . . $ Puts_fp#)(int window, const char *str); |
| 77 | |
| 78 | struct Console_Commandscxx . . $ Console_Commands# { |
| 79 | Create_fpcxx . . $ Create_fp# con_Createcxx . . $ Console_Commands#con_Create.; |
| 80 | Destroy_fpcxx . . $ Destroy_fp# con_Destroycxx . . $ Console_Commands#con_Destroy.; |
| 81 | Defer_fpcxx . . $ Defer_fp# con_Defercxx . . $ Console_Commands#con_Defer.; |
| 82 | Input_fpcxx . . $ Input_fp# con_Inputcxx . . $ Console_Commands#con_Input.; |
| 83 | Puts_fpcxx . . $ Puts_fp# con_Putscxx . . $ Console_Commands#con_Puts.; |
| 84 | }; |
| 85 | |
| 86 | //////////////////////////////////////////////// |
| 87 | // NULL driver functions |
| 88 | bool con_null_Inputcxx . . $ con_null_Input(37e8d18a243bba77).(char *buf, int buflen); |
| 89 | void con_null_Defercxx . . $ con_null_Defer(49f6e7a06ebc5aa8).(); |
| 90 | bool con_null_Createcxx . . $ con_null_Create(c9857a73d68e59ba).(); |
| 91 | void con_null_Destroycxx . . $ con_null_Destroy(49f6e7a06ebc5aa8).(); |
| 92 | void con_null_Putscxx . . $ con_null_Puts(66835f578de1bcb3).(int window, const char *str); |
| 93 | |
| 94 | //////////////////////////////////////////////// |
| 95 | // raw driver functions |
| 96 | bool con_raw_Inputcxx . . $ con_raw_Input(37e8d18a243bba77).(char *buf, int buflen); |
| 97 | void con_raw_Defercxx . . $ con_raw_Defer(49f6e7a06ebc5aa8).(); |
| 98 | bool con_raw_Createcxx . . $ con_raw_Create(c9857a73d68e59ba).(); |
| 99 | void con_raw_Destroycxx . . $ con_raw_Destroy(49f6e7a06ebc5aa8).(); |
| 100 | void con_raw_Putscxx . . $ con_raw_Puts(66835f578de1bcb3).(int window, const char *str); |
| 101 | |
| 102 | Console_Commandscxx . . $ Console_Commands# commandscxx . . $ commands.[Console_totalcxx . . $ Console_total.] = { |
| 103 | { |
| 104 | con_null_Createcxx . . $ con_null_Create(c9857a73d68e59ba)., |
| 105 | con_null_Destroycxx . . $ con_null_Destroy(49f6e7a06ebc5aa8)., |
| 106 | con_null_Defercxx . . $ con_null_Defer(49f6e7a06ebc5aa8)., |
| 107 | con_null_Inputcxx . . $ con_null_Input(37e8d18a243bba77)., |
| 108 | con_null_Putscxx . . $ con_null_Puts(66835f578de1bcb3)., |
| 109 | }, |
| 110 | { |
| 111 | con_raw_Createcxx . . $ con_raw_Create(c9857a73d68e59ba)., |
| 112 | con_raw_Destroycxx . . $ con_raw_Destroy(49f6e7a06ebc5aa8)., |
| 113 | con_raw_Defercxx . . $ con_raw_Defer(49f6e7a06ebc5aa8)., |
| 114 | con_raw_Inputcxx . . $ con_raw_Input(37e8d18a243bba77)., |
| 115 | con_raw_Putscxx . . $ con_raw_Puts(66835f578de1bcb3)., |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | ////////////////////////////////////////////////// |
| 120 | // Global Variables |
| 121 | bool Con_initcxx . . $ Con_init. = false; // Console has been initialized |
| 122 | |
| 123 | #ifdef mem_malloc |
| 124 | #undef mem_malloc |
| 125 | #endif |
| 126 | #ifdef mem_free |
| 127 | #undef mem_free |
| 128 | #endif |
| 129 | #define mem_malloccxx . . $ `linux/lnxcon.cpp:129:9`!(x) malloc(x) |
| 130 | #define mem_freecxx . . $ `linux/lnxcon.cpp:130:9`!(x) free(x) |
| 131 | |
| 132 | ////////////////////////////////////////////////// |
| 133 | // Functions |
| 134 | |
| 135 | void con_Printfcxx . . $ con_Printf(326aeb464d16410c).(const char *fmt, ...) { |
| 136 | if (!Con_initcxx . . $ Con_init.) // console hasn't been initialized yet, int16_t circuit |
| 137 | return; |
| 138 | |
| 139 | char buf[CON_MAX_STRINGLENcxx . . $ `lib/AppConsole.h:44:9`!]; |
| 140 | va_listcxx . . $ va_list# args; |
| 141 | |
| 142 | // filter out messages |
| 143 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, fmt); |
| 144 | stdcxx . . $ std/::vsnprintfcxx . . $ std/vsnprintf(348eb892742d8727).(buf, sizeof(buf), fmt, args); |
| 145 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 146 | |
| 147 | // filter out unprintable characters |
| 148 | char *p, *fp, filter_buf[CON_MAX_STRINGLENcxx . . $ `lib/AppConsole.h:44:9`!]; |
| 149 | p = buf; |
| 150 | fp = filter_buf; |
| 151 | |
| 152 | while (*p) { |
| 153 | if (*p == 0x01) { |
| 154 | // this is a color, skip the next 3 |
| 155 | p += 4; |
| 156 | } else { |
| 157 | if (isalnum(*p) || ispunct(*p) || (*p == ' ') || (*p == '\n') || (*p == '\r') || (*p == '\b')) { |
| 158 | *fp = *p; |
| 159 | fp++; |
| 160 | } |
| 161 | p++; |
| 162 | } |
| 163 | } |
| 164 | *fp = '\0'; |
| 165 | |
| 166 | commandscxx . . $ commands.[Console_modecxx . . $ Console_mode.].con_Putscxx . . $ Console_Commands#con_Puts.(0, filter_buf); |
| 167 | } |
| 168 | |
| 169 | bool con_Inputcxx . . $ con_Input(37e8d18a243bba77).(char *buf, int buflen) { |
| 170 | if (!Con_initcxx . . $ Con_init.) { // the console hasn't been initialized yet |
| 171 | *buf = '\0'; |
| 172 | return false; |
| 173 | } |
| 174 | return commandscxx . . $ commands.[Console_modecxx . . $ Console_mode.].con_Inputcxx . . $ Console_Commands#con_Input.(buf, buflen); |
| 175 | } |
| 176 | |
| 177 | void con_Defercxx . . $ con_Defer(49f6e7a06ebc5aa8).() { |
| 178 | if (!Con_initcxx . . $ Con_init.) { |
| 179 | // the console hasn't been initialized yet |
| 180 | return; |
| 181 | } |
| 182 | commandscxx . . $ commands.[Console_modecxx . . $ Console_mode.].con_Defercxx . . $ Console_Commands#con_Defer.(); |
| 183 | } |
| 184 | |
| 185 | bool con_Createcxx . . $ con_Create(83c267b20bac841a).(int flags) { |
| 186 | // determine what type of console to open up |
| 187 | if (flags & APPFLAG_USESERVICEcxx . . $ `linux/lnxapp.h:27:9`!) { |
| 188 | // use the NULL driver! |
| 189 | Console_modecxx . . $ Console_mode. = Console_nullcxx . . $ Console_null.; |
| 190 | } else { |
| 191 | // use stdout driver |
| 192 | Console_modecxx . . $ Console_mode. = Console_rawcxx . . $ Console_raw.; |
| 193 | } |
| 194 | Con_initcxx . . $ Con_init. = commandscxx . . $ commands.[Console_modecxx . . $ Console_mode.].con_Createcxx . . $ Console_Commands#con_Create.(); |
| 195 | return Con_initcxx . . $ Con_init.; |
| 196 | } |
| 197 | |
| 198 | void con_Destroycxx . . $ con_Destroy(49f6e7a06ebc5aa8).() { commandscxx . . $ commands.[Console_modecxx . . $ Console_mode.].con_Destroycxx . . $ Console_Commands#con_Destroy.(); } |