From d858f899525dc40fe684a8ed15f5103ed8d733b7 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Mon, 2 Jan 2012 00:16:08 +0100 Subject: move to system d-bus --- debian/fmrxd.aegis | 15 ++++++++++++++- debian/fmrxd.install | 5 ++--- fmrx-cat.c | 11 ++++++++--- fmrx-ctl.c | 29 ++++++++++++++++++++++------- fmrxd.h | 2 +- fmrxd.service | 1 + radio.c | 4 ++-- server.c | 2 +- tuner.c | 20 +++++++++++++++----- 9 files changed, 66 insertions(+), 23 deletions(-) diff --git a/debian/fmrxd.aegis b/debian/fmrxd.aegis index 18d2100..93e4093 100644 --- a/debian/fmrxd.aegis +++ b/debian/fmrxd.aegis @@ -1,15 +1,28 @@ + + + + + + + + + + + + + + - diff --git a/debian/fmrxd.install b/debian/fmrxd.install index 58728e8..cc96e62 100644 --- a/debian/fmrxd.install +++ b/debian/fmrxd.install @@ -1,3 +1,2 @@ -fmrxd usr/sbin -fmrx-ctl usr/bin -fmrx-cat usr/bin +usr/sbin +usr/share/dbus-1/system-services diff --git a/fmrx-cat.c b/fmrx-cat.c index 580203e..c22d039 100644 --- a/fmrx-cat.c +++ b/fmrx-cat.c @@ -59,10 +59,15 @@ static void connect() dbus_error_init(&err); reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err); - g_assert(reply != NULL); - g_assert(!dbus_error_is_set(&err)); dbus_message_unref(msg); + if (!reply) { + g_assert(dbus_error_is_set(&err)); + g_printerr("Server error: %s", err.message); + dbus_error_free(&err); + return; + } + ret = dbus_message_get_args(reply, &err, DBUS_TYPE_UNIX_FD, &fd, DBUS_TYPE_INVALID); g_assert(ret == TRUE); g_assert(!dbus_error_is_set(&err)); @@ -74,7 +79,7 @@ static void connect() static void dbus_init() { DBusError err; dbus_error_init(&err); - bus = dbus_bus_get(DBUS_BUS_SESSION, &err); + bus = dbus_bus_get(DBUS_BUS_SYSTEM, &err); g_assert(bus != NULL); g_assert(!dbus_error_is_set(&err)); dbus_connection_setup_with_g_main(bus, g_main_loop_get_context(main_loop)); diff --git a/fmrx-ctl.c b/fmrx-ctl.c index 408dadd..5738c99 100644 --- a/fmrx-ctl.c +++ b/fmrx-ctl.c @@ -35,10 +35,15 @@ static void tune(double f) dbus_message_append_args(msg, DBUS_TYPE_DOUBLE, &f, DBUS_TYPE_INVALID); reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err); - g_assert(reply != NULL); - g_assert(!dbus_error_is_set(&err)); dbus_message_unref(msg); + if (!reply) { + g_assert(dbus_error_is_set(&err)); + g_printerr("Tune error: %s", err.message); + dbus_error_free(&err); + return; + } + dbus_message_unref(reply); } @@ -53,10 +58,15 @@ static void next() dbus_error_init(&err); reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err); - g_assert(reply != NULL); - g_assert(!dbus_error_is_set(&err)); dbus_message_unref(msg); + if (!reply) { + g_assert(dbus_error_is_set(&err)); + g_printerr("Tune error: %s", err.message); + dbus_error_free(&err); + return; + } + dbus_message_unref(reply); } @@ -71,17 +81,22 @@ static void prev() dbus_error_init(&err); reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &err); - g_assert(reply != NULL); - g_assert(!dbus_error_is_set(&err)); dbus_message_unref(msg); + if (!reply) { + g_assert(dbus_error_is_set(&err)); + g_printerr("Tune error: %s", err.message); + dbus_error_free(&err); + return; + } + dbus_message_unref(reply); } static void dbus_init() { DBusError err; dbus_error_init(&err); - bus = dbus_bus_get(DBUS_BUS_SESSION, &err); + bus = dbus_bus_get(DBUS_BUS_SYSTEM, &err); g_assert(bus != NULL); g_assert(!dbus_error_is_set(&err)); dbus_connection_setup_with_g_main(bus, g_main_loop_get_context(main_loop)); diff --git a/fmrxd.h b/fmrxd.h index e0e3b22..5f95d6a 100644 --- a/fmrxd.h +++ b/fmrxd.h @@ -11,7 +11,7 @@ #define BUS_INTERFACE BUS_NAME #define SERVER_ON_DEMAND 1 -#define SERVER_LINGER_TIME 6 +#define SERVER_LINGER_TIME 10 #define RADIO_LINGER_TIME 2 diff --git a/fmrxd.service b/fmrxd.service index 45c9afb..ba7a21a 100644 --- a/fmrxd.service +++ b/fmrxd.service @@ -1,3 +1,4 @@ [D-BUS Service] Name=com.javispedro.fmrxd Exec=/usr/sbin/fmrxd +User=root diff --git a/radio.c b/radio.c index bc0d5ea..7b53fba 100644 --- a/radio.c +++ b/radio.c @@ -79,7 +79,7 @@ void radio_queue_start() /* If there was a queued stop pending, remove it. */ g_source_remove(stop_source_id); } - if (!start_source_id) { + if (!radio_active() && !start_source_id) { start_source_id = g_idle_add(radio_start_func, NULL); } } @@ -90,7 +90,7 @@ void radio_queue_stop() /* If there is a queued start, remove it. */ g_source_remove(start_source_id); } - if (!stop_source_id) { + if (radio_active() && !stop_source_id) { stop_source_id = g_timeout_add_seconds(RADIO_LINGER_TIME, radio_stop_func, NULL); } diff --git a/server.c b/server.c index 442b63b..cfee65a 100644 --- a/server.c +++ b/server.c @@ -309,7 +309,7 @@ int server_start() rbuffer.write_p = 0; - bus = dbus_bus_get(DBUS_BUS_SESSION, &err); + bus = dbus_bus_get(DBUS_BUS_SYSTEM, &err); if (dbus_error_is_set(&err)) { g_critical("Cannot connect to the system message bus: %s", err.message); return -1; diff --git a/tuner.c b/tuner.c index eb0cda8..3d8e1ff 100644 --- a/tuner.c +++ b/tuner.c @@ -35,6 +35,11 @@ static unsigned long tuner_min, tuner_max; static unsigned long tuner_freq = 0; +static inline bool tuner_is_open() +{ + return tuner_fd != -1; +} + static inline unsigned long v4l_to_hz(unsigned f) { if (tuner_precise) { @@ -73,7 +78,7 @@ static bool tuner_tune(unsigned long hz) .frequency = hz_to_v4l(hz) }; - g_return_val_if_fail(tuner_fd != -1, false); + g_return_val_if_fail(tuner_is_open(), false); if (ioctl(tuner_fd, VIDIOC_S_FREQUENCY, &t_freq) < 0) { g_warning("Failed to tune: %s", strerror(errno)); @@ -89,7 +94,7 @@ static unsigned long tuner_get_tuned_freq() .tuner = TUNER_DEVICE_ID }; - g_return_val_if_fail(tuner_fd != -1, 0); + g_return_val_if_fail(tuner_is_open(), 0); if (ioctl(tuner_fd, VIDIOC_G_FREQUENCY, &t_freq) < 0) { g_warning("Failed to get freq: %s", strerror(errno)); @@ -105,7 +110,7 @@ static uint16_t tuner_get_signal() .index = TUNER_DEVICE_ID }; - g_return_val_if_fail(tuner_fd != -1, 0); + g_return_val_if_fail(tuner_is_open(), 0); if (ioctl(tuner_fd, VIDIOC_G_TUNER, &t_tuner) < 0) { g_warning("Failed to get signal level: %s", strerror(errno)); @@ -168,7 +173,12 @@ bool tuner_set_frequency(double mhz) { unsigned long hz = mhz_to_hz(mhz); - if (tuner_tune(hz)) { + if (!tuner_is_open()) { + // This is not an error; we just store the frequency + // and will set it later, when the tuner is opened. + tuner_freq = hz; + return true; + } else if (tuner_tune(hz)) { g_message("Tuned to %.1f Mhz", mhz); server_notify_tuned(mhz); tuner_freq = hz; @@ -187,7 +197,7 @@ bool tuner_search(bool forward) .seek_upward = forward }; - g_return_val_if_fail(tuner_fd != -1, false); + g_return_val_if_fail(tuner_is_open(), false); if (ioctl(tuner_fd, VIDIOC_S_HW_FREQ_SEEK, &t_freq_seek) < 0) { g_warning("Failed to start seek: %s", strerror(errno)); -- cgit v1.2.3