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>
1314#include <SDL3/SDL.h>
1415#include <fonts/NotoSans14.h>
1516
17+
18+
1619static 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
3238void 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 ;
0 commit comments