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