2 * console-io.c: ConsoleDriver internal calls for Unix systems.
5 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 * Copyright (C) 2005-2009 Novell, Inc. (http://www.novell.com)
9 #if defined(__native_client__)
10 #include "console-null.c"
20 #ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
23 #include <sys/types.h>
27 #include <mono/metadata/appdomain.h>
28 #include <mono/metadata/object-internals.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/domain-internals.h>
31 #include <mono/metadata/gc-internal.h>
32 #include <mono/metadata/metadata.h>
33 #include <mono/metadata/threadpool.h>
35 /* On solaris, curses.h must come before both termios.h and term.h */
46 /* Needed for FIONREAD under solaris */
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 # include <sys/ioctl.h>
54 #include <mono/metadata/console-io.h>
55 #include <mono/metadata/exception.h>
57 static gboolean setup_finished;
58 static gboolean atexit_called;
60 /* The string used to return the terminal to its previous state */
61 static gchar *teardown_str;
63 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
64 static gchar *keypad_xmit_str;
67 /* This is the last state used by Mono, used after a CONT signal is received */
68 static struct termios mono_attr;
71 /* static void console_restore_signal_handlers (void); */
72 static void console_set_signal_handlers (void);
75 mono_console_init (void)
79 /* Make sure the standard file descriptors are opened */
80 fd = open ("/dev/null", O_RDWR);
81 while (fd >= 0 && fd < 3) {
82 fd = open ("/dev/null", O_RDWR);
87 static struct termios initial_attr;
90 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
94 return isatty (GPOINTER_TO_INT (handle));
98 set_property (gint property, gboolean value)
101 gboolean callset = FALSE;
106 if (tcgetattr (STDIN_FILENO, &attr) == -1)
109 check = (attr.c_lflag & property) != 0;
110 if ((value || check) && !(value && check)) {
113 attr.c_lflag |= property;
115 attr.c_lflag &= ~property;
121 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
129 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
132 return set_property (ECHO, want_echo);
136 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
138 return set_property (IGNBRK, !want_break);
142 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
146 struct timeval *tvptr;
154 FD_SET (STDIN_FILENO, &rfds);
156 divvy = div (timeout, 1000);
157 tv.tv_sec = divvy.quot;
158 tv.tv_usec = divvy.rem;
163 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
164 } while (ret == -1 && errno == EINTR);
168 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
173 return (ret > 0) ? ret : 0;
176 static gint32 cols_and_lines;
180 terminal_get_dimensions (void)
184 int save_errno = errno;
186 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0){
187 ret = (ws.ws_col << 16) | ws.ws_row;
195 terminal_get_dimensions (void)
211 if (teardown_str != NULL) {
212 unused = write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
213 g_free (teardown_str);
217 tcflush (STDIN_FILENO, TCIFLUSH);
218 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
219 set_property (ECHO, TRUE);
220 setup_finished = FALSE;
224 do_console_cancel_event (void)
226 static MonoClassField *cancel_handler_field;
227 MonoDomain *domain = mono_domain_get ();
229 MonoDelegate *load_value;
231 MonoMethodMessage *msg;
235 /* FIXME: this should likely iterate all the domains, instead */
239 klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
243 if (cancel_handler_field == NULL) {
244 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
245 g_assert (cancel_handler_field);
248 vtable = mono_class_vtable_full (domain, klass, FALSE);
251 mono_field_static_get_value (vtable, cancel_handler_field, &load_value);
252 if (load_value == NULL)
255 klass = load_value->object.vtable->klass;
256 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
257 g_assert (method != NULL);
258 im = mono_get_delegate_invoke (method->klass);
259 msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
260 mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
263 static int need_cancel = FALSE;
264 /* this is executed from the finalizer thread */
266 mono_console_handle_async_ops (void)
270 do_console_cancel_event ();
274 static gboolean in_sigint;
276 sigint_handler (int signo)
287 mono_gc_finalize_notify ();
292 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
295 sigcont_handler (int signo, void *the_siginfo, void *data)
298 // Ignore error, there is not much we can do in the sigcont handler.
299 tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
301 if (keypad_xmit_str != NULL)
302 unused = write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
304 // Call previous handler
305 if (save_sigcont.sa_sigaction != NULL &&
306 save_sigcont.sa_sigaction != (void *)SIG_DFL &&
307 save_sigcont.sa_sigaction != (void *)SIG_IGN)
308 (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
312 sigwinch_handler (int signo, void *the_siginfo, void *data)
314 int dims = terminal_get_dimensions ();
316 cols_and_lines = dims;
318 // Call previous handler
319 if (save_sigwinch.sa_sigaction != NULL &&
320 save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
321 save_sigwinch.sa_sigaction != (void *)SIG_IGN)
322 (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
326 * console_set_signal_handlers:
328 * Installs various signals handlers for the use of the console, as
331 * SIGCONT: this is received after the application has resumed execution
332 * if it was suspended with Control-Z before. This signal handler needs
333 * to resend the terminal sequence to send keyboard in keypad mode (this
334 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
337 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
338 * a thread from the thread pool which notifies all registered cancel_event
341 * SIGWINCH: is used to track changes to the console window when a GUI
342 * terminal is resized. It sets an internal variable that is checked
343 * by System.Console when the Terminfo driver has been activated.
346 console_set_signal_handlers ()
348 struct sigaction sigcont, sigint, sigwinch;
350 memset (&sigcont, 0, sizeof (struct sigaction));
351 memset (&sigint, 0, sizeof (struct sigaction));
352 memset (&sigwinch, 0, sizeof (struct sigaction));
355 sigcont.sa_handler = (void *) sigcont_handler;
356 sigcont.sa_flags = 0;
357 sigemptyset (&sigcont.sa_mask);
358 sigaction (SIGCONT, &sigcont, &save_sigcont);
361 sigint.sa_handler = sigint_handler;
363 sigemptyset (&sigint.sa_mask);
364 sigaction (SIGINT, &sigint, &save_sigint);
366 // Window size changed
367 sigwinch.sa_handler = (void *) sigwinch_handler;
368 sigwinch.sa_flags = 0;
369 sigemptyset (&sigwinch.sa_mask);
370 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
373 #if currently_unuused
375 // Currently unused, should we ever call the restore handler?
376 // Perhaps before calling into Process.Start?
379 console_restore_signal_handlers ()
381 sigaction (SIGCONT, &save_sigcont, NULL);
382 sigaction (SIGINT, &save_sigint, NULL);
383 sigaction (SIGWINCH, &save_sigwinch, NULL);
388 set_control_chars (MonoArray *control_chars, const guchar *cc)
390 /* The index into the array comes from corlib/System/ControlCharacters.cs */
392 mono_array_set (control_chars, gchar, 0, cc [VINTR]);
395 mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
398 mono_array_set (control_chars, gchar, 2, cc [VERASE]);
401 mono_array_set (control_chars, gchar, 3, cc [VKILL]);
404 mono_array_set (control_chars, gchar, 4, cc [VEOF]);
407 mono_array_set (control_chars, gchar, 5, cc [VTIME]);
410 mono_array_set (control_chars, gchar, 6, cc [VMIN]);
413 mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
416 mono_array_set (control_chars, gchar, 8, cc [VSTART]);
419 mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
422 mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
425 mono_array_set (control_chars, gchar, 11, cc [VEOL]);
428 mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
431 mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
434 mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
437 mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
440 mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
445 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
451 dims = terminal_get_dimensions ();
453 int cols = 0, rows = 0;
455 char *str = getenv ("COLUMNS");
458 str = getenv ("LINES");
462 if (cols != 0 && rows != 0)
463 cols_and_lines = (cols << 16) | rows;
467 cols_and_lines = dims;
470 *size = &cols_and_lines;
472 /* 17 is the number of entries set in set_control_chars() above.
473 * NCCS is the total size, but, by now, we only care about those 17 values*/
474 mono_gc_wbarrier_generic_store (control_chars, (MonoObject*) mono_array_new (mono_domain_get (), mono_defaults.byte_class, 17));
475 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
478 mono_attr = initial_attr;
479 mono_attr.c_lflag &= ~(ICANON);
480 mono_attr.c_iflag &= ~(IXON|IXOFF);
481 mono_attr.c_cc [VMIN] = 1;
482 mono_attr.c_cc [VTIME] = 0;
484 /* Disable C-y being used as a suspend character on OSX */
485 mono_attr.c_cc [VDSUSP] = 255;
487 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
490 set_control_chars (*control_chars, mono_attr.c_cc);
491 /* If initialized from another appdomain... */
495 keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
497 console_set_signal_handlers ();
498 setup_finished = TRUE;
499 if (!atexit_called) {
500 if (teardown != NULL)
501 teardown_str = mono_string_to_utf8 (teardown);
503 atexit (tty_teardown);
508 #endif /* #if defined(__native_client__) */