Implement more functionality:
[stopish] / src / timer.c
diff --git a/src/timer.c b/src/timer.c
new file mode 100644 (file)
index 0000000..ca1c41d
--- /dev/null
@@ -0,0 +1,119 @@
+//      timer.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>
+
+#include "stopish.h"
+
+static long int timerStartTime = 0;
+static long int timerSave = 0;
+
+
+//
+// Timer callback
+//
+gint stopish_timeout_cb( gpointer data )
+{
+    GtkLabel *timerLabel;
+    char formatBuffer[64];
+    char *tempString;
+
+    if ( !data )
+        return -1;
+
+    timerLabel = ( GtkLabel * ) data;
+
+    // print to screen
+    tempString = stopish_get_time_string(  );
+    sprintf( formatBuffer, "<span font_family=\"monospace\" size=\"xx-large\">%s</span>", tempString );
+    free( tempString );
+    gtk_label_set_markup( GTK_LABEL( timerLabel ), formatBuffer );
+
+    return TRUE;
+}
+
+
+char *stopish_get_time_string( void )
+{
+    char *timeBuffer;
+    long int currentTime;
+    int h, m, s, ss;
+
+    // get current time
+    currentTime = stopish_current_time(  );
+
+    // calculate time format
+    ss = ( currentTime - timerStartTime ) % 10;
+    s = ( currentTime - timerStartTime ) / 10;
+    m = s / 60;
+    s = s % 60;
+    h = m / 60;
+    m = m % 60;
+
+    // rollover once we hit one day
+    if ( h > 24 ) {
+        stopish_set_time_start( stopish_current_time(  ) );
+        h = m = s = ss = 0;
+    }
+
+    timeBuffer = malloc( 64 );
+    sprintf( timeBuffer, "%.02d:%.02d:%.02d.%.1d",
+             h, m, s, ss );
+
+    return timeBuffer;
+}
+
+
+void stopish_set_time_start( long int time )
+{
+    // reset timer to user-inputted time
+    timerStartTime = time;
+    timerSave = 0;
+}
+
+
+void stopish_timer_resume( void )
+{
+    timerStartTime = stopish_current_time(  );
+    timerStartTime -= timerSave;
+}
+
+
+void stopish_timer_save( void )
+{
+    // save time counter
+    timerSave = stopish_current_time(  ) - timerStartTime;
+}
+
+
+long int stopish_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 );
+}