Works with letters, added trimmed Lovecraft
[name-generation.git] / element-lists / names-2.0.1 / src / keyb.c
1 /*---------------------------------------------------------------------
2 * Borrowed from chin.c v1.1, written by Davor Slamnig, 6/94 *
3 * Modified by Michael Harvey 8/94 *
4 * *
5 * Single character keyboard input on UNIX in three modes: *
6 * *
7 * wait - getchar() blocks until a character is read. *
8 * nowait - getchar() returns immediately, -1 is returned *
9 * if there are no characters in queue. *
10 * timeout - getchar() blocks until a character is read, *
11 * or a specified time limit is exceeded. *
12 * *
13 * vidsetup(1, 0); /* wait * / *
14 * vidsetup(0, 0); /* nowait * / *
15 * vidsetup(0, V_TIMEOUT); /* timeout * / *
16 * *
17 * vidsave(); /* save terminal * / *
18 * vidreset(); /* restore normal settings * / *
19 * *
20 * kb_init(); /* init kb mode * / *
21 * kb_done(); /* reset kb mode * / *
22 * kb_shell(); /* shell mode * / *
23 * *
24 * kb_wait(); /* wait mode * / *
25 * kb_nowait(); /* nowait mode * / *
26 * kb_timeout(); /* timeout mode * / *
27 * *
28 * See termio(7) *
29 ---------------------------------------------------------------------*/
30
31 #include <stdio.h>
32 #include <termio.h>
33 #include "keyb.h"
34
35 #define V_TIMEOUT 10 /* 1 second */
36
37 struct termio Savevid;
38
39 void vid_save(vmin, vtime)
40 {
41 if(ioctl(0, TCGETA, &Savevid) == -1)
42 {
43 perror("vidsave failed");
44 return;
45 }
46 }
47
48 void vid_setup(vmin, vtime)
49 int vmin, vtime;
50 {
51 static struct termio newvid; /* must be static or global */
52
53 if(ioctl(0, TCGETA, &newvid) == -1)
54 {
55 perror("TCGETA failed");
56 return;
57 }
58
59 newvid.c_lflag = (newvid.c_lflag & ~ICANON & ~ECHO);
60 newvid.c_cc[VMIN] = vmin;
61 newvid.c_cc[VTIME] = vtime;
62
63 if(ioctl(0, TCSETA, &newvid) == -1)
64 {
65 perror("vidsetup failed");
66 return;
67 }
68 }
69
70 void vid_reset()
71 {
72 if(ioctl(0, TCSETA, &Savevid) == -1)
73 {
74 perror("vidreset failed");
75 return;
76 }
77 }
78
79 void vid_wait()
80 {
81 /* wait mode - getchar() blocks until a character is read. */
82
83 vid_setup(1, 0);
84 }
85
86 void vid_nowait()
87 {
88 /* nowait mode - getchar() returns immediately, -1 is returned. */
89 /* if there are no characters in queue. */
90
91 vid_setup(0, 0);
92 }
93
94 void vid_timeout()
95 {
96 /* timeout mode - getchar() blocks until a character is read, */
97 /* or a specified time limit is exceeded. */
98
99 vid_setup(0, V_TIMEOUT);
100 }
101
102 void kb_init() { vid_save(0,0); }
103 void kb_done() { vid_reset(); }
104 void kb_shell() { vid_reset(); }
105 void kb_wait() { vid_wait(); }
106 void kb_nowait() { vid_nowait(); }
107 void kb_timeout() { vid_timeout(); }
108