1
// ------------------------------------------------------------------------
2
// eca-curses.cpp: Curses implementation of the console user interface.
3
// Copyright (C) 1999-2004,2007 Kai Vehmanen
6
// eca-style-version: 2
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 2 of the License, or
11
// (at your option) any later version.
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
// GNU General Public License for more details.
18
// You should have received a copy of the GNU General Public License
19
// along with this program; if not, write to the Free Software
20
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
// ------------------------------------------------------------------------
32
#if defined(ECA_PLATFORM_CURSES)
34
#ifdef ECA_USE_NCURSES_H
36
#include <term.h> /* for setupterm() */
37
#elif ECA_USE_NCURSES_NCURSES_H
38
#include <ncurses/ncurses.h>
39
#include <ncurses/term.h> /* for setupterm() */
42
#include <term.h> /* for setupterm() */
45
#define READLINE_LIBRARY
49
#include <eca-iamode-parser.h>
50
#include <eca-version.h>
51
#include <kvu_utils.h> /* kvu_string_search_and_replace() */
54
#include "eca-curses.h"
56
#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0402
57
static char** ecasound_completion (const char *text, int start, int end);
58
static char* ecasound_command_generator (const char* text, int state);
60
static char** ecasound_completion (char *text, int start, int end);
61
static char* ecasound_command_generator (char* text, int state);
64
ECA_CURSES::ECA_CURSES(void)
66
rl_initialized_rep = false;
67
setupterm((char *)0, 1, (int *)0);
68
init_readline_support();
71
ECA_CURSES::~ECA_CURSES(void)
73
if (rl_initialized_rep == true) {
74
rl_cleanup_after_signal();
78
void ECA_CURSES::print(const std::string& msg)
80
std::cout << msg << std::endl;
83
void ECA_CURSES::print_banner(void)
86
if (width > ECASOUND_TERM_WIDTH_DEFAULT)
87
width = ECASOUND_TERM_WIDTH_DEFAULT;
88
string banner (width, '*');
89
std::cout << banner << std::endl;
91
putp(tigetstr("bold"));
92
std::cout << " ecasound v"
93
<< ecasound_library_version
94
<< ECASOUND_COPYRIGHT;
95
putp(tigetstr("sgr0"));
97
std::cout << banner << std::endl;
100
void ECA_CURSES::read_command(const std::string& prompt)
102
if (rl_initialized_rep != true) rl_initialized_rep = true;
103
last_cmdchar_repp = readline(const_cast<char*>(prompt.c_str()));
104
if (last_cmdchar_repp != 0) {
105
add_history(last_cmdchar_repp);
106
last_cmd_rep = last_cmdchar_repp;
107
free(last_cmdchar_repp);
115
const std::string& ECA_CURSES::last_command(void) const
120
void ECA_CURSES::init_readline_support(void)
122
/* for conditional parsing of ~/.inputrc file. */
123
rl_readline_name = "ecasound";
125
/* we want to attempt completion first */
126
#if RL_READLINE_VERSION >= 0x0402
127
rl_attempted_completion_function = (rl_completion_func_t*)ecasound_completion;
129
rl_attempted_completion_function = (CPPFunction *)ecasound_completion;
133
/* **************************************************************** */
135
/* Interface to Readline Completion */
137
/* **************************************************************** */
140
* Attempt to complete on the contents of TEXT. START and END bound the
141
* region of rl_line_buffer that contains the word to complete. TEXT is
142
* the word to complete. We can use the entire contents of rl_line_buffer
143
* in case we want to do some simple parsing. Return the array of matches,
144
* or NULL if there aren't any.
146
#if RL_READLINE_VERSION >= 0x0402
147
char** ecasound_completion (const char *text, int start, int end)
149
char** ecasound_completion (char *text, int start, int end)
153
matches = (char **)NULL;
155
/* complete only the first command, otherwise complete files in
156
* the current directory */
158
#if RL_READLINE_VERSION >= 0x0402
159
matches = rl_completion_matches (text, (rl_compentry_func_t *)ecasound_command_generator);
161
matches = completion_matches (text, (CPFunction *)ecasound_command_generator);
168
* Generator function for command completion. STATE lets us know whether
169
* to start from scratch; without any state (i.e. STATE == 0), then we
170
* start at the top of the list.
172
#if RL_READLINE_VERSION >= 0x0402
173
char* ecasound_command_generator (const char* text, int state)
175
char* ecasound_command_generator (char* text, int state)
178
static int list_index, len;
179
static const std::map<std::string,int>& map_ref = ECA_IAMODE_PARSER::registered_commands();
180
static std::map<std::string,int>::const_iterator p;
181
static std::string cmd;
183
/* If this is a new word to complete, initialize now. This includes
184
* saving the length of TEXT for efficiency, and initializing the index
193
/* Return the next name which partially matches from the command list */
194
while (p != map_ref.end()) {
198
if (p != map_ref.end()) {
199
std::string hyphenstr = kvu_string_search_and_replace(text, '_', '-');
200
if (strncmp(hyphenstr.c_str(), cmd.c_str(), len) == 0) {
201
return strdup(cmd.c_str());
210
#include "eca-curses.h"
212
ECA_CURSES::ECA_CURSES(void) {}
213
ECA_CURSES::~ECA_CURSES(void) {}
214
void ECA_CURSES::print(const std::string& msg) {}
215
void ECA_CURSES::print_banner(void) {}
216
void ECA_CURSES::read_command(const std::string& prompt) {}
217
const std::string& ECA_CURSES::last_command(void) const { static std::string empty; return empty; }
218
void ECA_CURSES::init_readline_support(void) {}
220
#endif /* ECA_PLATFORM_CURSES */