lnav/src/vt52_curses.cc

277 lines
7.9 KiB
C++
Raw Normal View History

2009-09-14 01:07:32 +00:00
/**
2013-05-03 06:02:03 +00:00
* Copyright (c) 2007-2012, Timothy Stack
*
* All rights reserved.
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHAN`TABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2013-05-03 06:02:03 +00:00
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
2009-09-14 01:07:32 +00:00
* @file vt52_curses.cc
*/
#include "config.h"
#include <unistd.h>
2009-10-14 19:42:58 +00:00
#include <string.h>
2009-09-14 01:07:32 +00:00
#include <map>
#include "lnav_log.hh"
2009-09-14 01:07:32 +00:00
#include "vt52_curses.hh"
2012-04-19 02:30:53 +00:00
#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
2009-09-14 01:07:32 +00:00
using namespace std;
/**
* Singleton used to hold the mapping of ncurses keycodes to VT52 escape
* sequences.
*/
class vt52_escape_map {
public:
/** @return The singleton. */
static vt52_escape_map &singleton()
{
2013-05-28 04:35:00 +00:00
static vt52_escape_map s_vem;
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
return s_vem;
2009-09-14 01:07:32 +00:00
};
/**
* @param ch The ncurses keycode.
* @return The null terminated VT52 escape sequence.
*/
const char *operator[](int ch) const
{
2013-05-28 04:35:00 +00:00
map<int, const char *>::const_iterator iter;
const char *retval = NULL;
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
if ((iter = this->vem_map.find(ch)) != this->vem_map.end()) {
retval = iter->second;
}
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
return retval;
2009-09-14 01:07:32 +00:00
};
const char *operator[](const char *seq) const
{
2013-05-28 04:35:00 +00:00
map<string, const char *>::const_iterator iter;
const char *retval = NULL;
2009-09-14 01:07:32 +00:00
require(seq != NULL);
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
if ((iter = this->vem_input_map.find(seq)) !=
this->vem_input_map.end()) {
retval = iter->second;
}
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
return retval;
2009-09-14 01:07:32 +00:00
};
private:
/** Construct the map with a few escape sequences. */
vt52_escape_map()
{
2013-05-28 04:35:00 +00:00
static char area_buffer[1024];
char * area = area_buffer;
if (tgetent(NULL, "vt52") == ERR) {
perror("tgetent");
}
this->vem_map[KEY_UP] = tgetstr((char *)"ku", &area);
this->vem_map[KEY_DOWN] = tgetstr((char *)"kd", &area);
this->vem_map[KEY_RIGHT] = tgetstr((char *)"kr", &area);
this->vem_map[KEY_LEFT] = tgetstr((char *)"kl", &area);
this->vem_map[KEY_HOME] = tgetstr((char *)"kh", &area);
if (this->vem_map[KEY_HOME] == NULL) {
this->vem_map[KEY_HOME] = "\x01";
}
2013-05-28 04:35:00 +00:00
this->vem_map[KEY_BACKSPACE] = "\010";
this->vem_map[KEY_DC] = "\x04";
2013-05-28 04:35:00 +00:00
this->vem_map[KEY_BEG] = "\x01";
this->vem_map[KEY_END] = "\x05";
2013-05-28 04:35:00 +00:00
this->vem_map[KEY_SLEFT] = tgetstr((char *)"#4", &area);
if (this->vem_map[KEY_SLEFT] == NULL) {
this->vem_map[KEY_SLEFT] = "\033b";
}
this->vem_map[KEY_SRIGHT] = tgetstr((char *)"%i", &area);
if (this->vem_map[KEY_SRIGHT] == NULL) {
this->vem_map[KEY_SRIGHT] = "\033f";
}
this->vem_input_map[tgetstr((char *)"ce", &area)] = "ce";
this->vem_input_map[tgetstr((char *)"kl", &area)] = "kl";
this->vem_input_map[tgetstr((char *)"kr", &area)] = "kr";
tgetent(NULL, getenv("TERM"));
2009-09-14 01:07:32 +00:00
};
/** Map of ncurses keycodes to VT52 escape sequences. */
2013-05-28 04:35:00 +00:00
map<int, const char *> vem_map;
map<string, const char *> vem_input_map;
2009-09-14 01:07:32 +00:00
};
vt52_curses::vt52_curses()
: vc_window(NULL),
vc_x(0),
vc_y(0),
2013-04-17 16:27:12 +00:00
vc_max_height(0),
vc_escape_len(0),
vc_map_buffer(0)
2009-09-14 01:07:32 +00:00
{ }
vt52_curses::~vt52_curses()
{ }
const char *vt52_curses::map_input(int ch, int &len_out)
{
const char *esc, *retval;
/* Check for an escape sequence, otherwise just return the char. */
if ((esc = vt52_escape_map::singleton()[ch]) != NULL) {
2013-05-28 04:35:00 +00:00
retval = esc;
len_out = strlen(retval);
2009-09-14 01:07:32 +00:00
}
else {
switch (ch) {
case 0x7f:
ch = BACKSPACE;
break;
case KEY_BTAB:
ch = BACKTAB;
break;
}
2013-05-28 04:35:00 +00:00
this->vc_map_buffer = (char)ch;
retval = &this->vc_map_buffer; /* XXX probably shouldn't do this. */
len_out = 1;
2009-09-14 01:07:32 +00:00
}
ensure(retval != NULL);
ensure(len_out > 0);
2009-09-14 01:07:32 +00:00
return retval;
}
void vt52_curses::map_output(const char *output, int len)
{
int lpc;
2009-09-14 01:07:32 +00:00
require(this->vc_window != nullptr);
2009-09-14 01:07:32 +00:00
for (lpc = 0; lpc < len; lpc++) {
2013-05-28 04:35:00 +00:00
if (this->vc_escape_len > 0) {
const char *cap;
this->vc_escape[this->vc_escape_len] = output[lpc];
this->vc_escape_len += 1;
this->vc_escape[this->vc_escape_len] = '\0';
if ((cap = vt52_escape_map::singleton()[this->vc_escape]) !=
nullptr) {
2013-05-28 04:35:00 +00:00
if (strcmp(cap, "ce") == 0) {
this->vc_line.erase(this->vc_x);
2013-05-28 04:35:00 +00:00
this->vc_escape_len = 0;
}
else if (strcmp(cap, "kl") == 0) {
this->vc_x -= 1;
this->vc_escape_len = 0;
}
else if (strcmp(cap, "kr") == 0) {
this->vc_x += 1;
this->vc_escape_len = 0;
}
else {
ensure(0);
2013-05-28 04:35:00 +00:00
}
}
}
else {
switch (output[lpc]) {
case STX:
this->vc_x = 0;
this->vc_line.clear();
2013-05-28 04:35:00 +00:00
break;
case BELL:
flash();
break;
case BACKSPACE:
this->vc_x -= 1;
break;
case ESCAPE:
this->vc_escape[0] = ESCAPE;
2013-05-28 04:35:00 +00:00
this->vc_escape_len = 1;
break;
case '\n':
this->vc_x = 0;
this->vc_line.clear();
break;
2013-05-28 04:35:00 +00:00
case '\r':
this->vc_x = 0;
break;
default:
if (this->vc_x < this->vc_line.length()) {
this->vc_line.get_string()[this->vc_x] = output[lpc];
} else {
this->vc_line.append(1, output[lpc]);
}
2013-05-28 04:35:00 +00:00
this->vc_x += 1;
break;
}
}
2009-09-14 01:07:32 +00:00
}
}
void vt52_curses::do_update()
2009-09-14 01:07:32 +00:00
{
view_curses::mvwattrline(this->vc_window,
this->get_actual_y(), this->vc_left,
this->vc_line,
line_range{ 0, (int) this->vc_width });
2009-09-14 01:07:32 +00:00
}