quakeforge/libs/console/inputline.c

165 lines
4.1 KiB
C
Raw Normal View History

/*
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
$Id$
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#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"
#include "QF/keys.h"
struct inputline_s *
Con_CreateInputLine (int lines, int width, char prompt)
{
int size;
inputline_t *inputline;
char **p;
char *l;
size = sizeof (inputline_t); // space for the header
size += sizeof (char *[lines]); // space for the line pointers
size += lines * width; // space for the lines themselves
inputline = calloc (1, size);
p = (char**)(inputline + 1);
l = (char*)&p[lines];
inputline->num_lines = lines;
inputline->line_width = width;
while (lines--) {
*p++ = l;
l += width;
}
inputline->prompt_char = prompt;
return inputline;
}
void
Con_DestroyInputLine (inputline_t *inputline)
{
free (inputline);
}
void
Con_ProcessInput (inputline_t *il, int ch)
{
int i;
switch (ch) {
case K_ENTER:
if (il->enter)
il->enter (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;
il->lines[il->edit_line][1] = 0;
il->linepos = 1;
break;
case K_TAB:
if (il->complete)
il->complete (il);
break;
case K_BACKSPACE:
if (il->linepos > 1) {
strcpy (il->lines[il->edit_line] + il->linepos - 1,
il->lines[il->edit_line] + il->linepos);
il->linepos--;
}
break;
case K_DEL:
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);
break;
case K_RIGHTARROW:
if (il->linepos < strlen (il->lines[il->edit_line]))
il->linepos++;
break;
case K_LEFTARROW:
if (il->linepos > 1)
il->linepos--;
break;
case K_UPARROW:
do {
il->history_line = (il->history_line - 1) % il->num_lines;
} while (il->history_line != il->edit_line
&& !il->lines[il->history_line][1]);
if (il->history_line == il->edit_line)
il->history_line = (il->edit_line + 1) % il->num_lines;
strcpy (il->lines[il->edit_line], il->lines[il->history_line]);
il->linepos = strlen (il->lines[il->edit_line]);
break;
case K_DOWNARROW:
if (il->history_line == il->edit_line)
break;
do {
il->history_line = (il->history_line + 1) % il->num_lines;
} while (il->history_line != il->edit_line
&& !il->lines[il->history_line][1]);
if (il->history_line == il->edit_line) {
il->lines[il->edit_line][0] = ']';
il->lines[il->edit_line][1] = 0;
il->linepos = 1;
} else {
strcpy (il->lines[il->edit_line], il->lines[il->history_line]);
il->linepos = strlen (il->lines[il->edit_line]);
}
break;
case K_HOME:
il->linepos = 1;
break;
case K_END:
il->linepos = strlen (il->lines[il->edit_line]);
break;
default:
if (ch >= ' ' && ch < 256 && ch != 127) {
i = strlen (il->lines[il->edit_line]);
if (i >= il->line_width - 1)
break;
// This also moves the ending \0
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++;
}
break;
}
}