*Hildon-ize the app for better widget support.
[stopish] / src / timer.c
1 //      timer.c
2 //
3 //      Copyright 2009 Michael Cronenworth <mike@cchtml.com>
4 //
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 2 of the License, or
8 //      (at your option) any later version.
9 //
10 //      This program is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //
15 //      You should have received a copy of the GNU General Public License
16 //      along with this program; if not, write to the Free Software
17 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 //      MA 02110-1301, USA.
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <gtk/gtk.h>
24 #include <libosso.h>
25
26 #include "stopish.h"
27
28 static long int timerStartTime = 0;
29 static long int timerSave = 0;
30
31
32 //
33 // Timer callback
34 //
35 gint stopish_timeout_cb( gpointer data )
36 {
37     GtkLabel *timerLabel;
38     char formatBuffer[128];
39     char *tempString;
40
41     if ( !data )
42         return -1;
43
44     timerLabel = ( GtkLabel * ) data;
45
46     // print to screen
47     tempString = stopish_get_time_string(  );
48     sprintf( formatBuffer, "<span font_family=\"monospace\" "
49                           "size=\"70000\" weight=\"ultrabold\">"
50                           "%s</span>", tempString );
51     free( tempString );
52     gtk_label_set_markup( GTK_LABEL( timerLabel ), formatBuffer );
53
54     return TRUE;
55 }
56
57
58 char *stopish_get_time_string( void )
59 {
60     char *timeBuffer;
61     long int currentTime;
62     int h, m, s, ss;
63
64     // get current time
65     currentTime = stopish_current_time(  );
66
67     // calculate time format
68     ss = ( currentTime - timerStartTime ) % 10;
69     s = ( currentTime - timerStartTime ) / 10;
70     m = s / 60;
71     s = s % 60;
72     h = m / 60;
73     m = m % 60;
74
75     // rollover once we hit one day
76     if ( h > 24 ) {
77         stopish_set_time_start( stopish_current_time(  ) );
78         h = m = s = ss = 0;
79     }
80
81     timeBuffer = malloc( 64 );
82     sprintf( timeBuffer, "%.02d:%.02d:%.02d.%.1d",
83              h, m, s, ss );
84
85     return timeBuffer;
86 }
87
88
89 void stopish_set_time_start( long int time )
90 {
91     // reset timer to user-inputted time
92     timerStartTime = time;
93     timerSave = 0;
94 }
95
96
97 void stopish_timer_resume( void )
98 {
99     timerStartTime = stopish_current_time(  );
100     timerStartTime -= timerSave;
101 }
102
103
104 void stopish_timer_save( void )
105 {
106     // save time counter
107     timerSave = stopish_current_time(  ) - timerStartTime;
108 }
109
110
111 long int stopish_current_time( void )
112 {
113     struct timeval tv;
114     int s, us;
115
116     gettimeofday( &tv, NULL );
117     s = tv.tv_sec % 100000;
118     us = tv.tv_usec / 100000;
119
120     return ( s * 10 + us );
121 }