Skip to content

Commit 3b6f986

Browse files
committed
support window resize event
1 parent fe1e397 commit 3b6f986

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

src/main.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333

3434
/* Arbitrary sizes */
3535
#define DRAW_BUF_SIZ 20 * 1024
36-
37-
#define REDRAW_TIMEOUT (80 * 1000) /* 80 ms */
36+
// #define REDRAW_TIMEOUT (80 * 1000) /* 80 ms */
3837

3938
/* macros */
4039
#define TIMEDIFF(t1, t2) ((t1.tv_sec - t2.tv_sec) * 1000 + (t1.tv_usec - t2.tv_usec) / 1000)
@@ -227,19 +226,27 @@ void sdl_shutdown(void) {
227226

228227
void window_event_handler(SDL_Event *event) {
229228
#ifdef BR2
230-
return; // no resize for BR2 handheld devices builds because of kms video driver
229+
return; // no window events for BR2 handheld devices builds because of kms video driver
231230
#endif
232231
switch (event->window.event) {
233232
case SDL_WINDOWEVENT_RESIZED:
234-
scale_to_size(event->window.data1, event->window.data2);
233+
scale_to_size((int)(event->window.data1 / opt_scale), (int)(event->window.data2 / opt_scale));
234+
break;
235+
case SDL_WINDOWEVENT_FOCUS_GAINED:
236+
main_window.state |= WIN_FOCUSED;
237+
// redraw(); // redraw to update cursor color
238+
break;
239+
case SDL_WINDOWEVENT_FOCUS_LOST:
240+
main_window.state &= ~WIN_FOCUSED;
241+
//redraw(); // redraw to update cursor color
235242
break;
236243
default:
237244
break;
238245
}
239246
}
240247

241248
void scale_to_size(int width, int height) {
242-
if (width <= 0 || height <= 0 || width > 8192 || height > 8192) return;
249+
if (width <= 100 || height <= 80 || width > 8192 || height > 8192) return;
243250
main_window.width = width;
244251
main_window.height = height;
245252
printf("Set scale to size: %dx%d (x%.1f)\n", main_window.width, main_window.height, opt_scale);
@@ -277,6 +284,7 @@ void scale_to_size(int width, int height) {
277284
t_resize(col, row);
278285
x_resize(col, row);
279286
tty_resize();
287+
redraw();
280288
}
281289

282290
void sdl_init(void) {
@@ -315,7 +323,7 @@ void sdl_init(void) {
315323
printf("Setting resolution to: %dx%d\n", main_window.width, main_window.height);
316324
}
317325

318-
main_window.window = SDL_CreateWindow("Simple Terminal", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, main_window.width, main_window.height, SDL_WINDOW_SHOWN);
326+
main_window.window = SDL_CreateWindow("Simple Terminal", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, main_window.width, main_window.height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
319327
if (!main_window.window) {
320328
fprintf(stderr, "Unable to create window: %s\n", SDL_GetError());
321329
exit(EXIT_FAILURE);
@@ -686,11 +694,10 @@ void x_draw_cursor(void) {
686694
}
687695

688696
void redraw(void) {
689-
struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
690-
697+
//struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
691698
t_full_dirt();
692699
draw();
693-
nanosleep(&tv, NULL);
700+
//nanosleep(&tv, NULL);
694701
}
695702

696703
void draw(void) {
@@ -1066,14 +1073,8 @@ void main_loop(void) {
10661073
continue; // skip mouse events
10671074
}
10681075
if (ev.type == SDL_WINDOWEVENT) {
1069-
// if (ev.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
1070-
// main_window.state |= WIN_FOCUSED;
1071-
// draw(); // redraw to update cursor color
1072-
// } else if (ev.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
1073-
// main_window.state &= ~WIN_FOCUSED;
1074-
// draw(); // redraw to update cursor color
1075-
// }
1076-
continue; // skip other window events for now
1076+
window_event_handler(&ev);
1077+
continue;
10771078
}
10781079

10791080
if (ev.type == SDL_KEYDOWN || ev.type == SDL_KEYUP) {

0 commit comments

Comments
 (0)