aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/terminal/terminaldisplayinit.cc
blob: 3defa9dece6d9cc31df3e68cee7a6c4e204ed795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
    Mosh: the mobile shell
    Copyright 2012 Keith Winstein

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations including
    the two.

    You must obey the GNU General Public License in all respects for all
    of the code used other than OpenSSL. If you modify file(s) with this
    exception, you may extend this exception to your version of the
    file(s), but you are not obligated to do so. If you do not wish to do
    so, delete this exception statement from your version. If you delete
    this exception statement from all source files in the program, then
    also delete it here.
*/

/* This is in its own file because otherwise the ncurses #defines
   alias our own variable names. */

#include "src/include/config.h"
#include "terminaldisplay.h"

#include <string>
#include <stdexcept>

#if defined HAVE_NCURSESW_CURSES_H
#  include <ncursesw/curses.h>
#  include <ncursesw/term.h>
#elif defined HAVE_NCURSESW_H
#  include <ncursesw.h>
#  include <term.h>
#elif defined HAVE_NCURSES_CURSES_H
#  include <ncurses/curses.h>
#  include <ncurses/term.h>
#elif defined HAVE_NCURSES_H
#  include <ncurses.h>
#  include <term.h>
#elif defined HAVE_CURSES_H
#  include <curses.h>
#  include <term.h>
#else
#  error "SysV or X/Open-compatible Curses header file required"
#endif
#include <stdlib.h>
#include <string.h>

using namespace Terminal;

static bool ti_flag( const char *capname )
{
  int val = tigetflag( const_cast<char *>( capname ) );
  if ( val == -1 ) {
    throw std::invalid_argument( std::string( "Invalid terminfo boolean capability " ) + capname );
  }
  return val;
}

static const char *ti_str( const char *capname )
{
  const char *val = tigetstr( const_cast<char *>( capname ) );
  if ( val == (const char *)-1 ) {
    throw std::invalid_argument( std::string( "Invalid terminfo string capability " ) + capname );
  }
  return val;
}

Display::Display( bool use_environment )
  : has_ech( true ), has_bce( true ), has_title( true ), smcup( NULL ), rmcup( NULL )
{
  if ( use_environment ) {
    int errret = -2;
    int ret = setupterm( (char *)0, 1, &errret );

    if ( ret != OK ) {
      switch ( errret ) {
      case 1:
	throw std::runtime_error( "Terminal is hardcopy and cannot be used by curses applications." );
	break;
      case 0:
	throw std::runtime_error( "Unknown terminal type." );
	break;
      case -1:
	throw std::runtime_error( "Terminfo database could not be found." );
	break;
      default:
	throw std::runtime_error( "Unknown terminfo error." );
	break;
      } 
    }

    /* check for ECH */
    has_ech = ti_str( "ech" );

    /* check for BCE */
    has_bce = ti_flag( "bce" );

    /* Check if we can set the window title and icon name.  terminfo does not
       have reliable information on this, so we hardcode a whitelist of
       terminal type prefixes. */
    static const char * const title_term_types[] = {
      "xterm", "rxvt", "kterm", "Eterm", "alacritty", "screen", "tmux"
    };

    has_title = false;
    const char *term_type = getenv( "TERM" );
    if ( term_type ) {
      for ( size_t i = 0;
            i < sizeof( title_term_types ) / sizeof( const char * );
            i++ ) {
        if ( 0 == strncmp( term_type, title_term_types[ i ],
                           strlen( title_term_types[ i ] ) ) ) {
          has_title = true;
          break;
        }
      }
    }

    if ( !getenv( "MOSH_NO_TERM_INIT" ) ) {
      smcup = ti_str("smcup");
      rmcup = ti_str("rmcup");
    }
  }
}