GConf support.
authorwellu <wellu@wellu.org>
Thu, 11 Sep 2008 13:47:53 +0000 (13:47 +0000)
committerwellu <wellu@wellu.org>
Thu, 11 Sep 2008 13:47:53 +0000 (13:47 +0000)
git-svn-id: file:///svnroot/speedometer/trunk@32 df364472-da61-43ef-8a67-511c89aa921b

Makefile
appdata.h
callbacks.c
debian/changelog
main.c
ui.c
ui.h
util.c
util.h

index 91ee7bc..e13141e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,9 @@
+PKGS     = gtk+-2.0 liblocation gconf-2.0
 CC       = gcc
 LINK     = gcc
 CFLAGS   = -g -Wall -O0
-INCPATH  = `pkg-config --cflags gtk+-2.0 liblocation`
-LIBS     = `pkg-config --libs gtk+-2.0 liblocation`
+INCPATH  = `pkg-config --cflags $(PKGS)`
+LIBS     = `pkg-config --libs $(PKGS)`
 
 SOURCES  = main.c callbacks.c ui.c util.c
 OBJECTS  = main.o callbacks.o ui.o util.o
index f1a39fa..c8befbf 100644 (file)
--- a/appdata.h
+++ b/appdata.h
 #include <hildon/hildon-program.h>
 #include <hildon/hildon-window.h>
 
-typedef struct _AppData AppData;
-struct _AppData {
+typedef struct _AppData {
     HildonProgram *program;
     HildonWindow *window;
 
-       GtkImage* image_array[10]; // stores the images
-       GtkImage* speed_array[3]; // actual images that are shown
-};
+       GtkWidget* image_array[10]; // original images from the disk
+       GtkWidget* digit_array[3]; // actual images that are shown as speed
+} AppData;
 
 #endif /* APPDATA_H_ */
index 3c48b2d..57964cb 100644 (file)
 #include "appdata.h"
 #include "util.h"
 
