From 10ad280d346ab341a0ab4437d1f3f81d48a5a0f0 Mon Sep 17 00:00:00 2001 From: Jose Dapena Paz Date: Sat, 26 Sep 2009 17:57:44 +0200 Subject: [PATCH] Add support for IP Heartbeat API in maemo utils. --- configure.ac | 10 +++ src/hildon2/Makefile.am | 1 + src/hildon2/modest-maemo-utils.c | 124 ++++++++++++++++++++++++++++++++++++++ src/hildon2/modest-maemo-utils.h | 15 +++++ 4 files changed, 150 insertions(+) diff --git a/configure.ac b/configure.ac index 538904f..4fa64d1 100644 --- a/configure.ac +++ b/configure.ac @@ -535,6 +535,16 @@ else fi AC_SUBST(MODEST_LIBTIME_LIBS) +AC_CHECK_HEADERS([iphbd/libiphb.h], have_libiphb=true, have_libiphb=false) + +if test "x$have_libiphb" == "xtrue"; then + AC_DEFINE_UNQUOTED(MODEST_USE_IPHB, 1, ["use IP Heartbeat API"]) + MODEST_LIBIPHB_LIBS=-liphb +else + MODEST_LIBIPHB_LIBS= +fi +AC_SUBST(MODEST_LIBIPHB_LIBS) + # # if we don't have an addressbook, use the dummy one # diff --git a/src/hildon2/Makefile.am b/src/hildon2/Makefile.am index 421448a..5224c13 100644 --- a/src/hildon2/Makefile.am +++ b/src/hildon2/Makefile.am @@ -118,6 +118,7 @@ libmodest_ui_la_LIBADD = \ $(MODEST_LIBCONIC_LIBS) \ $(MODEST_HILDON_HELP_LIBS) \ $(MODEST_LIBALARM_LIBS) \ + $(MODEST_LIBIPHB_LIBS) \ $(MODEST_HILDON_NOTIFY_LIBS) UI_FILES=\ diff --git a/src/hildon2/modest-maemo-utils.c b/src/hildon2/modest-maemo-utils.c index 140e2f3..b86fa2d 100644 --- a/src/hildon2/modest-maemo-utils.c +++ b/src/hildon2/modest-maemo-utils.c @@ -53,6 +53,9 @@ #include "modest-platform.h" #include "modest-ui-constants.h" #include +#ifdef MODEST_USE_IPHB +#include +#endif /* Label child of a captioned */ #define CAPTIONED_LABEL_CHILD "captioned-label" @@ -622,3 +625,124 @@ modest_maemo_utils_scroll_pannable (HildonPannableArea *pannable, hildon_pannable_area_scroll_to (pannable, h_pos, v_pos); } + +#ifdef MODEST_USE_IPHB + +typedef struct _ModestHeartbeatSource { + GSource source; + iphb_t iphb; + GPollFD poll; + gint interval; +} ModestHeartbeatSource; + +static gboolean modest_heartbeat_prepare (GSource* source, gint *timeout) +{ + *timeout = -1; + return FALSE; +} + +static gboolean +modest_heartbeat_check(GSource* source) +{ + return ((ModestHeartbeatSource *) source)->poll.revents != 0; +} + +static gboolean modest_heartbeat_dispatch (GSource *source, GSourceFunc callback, gpointer userdata) +{ + if (callback(userdata)) + { + ModestHeartbeatSource *hb_source = (ModestHeartbeatSource *) source; + + g_source_remove_poll (source, &(hb_source->poll)); + + int min = MAX(hb_source->interval - 30, 5); + iphb_wait(hb_source->iphb, min, min + 60, 0); + + hb_source->poll.fd = iphb_get_fd(hb_source->iphb); + hb_source->poll.events = G_IO_IN; + hb_source->poll.revents = 0; + + g_source_add_poll(source, &(hb_source->poll)); + + return TRUE; + } else { + return FALSE; + } +} + +static void +modest_heartbeat_finalize (GSource* source) +{ + ModestHeartbeatSource* hb_source = (ModestHeartbeatSource *) source; + hb_source->iphb = iphb_close(hb_source->iphb); +} + +GSourceFuncs modest_heartbeat_funcs = +{ + modest_heartbeat_prepare, + modest_heartbeat_check, + modest_heartbeat_dispatch, + modest_heartbeat_finalize, + NULL, + NULL +}; + +static GSource * +modest_heartbeat_source_new (void) +{ + GSource *source; + ModestHeartbeatSource *hb_source; + iphb_t iphb; + int hb_interval; + + source = NULL; + hb_interval = 0; + + iphb = iphb_open (&hb_interval); + + if (iphb != 0) { + int min; + source = g_source_new (&modest_heartbeat_funcs, sizeof (ModestHeartbeatSource)); + hb_source = (ModestHeartbeatSource *) source; + g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE); + hb_source->iphb = iphb; + hb_source->interval = hb_interval; + + min = MAX(hb_interval - 30, 5); + iphb_wait(hb_source->iphb, min, min + 60, 0); + + hb_source->poll.fd = iphb_get_fd(hb_source->iphb); + hb_source->poll.events = G_IO_IN; + hb_source->poll.revents = 0; + + g_source_add_poll(source, &(hb_source->poll)); + } else { + source = g_idle_source_new (); + } + + return source; +} + +guint +modest_heartbeat_add (GSourceFunc function, + gpointer userdata) +{ + GSource *source; + guint id; + + source = modest_heartbeat_source_new (); + g_source_set_callback (source, function, userdata, NULL); + id = g_source_attach (source, NULL); + g_source_unref (source); + + return id; +} + +#else +guint +modest_heartbeat_add (GSourceFunc function, + gpointer userdata) +{ + return g_idle_add (function, userdata); +} +#endif diff --git a/src/hildon2/modest-maemo-utils.h b/src/hildon2/modest-maemo-utils.h index 0066e54..71d909b 100644 --- a/src/hildon2/modest-maemo-utils.h +++ b/src/hildon2/modest-maemo-utils.h @@ -174,4 +174,19 @@ modest_maemo_utils_scroll_pannable(HildonPannableArea *pannable, gint horizontal, gint vertical); +/** + * modest_heartbeat_add: + * @function: function to call + * @userdata: data to pass to @function. + * + * Adds a function to be called when heartbeat is called. If the + * function returns FALSE it is automatically removed from the + * list of event sources and will not be called again. + * + * Returns: the ID (greater than 0) of the event source + */ +guint +modest_heartbeat_add (GSourceFunc function, + gpointer userdata); + #endif /*__MODEST_MAEMO_UTILS_H__*/ -- 1.7.9.5