2 Speedometer, shows your current speed using GPS
3 Copyright (C) 2008 Wellu Mäkinen <wellu@wellu.org>
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.
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.
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/>.
20 #include <glib/gprintf.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include <gconf/gconf-client.h>
26 #include "callbacks.h"
28 #define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
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:
36 * m/s, km/h, mph, f/s, knot
38 gdouble conversion[] = { 1, 3.6, 2.237, 3.281, 1.944 };
41 // TODO: wrap this mess up somewhere
42 // TODO: knot graphics are missing
44 static GdkPixbuf* big_graphics[14];
45 static GdkPixbuf* small_graphics[14];
46 static GtkWidget* digits[5];
47 static GtkWidget* unit_graphic;
49 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
51 static void set_widget_bg_black(GtkWidget* widget) {
57 gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
61 /* Loads all the graphics from the disk and
62 * scales the images down to be used in several
63 * places. Might block for few seconds depending
66 void load_graphics() {
67 g_print("Loading and scaling images\n");
70 /* This loop will load all the images from the disk
71 * and store the pixbufs to the array. Pixbufs are
72 * correct size to be used in the big digits.
75 char* path = g_malloc(30);
76 g_sprintf(path, IMAGE_PATH, i);
80 big_graphics[i] = gdk_pixbuf_new_from_file(path, &error);
82 g_print("Error loading graphics: %s\n", error->message);
88 small_graphics[i] = gdk_pixbuf_scale_simple(
99 void set_digits_to_zero() {
100 digits[0] = gtk_image_new_from_pixbuf(big_graphics[0]);
101 digits[1] = gtk_image_new_from_pixbuf(big_graphics[0]);
102 digits[2] = gtk_image_new_from_pixbuf(big_graphics[0]);
103 digits[3] = gtk_image_new_from_pixbuf(small_graphics[0]);
104 digits[4] = gtk_image_new_from_pixbuf(small_graphics[0]);
105 // TODO: load this from GConf
106 unit_graphic = gtk_image_new_from_pixbuf(small_graphics[11]);
109 static void set_nth_digit(guint n, guint value) {
110 g_assert(value < 10);
114 GtkWidget* image = digits[n];
115 GdkPixbuf* buf = big_graphics[value];
116 gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
119 GtkWidget* image = digits[n];
120 GdkPixbuf* buf = small_graphics[value];
121 gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
125 static void repaint_all_digits() {
126 gtk_widget_queue_draw(GTK_WIDGET(digits[0]));
127 gtk_widget_queue_draw(GTK_WIDGET(digits[1]));
128 gtk_widget_queue_draw(GTK_WIDGET(digits[2]));
131 void create_ui(AppData* appdata) {
138 vbox = gtk_vbox_new(FALSE, 0);
139 hbox = gtk_hbox_new(TRUE, 0);
140 bhbox = gtk_hbox_new(TRUE, 0);
142 GtkWidget* top_e = gtk_event_box_new();
143 GtkWidget* middle_e = gtk_event_box_new();
144 GtkWidget* bottom_e = gtk_event_box_new();
146 g_signal_connect(G_OBJECT(top_e),
147 "button_press_event",
148 G_CALLBACK(top_event_box_button_press),
151 g_signal_connect(G_OBJECT(middle_e),
152 "button_press_event",
153 G_CALLBACK(middle_event_box_button_press),
156 g_signal_connect(G_OBJECT(bottom_e),
157 "button_press_event",
158 G_CALLBACK(bottom_event_box_button_press),
161 g_signal_connect(G_OBJECT(appdata->window),
163 G_CALLBACK(gtk_main_quit),
166 g_signal_connect(G_OBJECT(appdata->window),
168 G_CALLBACK(key_press_cb),
171 // add three big digits to the hbox
172 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[0]), FALSE, FALSE, 0);
173 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[1]), FALSE, FALSE, 0);
174 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[2]), FALSE, FALSE, 0);
176 // add small digits to another hbox
177 gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(unit_graphic), FALSE, FALSE, 0);
178 gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[3]), FALSE, FALSE, 0);
179 gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[4]), FALSE, FALSE, 0);
181 // add hboxes to the event boxes
182 gtk_container_add(GTK_CONTAINER(middle_e), hbox);
183 gtk_container_add(GTK_CONTAINER(bottom_e), bhbox);
185 // add event boxes to the vbox
186 gtk_box_pack_start_defaults(GTK_BOX(vbox), top_e);
187 gtk_box_pack_start_defaults(GTK_BOX(vbox), middle_e);
188 gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_e);
190 // finally add the vertical box with all the children to the window
191 gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
193 // set backgrounds black
194 set_widget_bg_black(GTK_WIDGET(appdata->window));
195 set_widget_bg_black(bottom_e);
196 set_widget_bg_black(top_e);
197 set_widget_bg_black(middle_e);
199 gtk_window_fullscreen(GTK_WINDOW(appdata->window));
200 gtk_widget_show_all(GTK_WIDGET(appdata->window));
206 if(unit >= UNIT_COUNT) {
209 g_print("Unit used in conversion: %f\n", conversion[unit]);
211 GdkPixbuf* buf = small_graphics[10 + unit];
212 gtk_image_set_from_pixbuf(GTK_IMAGE(unit_graphic), buf);
215 void interpret_and_set_speed(gdouble speed) {
216 g_assert(!isnan(speed));
218 /* speed is in m/s so let's convert
219 * it to the unit that we are using
221 speed *= conversion[unit];
223 /* Convert float to a 6 digits (including dot) wide
224 * string with leading zeros. After conversion
225 * the speed looks like:
227 * 009.20 (9,20 km/h) or
228 * 010.90 (10,90 km/h) or
229 * 120.10 (120,10 km/h)
231 * Now, regardless of speed we know the position
232 * of the digits and can access the array directly.
234 gchar* charspeed = g_malloc(10); // alloc
235 g_sprintf(charspeed, "%0*.2f", 6, speed);
237 g_print("Speed is %s km/h\n", charspeed);
239 // these three lines will set the big digits
240 set_nth_digit(0, g_ascii_digit_value(charspeed[0]));
241 set_nth_digit(1, g_ascii_digit_value(charspeed[1]));
242 set_nth_digit(2, g_ascii_digit_value(charspeed[2]));
244 // these two lines will set the small digits
245 set_nth_digit(3, g_ascii_digit_value(charspeed[4]));
246 set_nth_digit(4, g_ascii_digit_value(charspeed[5]));
248 repaint_all_digits();
253 static void show_dialog() {
254 GtkWidget *dialog = gtk_message_dialog_new(
259 "This program is licensed under GNU General Public License, "
260 "which means (among other things) that you don't have to pay "
262 "If you think, however, that this software is worth it, you "
263 "can always drop me a postcard.\n\n"
268 gtk_dialog_run(GTK_DIALOG(dialog));
269 gtk_widget_destroy(dialog);
272 void show_cardware_dialog() {
273 GConfClient* client = gconf_client_get_default();
274 g_assert(GCONF_IS_CLIENT(client));
276 GConfValue* gcvalue = NULL;
277 gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
279 if(gcvalue == NULL) {
280 g_print("GConf key not found so show dialog.\n");
282 gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
284 g_object_unref(client);