~ubuntu-branches/ubuntu/wily/ecasound/wily-proposed

« back to all changes in this revision

Viewing changes to ecasound/eca-curses.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-05-12 17:58:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110512175803-zy3lodjecabt9r3v
Tags: upstream-2.8.0
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// eca-curses.cpp: Curses implementation of the console user interface.
 
3
// Copyright (C) 1999-2004,2007 Kai Vehmanen
 
4
//
 
5
// Attributes:
 
6
//     eca-style-version: 2
 
7
//
 
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.
 
12
// 
 
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.
 
17
// 
 
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
// ------------------------------------------------------------------------
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include <cstdlib>
 
28
#include <iostream>
 
29
#include <map>
 
30
#include <string>
 
31
 
 
32
#if defined(ECA_PLATFORM_CURSES) 
 
33
 
 
34
#ifdef ECA_USE_NCURSES_H
 
35
#include <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() */
 
40
#else
 
41
#include <curses.h>
 
42
#include <term.h> /* for setupterm() */
 
43
#endif
 
44
 
 
45
#define READLINE_LIBRARY
 
46
#include <readline.h>
 
47
#include <history.h>
 
48
 
 
49
#include <eca-iamode-parser.h>
 
50
#include <eca-version.h>
 
51
#include <kvu_utils.h> /* kvu_string_search_and_replace() */
 
52
 
 
53
#include "ecasound.h"
 
54
#include "eca-curses.h"
 
55
 
 
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);
 
59
#else 
 
60
static char** ecasound_completion (char *text, int start, int end);
 
61
static char* ecasound_command_generator (char* text, int state);
 
62
#endif
 
63
 
 
64
ECA_CURSES::ECA_CURSES(void)
 
65
{
 
66
  rl_initialized_rep = false;
 
67
  setupterm((char *)0, 1, (int *)0);
 
68
  init_readline_support();
 
69
}
 
70
 
 
71
ECA_CURSES::~ECA_CURSES(void)
 
72
{
 
73
  if (rl_initialized_rep == true) {
 
74
    rl_cleanup_after_signal();
 
75
  }
 
76
}
 
77
 
 
78
void ECA_CURSES::print(const std::string& msg)
 
79
{
 
80
  std::cout << msg << std::endl;
 
81
}
 
82
 
 
83
void ECA_CURSES::print_banner(void)
 
84
{
 
85
  int width = COLS - 4;
 
86
  if (width > ECASOUND_TERM_WIDTH_DEFAULT)
 
87
    width = ECASOUND_TERM_WIDTH_DEFAULT;
 
88
  string banner (width, '*');
 
89
  std::cout << banner << std::endl;
 
90
  std::cout << "*";
 
91
  putp(tigetstr("bold"));
 
92
  std::cout << "        ecasound v" 
 
93
       << ecasound_library_version
 
94
       << ECASOUND_COPYRIGHT;
 
95
  putp(tigetstr("sgr0"));
 
96
  std::cout << "\n";
 
97
  std::cout << banner << std::endl;
 
98
}
 
99
 
 
100
void ECA_CURSES::read_command(const std::string& prompt)
 
101
{
 
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);
 
108
  }
 
109
  else {
 
110
    /* handle EOF */
 
111
    last_cmd_rep = "q";
 
112
  }
 
113
}
 
114
 
 
115
const std::string& ECA_CURSES::last_command(void) const
 
116
{
 
117
  return last_cmd_rep;
 
118
}
 
119
 
 
120
void ECA_CURSES::init_readline_support(void)
 
121
{
 
122
  /* for conditional parsing of ~/.inputrc file. */
 
123
  rl_readline_name = "ecasound";
 
124
 
 
125
  /* we want to attempt completion first */
 
126
#if RL_READLINE_VERSION >= 0x0402
 
127
  rl_attempted_completion_function = (rl_completion_func_t*)ecasound_completion;
 
128
#else
 
129
  rl_attempted_completion_function = (CPPFunction *)ecasound_completion;
 
130
#endif
 
131
}
 
132
 
 
133
/* **************************************************************** */
 
134
/*                                                                  */
 
135
/*                  Interface to Readline Completion                */
 
136
/*                                                                  */
 
137
/* **************************************************************** */
 
138
 
 
139
/**
 
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.
 
145
 */
 
146
#if RL_READLINE_VERSION >= 0x0402
 
147
char** ecasound_completion (const char *text, int start, int end)
 
148
#else
 
149
char** ecasound_completion (char *text, int start, int end)
 
150
#endif
 
151
{
 
152
  char **matches;
 
153
  matches = (char **)NULL;
 
154
 
 
155
  /* complete only the first command, otherwise complete files in 
 
156
   * the current directory */
 
157
  if (start == 0) {
 
158
#if RL_READLINE_VERSION >= 0x0402
 
159
    matches = rl_completion_matches (text, (rl_compentry_func_t *)ecasound_command_generator);
 
160
#else
 
161
    matches = completion_matches (text, (CPFunction *)ecasound_command_generator);
 
162
#endif
 
163
  }
 
164
  return matches;
 
165
}
 
166
 
 
167
/**
 
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.
 
171
 */
 
172
#if RL_READLINE_VERSION >= 0x0402
 
173
char* ecasound_command_generator (const char* text, int state)
 
174
#else
 
175
char* ecasound_command_generator (char* text, int state)
 
176
#endif
 
177
{
 
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;
 
182
 
 
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
 
185
   * variable to 0
 
186
   */
 
187
  if (!state) {
 
188
      list_index = 0;
 
189
      p = map_ref.begin();
 
190
      len = strlen (text);
 
191
  }
 
192
 
 
193
  /* Return the next name which partially matches from the command list */
 
194
  while (p != map_ref.end()) {
 
195
      cmd = p->first;
 
196
      list_index++;
 
197
      ++p;
 
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());
 
202
        }
 
203
      }
 
204
  }
 
205
  return (char *)0;
 
206
}
 
207
 
 
208
#else
 
209
 
 
210
#include "eca-curses.h"
 
211
 
 
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) {}
 
219
 
 
220
#endif /* ECA_PLATFORM_CURSES */