New graphics including knots.
[speedometer] / ui.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 <gtk/gtk.h>
20 #include <glib/gprintf.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <math.h>
23 #include <gconf/gconf-client.h>
24
25 #include "ui.h"
26 #include "callbacks.h"
27
28 #define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
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, f/s, knot
37  */
38 gdouble conversion[] = { 1, 3.6, 2.237, 3.281, 1.944 };
39 guint unit = 1;
40 #define UNIT_COUNT 5
41 // TODO: wrap this mess up somewhere
42
43 static GdkPixbuf* big_graphics[15];
44 static GdkPixbuf* small_graphics[15];
45 static GtkWidget* digits[5];
46 static GtkWidget* unit_graphic;
47
48 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
49
50 static void set_widget_bg_black(GtkWidget* widget) {
51         g_assert(widget);
52         GdkColor black;
53         black.red = 0x0000;
54         black.blue = 0x0000;
55         black.green = 0x0000;
56         gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
57 }
58
59
60 /* Loads all the graphics from the disk and
61  * scales the images down to be used in several
62  * places. Might block for few seconds depending
63  * on the load.
64  */
65 void load_graphics() {
66         g_print("Loading and scaling images\n");
67         guint i = 0;
68
69         /* This loop will load all the images from the disk
70          * and store the pixbufs to the array. Pixbufs are
71          * correct size to be used in the big digits.
72          */
73         while(i < 15) {
74                 char* path = g_malloc(30);
75                 g_sprintf(path, IMAGE_PATH, i);
76                 g_print(path);
77                 g_print("\n");
78                 GError* error = NULL;
79                 big_graphics[i] = gdk_pixbuf_new_from_file_at_scale(path,
80                                 160,
81                                 160,
82                                 FALSE,
83                                 &error);
84                 if(error) {
85                         g_print("Error loading big graphics: %s\n", error->message);
86                         g_error_free(error);
87                         error = NULL;
88                 }
89                 small_graphics[i] = gdk_pixbuf_new_from_file_at_scale(path,
90                                                 120,
91                                                 120,
92                                                 FALSE,
93                                                 &error);
94                 if(error) {
95                         g_print("Error loading small graphics: %s\n", error->message);
96                         g_error_free(error);
97                         error = NULL;
98                 }
99                 g_free(path);
100                 i++;
101         }
102         g_print("Done\n");
103 }
104
105 void set_digits_to_zero() {
106         digits[0] = gtk_image_new_from_pixbuf(big_graphics[0]);
107         digits[1] = gtk_image_new_from_pixbuf(big_graphics[0]);
108         digits[2] = gtk_image_new_from_pixbuf(big_graphics[0]);
109         digits[3] = gtk_image_new_from_pixbuf(small_graphics[0]);
110         digits[4] = gtk_image_new_from_pixbuf(small_graphics[0]);
111         // TODO: load this from GConf
112         unit_graphic = gtk_image_new_from_pixbuf(small_graphics[11]);
113 }
114
115 static void set_nth_digit(guint n, guint value) {
116         g_assert(value < 10);
117         g_assert(n < 5);
118
119         if(n < 3) {
120                 GtkWidget* image = digits[n];
121                 GdkPixbuf* buf = big_graphics[value];
122                 gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
123         }
124         else {
125                 GtkWidget* image = digits[n];
126                 GdkPixbuf* buf = small_graphics[value];
127                 gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
128         }
129 }
130
131 static void repaint_all_digits() {
132         gtk_widget_queue_draw(GTK_WIDGET(digits[0]));
133         gtk_widget_queue_draw(GTK_WIDGET(digits[1]));
134         gtk_widget_queue_draw(GTK_WIDGET(digits[2]));
135 }
136
137 void create_ui(AppData* appdata) {
138         g_assert(appdata);
139
140         GtkWidget *top_hbox;
141         GtkWidget *bottom_hbox;
142         GtkWidget *vbox;
143
144         vbox = gtk_vbox_new(FALSE, 0);
145         top_hbox = gtk_hbox_new(TRUE, 0);
146         bottom_hbox = gtk_hbox_new(TRUE, 0);
147
148         GtkWidget* event_box = gtk_event_box_new();
149
150         g_signal_connect(G_OBJECT(event_box),
151                         "tap_and_hold",
152                         G_CALLBACK(long_tap),
153                         appdata);
154         gtk_widget_tap_and_hold_setup(event_box, NULL, NULL, 0);
155
156         g_signal_connect(G_OBJECT(event_box),
157                         "button_press_event",
158                         G_CALLBACK(short_tap),
159                         appdata);
160
161         g_signal_connect(G_OBJECT(appdata->window),
162                         "delete_event",
163                         G_CALLBACK(gtk_main_quit),
164                         NULL);
165
166         g_signal_connect(G_OBJECT(appdata->window),
167                         "key_press_event",
168                         G_CALLBACK(key_press_cb),
169                         appdata->window);
170
171         // add three big digits to the hbox
172         // creates the top row where big digits are placed
173         gtk_box_pack_start(GTK_BOX(top_hbox), GTK_WIDGET(digits[0]), FALSE, FALSE, 0);
174         gtk_box_pack_start(GTK_BOX(top_hbox), GTK_WIDGET(digits[1]), FALSE, FALSE, 0);
175         gtk_box_pack_start(GTK_BOX(top_hbox), GTK_WIDGET(digits[2]), FALSE, FALSE, 0);
176
177         // add small digits to another hbox
178         // creates the bottom row with decimals and unit graphic
179         gtk_box_pack_start(GTK_BOX(bottom_hbox), GTK_WIDGET(unit_graphic), FALSE, FALSE, 0);
180         gtk_box_pack_start(GTK_BOX(bottom_hbox), GTK_WIDGET(digits[3]), FALSE, FALSE, 0);
181         gtk_box_pack_start(GTK_BOX(bottom_hbox), GTK_WIDGET(digits[4]), FALSE, FALSE, 0);
182
183         // add event boxes to the vbox
184         gtk_box_pack_start_defaults(GTK_BOX(vbox), top_hbox);
185         gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_hbox);
186
187         // add vertival box to the event box
188         gtk_container_add(GTK_CONTAINER(event_box), GTK_WIDGET(vbox));
189
190         // finally add the event box with all the children to the window
191         gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(event_box));
192
193         // set backgrounds black
194         set_widget_bg_black(GTK_WIDGET(appdata->window));
195         set_widget_bg_black(event_box);
196
197         gtk_window_fullscreen(GTK_WINDOW(appdata->window));
198         gtk_widget_show_all(GTK_WIDGET(appdata->window));
199 }
200
201 void change_unit() {
202         unit++;
203
204         if(unit >= UNIT_COUNT) {
205                 unit = 0;
206         }
207         g_print("Unit used in conversion: %f\n", conversion[unit]);
208
209         GdkPixbuf* buf = small_graphics[10 + unit];
210         gtk_image_set_from_pixbuf(GTK_IMAGE(unit_graphic), buf);
211 }
212
213 void interpret_and_set_speed(gdouble speed) {
214         g_assert(!isnan(speed));
215
216         /* speed is in m/s so let's convert
217          * it to the unit that we are using
218          */
219         speed *= conversion[unit];
220
221         /* we need to limit the speed down to
222          * less than 1000 whatever or else hell
223          * breaks loose in the form of assert
224          */
225         if(!(fabs(speed) < 1000)) {
226             g_print("Ahem, may I suggest to limit the speed (%f) a bit, Sir.\n", speed);
227             speed = 0;
228         }
229
230         /* Convert float to a 6 digits (including dot) wide
231          * string with leading zeros. After conversion
232          * the speed looks like:
233          *
234          * 009.20 (9,20 km/h) or
235          * 010.90 (10,90 km/h) or
236          * 120.10 (120,10 km/h)
237          *
238          * Now, regardless of speed we know the position
239          * of the digits and can access the array directly.
240          */
241         gchar* charspeed = g_malloc(10); // alloc
242         g_sprintf(charspeed, "%0*.2f", 6, speed);
243
244         g_print("Speed is %s km/h\n", charspeed);
245
246         // these three lines will set the big digits
247         set_nth_digit(0, g_ascii_digit_value(charspeed[0]));
248         set_nth_digit(1, g_ascii_digit_value(charspeed[1]));
249         set_nth_digit(2, g_ascii_digit_value(charspeed[2]));
250
251         // these two lines will set the small digits
252         set_nth_digit(3, g_ascii_digit_value(charspeed[4]));
253         set_nth_digit(4, g_ascii_digit_value(charspeed[5]));
254
255         repaint_all_digits();
256
257         g_free(charspeed);
258 }
259
260 static void show_dialog() {
261         GtkWidget *dialog = gtk_message_dialog_new(
262                         NULL,
263                         GTK_DIALOG_MODAL,
264                         GTK_MESSAGE_INFO,
265                         GTK_BUTTONS_OK,
266                         "This program is licensed under GNU General Public License, "
267                         "which means (among other things) that you don't have to pay "
268                         "a dime for it. "
269                         "PS. Long-tapping on the screen will quit the program.");
270         gtk_dialog_run(GTK_DIALOG(dialog));
271         gtk_widget_destroy(dialog);
272 }
273
274 void show_cardware_dialog() {
275         GConfClient* client = gconf_client_get_default();
276         g_assert(GCONF_IS_CLIENT(client));
277
278         GConfValue* gcvalue = NULL;
279         gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
280
281         if(gcvalue == NULL) {
282                 g_print("GConf key not found so show dialog.\n");
283                 show_dialog();
284                 gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
285         }
286         g_object_unref(client);
287 }
288