2 * console-io.c: ConsoleDriver internal calls
5 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 * Copyright (C) 2005 Novell, Inc. (http://www.novell.com)
17 #include <sys/types.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/object-internals.h>
21 #include <mono/metadata/class-internals.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/metadata.h>
24 #include <mono/metadata/threadpool.h>
25 /* On solaris, curses.h must come before both termios.h and term.h */
35 /* Needed for FIONREAD under solaris */
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
39 #ifndef PLATFORM_WIN32
41 #include <sys/ioctl.h>
45 #include <mono/metadata/console-io.h>
46 #include <mono/metadata/exception.h>
48 static gboolean setup_finished;
49 static gboolean atexit_called;
50 static gchar *teardown_str;
54 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
58 return (GetFileType (handle) == FILE_TYPE_CHAR);
62 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
68 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
74 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
80 ves_icall_System_ConsoleDriver_TtySetup (MonoString *teardown, char *verase, char *vsusp, char *intr)
86 ves_icall_System_ConsoleDriver_GetTtySize (HANDLE handle, gint32 *width, gint32 *height)
92 static struct termios initial_attr;
95 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
99 return isatty (GPOINTER_TO_INT (handle));
103 set_property (gint property, gboolean value)
106 gboolean callset = FALSE;
111 if (tcgetattr (STDIN_FILENO, &attr) == -1)
114 check = (attr.c_lflag & property) != 0;
115 if ((value || check) && !(value && check)) {
118 attr.c_lflag |= property;
120 attr.c_lflag &= ~property;
126 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
133 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
135 return set_property (ECHO, want_echo);
139 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
141 return set_property (IGNBRK, !want_break);
145 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
149 struct timeval *tvptr;
157 FD_SET (STDIN_FILENO, &rfds);
159 divvy = div (timeout, 1000);
160 tv.tv_sec = divvy.quot;
161 tv.tv_usec = divvy.rem;
166 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
167 } while (ret == -1 && errno == EINTR);
171 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
176 return (ret > 0) ? ret : 0;
187 if (teardown_str != NULL) {
188 write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
189 g_free (teardown_str);
193 tcflush (STDIN_FILENO, TCIFLUSH);
194 tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
195 set_property (ECHO, TRUE);
196 setup_finished = FALSE;
200 do_console_cancel_event (void)
202 static MonoClassField *cancel_handler_field;
203 MonoDomain *domain = mono_domain_get ();
205 MonoDelegate *load_value;
207 MonoMethodMessage *msg;
213 klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
217 if (cancel_handler_field == NULL) {
218 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
219 g_assert (cancel_handler_field);
222 mono_field_static_get_value (mono_class_vtable (domain, klass), cancel_handler_field, &load_value);
223 if (load_value == NULL)
226 klass = load_value->object.vtable->klass;
227 method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
228 g_assert (method != NULL);
229 im = mono_get_delegate_invoke (method->klass);
230 msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
231 mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
234 static gboolean in_sigint;
236 sigint_handler (int signo)
244 do_console_cancel_event ();
249 ves_icall_System_ConsoleDriver_TtySetup (MonoString *teardown, char *verase, char *vsusp, char*intr)
259 if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
262 /* TODO: handle SIGTSTP - Ctrl-Z */
264 attr.c_lflag &= ~ICANON;
265 attr.c_cc [VMIN] = 1;
266 attr.c_cc [VTIME] = 0;
267 if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
270 *verase = initial_attr.c_cc [VERASE];
271 *vsusp = initial_attr.c_cc [VSUSP];
272 *intr = initial_attr.c_cc [VINTR];
273 /* If initialized from another appdomain... */
277 setup_finished = TRUE;
278 if (!atexit_called) {
279 if (teardown != NULL)
280 teardown_str = mono_string_to_utf8 (teardown);
282 atexit (tty_teardown);
289 ves_icall_System_ConsoleDriver_GetTtySize (HANDLE handle, gint32 *width, gint32 *height)
297 res = ioctl (GPOINTER_TO_INT (handle), TIOCGWINSZ, &ws);
311 #endif /* !PLATFORM_WIN32 */