Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
glib: add helper functions to convert flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Chiossi committed Sep 10, 2015
1 parent e1d652e commit 16b4a76
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/lib/common/sol-mainloop-impl-glib.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,41 +135,54 @@ struct fd_wrap_data {
const void *data;
};

static gboolean
on_fd(gint fd, GIOCondition cond, gpointer data)
static GIOCondition
sol_to_glib_flags(unsigned int sol_flags)
{
GIOCondition glib_flags = 0;

if (sol_flags & SOL_FD_FLAGS_IN) glib_flags |= G_IO_IN;
if (sol_flags & SOL_FD_FLAGS_OUT) glib_flags |= G_IO_OUT;
if (sol_flags & SOL_FD_FLAGS_PRI) glib_flags |= G_IO_PRI;
if (sol_flags & SOL_FD_FLAGS_ERR) glib_flags |= G_IO_ERR;
if (sol_flags & SOL_FD_FLAGS_HUP) glib_flags |= G_IO_HUP;
if (sol_flags & SOL_FD_FLAGS_NVAL) glib_flags |= G_IO_NVAL;

return glib_flags;
}

static unsigned int
glib_to_sol_flags(GIOCondition glib_flags)
{
struct fd_wrap_data *wrap_data = data;
unsigned int sol_flags = 0;

if (cond & G_IO_IN) sol_flags |= SOL_FD_FLAGS_IN;
if (cond & G_IO_OUT) sol_flags |= SOL_FD_FLAGS_OUT;
if (cond & G_IO_PRI) sol_flags |= SOL_FD_FLAGS_PRI;
if (cond & G_IO_ERR) sol_flags |= SOL_FD_FLAGS_ERR;
if (cond & G_IO_HUP) sol_flags |= SOL_FD_FLAGS_HUP;
if (cond & G_IO_NVAL) sol_flags |= SOL_FD_FLAGS_NVAL;
if (glib_flags & G_IO_IN) sol_flags |= SOL_FD_FLAGS_IN;
if (glib_flags & G_IO_OUT) sol_flags |= SOL_FD_FLAGS_OUT;
if (glib_flags & G_IO_PRI) sol_flags |= SOL_FD_FLAGS_PRI;
if (glib_flags & G_IO_ERR) sol_flags |= SOL_FD_FLAGS_ERR;
if (glib_flags & G_IO_HUP) sol_flags |= SOL_FD_FLAGS_HUP;
if (glib_flags & G_IO_NVAL) sol_flags |= SOL_FD_FLAGS_NVAL;

return wrap_data->cb((void *)wrap_data->data, fd, sol_flags);
return sol_flags;
}

static gboolean
on_fd(gint fd, GIOCondition cond, gpointer data)
{
struct fd_wrap_data *wrap_data = data;
return wrap_data->cb((void *)wrap_data->data, fd, glib_to_sol_flags(cond));
}

void *
sol_mainloop_impl_fd_add(int fd, unsigned int flags, bool (*cb)(void *data, int fd, unsigned int active_flags), const void *data)
{
GIOCondition glib_flags = 0;
struct fd_wrap_data *wrap_data = malloc(sizeof(*wrap_data));

SOL_NULL_CHECK(wrap_data, NULL);

wrap_data->cb = cb;
wrap_data->data = data;

if (flags & SOL_FD_FLAGS_IN) glib_flags |= G_IO_IN;
if (flags & SOL_FD_FLAGS_OUT) glib_flags |= G_IO_OUT;
if (flags & SOL_FD_FLAGS_PRI) glib_flags |= G_IO_PRI;
if (flags & SOL_FD_FLAGS_ERR) glib_flags |= G_IO_ERR;
if (flags & SOL_FD_FLAGS_HUP) glib_flags |= G_IO_HUP;
if (flags & SOL_FD_FLAGS_NVAL) glib_flags |= G_IO_NVAL;

return (void *)(long)g_unix_fd_add_full(0, fd, glib_flags, on_fd, wrap_data,
return (void *)(long)g_unix_fd_add_full(0, fd, sol_to_glib_flags(flags), on_fd, wrap_data,
free);
}

Expand Down

0 comments on commit 16b4a76

Please sign in to comment.