Debian Codegraph @

linux/lnxcon.cpp

raw symbols SCIP
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"
64 #include "AppConsole.h"
65
66 enum {
67 Console_null,
68 Console_raw,
69 Console_total,
70 } Console_mode;
71
72 typedef bool (*Input_fp)(char *buf, int buflen);
73 typedef void (*Defer_fp)();
74 typedef bool (*Create_fp)();
75 typedef void (*Destroy_fp)();
76 typedef void (*Puts_fp)(int window, const char *str);
77
78 struct Console_Commands {
79 Create_fp con_Create;
80 Destroy_fp con_Destroy;
81 Defer_fp con_Defer;
82 Input_fp con_Input;
83 Puts_fp con_Puts;
84 };
85
86 ////////////////////////////////////////////////
87 // NULL driver functions
88 bool con_null_Input(char *buf, int buflen);
89 void con_null_Defer();
90 bool con_null_Create();
91 void con_null_Destroy();
92 void con_null_Puts(int window, const char *str);
93
94 ////////////////////////////////////////////////
95 // raw driver functions
96 bool con_raw_Input(char *buf, int buflen);
97 void con_raw_Defer();
98 bool con_raw_Create();
99 void con_raw_Destroy();
100 void con_raw_Puts(int window, const char *str);
101
102 Console_Commands commands[Console_total] = {
103 {
104 con_null_Create,
105 con_null_Destroy,
106 con_null_Defer,
107 con_null_Input,
108 con_null_Puts,
109 },
110 {
111 con_raw_Create,
112 con_raw_Destroy,
113 con_raw_Defer,
114 con_raw_Input,
115 con_raw_Puts,
116 },
117 };
118
119 //////////////////////////////////////////////////
120 // Global Variables
121 bool 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_malloc(x) malloc(x)
130 #define mem_free(x) free(x)
131
132 //////////////////////////////////////////////////
133 // Functions
134
135 void con_Printf(const char *fmt, ...) {
136 if (!Con_init) // console hasn't been initialized yet, int16_t circuit
137 return;
138
139 char buf[CON_MAX_STRINGLEN];
140 va_list args;
141
142 // filter out messages
143 va_start(args, fmt);
144 std::vsnprintf(buf, sizeof(buf), fmt, args);
145 va_end(args);
146
147 // filter out unprintable characters
148 char *p, *fp, filter_buf[CON_MAX_STRINGLEN];
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 commands[Console_mode].con_Puts(0, filter_buf);
167 }
168
169 bool con_Input(char *buf, int buflen) {
170 if (!Con_init) { // the console hasn't been initialized yet
171 *buf = '\0';
172 return false;
173 }
174 return commands[Console_mode].con_Input(buf, buflen);
175 }
176
177 void con_Defer() {
178 if (!Con_init) {
179 // the console hasn't been initialized yet
180 return;
181 }
182 commands[Console_mode].con_Defer();
183 }
184
185 bool con_Create(int flags) {
186 // determine what type of console to open up
187 if (flags & APPFLAG_USESERVICE) {
188 // use the NULL driver!
189 Console_mode = Console_null;
190 } else {
191 // use stdout driver
192 Console_mode = Console_raw;
193 }
194 Con_init = commands[Console_mode].con_Create();
195 return Con_init;
196 }
197
198 void con_Destroy() { commands[Console_mode].con_Destroy(); }