Commit 60d14a6a authored by Alyssa Rosenzweig's avatar Alyssa Rosenzweig 💜

Refactor event loop

parent 501e0631
#include "event-loop.h"
#include "purple.h"
#include <glib.h>
/**
* The following eventloop functions are used in both pidgin and purple-text. If your
* application uses glib mainloop, you can safely use this verbatim.
*/
#define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
#define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
typedef struct _PurpleGLibIOClosure {
PurpleInputFunction function;
guint result;
gpointer data;
} PurpleGLibIOClosure;
static void purple_glib_io_destroy(gpointer data)
{
g_free(data);
}
static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
{
PurpleGLibIOClosure *closure = data;
PurpleInputCondition purple_cond = 0;
if (condition & PURPLE_GLIB_READ_COND)
purple_cond |= PURPLE_INPUT_READ;
if (condition & PURPLE_GLIB_WRITE_COND)
purple_cond |= PURPLE_INPUT_WRITE;
closure->function(closure->data, g_io_channel_unix_get_fd(source),
purple_cond);
return TRUE;
}
static guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function,
gpointer data)
{
PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
GIOChannel *channel;
GIOCondition cond = 0;
closure->function = function;
closure->data = data;
if (condition & PURPLE_INPUT_READ)
cond |= PURPLE_GLIB_READ_COND;
if (condition & PURPLE_INPUT_WRITE)
cond |= PURPLE_GLIB_WRITE_COND;
#if defined _WIN32 && !defined WINPIDGIN_USE_GLIB_IO_CHANNEL
channel = wpurple_g_io_channel_win32_new_socket(fd);
#else
channel = g_io_channel_unix_new(fd);
#endif
closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
purple_glib_io_invoke, closure, purple_glib_io_destroy);
g_io_channel_unref(channel);
return closure->result;
}
static PurpleEventLoopUiOps glib_eventloops =
{
g_timeout_add,
g_source_remove,
glib_input_add,
g_source_remove,
NULL,
#if GLIB_CHECK_VERSION(2,14,0)
g_timeout_add_seconds,
#else
NULL,
#endif
/* padding */
NULL,
NULL,
NULL
};
void
sapphire_set_eventloop(void)
{
purple_eventloop_set_ui_ops(&glib_eventloops);
}
/*** End of the eventloop functions. ***/
/*
* Sapphire backend
*
* Copyright (C) 2018 Alyssa Rosenzweigj
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*
*/
#ifndef __EVENT_LOOP_H_
#define __EVENT_LOOP_H_
void sapphire_set_eventloop(void);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment