3 // Copyright 2010 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.
24 #include <hildon/hildon.h>
25 #include <dbus/dbus.h>
26 #include <mce/mode-names.h>
27 #include <mce/dbus-names.h>
28 #define MCE_SIGNAL_MATCH "type='signal'," \
29 "interface='" MCE_SIGNAL_IF "'," \
30 "member='" MCE_DEVICE_ORIENTATION_SIG "'"
34 // Application data struct
35 typedef struct _AppData AppData;
37 GtkWindow *main_window;
38 GtkWindow *countdown_window;
39 osso_context_t *osso_context;
40 DBusConnection *system_bus;
44 static AppData appdata;
45 static int stopishMode = STOPISH_MODE_START;
46 static int stopishOrientation = STOPISH_LANDSCAPE;
47 static int stopishType = STOPISH_TYPE_STOPWATCH;
51 gint dbus_callback( const gchar *interface, const gchar *method,
52 GArray *arguments, gpointer data, osso_rpc_t *retval );
53 static void main_menu( GtkWindow *window );
54 static void change_type_cb( GtkButton* button, gpointer data );
55 static void close_cb( GtkButton* button, gpointer data );
56 static void accelerometer_enable( void );
57 static void accelerometer_disable( void );
58 static DBusHandlerResult mce_filter_func( DBusConnection * connection,
59 DBusMessage * message,
63 int main( int argc, char *argv[] )
66 HildonProgram *program;
68 appdata.osso_context = osso_initialize( "com.nokia.stopish",
69 PACKAGE_VERSION, TRUE, NULL );
70 if ( appdata.osso_context == NULL ) {
71 fprintf( stderr, "osso_initialize failed.\n" );
75 // initialize Hildonized GTK libraries
76 hildon_gtk_init( &argc, &argv );
77 program = hildon_program_get_instance( );
80 appdata.main_window = stopish_stopwatch_new( );
82 // attach signals to main window
83 g_signal_connect( G_OBJECT( appdata.main_window ), "destroy",
84 G_CALLBACK( close_cb ), appdata.main_window );
85 g_signal_connect( G_OBJECT( appdata.main_window ), "focus-in-event",
86 G_CALLBACK( stopish_focus_in_cb ), NULL );
87 g_signal_connect( G_OBJECT( appdata.main_window ), "focus-out-event",
88 G_CALLBACK( stopish_focus_out_cb ), NULL );
91 main_menu( appdata.main_window );
93 hildon_program_add_window( program, HILDON_WINDOW( appdata.main_window ) );
95 // Connect to session bus, add a match rule, install filter callback
96 appdata.system_bus = osso_get_sys_dbus_connection( appdata.osso_context );
97 if ( appdata.system_bus ) {
98 dbus_bus_add_match( appdata.system_bus, MCE_SIGNAL_MATCH, NULL );
99 dbus_connection_add_filter( appdata.system_bus,
104 g_printerr( "ERROR: Cannot connect to system dbus.\n" );
106 ret = osso_rpc_set_default_cb_f( appdata.osso_context,
107 dbus_callback, appdata.main_window );
108 if ( ret != OSSO_OK ) {
109 fprintf( stderr, "osso_rpc_set_default_cb_f failed: %d.\n", ret );
119 gint dbus_callback( const gchar *interface, const gchar *method,
120 GArray *arguments, gpointer data, osso_rpc_t *retval )
122 //printf( "stopish dbus: %s, %s\n", interface, method );
124 if ( !strcmp( method, "top_application" ) )
125 gtk_window_present( GTK_WINDOW( data ) );
127 retval->type = DBUS_TYPE_INVALID;
133 void stopish_about_cb( GtkButton* button, gpointer data )
140 authors[0] = strdup( "Michael Cronenworth" );
143 dialog = gtk_about_dialog_new( );
145 gtk_about_dialog_set_program_name( GTK_ABOUT_DIALOG( dialog ),
147 gtk_about_dialog_set_version( GTK_ABOUT_DIALOG( dialog ),
148 STOPISH_VERSION_STR );
149 gtk_about_dialog_set_authors( GTK_ABOUT_DIALOG( dialog ),
150 ( const char ** ) authors );
151 logo = gdk_pixbuf_new_from_file( "/usr/share/icons/hicolor/40x40/hildon/stopish.png",
153 gtk_about_dialog_set_logo( GTK_ABOUT_DIALOG( dialog ),
156 gtk_dialog_run( GTK_DIALOG( dialog ) );
158 gtk_widget_destroy( dialog );
163 int stopish_get_mode( void )
169 void stopish_set_mode( int newMode )
171 stopishMode = newMode;
175 int stopish_get_type( void )
181 void stopish_set_type( int newType )
183 stopishType = newType;
187 int stopish_get_orientation( void )
189 return stopishOrientation;
193 gboolean stopish_focus_in_cb( GtkWidget *widget, GdkEventFocus *event,
196 // enable accelerometer hardware for portrait mode support
197 accelerometer_enable( );
203 gboolean stopish_focus_out_cb( GtkWidget *widget, GdkEventFocus *event,
206 // disable accelerometer for battery savings
207 accelerometer_disable( );
213 static void main_menu( GtkWindow *window )
216 GtkWidget *button, *radio;
218 menu = ( HildonAppMenu * ) hildon_app_menu_new( );
220 button = gtk_button_new_with_label( "Countdown" );
221 g_signal_connect_after( G_OBJECT( button ), "clicked",
222 G_CALLBACK( change_type_cb ), NULL );
223 hildon_app_menu_append( menu, GTK_BUTTON( button ) );
225 button = gtk_button_new_with_label( "About" );
226 g_signal_connect_after( G_OBJECT( button ), "clicked",
227 G_CALLBACK( stopish_about_cb ), NULL );
228 hildon_app_menu_append( menu, GTK_BUTTON( button ) );
231 radio = gtk_radio_button_new_with_label( NULL, "Hour" );
232 gtk_toggle_button_set_mode( GTK_TOGGLE_BUTTON( radio ), FALSE );
233 g_signal_connect_after( G_OBJECT( radio ), "clicked",
234 G_CALLBACK( stopish_stopwatch_perf_timer_hour ), NULL );
235 hildon_app_menu_add_filter( menu, GTK_BUTTON( radio ) );
238 radio = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON( radio ), "Minute" );
239 gtk_toggle_button_set_mode( GTK_TOGGLE_BUTTON ( radio ), FALSE );
240 g_signal_connect_after( G_OBJECT( radio ), "clicked",
241 G_CALLBACK( stopish_stopwatch_perf_timer_minute ), NULL );
242 hildon_app_menu_add_filter( menu, GTK_BUTTON( radio ) );
245 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( radio ), TRUE );
247 gtk_widget_show_all( GTK_WIDGET( menu ) );
249 hildon_window_set_app_menu( HILDON_WINDOW( window ), menu );
253 static void change_type_cb( GtkButton* button, gpointer data )
255 stopish_stopwatch_reset( );
256 stopishType = STOPISH_TYPE_COUNTDOWN;
257 stopish_countdown_new( );
261 static void close_cb( GtkButton* button, gpointer data )
263 // disable accelerometer for battery savings
264 accelerometer_disable( );
266 // destroy main window and exit gtk main loop
267 gtk_widget_destroy( GTK_WIDGET( data ) );
272 static void accelerometer_enable( void )
274 if ( osso_rpc_run_system( appdata.osso_context, MCE_SERVICE,
275 MCE_REQUEST_PATH, MCE_REQUEST_IF,
276 "req_accelerometer_enable", NULL,
277 DBUS_TYPE_INVALID ) != OSSO_OK ) {
278 g_printerr("WARN: Cannot enable accelerometers\n");
283 static void accelerometer_disable( void )
285 if ( osso_rpc_run_system( appdata.osso_context, MCE_SERVICE,
286 MCE_REQUEST_PATH, MCE_REQUEST_IF,
287 "req_accelerometer_disable", NULL,
288 DBUS_TYPE_INVALID ) != OSSO_OK ) {
289 g_printerr("WARN: Cannot disable accelerometers\n");
294 static DBusHandlerResult mce_filter_func( DBusConnection * connection,
295 DBusMessage * message,
298 DBusMessageIter iter;
299 char *rotation = NULL;
301 if ( dbus_message_is_signal( message, MCE_SIGNAL_IF,
302 MCE_DEVICE_ORIENTATION_SIG ) ) {
303 // here if we received an orientation dbus signal
304 if ( dbus_message_iter_init( message, &iter ) ) {
305 dbus_message_iter_get_basic( &iter, &rotation );
307 // Rotate main window
308 if ( !strcmp( rotation, MCE_ORIENTATION_PORTRAIT ) ) {
309 hildon_gtk_window_set_portrait_flags( GTK_WINDOW( appdata.main_window ),
310 HILDON_PORTRAIT_MODE_REQUEST );
311 if ( stopishType == STOPISH_TYPE_STOPWATCH )
312 stopish_stopwatch_label_timer_portrait( );
314 stopish_countdown_label_timer_portrait( );
315 stopishOrientation = STOPISH_PORTRAIT;
318 hildon_gtk_window_set_portrait_flags( GTK_WINDOW( appdata.main_window ),
319 ~HILDON_PORTRAIT_MODE_REQUEST );
320 if ( stopishType == STOPISH_TYPE_STOPWATCH )
321 stopish_stopwatch_label_timer_landscape( );
323 stopish_countdown_label_timer_landscape( );
324 stopishOrientation = STOPISH_LANDSCAPE;
328 g_printerr( "ERROR: dbus_message_iter_init() failed.\n" );
331 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;