2002-01-23 22:37:44 +00:00
|
|
|
/*
|
|
|
|
bi_inputline.c
|
|
|
|
|
|
|
|
CSQC inputline builtins
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-01-30 08:41:18 +00:00
|
|
|
#include <stdlib.h>
|
2002-01-23 22:37:44 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
2002-01-30 08:41:18 +00:00
|
|
|
# include <string.h>
|
2002-01-23 22:37:44 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
2002-01-30 08:41:18 +00:00
|
|
|
# include <strings.h>
|
2002-01-23 22:37:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/console.h"
|
2003-01-06 18:28:13 +00:00
|
|
|
#include "QF/csqc.h"
|
2002-01-30 08:41:18 +00:00
|
|
|
#include "QF/draw.h"
|
2002-01-23 22:37:44 +00:00
|
|
|
#include "QF/progs.h"
|
2002-04-09 16:06:05 +00:00
|
|
|
#include "QF/sys.h"
|
2002-01-23 22:37:44 +00:00
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2002-01-30 08:41:18 +00:00
|
|
|
typedef struct {
|
|
|
|
inputline_t **lines;
|
|
|
|
int max_lines;
|
2002-08-20 21:19:53 +00:00
|
|
|
void (*draw)(inputline_t *il);
|
2002-01-30 08:41:18 +00:00
|
|
|
} il_resources_t;
|
|
|
|
|
|
|
|
//FIXME need to robustify the interface to avoid segfaults caused by errant
|
|
|
|
//progs
|
|
|
|
|
2002-04-09 15:29:48 +00:00
|
|
|
static inputline_t *
|
|
|
|
get_inputline (progs_t *pr, int arg, const char *func)
|
|
|
|
{
|
|
|
|
pr_type_t *handle;
|
|
|
|
inputline_t *line;
|
|
|
|
|
|
|
|
if (arg <= ((pr_type_t *) pr->zone - pr->pr_globals)
|
2003-04-17 00:01:48 +00:00
|
|
|
|| (size_t) arg >= (pr->zone_size / sizeof (pr_type_t)))
|
2002-04-09 15:29:48 +00:00
|
|
|
PR_RunError (pr, "%s: Invalid inputline_t", func);
|
|
|
|
|
|
|
|
handle = pr->pr_globals + arg;
|
|
|
|
|
|
|
|
line = *(inputline_t **)handle;
|
|
|
|
if (!line)
|
|
|
|
PR_RunError (pr, "Invalid inputline_t");
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2002-01-23 22:37:44 +00:00
|
|
|
static void
|
|
|
|
bi_InputLine_Create (progs_t *pr)
|
|
|
|
{
|
2002-01-30 08:41:18 +00:00
|
|
|
il_resources_t *res = PR_Resources_Find (pr, "InputLine");
|
|
|
|
inputline_t **line = 0;
|
|
|
|
int i;
|
2002-07-24 21:42:33 +00:00
|
|
|
int lines = P_INT (pr, 0);
|
|
|
|
int size = P_INT (pr, 1);
|
|
|
|
int prompt = P_INT (pr, 2);
|
2002-01-30 08:41:18 +00:00
|
|
|
pr_type_t *handle;
|
|
|
|
|
|
|
|
for (i = 0; i < res->max_lines; i++)
|
|
|
|
if (!res->lines[i]) {
|
|
|
|
line = &res->lines[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!line) {
|
2002-04-09 16:06:05 +00:00
|
|
|
Sys_Printf ("out of resources\n");
|
2002-07-24 21:42:33 +00:00
|
|
|
R_INT (pr) = 0;
|
2002-01-30 08:41:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-01-30 21:25:00 +00:00
|
|
|
*line = Con_CreateInputLine (lines, size, prompt);
|
2002-01-30 08:41:18 +00:00
|
|
|
if (!*line) {
|
2002-04-09 16:06:05 +00:00
|
|
|
Sys_Printf ("failed to create inputline\n");
|
2002-07-24 21:42:33 +00:00
|
|
|
R_INT (pr) = 0;
|
2002-01-30 08:41:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-08-20 21:19:53 +00:00
|
|
|
(*line)->draw = res->draw;
|
2002-01-30 08:41:18 +00:00
|
|
|
handle = PR_Zone_Malloc (pr, sizeof (inputline_t *));
|
|
|
|
*(inputline_t**)handle = *line;
|
2002-07-24 21:42:33 +00:00
|
|
|
R_INT (pr) = handle - pr->pr_globals;
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
2002-08-16 21:52:37 +00:00
|
|
|
static void
|
|
|
|
bi_InputLine_SetUserData (progs_t *pr)
|
|
|
|
{
|
|
|
|
inputline_t *line = get_inputline (pr, P_INT (pr, 0),
|
|
|
|
"InputLine_SetWidth");
|
2003-04-22 18:20:15 +00:00
|
|
|
pr_type_t *data = P_GPOINTER (pr, 1);
|
2002-08-16 21:52:37 +00:00
|
|
|
|
|
|
|
line->user_data = data;
|
|
|
|
}
|
|
|
|
|
2002-01-30 21:25:00 +00:00
|
|
|
static void
|
|
|
|
bi_InputLine_SetWidth (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
inputline_t *line = get_inputline (pr, P_INT (pr, 0),
|
2002-04-09 15:29:48 +00:00
|
|
|
"InputLine_SetWidth");
|
2002-07-24 21:42:33 +00:00
|
|
|
int width = P_INT (pr, 1);
|
2002-01-30 21:25:00 +00:00
|
|
|
|
|
|
|
line->width = width;
|
|
|
|
}
|
|
|
|
|
2002-01-23 22:37:44 +00:00
|
|
|
static void
|
|
|
|
bi_InputLine_Destroy (progs_t *pr)
|
|
|
|
{
|
2002-01-30 08:41:18 +00:00
|
|
|
il_resources_t *res = PR_Resources_Find (pr, "InputLine");
|
2002-04-09 15:29:48 +00:00
|
|
|
pr_type_t *handle;
|
2002-07-24 21:42:33 +00:00
|
|
|
int arg = P_INT (pr, 0);
|
2002-01-30 08:41:18 +00:00
|
|
|
int i;
|
2002-04-09 15:29:48 +00:00
|
|
|
inputline_t *line;
|
|
|
|
|
|
|
|
if (arg <= ((pr_type_t *) pr->zone - pr->pr_globals)
|
2003-04-17 00:01:48 +00:00
|
|
|
|| (size_t) arg >= (pr->zone_size / sizeof (pr_type_t)))
|
2002-04-09 15:29:48 +00:00
|
|
|
PR_RunError (pr, "InputLine_Destroy: Invalid inputline_t");
|
|
|
|
|
|
|
|
handle = pr->pr_globals + arg;
|
|
|
|
|
|
|
|
line = *(inputline_t **)handle;
|
|
|
|
if (!line)
|
|
|
|
PR_RunError (pr, "InputLine_Destroy: Invalid inputline_t");
|
2002-01-30 08:41:18 +00:00
|
|
|
|
|
|
|
for (i = 0; i < res->max_lines; i++)
|
|
|
|
if (res->lines[i] == line) {
|
|
|
|
Con_DestroyInputLine (line);
|
|
|
|
res->lines = 0;
|
|
|
|
PR_Zone_Free (pr, handle);
|
|
|
|
}
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_InputLine_Clear (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
inputline_t *line = get_inputline (pr, P_INT (pr, 0), "InputLine_Clear");
|
2002-08-28 16:02:43 +00:00
|
|
|
int save = P_INT (pr, 1);
|
2002-01-30 08:41:18 +00:00
|
|
|
|
2002-08-28 16:02:43 +00:00
|
|
|
Con_ClearTyping (line, save);
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_InputLine_Process (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
inputline_t *line = get_inputline (pr, P_INT (pr, 0), "InputLine_Process");
|
|
|
|
int ch = P_INT (pr, 1);
|
2002-01-30 08:41:18 +00:00
|
|
|
|
|
|
|
Con_ProcessInputLine (line, ch);
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
2002-03-19 17:07:56 +00:00
|
|
|
/*
|
|
|
|
bi_InputLine_SetText
|
|
|
|
|
|
|
|
Sets the inputline to a specified text
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bi_InputLine_SetText (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
inputline_t *il = get_inputline (pr, P_INT (pr, 0), "InputLine_SetText");
|
2003-04-22 18:20:15 +00:00
|
|
|
const char *str = P_GSTRING (pr, 1);
|
2002-03-19 17:07:56 +00:00
|
|
|
|
|
|
|
/* this was segfault trap:
|
|
|
|
il->lines[il->edit_line][0] is promt character
|
|
|
|
*/
|
|
|
|
strncpy(il->lines[il->edit_line] + 1,str,il->line_size - 1);
|
|
|
|
il->lines[il->edit_line][il->line_size-1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
bi_InputLine_GetText
|
|
|
|
|
|
|
|
Gets the text from a inputline
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bi_InputLine_GetText (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
inputline_t *il = get_inputline (pr, P_INT (pr, 0), "InputLine_GetText");
|
2002-03-19 17:07:56 +00:00
|
|
|
|
|
|
|
RETURN_STRING(pr, il->lines[il->edit_line]+1);
|
|
|
|
}
|
|
|
|
|
2002-01-23 22:37:44 +00:00
|
|
|
static void
|
|
|
|
bi_InputLine_Draw (progs_t *pr)
|
|
|
|
{
|
2002-07-24 21:42:33 +00:00
|
|
|
inputline_t *il = get_inputline (pr, P_INT (pr, 0), "InputLine_Draw");
|
2002-08-20 21:19:53 +00:00
|
|
|
|
|
|
|
il->draw (il);
|
2002-01-30 08:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bi_il_clear (progs_t *pr, void *data)
|
|
|
|
{
|
|
|
|
il_resources_t *res = (il_resources_t *)data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < res->max_lines; i++)
|
|
|
|
if (res->lines[i]) {
|
|
|
|
Con_DestroyInputLine (res->lines[i]);
|
|
|
|
res->lines[i] = 0;
|
|
|
|
}
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputLine_Progs_Init (progs_t *pr)
|
|
|
|
{
|
2002-01-30 08:41:18 +00:00
|
|
|
il_resources_t *res = malloc (sizeof (il_resources_t));
|
|
|
|
res->max_lines = 64;
|
|
|
|
res->lines = calloc (sizeof (inputline_t *), res->max_lines);
|
|
|
|
|
|
|
|
PR_Resources_Register (pr, "InputLine", res, bi_il_clear);
|
2002-01-23 22:37:44 +00:00
|
|
|
PR_AddBuiltin (pr, "InputLine_Create", bi_InputLine_Create, -1);
|
2002-08-16 21:52:37 +00:00
|
|
|
PR_AddBuiltin (pr, "InputLine_SetUserData",
|
|
|
|
bi_InputLine_SetUserData, -1);
|
2002-01-30 21:25:00 +00:00
|
|
|
PR_AddBuiltin (pr, "InputLine_SetWidth", bi_InputLine_SetWidth, -1);
|
2002-03-19 17:07:56 +00:00
|
|
|
PR_AddBuiltin (pr, "InputLine_SetText", bi_InputLine_SetText, -1);
|
|
|
|
PR_AddBuiltin (pr, "InputLine_GetText", bi_InputLine_GetText, -1);
|
2002-01-23 22:37:44 +00:00
|
|
|
PR_AddBuiltin (pr, "InputLine_Destroy", bi_InputLine_Destroy, -1);
|
|
|
|
PR_AddBuiltin (pr, "InputLine_Clear", bi_InputLine_Clear, -1);
|
|
|
|
PR_AddBuiltin (pr, "InputLine_Process", bi_InputLine_Process, -1);
|
|
|
|
PR_AddBuiltin (pr, "InputLine_Draw", bi_InputLine_Draw, -1);
|
|
|
|
}
|
2002-08-20 21:19:53 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
InputLine_Progs_SetDraw (progs_t *pr, void (*draw)(inputline_t *))
|
|
|
|
{
|
|
|
|
il_resources_t *res = PR_Resources_Find (pr, "InputLine");
|
|
|
|
res->draw = draw;
|
|
|
|
}
|