| 1 |
/* |
| 2 |
* osbeos.c -- copyright (c) 2000 by Christopher Tate, all rights reserved. |
| 3 |
* |
| 4 |
* Please see the accompanying license file, LICENSE.TXT, for information |
| 5 |
* on using and copying this software. |
| 6 |
*/ |
| 7 |
|
| 8 |
// Just the parts of osbeos.cpp that are superceded by HTML Tads |
| 9 |
|
| 10 |
// appctx.h needs size_t to be defined |
| 11 |
#include <size_t.h> |
| 12 |
#include "appctx.h" |
| 13 |
#include "os.h" |
| 14 |
|
| 15 |
extern "C" { |
| 16 |
|
| 17 |
void set_cbreak_mode (int mode); |
| 18 |
|
| 19 |
/* write a string to the main console */ |
| 20 |
void os_printz(const char *str) |
| 21 |
{ |
| 22 |
fputs(str, stdout); |
| 23 |
} |
| 24 |
|
| 25 |
/* write a non-null-terminated string to the console */ |
| 26 |
void os_print(const char *str, size_t len) |
| 27 |
{ |
| 28 |
if (os_get_status() == 0) // discard non-main-window |
| 29 |
{ |
| 30 |
printf("%.*s", (int)len, str); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/* display a string in the score area in the status line. These stubs are only for non-HTML tads */ |
| 35 |
void os_strsc(const char* /*p*/) |
| 36 |
{ |
| 37 |
// no status line; ignore |
| 38 |
} |
| 39 |
|
| 40 |
/* set the status line mode */ |
| 41 |
// global state: are we updating the status line, or the main text of the game log? |
| 42 |
static int gStatusMode = 0; |
| 43 |
|
| 44 |
void os_status(int stat) |
| 45 |
{ |
| 46 |
gStatusMode = stat; |
| 47 |
} |
| 48 |
|
| 49 |
/* get the status line mode */ |
| 50 |
int os_get_status() |
| 51 |
{ |
| 52 |
return gStatusMode; |
| 53 |
} |
| 54 |
|
| 55 |
/* set the score value */ |
| 56 |
void os_score(int /*cur*/, int /*turncount*/) |
| 57 |
{ |
| 58 |
// no status line; ignore |
| 59 |
} |
| 60 |
|
| 61 |
/* clear the screen */ |
| 62 |
void oscls(void) |
| 63 |
{ |
| 64 |
// !!! don't do anything for now |
| 65 |
} |
| 66 |
|
| 67 |
/* flush any buffered display output */ |
| 68 |
void os_flush(void) |
| 69 |
{ |
| 70 |
fflush(stdout); |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* Get highlighting character code. This returns a character code to |
| 75 |
* write to the display in order to activate a highlighting mode: mode 1 |
| 76 |
* = turn on boldface, mode 2 = turn off boldface. A return value of |
| 77 |
* zero indicates that highlighting is not supported; otherwise, the |
| 78 |
* return value is the character to write to the display to activate the |
| 79 |
* given mode. |
| 80 |
*/ |
| 81 |
int os_hilite(int /*mode*/) |
| 82 |
{ |
| 83 |
return 0; // !!! no hilighting for now |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* os_plain() - Use plain ascii mode for the display. If possible and |
| 88 |
* necessary, turn off any text formatting effects, such as cursor |
| 89 |
* positioning, highlighting, or coloring. If this routine is called, |
| 90 |
* the terminal should be treated as a simple text stream; users might |
| 91 |
* wish to use this mode for things like text-to-speech converters. |
| 92 |
* |
| 93 |
* Purely graphical implementations that cannot offer a textual mode |
| 94 |
* (such as Mac OS or Windows) can ignore this setting. |
| 95 |
* |
| 96 |
* If this routine is to be called, it must be called BEFORE os_init(). |
| 97 |
* The implementation should set a flag so that os_init() will know to |
| 98 |
* set up the terminal for plain text output. |
| 99 |
*/ |
| 100 |
#ifndef os_plain |
| 101 |
/* |
| 102 |
* some platforms (e.g. Mac OS) define this to be a null macro, so don't |
| 103 |
* define a prototype in those cases |
| 104 |
*/ |
| 105 |
void os_plain(void) |
| 106 |
{ |
| 107 |
} |
| 108 |
#endif // #ifndef os_plain |
| 109 |
|
| 110 |
/* |
| 111 |
* Set the game title. The output layer calls this routine when a game |
| 112 |
* sets its title (via an HTML <title> tag, for example). If it's |
| 113 |
* convenient to do so, the OS layer can use this string to set a window |
| 114 |
* caption, or whatever else makes sense on each system. Most |
| 115 |
* character-mode implementations will provide an empty implementation, |
| 116 |
* since there's not usually any standard way to show the current |
| 117 |
* application title on a character-mode display. |
| 118 |
*/ |
| 119 |
void os_set_title(const char */*title*/) |
| 120 |
{ |
| 121 |
// don't bother on BeOS in a Terminal |
| 122 |
} |
| 123 |
|
| 124 |
/* These terminal-based input functions are only for non-HTML tads */ |
| 125 |
unsigned char *os_gets(unsigned char *buf, size_t bufl) |
| 126 |
{ |
| 127 |
fgets((char*) buf, bufl, stdin); |
| 128 |
|
| 129 |
// postprocess the string to handle editing characters, etc. |
| 130 |
char* newbuf = (char*) malloc(bufl); |
| 131 |
memcpy(newbuf, buf, bufl); |
| 132 |
|
| 133 |
char* p = (char*) buf; |
| 134 |
char* q = newbuf; |
| 135 |
while (*p) |
| 136 |
{ |
| 137 |
switch (*p) |
| 138 |
{ |
| 139 |
case 0x08: // backspace |
| 140 |
if (q > newbuf) q--; |
| 141 |
break; |
| 142 |
|
| 143 |
case 0x15: // control-U == kill entire line |
| 144 |
q = newbuf; |
| 145 |
break; |
| 146 |
|
| 147 |
case '\r': // end of line |
| 148 |
case '\n': |
| 149 |
*p = *q = '\0'; |
| 150 |
break; |
| 151 |
|
| 152 |
default: |
| 153 |
*q++ = *p++; |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
memcpy(buf, newbuf, bufl); |
| 159 |
free(newbuf); |
| 160 |
return buf; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Read a character from the keyboard. For extended keystrokes, this |
| 165 |
* function returns zero, and then returns the CMD_xxx code for the |
| 166 |
* extended keystroke on the next call. For example, if the user |
| 167 |
* presses the up-arrow key, the first call to os_getc() should return |
| 168 |
* 0, and the next call should return CMD_UP. Refer to the CMD_xxx |
| 169 |
* codes below. |
| 170 |
* |
| 171 |
* os_getc() should return a high-level, translated command code for |
| 172 |
* command editing. This means that, where a functional interpretation |
| 173 |
* of a key and the raw key-cap interpretation both exist as CMD_xxx |
| 174 |
* codes, the functional interpretation should be returned. For |
| 175 |
* example, on Unix, Ctrl-E is conventionally used in command editing to |
| 176 |
* move to the end of the line, following Emacs key bindings. Hence, |
| 177 |
* os_getc() should return CMD_END for this keystroke, rather than |
| 178 |
* (CMD_CTRL + 'E' - 'A'), because CMD_END is the high-level command |
| 179 |
* code for the operation. |
| 180 |
* |
| 181 |
* The translation ability of this function allows for system-dependent |
| 182 |
* key mappings to functional meanings. |
| 183 |
*/ |
| 184 |
int os_getc(void) |
| 185 |
{ |
| 186 |
return os_getc_raw(); |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
* Read a character from the keyboard, following the same protocol as |
| 191 |
* os_getc() for CMD_xxx codes (i.e., when an extended keystroke is |
| 192 |
* encountered, os_getc_raw() returns zero, then returns the CMD_xxx |
| 193 |
* code on the subsequent call). |
| 194 |
* |
| 195 |
* This function differs from os_getc() in that this function returns |
| 196 |
* the low-level, untranslated key code. This means that, when a |
| 197 |
* functional interpretation of a key and the raw key-cap interpretation |
| 198 |
* both exist as CMD_xxx codes, this function returns the key-cap |
| 199 |
* interpretation. For the Unix Ctrl-E example in the comments |
| 200 |
* describing os_getc() above, this function should return 5 (the ASCII |
| 201 |
* code for Ctrl-E), because the CMD_CTRL interpretation is the |
| 202 |
* low-level key code. |
| 203 |
* |
| 204 |
* This function should return all control keys using their ASCII |
| 205 |
* control codes, whenever possible. Similarly, this function should |
| 206 |
* return ASCII 27 for the Escape key, if possible. |
| 207 |
*/ |
| 208 |
int os_getc_raw(void) |
| 209 |
{ |
| 210 |
set_cbreak_mode(1); |
| 211 |
int c = getchar(); |
| 212 |
set_cbreak_mode(0); |
| 213 |
return c; |
| 214 |
} |
| 215 |
|
| 216 |
/* wait for a character to become available from the keyboard */ |
| 217 |
void os_waitc(void) |
| 218 |
{ |
| 219 |
(void) os_getc(); |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* Get an input event. The event types are shown above. If use_timeout |
| 224 |
* is false, this routine should simply wait until one of the events it |
| 225 |
* recognizes occurs, then return the appropriate information on the |
| 226 |
* event. If use_timeout is true, this routine should return |
| 227 |
* OS_EVT_TIMEOUT after the given number of milliseconds elapses if no |
| 228 |
* event occurs first. |
| 229 |
* |
| 230 |
* This function is not obligated to obey the timeout. If a timeout is |
| 231 |
* specified and it is not possible to obey the timeout, the function |
| 232 |
* should simply return OS_EVT_NOTIMEOUT. The trivial implementation |
| 233 |
* thus checks for a timeout, returns an error if specified, and |
| 234 |
* otherwise simply waits for the user to press a key. |
| 235 |
*/ |
| 236 |
int os_get_event(unsigned long /*timeout_in_milliseconds*/, int use_timeout, os_event_info_t *info) |
| 237 |
{ |
| 238 |
/* if there's a timeout, return an error indicating we don't allow it */ |
| 239 |
if (use_timeout) |
| 240 |
return OS_EVT_NOTIMEOUT; |
| 241 |
|
| 242 |
/* get a key the normal way */ |
| 243 |
info->key[0] = os_getc(); |
| 244 |
|
| 245 |
/* if it's an extended key, get the other key */ |
| 246 |
if (info->key[0] == 0) |
| 247 |
info->key[1] = os_getc(); |
| 248 |
|
| 249 |
/* return the keyboard event */ |
| 250 |
return OS_EVT_KEY; |
| 251 |
} |
| 252 |
|
| 253 |
/* pause prior to exit */ |
| 254 |
void os_expause(void) |
| 255 |
{ |
| 256 |
os_printz("(Strike any key to exit...)"); |
| 257 |
os_flush(); |
| 258 |
os_waitc(); |
| 259 |
os_printz("\n"); |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* Check for user break (control-C, etc) - returns true if a break is |
| 264 |
* pending, false if not. If this returns true, it should "consume" the |
| 265 |
* pending break (probably by simply clearing the OS code's internal |
| 266 |
* break-pending flag). |
| 267 |
*/ |
| 268 |
int os_break(void) |
| 269 |
{ |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
/* ------------------------------------------------------------------------*/ |
| 274 |
/* |
| 275 |
* Translate a character from the HTML 4 Unicode character set to the |
| 276 |
* current character set used for display. Takes an HTML 4 character |
| 277 |
* code and returns the appropriate local character code. |
| 278 |
* |
| 279 |
* The result buffer should be filled in with a null-terminated string |
| 280 |
* that should be used to represent the character. Multi-character |
| 281 |
* results are possible, which may be useful for certain approximations |
| 282 |
* (such as using "(c)" for the copyright symbol). |
| 283 |
* |
| 284 |
* Note that we only define this prototype if this symbol isn't already |
| 285 |
* defined as a macro, which may be the case on some platforms. |
| 286 |
* Alternatively, if the function is already defined (for example, as an |
| 287 |
* inline function), the defining code can define OS_XLAT_HTML4_DEFINED, |
| 288 |
* in which case we'll also omit this prototype. |
| 289 |
* |
| 290 |
* Important: this routine provides the *default* mapping that is used |
| 291 |
* when no external character mapping file is present, and for any named |
| 292 |
* entities not defined in the mapping file. Any entities in the |
| 293 |
* mapping file, if used, will override this routine. |
| 294 |
* |
| 295 |
* A trivial implementation of this routine (that simply returns a |
| 296 |
* one-character result consisting of the original input character, |
| 297 |
* truncated to eight bits if necessary) can be used if you want to |
| 298 |
* require an external mapping file to be used for any game that |
| 299 |
* includes HTML character entities. The DOS version implements this |
| 300 |
* routine so that games will still look reasonable when played with no |
| 301 |
* mapping file present, but other systems are not required to do this. |
| 302 |
*/ |
| 303 |
#ifndef os_xlat_html4 |
| 304 |
# ifndef OS_XLAT_HTML4_DEFINED |
| 305 |
void os_xlat_html4(unsigned int html4_char, |
| 306 |
char *result, size_t result_buf_len) |
| 307 |
{ |
| 308 |
// This routine actually translates a Unicode-16 character to an appropriate |
| 309 |
// implementation-defined string. On BeOS, this just means translating it to |
| 310 |
// UTF-8, which is supported by the sprintf() routine's "wide char" formatting. |
| 311 |
snprintf(result, result_buf_len, "%lc", html4_char); |
| 312 |
} |
| 313 |
# endif |
| 314 |
#endif |
| 315 |
|
| 316 |
} // extern "C" |