1 // On-screen display (ie. console)
2 // for the Build Engine
3 // by Jonathon Fowler (jf@jonof.id.au)
4 
5 #ifndef osd_h_
6 #define osd_h_
7 
8 #include "collections.h"
9 #include "mutex.h"
10 #include "vfs.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef struct {
17     int32_t numparms;
18     const char *name;
19     const char **parms;
20     const char *raw;
21 } osdfuncparm_t;
22 
23 using osdcmdptr_t = osdfuncparm_t const * const;
24 
25 const char *OSD_StripColors(char *outBuf, const char *inBuf);
26 
27 #define OSDDEFAULTMAXLINES  128
28 #define OSDEDITLENGTH       512
29 #define OSDMINHISTORYDEPTH  32
30 #define OSDMAXHISTORYDEPTH  256
31 #define OSDBUFFERSIZE       32768
32 #define OSDDEFAULTCOLS      60
33 #define OSDLOGCUTOFF        131072
34 #define OSDMAXSYMBOLS       512
35 
36 enum cvartype_t
37 {
38     CVAR_FLOAT         = 0x00000001,
39     CVAR_INT           = 0x00000002,
40     CVAR_UINT          = 0x00000004,
41     CVAR_BOOL          = 0x00000008,
42     CVAR_STRING        = 0x00000010,
43     CVAR_DOUBLE        = 0x00000020,
44     CVAR_READONLY      = 0x00000040,
45     CVAR_MULTI         = 0x00000080,
46     CVAR_NOSAVE        = 0x00000100,
47     CVAR_FUNCPTR       = 0x00000200,
48     CVAR_RESTARTVID    = 0x00000400,
49     CVAR_INVALIDATEALL = 0x00000800,
50     CVAR_INVALIDATEART = 0x00001000,
51     CVAR_MODIFIED      = 0x00002000,
52 
53     CVAR_TYPEMASK = CVAR_FLOAT | CVAR_DOUBLE | CVAR_INT | CVAR_UINT | CVAR_BOOL | CVAR_STRING
54 };
55 
56 typedef struct _symbol
57 {
58     const char *name;
59     struct _symbol *next;
60 
61     const char *help;
62     int32_t(*func)(osdcmdptr_t);
63 } osdsymbol_t;
64 
65 typedef struct
66 {
67     const char *name;
68     const char *desc;
69 
70     union
71     {
72         void *      ptr;
73         int32_t *   i32;
74         uint32_t *  u32;
75         float *     f;
76         double *    d;
77         char *const string;
78     };
79 
80     uint32_t flags;   // see cvartype_t
81     int32_t const min;
82     int32_t const max;    // for string, is the length
83 } osdcvardata_t;
84 
85 typedef struct
86 {
87     osdcvardata_t *pData;
88 
89     // default value for cvar, assigned when var is registered
90     union
91     {
92         int32_t  i32;
93         uint32_t u32;
94         float    f;
95         double   d;
96     } defaultValue;
97 
98 } osdcvar_t;
99 
100 // version string
101 typedef struct
102 {
103     char *buf;
104 
105     uint8_t len;
106     uint8_t shade;
107     uint8_t pal;
108 } osdstr_t;
109 
110 // command prompt editing
111 typedef struct
112 {
113     char *buf;  // [OSDEDITLENGTH+1]; // editing buffer
114     char *tmp;  // [OSDEDITLENGTH+1]; // editing buffer temporary workspace
115 
116     int16_t len, pos;  // length of characters and position of cursor in buffer
117     int16_t start, end;
118 } osdedit_t;
119 
120 // main text buffer
121 typedef struct
122 {
123     // each character in the buffer also has a format byte containing shade and color
124     char *buf;
125     char *fmt;
126 
127     int32_t pos;       // position next character will be written at
128     int32_t lines;     // total number of lines in buffer
129     int32_t maxlines;  // max lines in buffer
130 } osdtext_t;
131 
132 // history display
133 typedef struct
134 {
135     char *buf[OSDMAXHISTORYDEPTH];
136 
137     int32_t maxlines;  // max entries in buffer, ranges from OSDMINHISTORYDEPTH to OSDMAXHISTORYDEPTH
138     int32_t pos;       // current buffer position
139     int32_t lines;     // entries currently in buffer
140     int32_t total;     // total number of entries
141     int32_t exec;      // number of lines from the head of the history buffer to execute
142 } osdhist_t;
143 
144 // active display parameters
145 typedef struct
146 {
147     char const *errorfmt;
148     char const *highlight;
149 
150     int32_t  promptshade, promptpal;
151     int32_t  editshade, editpal;
152     int32_t  textshade, textpal;
153     int32_t  mode;
154     int32_t  rows;  // # lines of the buffer that are visible
155     int32_t  cols;  // width of onscreen display in text columns
156     uint16_t head;  // topmost visible line number
157     int8_t   scrolling;
158     int      errfmtlen;
159 } osddraw_t;
160 
161 typedef struct
162 {
163     buildvfs_FILE fp;
164     int32_t cutoff;
165     int32_t lines;
166 } osdlog_t;
167 
168 typedef struct
169 {
170     osdtext_t text;
171     osdedit_t editor;
172     osdhist_t history;
173     osddraw_t draw;
174     osdstr_t  version;
175 
176     uint32_t   flags;  // controls initialization, etc
177     osdcvar_t *cvars;
178     int32_t    numcvars;
179 
180     osdsymbol_t *symbols;
181     osdsymbol_t *symbptrs[OSDMAXSYMBOLS];
182 
183     int32_t numsymbols;
184     int32_t execdepth;  // keeps track of nested execution
185     mutex_t mutex;
186     int32_t keycode;
187 
188     osdlog_t log;
189 } osdmain_t;
190 
191 extern osdmain_t *osd;
192 extern GrowArray<char *> osdstrings;
193 
194 extern buildvfs_FILE osdlog;
195 extern const char* osdlogfn;
196 
197 enum osdflags_t
198 {
199 //    OSD_INITIALIZED = 0x00000001,
200     OSD_DRAW        = 0x00000002,
201     OSD_CAPTURE     = 0x00000004,
202     OSD_OVERTYPE    = 0x00000008,
203     OSD_SHIFT       = 0x00000010,
204     OSD_CTRL        = 0x00000020,
205     OSD_CAPS        = 0x00000040,
206     OSD_PROTECTED   = 0x00000080,
207 };
208 
209 #define OSD_ALIAS     (int (*)(osdcmdptr_t))0x1337
210 #define OSD_UNALIASED (int (*)(osdcmdptr_t))0xDEAD
211 
212 #define OSDCMD_OK	0
213 #define OSDCMD_SHOWHELP 1
214 
215 int OSD_ParsingScript(void);
216 
217 int OSD_OSDKey(void);
218 int OSD_GetTextMode(void);
219 void OSD_SetTextMode(int mode);
220 
221 int OSD_Exec(const char *szScript);
222 
223 // Get shade and pal index from the OSD format buffer.
224 void OSD_GetShadePal(const char *ch, int *shd, int *pal);
225 
226 int OSD_GetCols(void);
227 int OSD_IsMoving(void);
228 int OSD_GetRowsCur(void);
229 
230 // initializes things
231 void OSD_Init(void);
232 
233 // cleans things up. these comments are retarded.
234 void OSD_Cleanup(void);
235 
236 // sets the file to echo output to
237 void OSD_SetLogFile(const char *fn);
238 
239 // sets the functions the OSD will call to interrogate the environment
240 void OSD_SetFunctions(void (*drawchar)(int, int, char, int, int),
241                       void (*drawstr)(int, int, const char *, int, int, int),
242                       void (*drawcursor)(int, int, int, int),
243                       int (*colwidth)(int),
244                       int (*rowheight)(int),
245                       void (*clearbg)(int, int),
246                       int32_t (*gtime)(void),
247                       void (*showosd)(int));
248 
249 // sets the parameters for presenting the text
250 void OSD_SetParameters(int promptShade, int promptPal, int editShade, int editPal, int textShade, int textPal,
251                        char const *errorStr, char const *highlight, uint32_t flags);
252 
253 // sets the scancode for the key which activates the onscreen display
254 void OSD_CaptureKey(uint8_t scanCode);
255 
256 // handles keyboard input when capturing input. returns 0 if key was handled
257 // or the scancode if it should be handled by the game.
258 int OSD_HandleScanCode(uint8_t scanCode, int keyDown);
259 int OSD_HandleChar(char ch);
260 void OSD_HandleWheel(void);
261 // handles the readjustment when screen resolution changes
262 void OSD_ResizeDisplay(int w,int h);
263 
264 // captures and frees osd input
265 void OSD_CaptureInput(int cap);
266 
267 // sets the console version string
268 void OSD_SetVersion(const char *pszVersion, int osdShade, int osdPal);
269 
270 // shows or hides the onscreen display
271 void OSD_ShowDisplay(int onf);
272 
273 // draw the osd to the screen
274 void OSD_Draw(void);
275 
276 // just like printf
277 int OSD_Printf(const char *fmt, ...) ATTRIBUTE((format(printf,1,2)));
278 
279 // just like puts
280 void OSD_Puts(const char *putstr, int const nolog = false);
281 
282 // executes buffered commands
283 void OSD_DispatchQueued(void);
284 
285 // executes a string
286 void OSD_Dispatch(const char *cmd);
287 
288 // registers a function
289 //   name = name of the function
290 //   help = a short help string
291 //   func = the entry point to the function
292 int OSD_RegisterFunction(const char *pszName, const char *pszDesc, int (*func)(osdcmdptr_t));
293 
294 int osdcmd_cvar_set(osdcmdptr_t parm);
295 void OSD_RegisterCvar(osdcvardata_t * cvar, int (*func)(osdcmdptr_t));
296 void OSD_WriteAliases(buildvfs_FILE fp);
297 void OSD_WriteCvars(buildvfs_FILE fp);
298 
OSD_SetHistory(int idx,const char * src)299 static inline void OSD_SetHistory(int idx, const char *src)
300 {
301     osd->history.buf[idx] = (char *)Xmalloc(OSDEDITLENGTH);
302     Bstrncpyz(osd->history.buf[idx], src, OSDEDITLENGTH);
303 }
304 
305 extern int osdcmd_restartvid(osdcmdptr_t parm);
306 
307 extern void M32RunScript(const char *s);
308 
309 #ifdef __cplusplus
310 }
311 #endif
312 
313 #endif // osd_h_
314 
315