[go: up one dir, main page]

File: Tui.cpp

package info (click to toggle)
spew 1.0.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 988 kB
  • ctags: 775
  • sloc: cpp: 5,878; sh: 5,377; makefile: 112
file content (165 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//////////////////////////  -*-C++-*- /////////////////////////////////////////
//
// Tui.cpp
//
// Spew
//
// Copyright (C) 2004 Hewlett-Packard Corp.
//
//
// 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 version 2 of the License.
// 
// 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, write to the Free Software Foundation, Inc.,
// 675 Mass Ave, Cambridge, MA 02139, USA.

using namespace std;

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <ncurses.h>
#include <sys/ioctl.h>

#include "common.h"
#include "Tui.h"

//////////////////////////  Tui::Tui()  ///////////////////////////////////////
Tui::Tui()
{
   mClosed = false;
   mCurrentScreenRows = 0;
   mCurrentScreenColumns = 0;
}


//////////////////////////  Tui::init()  //////////////////////////////////////
int Tui::init()
{
   initscr();
   cbreak();
   noecho();
   curs_set(0);

   this->setScreenSize();

   return 0;
}


//////////////////////////  Tui::close()  /////////////////////////////////////
int Tui::close()
{
   endwin();
   mClosed = true;

   return 0;
}


//////////////////////////  Tui::clearScreen()  ///////////////////////////////
int Tui::clearScreen()
{
   clear();
   return 0;
}


//////////////////////////  Tui::print()  /////////////////////////////////////
int Tui::print(const char *msg)
{
   printw((char *)msg);
   refresh();
   return 0;
}


//////////////////////////  Tui::setScreenSize()  ////////////////////////////
int Tui::setScreenSize()
{
#ifdef HAVE_WINSIZE_IOCTL
   int newScreenRows, newScreenColumns;
   getmaxyx(stdscr, newScreenRows, newScreenColumns);

#  ifdef TIOCGWINSZ 
   struct winsize w;
   if (ioctl(2, TIOCGWINSZ, &w) < 0)
   {
      return EXIT_ERROR_SYSTEM;
   }
   else
   {
      if (w.ws_row > 0)
          newScreenRows = w.ws_row;
      if (w.ws_col > 0)
         newScreenColumns = w.ws_col;
   }
#  else
#    ifdef WIOCGETD
   struct uwdata w;
   if (ioctl(2, WIOCGETD, &w) < 0)
   {
      return EXIT_ERROR_SYSTEM
   }
   else
   {
      if (w.uw_height > 0)
          newScreenRows = w.uw_height / w.uw_vs;
      if (w.uw_width > 0)
          newScreenColumns = w.uw_width / w.uw_hs;
   }
#    endif
#  endif

   if (newScreenRows != mCurrentScreenRows || 
       newScreenColumns != mCurrentScreenColumns)
   {
      mCurrentScreenRows = newScreenRows;
      mCurrentScreenColumns = newScreenColumns;
      resizeterm(mCurrentScreenRows, mCurrentScreenColumns);
   }
#else // HAVE_WINSIZE_IOCTL
   char *errPtr;
   long int tmpval;

   mCurrentScreenColumns = DEFAULT_SCREEN_COLUMNS;
   const char *colsEnv = getenv("COLUMNS");
   if (colsEnv)
   {
      tmpval = strtol(colsEnv, &errPtr, 10);
      if (errPtr == '\0')
          mCurrentScreenColumns = tmpval;
   }
   
   mCurrentScreenRows = DEFAULT_SCREEN_ROWS;
   const char *rowsEnv = getenv("LINES");
   if (rowsEnv)
   {
      tmpval = strtol(rowsEnv, &errPtr, 10);
      if (errPtr == '\0')
          mCurrentScreenRows = tmpval;
   }
#endif // HAVE_WINSIZE_IOCTL

   return EXIT_OK;
}



//////////////////////////  Tui::~Tui()  //////////////////////////////////////
Tui::~Tui()
{
   if (!mClosed)
      this->close();
}