From bbf830aca2d3e08a9b31b3de87d3635c9a40203e Mon Sep 17 00:00:00 2001 From: Ankit Date: Mon, 16 Mar 2015 01:15:59 +0530 Subject: [PATCH] dbus,location: Add & set 'Speed' prop Add a new property, 'Speed' on D-Bus location that reflects the speed in meters per second. This property is re-calculated and updated on each location update. This API will be helpful for apps that are interested in user's movements. https://bugs.freedesktop.org/show_bug.cgi?id=89395 --- src/gclue-location-source.c | 1 + src/gclue-location.c | 145 +++++++++++++++++++++++++++++++++++++++ src/gclue-location.h | 19 +++++ src/gclue-locator.c | 2 + src/gclue-service-location.c | 4 ++ src/org.freedesktop.GeoClue2.xml | 7 ++ 6 files changed, 178 insertions(+) diff --git a/src/gclue-location-source.c b/src/gclue-location-source.c index 079c45d..de191b2 100644 --- a/src/gclue-location-source.c +++ b/src/gclue-location-source.c @@ -277,6 +277,7 @@ gclue_location_source_set_location (GClueLocationSource *source, "longitude", geocode_location_get_longitude (gloc), "accuracy", geocode_location_get_accuracy (gloc), "description", geocode_location_get_description (gloc), + "speed", gclue_location_get_speed (location), NULL); g_object_notify (G_OBJECT (source), "location"); diff --git a/src/gclue-location.c b/src/gclue-location.c index c0a6aa0..cbf2d38 100644 --- a/src/gclue-location.c +++ b/src/gclue-location.c @@ -25,9 +25,75 @@ #include "gclue-location.h" +struct _GClueLocationPrivate { + gdouble speed; +}; + +enum { + PROP_0, + + PROP_SPEED, +}; + G_DEFINE_TYPE (GClueLocation, gclue_location, GEOCODE_TYPE_LOCATION); static void +gclue_location_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + GClueLocation *location = GCLUE_LOCATION (object); + + switch (property_id) { + case PROP_SPEED: + g_value_set_double (value, + gclue_location_get_speed (location)); + break; + + default: + /* We don't have any other property... */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +/** + * gclue_location_set_speed: + * @loc: a #GClueLocation + * @speed: speed in meters per second + * + * Sets the speed. + **/ +void +gclue_location_set_speed (GClueLocation *loc, + gdouble speed) +{ + loc->priv->speed = speed; +} + +static void +gclue_location_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + GClueLocation *location = GCLUE_LOCATION (object); + + switch (property_id) { + case PROP_SPEED: + gclue_location_set_speed (location, + g_value_get_double (value)); + break; + + default: + /* We don't have any other property... */ + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void gclue_location_finalize (GObject *glocation) { G_OBJECT_CLASS (gclue_location_parent_class)->finalize (glocation); @@ -37,14 +103,38 @@ static void gclue_location_class_init (GClueLocationClass *klass) { GObjectClass *glocation_class = G_OBJECT_CLASS (klass); + GParamSpec *pspec; glocation_class->finalize = gclue_location_finalize; + glocation_class->get_property = gclue_location_get_property; + glocation_class->set_property = gclue_location_set_property; + + g_type_class_add_private (klass, sizeof (GClueLocationPrivate)); + + /** + * GClueLocation:speed + * + * The speed in meters per second. + */ + pspec = g_param_spec_double ("speed", + "Speed", + "Speed in meters per second", + GCLUE_LOCATION_SPEED_UNKNOWN, + G_MAXDOUBLE, + GCLUE_LOCATION_SPEED_UNKNOWN, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS); + g_object_class_install_property (glocation_class, PROP_SPEED, pspec); } static void gclue_location_init (GClueLocation *location) { + location->priv = G_TYPE_INSTANCE_GET_PRIVATE ((location), + GCLUE_TYPE_LOCATION, + GClueLocationPrivate); + location->priv->speed = GCLUE_LOCATION_SPEED_UNKNOWN; } /** @@ -93,3 +183,58 @@ gclue_location_new_with_description (gdouble latitude, "description", description, NULL); } + +/** + * gclue_location_get_speed: + * @loc: a #GClueLocation + * + * Gets the speed in meters per second. + * + * Returns: The speed, or %GCLUE_LOCATION_SPEED_UNKNOWN if speed in unknown. + **/ +gdouble +gclue_location_get_speed (GClueLocation *loc) +{ + g_return_val_if_fail (GCLUE_IS_LOCATION (loc), + GCLUE_LOCATION_SPEED_UNKNOWN); + + return loc->priv->speed; +} + +/** + * gclue_location_set_speed_from_prev_location: + * @location: a #GClueLocation + * @prev_location: a #GClueLocation + * + * Calculates the speed based on provided previous location @prev_location + * and sets it on @location. + **/ +void +gclue_location_set_speed_from_prev_location (GClueLocation *location, + GClueLocation *prev_location) +{ + gdouble speed; + guint64 timestamp, prev_timestamp; + GeocodeLocation *gloc, *prev_gloc; + + g_return_if_fail (GCLUE_IS_LOCATION (location)); + g_return_if_fail (prev_location == NULL || + GCLUE_IS_LOCATION (prev_location)); + + if (prev_location == NULL) { + location->priv->speed = GCLUE_LOCATION_SPEED_UNKNOWN; + + return; + } + + gloc = GEOCODE_LOCATION (location); + prev_gloc = GEOCODE_LOCATION (prev_location); + + timestamp = geocode_location_get_timestamp (gloc); + prev_timestamp = geocode_location_get_timestamp (prev_gloc); + + speed = geocode_location_get_distance_from (gloc, prev_gloc) * + 1000.0 / (timestamp - prev_timestamp); + + location->priv->speed = speed; +} diff --git a/src/gclue-location.h b/src/gclue-location.h index 5ba9758..d8cc6bc 100644 --- a/src/gclue-location.h +++ b/src/gclue-location.h @@ -40,11 +40,14 @@ G_BEGIN_DECLS typedef struct _GClueLocation GClueLocation; typedef struct _GClueLocationClass GClueLocationClass; +typedef struct _GClueLocationPrivate GClueLocationPrivate; struct _GClueLocation { /* Parent instance structure */ GeocodeLocation parent_instance; + + GClueLocationPrivate *priv; }; struct _GClueLocationClass @@ -55,6 +58,13 @@ struct _GClueLocationClass GType gclue_location_get_type (void); +/** + * GCLUE_LOCATION_SPEED_UNKNOWN: + * + * Constant representing unknown speed. + */ +#define GCLUE_LOCATION_SPEED_UNKNOWN -1.0 + GClueLocation *gclue_location_new (gdouble latitude, gdouble longitude, gdouble accuracy); @@ -65,4 +75,13 @@ GClueLocation *gclue_location_new_with_description gdouble accuracy, const char *description); +void gclue_location_set_speed_from_prev_location + (GClueLocation *location, + GClueLocation *prev_location); + +gdouble gclue_location_get_speed (GClueLocation *loc); + +void gclue_location_set_speed (GClueLocation *loc, + gdouble speed); + #endif /* GCLUE_LOCATION_H */ \ No newline at end of file diff --git a/src/gclue-locator.c b/src/gclue-locator.c index c93ccf1..aee6c51 100644 --- a/src/gclue-locator.c +++ b/src/gclue-locator.c @@ -96,6 +96,8 @@ set_location (GClueLocator *locator, return; } + gclue_location_set_speed_from_prev_location (location, cur_location); + gclue_location_source_set_location (GCLUE_LOCATION_SOURCE (locator), location); } diff --git a/src/gclue-service-location.c b/src/gclue-service-location.c index ba29da9..5e74e38 100644 --- a/src/gclue-service-location.c +++ b/src/gclue-service-location.c @@ -97,6 +97,8 @@ gclue_service_location_get_property (GObject *object, gclue_dbus_location_get_longitude (location), gclue_dbus_location_get_accuracy (location), gclue_dbus_location_get_description (location)); + gclue_location_set_speed + (loc, gclue_dbus_location_get_speed (location)); altitude = gclue_dbus_location_get_altitude (location); if (altitude != GEOCODE_LOCATION_ALTITUDE_UNKNOWN) g_object_set (loc, "altitude", altitude, NULL); @@ -148,6 +150,8 @@ gclue_service_location_set_property (GObject *object, (location, geocode_location_get_accuracy (g_loc)); gclue_dbus_location_set_description (location, geocode_location_get_description (g_loc)); + gclue_dbus_location_set_speed + (location, gclue_location_get_speed (loc)); altitude = geocode_location_get_altitude (g_loc); if (altitude != GEOCODE_LOCATION_ALTITUDE_UNKNOWN) gclue_dbus_location_set_altitude (location, altitude); diff --git a/src/org.freedesktop.GeoClue2.xml b/src/org.freedesktop.GeoClue2.xml index ab14565..4d047d5 100644 --- a/src/org.freedesktop.GeoClue2.xml +++ b/src/org.freedesktop.GeoClue2.xml @@ -185,6 +185,13 @@ + + +