From 0a2a18b5a786d7df487a1ab5063df58226764d98 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 1 Sep 2011 11:09:55 +0400 Subject: [PATCH] Ticket #2601: incorrect TTY layer initialization. The xterm_flag variable was initialized in setup_mc() but used first time in init_key() and in tty_init() (in do_enter_ca_mode()) before setup_mc() call. Now xterm initialized in early step of mc start up process and xterm support and mouse are initialized in tty_init(). Signed-off-by: Andrew Borodin --- lib/tty/tty-internal.h | 1 + lib/tty/tty-ncurses.c | 7 ++++- lib/tty/tty-slang.c | 15 ++++++++-- lib/tty/tty.c | 71 +++++++++++++++++++++++++++++++++++++++++++++- lib/tty/tty.h | 4 ++- lib/tty/win.c | 2 +- lib/tty/win.h | 2 +- src/args.c | 3 -- src/filemanager/midnight.c | 69 +------------------------------------------- src/main.c | 10 ++++++- 10 files changed, 105 insertions(+), 79 deletions(-) diff --git a/lib/tty/tty-internal.h b/lib/tty/tty-internal.h index 4ab3c5de0..450b1fe75 100644 --- a/lib/tty/tty-internal.h +++ b/lib/tty/tty-internal.h @@ -39,6 +39,7 @@ extern gboolean mouse_enabled; /*** declarations of public functions ************************************************************/ char *mc_tty_normalize_from_utf8 (const char *); +void tty_init_xterm_support (gboolean is_xterm); /*** inline functions ****************************************************************************/ #endif /* MC_TTY_INTERNAL_H */ diff --git a/lib/tty/tty-ncurses.c b/lib/tty/tty-ncurses.c index 10a7c492b..693c8a67b 100644 --- a/lib/tty/tty-ncurses.c +++ b/lib/tty/tty-ncurses.c @@ -48,6 +48,7 @@ #include "tty-internal.h" /* slow_tty */ #include "tty.h" #include "color-internal.h" +#include "mouse.h" #include "win.h" /* include at last !!! */ @@ -139,7 +140,7 @@ mc_tty_normalize_lines_char (const char *ch) /* --------------------------------------------------------------------------------------------- */ void -tty_init (gboolean slow, gboolean ugly_lines) +tty_init (gboolean slow, gboolean ugly_lines, gboolean mouse_enable, gboolean is_xterm) { slow_tty = slow; (void) ugly_lines; @@ -167,6 +168,10 @@ tty_init (gboolean slow, gboolean ugly_lines) tty_start_interrupt_key (); + if (!mouse_enable) + use_mouse_p = MOUSE_DISABLED; + tty_init_xterm_support (is_xterm); /* do it before do_enter_ca_mode() call */ + init_mouse (); do_enter_ca_mode (); tty_raw_mode (); noecho (); diff --git a/lib/tty/tty-slang.c b/lib/tty/tty-slang.c index f42fd4a37..1ab24bc77 100644 --- a/lib/tty/tty-slang.c +++ b/lib/tty/tty-slang.c @@ -241,14 +241,14 @@ mc_tty_normalize_lines_char (const char *str) /* --------------------------------------------------------------------------------------------- */ void -tty_init (gboolean slow, gboolean ugly_lines) +tty_init (gboolean slow, gboolean ugly_lines, gboolean mouse_enable, gboolean is_xterm) { slow_tty = slow; ugly_line_drawing = ugly_lines; SLtt_Ignore_Beep = 1; - SLutf8_enable (-1); /* has to be called first before any of the other functions. */ + SLutf8_enable (-1); /* has to be called first before any of the other functions. */ SLtt_get_terminfo (); /* * If the terminal in not in terminfo but begins with a well-known @@ -300,7 +300,18 @@ tty_init (gboolean slow, gboolean ugly_lines) /* It's the small part from the previous init_key() */ init_key_input_fd (); + /* For 8-bit locales, NCurses handles 154 (0x9A) symbol properly, while S-Lang + * requires SLsmg_Display_Eight_Bit >= 154 (OR manual filtering if xterm display + * detected - but checking TERM would fail under screen, OR running xterm + * with allowC1Printable). + */ + tty_display_8bit (FALSE); + SLsmg_init_smg (); + if (!mouse_enable) + use_mouse_p = MOUSE_DISABLED; + tty_init_xterm_support (is_xterm); /* do it before do_enter_ca_mode() call */ + init_mouse (); do_enter_ca_mode (); tty_keypad (TRUE); tty_nodelay (FALSE); diff --git a/lib/tty/tty.c b/lib/tty/tty.c index dc7ca6c7b..fe2dc49e4 100644 --- a/lib/tty/tty.c +++ b/lib/tty/tty.c @@ -33,13 +33,16 @@ #include #include +#include +#include /* exit() */ #include "lib/global.h" #include "lib/strutil.h" #include "tty.h" #include "tty-internal.h" - +#include "mouse.h" /* use_mouse_p */ +#include "win.h" /*** global variables ****************************************************************************/ @@ -78,6 +81,34 @@ sigintr_handler (int signo) /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ +/** + * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE. + * + * @param force_xterm Set forced the XTerm type + * + * @return true if @param force_xterm is true or value of $TERM is one of term*, konsole* + * rxvt*, Eterm or dtterm + */ +gboolean +tty_check_term (gboolean force_xterm) +{ + const char *termvalue; + + termvalue = getenv ("TERM"); + if (termvalue == NULL || *termvalue == '\0') + { + fputs (_("The TERM environment variable is unset!\n"), stderr); + exit (EXIT_FAILURE); + } + + return force_xterm || strncmp (termvalue, "xterm", 5) == 0 + || strncmp (termvalue, "konsole", 7) == 0 + || strncmp (termvalue, "rxvt", 4) == 0 + || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0; +} + +/* --------------------------------------------------------------------------------------------- */ + extern gboolean tty_is_slow (void) { @@ -204,3 +235,41 @@ mc_tty_normalize_from_utf8 (const char *str) } /* --------------------------------------------------------------------------------------------- */ + +void +tty_init_xterm_support (gboolean is_xterm) +{ + const char *termvalue; + + termvalue = getenv ("TERM"); + + /* Check mouse capabilities */ + xmouse_seq = tty_tgetstr ("Km"); + + if (strcmp (termvalue, "cygwin") == 0) + { + is_xterm = TRUE; + use_mouse_p = MOUSE_DISABLED; + } + + if (is_xterm) + { + /* Default to the standard xterm sequence */ + if (xmouse_seq == NULL) + xmouse_seq = ESC_STR "[M"; + + /* Enable mouse unless explicitly disabled by --nomouse */ + if (use_mouse_p != MOUSE_DISABLED) + { + const char *color_term = getenv ("COLORTERM"); + if (strncmp (termvalue, "rxvt", 4) == 0 || + (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) || + strcmp (termvalue, "Eterm") == 0) + use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING; + else + use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING; + } + } +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/tty/tty.h b/lib/tty/tty.h index 521aad64e..7db6193aa 100644 --- a/lib/tty/tty.h +++ b/lib/tty/tty.h @@ -74,7 +74,9 @@ extern void tty_beep (void); extern int reset_hp_softkeys; -extern void tty_init (gboolean slow, gboolean ugly_lines); +extern gboolean tty_check_term (gboolean force_xterm); +extern void tty_init (gboolean slow, gboolean ugly_lines, gboolean mouse_enable, + gboolean is_xterm); extern void tty_shutdown (void); extern gboolean tty_is_slow (void); diff --git a/lib/tty/win.c b/lib/tty/win.c index 6d0b841f2..f55bd8d6e 100644 --- a/lib/tty/win.c +++ b/lib/tty/win.c @@ -42,7 +42,7 @@ /* This flag is set by xterm detection routine in function main() */ /* It is used by function view_other_cmd() */ -int xterm_flag = 0; +gboolean xterm_flag = FALSE; extern int keybar_visible; diff --git a/lib/tty/win.h b/lib/tty/win.h index bca052bfb..0de17f82e 100644 --- a/lib/tty/win.h +++ b/lib/tty/win.h @@ -15,7 +15,7 @@ /*** global variables defined in .c file *********************************************************/ -extern int xterm_flag; +extern gboolean xterm_flag; /*** declarations of public functions ************************************************************/ diff --git a/src/args.c b/src/args.c index a1addb984..da2243592 100644 --- a/src/args.c +++ b/src/args.c @@ -388,9 +388,6 @@ mc_setup_by_args (int argc, char *argv[]) const char *base; char *tmp; - if (mc_args__nomouse) - use_mouse_p = MOUSE_DISABLED; - if (mc_args__netfs_logfile != NULL) { #ifdef ENABLE_VFS_FTP diff --git a/src/filemanager/midnight.c b/src/filemanager/midnight.c index 9d5409605..8be079831 100644 --- a/src/filemanager/midnight.c +++ b/src/filemanager/midnight.c @@ -37,14 +37,13 @@ #include #include #include -#include #include /* for username in xterm title */ #include "lib/global.h" #include "lib/tty/tty.h" -#include "lib/tty/key.h" /* For init_key() */ #include "lib/tty/mouse.h" +#include "lib/tty/key.h" /* For init_key() */ #include "lib/tty/win.h" /* xterm_flag */ #include "lib/skin.h" #include "lib/util.h" @@ -719,69 +718,6 @@ ctl_x_cmd (void) /* --------------------------------------------------------------------------------------------- */ static void -init_xterm_support (void) -{ - const char *termvalue; - - termvalue = getenv ("TERM"); - if (!termvalue || !(*termvalue)) - { - fputs (_("The TERM environment variable is unset!\n"), stderr); - exit (EXIT_FAILURE); - } - - /* Check mouse capabilities */ - xmouse_seq = tty_tgetstr ("Km"); - - if (strcmp (termvalue, "cygwin") == 0) - { - mc_args__force_xterm = 1; - use_mouse_p = MOUSE_DISABLED; - } - - if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0 - || strncmp (termvalue, "konsole", 7) == 0 - || strncmp (termvalue, "rxvt", 4) == 0 - || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0) - { - xterm_flag = 1; - -#ifdef HAVE_SLANG - /* For 8-bit locales, NCurses handles 154 (0x9A) symbol properly, while S-Lang - * requires SLsmg_Display_Eight_Bit >= 154 (OR manual filtering if xterm display - * detected - but checking TERM would fail under screen, OR running xterm - * with allowC1Printable). - */ - tty_display_8bit (FALSE); -#endif - - /* Default to the standard xterm sequence */ - if (!xmouse_seq) - { - xmouse_seq = ESC_STR "[M"; - } - - /* Enable mouse unless explicitly disabled by --nomouse */ - if (use_mouse_p != MOUSE_DISABLED) - { - const char *color_term = getenv ("COLORTERM"); - if (strncmp (termvalue, "rxvt", 4) == 0 || - (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) || - strcmp (termvalue, "Eterm") == 0) - { - use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING; - } - else - { - use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING; - } - } - } -} - -/* --------------------------------------------------------------------------------------------- */ - -static void setup_mc (void) { #ifdef HAVE_SLANG @@ -799,9 +735,6 @@ setup_mc (void) if ((tty_baudrate () < 9600) || tty_is_slow ()) verbose = 0; - - init_xterm_support (); - init_mouse (); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/main.c b/src/main.c index 155803d6c..3fb4281be 100644 --- a/src/main.c +++ b/src/main.c @@ -440,6 +440,13 @@ main (int argc, char *argv[]) if (!mc_args_handle (argc, argv, "mc")) exit (EXIT_FAILURE); + /* check terminal type + * $TEMR must be set and not empty + * xterm_flag is used in init_key() and tty_init() + * Do this after mc_args_handle() where mc_args__force_xterm is set up. + */ + xterm_flag = tty_check_term (mc_args__force_xterm); + /* NOTE: This has to be called before tty_init or whatever routine calls any define_sequence */ init_key (); @@ -464,7 +471,8 @@ main (int argc, char *argv[]) /* Must be done before init_subshell, to set up the terminal size: */ /* FIXME: Should be removed and LINES and COLS computed on subshell */ - tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing); + tty_init (mc_args__slow_terminal, mc_args__ugly_line_drawing, !mc_args__nomouse, + (gboolean) xterm_flag); load_setup (); -- 2.11.4.GIT