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