/[LeafOK_CVS]/lbbs/src/io.c
ViewVC logotype

Contents of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Tue Mar 22 13:36:13 2005 UTC (21 years ago) by sysadm
Branch: MAIN
Changes since 1.9: +7 -5 lines
Content type: text/x-csrc
*** empty log message ***

1 /***************************************************************************
2 io.c - description
3 -------------------
4 begin : Mon Oct 18 2004
5 copyright : (C) 2004 by Leaflet
6 email : leaflet@leafok.com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "io.h"
19 #include "common.h"
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <time.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/ioctl.h>
26
27 int
28 outc (char c)
29 {
30 int retval;
31
32 retval = fprintf (stdout, "%c", c);
33
34 return retval;
35 }
36
37 int
38 prints (const char *format, ...)
39 {
40 va_list args;
41 int retval;
42
43 va_start (args, format);
44 retval = vfprintf (stdout, format, args);
45 va_end (args);
46
47 return retval;
48 }
49
50 int
51 iflush ()
52 {
53 int retval;
54
55 retval = fflush (stdout);
56
57 return retval;
58 }
59
60 int
61 igetch ()
62 {
63 static char buf[256];
64 unsigned char c, tmp[256];
65 int out = KEY_NULL, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i =
66 0, j, result;
67 static int len = 0, pos = 0;
68 fd_set inputs, testfds;
69 struct timeval timeout;
70
71 if (pos >= len)
72 {
73 pos = 0;
74 len = 0;
75
76 FD_ZERO (&inputs);
77 FD_SET (0, &inputs);
78
79 testfds = inputs;
80 timeout.tv_sec = 1;
81 timeout.tv_usec = 0;
82
83 result = select (FD_SETSIZE, &testfds, (fd_set *) NULL,
84 (fd_set *) NULL, &timeout);
85
86 switch (result)
87 {
88 case 0:
89 out = KEY_TIMEOUT;
90 break;
91 case -1:
92 log_error ("select() error!\n");
93 break;
94 default:
95 if (FD_ISSET (0, &testfds))
96 {
97 len = read (0, buf, 255);
98 }
99 break;
100 }
101
102 //For debug
103 //for (j = 0; j < len; j++)
104 // log_std ("<--[%u]\n", (buf[j] + 256) % 256);
105 }
106
107 while (pos < len)
108 {
109 c = buf[pos++];
110
111 if (c == '\0')
112 {
113 out = c;
114 break;
115 }
116
117 if (c == KEY_CONTROL)
118 {
119 if (in_control == 0)
120 {
121 in_control = 1;
122 i = 0;
123 continue;
124 }
125 }
126
127 if (in_control)
128 {
129 tmp[i++] = c;
130 if (i >= 2)
131 {
132 out = (int) tmp[0] * 256 + tmp[1];
133 in_control = 0;
134 break;
135 }
136 continue;
137 }
138
139 if (c == ESC_KEY)
140 {
141 if (in_esc == 0)
142 {
143 in_esc = 1;
144 in_ascii = 1;
145 i = 0;
146 continue;
147 }
148 else
149 {
150 out = ESC_KEY;
151 in_esc = 0;
152 break;
153 }
154 }
155
156 in_esc = 0;
157
158 if (in_ascii)
159 {
160 tmp[i++] = c;
161 if (c == 'm')
162 {
163 in_ascii = 0;
164 continue;
165 }
166 if (i == 2 && c >= 'A' && c <= 'D')
167 {
168 in_ascii = 0;
169 switch (c)
170 {
171 case 'A':
172 out = KEY_UP;
173 break;
174 case 'B':
175 out = KEY_DOWN;
176 break;
177 case 'C':
178 out = KEY_RIGHT;
179 break;
180 case 'D':
181 out = KEY_LEFT;
182 break;
183 }
184 break;
185 }
186 if (i == 3 && tmp[0] == 91 && tmp[2] == 126)
187 {
188 in_ascii = 0;
189 switch (tmp[1])
190 {
191 case 49:
192 out = KEY_HOME;
193 break;
194 case 51:
195 out = KEY_DEL;
196 break;
197 case 52:
198 out = KEY_END;
199 break;
200 case 53:
201 out = KEY_PGUP;
202 break;
203 case 54:
204 out = KEY_PGDN;
205 break;
206 }
207 break;
208 }
209 continue;
210 }
211
212 out = ((int) c + 256) % 256;
213 break;
214 }
215
216 //for debug
217 //log_std ("-->[%u]\n", out);
218
219 return out;
220 }
221
222 int
223 igetch_t (long int sec)
224 {
225 int ch;
226 time_t t_begin = time (0);
227
228 do
229 {
230 ch = igetch ();
231 }
232 while ((ch == KEY_TIMEOUT) && (time (0) - t_begin < sec));
233
234 return ch;
235 }
236
237 int
238 ikbhit ()
239 {
240 int len;
241
242 ioctl (0, FIONREAD, &len);
243
244 return len;
245 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1