2001-07-16 22:30:11 +00:00
|
|
|
/*
|
|
|
|
sv_console.c
|
|
|
|
|
|
|
|
ncurses console for the server
|
|
|
|
|
|
|
|
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2001/7/10
|
|
|
|
|
|
|
|
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; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-07-16 22:30:11 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/console.h"
|
2001-07-17 16:46:51 +00:00
|
|
|
#include "QF/keys.h"
|
2001-07-16 22:30:11 +00:00
|
|
|
|
2001-09-26 16:31:36 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
__attribute__ ((visibility ("default")))
|
2001-07-17 16:46:51 +00:00
|
|
|
struct inputline_s *
|
2002-01-30 21:23:46 +00:00
|
|
|
Con_CreateInputLine (int lines, int lsize, char prompt)
|
2001-07-17 16:46:51 +00:00
|
|
|
{
|
2001-09-10 12:56:23 +00:00
|
|
|
char *l, **p;
|
|
|
|
int size;
|
|
|
|
inputline_t *inputline;
|
2001-09-20 08:12:28 +00:00
|
|
|
int i;
|
2001-07-17 16:46:51 +00:00
|
|
|
|
|
|
|
size = sizeof (inputline_t); // space for the header
|
2007-03-22 23:20:57 +00:00
|
|
|
size += sizeof(char*) * lines; // space for the line pointers
|
2002-01-30 21:23:46 +00:00
|
|
|
size += lines * lsize; // space for the lines themselves
|
2001-07-17 16:46:51 +00:00
|
|
|
|
|
|
|
inputline = calloc (1, size);
|
|
|
|
p = (char**)(inputline + 1);
|
|
|
|
l = (char*)&p[lines];
|
|
|
|
|
2001-09-16 05:41:28 +00:00
|
|
|
inputline->lines = p;
|
2001-07-17 16:46:51 +00:00
|
|
|
inputline->num_lines = lines;
|
2002-01-30 21:23:46 +00:00
|
|
|
inputline->line_size = lsize;
|
2001-07-17 16:46:51 +00:00
|
|
|
while (lines--) {
|
|
|
|
*p++ = l;
|
2002-01-30 21:23:46 +00:00
|
|
|
l += lsize;
|
2001-07-17 16:46:51 +00:00
|
|
|
}
|
|
|
|
inputline->prompt_char = prompt;
|
2001-09-16 05:41:28 +00:00
|
|
|
|
2001-09-20 08:12:28 +00:00
|
|
|
for (i = 0; i < inputline->num_lines; i++)
|
|
|
|
inputline->lines[i][0] = prompt;
|
2001-09-16 05:41:28 +00:00
|
|
|
inputline->linepos = 1;
|
2001-07-17 16:46:51 +00:00
|
|
|
return inputline;
|
|
|
|
}
|
2001-07-16 22:30:11 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
__attribute__ ((visibility ("default")))
|
2001-07-17 16:46:51 +00:00
|
|
|
void
|
|
|
|
Con_DestroyInputLine (inputline_t *inputline)
|
|
|
|
{
|
|
|
|
free (inputline);
|
|
|
|
}
|
2001-07-16 22:30:11 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
__attribute__ ((visibility ("default")))
|
2002-01-16 21:53:42 +00:00
|
|
|
void
|
2002-08-28 16:02:43 +00:00
|
|
|
Con_ClearTyping (inputline_t *il, int save)
|
2002-01-16 21:53:42 +00:00
|
|
|
{
|
2002-08-28 16:02:43 +00:00
|
|
|
if (save && il->lines[il->edit_line][1]) {
|
|
|
|
il->edit_line = (il->edit_line + 1) % il->num_lines;
|
|
|
|
il->history_line = il->edit_line;
|
|
|
|
}
|
|
|
|
il->lines[il->edit_line][0] = il->prompt_char;
|
2002-01-16 21:53:42 +00:00
|
|
|
il->lines[il->edit_line][1] = 0;
|
|
|
|
il->linepos = 1;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
__attribute__ ((visibility ("default")))
|
2001-07-16 22:30:11 +00:00
|
|
|
void
|
2001-09-16 05:41:28 +00:00
|
|
|
Con_ProcessInputLine (inputline_t *il, int ch)
|
2001-07-16 22:30:11 +00:00
|
|
|
{
|
2003-04-17 00:01:48 +00:00
|
|
|
size_t i;
|
2001-09-26 16:31:36 +00:00
|
|
|
char *text;
|
2001-07-16 22:30:11 +00:00
|
|
|
|
|
|
|
switch (ch) {
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_RETURN:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->enter)
|
|
|
|
il->enter (il->lines[il->edit_line] + 1);
|
2002-08-28 16:02:43 +00:00
|
|
|
Con_ClearTyping (il, 1);
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_TAB:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->complete)
|
|
|
|
il->complete (il);
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_BACKSPACE:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->linepos > 1) {
|
|
|
|
strcpy (il->lines[il->edit_line] + il->linepos - 1,
|
|
|
|
il->lines[il->edit_line] + il->linepos);
|
|
|
|
il->linepos--;
|
2001-07-16 22:30:11 +00:00
|
|
|
}
|
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_DELETE:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->linepos < strlen (il->lines[il->edit_line]))
|
|
|
|
strcpy (il->lines[il->edit_line] + il->linepos,
|
|
|
|
il->lines[il->edit_line] + il->linepos + 1);
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_RIGHT:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->linepos < strlen (il->lines[il->edit_line]))
|
|
|
|
il->linepos++;
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_LEFT:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->linepos > 1)
|
|
|
|
il->linepos--;
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_UP:
|
2001-09-20 15:14:44 +00:00
|
|
|
{
|
|
|
|
int j = (il->history_line + il->num_lines - 1) % il->num_lines;
|
|
|
|
if (j == il->edit_line || !il->lines[j][1])
|
2001-09-23 00:36:21 +00:00
|
|
|
break; // don't let it wrap
|
2001-09-20 15:14:44 +00:00
|
|
|
il->history_line = j;
|
|
|
|
}
|
2001-07-17 16:46:51 +00:00
|
|
|
strcpy (il->lines[il->edit_line], il->lines[il->history_line]);
|
|
|
|
il->linepos = strlen (il->lines[il->edit_line]);
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_DOWN:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->history_line == il->edit_line)
|
2001-09-23 00:36:21 +00:00
|
|
|
break; // don't let it wrap
|
2001-09-20 15:14:44 +00:00
|
|
|
il->history_line = (il->history_line + 1) % il->num_lines;
|
2001-07-17 16:46:51 +00:00
|
|
|
if (il->history_line == il->edit_line) {
|
2002-01-16 23:06:28 +00:00
|
|
|
il->lines[il->edit_line][0] = il->prompt_char;
|
2001-07-17 16:46:51 +00:00
|
|
|
il->lines[il->edit_line][1] = 0;
|
|
|
|
il->linepos = 1;
|
2001-07-16 22:30:11 +00:00
|
|
|
} else {
|
2001-07-17 16:46:51 +00:00
|
|
|
strcpy (il->lines[il->edit_line], il->lines[il->history_line]);
|
|
|
|
il->linepos = strlen (il->lines[il->edit_line]);
|
2001-07-16 22:30:11 +00:00
|
|
|
}
|
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_HOME:
|
2001-07-17 16:46:51 +00:00
|
|
|
il->linepos = 1;
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
2001-10-28 04:23:37 +00:00
|
|
|
case QFK_END:
|
2001-07-17 16:46:51 +00:00
|
|
|
il->linepos = strlen (il->lines[il->edit_line]);
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-07-17 16:46:51 +00:00
|
|
|
if (ch >= ' ' && ch < 256 && ch != 127) {
|
|
|
|
i = strlen (il->lines[il->edit_line]);
|
2001-09-26 16:31:36 +00:00
|
|
|
if (i >= il->line_size - 1)
|
2001-07-16 22:30:11 +00:00
|
|
|
break;
|
|
|
|
// This also moves the ending \0
|
2001-07-17 16:46:51 +00:00
|
|
|
memmove (il->lines[il->edit_line] + il->linepos + 1,
|
|
|
|
il->lines[il->edit_line] + il->linepos,
|
|
|
|
i - il->linepos + 1);
|
|
|
|
il->lines[il->edit_line][il->linepos] = ch;
|
|
|
|
il->linepos++;
|
2001-07-16 22:30:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-09-26 16:31:36 +00:00
|
|
|
i = il->linepos - 1;
|
|
|
|
if (il->scroll > i)
|
|
|
|
il->scroll = i;
|
2003-04-17 02:03:47 +00:00
|
|
|
if (il->scroll + (il->width - 2) - 1 < i)
|
2001-09-26 16:31:36 +00:00
|
|
|
il->scroll = i - (il->width - 2) + 1;
|
|
|
|
text = il->lines[il->edit_line] + il->scroll;
|
2003-04-17 00:01:48 +00:00
|
|
|
if (strlen (text + 1) < il->width - 2) {
|
2001-09-26 16:31:36 +00:00
|
|
|
text = il->lines[il->edit_line];
|
2003-04-17 02:03:47 +00:00
|
|
|
if ((i = strlen (text + 1)) > (il->width - 2))
|
|
|
|
il->scroll = i - (il->width - 2);
|
|
|
|
else
|
|
|
|
il->scroll = 0;
|
2001-09-26 16:31:36 +00:00
|
|
|
il->scroll = max (il->scroll, 0);
|
|
|
|
}
|
|
|
|
if (il->draw)
|
|
|
|
il->draw (il);
|
2001-07-16 22:30:11 +00:00
|
|
|
}
|