/finally/, libQFconsole is being used (qw-server only so far). Still has

some minor nits with ncurses and probably has some isues without ncurses
(quit didn't seem to work when I tried).
This commit is contained in:
Bill Currie 2001-09-16 05:41:28 +00:00
parent 9f715ae9f8
commit aade80a8d7
9 changed files with 161 additions and 454 deletions

View file

@ -73,12 +73,10 @@ extern int con_notifylines; // scan lines to clear for notify lines
void Con_DrawCharacter (int cx, int line, int num);
void Con_CheckResize (void);
void Con_Init (const char *plugin_name);
void Con_Shutdown (void);
void Con_Init_Cvars (void);
void Con_ProcessInput (inputline_t *il, int ch);
void Con_ProcessInputLine (inputline_t *il, int ch);
void Con_DrawConsole (int lines);
void Con_DrawDownload (int lines);
void Con_Print (const char *txt);
void Con_Printf (const char *fmt, ...) __attribute__((format(printf,1,2)));
void Con_DPrintf (const char *fmt, ...) __attribute__((format(printf,1,2)));
@ -92,6 +90,7 @@ void Con_ToggleConsole_f (void);
// (i.e. will display possible variables, aliases, commands
// that match what they've typed so far)
void Con_CompleteCommandLine(void);
void Con_BasicCompleteCommandLine (inputline_t *il);
// Generic libs/util/console.c function to display a list
// formatted in columns on the console
@ -102,4 +101,11 @@ void Con_DestroyInputLine (inputline_t *inputline);
extern struct cvar_s *developer;
// init/shutdown functions
void Con_Init (const char *plugin_name);
void Con_Init_Cvars (void);
void Con_Shutdown (void);
void Con_ProcessInput (void);
#endif // __console_h

View file

@ -35,9 +35,11 @@
#include <QF/plugin.h>
typedef void (QFPLUGIN *P_C_Print) (const char *fmt, va_list args);
typedef void (QFPLUGIN *P_C_ProcessInput) (void);
typedef struct console_func_s {
P_C_Print pC_Print;
P_C_Print pC_Print;
P_C_ProcessInput pC_ProcessInput;
} console_funcs_t;
typedef struct console_data_s {

View file

@ -59,8 +59,9 @@ console_t con_main;
console_t con_chat;
console_t *con; // point to either con_main or con_chat
int con_linewidth; // characters across screen
int con_totallines; // total lines in console scrollback
extern int con_linewidth; // characters across screen
float con_cursorspeed = 4;
@ -572,6 +573,11 @@ Con_DrawDownload (int lines)
Draw_Character ((i + 1) << 3, y, dlbar[i]);
}
static void
C_ProcessInput (void)
{
}
static general_funcs_t plugin_info_general_funcs = {
C_Init,
C_Shutdown,
@ -581,6 +587,7 @@ static general_data_t plugin_info_general_data;
static console_data_t plugin_info_console_data;
static console_funcs_t plugin_info_console_funcs = {
C_Print,
C_ProcessInput,
};
static plugin_funcs_t plugin_info_funcs = {

View file

@ -40,8 +40,9 @@
#include "QF/cvar.h"
#include "QF/plugin.h"
static plugin_t *con_module;
int con_linewidth; // characters across screen
static plugin_t *con_module;
void
Con_Init (const char *plugin_name)
@ -93,3 +94,10 @@ Con_DPrintf (const char *fmt, ...)
va_end (args);
}
}
void
Con_ProcessInput (void)
{
if (con_module)
con_module->functions->console->pC_ProcessInput ();
}

View file

@ -59,6 +59,7 @@ Con_CreateInputLine (int lines, int width, char prompt)
p = (char**)(inputline + 1);
l = (char*)&p[lines];
inputline->lines = p;
inputline->num_lines = lines;
inputline->line_width = width;
while (lines--) {
@ -66,6 +67,9 @@ Con_CreateInputLine (int lines, int width, char prompt)
l += width;
}
inputline->prompt_char = prompt;
inputline->lines[0][0] = prompt;
inputline->linepos = 1;
return inputline;
}
@ -76,13 +80,12 @@ Con_DestroyInputLine (inputline_t *inputline)
}
void
Con_ProcessInput (inputline_t *il, int ch)
Con_ProcessInputLine (inputline_t *il, int ch)
{
int i;
switch (ch) {
/*
case K_ENTER:
case K_RETURN:
if (il->enter)
il->enter (il->lines[il->edit_line] + 1);
il->edit_line = (il->edit_line + 1) % il->num_lines;
@ -102,20 +105,20 @@ Con_ProcessInput (inputline_t *il, int ch)
il->linepos--;
}
break;
case K_DEL:
case K_DELETE:
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:
case K_RIGHT:
if (il->linepos < strlen (il->lines[il->edit_line]))
il->linepos++;
break;
case K_LEFTARROW:
case K_LEFT:
if (il->linepos > 1)
il->linepos--;
break;
case K_UPARROW:
case K_UP:
do {
il->history_line = (il->history_line - 1) % il->num_lines;
} while (il->history_line != il->edit_line
@ -125,7 +128,7 @@ Con_ProcessInput (inputline_t *il, int ch)
strcpy (il->lines[il->edit_line], il->lines[il->history_line]);
il->linepos = strlen (il->lines[il->edit_line]);
break;
case K_DOWNARROW:
case K_DOWN:
if (il->history_line == il->edit_line)
break;
do {
@ -147,7 +150,6 @@ Con_ProcessInput (inputline_t *il, int ch)
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]);

View file

@ -50,12 +50,15 @@
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/keys.h"
#include "QF/plugin.h"
#include "QF/qtypes.h"
#include "QF/sys.h"
#include "compat.h"
extern int con_linewidth;
#ifdef HAVE_CURSES_H
static int use_curses = 1;
@ -64,6 +67,9 @@ static WINDOW *status;
static WINDOW *input;
static int screen_x, screen_y;
#define MAXCMDLINE 256
static inputline_t *input_line;
static chtype attr_table[4] = {
A_NORMAL,
COLOR_PAIR(1),
@ -92,6 +98,15 @@ static const byte attr_map[256] = {
#endif
static void
C_ExecLine (const char *line)
{
if (line[0] == '/')
line++;
Cbuf_AddText (line);
}
static void
C_Init (void)
{
@ -133,6 +148,12 @@ C_Init (void)
wrefresh (output);
wrefresh (status);
wrefresh (input);
input_line = Con_CreateInputLine (16, MAXCMDLINE, ']');
input_line->complete = Con_BasicCompleteCommandLine;
input_line->enter = C_ExecLine;
con_linewidth = screen_x;
} else
#endif
setvbuf (stdout, 0, _IOLBF, BUFSIZ);
@ -181,6 +202,99 @@ C_Print (const char *fmt, va_list args)
vfprintf (stdout, fmt, args);
}
static void
C_ProcessInput (void)
{
#ifdef HAVE_CURSES_H
if (use_curses) {
int ch = wgetch (input), i;
const char *text;
switch (ch) {
case KEY_ENTER:
case '\n':
case '\r':
ch = K_RETURN;
break;
case '\t':
ch = K_TAB;
break;
case KEY_BACKSPACE:
case '\b':
ch = K_BACKSPACE;
break;
case KEY_DC:
ch = K_DELETE;
break;
case KEY_RIGHT:
ch = K_RIGHT;
break;
case KEY_LEFT:
ch = K_LEFT;
break;
case KEY_UP:
ch = K_UP;
break;
case KEY_DOWN:
ch = K_DOWN;
break;
case KEY_HOME:
ch = K_HOME;
break;
case KEY_END:
ch = K_END;
break;
case KEY_NPAGE:
ch = K_PAGEDOWN;
break;
case KEY_PPAGE:
ch = K_PAGEUP;
break;
default:
if (ch < 0 || ch >= 256)
ch = 0;
}
if (ch)
Con_ProcessInputLine (input_line, ch);
i = input_line->linepos - 1;
if (input_line->scroll > i)
input_line->scroll = i;
if (input_line->scroll < i - (screen_x - 2) + 1)
input_line->scroll = i - (screen_x - 2) + 1;
text = input_line->lines[input_line->edit_line] + input_line->scroll;
if ((int)strlen (text + 1) < screen_x - 2) {
text = input_line->lines[input_line->edit_line];
input_line->scroll = strlen (text + 1) - (screen_x - 2);
input_line->scroll = max (input_line->scroll, 0);
text += input_line->scroll;
}
wmove (input, 0, 0);
if (input_line->scroll) {
waddch (input, '<');
} else {
waddch (input, *text++);
}
for (i = 0; i < screen_x - 2 && *text; i++)
waddch (input, *text++);
while (i++ < screen_x - 2)
waddch (input, ' ');
if (*text) {
waddch (input, '>');
} else {
waddch (input, ' ');
}
wmove (input, 0, input_line->linepos - input_line->scroll);
touchline (stdscr, screen_y - 1, 1);
wrefresh (input);
} else
#endif
while (1) {
const char *cmd = Sys_ConsoleInput ();
if (!cmd)
Cbuf_AddText (cmd);
}
}
static general_funcs_t plugin_info_general_funcs = {
C_Init,
C_Shutdown,
@ -189,6 +303,7 @@ static general_data_t plugin_info_general_data;
static console_funcs_t plugin_info_console_funcs = {
C_Print,
C_ProcessInput,
};
static console_data_t plugin_info_console_data;

View file

@ -71,7 +71,7 @@ else
syssv_SRC= sv_sys_unix.c
endif
server_SOURCES= crudefile.c sv_ccmds.c sv_console.c sv_cvar.c sv_ents.c \
server_SOURCES= crudefile.c sv_ccmds.c sv_cvar.c sv_ents.c \
sv_init.c sv_main.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)
@ -79,10 +79,12 @@ server_SOURCES= crudefile.c sv_ccmds.c sv_console.c sv_cvar.c sv_ents.c \
qf_server_LIBS= $(top_builddir)/libs/models/libQFmodels.la \
$(top_builddir)/libs/gamecode/engine/libQFgamecode.la \
$(top_builddir)/libs/gamecode/builtins/libQFgamecode_builtins.la \
$(top_builddir)/libs/console/libQFconsole.la \
$(top_builddir)/libs/util/libQFutil.la
qw_server_SOURCES= $(common_SOURCES) $(server_SOURCES)
qw_server_LDADD= libqfnet.la $(ASM) $(qf_server_LIBS) $(NET_LIBS) $(DL_LIBS) $(CURSES_LIBS)
qw_server_LDFLAGS= $(common_ldflags)
qw_server_DEPENDENCIES= libqfnet.la $(ASM) $(qf_server_LIBS)
qf_client_LIBS= $(top_builddir)/libs/video/targets/libQFjs.la \

View file

@ -1,435 +0,0 @@
/*
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
#ifdef HAVE_CURSES_H
# include <curses.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <stdlib.h>
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/qtypes.h"
#include "QF/sys.h"
#include "server.h"
#ifdef HAVE_CURSES_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,
};
#endif
void
Con_Init (const char *plugin_name)
{
#ifdef HAVE_CURSES_H
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);
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);
leaveok (output, TRUE);
scrollok (status, FALSE);
leaveok (status, TRUE);
scrollok (input, FALSE);
nodelay (input, TRUE);
keypad (input, TRUE);
wclear (output);
wbkgdset (status, COLOR_PAIR(4));
wclear (status);
wclear (input);
touchwin (stdscr);
wrefresh (output);
wrefresh (status);
wrefresh (input);
#else
if (setvbuf (stdout, 0, _IOLBF, BUFSIZ))
Con_Printf ("warning: couldn't set stdout to linebuffered.\n");
#endif
}
void
Con_Shutdown (void)
{
#ifdef HAVE_CURSES_H
endwin ();
#endif
}
void
Con_Print (const char *txt)
{
#ifdef HAVE_CURSES_H
if (output) {
chtype ch;
while ((ch = (byte)*txt++)) {
ch = sys_char_map[ch] | attr_table[attr_map[ch]];
waddch (output, ch);
}
touchwin (stdscr);
wrefresh (output);
} else {
#endif
int ch;
while ((ch = (byte)*txt++)) {
ch = sys_char_map[ch];
putchar (ch);
}
#ifdef HAVE_CURSES_H
}
#endif
}
void
Con_ProcessInput (inputline_t *il, int ch)
{
#ifdef HAVE_CURSES_H
int i;
int curs_x;
char *text = 0;
static int scroll;
ch = wgetch (input);
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:
SV_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 '\b':
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;
}
i = key_linepos - 1;
if (scroll > i)
scroll = i;
if (scroll < i - (screen_x - 2) + 1)
scroll = i - (screen_x - 2) + 1;
text = key_lines[edit_line] + scroll + 1;
if ((int)strlen (text) < screen_x - 2) {
scroll = strlen (key_lines[edit_line] + 1) - (screen_x - 2);
if (scroll < 0)
scroll = 0;
text = key_lines[edit_line] + scroll + 1;
}
curs_x = key_linepos - scroll;
wmove (input, 0, 0);
if (scroll) {
waddch (input, '<');
} else {
waddch (input, key_lines[edit_line][0]);
}
for (i = 0; i < screen_x - 2 && *text; i++)
waddch (input, *text++);
while (i++ < screen_x - 2)
waddch (input, ' ');
if (*text) {
waddch (input, '>');
} else {
waddch (input, ' ');
}
wmove (input, 0, curs_x);
touchline (stdscr, screen_y - 1, 1);
wrefresh (input);
#else
const char *cmd;
while (1) {
cmd = Sys_ConsoleInput ();
if (!cmd)
break;
Cbuf_AddText (cmd);
}
#endif
}
#ifdef HAVE_CURSES_H
/*
Con_CompleteCommandLine
New function for tab-completion system
Added by EvilTypeGuy
Thanks to Fett erich@heintz.com
Thanks to taniwha
*/
void
Con_CompleteCommandLine (void)
{
const char *cmd = "";
char *s;
int c, v, a, i;
int cmd_len;
const 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];
const 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'
SV_Printf("\n\35");
for (i = 0; i < screen_x - 4; i++)
SV_Printf("\36");
SV_Printf("\37\n");
// Print Possible Commands
if (c) {
SV_Printf("%i possible command%s\n", c, (c > 1) ? "s: " : ":");
Con_DisplayList(list[0], screen_x);
}
if (v) {
SV_Printf("%i possible variable%s\n", v, (v > 1) ? "s: " : ":");
Con_DisplayList(list[1], screen_x);
}
if (a) {
SV_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]);
}
#endif

View file

@ -1489,7 +1489,7 @@ SV_CheckTimeouts (void)
void
SV_GetConsoleCommands (void)
{
Con_ProcessInput (0, 0); //XXX parms not used yet
Con_ProcessInput ();
}
/*
@ -1600,7 +1600,7 @@ SV_Frame (float time)
svs.stats.packets = 0;
svs.stats.count = 0;
}
Con_ProcessInput (0, 0); //XXX evil hack to get the cursor in the right place
Con_ProcessInput (); //XXX evil hack to get the cursor in the right place
}
void