Cleanup
[speedometer] / ui.c
diff --git a/ui.c b/ui.c
index d5394c7..8e7d610 100644 (file)
--- a/ui.c
+++ b/ui.c
  * Base unit is m/s thus the first multiplier is
  * one. Units are in following order:
  *
- * m/s, km/h, mph
+ * m/s, km/h, mph, f/s, knot
  */
-gdouble conversion[] = { 1, 3.6, 2.237 };
+gdouble conversion[] = { 1, 3.6, 2.237, 3.281, 1.944 };
+guint unit = 1;
+#define UNIT_COUNT 5
+// TODO: wrap this mess up somewhere
 
-static GdkPixbuf* big_graphics[10];                    // contains all the big graphics
-static GdkPixbuf* small_graphics[10];  // contains small graphics
-static GtkWidget* digits[5];           //
+static GdkPixbuf* big_graphics[15];
+static GdkPixbuf* small_graphics[15];
+static GtkWidget* digits[5];
+static GtkWidget* unit_graphic;
 
 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
 
@@ -66,26 +70,33 @@ void load_graphics() {
         * and store the pixbufs to the array. Pixbufs are
         * correct size to be used in the big digits.
         */
-       while(i < 10) {
+       while(i < 15) {
                char* path = g_malloc(30);
                g_sprintf(path, IMAGE_PATH, i);
                g_print(path);
                g_print("\n");
                GError* error = NULL;
-               big_graphics[i] = gdk_pixbuf_new_from_file(path, &error);
+               big_graphics[i] = gdk_pixbuf_new_from_file_at_scale(path,
+                               160,
+                               160,
+                               FALSE,
+                               &error);
                if(error) {
-                       g_print("Error loading graphics: %s\n", error->message);
+                       g_print("Error loading big graphics: %s\n", error->message);
+                       g_error_free(error);
+                       error = NULL;
+               }
+               small_graphics[i] = gdk_pixbuf_new_from_file_at_scale(path,
+                                               120,
+                                               120,
+                                               FALSE,
+                                               &error);
+               if(error) {
+                       g_print("Error loading small graphics: %s\n", error->message);
                        g_error_free(error);
                        error = NULL;
                }
                g_free(path);
-
-               small_graphics[i] = gdk_pixbuf_scale_simple(
-                               big_graphics[i],
-                               60,
-                               60,
-                               GDK_INTERP_BILINEAR);
-
                i++;
        }
        g_print("Done\n");
@@ -97,6 +108,8 @@ void set_digits_to_zero() {
        digits[2] = gtk_image_new_from_pixbuf(big_graphics[0]);
        digits[3] = gtk_image_new_from_pixbuf(small_graphics[0]);
        digits[4] = gtk_image_new_from_pixbuf(small_graphics[0]);
+       // TODO: load this from GConf
+       unit_graphic = gtk_image_new_from_pixbuf(small_graphics[11]);
 }
 
 static void set_nth_digit(guint n, guint value) {
@@ -124,31 +137,25 @@ static void repaint_all_digits() {
 void create_ui(AppData* appdata) {
        g_assert(appdata);
 
-       GtkWidget *hbox;
-       GtkWidget *bhbox;
+       GtkWidget *top_hbox;
+       GtkWidget *bottom_hbox;
        GtkWidget *vbox;
 
        vbox = gtk_vbox_new(FALSE, 0);
-       hbox = gtk_hbox_new(TRUE, 0);
-       bhbox = gtk_hbox_new(TRUE, 0);
+       top_hbox = gtk_hbox_new(TRUE, 0);
+       bottom_hbox = gtk_hbox_new(TRUE, 0);
 
-       GtkWidget* top_e = gtk_event_box_new();
-       GtkWidget* middle_e = gtk_event_box_new();
-       GtkWidget* bottom_e = gtk_event_box_new();
+       GtkWidget* event_box = gtk_event_box_new();
 
-       g_signal_connect(G_OBJECT(top_e),
-                       "button_press_event",
-                       G_CALLBACK(top_event_box_button_press),
-                       appdata);
-
-       g_signal_connect(G_OBJECT(middle_e),
-                       "button_press_event",
-                       G_CALLBACK(middle_event_box_button_press),
+       g_signal_connect(G_OBJECT(event_box),
+                       "tap_and_hold",
+                       G_CALLBACK(long_tap),
                        appdata);
+       gtk_widget_tap_and_hold_setup(event_box, NULL, NULL, 0);
 
-       g_signal_connect(G_OBJECT(bottom_e),
+       g_signal_connect(G_OBJECT(event_box),
                        "button_press_event",
-                       G_CALLBACK(bottom_event_box_button_press),
+                       G_CALLBACK(short_tap),
                        appdata);
 
        g_signal_connect(G_OBJECT(appdata->window),
@@ -162,47 +169,67 @@ void create_ui(AppData* appdata) {
                        appdata->window);
 
        // add three big digits to the hbox
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[0]), FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[1]), FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[2]), FALSE, FALSE, 0);
+       // creates the top row where big digits are placed
+       gtk_box_pack_start(GTK_BOX(top_hbox), GTK_WIDGET(digits[0]), FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(top_hbox), GTK_WIDGET(digits[1]), FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(top_hbox), GTK_WIDGET(digits[2]), FALSE, FALSE, 0);
 
        // add small digits to another hbox
-       gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[3]), FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[4]), FALSE, FALSE, 0);
-
-       // add hboxes to the event boxes
-       gtk_container_add(GTK_CONTAINER(middle_e), hbox);
-       gtk_container_add(GTK_CONTAINER(bottom_e), bhbox);
+       // creates the bottom row with decimals and unit graphic
+       gtk_box_pack_start(GTK_BOX(bottom_hbox), GTK_WIDGET(unit_graphic), FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(bottom_hbox), GTK_WIDGET(digits[3]), FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(bottom_hbox), GTK_WIDGET(digits[4]), FALSE, FALSE, 0);
 
        // add event boxes to the vbox
-       gtk_box_pack_start_defaults(GTK_BOX(vbox), top_e);
-       gtk_box_pack_start_defaults(GTK_BOX(vbox), middle_e);
-       gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_e);
+       gtk_box_pack_start_defaults(GTK_BOX(vbox), top_hbox);
+       gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_hbox);
+
+       // add vertival box to the event box
+       gtk_container_add(GTK_CONTAINER(event_box), GTK_WIDGET(vbox));
 
