Skip to content

Commit 7969441

Browse files
committed
Refine event handling
This commit adds a member function for event handling to the backend interface, replacing the previous file-based system that relies on poll(2). The new design addresses issues created by backends like SDL, which do not expose underlying file descriptors. Integrating the fbdev backend into the updated interface remains to be completed. Close #4
1 parent 980bd4c commit 7969441

File tree

9 files changed

+66
-216
lines changed

9 files changed

+66
-216
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ libtwin.a_cflags-y :=
2323

2424
libtwin.a_files-y = \
2525
src/box.c \
26-
src/file.c \
2726
src/poly.c \
2827
src/toplevel.c \
2928
src/button.c \

apps/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ int main(void)
128128
20);
129129
#endif
130130

131-
twin_dispatch();
131+
twin_dispatch(tx);
132132

133133
return 0;
134134
}

backend/linux_input.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ static void *twin_linux_evdev_thread(void *arg)
144144
return NULL;
145145
}
146146

147-
static bool dummy(int file, twin_file_op_t ops, void *closure)
148-
{
149-
return true;
150-
}
151-
152147
void *twin_linux_input_create(twin_screen_t *screen)
153148
{
154149
/* Create object for handling Linux input system */
@@ -162,14 +157,6 @@ void *twin_linux_input_create(twin_screen_t *screen)
162157
tm->x = screen->width / 2;
163158
tm->y = screen->height / 2;
164159

165-
#if 1
166-
/* FIXME: Need to fix the unexpected termination of the program.
167-
* Hooking a dummy function here is only a hack*/
168-
169-
/* Set file handler for reading input device file */
170-
twin_set_file(dummy, tm->fd, TWIN_READ, tm);
171-
#endif
172-
173160
/* Start event handling thread */
174161
if (pthread_create(&tm->evdev_thread, NULL, twin_linux_evdev_thread, tm)) {
175162
log_error("Failed to create evdev thread");

backend/sdl.c

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -72,55 +72,6 @@ static void twin_sdl_damage(twin_screen_t *screen, twin_sdl_t *tx)
7272
twin_screen_damage(screen, 0, 0, width, height);
7373
}
7474

75-
static bool twin_sdl_read_events(int file maybe_unused,
76-
twin_file_op_t ops maybe_unused,
77-
void *closure)
78-
{
79-
twin_screen_t *screen = SCREEN(closure);
80-
twin_sdl_t *tx = PRIV(closure);
81-
82-
SDL_Event ev;
83-
while (SDL_PollEvent(&ev)) {
84-
twin_event_t tev;
85-
switch (ev.type) {
86-
case SDL_WINDOWEVENT:
87-
if (ev.window.event == SDL_WINDOWEVENT_EXPOSED ||
88-
ev.window.event == SDL_WINDOWEVENT_SHOWN) {
89-
twin_sdl_damage(screen, tx);
90-
}
91-
break;
92-
case SDL_QUIT:
93-
_twin_sdl_destroy(screen, tx);
94-
return false;
95-
case SDL_MOUSEBUTTONDOWN:
96-
case SDL_MOUSEBUTTONUP:
97-
tev.u.pointer.screen_x = ev.button.x;
98-
tev.u.pointer.screen_y = ev.button.y;
99-
tev.u.pointer.button =
100-
((ev.button.state >> 8) | (1 << (ev.button.button - 1)));
101-
tev.kind = ((ev.type == SDL_MOUSEBUTTONDOWN) ? TwinEventButtonDown
102-
: TwinEventButtonUp);
103-
twin_screen_dispatch(screen, &tev);
104-
break;
105-
case SDL_KEYDOWN:
106-
case SDL_KEYUP:
107-
tev.u.key.key = ev.key.keysym.sym;
108-
tev.kind = ((ev.key.type == SDL_KEYDOWN) ? TwinEventKeyDown
109-
: TwinEventKeyUp);
110-
twin_screen_dispatch(screen, &tev);
111-
break;
112-
case SDL_MOUSEMOTION:
113-
tev.u.pointer.screen_x = ev.motion.x;
114-
tev.u.pointer.screen_y = ev.motion.y;
115-
tev.kind = TwinEventMotion;
116-
tev.u.pointer.button = ev.motion.state;
117-
twin_screen_dispatch(screen, &tev);
118-
break;
119-
}
120-
}
121-
return true;
122-
}
123-
12475
static bool twin_sdl_work(void *closure)
12576
{
12677
twin_screen_t *screen = SCREEN(closure);
@@ -174,8 +125,6 @@ twin_context_t *twin_sdl_init(int width, int height)
174125
ctx->screen = twin_screen_create(width, height, _twin_sdl_put_begin,
175126
_twin_sdl_put_span, ctx);
176127

177-
twin_set_file(twin_sdl_read_events, 0, TWIN_READ, ctx);
178-
179128
twin_set_work(twin_sdl_work, TWIN_WORK_REDISPLAY, ctx);
180129

181130
return ctx;
@@ -195,6 +144,53 @@ static void twin_sdl_configure(twin_context_t *ctx)
195144
twin_screen_resize(ctx->screen, width, height);
196145
}
197146

147+
static bool twin_sdl_poll(twin_context_t *ctx)
148+
{
149+
twin_screen_t *screen = SCREEN(ctx);
150+
twin_sdl_t *tx = PRIV(ctx);
151+
152+
SDL_Event ev;
153+
while (SDL_PollEvent(&ev)) {
154+
twin_event_t tev;
155+
switch (ev.type) {
156+
case SDL_WINDOWEVENT:
157+
if (ev.window.event == SDL_WINDOWEVENT_EXPOSED ||
158+
ev.window.event == SDL_WINDOWEVENT_SHOWN) {
159+
twin_sdl_damage(screen, tx);
160+
}
161+
break;
162+
case SDL_QUIT:
163+
_twin_sdl_destroy(screen, tx);
164+
return false;
165+
case SDL_MOUSEBUTTONDOWN:
166+
case SDL_MOUSEBUTTONUP:
167+
tev.u.pointer.screen_x = ev.button.x;
168+
tev.u.pointer.screen_y = ev.button.y;
169+
tev.u.pointer.button =
170+
((ev.button.state >> 8) | (1 << (ev.button.button - 1)));
171+
tev.kind = ((ev.type == SDL_MOUSEBUTTONDOWN) ? TwinEventButtonDown
172+
: TwinEventButtonUp);
173+
twin_screen_dispatch(screen, &tev);
174+
break;
175+
case SDL_KEYDOWN:
176+
case SDL_KEYUP:
177+
tev.u.key.key = ev.key.keysym.sym;
178+
tev.kind = ((ev.key.type == SDL_KEYDOWN) ? TwinEventKeyDown
179+
: TwinEventKeyUp);
180+
twin_screen_dispatch(screen, &tev);
181+
break;
182+
case SDL_MOUSEMOTION:
183+
tev.u.pointer.screen_x = ev.motion.x;
184+
tev.u.pointer.screen_y = ev.motion.y;
185+
tev.kind = TwinEventMotion;
186+
tev.u.pointer.button = ev.motion.state;
187+
twin_screen_dispatch(screen, &tev);
188+
break;
189+
}
190+
}
191+
return true;
192+
}
193+
198194
static void twin_sdl_exit(twin_context_t *ctx)
199195
{
200196
if (!ctx)
@@ -209,5 +205,6 @@ static void twin_sdl_exit(twin_context_t *ctx)
209205
const twin_backend_t g_twin_backend = {
210206
.init = twin_sdl_init,
211207
.configure = twin_sdl_configure,
208+
.poll = twin_sdl_poll,
212209
.exit = twin_sdl_exit,
213210
};

include/twin.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,10 @@ typedef twin_time_t (*twin_timeout_proc_t)(twin_time_t now, void *closure);
455455

456456
typedef bool (*twin_work_proc_t)(void *closure);
457457

458-
typedef enum _twin_file_op { TWIN_READ = 1, TWIN_WRITE = 2 } twin_file_op_t;
459-
460-
typedef bool (*twin_file_proc_t)(int file, twin_file_op_t ops, void *closure);
461-
462458
#define twin_time_compare(a, op, b) (((a) - (b)) op 0)
463459

464460
typedef struct _twin_timeout twin_timeout_t;
465461
typedef struct _twin_work twin_work_t;
466-
typedef struct _twin_file twin_file_t;
467462

468463
/*
469464
* Widgets
@@ -585,6 +580,11 @@ struct _twin_scroll {
585580
twin_widget_t widget;
586581
};
587582

583+
typedef struct _twin_context {
584+
twin_screen_t *screen;
585+
void *priv;
586+
} twin_context_t;
587+
588588
/*
589589
* box.c
590590
*/
@@ -619,7 +619,7 @@ twin_pixmap_t *twin_make_cursor(int *hx, int *hy);
619619
* dispatch.c
620620
*/
621621

622-
void twin_dispatch(void);
622+
void twin_dispatch(twin_context_t *ctx);
623623

624624
/*
625625
* draw.c
@@ -654,17 +654,6 @@ void twin_premultiply_alpha(twin_pixmap_t *px);
654654

655655
void twin_event_enqueue(const twin_event_t *event);
656656

657-
/*
658-
* file.c
659-
*/
660-
661-
twin_file_t *twin_set_file(twin_file_proc_t file_proc,
662-
int file,
663-
twin_file_op_t ops,
664-
void *closure);
665-
666-
void twin_clear_file(twin_file_t *file);
667-
668657
/*
669658
* fixed.c
670659
*/
@@ -1170,11 +1159,6 @@ void twin_clear_work(twin_work_t *work);
11701159
* backend
11711160
*/
11721161

1173-
typedef struct _twin_context {
1174-
twin_screen_t *screen;
1175-
void *priv;
1176-
} twin_context_t;
1177-
11781162
twin_context_t *twin_create(int width, int height);
11791163

11801164
void twin_destroy(twin_context_t *ctx);

include/twin_private.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,6 @@ struct _twin_work {
472472
void *closure;
473473
};
474474

475-
struct _twin_file {
476-
twin_queue_t queue;
477-
int file;
478-
twin_file_op_t ops;
479-
twin_file_proc_t proc;
480-
void *closure;
481-
};
482-
483475
typedef enum _twin_order {
484476
TWIN_BEFORE = -1,
485477
TWIN_AT = 0,
@@ -504,8 +496,6 @@ twin_queue_t *_twin_queue_set_order(twin_queue_t **head);
504496

505497
void _twin_queue_review_order(twin_queue_t *first);
506498

507-
int _twin_run_file(twin_time_t delay);
508-
509499
void _twin_run_timeout(void);
510500

511501
twin_time_t _twin_timeout_delay(void);

src/dispatch.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
* All rights reserved.
55
*/
66

7+
#include <unistd.h>
8+
9+
#include "twin_backend.h"
710
#include "twin_private.h"
811

9-
void twin_dispatch(void)
12+
extern twin_backend_t g_twin_backend;
13+
14+
void twin_dispatch(twin_context_t *ctx)
1015
{
1116
for (;;) {
1217
_twin_run_timeout();
1318
_twin_run_work();
14-
if (!_twin_run_file(_twin_timeout_delay()))
19+
if (!g_twin_backend.poll(ctx)) {
20+
usleep(_twin_timeout_delay() * 1000);
1521
break;
22+
}
1623
}
1724
}

0 commit comments

Comments
 (0)