Skip to content

Commit 083d4c3

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 083d4c3

File tree

8 files changed

+63
-185
lines changed

8 files changed

+63
-185
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,9 @@ twin_pixmap_t *twin_make_cursor(int *hx, int *hy);
619619
* dispatch.c
620620
*/
621621

622-
void twin_dispatch(void);
622+
struct _twin_context;
623+
624+
void twin_dispatch(struct _twin_context *ctx);
623625

624626
/*
625627
* draw.c

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
}

src/file.c

Lines changed: 0 additions & 116 deletions
This file was deleted.

src/twin_backend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ typedef struct twin_backend {
1919
/* FIXME: Refine the parameter list */
2020
void (*configure)(twin_context_t *ctx);
2121

22+
bool (*poll)(twin_context_t *ctx);
23+
2224
/* Device cleanup when drawing is done */
2325
void (*exit)(twin_context_t *ctx);
2426
} twin_backend_t;

0 commit comments

Comments
 (0)