-// this is just here for debugging and tracing purposes
+// this is just here for debugging
 static void print_location(LocationGPSDevice* device) {
        g_print("Latitude: %.2f\n"
                        "Longitude: %.2f\n"
                        "Altitude: %.2f\n"
-                       "Speed: %d",
+                       "Speed: %d\n",
                        device->fix->latitude,
                        device->fix->longitude,
                        device->fix->altitude,
@@ -33,12 +33,12 @@ static void print_location(LocationGPSDevice* device) {
 }
 
 void location_changed(LocationGPSDevice* device, gpointer data) {
-       print_location(device);
+       //print_location(device);
 
        AppData* appdata = (AppData*) data;
 
-       if(device->fix && LOCATION_GPS_DEVICE_SPEED_SET) {
-               g_print("Speed is %f km/h", device->fix->speed);
+       if(device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET) {
+               g_print("Speed is %.2f km/h\n", device->fix->speed);
                interpret_speed_from_gps(appdata, device->fix->speed);
        }
 }
index a1a620f..e9ecae7 100644 (file)
@@ -1,3 +1,9 @@
+speedometer (0.3-alpha2) unstable; urgency=low
+
+  * Fixes to interpret_speed_from_gps.
+
+ -- Wellu Mäkinen <wellu@wellu.org>  Tue,  9 Sep 2008 14:41:50 +0300
+
 speedometer (0.3-alpha1) unstable; urgency=low
 
   * First alpha release.
diff --git a/main.c b/main.c
index 82c24c8..dd85ac2 100644 (file)
--- a/main.c
+++ b/main.c
@@ -40,7 +40,7 @@ int main(int argc, char** argv) {
        load_images(appdata);
 
        // set display to 000
-       reset_speed(appdata);
+       load_initial_pixbufs(appdata);
 
        // create ui structure
        create_ui(appdata);
@@ -52,6 +52,8 @@ int main(int argc, char** argv) {
 
        g_thread_init(NULL);
 
+       show_postcard_dialog();
+
        //g_idle_add(randomize, appdata);
 
        start_gps(appdata);
diff --git a/ui.c b/ui.c
index d582f5b..44fd2c8 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -33,50 +33,53 @@ static void set_widget_bg_black(GtkWidget* widget) {
 
 void load_images(AppData *appdata) {
        g_print("Loading images\n");
-       int i = 0;
+       guint i = 0;
        while(i < 10) {
-               char* path = malloc(30);
+               char* path = g_malloc(30);
                g_sprintf(path, IMAGE_PATH, i);
-               appdata->image_array[i] = (GtkImage*) gtk_image_new_from_file(path);
+               appdata->image_array[i] = gtk_image_new_from_file(path);
                g_print(path);
                g_print("\n");
-               free(path);
+               g_free(path);
                i++;
        }
 }
 
-void reset_speed(AppData* appdata) {
+void load_initial_pixbufs(AppData* appdata) {
 
-       GdkPixbuf* zero = gtk_image_get_pixbuf(appdata->image_array[0]);
+       GdkPixbuf* zero = gtk_image_get_pixbuf(GTK_IMAGE(appdata->image_array[0]));
 
-       appdata->speed_array[0] = (GtkImage*) gtk_image_new_from_pixbuf(zero);
-       appdata->speed_array[1] = (GtkImage*) gtk_image_new_from_pixbuf(zero);
-       appdata->speed_array[2] = (GtkImage*) gtk_image_new_from_pixbuf(zero);
+       appdata->digit_array[0] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
+       appdata->digit_array[1] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
+       appdata->digit_array[2] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
 }
 
 void set_nth_digit(AppData* appdata, guint n, guint value) {
        //assert(nth >= 0 && nth < 3);
 
-       GtkImage* image = appdata->speed_array[n];
-       GdkPixbuf* buf = gtk_image_get_pixbuf(appdata->image_array[value]);
-       gtk_image_set_from_pixbuf(image, buf);
-       gtk_widget_queue_draw(GTK_WIDGET(appdata->speed_array[n]));
+       GtkWidget* image = appdata->digit_array[n];
+       GdkPixbuf* buf = gtk_image_get_pixbuf(GTK_IMAGE(appdata->image_array[value]));
+       gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
 }
 
-guint randomize(AppData* appdata) {
+void repaint_all_digits(AppData* appdata) {
+       gtk_widget_queue_draw(GTK_WIDGET(appdata->digit_array[0]));
+       gtk_widget_queue_draw(GTK_WIDGET(appdata->digit_array[1]));
+       gtk_widget_queue_draw(GTK_WIDGET(appdata->digit_array[2]));
+}
+
+void randomize(AppData* appdata) {
 
        gint32 n = g_random_int_range(0, 3);
-       GtkImage* image = appdata->speed_array[n];
+       GtkWidget* image = appdata->digit_array[n];
 
        gint32 m = g_random_int_range(0, 10);
-       GdkPixbuf* buf = gtk_image_get_pixbuf(appdata->image_array[m]);
+       GdkPixbuf* buf = gtk_image_get_pixbuf(GTK_IMAGE(appdata->image_array[m]));
 
        g_print("Setting number %d to %d.. ", n+1, m);
 
-       gtk_image_set_from_pixbuf(image, buf);
-       gtk_widget_queue_draw(GTK_WIDGET(appdata->speed_array[n]));
-
-       return TRUE;
+       gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
+       gtk_widget_queue_draw(appdata->digit_array[n]);
 }
 
 void create_ui(AppData* appdata) {
@@ -111,9 +114,9 @@ void create_ui(AppData* appdata) {
                appdata->window);
 
        // add three digits to the hbox
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->speed_array[0]), FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->speed_array[1]), FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->speed_array[2]), FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->digit_array[0]), FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->digit_array[1]), FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->digit_array[2]), FALSE, FALSE, 0);
 
        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
diff --git a/ui.h b/ui.h
index fc5f8ea..79828ee 100644 (file)
--- a/ui.h
+++ b/ui.h
 
 void load_images(AppData *appdata);
 
-void reset_speed(AppData* appdata);
+void load_initial_pixbufs(AppData* appdata);
 
 void set_nth_digit(AppData* appdata, guint n, guint value);
 
-guint randomize(AppData* appdata);
+// queues repaint of all digits on the screen
+void repaint_all_digits(AppData* appdata);
+
+void randomize(AppData* appdata);
 
 void create_ui(AppData* appdata);
 
diff --git a/util.c b/util.c
index b53b8aa..dee79a0 100644 (file)
--- a/util.c
+++ b/util.c
 
 #include <location/location-gps-device.h>
 #include <location/location-gpsd-control.h>
+#include <gconf/gconf-client.h>
 
 #include "util.h"
 #include "appdata.h"
 #include "callbacks.h"
 #include "ui.h"
 
+#define GCONF_DIR "/apps/Maemo/speedometer/"
+
 static LocationGPSDevice *device = NULL;
 static LocationGPSDControl *control = NULL;
 
@@ -37,7 +40,7 @@ void start_gps(AppData* appdata) {
        location_gpsd_control_request_status(control);
        if(control->can_control) {
                location_gpsd_control_start(control);
-               }
+       }
 #endif // __arm__
 }
 
@@ -53,19 +56,64 @@ void stop_gps(AppData* appdata) {
 
 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
 
-       // first let's convert the number to characters
-       gchar* c_digits = calloc(30, 1);
+       // if speed is below one then it's zero
+       if(speed < 1) {
+               set_nth_digit(appdata, 0, 0);
+               set_nth_digit(appdata, 1, 0);
+               set_nth_digit(appdata, 2, 0);
+               return;
+       }
+
+       // speed => 1
+       // first let's convert the number to string
+       gchar* cspeed = g_malloc(30); // alloc
+       g_sprintf(cspeed, "%f", speed);
+
+       // split the string using comma as delimiter
+       gchar** splitted = g_strsplit(cspeed, ",", 2); // alloc
+
+       gchar* ckm = splitted[0]; // contains the km/h part e.g 123 assuming speed was 123,034510
+       gchar* cm = splitted[1]; // contains the m/h part e.g 034510 assuming speed was 123,034510
+
+       g_print("Original speed %c, splitted speed %c and %c\n", cspeed, ckm, cm);
+
+       // we need to pad km part in order to ensure it is *atleast* 3 digits long
+       gchar* padding = "00";
+       gchar* padded = g_strconcat(padding, cm, NULL); // alloc
+
+       g_print("Original speed %c, padded speed %c (km) and %c (m)\n", cspeed, padded, cm);
 
-       g_sprintf(c_digits, "%f", speed);
 
-       g_print("interpret_speed_from_gps: %s", c_digits);
+       guint i = 2;
+       guint pspeedl = strlen(padded);
 
-       int i = 0;
-       while(i < 3) {
-               guint value = g_ascii_digit_value(c_digits[i]);
+       while(i+1) {
+               guint value = g_ascii_digit_value(padded[pspeedl]);
                set_nth_digit(appdata, i, value);
-               i++;
+               i--;
+       }
+       repaint_all_digits(appdata);
+
+       g_free(padded);
+       g_free(cspeed);
+       g_strfreev(splitted);
+}
+
+void show_postcard_dialog() {
+       GConfClient* client = gconf_client_get_default();
+       g_assert(GCONF_IS_CLIENT(client));
+
+
+       GConfValue* gcvalue = NULL;
+       gcvalue = gconf_client_get_without_default(client, GCONF_DIR "disclaimer", NULL);
+
+       if(gcvalue == NULL) {
+               g_warning("key %sdisclaimer not found\n", GCONF_DIR);
+               gconf_client_set_bool(client, GCONF_DIR "disclaimer", TRUE, NULL);
+       }
+       else {
+               g_print("key %sdisclaimer was found\n", GCONF_DIR);
        }
-       g_free(c_digits);
+       g_object_unref(client);
 }
 
diff --git a/util.h b/util.h
index d6b639b..a7c5be6 100644 (file)
--- a/util.h
+++ b/util.h
@@ -30,4 +30,7 @@ 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_postcard_dialog();
+
 #endif /* UTIL_H_ */