Prep for 0.9.3 release.
[stopish] / src / timer.c
1 //      timer.c
2 //
3 //      Copyright 2010 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 //Preferences
32 static int timerPrecision = TIMER_PRECISION_MINUTE;
33
34
35 char *stopish_get_time_string( void )
36 {
37     char *timeBuffer;
38     long int currentTime;
39     int h, m, s, ss;
40
41     // get current time
42     currentTime = stopish_current_time(  );
43
44     // calculate time format
45     ss = ( currentTime - timerStartTime ) % 10;
46     s = ( currentTime - timerStartTime ) / 10;
47     m = s / 60;
48     s = s % 60;
49     h = m / 60;
50     m = m % 60;
51
52     if ( timerPrecision == TIMER_PRECISION_MINUTE && m > 60 ) {
53         // rollover once we hit one hour
54         stopish_set_time_start( stopish_current_time(  ) );
55         h = m = s = ss = 0;
56     }
57     else if ( timerPrecision == TIMER_PRECISION_HOUR && h > 24 ) {
58         // rollover once we hit one day
59         stopish_set_time_start( stopish_current_time(  ) );
60         h = m = s = ss = 0;
61     }
62
63     if ( stopish_get_type(  ) == STOPISH_TYPE_COUNTDOWN ) {
64         h = -h;
65         m = -m;
66         s = -s;
67         ss = -ss;
68     }
69     // countdown check
70     if ( stopish_get_type(  ) == STOPISH_TYPE_COUNTDOWN &&
71          ( h == 0 && m == 0 && s == 0 && ss == 0 ) )
72         return NULL;
73
74     timeBuffer = malloc( 64 );
75     if ( timerPrecision == TIMER_PRECISION_MINUTE )
76         sprintf( timeBuffer, "%.02d:%.02d.%.1d",
77                  ( unsigned int ) m, ( unsigned int ) s,
78                  ( unsigned int ) ss );
79     else
80         sprintf( timeBuffer, "%.02d:%.02d:%.02d.%.1d",
81                  ( unsigned int ) h, ( unsigned int ) m,
82                  ( unsigned int ) s, ( unsigned int ) ss );
83
84     return timeBuffer;
85 }
86
87
88 void stopish_set_time_start( long int time )
89 {
90     // reset timer to user-inputted time
91     timerStartTime = time;
92     timerSave = 0;
93 }
94
95
96 void stopish_timer_resume( void )
97 {
98     timerStartTime = stopish_current_time(  );
99     timerStartTime -= timerSave;
100 }
101
102
103 void stopish_timer_save( void )
104 {
105     // save time counter
106     timerSave = stopish_current_time(  ) - timerStartTime;
107 }
108
109
110 long int stopish_current_time( void )
111 {
112     struct timeval tv;
113     int s, us;
114
115     gettimeofday( &tv, NULL );
116     s = tv.tv_sec % 100000;
117     us = tv.tv_usec / 100000;
118
119     return ( s * 10 + us );
120 }
121
122
123 void stopish_timer_set_precision( int setting )
124 {
125     timerPrecision = setting;
126 }
127
128
129 int stopish_timer_get_precision( void )
130 {
131     return timerPrecision;
132 }