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
|
|
|
|
|
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
|
|
|
|
|
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"
|
|
|
|
|
2021-06-12 13:50:51 +00:00
|
|
|
#include "QF/ui/inputline.h"
|
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
typedef struct il_data_s {
|
|
|
|
struct il_data_s *next;
|
|
|
|
struct il_data_s **prev;
|
|
|
|
inputline_t *line;
|
|
|
|
progs_t *pr;
|
2022-01-18 06:32:43 +00:00
|
|
|
pr_func_t enter; // enter key callback
|
2022-01-18 03:11:14 +00:00
|
|
|
pr_ptr_t data[2]; // allow two data params for the callback
|
2011-07-09 00:44:37 +00:00
|
|
|
int method; // true if method rather than function
|
|
|
|
} il_data_t;
|
|
|
|
|
2002-01-30 08:41:18 +00:00
|
|
|
typedef struct {
|
2011-07-09 00:44:37 +00:00
|
|
|
PR_RESMAP (il_data_t) line_map;
|
|
|
|
il_data_t *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;
|
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
static il_data_t *
|
|
|
|
il_data_new (il_resources_t *res)
|
|
|
|
{
|
2021-03-21 12:26:36 +00:00
|
|
|
return PR_RESNEW (res->line_map);
|
2011-07-09 00:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
il_data_free (il_resources_t *res, il_data_t *line)
|
|
|
|
{
|
2021-03-21 12:26:36 +00:00
|
|
|
PR_RESFREE (res->line_map, line);
|
2011-07-09 00:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
il_data_reset (il_resources_t *res)
|
|
|
|
{
|
2021-03-21 12:26:36 +00:00
|
|
|
PR_RESRESET (res->line_map);
|
2011-07-09 00:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline il_data_t *
|
|
|
|
il_data_get (il_resources_t *res, unsigned index)
|
|
|
|
{
|
2021-03-21 12:26:36 +00:00
|
|
|
return PR_RESGET (res->line_map, index);
|
2011-07-09 00:44:37 +00:00
|
|
|
}
|
2002-01-30 08:41:18 +00:00
|
|
|
|
2020-12-20 17:12:51 +00:00
|
|
|
static inline int __attribute__((pure))
|
2011-07-09 00:44:37 +00:00
|
|
|
il_data_index (il_resources_t *res, il_data_t *line)
|
|
|
|
{
|
2021-03-21 12:26:36 +00:00
|
|
|
return PR_RESINDEX (res->line_map, line);
|
2011-07-09 00:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_il_clear (progs_t *pr, void *_res)
|
2011-07-09 00:44:37 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = (il_resources_t *)_res;
|
2011-07-09 00:44:37 +00:00
|
|
|
il_data_t *line;
|
|
|
|
|
|
|
|
for (line = res->lines; line; line = line->next)
|
|
|
|
Con_DestroyInputLine (line->line);
|
|
|
|
res->lines = 0;
|
|
|
|
il_data_reset (res);
|
|
|
|
}
|
|
|
|
|
2022-05-12 09:23:32 +00:00
|
|
|
static void
|
|
|
|
bi_il_destroy (progs_t *pr, void *_res)
|
|
|
|
{
|
2023-03-05 08:10:04 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
PR_RESDELMAP (res->line_map);
|
|
|
|
free (res);
|
2022-05-12 09:23:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 10:30:50 +00:00
|
|
|
static il_data_t * __attribute__((pure))
|
2022-02-14 03:28:38 +00:00
|
|
|
get_inputline (progs_t *pr, il_resources_t *res, int arg, const char *func)
|
2002-04-09 15:29:48 +00:00
|
|
|
{
|
2011-07-09 00:44:37 +00:00
|
|
|
il_data_t *line = il_data_get (res, arg);
|
2002-04-09 15:29:48 +00:00
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
// line->prev will be null if the handle is unallocated
|
|
|
|
if (!line || !line->prev)
|
|
|
|
PR_RunError (pr, "invalid inputline: passed to %s", func);
|
2002-04-09 15:29:48 +00:00
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
static void
|
|
|
|
bi_inputline_enter (inputline_t *il)
|
|
|
|
{
|
|
|
|
il_data_t *data = (il_data_t *) il->user_data;
|
|
|
|
progs_t *pr = data->pr;
|
|
|
|
const char *line = il->line;
|
|
|
|
|
|
|
|
if (!data->enter)
|
|
|
|
return; // no callback defined
|
|
|
|
|
|
|
|
PR_PushFrame (pr);
|
|
|
|
PR_RESET_PARAMS (pr);
|
|
|
|
if (data->method) {
|
|
|
|
P_POINTER (pr, 0) = data->data[0];
|
|
|
|
P_POINTER (pr, 1) = data->data[1];
|
|
|
|
P_STRING (pr, 2) = PR_SetTempString (pr, line);
|
2020-02-25 08:28:32 +00:00
|
|
|
pr->pr_argc = 3;
|
2011-07-09 00:44:37 +00:00
|
|
|
} else {
|
|
|
|
P_STRING (pr, 0) = PR_SetTempString (pr, line);
|
|
|
|
P_POINTER (pr, 1) = data->data[0];
|
2020-02-25 08:28:32 +00:00
|
|
|
pr->pr_argc = 2;
|
2011-07-09 00:44:37 +00:00
|
|
|
}
|
|
|
|
PR_ExecuteProgram (pr, data->enter);
|
|
|
|
PR_PopFrame (pr);
|
|
|
|
}
|
|
|
|
|
2002-01-23 22:37:44 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_Create (progs_t *pr, void *_res)
|
2002-01-23 22:37:44 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
2011-07-09 00:44:37 +00:00
|
|
|
il_data_t *data;
|
|
|
|
inputline_t *line;
|
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
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
line = Con_CreateInputLine (lines, size, prompt);
|
2002-01-30 08:41:18 +00:00
|
|
|
if (!line) {
|
2011-07-09 00:44:37 +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;
|
|
|
|
}
|
2011-07-09 00:44:37 +00:00
|
|
|
|
|
|
|
data = il_data_new (res);
|
|
|
|
if (!data) {
|
|
|
|
Con_DestroyInputLine (line);
|
|
|
|
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;
|
|
|
|
}
|
2011-07-09 00:44:37 +00:00
|
|
|
|
|
|
|
data->next = res->lines;
|
|
|
|
data->prev = &res->lines;
|
|
|
|
if (res->lines)
|
|
|
|
res->lines->prev = &data->next;
|
|
|
|
res->lines = data;
|
|
|
|
data->line = line;
|
2011-07-09 12:16:38 +00:00
|
|
|
data->pr = pr;
|
2011-07-09 00:44:37 +00:00
|
|
|
|
|
|
|
line->draw = res->draw;
|
|
|
|
line->enter = bi_inputline_enter;
|
|
|
|
line->user_data = data;
|
|
|
|
|
|
|
|
R_INT (pr) = il_data_index (res, data);
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
2002-08-16 21:52:37 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_SetPos (progs_t *pr, void *_res)
|
2002-08-16 21:52:37 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
2011-07-09 12:16:38 +00:00
|
|
|
"InputLine_SetPos");
|
|
|
|
line->line->x = P_INT (pr, 1);
|
|
|
|
line->line->y = P_INT (pr, 2);
|
|
|
|
}
|
2002-08-16 21:52:37 +00:00
|
|
|
|
2011-07-09 12:16:38 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_SetCursor (progs_t *pr, void *_res)
|
2011-07-09 12:16:38 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
2011-07-09 12:16:38 +00:00
|
|
|
"InputLine_SetCursor");
|
|
|
|
line->line->cursor = P_INT (pr, 1);
|
2011-07-09 00:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_SetEnter (progs_t *pr, void *_res)
|
2011-07-09 00:44:37 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
2011-07-09 00:44:37 +00:00
|
|
|
"InputLine_SetEnter");
|
|
|
|
|
|
|
|
line->data[1] = 0;
|
|
|
|
|
|
|
|
//FIXME look up implementation here?
|
|
|
|
if (pr->pr_argc == 4) {
|
|
|
|
line->enter = P_FUNCTION (pr, 1); // implementation
|
|
|
|
line->data[0] = P_POINTER (pr, 2); // object
|
|
|
|
line->data[1] = P_POINTER (pr, 3); // selector
|
|
|
|
line->method = 1;
|
|
|
|
} else {
|
|
|
|
line->enter = P_FUNCTION (pr, 1); // function
|
|
|
|
line->data[0] = P_POINTER (pr, 2); // data
|
|
|
|
line->data[1] = 0;
|
|
|
|
line->method = 0;
|
|
|
|
}
|
2002-08-16 21:52:37 +00:00
|
|
|
}
|
|
|
|
|
2002-01-30 21:25:00 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_SetWidth (progs_t *pr, void *_res)
|
2002-01-30 21:25:00 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, 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
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
line->line->width = width;
|
2002-01-30 21:25:00 +00:00
|
|
|
}
|
|
|
|
|
2002-01-23 22:37:44 +00:00
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_Destroy (progs_t *pr, void *_res)
|
2002-01-23 22:37:44 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
|
|
|
"InputLine_Destroy");
|
2002-01-30 08:41:18 +00:00
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
Con_DestroyInputLine (line->line);
|
|
|
|
*line->prev = line->next;
|
|
|
|
if (line->next)
|
|
|
|
line->next->prev = line->prev;
|
|
|
|
il_data_free (res, line);
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_Clear (progs_t *pr, void *_res)
|
2002-01-23 22:37:44 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, 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
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
Con_ClearTyping (line->line, save);
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_Process (progs_t *pr, void *_res)
|
2002-01-23 22:37:44 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
|
|
|
"InputLine_Process");
|
2002-07-24 21:42:33 +00:00
|
|
|
int ch = P_INT (pr, 1);
|
2002-01-30 08:41:18 +00:00
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
Con_ProcessInputLine (line->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
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_SetText (progs_t *pr, void *_res)
|
2002-03-19 17:07:56 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
|
|
|
"InputLine_SetText");
|
2011-07-09 00:44:37 +00:00
|
|
|
const char *str = P_GSTRING (pr, 1);
|
|
|
|
inputline_t *il = line->line;
|
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
|
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
Gets the text from a inputline
|
2002-03-19 17:07:56 +00:00
|
|
|
*/
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_GetText (progs_t *pr, void *_res)
|
2002-03-19 17:07:56 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
|
|
|
"InputLine_GetText");
|
2011-07-09 00:44:37 +00:00
|
|
|
inputline_t *il = line->line;
|
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
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_InputLine_Draw (progs_t *pr, void *_res)
|
2002-01-23 22:37:44 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
il_resources_t *res = _res;
|
|
|
|
il_data_t *line = get_inputline (pr, res, P_INT (pr, 0),
|
|
|
|
"InputLine_Draw");
|
2002-01-30 08:41:18 +00:00
|
|
|
|
2011-07-09 00:44:37 +00:00
|
|
|
line->line->draw (line->line);
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 12:54:03 +00:00
|
|
|
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
|
|
|
|
#define p(type) PR_PARAM(type)
|
2004-01-06 05:51:09 +00:00
|
|
|
static builtin_t builtins[] = {
|
2022-01-23 12:54:03 +00:00
|
|
|
bi(InputLine_Create, 3, p(int), p(int), p(int)),
|
|
|
|
bi(InputLine_SetPos, 3, p(ptr), p(int), p(int)),
|
|
|
|
bi(InputLine_SetCursor, 2, p(ptr), p(int)),
|
2011-07-09 00:44:37 +00:00
|
|
|
{"InputLine_SetEnter|^{tag _inputline_t=}(v*^v)^v",
|
2022-01-23 12:54:03 +00:00
|
|
|
bi_InputLine_SetEnter, -1, 3, {p(ptr), p(func), p(ptr)}},
|
2011-07-09 00:44:37 +00:00
|
|
|
{"InputLine_SetEnter|^{tag _inputline_t=}(@@:.)@:",
|
2022-01-23 12:54:03 +00:00
|
|
|
bi_InputLine_SetEnter, -1, 4, {p(ptr), p(func), p(ptr), p(ptr)}},
|
|
|
|
bi(InputLine_SetWidth, 2, p(ptr), p(int)),
|
|
|
|
bi(InputLine_SetText, 2, p(ptr), p(string)),
|
|
|
|
bi(InputLine_GetText, 1, p(ptr)),
|
|
|
|
bi(InputLine_Destroy, 1, p(ptr)),
|
|
|
|
bi(InputLine_Clear, 2, p(ptr), p(int)),
|
|
|
|
bi(InputLine_Process, 2, p(ptr), p(int)),
|
|
|
|
bi(InputLine_Draw, 1, p(ptr)),
|
2004-01-06 05:51:09 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-01-23 22:37:44 +00:00
|
|
|
InputLine_Progs_Init (progs_t *pr)
|
|
|
|
{
|
2011-07-09 00:44:37 +00:00
|
|
|
il_resources_t *res = calloc (1, sizeof (il_resources_t));
|
2002-01-30 08:41:18 +00:00
|
|
|
|
2022-05-12 09:23:32 +00:00
|
|
|
PR_Resources_Register (pr, "InputLine", res, bi_il_clear, bi_il_destroy);
|
2022-01-23 15:20:05 +00:00
|
|
|
PR_RegisterBuiltins (pr, builtins, res);
|
2002-01-23 22:37:44 +00:00
|
|
|
}
|
2002-08-20 21:19:53 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-08-20 21:19:53 +00:00
|
|
|
InputLine_Progs_SetDraw (progs_t *pr, void (*draw)(inputline_t *))
|
|
|
|
{
|
|
|
|
il_resources_t *res = PR_Resources_Find (pr, "InputLine");
|
|
|
|
res->draw = draw;
|
|
|
|
}
|