1*a248dafdSChristopher Ferris /*
2*a248dafdSChristopher Ferris * Copyright (C) 2014 Satoshi Noguchi
3*a248dafdSChristopher Ferris * Copyright (C) 2014 Synaptics Inc
4*a248dafdSChristopher Ferris *
5*a248dafdSChristopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
6*a248dafdSChristopher Ferris * you may not use this file except in compliance with the License.
7*a248dafdSChristopher Ferris * You may obtain a copy of the License at
8*a248dafdSChristopher Ferris *
9*a248dafdSChristopher Ferris * http://www.apache.org/licenses/LICENSE-2.0
10*a248dafdSChristopher Ferris *
11*a248dafdSChristopher Ferris * Unless required by applicable law or agreed to in writing, software
12*a248dafdSChristopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
13*a248dafdSChristopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*a248dafdSChristopher Ferris * See the License for the specific language governing permissions and
15*a248dafdSChristopher Ferris * limitations under the License.
16*a248dafdSChristopher Ferris */
17*a248dafdSChristopher Ferris
18*a248dafdSChristopher Ferris #include <stdio.h>
19*a248dafdSChristopher Ferris #include <sys/ioctl.h>
20*a248dafdSChristopher Ferris #include <termios.h>
21*a248dafdSChristopher Ferris #include <unistd.h>
22*a248dafdSChristopher Ferris #include <string.h>
23*a248dafdSChristopher Ferris
24*a248dafdSChristopher Ferris #include "display.h"
25*a248dafdSChristopher Ferris
26*a248dafdSChristopher Ferris #define ESC 0x1B
27*a248dafdSChristopher Ferris
28*a248dafdSChristopher Ferris // default display
Output(const char * buf)29*a248dafdSChristopher Ferris void Display::Output(const char * buf)
30*a248dafdSChristopher Ferris {
31*a248dafdSChristopher Ferris printf("%s", buf);
32*a248dafdSChristopher Ferris }
33*a248dafdSChristopher Ferris
34*a248dafdSChristopher Ferris // ansi console
AnsiConsole()35*a248dafdSChristopher Ferris AnsiConsole::AnsiConsole() : Display()
36*a248dafdSChristopher Ferris {
37*a248dafdSChristopher Ferris m_buf = NULL;
38*a248dafdSChristopher Ferris GetWindowSize();
39*a248dafdSChristopher Ferris m_curX = 0;
40*a248dafdSChristopher Ferris m_curY = 0;
41*a248dafdSChristopher Ferris m_maxCurX = 0;
42*a248dafdSChristopher Ferris m_maxCurY = 0;
43*a248dafdSChristopher Ferris }
44*a248dafdSChristopher Ferris
~AnsiConsole()45*a248dafdSChristopher Ferris AnsiConsole::~AnsiConsole()
46*a248dafdSChristopher Ferris {
47*a248dafdSChristopher Ferris delete [] m_buf;
48*a248dafdSChristopher Ferris }
49*a248dafdSChristopher Ferris
GetWindowSize()50*a248dafdSChristopher Ferris void AnsiConsole::GetWindowSize()
51*a248dafdSChristopher Ferris {
52*a248dafdSChristopher Ferris struct winsize winsz;
53*a248dafdSChristopher Ferris ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsz);
54*a248dafdSChristopher Ferris if (m_numRows != winsz.ws_row || m_numCols != winsz.ws_col)
55*a248dafdSChristopher Ferris {
56*a248dafdSChristopher Ferris m_numRows = winsz.ws_row;
57*a248dafdSChristopher Ferris m_numCols = winsz.ws_col;
58*a248dafdSChristopher Ferris if (m_buf != NULL)
59*a248dafdSChristopher Ferris {
60*a248dafdSChristopher Ferris delete [] m_buf;
61*a248dafdSChristopher Ferris }
62*a248dafdSChristopher Ferris m_buf = new char[m_numRows * m_numCols];
63*a248dafdSChristopher Ferris
64*a248dafdSChristopher Ferris Clear();
65*a248dafdSChristopher Ferris }
66*a248dafdSChristopher Ferris }
67*a248dafdSChristopher Ferris
Output(const char * buf)68*a248dafdSChristopher Ferris void AnsiConsole::Output(const char * buf)
69*a248dafdSChristopher Ferris {
70*a248dafdSChristopher Ferris char * p;
71*a248dafdSChristopher Ferris
72*a248dafdSChristopher Ferris while (m_curY < m_numRows &&
73*a248dafdSChristopher Ferris m_numCols * m_curY + m_curX < m_numRows * m_numCols)
74*a248dafdSChristopher Ferris {
75*a248dafdSChristopher Ferris p = &(m_buf[m_numCols * m_curY + m_curX]);
76*a248dafdSChristopher Ferris
77*a248dafdSChristopher Ferris if (*buf == '\0')
78*a248dafdSChristopher Ferris {
79*a248dafdSChristopher Ferris break;
80*a248dafdSChristopher Ferris }
81*a248dafdSChristopher Ferris else if (*buf == '\n')
82*a248dafdSChristopher Ferris {
83*a248dafdSChristopher Ferris memset(p, ' ', m_numCols - m_curX);
84*a248dafdSChristopher Ferris m_curX = 0;
85*a248dafdSChristopher Ferris m_curY++;
86*a248dafdSChristopher Ferris }
87*a248dafdSChristopher Ferris else if (m_curX < m_numCols)
88*a248dafdSChristopher Ferris {
89*a248dafdSChristopher Ferris *p = *buf;
90*a248dafdSChristopher Ferris m_curX++;
91*a248dafdSChristopher Ferris }
92*a248dafdSChristopher Ferris buf++;
93*a248dafdSChristopher Ferris
94*a248dafdSChristopher Ferris if (m_maxCurX < m_curX) m_maxCurX = m_curX;
95*a248dafdSChristopher Ferris if (m_maxCurY < m_curY) m_maxCurY = m_curY;
96*a248dafdSChristopher Ferris }
97*a248dafdSChristopher Ferris }
98*a248dafdSChristopher Ferris
Clear()99*a248dafdSChristopher Ferris void AnsiConsole::Clear()
100*a248dafdSChristopher Ferris {
101*a248dafdSChristopher Ferris printf("%c[2J", ESC);
102*a248dafdSChristopher Ferris }
103*a248dafdSChristopher Ferris
Reflesh()104*a248dafdSChristopher Ferris void AnsiConsole::Reflesh()
105*a248dafdSChristopher Ferris {
106*a248dafdSChristopher Ferris int i;
107*a248dafdSChristopher Ferris int j;
108*a248dafdSChristopher Ferris char * p;
109*a248dafdSChristopher Ferris
110*a248dafdSChristopher Ferris printf("%c[%d;%dH", ESC, 0, 0);
111*a248dafdSChristopher Ferris
112*a248dafdSChristopher Ferris for (j = 0; j < m_maxCurY; j++)
113*a248dafdSChristopher Ferris {
114*a248dafdSChristopher Ferris p = &(m_buf[m_numCols * j]);
115*a248dafdSChristopher Ferris
116*a248dafdSChristopher Ferris for (i = 0; i < m_maxCurX; i++)
117*a248dafdSChristopher Ferris {
118*a248dafdSChristopher Ferris putc(*p, stdout);
119*a248dafdSChristopher Ferris p++;
120*a248dafdSChristopher Ferris }
121*a248dafdSChristopher Ferris
122*a248dafdSChristopher Ferris putc('\n', stdout);
123*a248dafdSChristopher Ferris }
124*a248dafdSChristopher Ferris
125*a248dafdSChristopher Ferris GetWindowSize();
126*a248dafdSChristopher Ferris m_curX = 0;
127*a248dafdSChristopher Ferris m_curY = 0;
128*a248dafdSChristopher Ferris m_maxCurX = 0;
129*a248dafdSChristopher Ferris m_maxCurY = 0;
130*a248dafdSChristopher Ferris }
131