/* diversity-geoclue-backend - Locate Me! * * Copyright 2007-2008 OpenMoko, Inc. * Authored by Chia-I Wu * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include "geoclue-position-marshal.h" #define GEOCLUE_DBUS_BUS DBUS_BUS_SYSTEM #define GEOCLUE_GPSD_DBUS_SERVICE "org.freedesktop.geoclue.position.gypsy" #define GEOCLUE_GPSD_DBUS_PATH "/org/freedesktop/geoclue/position/gypsy" #define GEOCLUE_POSITION_DBUS_INTERFACE "org.freedesktop.geoclue.position" #define GPS_2D_MASK (GEOCLUE_POSITION_ALTITUDE_AVAILABLE | \ GEOCLUE_POSITION_LONGITUDE_AVAILABLE) enum { GEOCLUE_POSITION_NO_SERVICE_AVAILABLE = 0, GEOCLUE_POSITION_ACQUIRING_ALTITUDE = 1, GEOCLUE_POSITION_ACQUIRING_LONGITUDE = 2, GEOCLUE_POSITION_ACQUIRING_LATITUDE = 4, GEOCLUE_POSITION_ALTITUDE_AVAILABLE = 8, GEOCLUE_POSITION_LONGITUDE_AVAILABLE = 16, GEOCLUE_POSITION_LATITUDE_AVAILABLE = 32 }; static struct { DiversityWorld *world; DBusGConnection *connection; DBusGProxy *proxy; gint status; } geoclue; static void on_service_status_changed(DBusGProxy *proxy, gint status, const gchar *msg) { DiversityBard *bard; DiversityObjectAccuracy acc; bard = diversity_world_get_self(geoclue.world); diversity_object_touch(DIVERSITY_OBJECT(bard)); status &= GPS_2D_MASK; if (geoclue.status == status) return; geoclue.status = status; if ((status & GPS_2D_MASK) == GPS_2D_MASK) acc = DIVERSITY_OBJECT_ACCURACY_DIRECT; else acc = DIVERSITY_OBJECT_ACCURACY_NONE; diversity_object_set_accuracy(DIVERSITY_OBJECT(bard), acc); } static void on_current_position_changed(DBusGProxy *proxy, gint timestamp, gdouble lat, gdouble lon, gdouble alt, void *data) { DiversityBard *bard; bard = diversity_world_get_self(geoclue.world); diversity_object_touch(DIVERSITY_OBJECT(bard)); diversity_object_geometry_set(DIVERSITY_OBJECT(bard), lon, lat, 0.0, 0.0); } int geoclue_init(DiversityWorld *world, GError **error) { if (!g_thread_supported()) { g_thread_init(NULL); dbus_g_thread_init(); } geoclue.world = world; g_object_ref(world); geoclue.connection = dbus_g_bus_get(GEOCLUE_DBUS_BUS, error); if (!geoclue.connection) { geoclue_fini(); return FALSE; } geoclue.proxy = dbus_g_proxy_new_for_name(geoclue.connection, GEOCLUE_GPSD_DBUS_SERVICE, GEOCLUE_GPSD_DBUS_PATH, GEOCLUE_POSITION_DBUS_INTERFACE); if (!geoclue.proxy) { g_set_error(error, 0, 0, "failed to create proxy\n"); geoclue_fini(); return FALSE; } if (dbus_g_proxy_call(geoclue.proxy, "service_status", NULL, G_TYPE_INVALID, G_TYPE_INT, &geoclue.status, G_TYPE_STRING, NULL, G_TYPE_INVALID)) on_service_status_changed(geoclue.proxy, geoclue.status, NULL); else geoclue.status = GEOCLUE_POSITION_NO_SERVICE_AVAILABLE; dbus_g_object_register_marshaller( geoclue_position_marshal_VOID__INT_STRING, G_TYPE_NONE, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID); dbus_g_object_register_marshaller( geoclue_position_marshal_VOID__INT_DOUBLE_DOUBLE_DOUBLE, G_TYPE_NONE, G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_INVALID); dbus_g_proxy_add_signal(geoclue.proxy, "service_status_changed", G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID); dbus_g_proxy_add_signal(geoclue.proxy, "current_position_changed", G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_INVALID); dbus_g_proxy_connect_signal(geoclue.proxy, "service_status_changed", G_CALLBACK(on_service_status_changed), NULL, NULL); dbus_g_proxy_connect_signal(geoclue.proxy, "current_position_changed", G_CALLBACK(on_current_position_changed), NULL, NULL); return TRUE; } void geoclue_fini(void) { if (geoclue.world) { g_object_unref(G_OBJECT(geoclue.world)); geoclue.world = NULL; } if (geoclue.connection) { dbus_g_connection_unref(geoclue.connection); geoclue.connection = NULL; } if (geoclue.proxy) { g_object_unref(G_OBJECT(geoclue.proxy)); geoclue.proxy = NULL; } }