-       // finally add the vertical box with all the children to the window
-       gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
+       // finally add the event box with all the children to the window
+       gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(event_box));
 
        // set backgrounds black
        set_widget_bg_black(GTK_WIDGET(appdata->window));
-       set_widget_bg_black(bottom_e);
-       set_widget_bg_black(top_e);
-       set_widget_bg_black(middle_e);
+       set_widget_bg_black(event_box);
 
        gtk_window_fullscreen(GTK_WINDOW(appdata->window));
        gtk_widget_show_all(GTK_WIDGET(appdata->window));
 }
 
+void change_unit() {
+       unit++;
+
+       if(unit >= UNIT_COUNT) {
+               unit = 0;
+       }
+       g_print("Unit used in conversion: %f\n", conversion[unit]);
+
+       GdkPixbuf* buf = small_graphics[10 + unit];
+       gtk_image_set_from_pixbuf(GTK_IMAGE(unit_graphic), buf);
+}
+
 void interpret_and_set_speed(gdouble speed) {
        g_assert(!isnan(speed));
 
        /* speed is in m/s so let's convert
         * it to the unit that we are using
         */
-       speed *= conversion[1];
+       speed *= conversion[unit];
+
+       /* we need to limit the speed down to
+        * less than 1000 whatever or else hell
+        * breaks loose in the form of assert
+        */
+       if(!(fabs(speed) < 1000)) {
+           g_print("Ahem, may I suggest to limit the speed (%f) a bit, Sir.\n", speed);
+           speed = 0;
+       }
 
        /* Convert float to a 6 digits (including dot) wide
         * string with leading zeros. After conversion
-        * the speed might look like:
+        * the speed looks like:
         *
         * 009.20 (9,20 km/h) or
         * 010.90 (10,90 km/h) or
@@ -239,12 +266,7 @@ static void show_dialog() {
                        "This program is licensed under GNU General Public License, "
                        "which means (among other things) that you don't have to pay "
                        "a dime for it. "
-                       "If you think, however, that this software is worth it, you "
-                       "can always drop me a postcard.\n\n"
-                       "Wellu Mäkinen\n"
-                       "PO BOX\n"
-                       "33580 Tampere\n"
-                       "FINLAND");
+                       "PS. Long-tapping on the screen will quit the program.");
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
 }