File: master-pos-example.c

package info (click to toggle)
geoclue 0.12.0-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,132 kB
  • sloc: ansic: 12,414; sh: 10,127; xml: 2,036; makefile: 687
file content (140 lines) | stat: -rw-r--r-- 4,515 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Geoclue
 * master-example.c - Example using the Master client API
 *
 * Authors: Iain Holmes <iain@openedhand.com>
 *          Jussi Kukkonen <jku@o-hand.com>
 * Copyright 2008 by Garmin Ltd. or its subsidiaries
 *           2008 OpenedHand Ltd
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */


/* This example shows typical GPS-like usage. Following params are 
 * given to geoclue_master_client_set_requirements():
 * 	min_accuracy = GEOCLUE_ACCURACY_LEVEL_DETAILED
 * 		We require the highest level of accuracy
 * 	min_time = 0
 * 		No limit on frequency of position-changed signals
 * 		(this is not actually implemented yet)
 * 	require_updates = TRUE
 * 		We need position-changed signals
 * 	allowed_resources = GEOCLUE_RESOURCE_ALL
 * 		Any available resource can be used
 * 
 * Geoclue master will try to select a suitable provider based on these
 * requirements -- currently only Gypsy and Gpsd providers fulfill 
 * the above requiremens. Gpsd-provider should work out-of-the-box as 
 * long as gpsd is running in the default port. Gypsy provider requires 
 * that you set device name in the options: see README for details.
 * 
 */

#include <string.h>

#include <geoclue/geoclue-master.h>
#include <geoclue/geoclue-position.h>

static void
provider_changed_cb (GeoclueMasterClient *client,
                     char *iface,
                     char *name,
                     char *description, 
                     gpointer userdata)
{
	if (strlen (name) == 0) {
		g_print ("No provider available\n");
	} else {
		g_print ("now using provider: %s\n", name);
	}
}

static void
position_changed_cb (GeocluePosition      *position,
		     GeocluePositionFields fields,
		     int                   timestamp,
		     double                latitude,
		     double                longitude,
		     double                altitude,
		     GeoclueAccuracy      *accuracy,
		     gpointer              userdata)
{
	if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE &&
	    fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
		GeoclueAccuracyLevel level;
		
		geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
		g_print ("got position (accuracy level %d):\n", level);
		g_print ("\t%f, %f\n", latitude, longitude);
		
	} else {
		g_print ("position emitted, but latitude and longitude are not valid.\n");
	}
}

int
main (int    argc,
      char **argv)
{
	GError *error = NULL;
	GMainLoop *mainloop;
	GeoclueMaster *master;
	GeoclueMasterClient *client;
	GeocluePosition *position;
	
	g_type_init ();
	
	master = geoclue_master_get_default ();
	client = geoclue_master_create_client (master, NULL, NULL);
	g_object_unref (master);
	
	g_signal_connect (G_OBJECT (client), "position-provider-changed",
	                  G_CALLBACK (provider_changed_cb), NULL);
	
	/* We want provider that has detailed accuracy and emits signals.
	 * The provider is allowed to use any resources available. */
	if (!geoclue_master_client_set_requirements (client, 
	                                             GEOCLUE_ACCURACY_LEVEL_DETAILED,
	                                             0, TRUE,
	                                             GEOCLUE_RESOURCE_ALL,
	                                             NULL)){
		g_printerr ("Setting requirements failed");
		g_object_unref (client);
		return 1;
	}
	
	position = geoclue_master_client_create_position (client, &error);
	if (!position) {
		g_warning ("Creating GeocluePosition failed: %s", error->message);
		g_error_free (error);
		g_object_unref (client);
		return 1;
	}
	
	g_signal_connect (G_OBJECT (position), "position-changed",
			  G_CALLBACK (position_changed_cb), NULL);
	
	mainloop = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (mainloop);
	
	g_main_loop_unref (mainloop);
	g_object_unref (client);
	g_object_unref (position);
	
	return 0;
}