3 // Copyright 2009 Michael Cronenworth <mike@cchtml.com>
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.
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.
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.
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;
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 );
44 int main( int argc, char *argv[] )
50 //printf( "stopish: starting up\n" );
52 ctxt = osso_initialize( "com.nokia.stopish", PACKAGE_VERSION, TRUE, NULL );
54 fprintf( stderr, "osso_initialize failed.\n" );
58 gtk_init( &argc, &argv );
60 window = stopish_new( );
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 );
74 gint dbus_callback( const gchar *interface, const gchar *method,
75 GArray *arguments, gpointer data, osso_rpc_t *retval )
77 //printf( "stopish dbus: %s, %s\n", interface, method );
79 if ( !strcmp( method, "top_application" ) )
80 gtk_window_present( GTK_WINDOW( data ) );
82 retval->type = DBUS_TYPE_INVALID;
88 GtkWindow *stopish_new( void )
90 GtkWidget *window, *hBox, *label, *button;
91 GtkWidget *vBox, *vBox0;
93 window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
95 gtk_container_set_border_width( GTK_CONTAINER( window ), 20 );
97 gtk_window_set_title( GTK_WINDOW( window ), "Stopish" );
99 g_signal_connect( G_OBJECT( window ), "destroy",
100 G_CALLBACK( close_cb ), window );
102 vBox = gtk_vbox_new( FALSE, 10 );
104 label = gtk_label_new( "Stopish - The Stopwatch" );
105 gtk_box_pack_start( GTK_BOX( vBox ), label, FALSE, FALSE, 0 );
107 hBox = gtk_hbox_new( FALSE, 10 );
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 );
115 vBox0 = gtk_vbox_new( FALSE, 10 );
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 );
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 );
131 gtk_container_add( GTK_CONTAINER( hBox ), vBox0 );
133 gtk_container_add( GTK_CONTAINER( vBox ), hBox );
135 gtk_container_add( GTK_CONTAINER( window ), vBox );
137 gtk_widget_show_all( window );
139 return GTK_WINDOW( window );
143 static void start_cb( GtkButton* button, gpointer data )
145 if ( stopishMode == 0 ) {
146 // set label text and add timer handle
147 gtk_label_set_text( GTK_LABEL( data ), "Stop" );
149 timerHandle = g_timeout_add( 100, timeout_cb, NULL );
150 timerStartTime = current_time( ) - timeTicks;
153 // set label text and remove timer handle
154 gtk_label_set_text( GTK_LABEL( data ), "Start" );
158 gtk_label_set_markup( GTK_LABEL( timerLabel ),
159 "<big>00:00:00.0</big>" );
160 g_source_remove( timerHandle );
165 static void close_cb( GtkButton* button, gpointer data )
167 // destroy main window and exit gtk main loop
168 gtk_widget_destroy( GTK_WIDGET( data ) );
173 static gint timeout_cb( gpointer data )
175 static char timeBuffer[64];
176 int h, m, s, ss, currentTime;
179 currentTime = current_time( );
181 // calculate time format
182 timeTicks = ( currentTime - timerStartTime );
191 sprintf( timeBuffer, "<big>%.02d:%.02d:%.02d.%.1d</big>", h, m, s, ss);
192 gtk_label_set_markup( GTK_LABEL( timerLabel ), timeBuffer );
198 static int current_time( void )
203 gettimeofday( &tv, NULL );
204 s = tv.tv_sec % 100000;
205 us = tv.tv_usec / 100000;
207 return ( s * 10 + us );