0.4 release
[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 #include <math.h>
19 #include <location/location-gps-device.h>
20 #include <location/location-gpsd-control.h>
21 #include <gconf/gconf-client.h>
22
23 #include "util.h"
24 #include "appdata.h"
25 #include "callbacks.h"
26 #include "ui.h"
27
28 #define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
29
30 static LocationGPSDevice *device = NULL;
31 static LocationGPSDControl *control = NULL;
32
33 void start_gps(AppData* appdata) {
34 #ifdef __arm__
35         g_assert(appdata);
36         if(!device) {
37                 device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
38                 g_signal_connect(device, "changed", G_CALLBACK(location_changed), appdata);
39         }
40         control = location_gpsd_control_get_default();
41         location_gpsd_control_request_status(control);
42         if(control->can_control) {
43                 location_gpsd_control_start(control);
44         }
45 #endif // __arm__
46 }
47
48 void stop_gps(AppData* appdata) {
49 #ifdef __arm__
50         g_assert(appdata);
51         control = location_gpsd_control_get_default();
52         location_gpsd_control_request_status(control);
53         if(control->can_control) {
54                 location_gpsd_control_stop(control);
55         }
56 #endif // __arm__
57 }
58
59 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
60         g_assert(appdata);
61         g_assert(!isnan(speed));
62
63         // convert float to a 6 digits (including dot) wide string with leading zeros
64         gchar* charspeed = g_malloc(10); // alloc
65         g_sprintf(charspeed, "%0*.2f", 6, speed);
66
67         g_print("Speed is %s km/h\n", charspeed);
68
69         guint i = 3;
70         while(i) {
71                 i--;
72                 set_nth_digit(appdata, i, g_ascii_digit_value(charspeed[i]));
73         }
74         repaint_all_digits(appdata);
75
76         g_free(charspeed);
77 }
78
79
80 static show_dialog() {
81         GtkWidget *dialog = gtk_message_dialog_new(
82                         NULL,
83                         GTK_DIALOG_MODAL,
84                         GTK_MESSAGE_INFO,
85                         GTK_BUTTONS_OK,
86                         "This program is licensed under GNU General Public License, "
87                         "which means (among other things) that you don't have to pay "
88                         "a dime for it. "
89                         "If you think, however, that this software is worth it, you "
90                         "can always drop me a postcard.\n\n"
91                         "Wellu Mäkinen\n"
92                         "PO BOX\n"
93                         "33580 Tampere\n"
94                         "FINLAND");
95         gtk_dialog_run(GTK_DIALOG(dialog));
96         gtk_widget_destroy(dialog);
97 }
98
99 void show_cardware_dialog() {
100         GConfClient* client = gconf_client_get_default();
101         g_assert(GCONF_IS_CLIENT(client));
102
103         GConfValue* gcvalue = NULL;
104         gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
105
106         if(gcvalue == NULL) {
107                 g_print("GConf key not found so show dialog.");
108                 show_dialog();
109                 gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
110         }
111         g_object_unref(client);
112 }
113