Moved lots of UI stuff to ui.c.
authorwellu <wellu@wellu.org>
Thu, 2 Oct 2008 18:22:17 +0000 (18:22 +0000)
committerwellu <wellu@wellu.org>
Thu, 2 Oct 2008 18:22:17 +0000 (18:22 +0000)
New callbacks for detecting tapping on the center.
Cleanups here and there.

git-svn-id: file:///svnroot/speedometer/trunk@41 df364472-da61-43ef-8a67-511c89aa921b

TODO
callbacks.c
callbacks.h
main.c
ui.c
ui.h
util.c
util.h

diff --git a/TODO b/TODO
index 1295e92..fd1596e 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,8 +1 @@
-This is a list of things that are on the pipeline:
-
- - Make AppData part of ui.h --> lots of AppData pointer passing
-   should fade away
- - Themes: Splitting the package into two; graphics and the program itself
-
- -
+Nothing..
\ No newline at end of file
index f8e29ff..704f102 100644 (file)
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****/
 
-#include <math.h>
 #include <hildon/hildon-banner.h>
+#include <math.h>
 
 #include "callbacks.h"
 #include "appdata.h"
-#include "util.h"
+#include "ui.h"
 
 void location_changed(LocationGPSDevice* device, gpointer data) {
-       g_assert(data);
-       g_assert(device);
-
-       AppData* appdata = (AppData*) data;
-
        // check for NaN before passing values
        if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
                if(!isnan(device->fix->speed)) {
-                       interpret_speed_from_gps(appdata, device->fix->speed);
+                       interpret_and_set_speed(device->fix->speed);
                        }
        }
 }
@@ -80,6 +75,8 @@ gboolean key_press_cb(GtkWidget* widget, GdkEventKey* event, HildonWindow* windo
 }
 
 gboolean top_event_box_button_press(GtkWidget* widget, GdkEventButton* event, gpointer data) {
+       g_assert(data);
+
        gdouble x = event->x;
        g_print("Top event box pressed at: %f\n", x);
        AppData* appdata = (AppData*) data;
@@ -91,6 +88,12 @@ gboolean top_event_box_button_press(GtkWidget* widget, GdkEventButton* event, gp
        return TRUE;
 }
 
+gboolean middle_event_box_button_press(GtkWidget* widget, GdkEventButton* event, gpointer data) {
+       gdouble x = event->x;
+       g_print("Middle event box pressed at: %f\n", x);
+       g_print("TODO: change multiplier\n");
+       return TRUE;
+}
 
 gboolean bottom_event_box_button_press(GtkWidget* widget, GdkEventButton* event, gpointer data) {
        gdouble x = event->x;
index 66b868d..465482b 100644 (file)
@@ -29,6 +29,9 @@ void location_changed(LocationGPSDevice* device, gpointer data);
 // for hardware keys
 gboolean key_press_cb(GtkWidget* widget, GdkEventKey* event, HildonWindow* window);
 
+// middle event box
+gboolean middle_event_box_button_press(GtkWidget* widget, GdkEventButton* event, gpointer data);
+
 // top event box
 gboolean top_event_box_button_press(GtkWidget* widget, GdkEventButton* event, gpointer data);
 
diff --git a/main.c b/main.c
index ed83e7b..e67a164 100644 (file)
--- a/main.c
+++ b/main.c
@@ -52,7 +52,7 @@ void init_app() {
 
        osso_ctx = osso_initialize(PROGNAME, "1.0", FALSE, NULL);
 
-       delay_display_blanking(appdata->osso_ctx);
+       delay_display_blanking(osso_ctx);
        g_timeout_add(55000, (GSourceFunc) delay_display_blanking, osso_ctx);
 }
 
@@ -67,7 +67,7 @@ int main(int argc, char** argv) {
        init_app();
 
        // loads images from the disk to the image array
-       load_graphix(appdata);
+       load_graphics(appdata);
 
        // set display to 000
        set_digits_to_zero(appdata);
diff --git a/ui.c b/ui.c
index aec5446..d5394c7 100644 (file)
--- a/ui.c
+++ b/ui.c
 
 #include <gtk/gtk.h>
 #include <glib/gprintf.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+#include <math.h>
+#include <gconf/gconf-client.h>
 
 #include "ui.h"
 #include "callbacks.h"
 
-static GtkWidget* graphix[10];         // contains all the graphics
-static GtkWidget* big_digits[3];       // big digits that are shown as speed
-static GtkWidget* small_digits[2];     // small digits that are shown on the screen
+#define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
+
+/* This is used when converting to other units
+ * number represents a multiplier which is used
+ * when converting the base unit to other units.
+ * Base unit is m/s thus the first multiplier is
+ * one. Units are in following order:
+ *
+ * m/s, km/h, mph
+ */
+gdouble conversion[] = { 1, 3.6, 2.237 };
+
+static GdkPixbuf* big_graphics[10];                    // contains all the big graphics
+static GdkPixbuf* small_graphics[10];  // contains small graphics
+static GtkWidget* digits[5];           //
 
 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
 
@@ -37,60 +52,88 @@ static void set_widget_bg_black(GtkWidget* widget) {
        gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
 }
 
-void load_graphix(AppData *appdata) {
-       g_assert(appdata);
-       g_print("Loading images\n");
+
+/* Loads all the graphics from the disk and
+ * scales the images down to be used in several
+ * places. Might block for few seconds depending
+ * on the load.
+ */
+void load_graphics() {
+       g_print("Loading and scaling images\n");
        guint i = 0;
+
+       /* This loop will load all the images from the disk
+        * and store the pixbufs to the array. Pixbufs are
+        * correct size to be used in the big digits.
+        */
        while(i < 10) {
                char* path = g_malloc(30);
                g_sprintf(path, IMAGE_PATH, i);
-               graphix[i] = gtk_image_new_from_file(path);
                g_print(path);
                g_print("\n");
+               GError* error = NULL;
+               big_graphics[i] = gdk_pixbuf_new_from_file(path, &error);
+               if(error) {
+                       g_print("Error loading 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");
 }
 
-void set_digits_to_zero(AppData* appdata) {
-       g_assert(appdata);
-
-       GdkPixbuf* zero = gtk_image_get_pixbuf(GTK_IMAGE(graphix[0]));
-
-       big_digits[0] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
-       big_digits[1] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
-       big_digits[2] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
+void set_digits_to_zero() {
+       digits[0] = gtk_image_new_from_pixbuf(big_graphics[0]);
+       digits[1] = gtk_image_new_from_pixbuf(big_graphics[0]);
+       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]);
 }
 
-void set_nth_digit(AppData* appdata, guint n, guint value) {
-
+static void set_nth_digit(guint n, guint value) {
        g_assert(value < 10);
-       g_assert(n < 3);
+       g_assert(n < 5);
 
-       GtkWidget* image = big_digits[n];
-       GdkPixbuf* buf = gtk_image_get_pixbuf(GTK_IMAGE(graphix[value]));
-       gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
+       if(n < 3) {
+               GtkWidget* image = digits[n];
+               GdkPixbuf* buf = big_graphics[value];
+               gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
+       }
+       else {
+               GtkWidget* image = digits[n];
+               GdkPixbuf* buf = small_graphics[value];
+               gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
+       }
 }
 
-void repaint_all_digits(AppData* appdata) {
-
-       gtk_widget_queue_draw(GTK_WIDGET(big_digits[0]));
-       gtk_widget_queue_draw(GTK_WIDGET(big_digits[1]));
-       gtk_widget_queue_draw(GTK_WIDGET(big_digits[2]));
+static void repaint_all_digits() {
+       gtk_widget_queue_draw(GTK_WIDGET(digits[0]));
+       gtk_widget_queue_draw(GTK_WIDGET(digits[1]));
+       gtk_widget_queue_draw(GTK_WIDGET(digits[2]));
 }
 
 void create_ui(AppData* appdata) {
        g_assert(appdata);
 
-       appdata->unit = 1; // by default use km/h display
-
        GtkWidget *hbox;
+       GtkWidget *bhbox;
        GtkWidget *vbox;
 
        vbox = gtk_vbox_new(FALSE, 0);
        hbox = gtk_hbox_new(TRUE, 0);
+       bhbox = 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();
 
        g_signal_connect(G_OBJECT(top_e),
@@ -98,6 +141,11 @@ void create_ui(AppData* appdata) {
                        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),
+                       appdata);
+
        g_signal_connect(G_OBJECT(bottom_e),
                        "button_press_event",
                        G_CALLBACK(bottom_event_box_button_press),
@@ -108,28 +156,111 @@ void create_ui(AppData* appdata) {
                        G_CALLBACK(gtk_main_quit),
                        NULL);
 
-    g_signal_connect(G_OBJECT(appdata->window),
-               "key_press_event",
-               G_CALLBACK(key_press_cb),
-               appdata->window);
+       g_signal_connect(G_OBJECT(appdata->window),
+                       "key_press_event",
+                       G_CALLBACK(key_press_cb),
+                       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);
+
+       // 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 three digits to the hbox
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(big_digits[0]), FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(big_digits[1]), FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(big_digits[2]), 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);
 
-       gtk_box_pack_start_defaults(GTK_BOX(vbox), top_e); // add event box on top
-       gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox); // numbers to the middle
-       gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_e); // add event box on bottom
+       // 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);
 
+       // finally add the vertical box with all the children to the window
        gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
 
        // 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);
 
        gtk_window_fullscreen(GTK_WINDOW(appdata->window));
        gtk_widget_show_all(GTK_WIDGET(appdata->window));
 }
 
+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];
+
+       /* Convert float to a 6 digits (including dot) wide
+        * string with leading zeros. After conversion
+        * the speed might look like:
+        *
+        * 009.20 (9,20 km/h) or
+        * 010.90 (10,90 km/h) or
+        * 120.10 (120,10 km/h)
+        *
+        * Now, regardless of speed we know the position
+        * of the digits and can access the array directly.
+        */
+       gchar* charspeed = g_malloc(10); // alloc
+       g_sprintf(charspeed, "%0*.2f", 6, speed);
+
+       g_print("Speed is %s km/h\n", charspeed);
+
+       // these three lines will set the big digits
+       set_nth_digit(0, g_ascii_digit_value(charspeed[0]));
+       set_nth_digit(1, g_ascii_digit_value(charspeed[1]));
+       set_nth_digit(2, g_ascii_digit_value(charspeed[2]));
+
+       // these two lines will set the small digits
+       set_nth_digit(3, g_ascii_digit_value(charspeed[4]));
+       set_nth_digit(4, g_ascii_digit_value(charspeed[5]));
+
+       repaint_all_digits();
+
+       g_free(charspeed);
+}
+
+static void show_dialog() {
+       GtkWidget *dialog = gtk_message_dialog_new(
+                       NULL,
+                       GTK_DIALOG_MODAL,
+                       GTK_MESSAGE_INFO,
+                       GTK_BUTTONS_OK,
+                       "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");
+       gtk_dialog_run(GTK_DIALOG(dialog));
+       gtk_widget_destroy(dialog);
+}
+
+void show_cardware_dialog() {
+       GConfClient* client = gconf_client_get_default();
+       g_assert(GCONF_IS_CLIENT(client));
+
+       GConfValue* gcvalue = NULL;
+       gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
+
+       if(gcvalue == NULL) {
+               g_print("GConf key not found so show dialog.\n");
+               show_dialog();
+               gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
+       }
+       g_object_unref(client);
+}
+
diff --git a/ui.h b/ui.h
index 9997de0..7d5114f 100644 (file)
--- a/ui.h
+++ b/ui.h
 
 #include "appdata.h"
 
-void load_graphix(AppData *appdata);
+// loads all the graphics from the disk
+void load_graphics();
 
-void set_digits_to_zero(AppData* appdata);
-
-void set_nth_digit(AppData* appdata, guint n, guint value);
-
-// queues repaint of all digits on the screen
-void repaint_all_digits(AppData* appdata);
+// resets the display to 000.00
+void set_digits_to_zero();
 
+// creates all the widgets
 void create_ui(AppData* appdata);
 
+// signal handlers
 void connect_signals(AppData* appdata);
 
+// converts and sets the speed on display
+void interpret_and_set_speed(gdouble speed);
+
+// shows instructions how to send me a postcard
+void show_cardware_dialog();
+
 #endif /* UI_H_ */
diff --git a/util.c b/util.c
index 671f9cc..0cda59d 100644 (file)
--- a/util.c
+++ b/util.c
 #include <math.h>
 #include <location/location-gps-device.h>
 #include <location/location-gpsd-control.h>
-#include <gconf/gconf-client.h>
-#include <glib/gprintf.h>
 
 #include "util.h"
 #include "appdata.h"
 #include "callbacks.h"
 #include "ui.h"
 
-/* This is used when converting to other units
- * number represents a multiplier which is used
- * when converting the base unit to other units.
- * Base unit is m/s thus the first multiplier is
- * one. Units are in following order:
- *
- * m/s, km/h, mph
- */
-gdouble conversion[] = { 1, 3.6, 2.237 };
-
-#define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
-
-static LocationGPSDevice *device = NULL;
-static LocationGPSDControl *control = NULL;
+#ifdef __arm__
+       static LocationGPSDevice *device = NULL;
+       static LocationGPSDControl *control = NULL;
+#endif
 
-void start_gps(AppData* appdata) {
+void start_gps() {
 #ifdef __arm__
-       g_assert(appdata);
        if(!device) {
                device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
-               g_signal_connect(device, "changed", G_CALLBACK(location_changed), appdata);
+               g_signal_connect(device, "changed", G_CALLBACK(location_changed), NULL);
        }
        control = location_gpsd_control_get_default();
        location_gpsd_control_request_status(control);
@@ -57,9 +44,8 @@ void start_gps(AppData* appdata) {
 #endif // __arm__
 }
 
-void stop_gps(AppData* appdata) {
+void stop_gps() {
 #ifdef __arm__
-       g_assert(appdata);
        control = location_gpsd_control_get_default();
        location_gpsd_control_request_status(control);
        if(control->can_control) {
@@ -68,68 +54,9 @@ void stop_gps(AppData* appdata) {
 #endif // __arm__
 }
 
-void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
-       g_assert(appdata);
-       g_assert(!isnan(speed));
-
-       /* speed is in m/s so let's convert
-        * it to the unit that we are using
-        */
-       speed *= conversion[appdata->unit];
-
-       // convert float to a 6 digits (including dot) wide string with leading zeros
-       gchar* charspeed = g_malloc(10); // alloc
-       g_sprintf(charspeed, "%0*.2f", 6, speed);
-
-       g_print("Speed is %s km/h\n", charspeed);
-
-       // set the main digits
-       guint i = 3;
-       while(i) {
-               i--;
-               set_nth_digit(appdata, i, g_ascii_digit_value(charspeed[i]));
-       }
-
-       repaint_all_digits(appdata);
-
-       g_free(charspeed);
-}
-
-
-static void show_dialog() {
-       GtkWidget *dialog = gtk_message_dialog_new(
-                       NULL,
-                       GTK_DIALOG_MODAL,
-                       GTK_MESSAGE_INFO,
-                       GTK_BUTTONS_OK,
-                       "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");
-       gtk_dialog_run(GTK_DIALOG(dialog));
-       gtk_widget_destroy(dialog);
-}
-
-void show_cardware_dialog() {
-       GConfClient* client = gconf_client_get_default();
-       g_assert(GCONF_IS_CLIENT(client));
-
-       GConfValue* gcvalue = NULL;
-       gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
-
-       if(gcvalue == NULL) {
-               g_print("GConf key not found so show dialog.");
-               show_dialog();
-               gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
-       }
-       g_object_unref(client);
-}
-
+/* Loads the settings from GConf
+ *
+ */
 void load_settings() {
 
 }
diff --git a/util.h b/util.h
index 2e24e92..a33b468 100644 (file)
--- a/util.h
+++ b/util.h
 #include "appdata.h"
 
 // starts the gps
-void start_gps(AppData* appdata);
+void start_gps();
 
 // stops the gps
-void stop_gps(AppData* appdata);
-
-// converts the speed from the gps
-void interpret_speed_from_gps(AppData* appdata, gdouble speed);
-
-// shows instructions how to send me a postcard
-void show_cardware_dialog();
+void stop_gps();
 
 // loads settings from GConf
 void load_settings();