671f9cc82b292db268e039c636f90693de830261
[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 #include <glib/gprintf.h>
24
25 #include "util.h"
26 #include "appdata.h"
27 #include "callbacks.h"
28 #include "ui.h"
29
30 /* This is used when converting to other units
31  * number represents a multiplier which is used
32  * when converting the base unit to other units.
33  * Base unit is m/s thus the first multiplier is
34  * one. Units are in following order:
35  *
36  * m/s, km/h, mph
37  */
38 gdouble conversion[] = { 1, 3.6, 2.237 };
39
40 #define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
41
42 static LocationGPSDevice *device = NULL;
43 static LocationGPSDControl *control = NULL;
44
45 void start_gps(AppData* appdata) {
46 #ifdef __arm__
47         g_assert(appdata);
48         if(!device) {
49                 device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
50                 g_signal_connect(device, "changed", G_CALLBACK(location_changed), appdata);
51         }
52         control = location_gpsd_control_get_default();
53         location_gpsd_control_request_status(control);
54         if(control->can_control) {
55                 location_gpsd_control_start(control);
56         }
57 #endif // __arm__
58 }
59
60 void stop_gps(AppData* appdata) {
61 #ifdef __arm__
62         g_assert(appdata);
63         control = location_gpsd_control_get_default();
64         location_gpsd_control_request_status(control);
65         if(control->can_control) {
66                 location_gpsd_control_stop(control);
67         }
68 #endif // __arm__
69 }
70
71 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
72         g_assert(appdata);
73         g_assert(!isnan(speed));
74
75         /* speed is in m/s so let's convert
76          * it to the unit that we are using
77          */
78         speed *= conversion[appdata->unit];
79
80         // convert float to a 6 digits (including dot) wide string with leading zeros
81         gchar* charspeed = g_malloc(10); // alloc
82         g_sprintf(charspeed, "%0*.2f", 6, speed);
83
84         g_print("Speed is %s km/h\n", charspeed);
85
86         // set the main digits
87         guint i = 3;
88         while(i) {
89                 i--;
90                 set_nth_digit(appdata, i, g_ascii_digit_value(charspeed[i]));
91         }
92
93         repaint_all_digits(appdata);
94
95         g_free(charspeed);
96 }
97
98
99 static void show_dialog() {
100         GtkWidget *dialog = gtk_message_dialog_new(
101                         NULL,
102                         GTK_DIALOG_MODAL,
103                         GTK_MESSAGE_INFO,
104                         GTK_BUTTONS_OK,
105                         "This program is licensed under GNU General Public License, "
106                         "which means (among other things) that you don't have to pay "
107                         "a dime for it. "
108                         "If you think, however, that this software is worth it, you "
109                         "can always drop me a postcard.\n\n"
110                         "Wellu Mäkinen\n"
111                         "PO BOX\n"
112                         "33580 Tampere\n"
113                         "FINLAND");
114         gtk_dialog_run(GTK_DIALOG(dialog));
115         gtk_widget_destroy(dialog);
116 }
117
118 void show_cardware_dialog() {
119         GConfClient* client = gconf_client_get_default();
120         g_assert(GCONF_IS_CLIENT(client));
121
122         GConfValue* gcvalue = NULL;
123         gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
124
125         if(gcvalue == NULL) {
126                 g_print("GConf key not found so show dialog.");
127                 show_dialog();
128                 gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
129         }
130         g_object_unref(client);
131 }
132
133 void load_settings() {
134
135 }