Skip to content

Commit 5c3b34d

Browse files
committed
FBDev improvements
Added: Basic keyboard support Added: More fonts, especially large ones for presentations
1 parent cae8c54 commit 5c3b34d

17 files changed

+9241
-7
lines changed

src/fbdev/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ add_application(fbdev
1212
fonts/HackNerdMono14b.c
1313
fonts/HackNerdMono14bi.c
1414
fonts/HackNerdMono14i.c
15-
pointer.c)
15+
fonts/HackNerdMono32b.c
16+
fonts/HackNerdMono64b.c
17+
fonts/Roboto32.c
18+
fonts/Roboto48b.c
19+
fonts/RobotoBlack64b.c
20+
pointer.c
21+
keyboard.c
22+
)
1623
target_link_libraries(fbdev SDL3::SDL3 stdlib)
1724

1825

src/fbdev/fbdev.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "fbdev.h"
22
#include "fbdev_impl.h"
33
#include "pointer.h"
4+
#include "keyboard.h"
45

56
#include <cmrx/application.h>
67
#include <stdio.h>
@@ -13,6 +14,8 @@
1314
#include <SDL3/SDL.h>
1415
#include <fonts/NotoSans14.h>
1516

17+
18+
1619
static struct FBDevImpl fbdev_impl = {
1720
.running = true,
1821
.window = NULL,
@@ -26,7 +29,10 @@ struct FBDev fbdev = {
2629
.impl = &fbdev_impl,
2730
.do_cull = false,
2831
.cull_area = { .col = 0, .row = 0, .width = WINDOW_WIDTH, .height = WINDOW_HEIGHT },
29-
.current_font = &noto_sans_14
32+
.current_font = &noto_sans_14,
33+
.scale = 1.,
34+
.win_pad_left = 0,
35+
.win_pad_top = 0
3036
};
3137

3238
void fbdev_blend(struct FBDev * fb, unsigned col, unsigned row, uint32_t rgba)
@@ -82,7 +88,16 @@ SDL_AppResult SDL_AppEvent(struct FBDev * device, SDL_Event *event)
8288
case SDL_EVENT_MOUSE_MOTION:
8389
pointer.pos.col = event->motion.x;
8490
pointer.pos.row = event->motion.y;
85-
generate_interrupt(15);
91+
if (device->scale != 1. || device->win_pad_left != 0 || device->win_pad_top != 0)
92+
{
93+
pointer.pos.col = (pointer.pos.col - device->win_pad_left) / device->scale;
94+
pointer.pos.row = (pointer.pos.row - device->win_pad_top) / device->scale;
95+
}
96+
if (pointer.pos.col < WINDOW_WIDTH && pointer.pos.row < WINDOW_HEIGHT)
97+
{
98+
// Generate interrupt only if cursor is within visible area
99+
generate_interrupt(15);
100+
}
86101
break;
87102

88103
case SDL_EVENT_MOUSE_BUTTON_DOWN:
@@ -95,6 +110,49 @@ SDL_AppResult SDL_AppEvent(struct FBDev * device, SDL_Event *event)
95110
generate_interrupt(15);
96111
break;
97112

113+
case SDL_EVENT_KEY_DOWN:
114+
switch (event->key.scancode) {
115+
// case SDL_SCANCODE_TAB:
116+
case SDL_SCANCODE_CAPSLOCK:
117+
case SDL_SCANCODE_LSHIFT:
118+
case SDL_SCANCODE_RSHIFT:
119+
case SDL_SCANCODE_LALT:
120+
case SDL_SCANCODE_RALT:
121+
case SDL_SCANCODE_LCTRL:
122+
case SDL_SCANCODE_RCTRL:
123+
// case SDL_SCANCODE_ESCAPE:
124+
break;
125+
126+
default:
127+
keyboard.key = SDL_GetKeyFromScancode(event->key.scancode, event->key.mod, false);
128+
generate_interrupt(14);
129+
}
130+
131+
break;
132+
133+
case SDL_EVENT_WINDOW_RESIZED:
134+
printf("New window size is %dx%d\n", event->window.data1, event->window.data2);
135+
double new_width = event->window.data1;
136+
double new_height = event->window.data2;
137+
double new_aspect = new_width / new_height;
138+
static const double default_aspect = (double) WINDOW_WIDTH / (double) WINDOW_HEIGHT;
139+
140+
if (new_aspect < default_aspect)
141+
{
142+
// 16:10, 16:11, ....
143+
device->scale = (double) new_width / (double) WINDOW_WIDTH;
144+
device->win_pad_top = (new_height - (new_width / default_aspect)) / 2.;
145+
device->win_pad_left = 0;
146+
}
147+
else
148+
{
149+
device->scale = (double) new_height / (double) WINDOW_HEIGHT;
150+
device->win_pad_left = (new_width - (new_height * default_aspect )) / 2.;
151+
device->win_pad_top = 0;
152+
}
153+
printf("Scale = %f\nPadding left = %d\nPadding top = %d\n", device->scale, device->win_pad_left, device->win_pad_top);
154+
break;
155+
98156
default:
99157
// ignore
100158
break;

src/fbdev/fbdev.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ enum FBFont {
3838
FONT_NORMAL = 0,
3939
FONT_BOLD = 1 << 0,
4040
FONT_ITALIC = 1 << 1,
41-
FONT_MONO = 1 << 2
41+
FONT_MONO = 1 << 2,
42+
FONT_SIZE_14 = 0,
43+
FONT_SIZE_32 = 1 << 3,
44+
FONT_SIZE_48 = 1 << 4,
45+
FONT_SIZE_64 = 1 << 5
4246
};
4347

4448
struct FBDevVTable {
@@ -62,6 +66,8 @@ struct FBDev {
6266
bool do_cull;
6367
struct FBRectangle cull_area;
6468
const struct text_font_t * current_font;
69+
double scale;
70+
unsigned win_pad_left, win_pad_top;
6571
};
6672

6773
extern struct FBDev fbdev;

src/fbdev/fbdev_api.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
#include <fonts/HackNerdMono14b.h>
1414
#include <fonts/HackNerdMono14bi.h>
1515
#include <fonts/HackNerdMono14i.h>
16+
#include <fonts/HackNerdMono32b.h>
17+
#include <fonts/HackNerdMono64b.h>
18+
#include <fonts/Roboto32.h>
19+
#include <fonts/Roboto48b.h>
20+
#include <fonts/RobotoBlack64b.h>
1621

1722
#include <assert.h>
1823
#include <stdio.h>
@@ -66,7 +71,11 @@ static void fbdev_blit(INSTANCE(this), const struct FBRectangle * destination, c
6671
assert(col < WINDOW_WIDTH && row < WINDOW_HEIGHT);
6772
if (col < WINDOW_WIDTH && row < WINDOW_HEIGHT)
6873
{
69-
fb[(row + destination->row) * WINDOW_WIDTH + (col + destination->col)] = buffer[(row % texture->height) * texture->width + (col % texture->width)];
74+
uint32_t pixel = buffer[(row % texture->height) * texture->width + (col % texture->width)];
75+
if (pixel && 0xFF != 0)
76+
{
77+
fb[(row + destination->row) * WINDOW_WIDTH + (col + destination->col)] = pixel;
78+
}
7079
}
7180
}
7281
}
@@ -210,7 +219,12 @@ FontEntry fonts[] = {
210219
{ FONT_MONO, &hack_nerd_14 },
211220
{ FONT_MONO | FONT_BOLD, &hack_nerd_14_b },
212221
{ FONT_MONO | FONT_ITALIC, &hack_nerd_14_i },
213-
{ FONT_MONO | FONT_BOLD | FONT_ITALIC, &hack_nerd_14_bi }
222+
{ FONT_MONO | FONT_BOLD | FONT_ITALIC, &hack_nerd_14_bi },
223+
{ FONT_MONO | FONT_BOLD | FONT_SIZE_32, &hack_nerd_32_b },
224+
{ FONT_MONO | FONT_BOLD | FONT_SIZE_64, &hack_nerd_64_b },
225+
{ FONT_SIZE_32, &roboto_32 },
226+
{ FONT_BOLD | FONT_SIZE_64, &roboto_black_64_b },
227+
{ FONT_BOLD | FONT_SIZE_48, &roboto_48_b },
214228
};
215229

216230
static const text_font * fbdev_select_font(unsigned font)

src/fbdev/fbdev_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// SDL window dimensions = framebuffer screen dimensions
88
#define WINDOW_WIDTH 1280
9-
#define WINDOW_HEIGHT 800
9+
#define WINDOW_HEIGHT 720
1010

1111
struct FBDevImpl {
1212
bool running;

0 commit comments

Comments
 (0)