2001-07-16 21:39:50 +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
|
|
|
|
|
|
|
|
$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
|
2001-09-10 12:56:23 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-25 20:16:24 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <signal.h>
|
2001-09-10 12:56:23 +00:00
|
|
|
#include <stdlib.h>
|
2001-07-16 21:39:50 +00:00
|
|
|
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/console.h"
|
|
|
|
#include "QF/cvar.h"
|
2001-09-16 05:41:28 +00:00
|
|
|
#include "QF/keys.h"
|
2001-07-16 23:15:27 +00:00
|
|
|
#include "QF/plugin.h"
|
2001-07-16 21:39:50 +00:00
|
|
|
#include "QF/qtypes.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2001-08-02 23:00:39 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-09-16 05:41:28 +00:00
|
|
|
extern int con_linewidth;
|
|
|
|
|
2001-07-18 18:22:13 +00:00
|
|
|
#ifdef HAVE_CURSES_H
|
2001-07-16 23:15:27 +00:00
|
|
|
static int use_curses = 1;
|
2001-07-16 21:39:50 +00:00
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static WINDOW *output;
|
|
|
|
static WINDOW *status;
|
|
|
|
static WINDOW *input;
|
|
|
|
static int screen_x, screen_y;
|
2001-09-25 20:35:37 +00:00
|
|
|
static int interrupted;
|
2001-07-16 23:15:27 +00:00
|
|
|
|
2001-09-16 05:41:28 +00:00
|
|
|
#define MAXCMDLINE 256
|
|
|
|
static inputline_t *input_line;
|
|
|
|
|
2001-07-16 21:39:50 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2001-09-10 12:56:23 +00:00
|
|
|
|
2001-09-16 05:41:28 +00:00
|
|
|
static void
|
|
|
|
C_ExecLine (const char *line)
|
|
|
|
{
|
|
|
|
if (line[0] == '/')
|
|
|
|
line++;
|
|
|
|
Cbuf_AddText (line);
|
|
|
|
}
|
2001-09-25 20:16:24 +00:00
|
|
|
|
2001-09-26 16:31:36 +00:00
|
|
|
static void
|
|
|
|
C_DrawInputLine (inputline_t *il)
|
|
|
|
{
|
|
|
|
WINDOW *win = (WINDOW *) il->user_data;
|
|
|
|
int i;
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
text = il->lines[il->edit_line] + il->scroll;
|
|
|
|
wmove (win, 0, 0);
|
|
|
|
if (il->scroll) {
|
|
|
|
waddch (win, '<' | COLOR_PAIR (5));
|
|
|
|
text++;
|
|
|
|
} else {
|
|
|
|
waddch (win, *text++);
|
|
|
|
}
|
|
|
|
for (i = 0; i < il->width - 2 && *text; i++) {
|
|
|
|
chtype ch = (byte)*text++;
|
|
|
|
ch = sys_char_map[ch] | attr_table[attr_map[ch]];
|
|
|
|
waddch (win, ch);
|
|
|
|
}
|
|
|
|
while (i++ < il->width - 2) {
|
|
|
|
waddch (win, ' ');
|
|
|
|
}
|
|
|
|
if (*text) {
|
|
|
|
waddch (win, '>' | COLOR_PAIR (5));
|
|
|
|
} else {
|
|
|
|
waddch (win, ' ');
|
|
|
|
}
|
|
|
|
wmove (win, 0, il->linepos - il->scroll);
|
|
|
|
wrefresh (win);
|
|
|
|
}
|
|
|
|
|
2001-09-25 20:16:24 +00:00
|
|
|
static void
|
|
|
|
sigwinch (int sig)
|
|
|
|
{
|
2001-09-25 20:35:37 +00:00
|
|
|
interrupted = 1;
|
2001-09-25 20:16:24 +00:00
|
|
|
signal (SIGWINCH, sigwinch);
|
|
|
|
}
|
2001-09-21 17:28:35 +00:00
|
|
|
#endif
|
2001-09-16 05:41:28 +00:00
|
|
|
|
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static void
|
|
|
|
C_Init (void)
|
2001-07-16 21:39:50 +00:00
|
|
|
{
|
2001-09-20 19:34:02 +00:00
|
|
|
#ifdef HAVE_CURSES_H
|
2001-09-16 06:08:09 +00:00
|
|
|
cvar_t *curses = Cvar_Get ("sv_use_curses", "1", CVAR_ROM, NULL,
|
|
|
|
"set to 0 to disable curses server console");
|
2001-09-20 06:51:25 +00:00
|
|
|
use_curses = curses->int_val;
|
2001-07-16 23:15:27 +00:00
|
|
|
if (use_curses) {
|
2001-09-25 20:16:24 +00:00
|
|
|
signal (SIGWINCH, sigwinch);
|
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
initscr ();
|
|
|
|
start_color ();
|
|
|
|
cbreak ();
|
|
|
|
noecho ();
|
|
|
|
|
|
|
|
nonl ();
|
|
|
|
intrflush (stdscr, FALSE);
|
|
|
|
|
|
|
|
getmaxyx (stdscr, screen_y, screen_x);
|
2001-09-26 05:45:47 +00:00
|
|
|
output = newwin (screen_y - 2, screen_x, 0, 0);
|
|
|
|
status = newwin (1, screen_x, screen_y - 2, 0);
|
|
|
|
input = newwin (1, screen_x, screen_y - 1, 0);
|
2001-07-16 23:15:27 +00:00
|
|
|
|
|
|
|
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);
|
2001-09-26 16:31:36 +00:00
|
|
|
init_pair (5, COLOR_CYAN, COLOR_BLACK);
|
2001-07-16 23:15:27 +00:00
|
|
|
|
|
|
|
scrollok (output, TRUE);
|
|
|
|
leaveok (output, TRUE);
|
|
|
|
|
|
|
|
scrollok (status, FALSE);
|
|
|
|
leaveok (status, TRUE);
|
|
|
|
|
|
|
|
scrollok (input, FALSE);
|
2001-09-26 06:09:45 +00:00
|
|
|
leaveok (input, FALSE);
|
2001-07-16 23:15:27 +00:00
|
|
|
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);
|
2001-09-16 05:41:28 +00:00
|
|
|
|
|
|
|
input_line = Con_CreateInputLine (16, MAXCMDLINE, ']');
|
|
|
|
input_line->complete = Con_BasicCompleteCommandLine;
|
|
|
|
input_line->enter = C_ExecLine;
|
2001-09-26 16:31:36 +00:00
|
|
|
input_line->width = screen_x;
|
|
|
|
input_line->user_data = input;
|
|
|
|
input_line->draw = C_DrawInputLine;
|
2001-09-16 05:41:28 +00:00
|
|
|
|
|
|
|
con_linewidth = screen_x;
|
2001-09-26 16:31:36 +00:00
|
|
|
|
|
|
|
C_DrawInputLine (input_line);
|
2001-08-09 15:33:44 +00:00
|
|
|
} else
|
2001-07-16 23:15:27 +00:00
|
|
|
#endif
|
2001-08-09 15:33:44 +00:00
|
|
|
setvbuf (stdout, 0, _IOLBF, BUFSIZ);
|
2001-07-16 21:39:50 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static void
|
|
|
|
C_Shutdown (void)
|
2001-07-16 21:39:50 +00:00
|
|
|
{
|
2001-07-16 23:15:27 +00:00
|
|
|
#ifdef HAVE_CURSES_H
|
|
|
|
if (use_curses)
|
|
|
|
endwin ();
|
|
|
|
#endif
|
2001-07-16 21:39:50 +00:00
|
|
|
}
|
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static void
|
|
|
|
C_Print (const char *fmt, va_list args)
|
2001-07-16 21:39:50 +00:00
|
|
|
{
|
2001-09-25 16:19:26 +00:00
|
|
|
static unsigned char *buffer;
|
|
|
|
unsigned char *txt;
|
|
|
|
int size;
|
|
|
|
static int buffer_size;
|
|
|
|
|
2001-09-25 16:32:00 +00:00
|
|
|
size = vsnprintf (buffer, buffer_size, fmt, args) + 1; // +1 for nul
|
|
|
|
if (size > buffer_size) {
|
|
|
|
buffer_size = (size + 1023) & ~1023; // 1k multiples
|
2001-09-25 16:19:26 +00:00
|
|
|
buffer = realloc (buffer, buffer_size);
|
|
|
|
if (!buffer)
|
|
|
|
Sys_Error ("console: could not allocate %d bytes\n",
|
|
|
|
buffer_size);
|
|
|
|
vsnprintf (buffer, buffer_size, fmt, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
txt = buffer;
|
2001-07-16 23:15:27 +00:00
|
|
|
#ifdef HAVE_CURSES_H
|
|
|
|
if (use_curses) {
|
2001-09-10 12:56:23 +00:00
|
|
|
chtype ch;
|
2001-07-16 23:15:27 +00:00
|
|
|
|
|
|
|
txt = buffer;
|
|
|
|
|
2001-09-25 16:13:41 +00:00
|
|
|
while ((ch = (byte)*txt++)) {
|
2001-07-16 23:15:27 +00:00
|
|
|
ch = sys_char_map[ch] | attr_table[attr_map[ch]];
|
|
|
|
waddch (output, ch);
|
|
|
|
}
|
|
|
|
touchwin (stdscr);
|
|
|
|
wrefresh (output);
|
2001-09-26 06:09:45 +00:00
|
|
|
wrefresh (input);
|
2001-07-16 23:15:27 +00:00
|
|
|
} else
|
|
|
|
#endif
|
2001-09-25 16:13:41 +00:00
|
|
|
{
|
2001-09-25 16:19:26 +00:00
|
|
|
while (*txt)
|
|
|
|
putc (sys_char_map[*txt++], stdout);
|
2001-09-25 16:13:41 +00:00
|
|
|
fflush (stdout);
|
|
|
|
}
|
2001-07-16 21:39:50 +00:00
|
|
|
}
|
|
|
|
|
2001-09-16 05:41:28 +00:00
|
|
|
static void
|
|
|
|
C_ProcessInput (void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CURSES_H
|
|
|
|
if (use_curses) {
|
2001-09-26 05:45:47 +00:00
|
|
|
int ch;
|
2001-09-16 05:41:28 +00:00
|
|
|
|
2001-09-25 20:35:37 +00:00
|
|
|
if (interrupted) {
|
|
|
|
struct winsize size;
|
|
|
|
|
|
|
|
interrupted = 0;
|
|
|
|
if (ioctl (fileno (stdout), TIOCGWINSZ, &size) == 0) {
|
|
|
|
resizeterm (size.ws_row, size.ws_col);
|
2001-09-25 23:33:37 +00:00
|
|
|
getmaxyx (stdscr, screen_y, screen_x);
|
|
|
|
con_linewidth = screen_x;
|
2001-09-26 16:31:36 +00:00
|
|
|
input_line->width = screen_x;
|
2001-09-26 18:08:30 +00:00
|
|
|
/* while a little ugly, this is needed because ncurses auto
|
|
|
|
* resizing doesn't always do the right thing
|
|
|
|
*/
|
|
|
|
wresize (input, 1, screen_x);
|
|
|
|
mvwin (input, screen_y - 1, 0);
|
|
|
|
wresize (status, 1, screen_x);
|
|
|
|
mvwin (status, screen_y - 2, 0);
|
|
|
|
wresize (output, screen_y - 2, screen_x);
|
|
|
|
mvwin (status, 0, 0);
|
2001-09-26 05:45:47 +00:00
|
|
|
wrefresh (curscr);
|
2001-09-25 20:35:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-26 05:45:47 +00:00
|
|
|
ch = wgetch (input);
|
2001-09-16 05:41:28 +00:00
|
|
|
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)
|
2001-09-26 18:08:30 +00:00
|
|
|
ch = 0;
|
2001-09-16 05:41:28 +00:00
|
|
|
}
|
2001-09-26 05:45:47 +00:00
|
|
|
Con_ProcessInputLine (input_line, ch);
|
2001-09-16 05:41:28 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
while (1) {
|
|
|
|
const char *cmd = Sys_ConsoleInput ();
|
|
|
|
if (!cmd)
|
2001-09-16 06:08:09 +00:00
|
|
|
break;
|
|
|
|
Cbuf_AddText (cmd);
|
2001-09-16 05:41:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static general_funcs_t plugin_info_general_funcs = {
|
|
|
|
C_Init,
|
|
|
|
C_Shutdown,
|
|
|
|
};
|
|
|
|
static general_data_t plugin_info_general_data;
|
2001-07-16 21:39:50 +00:00
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static console_funcs_t plugin_info_console_funcs = {
|
|
|
|
C_Print,
|
2001-09-16 05:41:28 +00:00
|
|
|
C_ProcessInput,
|
2001-07-16 23:15:27 +00:00
|
|
|
};
|
|
|
|
static console_data_t plugin_info_console_data;
|
|
|
|
|
|
|
|
static plugin_funcs_t plugin_info_funcs = {
|
|
|
|
&plugin_info_general_funcs,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
&plugin_info_console_funcs,
|
|
|
|
};
|
2001-07-16 21:39:50 +00:00
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static plugin_data_t plugin_info_data = {
|
|
|
|
&plugin_info_general_data,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
&plugin_info_console_data,
|
|
|
|
};
|
2001-07-16 21:39:50 +00:00
|
|
|
|
2001-07-16 23:15:27 +00:00
|
|
|
static plugin_t plugin_info = {
|
|
|
|
qfp_console,
|
|
|
|
0,
|
|
|
|
QFPLUGIN_VERSION,
|
|
|
|
"0.1",
|
|
|
|
"server console driver",
|
|
|
|
"Copyright (C) 1996-1997 id Software, Inc.\n"
|
|
|
|
"Copyright (C) 1999,2000,2001 contributors of the QuakeForge"
|
|
|
|
" project\n"
|
|
|
|
"Please see the file \"AUTHORS\" for a list of contributors",
|
|
|
|
&plugin_info_funcs,
|
|
|
|
&plugin_info_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
plugin_t *
|
2001-08-31 00:05:58 +00:00
|
|
|
console_server_PluginInfo (void)
|
2001-07-16 23:15:27 +00:00
|
|
|
{
|
|
|
|
return &plugin_info;
|
2001-07-16 21:39:50 +00:00
|
|
|
}
|