Wild Life  2.29
 All Data Structures Files Functions Variables Typedefs Macros
raw.c
Go to the documentation of this file.
1 /* Copyright 1991 Digital Equipment Corporation.
2 ** All Rights Reserved.
3 *****************************************************************/
4 /* $Id: raw.c,v 1.3 1995/07/27 19:21:19 duchier Exp $ */
5 
6 /* RM: Feb 18 1994
7  Added "NORAW" compile flag which removes most of this file.
8  */
9 
10 #include "defs.h"
11 
12 #ifndef NORAW
13 
14 
15 // extern long level;
16 static struct sgttyb param_input;
17 static long mode_raw = FALSE;
18 static char bufbuf[BUFSIZ+1] = {0};
19 
20 /*****************************************************************/
21 /******** BeginRaw
22 
23  c_begin_raw ()
24 
25  set the keyboard to be in mode raw (return each key when pressed)
26  and don't echo the character on the standard output.
27 
28  this built-in should be used only by the life-shell of Kathleen.
29 
30  */
31 
32 long c_begin_raw ()
33 
34 {
35  struct sgttyb param;
36  struct termio argio;
37 
38  if (mode_raw)
39  {
40  Errorline ("in begin_raw: already in mode raw\n");
41  return FALSE;
42  }
43 
44  if (ioctl (stdin_fileno, TIOCGETP, &param_input) == -1)
45  Errorline ("in begin_raw: cannot get the input parameters\n");
46 
47  bcopy ((char*)&param_input, (char*)&param, sizeof (param));
48 
49 #if 0
50  /* with RAW, we catch everything (eg: ^C is 3) */
51  param.sg_flags |= CBREAK | TANDEM | RAW;
52 #else
53  param.sg_flags |= CBREAK | TANDEM;
54 #endif
55 
56  param.sg_flags &= ~ECHO;
57 
58  if (ioctl (stdin_fileno, TIOCSETN, &param) == -1)
59  Errorline ("in begin_raw: cannot set the input parameters\n");
60 
61  if (ioctl (stdin_fileno, TCGETA, &argio) == -1)
62  Errorline ("in begin_raw: cannot get the terminal\n");
63 
64  /* do not strip the characters (the 8th bit is used for the key Compose) */
65 #if 1
66  /* catch every character */
67  argio.c_lflag &= ~(ICANON|ISIG);
68  argio.c_cc[VMIN] = 1;
69  argio.c_cc[VTIME] = 0;
70 
71  /* with IXON, do not interpret ctrl-S and ctrl-Q */
72  argio.c_iflag &= ~(ISTRIP|IXON);
73 
74  /* map LF to CR-LF */
75  argio.c_oflag |= OPOST|ONLCR;
76 #else
77  argio.c_iflag &= ~(ISTRIP);
78 #endif
79 
80  if (ioctl (stdin_fileno, TCSETA, &argio) == -1)
81  Errorline ("in begin_raw: cannot set the terminal\n");
82 
83  (void)setvbuf (stdin, bufbuf, _IOFBF, BUFSIZ);
84 
85  bzero (bufbuf, BUFSIZ+1);
86 
87  mode_raw = TRUE;
88  return TRUE;
89 }
90 
91 
92 /*****************************************************************/
93 /******** GetRaw
94 
95  c_get_raw (-Char, -EventFlag)
96 
97  return the next key pressed in Char or if a X event has occured
98 
99  this built-in should be used only by the life-shell of Kathleen.
100 
101  */
102 
103 long c_get_raw ()
104 {
106  ptr_definition types[2];
107  long nfds;
108  fd_set readfd, writefd, exceptfd;
109  struct timeval timeout;
110  long char_flag = FALSE, event_flag = FALSE;
111  long c = 0;
112  // ptr_psi_term key_code;
113  long level;
114 
115  types[0] = real;
116  types[1] = boolean;
117 
118  begin_builtin (c_get_raw, 2, 0, types);
119 
120  if ((int)strlen (bufbuf) == 0)
121  {
122  level = (unsigned long) aim->c;
123 
124 
125  do
126  {
127  FD_ZERO(&readfd);
128  FD_SET(stdin_fileno, &readfd);
129  FD_ZERO(&writefd);
130  FD_ZERO(&exceptfd);
131  timeout.tv_sec = 0;
132  timeout.tv_usec = 100000;
133 
134  nfds = select (32, &readfd, &writefd, &exceptfd, &timeout);
135  if (nfds == -1)
136  {
137  if (errno != EINTR)
138  {
139  Errorline ("it is not possible to read characters or X events\n");
140  exit_life(TRUE);
141  }
142  else
143  interrupt ();
144  }
145  else
146  if (nfds == 0)
147  {
148 #ifdef X11
149  if (x_exist_event ())
150  {
151  event_flag = TRUE;
153  }
154 #endif
155  }
156  else
157  {
158  if (FD_ISSET(stdin_fileno, &readfd) != 0)
159  {
160  /* c cna be equal to 0 with the mouse's selection */
161  /* first the selection is inside the buffer bufbuf */
162  /* later fgetc returns zeros */
163  /* I don't understand - jch - Fri Aug 28 1992 */
164  if ((c = fgetc (stdin)) != 0)
165  {
166  (void)unify_real_result (args[0], (REAL) c);
167  char_flag = TRUE;
168  /* the shift is done below */
169  }
170  }
171  else
172  Errorline ("in select: unknown descriptor\n");
173  }
174  } while (!(char_flag || event_flag));
175  }
176  else
177  {
178  (void)unify_real_result (args[0], (REAL) bufbuf[0]);
179  char_flag = TRUE;
180  }
181 
182  /* shift */
183  if (char_flag)
184  bcopy (&bufbuf[1], bufbuf, BUFSIZ-1);
185 
186  /* return if an X event has occured */
187  unify_bool_result (args[1], event_flag);
188 
189  success = TRUE;
190  end_builtin ();
191 }
192 
193 
194 /*****************************************************************/
195 /******** PutRaw
196 
197  c_put_raw (+Char)
198 
199  write the specified char on the standard output
200 
201  this built-in should be used only by the life-shell of Kathleen.
202 
203  */
204 
205 long c_put_raw ()
206 {
208  ptr_definition types[1];
209 
210  types[0] = real;
211 
212  begin_builtin (c_put_raw, 1, 0, types);
213 
214  (void)putchar ((char) val[0]);
215  (void)fflush (stdout);
216  success = TRUE;
217  end_builtin ();
218 }
219 
220 
221 /*****************************************************************/
222 /******** EndRaw
223 
224  end_raw ()
225 
226  reset the keyboard in the previous state before xcInitModeRaw.
227 
228  this built-in should be used only by the life-shell of Kathleen.
229 
230  */
231 
232 long c_end_raw ()
233 
234 {
235  if (!mode_raw)
236  {
237  Errorline ("in c_end_raw: not in mode raw\n");
238  return FALSE;
239  }
240 
241  if (ioctl (stdin_fileno, TIOCSETN, &param_input) == -1)
242  Errorline ("in end_raw: cannot reset mode raw\n");
243 
244  (void)setvbuf (stdin, bufbuf, _IONBF, BUFSIZ);
245  bzero (bufbuf, BUFSIZ);
246 
247  mode_raw = FALSE;
248  return TRUE;
249 }
250 
251 
252 /*****************************************************************/
253 /******** InRaw
254 
255  in_raw ()
256 
257  return TRUE if mode raw
258 
259  this built-in should be used only by the life-shell of Kathleen.
260 
261  */
262 
263 long c_in_raw ()
264 
265 {
266  deref_ptr (aim->a);
268 
269  return TRUE;
270 }
271 
272 
273 /*****************************************************************/
274 /******** WindowFlag
275 
276  window_flag ()
277 
278  return TRUE if a window has been created
279 
280  this built-in should be used only by the life-shell of Kathleen.
281 
282  */
283 
285 
286 {
287  deref_ptr (aim->a);
288 #ifdef X11
290 #else
292 #endif
293 
294  return TRUE;
295 }
296 
297 
298 /*****************************************************************/
299 /******** ResetWindowFlag
300 
301  reset_window_flag ()
302 
303  return the flag x_window_creation
304 
305  this built-in should be used only by the life-shell of Kathleen.
306 
307  */
308 
310 
311 {
312  deref_ptr (aim->a);
313 #ifdef X11
315 #endif
316 
317  return TRUE;
318 }
319 
320 
321 #endif
322 
323 /*****************************************/
324 
325 /* set up the built-ins for the mode raw */
326 
328 
329 {
330 #ifndef NORAW
337  new_built_in(bi_module,"reset_window_flag", predicate, c_reset_window_flag);
338 #endif
339 }
void new_built_in(ptr_module m, char *s, def_type t, long(*r)())
new_built_in
Definition: built_ins.c:5371
#define predicate
Definition: def_const.h:361
#define function_it
Definition: def_const.h:362
void interrupt()
INTERRUPT()
Definition: interrupt.c:21
void exit_life(long nl_flag)
exit_life
Definition: built_ins.c:2220
long c_get_raw()
Definition: raw.c:103
void unify_bool_result(ptr_psi_term t, long v)
unify_bool_result
Definition: built_ins.c:345
long c_begin_raw()
Definition: raw.c:32
#define stdin_fileno
Definition: def_const.h:343
#define REAL
Definition: def_const.h:72
long x_window_creation
Definition: def_glob.h:217
#define begin_builtin(FUNCNAME, NBARGS, NBARGSIN, TYPES)
Definition: def_macro.h:198
long c_put_raw()
Definition: raw.c:205
long c_window_flag()
Definition: raw.c:284
void release_resid(ptr_psi_term t)
release_resid
Definition: lefun.c:445
static long mode_raw
Definition: raw.c:17
void Errorline(char *format,...)
Definition: error.c:414
ptr_definition real
Definition: def_glob.h:102
#define deref_ptr(P)
Definition: def_macro.h:95
void raw_setup_builtins()
Definition: raw.c:327
long c_end_raw()
Definition: raw.c:232
#define TRUE
Definition: def_const.h:127
#define FALSE
Definition: def_const.h:128
static char bufbuf[BUFSIZ+1]
Definition: raw.c:18
long c_in_raw()
Definition: raw.c:263
ptr_goal aim
Definition: def_glob.h:49
static struct sgttyb param_input
Definition: raw.c:16
#define TANDEM
Definition: defs.h:26
long c_reset_window_flag()
Definition: raw.c:309
long unify_real_result(ptr_psi_term t, REAL v)
unify_real_result
Definition: built_ins.c:387
ptr_module bi_module
Definition: def_glob.h:155
ptr_definition boolean
Definition: def_glob.h:73
ptr_psi_term xevent_existing
Definition: def_glob.h:208
#define include_var_builtin(NBARGS)
Definition: def_macro.h:191
#define end_builtin()
Definition: def_macro.h:254