-
/*
* console-io.c: ConsoleDriver internal calls
*
* Author:
* Gonzalo Paniagua Javier (gonzalo@ximian.com)
*
- * Copyright (C) 2005 Novell, Inc. (http://www.novell.com)
+ * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
*/
#include <config.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
-#ifndef PLATFORM_WIN32
#ifndef TIOCGWINSZ
+#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#endif
static struct termios mono_attr;
#endif
-#ifdef PLATFORM_WIN32
+#if defined (PLATFORM_WIN32) || defined (MONO_NULL_TTYDRIVER)
MonoBoolean
ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
{
}
MonoBoolean
-ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char *intr)
-{
- return FALSE;
-}
-
-MonoBoolean
-ves_icall_System_ConsoleDriver_GetTtySize (HANDLE handle, gint32 *width, gint32 *height)
+ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
{
return FALSE;
}
return (ret > 0) ? ret : 0;
}
+static gint32 cols_and_lines;
+
+#ifdef TIOCGWINSZ
+static int
+terminal_get_dimensions (void)
+{
+ struct winsize ws;
+
+ if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
+ return (ws.ws_col << 16) | ws.ws_row;
+
+ return -1;
+}
+#else
+static int
+terminal_get_dimensions (void)
+{
+ return -1;
+}
+#endif
+
static void
tty_teardown (void)
{
in_sigint = FALSE;
}
-static struct sigaction save_sigcont, save_sigint;
+static struct sigaction save_sigcont, save_sigint, save_sigwinch;
static void
sigcont_handler (int signo, void *the_siginfo, void *data)
if (keypad_xmit_str != NULL)
write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
+ // Call previous handler
if (save_sigcont.sa_sigaction != NULL &&
save_sigcont.sa_sigaction != (void *)SIG_DFL &&
save_sigcont.sa_sigaction != (void *)SIG_IGN)
(*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
}
+static void
+sigwinch_handler (int signo, void *the_siginfo, void *data)
+{
+ int dims = terminal_get_dimensions ();
+ if (dims != -1)
+ cols_and_lines = dims;
+
+ // Call previous handler
+ if (save_sigwinch.sa_sigaction != NULL &&
+ save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
+ save_sigwinch.sa_sigaction != (void *)SIG_IGN)
+ (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
+}
+
void
console_set_signal_handlers ()
{
- struct sigaction sigcont, sigint;
+ struct sigaction sigcont, sigint, sigwinch;
memset (&sigcont, 0, sizeof (struct sigaction));
memset (&sigint, 0, sizeof (struct sigaction));
+ memset (&sigwinch, 0, sizeof (struct sigaction));
// Continuing
sigcont.sa_handler = (void *) sigcont_handler;
sigint.sa_flags = 0;
sigemptyset (&sigint.sa_mask);
sigaction (SIGINT, &sigint, &save_sigint);
+
+ // Window size changed
+ sigwinch.sa_handler = (void *) sigwinch_handler;
+ sigwinch.sa_flags = 0;
+ sigemptyset (&sigwinch.sa_mask);
+ sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
}
//
{
sigaction (SIGCONT, &save_sigcont, NULL);
sigaction (SIGINT, &save_sigint, NULL);
+ sigaction (SIGWINCH, &save_sigwinch, NULL);
}
MonoBoolean
-ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr)
+ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
{
+ int dims;
+
MONO_ARCH_SAVE_REGS;
+ dims = terminal_get_dimensions ();
+ if (dims == -1){
+ int cols = 0, rows = 0;
+
+ char *str = getenv ("COLUMNS");
+ if (str != NULL)
+ cols = atoi (str);
+ str = getenv ("LINES");
+ if (str != NULL)
+ rows = atoi (str);
+
+ if (cols != 0 && rows != 0)
+ cols_and_lines = (cols << 16) | rows;
+ else
+ cols_and_lines = -1;
+ } else {
+ cols_and_lines = dims;
+ }
+
+ *size = &cols_and_lines;
+
*verase = '\0';
*vsusp = '\0';
*intr = '\0';
return FALSE;
mono_attr = initial_attr;
- mono_attr.c_lflag &= ~ICANON;
+ mono_attr.c_lflag &= ~(ICANON);
+ mono_attr.c_iflag &= ~(IXON|IXOFF);
mono_attr.c_cc [VMIN] = 1;
mono_attr.c_cc [VTIME] = 0;
if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
return TRUE;
}
-MonoBoolean
-ves_icall_System_ConsoleDriver_GetTtySize (HANDLE handle, gint32 *width, gint32 *height)
-{
-#ifdef TIOCGWINSZ
- struct winsize ws;
- int res;
-
- MONO_ARCH_SAVE_REGS;
-
- res = ioctl (GPOINTER_TO_INT (handle), TIOCGWINSZ, &ws);
-
- if (!res) {
- *width = ws.ws_col;
- *height = ws.ws_row;
- return TRUE;
- }
- else
- return FALSE;
-#else
- return FALSE;
-#endif
-}
-
#endif /* !PLATFORM_WIN32 */