Initial commit
[gps-tracker] / gps-tracker.c
1 #include <glib.h>
2
3 #include <hildon/hildon.h>
4 #include <location/location-gpsd-control.h>
5 #include <location/location-gps-device.h>
6 #include <location/location-misc.h>
7 #include <location/location-distance-utils.h>
8
9 static void
10 on_gps_device_changed (LocationGPSDevice *device, gpointer data)
11 {
12   GtkLabel *info = (GtkLabel*)data;
13   GString *msg;
14         if (!device)
15                 return;
16
17   msg = g_string_new("");
18         if (device->fix) {
19                 if (device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET) {
20                         g_print ("time = %f\n", device->fix->time);
21       g_string_append_printf(msg, "time = %f\n", device->fix->time);
22     }
23
24                 if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET) {
25                         g_print ("lat = %f, long = %f\n",
26                                         device->fix->latitude,
27                                         device->fix->longitude);
28                         g_string_append_printf (msg, "lat = %f, long = %f\n",
29                                         device->fix->latitude,
30                                         device->fix->longitude);
31     }
32
33                 if (device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET) {
34                         g_print ("alt = %f\n", device->fix->altitude);
35                         g_string_append_printf (msg, "alt = %f\n", device->fix->altitude);
36     }
37
38                 if (device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
39                         g_print ("speed = %f\n", device->fix->speed);
40                         g_string_append_printf (msg, "speed = %f\n", device->fix->speed);
41     }
42
43                 if (device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET) {
44                         g_print ("track = %f\n", device->fix->track);
45                         g_string_append_printf (msg, "track = %f\n", device->fix->track);
46     }
47
48                 if (device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET) {
49                         g_print ("climb = %f\n", device->fix->climb);
50                         g_string_append_printf (msg, "climb = %f\n", device->fix->climb);
51     }
52
53                 g_print ("Accuracy values:\n");
54                 g_print ("\tept = %e, eph = %e, epv = %e, epd = %e, "
55                                 "eps = %e, epc = %e\n",
56                                 device->fix->ept,
57                                 device->fix->eph,
58                                 device->fix->epv,
59                                 device->fix->epd,
60                                 device->fix->eps,
61                                 device->fix->epc);
62         }
63         
64         g_print ("Satellites in view: %d\n", device->satellites_in_view);
65         g_print ("Satellites in use: %d\n", device->satellites_in_use);
66         g_print ("GPS status: %d\n", device->status);
67
68         if (device->cell_info) {
69                 if (device->cell_info->flags & LOCATION_CELL_INFO_GSM_CELL_INFO_SET)
70                         g_print ("Mobile Coutry Code GSM: %d\n", device->cell_info->gsm_cell_info.mcc);
71
72                 if (device->cell_info->flags & LOCATION_CELL_INFO_WCDMA_CELL_INFO_SET)
73                         g_print ("Mobile Coutry Code WCDMA: %d\n", device->cell_info->wcdma_cell_info.mcc);
74         }
75
76   gtk_label_set_text(info, msg->str);
77   //gtk_widget_show(GTK_WIDGET(info));
78   g_string_free(msg, TRUE);
79 }
80
81 static void
82 on_gps_error (LocationGPSDevice *device, gpointer data)
83 {
84         g_error ("GPS error");
85 }
86
87 static void
88 on_gps_stop (LocationGPSDevice *device, gpointer data)
89 {
90         g_warning ("GPS stopped");
91 }
92
93 static void
94 on_gps_start (LocationGPSDevice *device, gpointer data)
95 {
96         g_warning ("GPS started");
97 }
98
99 int main (int argc, char **argv)
100 {
101         HildonProgram *program = NULL;
102         GtkWidget *window = NULL;
103         GtkWidget *picker_button = NULL;
104   GtkVBox *vbox;
105   GtkWidget *status_label;
106
107         hildon_gtk_init (&argc, &argv);
108         LocationGPSDControl *control;
109         LocationGPSDevice *device;
110
111         program = hildon_program_get_instance ();
112         g_set_application_name("GPS tracker");
113
114         window = hildon_stackable_window_new ();
115         hildon_program_add_window (program, HILDON_WINDOW (window));
116
117   vbox = (void*)gtk_vbox_new(TRUE, 8);
118
119         /* Create a picker button */
120         picker_button = hildon_date_button_new (HILDON_SIZE_AUTO,
121                         HILDON_BUTTON_ARRANGEMENT_VERTICAL);
122
123         /* Set a title to the button*/
124         hildon_button_set_title (HILDON_BUTTON (picker_button), "Pick a date");
125
126   gtk_box_pack_start(GTK_BOX(vbox), picker_button, FALSE, FALSE, 0);
127   status_label = gtk_label_new("Hier kommt der Status hin\nUnd hier ist die 2. Zeile");
128   gtk_box_pack_start_defaults(GTK_BOX(vbox), status_label);
129         /* Add vbox to main window */
130         gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET(vbox));
131
132         g_signal_connect (G_OBJECT (window), "destroy",
133                         G_CALLBACK (gtk_main_quit), NULL);
134
135         control = location_gpsd_control_get_default ();
136         location_gpsd_control_start (control);
137
138         /*
139          * Note that in real life one may want to use some other method and interval
140          * than LOCATION_METHOD_USER_SELECTED and LOCATION_INTERVAL_DEFAULT,
141          * respectively. For more information on possible values for these parameters
142          * please see liblocation online documentation.
143          */
144         g_object_set (G_OBJECT (control), 
145                         "preferred-method", LOCATION_METHOD_USER_SELECTED,
146                         "preferred-interval", LOCATION_INTERVAL_DEFAULT,
147                         NULL);
148
149         device  = g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
150
151         g_signal_connect (control, "error",             G_CALLBACK (on_gps_error),              NULL);
152         g_signal_connect (control, "gpsd-running",      G_CALLBACK (on_gps_start),              NULL);
153         g_signal_connect (control, "gpsd-stopped",      G_CALLBACK (on_gps_stop),               NULL);
154         g_signal_connect (device,  "changed",           G_CALLBACK (on_gps_device_changed),     status_label);
155
156         gtk_widget_show_all (GTK_WIDGET (window));
157
158         gtk_main ();
159
160         location_gpsd_control_stop (control);
161
162         g_object_unref (device);
163         g_object_unref (control);
164
165         return 0;
166 }