Use separate directories for data and source.
[stopish] / src / stopish.c
1 //      stopish.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
21 #include <string.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24
25 #include <gtk/gtk.h>
26 #include <libosso.h>
27
28 static GtkWidget *timerLabel = NULL;
29 static int stopishMode = 0; // 0 = Start, 1 = Stop
30 static int timerHandle = -1;
31 static int timerStartTime = 0;
32 static int timeTicks = 0;
33
34 //Prototypes
35 gint dbus_callback( const gchar *interface, const gchar *method,
36                         GArray *arguments, gpointer data, osso_rpc_t *retval );
37 GtkWindow *stopish_new( void );
38 static void start_cb( GtkButton* button, gpointer data );
39 static void close_cb( GtkButton* button, gpointer data );
40 static gint timeout_cb( gpointer data );
41 static int current_time( void );
42
43
44 int main( int argc, char *argv[] )
45 {
46     osso_context_t *ctxt;
47     osso_return_t ret;
48     GtkWindow *window;
49
50     //printf( "stopish: starting up\n" );
51
52     ctxt = osso_initialize( "com.nokia.stopish", PACKAGE_VERSION, TRUE, NULL );
53     if ( ctxt == NULL ) {
54         fprintf( stderr, "osso_initialize failed.\n" );
55         exit( 1 );
56     }
57
58     gtk_init( &argc, &argv );
59
60     window = stopish_new(  );
61
62     ret = osso_rpc_set_default_cb_f( ctxt, dbus_callback, window );
63     if ( ret != OSSO_OK ) {
64         fprintf( stderr, "osso_rpc_set_default_cb_f failed: %d.\n", ret );
65         exit( 1 );
66     }
67
68     gtk_main(  );
69
70     return 0;
71 }
72
73
74 gint dbus_callback( const gchar *interface, const gchar *method,
75                     GArray *arguments, gpointer data, osso_rpc_t *retval )
76 {
77     //printf( "stopish dbus: %s, %s\n", interface, method );
78
79     if ( !strcmp( method, "top_application" ) )
80         gtk_window_present( GTK_WINDOW( data ) );
81
82     retval->type = DBUS_TYPE_INVALID;
83
84     return OSSO_OK;
85 }
86
87
88 GtkWindow *stopish_new( void )
89 {
90     GtkWidget *window, *hBox, *label, *button;
91     GtkWidget *vBox, *vBox0;
92
93     window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
94
95     gtk_container_set_border_width( GTK_CONTAINER( window ), 20 );
96
97     gtk_window_set_title( GTK_WINDOW( window ), "Stopish" );
98
99     g_signal_connect( G_OBJECT( window ), "destroy",
100                       G_CALLBACK( close_cb ), window );
101
102     vBox = gtk_vbox_new( FALSE, 10 );
103
104     label = gtk_label_new( "Stopish - The Stopwatch" );
105     gtk_box_pack_start( GTK_BOX( vBox ), label, FALSE, FALSE, 0 );
106
107     hBox = gtk_hbox_new( FALSE, 10 );
108
109     // stop watch area
110     timerLabel = gtk_label_new( NULL );
111     gtk_label_set_markup( GTK_LABEL( timerLabel ), "<big>00:00:00.0</big>" );
112     gtk_container_add( GTK_CONTAINER( hBox ), timerLabel );
113
114     // button area
115     vBox0 = gtk_vbox_new( FALSE, 10 );
116
117     // close button
118     button = gtk_button_new(  );
119     label = gtk_label_new( "Start" );
120     gtk_container_add( GTK_CONTAINER( button ), label );
121     g_signal_connect( G_OBJECT( button ), "clicked",
122                       G_CALLBACK( start_cb ), label );
123     gtk_box_pack_start( GTK_BOX( vBox0 ), button, FALSE, FALSE, 0 );
124
125     // start stopwatch button
126     button = gtk_button_new_with_label( "Close" );
127     g_signal_connect( G_OBJECT( button ), "clicked",
128                       G_CALLBACK( close_cb ), window );
129     gtk_box_pack_start( GTK_BOX( vBox0 ), button, FALSE, FALSE, 0 );
130
131     gtk_container_add( GTK_CONTAINER( hBox ), vBox0 );
132
133     gtk_container_add( GTK_CONTAINER( vBox ), hBox );
134
135     gtk_container_add( GTK_CONTAINER( window ), vBox );
136
137     gtk_widget_show_all( window );
138
139     return GTK_WINDOW( window );
140 }
141
142
143 static void start_cb( GtkButton* button, gpointer data )
144 {
145     if ( stopishMode == 0 ) {
146         // set label text and add timer handle
147         gtk_label_set_text( GTK_LABEL( data ), "Stop" );
148         stopishMode = 1;
149         timerHandle = g_timeout_add( 100, timeout_cb, NULL );
150         timerStartTime = current_time(  ) - timeTicks;
151     }
152     else {
153         // set label text and remove timer handle
154         gtk_label_set_text( GTK_LABEL( data ), "Start" );
155         stopishMode = 0;
156         timerStartTime = 0;
157         timeTicks = 0;
158         gtk_label_set_markup( GTK_LABEL( timerLabel ),
159                               "<big>00:00:00.0</big>" );
160         g_source_remove( timerHandle );
161     }
162 }
163
164
165 static void close_cb( GtkButton* button, gpointer data )
166 {
167     // destroy main window and exit gtk main loop
168     gtk_widget_destroy( GTK_WIDGET( data ) );
169     gtk_main_quit(  );
170 }
171
172
173 static gint timeout_cb( gpointer data )
174 {
175     static char timeBuffer[64];
176     int h, m, s, ss, currentTime;
177
178     // get current time
179     currentTime = current_time(  );
180
181     // calculate time format
182     timeTicks = ( currentTime - timerStartTime );
183     ss = timeTicks % 10;
184     s = timeTicks / 10;
185     m = s / 60;
186     s = s % 60;
187     h = m / 60;
188     m = m % 60;
189
190     // print to screen
191     sprintf( timeBuffer, "<big>%.02d:%.02d:%.02d.%.1d</big>", h, m, s, ss);
192     gtk_label_set_markup( GTK_LABEL( timerLabel ), timeBuffer );
193
194     return TRUE;
195 }
196
197
198 static int current_time( void )
199 {
200     struct timeval tv;
201     int s, us;
202
203     gettimeofday( &tv, NULL );
204     s = tv.tv_sec % 100000;
205     us = tv.tv_usec / 100000;
206
207     return ( s * 10 + us );
208 }