r7061: initial property settings
[ctsim.git] / libctsupport / consoleio.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (c) 1983-2001 Kevin Rosenberg
4 **
5 **  $Id$
6 **
7 **  This program is free software; you can redistribute it and/or modify
8 **  it under the terms of the GNU General Public License (version 2) as
9 **  published by the Free Software Foundation.
10 **
11 **  This program is distributed in the hope that it will be useful,
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 **  GNU General Public License for more details.
15 **
16 **  You should have received a copy of the GNU General Public License
17 **  along with this program; if not, write to the Free Software
18 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ******************************************************************************/
20
21 #include <stdio.h>
22 #include <string.h>
23 #include "ctsupport.h"
24
25
26 /* NAME
27  *   cio_put_c                          Put a character on screen
28  *
29  * SYNOPSIS
30  *   cio_put_c (c)
31  *   char c                             Character to write
32  *
33  * NOTES
34  *   Color of character is determined by the global variable, crtv.text_attr.
35  *
36  * SIDE EFFECTS
37  *   Cursor is advanced by one.  If necessary, the cursor will wrap around
38  *   and maybe the screen will scroll
39  */
40
41 void 
42 cio_put_c (int c)
43 {
44     fputc(c, stdout);
45 }
46
47
48
49 /* NAME
50  *    cio_put_cc                        Put a char on screen count times
51  *
52  * SYNOPSIS
53  *    cio_put_cc (c, count)
54  *    char c                            Character to write
55  *    int count                         Number of characters to write
56  */
57
58 void 
59 cio_put_cc (int c, int count)
60 {
61     for (int i = 0; i < count; i++)
62         cio_put_c (c);
63 }
64
65
66 void 
67 cio_put_str (const char *str)
68 {
69     fputs (str, stdout);
70 }
71
72
73
74 /* NAME
75  *   kb_getc                    Get a character from the keyboard
76  *
77  * SYNOPSIS
78  *   key = kb_getc()
79  *
80  * DESCRIPTION
81  *      1. This routine returns an EXTENTED ASCII code,
82  *         the extended codes have a low byte of 0 and a distinctive
83  *         high byte, such as 0x2D00 and 0x3200
84  *      2. This routine waits until a key has been typed
85  *      2. The keystroke will not be echoed.
86  */
87
88 unsigned int cio_kb_getc(void)
89 {
90     return fgetc(stdin);
91 }
92
93 void 
94 cio_kb_ungetc (unsigned int c)
95 {
96     ungetc(c, stdin);
97 }
98
99 /* NAME
100  *    kb_gets                           Get a string from the keyboard
101  *
102  * SYNOPSIS
103  *    str = kb_gets (str, maxlen)
104  *    char *str                         Space to store input string
105  *    int maxlen                        Maximum number of characters to read
106  *                                      (Not including EOS)
107  * NOTES
108  *    Backspace - erases character to the right
109  *    Escape    - erases to beginning of line
110  *    Return    - ends string (no not cause a linefeed)
111  */
112
113 char *
114 cio_kb_gets (char *str, int maxlen)
115 {
116     return fgets(str, maxlen, stdin);
117 }
118
119 /* NAME
120  *   kb_waitc                   Wait for a character from the keyboard
121  *
122  * SYNOPSIS
123  *   key = kb_waitc (astr, estr, beep)
124  *   int key                    Keystroke entered
125  *   char *astr                 String of valid ascii characters
126  *   bool beep                  If TRUE, beep when user hits invalid key
127  *
128  */
129
130
131 unsigned int
132 cio_kb_waitc (const char *astr, int beep_on_error)
133 {
134   unsigned int c;
135   do {
136     c = cio_kb_getc ();
137     if (strchr (astr, c) != NULL)
138         break;
139     if (beep_on_error)
140       cio_beep();
141   } while (1);
142   
143   return (c);
144 }
145
146
147 /* NAME
148  *    beep                              sound a beep to user
149  *
150  * SYNOPSIS
151  *    beep()
152  */
153
154 void cio_beep (void)
155 {
156         cio_tone (2000.0, 0.3);
157 }
158
159 /* NAME
160  *    tone              play a frequency sound for some duration
161  *
162  * SYNOPSIS
163  *    tone (freq, length)
164  *    double freq       frequency to play in Hertz
165  *    double length     duration to play note in seconds
166  */
167
168 void 
169 cio_tone (double freq, double length)
170 {
171 #if 1
172   fprintf(stdout, "\007");
173 #else
174   cio_spkr_freq (freq);         /* Set frequency of tone */
175   cio_spkr_on ();                       /* Turn on speaker */
176   pause (length);                       /* Pause for length seconds */
177   cio_spkr_off ();                      /* Turn off speaker */
178 #endif
179 }