Debian Codegraph @

src/dinkc_console.cpp

raw
1 /**
2 * DinkC interactive console
3
4 * Copyright (C) 2005, 2006 Dan Walma
5 * Copyright (C) 2008, 2009, 2014 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "dinkc_console.h"
29
30 #include <string.h>
31 #include "str_util.h"
32 #include "dinkc.h"
33
34 #define NB_LINES 20
35 #define MAX_LINE_LEN 254
36
37 /* Console mode */
38 int console_active = 0;
39
40
41 /* Last 20 commands entered in the console */
42 static char history[NB_LINES][MAX_LINE_LEN+1];
43
44 /* Current history index */
45 static int cur_line = 0;
46
47 /* Result of the last evaluation */
48 static int console_return_value = 0;
49
50
51 void dinkc_console_process_key(SDL_Event* ev)
52 {
53 if (ev->type == SDL_KEYDOWN) {
54 SDL_KeyboardEvent kev = ev->key;
55 if (kev.keysym.scancode == SDL_SCANCODE_UP)
56 {
57 cur_line--;
58 /* Using (a+N)%N instead of a%N to avoid negative results */
59 cur_line = (cur_line + NB_LINES) % NB_LINES;
60 }
61 else if(kev.keysym.scancode == SDL_SCANCODE_DOWN)
62 {
63 cur_line++;
64 cur_line %= NB_LINES;
65 }
66 else if (kev.keysym.scancode == SDL_SCANCODE_BACKSPACE)
67 {
68 /* Delete last char */
69 int len = strlen(history[cur_line]);
70 if (len > 0)
71 history[cur_line][len-1] = '\0';
72 }
73 else if (kev.keysym.scancode == SDL_SCANCODE_ESCAPE)
74 {
75 dinkc_console_hide();
76 }
77 else if (kev.keysym.scancode == SDL_SCANCODE_RETURN)
78 {
79 /* Try to parse the string */
80 console_return_value = dinkc_execute_one_liner(history[cur_line]);
81
82 /* Go the next line */
83 cur_line++;
84 cur_line %= NB_LINES;
85 int len = strlen(history[cur_line]);
86 if (len > 0)
87 history[cur_line][0] = '\0';
88 }
89 } else if (ev->type == SDL_TEXTINPUT) {
90 /* Append character to the current line */
91 if (strlen(history[cur_line] + 1) <= MAX_LINE_LEN)
92 if (isprint(ev->text.text[0]))
93 strchar(history[cur_line], ev->text.text[0]);
94 }
95 }
96
97 char* dinkc_console_get_cur_line()
98 {
99 return history[cur_line];
100 }
101
102 int dinkc_console_get_last_retval()
103 {
104 return console_return_value;
105 }
106
107 void dinkc_console_show()
108 {
109 SDL_StartTextInput();
110 console_active = 1;
111 }
112
113 void dinkc_console_hide()
114 {
115 SDL_StopTextInput();
116 console_active = 0;
117 }