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/metadata.h>
32 #include <mono/metadata/threadpool.h>
34 /* On solaris, curses.h must come before both termios.h and term.h */
45 /* Needed for FIONREAD under solaris */
46 #ifdef HAVE_SYS_FILIO_H
47 # include <sys/filio.h>
49 #ifdef HAVE_SYS_IOCTL_H
50 # include <sys/ioctl.h>
53 #include <mono/metadata/console-io.h>
54 #include <mono/metadata/exception.h>
56 static gboolean setup_finished;
57 static gboolean atexit_called;
59 /* The string used to return the terminal to its previous state */
60 static gchar *teardown_str;
62 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
63 static gchar *keypad_xmit_str;
66 /* This is the last state used by Mono, used after a CONT signal is received */
67 static struct termios mono_attr;
70 /* static void console_restore_signal_handlers (void); */
71 static void console_set_signal_handlers (void);
74 mono_console_init (void)
78 /* Make sure the standard file descriptors are opened */
79 fd = open ("/dev/null", O_RDWR);
80 while (fd >= 0 && fd < 3) {
81 fd = open ("/dev/null", O_RDWR);
86 static struct termios initial_attr;
89 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
93 return isatty (GPOINTER_TO_INT (handle));
97 set_property (gint property, gboolean value)
100 gboolean callset = FALSE;
105 if (tcgetattr (STDIN_FILENO, &attr) == -1)
108 check = (attr.c_lflag & property) != 0;
109 if ((value || check) && !(value && check)) {
112 attr.c_lflag |= property;
114 attr.c_lflag &= ~property;
120 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
128 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
131 return set_property (ECHO, want_echo);
135 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
137 return set_property (IGNBRK, !want_break);
141 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
145 struct timeval *tvptr;
153 FD_SET (STDIN_FILENO, &rfds);
155 divvy = div (timeout, 1000);
156 tv.tv_sec = divvy.quot;
157 tv.tv_usec = divvy.rem;
162 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
163 } while (ret == -1 && errno == EINTR);
167 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
172 return (ret > 0) ? ret : 0;
175 static gint32 cols_and_lines;
179 terminal_get_dimensions (void)
183 int save_errno = errno;
185 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0){
186 ret = (ws.ws_col << 16) | ws.ws_row;
194 terminal_get_dimensions (void)
208 if (teardown_str != NULL) {
209 write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
210 g_free (teardown_str);
214 tcflush (STDIN_FILENO, TCIFLUSH);
215 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
216 set_property (ECHO, TRUE);
217 setup_finished = FALSE;
221 do_console_cancel_event (void)
223 static MonoClassField *cancel_handler_field;
224 MonoDomain *domain = mono_domain_get ();
226 MonoDelegate *load_value;
228 MonoMethodMessage *msg;
235 klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
239 if (cancel_handler_field == NULL) {
240 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
241 g_assert (cancel_handler_field);
244 vtable = mono_class_vtable_full (domain, klass, FALSE);
247 mono_field_static_get_value (vtable, cancel_handler_field, &load_value);
248 if (load_value == NULL)
251 klass = load_value->object.vtable->klass;
252 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
253 g_assert (method != NULL);
254 im = mono_get_delegate_invoke (method->klass);
255 msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
256 mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
259 static gboolean in_sigint;
261 sigint_handler (int signo)
271 do_console_cancel_event ();
276 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
279 sigcont_handler (int signo, void *the_siginfo, void *data)
281 // Ignore error, there is not much we can do in the sigcont handler.
282 tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
284 if (keypad_xmit_str != NULL)
285 write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
287 // Call previous handler
288 if (save_sigcont.sa_sigaction != NULL &&
289 save_sigcont.sa_sigaction != (void *)SIG_DFL &&
290 save_sigcont.sa_sigaction != (void *)SIG_IGN)
291 (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
295 sigwinch_handler (int signo, void *the_siginfo, void *data)
297 int dims = terminal_get_dimensions ();
299 cols_and_lines = dims;
301 // Call previous handler
302 if (save_sigwinch.sa_sigaction != NULL &&
303 save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
304 save_sigwinch.sa_sigaction != (void *)SIG_IGN)
305 (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
309 * console_set_signal_handlers:
311 * Installs various signals handlers for the use of the console, as
314 * SIGCONT: this is received after the application has resumed execution
315 * if it was suspended with Control-Z before. This signal handler needs
316 * to resend the terminal sequence to send keyboard in keypad mode (this
317 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
320 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
321 * a thread from the thread pool which notifies all registered cancel_event
324 * SIGWINCH: is used to track changes to the console window when a GUI
325 * terminal is resized. It sets an internal variable that is checked
326 * by System.Console when the Terminfo driver has been activated.
329 console_set_signal_handlers ()
331 struct sigaction sigcont, sigint, sigwinch;
333 memset (&sigcont, 0, sizeof (struct sigaction));
334 memset (&sigint, 0, sizeof (struct sigaction));
335 memset (&sigwinch, 0, sizeof (struct sigaction));
338 sigcont.sa_handler = (void *) sigcont_handler;
339 sigcont.sa_flags = 0;
340 sigemptyset (&sigcont.sa_mask);
341 sigaction (SIGCONT, &sigcont, &save_sigcont);
344 sigint.sa_handler = sigint_handler;
346 sigemptyset (&sigint.sa_mask);
347 sigaction (SIGINT, &sigint, &save_sigint);
349 // Window size changed
350 sigwinch.sa_handler = (void *) sigwinch_handler;
351 sigwinch.sa_flags = 0;
352 sigemptyset (&sigwinch.sa_mask);
353 sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
356 #if currently_unuused
358 // Currently unused, should we ever call the restore handler?
359 // Perhaps before calling into Process.Start?
362 console_restore_signal_handlers ()
364 sigaction (SIGCONT, &save_sigcont, NULL);
365 sigaction (SIGINT, &save_sigint, NULL);
366 sigaction (SIGWINCH, &save_sigwinch, NULL);
371 set_control_chars (MonoArray *control_chars, const guchar *cc)
373 /* The index into the array comes from corlib/System/ControlCharacters.cs */
375 mono_array_set (control_chars, gchar, 0, cc [VINTR]);
378 mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
381 mono_array_set (control_chars, gchar, 2, cc [VERASE]);
384 mono_array_set (control_chars, gchar, 3, cc [VKILL]);
387 mono_array_set (control_chars, gchar, 4, cc [VEOF]);
390 mono_array_set (control_chars, gchar, 5, cc [VTIME]);
393 mono_array_set (control_chars, gchar, 6, cc [VMIN]);
396 mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
399 mono_array_set (control_chars, gchar, 8, cc [VSTART]);
402 mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
405 mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
408 mono_array_set (control_chars, gchar, 11, cc [VEOL]);
411 mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
414 mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
417 mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
420 mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
423 mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
428 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
434 dims = terminal_get_dimensions ();
436 int cols = 0, rows = 0;
438 char *str = getenv ("COLUMNS");
441 str = getenv ("LINES");
445 if (cols != 0 && rows != 0)
446 cols_and_lines = (cols << 16) | rows;
450 cols_and_lines = dims;
453 *size = &cols_and_lines;
455 /* 17 is the number of entries set in set_control_chars() above.
456 * NCCS is the total size, but, by now, we only care about those 17 values*/
457 mono_gc_wbarrier_generic_store (control_chars, (MonoObject*) mono_array_new (mono_domain_get (), mono_defaults.byte_class, 17));
458 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
461 mono_attr = initial_attr;
462 mono_attr.c_lflag &= ~(ICANON);
463 mono_attr.c_iflag &= ~(IXON|IXOFF);
464 mono_attr.c_cc [VMIN] = 1;
465 mono_attr.c_cc [VTIME] = 0;
467 /* Disable C-y being used as a suspend character on OSX */
468 mono_attr.c_cc [VDSUSP] = 255;
470 if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
473 set_control_chars (*control_chars, mono_attr.c_cc);
474 /* If initialized from another appdomain... */
478 keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
480 console_set_signal_handlers ();
481 setup_finished = TRUE;
482 if (!atexit_called) {
483 if (teardown != NULL)
484 teardown_str = mono_string_to_utf8 (teardown);
486 atexit (tty_teardown);
491 #endif /* #if defined(__native_client__) */