1 | /*
|
---|
2 | * Copyright (C) 2017 The Contributors
|
---|
3 | *
|
---|
4 | * This program is free software: you can redistribute it and/or modify
|
---|
5 | * it under the terms of the GNU General Public License as published by
|
---|
6 | * the Free Software Foundation, either version 3 of the License, or (at
|
---|
7 | * your option) any later version.
|
---|
8 | *
|
---|
9 | * This program is distributed in the hope that it will be useful, but
|
---|
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | * General Public License for more details.
|
---|
13 | *
|
---|
14 | * A copy of the GNU General Public License can be found in the file
|
---|
15 | * "gpl.txt" in the top directory of this repository.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <all.h>
|
---|
19 |
|
---|
20 | #define ver(...) _ver(ser_verbose, 0, __VA_ARGS__)
|
---|
21 | #define ver2(...) _ver(ser_verbose, 1, __VA_ARGS__)
|
---|
22 | #define ver3(...) _ver(ser_verbose, 2, __VA_ARGS__)
|
---|
23 |
|
---|
24 | #define CON_W 80
|
---|
25 | #define CON_H 25
|
---|
26 |
|
---|
27 | #define CON_BGR 0x00000000
|
---|
28 | #define CON_CUR 0x00e87000
|
---|
29 | #define CON_FGR ((SDL_Color){ .r = 255, .b = 255, .g = 255, .a = 255 })
|
---|
30 |
|
---|
31 | #define CON_FONT "ttf/vera-sans-mono.ttf"
|
---|
32 |
|
---|
33 | int32_t ser_verbose = 0;
|
---|
34 |
|
---|
35 | static uint8_t mem[CON_H][CON_W + 1];
|
---|
36 |
|
---|
37 | static TTF_Font *fon;
|
---|
38 | static int32_t fon_w, fon_h;
|
---|
39 |
|
---|
40 | static int32_t sur_w, sur_h;
|
---|
41 | static SDL_Surface *sur;
|
---|
42 |
|
---|
43 | static int32_t cur_x = 0, cur_y = 0;
|
---|
44 |
|
---|
45 | static void update(void)
|
---|
46 | {
|
---|
47 | if (SDL_FillRect(sur, NULL, CON_BGR) < 0) {
|
---|
48 | fail("SDL_FillRect() failed: %s", SDL_GetError());
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (SDL_FillRect(sur, &(SDL_Rect){
|
---|
52 | .x = cur_x * fon_w,
|
---|
53 | .y = cur_y * fon_h,
|
---|
54 | .w = fon_w,
|
---|
55 | .h = fon_h
|
---|
56 | }, CON_CUR) < 0) {
|
---|
57 | fail("SDL_FillRect() failed: %s", SDL_GetError());
|
---|
58 | }
|
---|
59 |
|
---|
60 | for (int32_t y = 0; y < CON_H; ++y) {
|
---|
61 | SDL_Surface *lin = TTF_RenderText_Blended(fon, (char *)mem[y], CON_FGR);
|
---|
62 |
|
---|
63 | if (lin == NULL) {
|
---|
64 | fail("TTF_RenderText_Blended() failed: %s", TTF_GetError());
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (SDL_BlitSurface(lin, NULL, sur, &(SDL_Rect){
|
---|
68 | .x = 0,
|
---|
69 | .y = y * fon_h,
|
---|
70 | .w = CON_W * fon_w,
|
---|
71 | .h = fon_h
|
---|
72 | })) {
|
---|
73 | fail("SDL_BlitSurface() failed: %s", SDL_GetError());
|
---|
74 | }
|
---|
75 |
|
---|
76 | SDL_FreeSurface(lin);
|
---|
77 | }
|
---|
78 |
|
---|
79 | SDL_Texture *tex = SDL_CreateTextureFromSurface(sdl_ren, sur);
|
---|
80 |
|
---|
81 | if (tex == NULL) {
|
---|
82 | fail("SDL_CreateTextureFromSurface() failed: %s", SDL_GetError());
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (SDL_RenderCopy(sdl_ren, tex, NULL, NULL) < 0) {
|
---|
86 | fail("SDL_RenderCopy() failed: %s", SDL_GetError());
|
---|
87 | }
|
---|
88 |
|
---|
89 | SDL_DestroyTexture(tex);
|
---|
90 | SDL_RenderPresent(sdl_ren);
|
---|
91 | }
|
---|
92 |
|
---|
93 | static void scroll(void)
|
---|
94 | {
|
---|
95 | memmove(mem, mem + 1, (CON_H - 1) * (CON_W + 1));
|
---|
96 | memset(mem + (CON_H - 1), ' ', CON_W);
|
---|
97 | }
|
---|
98 |
|
---|
99 | static void forw(void)
|
---|
100 | {
|
---|
101 | if (cur_x < CON_W - 1) {
|
---|
102 | ++cur_x;
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (cur_y == CON_H - 1) {
|
---|
107 | cur_x = 0;
|
---|
108 | scroll();
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | cur_x = 0;
|
---|
113 | ++cur_y;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void back(void)
|
---|
117 | {
|
---|
118 | if (cur_x > 0) {
|
---|
119 | --cur_x;
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (cur_y == 0) {
|
---|
124 | return;
|
---|
125 | }
|
---|
126 |
|
---|
127 | cur_x = CON_W - 1;
|
---|
128 | --cur_y;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static void down(void)
|
---|
132 | {
|
---|
133 | if (cur_y < CON_H - 1) {
|
---|
134 | ++cur_y;
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | scroll();
|
---|
139 | }
|
---|
140 |
|
---|
141 | static void echo(uint8_t c)
|
---|
142 | {
|
---|
143 | if (c < 32) {
|
---|
144 | switch (c) {
|
---|
145 | case '\r':
|
---|
146 | cur_x = 0;
|
---|
147 | break;
|
---|
148 |
|
---|
149 | case '\n':
|
---|
150 | down();
|
---|
151 | break;
|
---|
152 |
|
---|
153 | case '\b':
|
---|
154 | back();
|
---|
155 | break;
|
---|
156 |
|
---|
157 | default:
|
---|
158 | echo('^');
|
---|
159 | echo((uint8_t)(c + '@'));
|
---|
160 | return;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | else {
|
---|
164 | mem[cur_y][cur_x] = c;
|
---|
165 | forw();
|
---|
166 | }
|
---|
167 |
|
---|
168 | update();
|
---|
169 | }
|
---|
170 |
|
---|
171 | void ser_key(SDL_KeyboardEvent *ev)
|
---|
172 | {
|
---|
173 | switch (ev->keysym.sym) {
|
---|
174 | case SDLK_BACKSPACE:
|
---|
175 | echo('\b');
|
---|
176 | break;
|
---|
177 |
|
---|
178 | case SDLK_RETURN:
|
---|
179 | echo('\r');
|
---|
180 | echo('\n');
|
---|
181 | break;
|
---|
182 |
|
---|
183 | default:
|
---|
184 | if ((ev->keysym.mod & KMOD_CTRL) != 0 &&
|
---|
185 | ev->keysym.sym >= SDLK_a && ev->keysym.sym <= SDLK_z) {
|
---|
186 | echo((uint8_t)(ev->keysym.sym - SDLK_a + 1));
|
---|
187 | }
|
---|
188 |
|
---|
189 | break;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | void ser_text(SDL_TextInputEvent *ev)
|
---|
194 | {
|
---|
195 | for (int32_t i = 0; ev->text[i] != 0; ++i) {
|
---|
196 | echo((uint8_t)ev->text[i]);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | void ser_init(void)
|
---|
201 | {
|
---|
202 | ver("ser init");
|
---|
203 |
|
---|
204 | SDL_RWops *ops = SDL_RWFromFile(CON_FONT, "rb");
|
---|
205 |
|
---|
206 | if (ops == NULL) {
|
---|
207 | fail("error while opening font file " CON_FONT ": %s", SDL_GetError());
|
---|
208 | }
|
---|
209 |
|
---|
210 | fon = TTF_OpenFontRW(ops, 1, 32);
|
---|
211 |
|
---|
212 | if (fon == NULL) {
|
---|
213 | fail("error while loading font file " CON_FONT ": %s", TTF_GetError());
|
---|
214 | }
|
---|
215 |
|
---|
216 | fon_h = TTF_FontLineSkip(fon);
|
---|
217 |
|
---|
218 | if (TTF_GlyphMetrics(fon, 'X', NULL, NULL, NULL, NULL, &fon_w) < 0) {
|
---|
219 | fail("error while measuring font width: %s", TTF_GetError());
|
---|
220 | }
|
---|
221 |
|
---|
222 | sur_w = CON_W * fon_w;
|
---|
223 | sur_h = CON_H * fon_h;
|
---|
224 |
|
---|
225 | sur = SDL_CreateRGBSurface(0, sur_w, sur_h, 32, 0, 0, 0, 0);
|
---|
226 |
|
---|
227 | if (sur == NULL) {
|
---|
228 | fail("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
|
---|
229 | }
|
---|
230 |
|
---|
231 | for (int32_t y = 0; y < CON_H; ++y) {
|
---|
232 | for (int32_t x = 0; x < CON_W; ++x) {
|
---|
233 | mem[y][x] = ' ';
|
---|
234 | }
|
---|
235 |
|
---|
236 | mem[y][CON_W] = 0;
|
---|
237 | }
|
---|
238 |
|
---|
239 | update();
|
---|
240 | }
|
---|
241 |
|
---|
242 | void ser_quit(void)
|
---|
243 | {
|
---|
244 | ver("ser quit");
|
---|
245 |
|
---|
246 | SDL_FreeSurface(sur);
|
---|
247 | TTF_CloseFont(fon);
|
---|
248 | }
|
---|
249 |
|
---|
250 | void ser_exec(void)
|
---|
251 | {
|
---|
252 | ver3("ser exec");
|
---|
253 | }
|
---|
254 |
|
---|
255 | uint32_t ser_read(uint32_t off, int32_t sz)
|
---|
256 | {
|
---|
257 | ver2("ser rd %u:%d", off, sz * 8);
|
---|
258 | return 0;
|
---|
259 | }
|
---|
260 |
|
---|
261 | void ser_write(uint32_t off, int32_t sz, uint32_t val)
|
---|
262 | {
|
---|
263 | ver2("ser wr %u:%d 0x%0*x", off, sz * 8, sz * 2, val);
|
---|
264 | }
|
---|