a20f0b130d88e2aba51f22be3c8864bf31722015
[speedometer] / util.c
1 /****
2         Speedometer, shows your current speed using GPS
3         Copyright (C) 2008 Wellu Mäkinen <wellu@wellu.org>
4
5         This program is free software: you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation, either version 3 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 ****/
18
19 #include <math.h>
20 #include <location/location-gps-device.h>
21 #include <location/location-gpsd-control.h>
22 #include <gconf/gconf-client.h>
23
24 #include "util.h"
25 #include "appdata.h"
26 #include "callbacks.h"
27 #include "ui.h"
28
29
30 gdouble unit_array[] = {3.6};
31
32 #define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
33
34 static LocationGPSDevice *device = NULL;
35 static LocationGPSDControl *control = NULL;
36
37 void start_gps(AppData* appdata) {
38 #ifdef __arm__
39         g_assert(appdata);
40         if(!device) {
41                 device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
42                 g_signal_connect(device, "changed", G_CALLBACK(location_changed), appdata);
43         }
44         control = location_gpsd_control_get_default();
45         location_gpsd_control_request_status(control);
46         if(control->can_control) {
47                 location_gpsd_control_start(control);
48         }
49 #endif // __arm__
50 }
51
52 void stop_gps(AppData* appdata) {
53 #ifdef __arm__
54         g_assert(appdata);
55         control = location_gpsd_control_get_default();
56         location_gpsd_control_request_status(control);
57         if(control->can_control) {
58                 location_gpsd_control_stop(control);
59         }
60 #endif // __arm__
61 }
62
63 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
64         g_assert(appdata);
65         g_assert(!isnan(speed));
66
67         // convert float to a 6 digits (including dot) wide string with leading zeros
68         gchar* charspeed = g_malloc(10); // alloc
69         g_sprintf(charspeed, "%0*.2f", 6, speed);
70
71         g_print("Speed is %s km/h\n", charspeed);
72
73         // set the main digits
74         guint i = 3;
75         while(i) {
76                 i--;
77                 set_nth_digit(appdata, i, g_ascii_digit_value(charspeed[i]));
78         }
79
80         repaint_all_digits(appdata);
81
82         g_free(charspeed);
83 }
84
85
86 static show_dialog() {
87         GtkWidget *dialog = gtk_message_dialog_new(
88                         NULL,
89                         GTK_DIALOG_MODAL,
90                         GTK_MESSAGE_INFO,
91                         GTK_BUTTONS_OK,
92                         "This program is licensed under GNU General Public License, "
93                         "which means (among other things) that you don't have to pay "
94                         "a dime for it. "
95                         "If you think, however, that this software is worth it, you "
96                         "can always drop me a postcard. (or a speeding ticket :)\n\n"
97                         "Wellu Mäkinen\n"
98                         "PO BOX\n"
99                         "33580 Tampere\n"
100                         "FINLAND");
101         gtk_dialog_run(GTK_DIALOG(dialog));
102         gtk_widget_destroy(dialog);
103 }
104
105 void show_cardware_dialog() {
106         GConfClient* client = gconf_client_get_default();
107         g_assert(GCONF_IS_CLIENT(client));
108
109         GConfValue* gcvalue = NULL;
110         gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
111
112         if(gcvalue == NULL) {
113                 g_print("GConf key not found so show dialog.");
114                 show_dialog();
115                 gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
116         }
117         g_object_unref(client);
118 }
119