Wild Life  2.29
 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 
22 /*****************************************************************/
23 /******** BeginRaw
24 
25  c_begin_raw ()
26 
27  set the keyboard to be in mode raw (return each key when pressed)
28  and don't echo the character on the standard output.
29 
30  this built-in should be used only by the life-shell of Kathleen.
31 
32  */
33 
39 long c_begin_raw ()
40 {
41  struct sgttyb param;
42  struct termio argio;
43 
44  if (mode_raw)
45  {
46  Errorline ("in begin_raw: already in mode raw\n");
47  return FALSE;
48  }
49 
50  if (ioctl (stdin_fileno, TIOCGETP, &param_input) == -1)
51  Errorline ("in begin_raw: cannot get the input parameters\n");
52 
53  bcopy ((char*)&param_input, (char*)&param, sizeof (param));
54 
55 #if 0
56  /* with RAW, we catch everything (eg: ^C is 3) */
57  param.sg_flags |= CBREAK | TANDEM | RAW;
58 #else
59  param.sg_flags |= CBREAK | TANDEM;
60 #endif
61 
62  param.sg_flags &= ~ECHO;
63 
64  if (ioctl (stdin_fileno, TIOCSETN, &param) == -1)
65  Errorline ("in begin_raw: cannot set the input parameters\n");
66 
67  if (ioctl (stdin_fileno, TCGETA, &argio) == -1)
68  Errorline ("in begin_raw: cannot get the terminal\n");
69 
70  /* do not strip the characters (the 8th bit is used for the key Compose) */
71 #if 1
72  /* catch every character */
73  argio.c_lflag &= ~(ICANON|ISIG);
74  argio.c_cc[VMIN] = 1;
75  argio.c_cc[VTIME] = 0;
76 
77  /* with IXON, do not interpret ctrl-S and ctrl-Q */
78  argio.c_iflag &= ~(ISTRIP|IXON);
79 
80  /* map LF to CR-LF */
81  argio.c_oflag |= OPOST|ONLCR;
82 #else
83  argio.c_iflag &= ~(ISTRIP);
84 #endif
85 
86  if (ioctl (stdin_fileno, TCSETA, &argio) == -1)
87  Errorline ("in begin_raw: cannot set the terminal\n");
88 
89  (void)setvbuf (stdin, bufbuf, _IOFBF, BUFSIZ);
90 
91  bzero (bufbuf, BUFSIZ+1);
92 
93  mode_raw = TRUE;
94  return TRUE;
95 }
96 
97 
107 long c_get_raw ()
108 {
110  ptr_definition types[2];
111  long nfds;
112  fd_set readfd, writefd, exceptfd;
113  struct timeval timeout;
114  long char_flag = FALSE, event_flag = FALSE;
115  long c = 0;
116  // ptr_psi_term key_code;
117  long level;
118 
119  types[0] = real;
120  types[1] = boolean;
121 
122  begin_builtin (c_get_raw, 2, 0, types);
123 
124  if ((int)strlen (bufbuf) == 0)
125  {
126  level = (unsigned long) aim->c;
127 
128 
129  do
130  {
131  FD_ZERO(&readfd);
132  FD_SET(stdin_fileno, &readfd);
133  FD_ZERO(&writefd);
134  FD_ZERO(&exceptfd);
135  timeout.tv_sec = 0;
136  timeout.tv_usec = 100000;
137 
138  nfds = select (32, &readfd, &writefd, &exceptfd, &timeout);
139  if (nfds == -1)
140  {
141  if (errno != EINTR)
142  {
143  Errorline ("it is not possible to read characters or X events\n");
144  exit_life(TRUE);
145  }
146  else
147  interrupt ();
148  }
149  else
150  if (nfds == 0)
151  {
152 #ifdef X11
153  if (x_exist_event ())
154  {
155  event_flag = TRUE;
157  }
158 #endif
159  }
160  else
161  {
162  if (FD_ISSET(stdin_fileno, &readfd) != 0)
163  {
164  /* c cna be equal to 0 with the mouse's selection */
165  /* first the selection is inside the buffer bufbuf */
166  /* later fgetc returns zeros */
167  /* I don't understand - jch - Fri Aug 28 1992 */
168  if ((c = fgetc (stdin)) != 0)
169  {
170  (void)unify_real_result (args[0], (REAL) c);
171  char_flag = TRUE;
172  /* the shift is done below */
173  }
174  }
175  else
176  Errorline ("in select: unknown descriptor\n");
177  }
178  } while (!(char_flag || event_flag));
179  }
180  else
181  {
182  (void)unify_real_result (args[0], (REAL) bufbuf[0]);
183  char_flag = TRUE;
184  }
185 
186  /* shift */
187  if (char_flag)
188  bcopy (&bufbuf[1], bufbuf, BUFSIZ-1);
189 
190  /* return if an X event has occured */
191  unify_bool_result (args[1], event_flag);
192 
193  success = TRUE;
194  end_builtin ();
195 }
196 
206 long c_put_raw ()
207 {
209  ptr_definition types[1];
210 
211  types[0] = real;
212 
213  begin_builtin (c_put_raw, 1, 0, types);
214 
215  (void)putchar ((char) val[0]);
216  (void)fflush (stdout);
217  success = TRUE;
218  end_builtin ();
219 }
220 
230 long c_end_raw ()
231 {
232  if (!mode_raw)
233  {
234  Errorline ("in c_end_raw: not in mode raw\n");
235  return FALSE;
236  }
237 
238  if (ioctl (stdin_fileno, TIOCSETN, &param_input) == -1)
239  Errorline ("in end_raw: cannot reset mode raw\n");
240 
241  (void)setvbuf (stdin, bufbuf, _IONBF, BUFSIZ);
242  bzero (bufbuf, BUFSIZ);
243 
244  mode_raw = FALSE;
245  return TRUE;
246 }
247 
256 long c_in_raw ()
257 {
258  deref_ptr (aim->a);
260 
261  return TRUE;
262 }
263 
274 {
275  deref_ptr (aim->a);
276 #ifdef X11
278 #else
280 #endif
281 
282  return TRUE;
283 }
284 
295 {
296  deref_ptr (aim->a);
297 #ifdef X11
299 #endif
300 
301  return TRUE;
302 }
303 #endif
304 
312 {
313 #ifndef NORAW
320  new_built_in(bi_module,"reset_window_flag", predicate, c_reset_window_flag);
321 #endif
322 }
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()
c_get_raw
Definition: raw.c:107
void unify_bool_result(ptr_psi_term t, long v)
unify_bool_result
Definition: built_ins.c:345
long c_begin_raw()
c_begin_raw
Definition: raw.c:39
#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()
c_put_raw
Definition: raw.c:206
long c_window_flag()
c_window_flag
Definition: raw.c:273
void release_resid(ptr_psi_term t)
release_resid
Definition: lefun.c:445
static long mode_raw
Definition: raw.c:19
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()
raw_setup_builtins
Definition: raw.c:311
long c_end_raw()
c_end_raw
Definition: raw.c:230
#define TRUE
Definition: def_const.h:127
#define FALSE
Definition: def_const.h:128
static char bufbuf[BUFSIZ+1]
Definition: raw.c:20
long c_in_raw()
Definition: raw.c:256
ptr_goal aim
Definition: def_glob.h:49
static struct sgttyb param_input
Definition: raw.c:18
#define TANDEM
Definition: defs.h:26
long c_reset_window_flag()
c_reset_window_flag
Definition: raw.c:294
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