From: Alex Badea Date: Wed, 9 Jun 2010 19:41:34 +0000 (+0300) Subject: Import daemon source, and adjust build X-Git-Url: http://git.maemo.org/git/?p=espeaktime;a=commitdiff_plain;h=7ee8e603991fd38e9b2b186e1c999cb5aaedc20c Import daemon source, and adjust build --- diff --git a/Makefile.am b/Makefile.am index af437a6..85d5f75 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1 +1,2 @@ -SUBDIRS = src +SUBDIRS = src data + diff --git a/configure.ac b/configure.ac index fee9fbd..57fb79b 100644 --- a/configure.ac +++ b/configure.ac @@ -5,7 +5,24 @@ AC_PROG_CC AC_PROG_INSTALL AC_HEADER_STDC +PKG_CHECK_MODULES(GLIB, [glib-2.0]) +AC_SUBST(GLIB_CFLAGS) +AC_SUBST(GLIB_LIBS) + +PKG_CHECK_MODULES(DBUS, [dbus-glib-1]) +AC_SUBST(DBUS_CFLAGS) +AC_SUBST(DBUS_LIBS) + +PKG_CHECK_MODULES(MCE, [mce]) +AC_SUBST(MCE_CFLAGS) +AC_SUBST(MCE_LIBS) + +PKG_CHECK_MODULES(HAL, [hal]) +AC_SUBST(HAL_CFLAGS) +AC_SUBST(HAL_LIBS) + AC_OUTPUT([ Makefile src/Makefile + data/Makefile ]) diff --git a/data/Makefile.am b/data/Makefile.am new file mode 100644 index 0000000..6f46a9f --- /dev/null +++ b/data/Makefile.am @@ -0,0 +1,3 @@ +install-data-local: + $(INSTALL_DATA) -D $(srcdir)/espeaktime $(DESTDIR)/etc/event.d/espeaktime + diff --git a/data/espeaktime b/data/espeaktime new file mode 100644 index 0000000..823dcdc --- /dev/null +++ b/data/espeaktime @@ -0,0 +1,13 @@ +description "espeaktime daemon" +author "Alex Badea" + +service +console none + +start on started hildon-desktop +stop on stopping shutdown + +# Note, run-standalone.sh does not exec its argument; +# we work around that by explicitly saying "exec", +# to make job stopping work. +exec /usr/bin/run-standalone.sh exec /usr/bin/espeaktimed > /dev/null 2>&1 diff --git a/debian/changelog b/debian/changelog index c1c0667..5ca2828 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,15 @@ +espeaktime (0.2-2) unstable; urgency=low + + * Fix Build-depends + + -- Alex Badea Wed, 09 Jun 2010 22:32:05 +0300 + +espeaktime (0.2-1) unstable; urgency=low + + * Add C language daemon implementation + + -- Alex Badea Tue, 08 Jun 2010 20:38:07 +0300 + espeaktime (0.2) unstable; urgency=low * Add autoconf build system, and a no-op espeaktime-daemon diff --git a/debian/control b/debian/control index 52a78fc..786f1c9 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: espeaktime Section: user/multimedia Priority: extra Maintainer: Alex Badea -Build-Depends: debhelper (>= 5) +Build-Depends: debhelper (>= 5), libglib2.0-dev, libdbus-glib-1-dev, libhal-dev, mce-dev (>= 1.8.10) Standards-Version: 3.7.2 Package: espeaktime-python @@ -18,7 +18,7 @@ XSBC-Bugtracker: mailto:vamposdecampos@gmail.com Package: espeaktime-daemon Architecture: any -Depends: ${shlibs:Depends}, ${misc:Depends} +Depends: ${shlibs:Depends}, ${misc:Depends}, espeak Description: Talking clock based on eSpeak This is a simple talking clock daemon. Activate it by double-pressing the power button while the display is locked. diff --git a/debian/espeaktime-daemon.install b/debian/espeaktime-daemon.install index 0210e01..74d5599 100644 --- a/debian/espeaktime-daemon.install +++ b/debian/espeaktime-daemon.install @@ -1 +1,3 @@ -usr/bin/espeaktimed usr/bin/ +usr/bin/espeaktimed +usr/bin/espeaktime-now.sh +etc/event.d/* diff --git a/debian/espeaktime-daemon.postinst b/debian/espeaktime-daemon.postinst new file mode 100644 index 0000000..aeded95 --- /dev/null +++ b/debian/espeaktime-daemon.postinst @@ -0,0 +1,8 @@ +#! /bin/sh + +#DEBHELPER# + +start espeaktime + +exit 0 + diff --git a/debian/espeaktime-daemon.prerm b/debian/espeaktime-daemon.prerm new file mode 100644 index 0000000..e6f90d6 --- /dev/null +++ b/debian/espeaktime-daemon.prerm @@ -0,0 +1,8 @@ +#! /bin/sh + +stop espeaktime + +#DEBHELPER# + +exit 0 + diff --git a/debian/rules b/debian/rules index 9d594de..d400703 100755 --- a/debian/rules +++ b/debian/rules @@ -45,6 +45,7 @@ install: build $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install install -D python/espeaktime.py $(CURDIR)/debian/tmp/usr/bin/espeaktime.py + install -D src/espeaktime-now.sh $(CURDIR)/debian/tmp/usr/bin/espeaktime-now.sh # Build architecture-independent files here. binary-indep: build install diff --git a/src/Makefile.am b/src/Makefile.am index f7bdf89..d46aee8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,4 @@ bin_PROGRAMS = espeaktimed espeaktimed_SOURCES = daemon.c - +espeaktimed_CFLAGS = $(GLIB_CFLAGS) $(DBUS_CFLAGS) $(MCE_CFLAGS) $(HAL_CFLAGS) +espeaktimed_LDADD = $(GLIB_LIBS) $(DBUS_LIBS) $(MCE_LIBS) $(HAL_LIBS) diff --git a/src/daemon.c b/src/daemon.c index 50bc2c9..87d816f 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,4 +1,182 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +#define HAL_COND_BUTTONPRESSED "ButtonPressed" +#define HAL_BUTTON_POWER "power" +#define HAL_INPUTDEV_PATH "/org/freedesktop/Hal/devices/computer_logicaldev_input" + +struct app_data { + DBusGConnection *bus; + LibHalContext *hal; + + gboolean mode_locked; + gboolean display_on; +}; + +static void sig_tklock_mode(DBusGProxy *proxy, const char *mode, gpointer user_data) +{ + struct app_data *app = user_data; + g_debug("sig_tklock_mode [%s]", mode); + app->mode_locked = !strcmp(mode, MCE_TK_LOCKED); +} + +static void sig_display_status(DBusGProxy *proxy, const char *status, gpointer user_data) +{ + struct app_data *app = user_data; + g_debug("sig_display_status [%s]", status); + app->display_on = !strcmp(status, MCE_DISPLAY_ON_STRING); +} + +static void debug_log(const gchar *log_domain, + GLogLevelFlags log_level, const gchar *message, gpointer unused_data) +{ + g_print("# %s\n", message); +} + +static void power_button(struct app_data *app) +{ + g_debug("power button"); + if (app->mode_locked && app->display_on) { + int res = system("espeaktime-now.sh"); + g_debug("speak script: %d", res); + } +} + +/** + * Call a method with no input arguments and one string output argument. + * Return the allocaed result string on success, NULL on failure. + */ +static char *mce_call_getstr(DBusGProxy *proxy, const char *method) +{ + GError *err = NULL; + char *s = NULL; + + if (dbus_g_proxy_call(proxy, method, &err, + G_TYPE_INVALID, + G_TYPE_STRING, &s, G_TYPE_INVALID)) + return s; + + g_error("Couldn't call MCE (%s): %s\n", method, err->message); + g_error_free(err); + return NULL; +} + +static gboolean prefill_status(struct app_data *app) +{ + DBusGProxy *mce_req; + char *mode, *status; + + mce_req = dbus_g_proxy_new_for_name(app->bus, MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF); + g_assert(mce_req); + + mode = mce_call_getstr(mce_req, MCE_TKLOCK_MODE_GET); + status = mce_call_getstr(mce_req, MCE_DISPLAY_STATUS_GET); + if (mode) + sig_tklock_mode(NULL, mode, app); + if (status) + sig_display_status(NULL, status, app); + g_free(mode); + g_free(status); + g_object_unref(mce_req); + + return mode && status; +} + +static void connect_signals(struct app_data *app) +{ + DBusGProxy *mce_sig; + + mce_sig = dbus_g_proxy_new_for_name(app->bus, MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF); + g_assert(mce_sig); + dbus_g_proxy_add_signal(mce_sig, MCE_TKLOCK_MODE_SIG, G_TYPE_STRING, G_TYPE_INVALID); + dbus_g_proxy_add_signal(mce_sig, MCE_DISPLAY_SIG, G_TYPE_STRING, G_TYPE_INVALID); + dbus_g_proxy_connect_signal(mce_sig, MCE_TKLOCK_MODE_SIG, G_CALLBACK(sig_tklock_mode), app, NULL); + dbus_g_proxy_connect_signal(mce_sig, MCE_DISPLAY_SIG, G_CALLBACK(sig_display_status), app, NULL); +} + + +static void device_condition(LibHalContext *ctx, + const char *udi, + const char *condition_name, + const char *condition_detail) +{ + g_debug("device_condition: name [%s] detail [%s]", + condition_name, condition_detail); + if (!strcmp(condition_name, HAL_COND_BUTTONPRESSED) && !strcmp(condition_detail, HAL_BUTTON_POWER)) { + struct app_data *app = libhal_ctx_get_user_data(ctx); + power_button(app); + } +} + + +static gboolean init_hal(struct app_data *app) +{ + DBusError err; + + app->hal = libhal_ctx_new(); + if (!app->hal) { + g_error("Couldn't get a HAL context"); + return FALSE; + } + libhal_ctx_set_dbus_connection(app->hal, + dbus_g_connection_get_connection(app->bus)); + libhal_ctx_set_device_condition(app->hal, device_condition); + libhal_ctx_set_user_data(app->hal, app); + + dbus_error_init(&err); + if (!libhal_ctx_init(app->hal, &err)) { + g_error("Couldn't initialize HAL context: %s", err.message); + dbus_error_free(&err); + libhal_ctx_free(app->hal); + return FALSE; + } + + if (!libhal_device_add_property_watch(app->hal, HAL_INPUTDEV_PATH, &err)) { + g_error("Couldn't add HAL watch: %s", err.message); + dbus_error_free(&err); + libhal_ctx_free(app->hal); + } + return TRUE; +} + int main(int argc, char *argv[]) { + GMainLoop *loop; + GError *err = NULL; + struct app_data app; + + if (argc > 1 && !strcmp(argv[1], "-d")) + g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, debug_log, NULL); + + g_debug("init"); + memset(&app, 0, sizeof(app)); + + g_type_init(); + loop = g_main_loop_new(NULL, FALSE); + + app.bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &err); + if (!app.bus) { + g_error("Couldn't get DBUS connection: %s\n", err->message); + g_error_free(err); + return 1; + } + + if (!init_hal(&app)) + return 1; + + if (!prefill_status(&app)) + return 1; + connect_signals(&app); + + g_debug("running"); + g_main_loop_run(loop); + dbus_g_connection_unref(app.bus); return 0; } diff --git a/src/espeaktime-now.sh b/src/espeaktime-now.sh new file mode 100755 index 0000000..abea520 --- /dev/null +++ b/src/espeaktime-now.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +exec espeak $(date "+%H:%M")