ncurses console for the server. input handling is currently a nasty bit of

code duplication, but it seems to work nicely (except for cursor movement
keys) and even gives tab completion to the server :)
This commit is contained in:
Bill Currie 2001-07-10 22:36:22 +00:00
parent ecff96258f
commit f9f31ad4b3
7 changed files with 391 additions and 14 deletions

View file

@ -92,7 +92,7 @@ AC_HEADER_STDC
AC_HEADER_MAJOR
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(
arpa/inet.h asm/io.h assert.h conio.h ctype.h ddraw.h dinput.h \
arpa/inet.h asm/io.h assert.h conio.h ctype.h curses.h ddraw.h dinput.h \
direct.h dirent.h dlfcn.h dmedia/audio.h dmedia/cdaudio.h dpmi.h \
dsound.h errno.h fcntl.h fnmatch.h glide/sst1vid.h io.h \
libc.h limits.h linux/cdrom.h linux/joystick.h linux/soundcard.h \
@ -270,6 +270,15 @@ if test "x$ac_cv_func_dlopen" != "xyes"; then
fi
AC_SUBST(DL_LIBS)
AC_CHECK_LIB(ncurses, initscr,
CURSES_LIBS=-lncurses,
AC_CHECK_LIB(curses, initscr,
CURSES_LIBS=-ncurses,
CURSES_LIBS=
)
)
AC_SUBST(CURSES_LIBS)
dnl Checks for stricmp/strcasecmp
#AC_CHECK_FUNC(strcasecmp,
# ,

View file

@ -60,7 +60,9 @@ void Con_DrawCharacter (int cx, int line, int num);
void Con_CheckResize (void);
void Con_Init (void);
void Con_Shutdown (void);
void Con_Init_Cvars (void);
void Con_ProcessInput (void);
void Con_DrawConsole (int lines);
void Con_DrawDownload (int lines);
void Con_Print (char *txt);

View file

@ -74,7 +74,8 @@ else
syssv_SRC= sv_sys_unix.c
endif
server_SOURCES= sv_ccmds.c sv_cvar.c sv_ents.c sv_init.c sv_main.c sv_misc.c \
server_SOURCES= sv_ccmds.c sv_console.c sv_cvar.c sv_ents.c sv_init.c \
sv_main.c sv_misc.c \
sv_model.c sv_move.c sv_nchan.c sv_phys.c sv_pr_cmds.c \
sv_progs.c sv_send.c sv_user.c world.c $(syssv_SRC)
@ -83,7 +84,7 @@ qf_server_LIBS= $(top_builddir)/libs/models/libQFmodels.la \
$(top_builddir)/libs/util/libQFutil.la
qw_server_SOURCES= $(common_SOURCES) $(server_SOURCES)
qw_server_LDADD= -L. libqfnet.la $(ASM) $(qf_server_LIBS) $(NET_LIBS) $(DL_LIBS)
qw_server_LDADD= -L. libqfnet.la $(ASM) $(qf_server_LIBS) $(NET_LIBS) $(DL_LIBS) $(CURSES_LIBS)
qw_server_DEPENDENCIES= libqfnet.la $(ASM) $(qf_server_LIBS)
# Client builds

365
qw/source/sv_console.c Normal file
View file

@ -0,0 +1,365 @@
/*
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_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_CURSES_H
# include <curses.h>
#endif
#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/cvar.h"
#include "QF/qtypes.h"
#include "QF/sys.h"
static WINDOW *output;
static WINDOW *status;
static WINDOW *input;
static int screen_x, screen_y;
#define MAXCMDLINE 256
char key_lines[32][MAXCMDLINE];
int edit_line;
int history_line;
int key_linepos;
static chtype attr_table[4] = {
A_NORMAL,
COLOR_PAIR(1),
COLOR_PAIR(2),
COLOR_PAIR(3),
};
static const byte attr_map[256] = {
3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3, 3, 0, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3, 3, 0, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
void
Con_Init (void)
{
int i;
for (i = 0; i < 32; i++) {
key_lines[i][0] = ']';
key_lines[i][1] = 0;
}
key_linepos = 1;
initscr ();
start_color ();
cbreak ();
noecho ();
nonl ();
intrflush (stdscr, FALSE);
keypad (stdscr, TRUE);
getmaxyx (stdscr, screen_y, screen_x);
output = subwin (stdscr, screen_y - 2, screen_x, 0, 0);
status = subwin (stdscr, 1, screen_x, screen_y - 2, 0);
input = subwin (stdscr, 1, screen_x, screen_y - 1, 0);
init_pair (1, COLOR_YELLOW, COLOR_BLACK);
init_pair (2, COLOR_GREEN, COLOR_BLACK);
init_pair (3, COLOR_RED, COLOR_BLACK);
init_pair (4, COLOR_YELLOW, COLOR_BLUE);
scrollok (output, TRUE);
scrollok (status, FALSE);
scrollok (input, FALSE);
nodelay (input, TRUE);
wclear (output);
wbkgdset (status, COLOR_PAIR(4));
wclear (status);
wclear (input);
touchwin (stdscr);
wrefresh (output);
wrefresh (status);
wrefresh (input);
}
void
Con_Shutdown (void)
{
endwin ();
}
void
Con_Print (char *txt)
{
chtype ch;
while ((ch = *txt++)) {
ch = sys_char_map[ch] | attr_table[attr_map[ch]];
waddch (output, ch);
}
touchwin (stdscr);
wrefresh (output);
}
void
Con_ProcessInput (void)
{
int ch = wgetch (input);
int i;
char *text = 0;
switch (ch) {
case KEY_ENTER:
case '\n':
case '\r':
if (key_lines[edit_line][1] == '/'
&& key_lines[edit_line][2] == '/')
goto no_lf;
else if (key_lines[edit_line][1] == '\\'
|| key_lines[edit_line][1] == '/')
Cbuf_AddText (key_lines[edit_line] + 2);
else
Cbuf_AddText (key_lines[edit_line] + 1);
Cbuf_AddText ("\n");
no_lf:
Con_Printf ("%s\n", key_lines[edit_line]);
edit_line = (edit_line + 1) & 31;
history_line = edit_line;
key_lines[edit_line][0] = ']';
key_lines[edit_line][1] = 0;
key_linepos = 1;
break;
case '\t':
Con_CompleteCommandLine();
break;
case KEY_BACKSPACE:
case '\x7f':
if (key_linepos > 1) {
strcpy (key_lines[edit_line] + key_linepos - 1,
key_lines[edit_line] + key_linepos);
key_linepos--;
}
break;
case KEY_DC:
if (key_linepos < strlen (key_lines[edit_line]))
strcpy (key_lines[edit_line] + key_linepos,
key_lines[edit_line] + key_linepos + 1);
break;
case KEY_RIGHT:
if (key_linepos < strlen (key_lines[edit_line]))
key_linepos++;
break;
case KEY_LEFT:
if (key_linepos > 1)
key_linepos--;
break;
case KEY_UP:
do {
history_line = (history_line - 1) & 31;
} while (history_line != edit_line && !key_lines[history_line][1]);
if (history_line == edit_line)
history_line = (edit_line + 1) & 31;
strcpy (key_lines[edit_line], key_lines[history_line]);
key_linepos = strlen (key_lines[edit_line]);
break;
case KEY_DOWN:
if (history_line == edit_line)
break;
do {
history_line = (history_line + 1) & 31;
} while (history_line != edit_line && !key_lines[history_line][1]);
if (history_line == edit_line) {
key_lines[edit_line][0] = ']';
key_lines[edit_line][1] = 0;
key_linepos = 1;
} else {
strcpy (key_lines[edit_line], key_lines[history_line]);
key_linepos = strlen (key_lines[edit_line]);
}
break;
case KEY_PPAGE:
break;
case KEY_NPAGE:
break;
case KEY_HOME:
key_linepos = 1;
break;
case KEY_END:
key_linepos = strlen (key_lines[edit_line]);
break;
default:
if (ch >= ' ' && ch < 127) {
i = strlen (key_lines[edit_line]);
if (i >= MAXCMDLINE - 1)
break;
// This also moves the ending \0
memmove (key_lines[edit_line] + key_linepos + 1,
key_lines[edit_line] + key_linepos,
i - key_linepos + 1);
key_lines[edit_line][key_linepos] = ch;
key_linepos++;
}
break;
}
werase (input);
text = key_lines[edit_line];
if (screen_x < strlen (text) - 1)
text += screen_x - (strlen (text) - 1);
wmove (input, 0, 0);
while (*text)
waddch (input, *text++);
touchline (stdscr, screen_y - 1, 1);
wrefresh (input);
}
/*
Con_CompleteCommandLine
New function for tab-completion system
Added by EvilTypeGuy
Thanks to Fett erich@heintz.com
Thanks to taniwha
*/
void
Con_CompleteCommandLine (void)
{
char *cmd = "";
char *s;
int c, v, a, i;
int cmd_len;
char **list[3] = {0, 0, 0};
s = key_lines[edit_line] + 1;
if (*s == '\\' || *s == '/')
s++;
// Count number of possible matches
c = Cmd_CompleteCountPossible(s);
v = Cvar_CompleteCountPossible(s);
a = Cmd_CompleteAliasCountPossible(s);
if (!(c + v + a)) // No possible matches
return;
if (c + v + a == 1) {
if (c)
list[0] = Cmd_CompleteBuildList(s);
else if (v)
list[0] = Cvar_CompleteBuildList(s);
else
list[0] = Cmd_CompleteAliasBuildList(s);
cmd = *list[0];
cmd_len = strlen (cmd);
} else {
if (c)
cmd = *(list[0] = Cmd_CompleteBuildList(s));
if (v)
cmd = *(list[1] = Cvar_CompleteBuildList(s));
if (a)
cmd = *(list[2] = Cmd_CompleteAliasBuildList(s));
cmd_len = strlen (s);
do {
for (i = 0; i < 3; i++) {
char ch = cmd[cmd_len];
char **l = list[i];
if (l) {
while (*l && (*l)[cmd_len] == ch)
l++;
if (*l)
break;
}
}
if (i == 3)
cmd_len++;
} while (i == 3);
// 'quakebar'
Con_Printf("\n\35");
for (i = 0; i < screen_x - 4; i++)
Con_Printf("\36");
Con_Printf("\37\n");
// Print Possible Commands
if (c) {
Con_Printf("%i possible command%s\n", c, (c > 1) ? "s: " : ":");
Con_DisplayList(list[0], screen_x);
}
if (v) {
Con_Printf("%i possible variable%s\n", v, (v > 1) ? "s: " : ":");
Con_DisplayList(list[1], screen_x);
}
if (a) {
Con_Printf("%i possible aliases%s\n", a, (a > 1) ? "s: " : ":");
Con_DisplayList(list[2], screen_x);
}
}
if (cmd) {
key_lines[edit_line][1] = '/';
strncpy(key_lines[edit_line] + 2, cmd, cmd_len);
key_linepos = cmd_len + 2;
if (c + v + a == 1) {
key_lines[edit_line][key_linepos] = ' ';
key_linepos++;
}
key_lines[edit_line][key_linepos] = 0;
}
for (i = 0; i < 3; i++)
if (list[i])
free (list[i]);
}

View file

@ -40,7 +40,7 @@
#include <stdlib.h>
#include "QF/cmd.h"
#include "compat.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/model.h"
#include "QF/msg.h"
@ -54,6 +54,7 @@
#include "bothdefs.h"
#include "buildnum.h"
#include "compat.h"
#include "game.h"
#include "net.h"
#include "pmove.h"
@ -184,6 +185,7 @@ SV_Shutdown (void)
sv_logfile = NULL;
}
NET_Shutdown ();
Con_Shutdown ();
}
/*
@ -1440,14 +1442,7 @@ SV_CheckTimeouts (void)
void
SV_GetConsoleCommands (void)
{
char *cmd;
while (1) {
cmd = Sys_ConsoleInput ();
if (!cmd)
break;
Cbuf_AddText (cmd);
}
Con_ProcessInput ();
}
/*
@ -2037,6 +2032,8 @@ SV_Init (void)
PI_Init ();
Con_Init ();
COM_Filesystem_Init_Cvars ();
Game_Init_Cvars ();
COM_Init_Cvars ();

View file

@ -166,7 +166,7 @@ Con_Printf (char *fmt, ...)
pending = 0;
}
Sys_Printf ("%s", msg2); // also echo to debugging console
Con_Print (msg2); // also echo to debugging console
if (sv_logfile)
Qprintf (sv_logfile, "%s", msg2);
}

View file

@ -45,11 +45,13 @@
#include <sys/time.h>
#include <sys/types.h>
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/qargs.h"
#include "server.h"
#include "QF/sys.h"
#include "server.h"
#ifdef NeXT
# include <libc.h>
#endif
@ -184,6 +186,7 @@ main (int argc, char *argv[])
// run one frame immediately for first heartbeat
SV_Frame (0.1);
Con_ProcessInput (); // dirty hack to get the cursor in the right place
//
// main loop