Use an id for watches

This commit is contained in:
Kovid Goyal 2018-07-09 15:25:19 +05:30
parent 3d6995d1e6
commit 3797294838
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 25 additions and 14 deletions

16
glfw/backend_utils.c vendored
View File

@ -28,22 +28,24 @@ update_fds(EventLoopData *eld) {
}
}
int
static id_type watch_counter = 0;
id_type
addWatch(EventLoopData *eld, int fd, int events, int enabled, watch_callback_func cb, void *cb_data) {
removeWatch(eld, fd, events);
if (eld->watches_count >= sizeof(eld->watches)/sizeof(eld->watches[0])) return 0;
Watch *w = eld->watches + eld->watches_count++;
w->fd = fd; w->events = events; w->enabled = enabled;
w->callback = cb;
w->callback_data = cb_data;
w->id = ++watch_counter;
update_fds(eld);
return 1;
return w->id;
}
void
removeWatch(EventLoopData *eld, int fd, int events) {
removeWatch(EventLoopData *eld, id_type watch_id) {
for (nfds_t i = 0; i < eld->watches_count; i++) {
if (eld->watches[i].fd == fd && eld->watches[i].events == events) {
if (eld->watches[i].id == watch_id) {
eld->watches_count--;
if (i < eld->watches_count) {
memmove(eld->watches + i, eld->watches + i + 1, sizeof(eld->watches[0]) * (eld->watches_count - i));
@ -55,9 +57,9 @@ removeWatch(EventLoopData *eld, int fd, int events) {
}
void
toggleWatch(EventLoopData *eld, int fd, int events, int enabled) {
toggleWatch(EventLoopData *eld, id_type watch_id, int enabled) {
for (nfds_t i = 0; i < eld->watches_count; i++) {
if (eld->watches[i].fd == fd && eld->watches[i].events == events) {
if (eld->watches[i].fd == watch_id) {
if (eld->watches[i].enabled != enabled) {
eld->watches[i].enabled = enabled;
update_fds(eld);

View File

@ -29,11 +29,13 @@
#include <unistd.h>
typedef void(*watch_callback_func)(int, int, void*);
typedef unsigned long long id_type;
typedef struct {
int fd, events, enabled, ready;
watch_callback_func callback;
void *callback_data;
id_type id;
} Watch;
typedef struct {
@ -44,9 +46,9 @@ typedef struct {
} EventLoopData;
int addWatch(EventLoopData *eld, int fd, int events, int enabled, watch_callback_func cb, void *cb_data);
void removeWatch(EventLoopData *eld, int fd, int events);
void toggleWatch(EventLoopData *eld, int fd, int events, int enabled);
id_type addWatch(EventLoopData *eld, int fd, int events, int enabled, watch_callback_func cb, void *cb_data);
void removeWatch(EventLoopData *eld, id_type watch_id);
void toggleWatch(EventLoopData *eld, id_type watch_id, int enabled);
void prepareForPoll(EventLoopData *eld);
int pollWithTimeout(struct pollfd *fds, nfds_t nfds, double timeout);
void dispatchEvents(EventLoopData *eld);

15
glfw/dbus_glfw.c vendored
View File

@ -27,6 +27,7 @@
#include "internal.h"
#include "dbus_glfw.h"
#include <stdlib.h>
static inline void
report_error(DBusError *err, const char *fmt, ...) {
@ -71,18 +72,24 @@ events_for_watch(DBusWatch *watch) {
static dbus_bool_t
add_dbus_watch(DBusWatch *watch, void *data) {
if (addWatch(dbus_data->eld, dbus_watch_get_unix_fd(watch), events_for_watch(watch), dbus_watch_get_enabled(watch), on_dbus_watch_ready, watch)) return TRUE;
return FALSE;
id_type watch_id = addWatch(dbus_data->eld, dbus_watch_get_unix_fd(watch), events_for_watch(watch), dbus_watch_get_enabled(watch), on_dbus_watch_ready, watch);
if (!watch_id) return FALSE;
id_type *idp = malloc(sizeof(id_type));
if (!idp) return FALSE;
dbus_watch_set_data(watch, idp, free);
return TRUE;
}
static void
remove_dbus_watch(DBusWatch *watch, void *data) {
removeWatch(dbus_data->eld, dbus_watch_get_unix_fd(watch), events_for_watch(watch));
id_type *idp = dbus_watch_get_data(watch);
if (idp) removeWatch(dbus_data->eld, *idp);
}
static void
toggle_dbus_watch(DBusWatch *watch, void *data) {
toggleWatch(dbus_data->eld, dbus_watch_get_unix_fd(watch), events_for_watch(watch), dbus_watch_get_enabled(watch));
id_type *idp = dbus_watch_get_data(watch);
if (idp) toggleWatch(dbus_data->eld, *idp, dbus_watch_get_enabled(watch));
}
DBusConnection*