Use separate directories for data and source.
[stopish] / src / stopish.c
diff --git a/src/stopish.c b/src/stopish.c
new file mode 100644 (file)
index 0000000..c1fd229
--- /dev/null
@@ -0,0 +1,208 @@
+//      stopish.c
+//
+//      Copyright 2009 Michael Cronenworth <mike@cchtml.com>
+//
+//      This program is free software; you can redistribute it and/or modify
+//      it under the terms of the GNU General Public License as published by
+//      the Free Software Foundation; either version 2 of the License, or
+//      (at your option) any later version.
+//
+//      This program is distributed in the hope that it will be useful,
+//      but WITHOUT ANY WARRANTY; without even the implied warranty of
+//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//      GNU General Public License for more details.
+//
+//      You should have received a copy of the GNU General Public License
+//      along with this program; if not, write to the Free Software
+//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+//      MA 02110-1301, USA.
+
+
+#include <string.h>
+#include <stdlib.h>
+#include <sys/time.h>
+
+#include <gtk/gtk.h>
+#include <libosso.h>
+
+static GtkWidget *timerLabel = NULL;
+static int stopishMode = 0; // 0 = Start, 1 = Stop
+static int timerHandle = -1;
+static int timerStartTime = 0;
+static int timeTicks = 0;
+
+//Prototypes
+gint dbus_callback( const gchar *interface, const gchar *method,
+                       GArray *arguments, gpointer data, osso_rpc_t *retval );
+GtkWindow *stopish_new( void );
+static void start_cb( GtkButton* button, gpointer data );
+static void close_cb( GtkButton* button, gpointer data );
+static gint timeout_cb( gpointer data );
+static int current_time( void );
+
+
+int main( int argc, char *argv[] )
+{
+    osso_context_t *ctxt;
+    osso_return_t ret;
+    GtkWindow *window;
+
+    //printf( "stopish: starting up\n" );
+
+    ctxt = osso_initialize( "com.nokia.stopish", PACKAGE_VERSION, TRUE, NULL );
+    if ( ctxt == NULL ) {
+        fprintf( stderr, "osso_initialize failed.\n" );
+        exit( 1 );
+    }
+
+    gtk_init( &argc, &argv );
+
+    window = stopish_new(  );
+
+    ret = osso_rpc_set_default_cb_f( ctxt, dbus_callback, window );
+    if ( ret != OSSO_OK ) {
+        fprintf( stderr, "osso_rpc_set_default_cb_f failed: %d.\n", ret );
+        exit( 1 );
+    }
+
+    gtk_main(  );
+
+    return 0;
+}
+
+
+gint dbus_callback( const gchar *interface, const gchar *method,
+                   GArray *arguments, gpointer data, osso_rpc_t *retval )
+{
+    //printf( "stopish dbus: %s, %s\n", interface, method );
+
+    if ( !strcmp( method, "top_application" ) )
+        gtk_window_present( GTK_WINDOW( data ) );
+
+    retval->type = DBUS_TYPE_INVALID;
+
+    return OSSO_OK;
+}
+
+
+GtkWindow *stopish_new( void )
+{
+    GtkWidget *window, *hBox, *label, *button;
+    GtkWidget *vBox, *vBox0;
+
+    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
+
+    gtk_container_set_border_width( GTK_CONTAINER( window ), 20 );
+
+    gtk_window_set_title( GTK_WINDOW( window ), "Stopish" );
+
+    g_signal_connect( G_OBJECT( window ), "destroy",
+                      G_CALLBACK( close_cb ), window );
+
+    vBox = gtk_vbox_new( FALSE, 10 );
+
+    label = gtk_label_new( "Stopish - The Stopwatch" );
+    gtk_box_pack_start( GTK_BOX( vBox ), label, FALSE, FALSE, 0 );
+
+    hBox = gtk_hbox_new( FALSE, 10 );
+
+    // stop watch area
+    timerLabel = gtk_label_new( NULL );
+    gtk_label_set_markup( GTK_LABEL( timerLabel ), "<big>00:00:00.0</big>" );
+    gtk_container_add( GTK_CONTAINER( hBox ), timerLabel );
+
+    // button area
+    vBox0 = gtk_vbox_new( FALSE, 10 );
+
+    // close button
+    button = gtk_button_new(  );
+    label = gtk_label_new( "Start" );
+    gtk_container_add( GTK_CONTAINER( button ), label );
+    g_signal_connect( G_OBJECT( button ), "clicked",
+                      G_CALLBACK( start_cb ), label );
+    gtk_box_pack_start( GTK_BOX( vBox0 ), button, FALSE, FALSE, 0 );
+
+    // start stopwatch button
+    button = gtk_button_new_with_label( "Close" );
+    g_signal_connect( G_OBJECT( button ), "clicked",
+                      G_CALLBACK( close_cb ), window );
+    gtk_box_pack_start( GTK_BOX( vBox0 ), button, FALSE, FALSE, 0 );
+
+    gtk_container_add( GTK_CONTAINER( hBox ), vBox0 );
+
+    gtk_container_add( GTK_CONTAINER( vBox ), hBox );
+
+    gtk_container_add( GTK_CONTAINER( window ), vBox );
+
+    gtk_widget_show_all( window );
+
+    return GTK_WINDOW( window );
+}
+
+
+static void start_cb( GtkButton* button, gpointer data )
+{
+    if ( stopishMode == 0 ) {
+        // set label text and add timer handle
+        gtk_label_set_text( GTK_LABEL( data ), "Stop" );
+        stopishMode = 1;
+        timerHandle = g_timeout_add( 100, timeout_cb, NULL );
+        timerStartTime = current_time(  ) - timeTicks;
+    }
+    else {
+        // set label text and remove timer handle
+        gtk_label_set_text( GTK_LABEL( data ), "Start" );
+        stopishMode = 0;
+        timerStartTime = 0;
+        timeTicks = 0;
+        gtk_label_set_markup( GTK_LABEL( timerLabel ),
+                              "<big>00:00:00.0</big>" );
+        g_source_remove( timerHandle );
+    }
+}
+
+
+static void close_cb( GtkButton* button, gpointer data )
+{
+    // destroy main window and exit gtk main loop
+    gtk_widget_destroy( GTK_WIDGET( data ) );
+    gtk_main_quit(  );
+}
+
+
+static gint timeout_cb( gpointer data )
+{
+    static char timeBuffer[64];
+    int h, m, s, ss, currentTime;
+
+    // get current time
+    currentTime = current_time(  );
+
+    // calculate time format
+    timeTicks = ( currentTime - timerStartTime );
+    ss = timeTicks % 10;
+    s = timeTicks / 10;
+    m = s / 60;
+    s = s % 60;
+    h = m / 60;
+    m = m % 60;
+
+    // print to screen
+    sprintf( timeBuffer, "<big>%.02d:%.02d:%.02d.%.1d</big>", h, m, s, ss);
+    gtk_label_set_markup( GTK_LABEL( timerLabel ), timeBuffer );
+
+    return TRUE;
+}
+
+
+static int current_time( void )
+{
+    struct timeval tv;
+    int s, us;
+
+    gettimeofday( &tv, NULL );
+    s = tv.tv_sec % 100000;
+    us = tv.tv_usec / 100000;
+
+    return ( s * 10 + us );
+}