Commit dc9d1b80 authored by Jussi Kukkonen's avatar Jussi Kukkonen

add async method to Position C API

parent a81e0b7e
noinst_PROGRAMS = \ noinst_PROGRAMS = \
position-example \ position-example \
master-example \ position-async-example \
master-example \
address-example \ address-example \
geocode-example \ geocode-example \
revgeocode-example \ revgeocode-example \
...@@ -8,6 +9,7 @@ noinst_PROGRAMS = \ ...@@ -8,6 +9,7 @@ noinst_PROGRAMS = \
common_example_SOURCES = common-example.c common_example_SOURCES = common-example.c
position_example_SOURCES = position-example.c position_example_SOURCES = position-example.c
position_async_example_SOURCES = position-async-example.c
address_example_SOURCES = address-example.c address_example_SOURCES = address-example.c
geocode_example_SOURCES = geocode-example.c geocode_example_SOURCES = geocode-example.c
revgeocode_example_SOURCES = revgeocode-example.c revgeocode_example_SOURCES = revgeocode-example.c
......
/*
* Geoclue
* position-example.c - Example using the Position client API
* asynchronous method call
*
* Author: Jussi Kukkonen <jku@openedhand.com>
* Copyright 2008 by Garmin Ltd. or its subsidiaries
*/
#include <glib.h>
#include <geoclue/geoclue-common.h>
#include <geoclue/geoclue-position.h>
static void
position_callback (GeocluePosition *pos,
GeocluePositionFields fields,
int timestamp,
double latitude,
double longitude,
double altitude,
GeoclueAccuracy *accuracy,
GError *error,
gpointer userdata)
{
if (error) {
g_printerr ("Error getting position: %s\n", error->message);
g_error_free (error);
g_object_unref (pos);
} else {
if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE &&
fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
GeoclueAccuracyLevel level;
double horiz_acc;
geoclue_accuracy_get_details (accuracy, &level, &horiz_acc, NULL);
g_print ("Current position:\n");
g_print ("\t%f, %f\n", latitude, longitude);
g_print ("\tAccuracy level %d (%.0f meters)\n", level, horiz_acc);
} else {
g_print ("Latitude and longitude not available.\n");
}
}
g_main_loop_quit ((GMainLoop *)userdata);
}
int main (int argc, char** argv)
{
gchar *service, *path;
GMainLoop *mainloop;
GeocluePosition *pos = NULL;
if (argc < 2 || argc % 2 != 0) {
g_printerr ("Usage:\n position-example <provider_name>");
return 1;
}
g_type_init();
mainloop = g_main_loop_new (NULL, FALSE);
g_print ("Using provider '%s'\n", argv[1]);
service = g_strdup_printf ("org.freedesktop.Geoclue.Providers.%s", argv[1]);
path = g_strdup_printf ("/org/freedesktop/Geoclue/Providers/%s", argv[1]);
pos = geoclue_position_new (service, path);
g_free (service);
g_free (path);
if (pos == NULL) {
g_printerr ("Error while creating GeocluePosition object.\n");
return 1;
}
geoclue_position_get_position_async (pos,
(GeocluePositionCallback) position_callback,
mainloop);
g_print ("Asynchronous call made, going to main loop now...\n");
g_main_loop_run (mainloop);
g_main_loop_unref (mainloop);
g_object_unref (pos);
return 0;
}
...@@ -200,3 +200,53 @@ geoclue_position_get_position (GeocluePosition *position, ...@@ -200,3 +200,53 @@ geoclue_position_get_position (GeocluePosition *position,
return fields; return fields;
} }
typedef struct _GeocluePositionAsyncData {
GeocluePosition *position;
GCallback callback;
gpointer userdata;
} GeocluePositionAsyncData;
static void
get_position_async_callback (DBusGProxy *proxy,
GeocluePositionFields fields,
int timestamp,
double latitude,
double longitude,
double altitude,
GeoclueAccuracy *accuracy,
GError *error,
GeocluePositionAsyncData *data)
{
(*(GeocluePositionCallback)data->callback) (data->position,
fields,
timestamp,
latitude,
longitude,
altitude,
accuracy,
error,
data->userdata);
g_free (data);
}
void
geoclue_position_get_position_async (GeocluePosition *position,
GeocluePositionCallback callback,
gpointer userdata)
{
GeoclueProvider *provider = GEOCLUE_PROVIDER (position);
GeocluePositionAsyncData *data;
data = g_new (GeocluePositionAsyncData, 1);
data->position = position;
data->callback = G_CALLBACK (callback);
data->userdata = userdata;
org_freedesktop_Geoclue_Position_get_position_async
(provider->proxy,
(org_freedesktop_Geoclue_Position_get_position_reply)get_position_async_callback,
data);
/*do we need "call" ?*/
}
...@@ -50,6 +50,19 @@ GeocluePositionFields geoclue_position_get_position (GeocluePosition *position, ...@@ -50,6 +50,19 @@ GeocluePositionFields geoclue_position_get_position (GeocluePosition *position,
GeoclueAccuracy **accuracy, GeoclueAccuracy **accuracy,
GError **error); GError **error);
typedef void (*GeocluePositionCallback) (GeocluePosition *position,
GeocluePositionFields fields,
int timestamp,
double latitude,
double longitude,
double altitude,
GeoclueAccuracy *accuracy,
GError *error,
gpointer userdata);
void geoclue_position_get_position_async (GeocluePosition *position,
GeocluePositionCallback callback,
gpointer userdata);
G_END_DECLS G_END_DECLS
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment