Wild Life  2.30
 All Data Structures Files Functions Variables Typedefs Macros
raw.c
Go to the documentation of this file.
1 
6 /* Copyright 1991 Digital Equipment Corporation.
7 ** All Rights Reserved.
8 *****************************************************************/
9 
10 /* RM: Feb 18 1994
11  Added "NORAW" compile flag which removes most of this file.
12  */
13 
14 #include "defs.h"
15 
16 #ifndef NORAW
17 
18 static struct sgttyb param_input;
19 static long mode_raw = FALSE;
20 static char bufbuf[BUFSIZ+1] = {0};
21 
31 long c_begin_raw ()
32 {
33  struct sgttyb param;
34  struct termio argio;
35 
36  if (mode_raw)
37  {
38  Errorline ("in begin_raw: already in mode raw\n");
39  return FALSE;
40  }
41 
42  if (ioctl (stdin_fileno, TIOCGETP, &param_input) == -1)
43  Errorline ("in begin_raw: cannot get the input parameters\n");
44 
45  bcopy ((char*)&param_input, (char*)&param, sizeof (param));
46 
47 #if 0
48  /* with RAW, we catch everything (eg: ^C is 3) */
49  param.sg_flags |= CBREAK | TANDEM | RAW;
50 #else
51  param.sg_flags |= CBREAK | TANDEM;
52 #endif
53 
54  param.sg_flags &= ~ECHO;
55 
56  if (ioctl (stdin_fileno, TIOCSETN, &param) == -1)
57  Errorline ("in begin_raw: cannot set the input parameters\n");
58 
59  if (ioctl (stdin_fileno, TCGETA, &argio) == -1)
60  Errorline ("in begin_raw: cannot get the terminal\n");
61 
62  /* do not strip the characters (the 8th bit is used for the key Compose) */
63 #if 1
64  /* catch every character */
65  argio.c_lflag &= ~(ICANON|ISIG);
66  argio.c_cc[VMIN] = 1;
67  argio.c_cc[VTIME] = 0;
68 
69  /* with IXON, do not interpret ctrl-S and ctrl-Q */
70  argio.c_iflag &= ~(ISTRIP|IXON);
71 
72  /* map LF to CR-LF */
73  argio.c_oflag |= OPOST|ONLCR;
74 #else
75  argio.c_iflag &= ~(ISTRIP);
76 #endif
77 
78  if (ioctl (stdin_fileno, TCSETA, &argio) == -1)
79  Errorline ("in begin_raw: cannot set the terminal\n");
80 
81  (void)setvbuf (stdin, bufbuf, _IOFBF, BUFSIZ);
82 
83  bzero (bufbuf, BUFSIZ+1);
84 
85  mode_raw = TRUE;
86  return TRUE;
87 }
88 
89 
99 long c_get_raw ()
100 {
102  ptr_definition types[2];
103  long nfds;
104  fd_set readfd, writefd, exceptfd;
105  struct timeval timeout;
106  long char_flag = FALSE, event_flag = FALSE;
107  long c = 0;
108  // ptr_psi_term key_code;
109  long level;
110 
111  types[0] = real;
112  types[1] = boolean;
113 
114  begin_builtin (c_get_raw, 2, 0, types);
115 
116  if ((int)strlen (bufbuf) == 0)
117  {
118  level = (unsigned long) aim->c;
119 
120 
121  do
122  {
123  FD_ZERO(&readfd);
124  FD_SET(stdin_fileno, &readfd);
125  FD_ZERO(&writefd);
126  FD_ZERO(&exceptfd);
127  timeout.tv_sec = 0;
128  timeout.tv_usec = 100000;
129 
130  nfds = select (32, &readfd, &writefd, &exceptfd, &timeout);
131  if (nfds == -1)
132  {
133  if (errno != EINTR)
134  {
135  Errorline ("it is not possible to read characters or X events\n");
136  exit_life(TRUE);
137  }
138  else
139  interrupt ();
140  }
141  else
142  if (nfds == 0)
143  {
144 #ifdef X11
145  if (x_exist_event ())
146  {
147  event_flag = TRUE;
149  }
150 #endif
151  }
152  else
153  {
154  if (FD_ISSET(stdin_fileno, &readfd) != 0)
155  {
156  /* c cna be equal to 0 with the mouse's selection */
157  /* first the selection is inside the buffer bufbuf */
158  /* later fgetc returns zeros */
159  /* I don't understand - jch - Fri Aug 28 1992 */
160  if ((c = fgetc (stdin)) != 0)
161  {
162  (void)unify_real_result (args[0], (REAL) c);
163  char_flag = TRUE;
164  /* the shift is done below */
165  }
166  }
167  else
168  Errorline ("in select: unknown descriptor\n");
169  }
170  } while (!(char_flag || event_flag));
171  }
172  else
173  {
174  (void)unify_real_result (args[0], (REAL) bufbuf[0]);
175  char_flag = TRUE;
176  }
177 
178  /* shift */
179  if (char_flag)
180  bcopy (&bufbuf[1], bufbuf, BUFSIZ-1);
181 
182  /* return if an X event has occured */
183  unify_bool_result (args[1], event_flag);
184 
185  success = TRUE;
186  end_builtin ();
187 }
188 
198 long c_put_raw ()
199 {
201  ptr_definition types[1];
202 
203  types[0] = real;
204 
205  begin_builtin (c_put_raw, 1, 0, types);
206 
207  (void)putchar ((char) val[0]);
208  (void)fflush (stdout);
209  success = TRUE;
210  end_builtin ();
211 }
212 
222 long c_end_raw ()
223 {
224  if (!mode_raw)
225  {
226  Errorline ("in c_end_raw: not in mode raw\n");
227  return FALSE;
228  }
229 
230  if (ioctl (stdin_fileno, TIOCSETN, &param_input) == -1)
231  Errorline ("in end_raw: cannot reset mode raw\n");
232 
233  (void)setvbuf (stdin, bufbuf, _IONBF, BUFSIZ);
234  bzero (bufbuf, BUFSIZ);
235 
236  mode_raw = FALSE;
237  return TRUE;
238 }
239 
248 long c_in_raw ()
249 {
250  deref_ptr (aim->a);
251  unify_bool_result (aim->b, mode_raw);
252 
253  return TRUE;
254 }
255 
265 long c_window_flag ()
266 {
267  deref_ptr (aim->a);
268 #ifdef X11
270 #else
272 #endif
273 
274  return TRUE;
275 }
276 
286 long c_reset_window_flag ()
287 {
288  deref_ptr (aim->a);
289 #ifdef X11
291 #endif
292 
293  return TRUE;
294 }
295 #endif
296 
304 {
305 #ifndef NORAW
306  new_built_in(bi_module,"begin_raw", predicate, c_begin_raw);
307  new_built_in(bi_module,"get_raw", predicate, c_get_raw);
308  new_built_in(bi_module,"put_raw", predicate, c_put_raw);
309  new_built_in(bi_module,"end_raw", predicate, c_end_raw);
312  new_built_in(bi_module,"reset_window_flag", predicate, c_reset_window_flag);
313 #endif
314 }
long x_exist_event()
x_exist_event
Definition: xpred.c:3787
void new_built_in(ptr_module m, char *s, def_type t, long(*r)())
new_built_in
Definition: built_ins.c:5375
#define function_it
was enum (def_type) in extern.h now there is typedef ptr_definition
Definition: def_const.h:1408
void interrupt()
INTERRUPT()
Definition: interrupt.c:21
void exit_life(long nl_flag)
exit_life
Definition: built_ins.c:2219
long c_end_raw()
void unify_bool_result(ptr_psi_term t, long v)
unify_bool_result
Definition: built_ins.c:344
includes
long c_in_raw()
#define stdin_fileno
Definition: def_const.h:1377
#define REAL
Which C type to use to represent reals and integers in Wild_Life.
Definition: def_const.h:132
ptr_definition boolean
symbol in bi module
Definition: def_glob.h:185
long x_window_creation
Definition: def_glob.h:1046
#define begin_builtin(FUNCNAME, NBARGS, NBARGSIN, TYPES)
Definition: def_macro.h:203
void release_resid(ptr_psi_term t)
release_resid
Definition: lefun.c:445
ptr_definition real
symbol in bi module
Definition: def_glob.h:375
long c_reset_window_flag()
void Errorline(char *format,...)
Errorline.
Definition: error.c:465
#define deref_ptr(P)
Definition: def_macro.h:100
void raw_setup_builtins()
raw_setup_builtins
Definition: raw.c:303
#define TRUE
Standard boolean.
Definition: def_const.h:268
#define FALSE
Standard boolean.
Definition: def_const.h:275
long c_put_raw()
ptr_goal aim
Definition: def_glob.h:1024
ptr_module bi_module
Module for public built-ins.
Definition: def_glob.h:687
long unify_real_result(ptr_psi_term t, REAL v)
unify_real_result
Definition: built_ins.c:386
long c_get_raw()
ptr_psi_term xevent_existing
Definition: def_glob.h:1037
long c_window_flag()
#define include_var_builtin(NBARGS)
Definition: def_macro.h:196
#define end_builtin()
Definition: def_macro.h:259
long c_begin_raw()