3 * console-io.c: ConsoleDriver internal calls
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright (C) 2005 Novell, Inc. (http://www.novell.com)
17 #ifdef HAVE_SYS_TIME_H
20 #include <sys/types.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/object-internals.h>
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/domain-internals.h>
28 #include <mono/metadata/metadata.h>
29 #include <mono/metadata/threadpool.h>
30 /* On solaris, curses.h must come before both termios.h and term.h */
40 /* Needed for FIONREAD under solaris */
41 #ifdef HAVE_SYS_FILIO_H
42 #include <sys/filio.h>
44 #ifndef PLATFORM_WIN32
46 #include <sys/ioctl.h>
50 #include <mono/metadata/console-io.h>
51 #include <mono/metadata/exception.h>
53 static gboolean setup_finished;
54 static gboolean atexit_called;
56 /* The string used to return the terminal to its previous state */
57 static gchar *teardown_str;
59 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
60 static gchar *keypad_xmit_str;
63 /* This is the last state used by Mono, used after a CONT signal is received */
64 static struct termios mono_attr;
69 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
73 return (GetFileType (handle) == FILE_TYPE_CHAR);
77 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
83 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
89 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
95 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
101 static struct termios initial_attr;
104 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
108 return isatty (GPOINTER_TO_INT (handle));
112 set_property (gint property, gboolean value)
115 gboolean callset = FALSE;
120 if (tcgetattr (STDIN_FILENO, &attr) == -1)
123 check = (attr.c_lflag & property) != 0;
124 if ((value || check) && !(value && check)) {
127 attr.c_lflag |= property;
129 attr.c_lflag &= ~property;
135 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
143 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
146 return set_property (ECHO, want_echo);
150 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
152 return set_property (IGNBRK, !want_break);
156 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
160 struct timeval *tvptr;
168 FD_SET (STDIN_FILENO, &rfds);
170 divvy = div (timeout, 1000);
171 tv.tv_sec = divvy.quot;
172 tv.tv_usec = divvy.rem;
177 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
178 } while (ret == -1 && errno == EINTR);
182 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
187 return (ret > 0) ? ret : 0;
190 static gint32 cols_and_lines;
194 terminal_get_dimensions (void)
198 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
199 return (ws.ws_col << 16) | ws.ws_row;
205 terminal_get_dimensions (void)
219 if (teardown_str != NULL) {
220 write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
221 g_free (teardown_str);
225 tcflush (STDIN_FILENO, TCIFLUSH);
226 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
227 set_property (ECHO, TRUE);
228 setup_finished = FALSE;
232 do_console_cancel_event (void)
234 static MonoClassField *cancel_handler_field;
235 MonoDomain *domain = mono_domain_get ();
237 MonoDelegate *load_value;
239 MonoMethodMessage *msg;
245 klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
249 if (cancel_handler_field == NULL) {
250 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
251 g_assert (cancel_handler_field);
254 mono_field_static_get_value (mono_class_vtable (domain, klass), cancel_handler_field, &load_value);
255 if (load_value == NULL)
258 klass = load_value->object.vtable->klass;
259 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
260 g_assert (method != NULL);
261 im = mono_get_delegate_invoke (method->klass);
262 msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
263 mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
266 static gboolean in_sigint;
268 sigint_handler (int signo)
276 do_console_cancel_event ();
280 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
283 sigcont_handler (int signo, void *the_siginfo, void *data)
285 // Ignore error, there is not much we can do in the sigcont handler.
286 tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
288 if (keypad_xmit_str != NULL)
289 write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
291 // Call previous handler
292 if (save_sigcont.sa_sigaction != NULL &&
293 save_sigcont.sa_sigaction != (void *)SIG_DFL &&
294 save_sigcont.sa_sigaction != (void *)SIG_IGN)
295 (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
299 sigwinch_handler (int signo, void *the_siginfo, void *data)
301 int dims = terminal_get_dimensions ();
303 cols_and_lines = dims;
305 // Call previous handler
306 if (save_sigwinch.sa_sigaction != NULL &&
307 save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
308 save_sigwinch.sa_sigaction != (void *)SIG_IGN)
309 (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
313 console_set_signal_handlers ()
315 struct sigaction sigcont, sigint, sigwinch;
317 memset (&sigcont, 0, sizeof (struct sigaction));
318 memset (&sigint, 0, sizeof (struct sigaction));
319 memset (&sigwinch, 0, sizeof (struct sigaction));
322 sigcont.sa_handler = (void *) sigcont_handler;
323 sigcont.sa_flags = 0;
324 sigemptyset (&sigcont.sa_mask);
325 sigaction (SIGCONT, &sigcont, &save_sigcont);
328 sigint.sa_handler = sigint_handler;
330 sigemptyset (&sigint.sa_mask);
331 sigaction (SIGINT, &sigint, &save_sigint);
333 // Window size changed
334 sigwinch.sa_handler = (void *) sigwinch_handler;
335 sigwinch.sa_flags = 0;
336 sigemptyset (&sigwinch.sa_mask);
337 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
341 // Currently unused, should we ever call the restore handler?
342 // Perhaps before calling into Process.Start?
345 console_restore_signal_handlers ()
347 sigaction (SIGCONT, &save_sigcont, NULL);
348 sigaction (SIGINT, &save_sigint, NULL);
349 sigaction (SIGWINCH, &save_sigwinch, NULL);
353 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
359 dims = terminal_get_dimensions ();
361 int cols = 0, rows = 0;
363 char *str = getenv ("COLUMNS");
366 str = getenv ("LINES");
370 if (cols != 0 && rows != 0)
371 cols_and_lines = (cols << 16) | rows;
375 cols_and_lines = dims;
378 *size = &cols_and_lines;
383 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
386 mono_attr = initial_attr;
387 mono_attr.c_lflag &= ~(ICANON);
388 mono_attr.c_iflag &= ~(IXON|IXOFF);
389 mono_attr.c_cc [VMIN] = 1;
390 mono_attr.c_cc [VTIME] = 0;
391 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
394 *verase = initial_attr.c_cc [VERASE];
395 *vsusp = initial_attr.c_cc [VSUSP];
396 *intr = initial_attr.c_cc [VINTR];
397 /* If initialized from another appdomain... */
401 keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
403 console_set_signal_handlers ();
404 setup_finished = TRUE;
405 if (!atexit_called) {
406 if (teardown != NULL)
407 teardown_str = mono_string_to_utf8 (teardown);
409 atexit (tty_teardown);
415 #endif /* !PLATFORM_WIN32 */