diff --git a/Makefile b/Makefile index b28a89a..8a612ec 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,6 @@ libtwin.a_cflags-y := libtwin.a_files-y = \ src/box.c \ - src/file.c \ src/poly.c \ src/toplevel.c \ src/button.c \ diff --git a/apps/main.c b/apps/main.c index 3b8120f..afd3dac 100644 --- a/apps/main.c +++ b/apps/main.c @@ -128,7 +128,7 @@ int main(void) 20); #endif - twin_dispatch(); + twin_dispatch(tx); return 0; } diff --git a/backend/linux_input.c b/backend/linux_input.c index b673588..4cd080d 100644 --- a/backend/linux_input.c +++ b/backend/linux_input.c @@ -284,11 +284,6 @@ static void *twin_linux_evdev_thread(void *arg) return NULL; } -static bool dummy(int file, twin_file_op_t ops, void *closure) -{ - return true; -} - void *twin_linux_input_create(twin_screen_t *screen) { /* Create object for handling Linux input system */ @@ -302,14 +297,6 @@ void *twin_linux_input_create(twin_screen_t *screen) tm->x = screen->width / 2; tm->y = screen->height / 2; -#if 1 - /* FIXME: Need to fix the unexpected termination of the program. - * Hooking a dummy function here is only a hack*/ - - /* Set file handler for reading input device file */ - twin_set_file(dummy, tm->fd, TWIN_READ, tm); -#endif - /* Start event handling thread */ if (pthread_create(&tm->evdev_thread, NULL, twin_linux_evdev_thread, tm)) { log_error("Failed to create evdev thread"); diff --git a/backend/sdl.c b/backend/sdl.c index dc1061a..879d65f 100644 --- a/backend/sdl.c +++ b/backend/sdl.c @@ -72,55 +72,6 @@ static void twin_sdl_damage(twin_screen_t *screen, twin_sdl_t *tx) twin_screen_damage(screen, 0, 0, width, height); } -static bool twin_sdl_read_events(int file maybe_unused, - twin_file_op_t ops maybe_unused, - void *closure) -{ - twin_screen_t *screen = SCREEN(closure); - twin_sdl_t *tx = PRIV(closure); - - SDL_Event ev; - while (SDL_PollEvent(&ev)) { - twin_event_t tev; - switch (ev.type) { - case SDL_WINDOWEVENT: - if (ev.window.event == SDL_WINDOWEVENT_EXPOSED || - ev.window.event == SDL_WINDOWEVENT_SHOWN) { - twin_sdl_damage(screen, tx); - } - break; - case SDL_QUIT: - _twin_sdl_destroy(screen, tx); - return false; - case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEBUTTONUP: - tev.u.pointer.screen_x = ev.button.x; - tev.u.pointer.screen_y = ev.button.y; - tev.u.pointer.button = - ((ev.button.state >> 8) | (1 << (ev.button.button - 1))); - tev.kind = ((ev.type == SDL_MOUSEBUTTONDOWN) ? TwinEventButtonDown - : TwinEventButtonUp); - twin_screen_dispatch(screen, &tev); - break; - case SDL_KEYDOWN: - case SDL_KEYUP: - tev.u.key.key = ev.key.keysym.sym; - tev.kind = ((ev.key.type == SDL_KEYDOWN) ? TwinEventKeyDown - : TwinEventKeyUp); - twin_screen_dispatch(screen, &tev); - break; - case SDL_MOUSEMOTION: - tev.u.pointer.screen_x = ev.motion.x; - tev.u.pointer.screen_y = ev.motion.y; - tev.kind = TwinEventMotion; - tev.u.pointer.button = ev.motion.state; - twin_screen_dispatch(screen, &tev); - break; - } - } - return true; -} - static bool twin_sdl_work(void *closure) { twin_screen_t *screen = SCREEN(closure); @@ -174,8 +125,6 @@ twin_context_t *twin_sdl_init(int width, int height) ctx->screen = twin_screen_create(width, height, _twin_sdl_put_begin, _twin_sdl_put_span, ctx); - twin_set_file(twin_sdl_read_events, 0, TWIN_READ, ctx); - twin_set_work(twin_sdl_work, TWIN_WORK_REDISPLAY, ctx); return ctx; @@ -195,6 +144,53 @@ static void twin_sdl_configure(twin_context_t *ctx) twin_screen_resize(ctx->screen, width, height); } +static bool twin_sdl_poll(twin_context_t *ctx) +{ + twin_screen_t *screen = SCREEN(ctx); + twin_sdl_t *tx = PRIV(ctx); + + SDL_Event ev; + while (SDL_PollEvent(&ev)) { + twin_event_t tev; + switch (ev.type) { + case SDL_WINDOWEVENT: + if (ev.window.event == SDL_WINDOWEVENT_EXPOSED || + ev.window.event == SDL_WINDOWEVENT_SHOWN) { + twin_sdl_damage(screen, tx); + } + break; + case SDL_QUIT: + _twin_sdl_destroy(screen, tx); + return false; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + tev.u.pointer.screen_x = ev.button.x; + tev.u.pointer.screen_y = ev.button.y; + tev.u.pointer.button = + ((ev.button.state >> 8) | (1 << (ev.button.button - 1))); + tev.kind = ((ev.type == SDL_MOUSEBUTTONDOWN) ? TwinEventButtonDown + : TwinEventButtonUp); + twin_screen_dispatch(screen, &tev); + break; + case SDL_KEYDOWN: + case SDL_KEYUP: + tev.u.key.key = ev.key.keysym.sym; + tev.kind = ((ev.key.type == SDL_KEYDOWN) ? TwinEventKeyDown + : TwinEventKeyUp); + twin_screen_dispatch(screen, &tev); + break; + case SDL_MOUSEMOTION: + tev.u.pointer.screen_x = ev.motion.x; + tev.u.pointer.screen_y = ev.motion.y; + tev.kind = TwinEventMotion; + tev.u.pointer.button = ev.motion.state; + twin_screen_dispatch(screen, &tev); + break; + } + } + return true; +} + static void twin_sdl_exit(twin_context_t *ctx) { if (!ctx) @@ -209,5 +205,6 @@ static void twin_sdl_exit(twin_context_t *ctx) const twin_backend_t g_twin_backend = { .init = twin_sdl_init, .configure = twin_sdl_configure, + .poll = twin_sdl_poll, .exit = twin_sdl_exit, }; diff --git a/backend/vnc.c b/backend/vnc.c index 0246571..6d97343 100644 --- a/backend/vnc.c +++ b/backend/vnc.c @@ -108,14 +108,6 @@ static void _twin_vnc_new_client(struct nvnc_client *client) nvnc_set_userdata(client, peer, NULL); } -static bool _twin_vnc_read_events(int fd, twin_file_op_t op, void *closure) -{ - (void) fd; - (void) op; - (void) closure; - return true; -} - static void _twin_vnc_pointer_event(struct nvnc_client *client, uint16_t x, uint16_t y, @@ -240,8 +232,6 @@ twin_context_t *twin_vnc_init(int width, int height) log_error("Failed to init VNC framebuffer"); goto bail_framebuffer; } - int aml_fd = aml_get_fd(tx->aml); - twin_set_file(_twin_vnc_read_events, aml_fd, TWIN_READ, tx); twin_set_work(_twin_vnc_work, TWIN_WORK_REDISPLAY, ctx); tx->screen = ctx->screen; diff --git a/include/twin.h b/include/twin.h index 356ccaa..c0be071 100644 --- a/include/twin.h +++ b/include/twin.h @@ -455,15 +455,10 @@ typedef twin_time_t (*twin_timeout_proc_t)(twin_time_t now, void *closure); typedef bool (*twin_work_proc_t)(void *closure); -typedef enum _twin_file_op { TWIN_READ = 1, TWIN_WRITE = 2 } twin_file_op_t; - -typedef bool (*twin_file_proc_t)(int file, twin_file_op_t ops, void *closure); - #define twin_time_compare(a, op, b) (((a) - (b)) op 0) typedef struct _twin_timeout twin_timeout_t; typedef struct _twin_work twin_work_t; -typedef struct _twin_file twin_file_t; /* * Widgets @@ -585,6 +580,11 @@ struct _twin_scroll { twin_widget_t widget; }; +typedef struct _twin_context { + twin_screen_t *screen; + void *priv; +} twin_context_t; + /* * box.c */ @@ -619,7 +619,7 @@ twin_pixmap_t *twin_make_cursor(int *hx, int *hy); * dispatch.c */ -void twin_dispatch(void); +void twin_dispatch(twin_context_t *ctx); /* * draw.c @@ -654,17 +654,6 @@ void twin_premultiply_alpha(twin_pixmap_t *px); void twin_event_enqueue(const twin_event_t *event); -/* - * file.c - */ - -twin_file_t *twin_set_file(twin_file_proc_t file_proc, - int file, - twin_file_op_t ops, - void *closure); - -void twin_clear_file(twin_file_t *file); - /* * fixed.c */ @@ -1170,11 +1159,6 @@ void twin_clear_work(twin_work_t *work); * backend */ -typedef struct _twin_context { - twin_screen_t *screen; - void *priv; -} twin_context_t; - twin_context_t *twin_create(int width, int height); void twin_destroy(twin_context_t *ctx); diff --git a/include/twin_private.h b/include/twin_private.h index 0dffb54..16b42fd 100644 --- a/include/twin_private.h +++ b/include/twin_private.h @@ -472,14 +472,6 @@ struct _twin_work { void *closure; }; -struct _twin_file { - twin_queue_t queue; - int file; - twin_file_op_t ops; - twin_file_proc_t proc; - void *closure; -}; - typedef enum _twin_order { TWIN_BEFORE = -1, TWIN_AT = 0, @@ -504,8 +496,6 @@ twin_queue_t *_twin_queue_set_order(twin_queue_t **head); void _twin_queue_review_order(twin_queue_t *first); -int _twin_run_file(twin_time_t delay); - void _twin_run_timeout(void); twin_time_t _twin_timeout_delay(void); diff --git a/src/dispatch.c b/src/dispatch.c index 613b898..d1f8aa4 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -4,14 +4,21 @@ * All rights reserved. */ +#include + +#include "twin_backend.h" #include "twin_private.h" -void twin_dispatch(void) +extern twin_backend_t g_twin_backend; + +void twin_dispatch(twin_context_t *ctx) { for (;;) { _twin_run_timeout(); _twin_run_work(); - if (!_twin_run_file(_twin_timeout_delay())) + if (g_twin_backend.poll && !g_twin_backend.poll(ctx)) { + usleep(_twin_timeout_delay() * 1000); break; + } } } diff --git a/src/file.c b/src/file.c deleted file mode 100644 index 20f3660..0000000 --- a/src/file.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Twin - A Tiny Window System - * Copyright (c) 2004 Keith Packard - * All rights reserved. - */ - -#include -#include -#include - -#ifdef USE_POLL -#include -#endif - -#include "twin_private.h" - -static twin_queue_t *head; - -static twin_order_t _twin_file_order(twin_queue_t *a, twin_queue_t *b) -{ - twin_file_t *af = (twin_file_t *) a; - twin_file_t *bf = (twin_file_t *) b; - - if (af->file < bf->file) - return TWIN_BEFORE; - if (af->file > bf->file) - return TWIN_AFTER; - return TWIN_AT; -} - -int _twin_run_file(twin_time_t delay) -{ - twin_file_t *first; - twin_file_t *file; - int n; - int i; -#ifdef USE_POLL - struct pollfd *polls; -#endif - - first = (twin_file_t *) _twin_queue_set_order(&head); - if (first) { - for (file = first, n = 0; file; - file = (twin_file_t *) file->queue.order, n++) - ; -#ifdef USE_POLL - polls = malloc(n * sizeof(struct pollfd)); - if (!polls) - return 1; - for (file = first, i = 0; file; - file = (twin_file_t *) file->queue.order, i++) { - short events = 0; - - if (file->ops & TWIN_READ) - events |= POLLIN; - if (file->ops & TWIN_WRITE) - events |= POLLOUT; - polls[i].fd = file->file; - polls[i].events = events; - } - int r = poll(polls, n, delay); - if (r > 0) -#endif - for (file = first, i = 0; file; - file = (twin_file_t *) file->queue.order, i++) { - twin_file_op_t op = 0; -#ifdef USE_POLL - short events = polls[i].revents; - assert(polls[i].fd == file->file); - if (events & POLLIN) - op |= TWIN_READ; - if (events & POLLOUT) - op |= TWIN_WRITE; -#endif - if ( -#ifdef USE_POLL - op && -#endif - !(*file->proc)(file->file, op, file->closure)) - _twin_queue_delete(&head, &file->queue); - } - _twin_queue_review_order(&first->queue); -#ifdef USE_POLL - free(polls); -#endif - return 1; - } else { - if (delay > 0) - usleep(delay * 1000); - return 0; - } -} - -twin_file_t *twin_set_file(twin_file_proc_t file_proc, - int fd, - twin_file_op_t ops, - void *closure) -{ - twin_file_t *file = malloc(sizeof(twin_file_t)); - - if (!file) - return 0; - - file->file = fd; - file->proc = file_proc; - file->ops = ops; - file->closure = closure; - - _twin_queue_insert(&head, _twin_file_order, &file->queue); - return file; -} - -void twin_clear_file(twin_file_t *file) -{ - _twin_queue_delete(&head, &file->queue); -} diff --git a/src/twin_backend.h b/src/twin_backend.h index 793a57a..5f02da1 100644 --- a/src/twin_backend.h +++ b/src/twin_backend.h @@ -19,6 +19,8 @@ typedef struct twin_backend { /* FIXME: Refine the parameter list */ void (*configure)(twin_context_t *ctx); + bool (*poll)(twin_context_t *ctx); + /* Device cleanup when drawing is done */ void (*exit)(twin_context_t *ctx); } twin_backend_t;