-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.h
123 lines (103 loc) · 4.96 KB
/
console.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef _CONSOLE_H
#define _CONSOLE_H
#include <stdarg.h>
#include "list.h"
#include "graphics.h"
#include "drawable.h"
#include "monotext.h"
#include "log.h"
#define console_debug(...) debugf("Console", __VA_ARGS__)
#define console_error(...) errorf("Console", __VA_ARGS__)
#define CONSOLE_CHAR_FOCUS 0x00A7 /* Paragraph key. */
#define CONSOLE_HISTORY_LINES 128 /* These many screen-width lines are guaranteed to fit into the history. */
#define CONSOLE_DISPLAY_LINES 25 /* These many screen-width lines are drawn at any given moment to the screen. */
#define CONSOLE_INPUT_MAX 256 /* Max number of chars user is allowed to input. */
#define CONSOLE_CMD_NAME_MAX 32 /* Max length of a console_cmd name. */
#define CONSOLE_INPUT_HISTORY_MAX 128 /* Number of command input stored in console->input_history. */
#define CONSOLE_ENV_MAX 255 /* Number of environment variables. */
struct console;
struct console_cmd;
typedef void (*console_cmd_func_t)(struct console *c, struct console_cmd *cmd, struct list *argv);
typedef void (*console_cmd_autocomplete_t)(struct console *c, struct console_cmd *cmd, struct list *argv, struct list *completions);
struct console_cmd {
char name[CONSOLE_CMD_NAME_MAX]; /* Name of this command. */
int argc; /* How many (non sub-command) arguments this command takes. */
console_cmd_func_t callback; /* Callback when this command is entered. */
console_cmd_autocomplete_t autocomplete; /* Run to determine how to autocomplete command. */
struct list *commands; /* Subcommands. */
};
define_element(struct cmd_element, struct console_cmd*);
#define CONSOLE_VAR_NAME_MAX 255
#define CONSOLE_VAR_TYPE_UNKNOWN 0
#define CONSOLE_VAR_TYPE_STR 1
#define CONSOLE_VAR_TYPE_1F 2
#define CONSOLE_VAR_TYPE_3F 3
struct console_var {
char name[CONSOLE_VAR_NAME_MAX]; /* Variable name. */
void *value; /* Variable value. */
int type; /* The type of data pointed to by .value: one of CONSOLE_VAR_TYPE_*. */
size_t value_size; /* The size in bytes of the value data. */
};
struct console_cursor {
size_t pos; /* Input cursor position. */
double time_input; /* When input was last entered. */
double time_blink; /* When the cursor was last blinked. */
struct basic_sprite sprite; /* Sprite used for rendering the cursor. */
struct monotext* txt; /* Text sprite used for rendering input text. */
};
struct console_env {
struct console_var vars[CONSOLE_ENV_MAX];
int len;
};
struct console_conf {
const char *data;
size_t len;
};
struct console {
int initialized;
int padding;
int display_lines; /* Number of lines to display. */
int focused; /* Use console_toggle_focus() to set. */
char *history; /* History buffer. */
char *history_cur; /* Current offset in history buffer. */
size_t history_len;
int chars_per_line;
char input[CONSOLE_INPUT_MAX];
size_t input_len; /* Length of input buffer. */
struct list *input_history; /* The last entered commands are stored here. */
int input_history_cur; /* The currently selected line in the input history. */
struct monotext txt_display;
struct monotext txt_input;
struct monofont *font;
struct console_cursor cursor;
struct console_cmd root_cmd;
struct basic_sprite background;
struct console_env env;
struct console_conf conf;
};
void console_new(struct console *c, struct monofont *font, int view_width,
int padding, GLuint *white_tex, struct shader *shader);
void console_free(struct console *c);
void console_print(struct console *c, const char *text, size_t text_len);
void console_printf(struct console *c, const char *fmt, ...);
void console_vprintf(struct console *c, const char *fmt, va_list args);
void console_think(struct console *c, float delta_time);
void console_render(struct console *c, struct shader *s, struct graphics *g);
void console_toggle_focus(struct console *c);
void console_parse_conf(struct console *c, struct console_conf *conf);
float console_height(struct console *c, int display_lines);
void console_input_feed_control(struct console *c, int key, int scancode, int action, int mods);
void console_input_feed_char(struct console *c, unsigned int key, int mods);
void console_input_clear(struct console *c);
void console_cmd_new(struct console_cmd *cmd, const char *name, int argc,
console_cmd_func_t callback, console_cmd_autocomplete_t autocomplete);
void console_cmd_add(struct console_cmd *cmd, struct console_cmd *parent);
void console_cmd_free(struct console_cmd *cmd);
void console_cmd_autocomplete(struct console *c, const char *input,
const size_t input_len, const size_t cursor_pos);
void console_parse(struct console *c, const char *in_str, size_t in_str_len);
int console_cmd_parse_1f(struct console *c, struct console_cmd *cmd,
struct list *argv, float *dst);
int console_env_bind_1f(struct console *c, const char *name, float *value);
int console_env_set_1f(struct console *c, const char *name, const float value);
#endif