settings/src/main.c

Go to the documentation of this file.
00001 /*
00002  * File Name: main.c
00003  */
00004 
00005 /*
00006  * This file is part of settings.
00007  *
00008  * settings is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * settings is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2009 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #include <gtk/gtk.h>
00033 #include <gdk/gdkx.h>
00034 #include <signal.h>
00035 #include <sys/types.h>
00036 #include <unistd.h>
00037 #include <stdlib.h>
00038 
00039 // ereader include files, between < >
00040 #include <libergtk/ergtk.h>
00041 
00042 // local include files, between " "
00043 #include "log.h"
00044 #include "advanced.h"
00045 #include "datetime.h"
00046 #include "device_info.h"
00047 #include "fdatetime.h"
00048 #include "flightmode.h"
00049 #include "flipbar.h"
00050 #include "i18n.h"
00051 #include "ipc.h"
00052 #include "irex-account.h"
00053 #include "languages.h"
00054 #include "margins.h"
00055 #include "power.h"
00056 #include "reset.h"
00057 #include "rotation.h"
00058 #include "sd_capacity.h"
00059 #include "sensors.h"
00060 #include "settings.h"
00061 #include "settings_utils.h"
00062 #include "stylus.h"
00063 
00064 
00065 //----------------------------------------------------------------------------
00066 // Type Declarations
00067 //----------------------------------------------------------------------------
00068 
00069 //----------------------------------------------------------------------------
00070 // Global Constants
00071 //----------------------------------------------------------------------------
00072 
00073 
00074 //----------------------------------------------------------------------------
00075 // Static Variables
00076 //----------------------------------------------------------------------------
00077 
00078 static int           g_select_type     = 0;
00079 static GtkWidget*    g_main_window     = NULL;
00080 
00081 
00082 //============================================================================
00083 // Local Function Definitions
00084 //============================================================================
00085 static void       usage                     (const char* argv_0);
00086 static void       parse_arguments           (int argc, char** argv);
00087 static void       create_concrete_win       (void);
00088 static void       on_sigterm                (int signo);
00089 static gboolean   on_startup_complete       (gpointer data);
00090 
00091 
00092 //============================================================================
00093 // Functions Implementation
00094 //============================================================================
00095 
00096 // print usage text and quit
00097 void usage(const char* argv_0)
00098 {
00099     LOGPRINTF("entry");
00100 
00101     static const char *usage_text =
00102                         "\n"
00103                         "usage: %s [options]\n"
00104                         "\n"
00105                         "options:\n"
00106                         "    --advanced\n"
00107                         "        show the advanced settings\n"
00108                         "    --datetime\n"
00109                         "        show the date & time settings\n"
00110                         "    --devinfo\n"
00111                         "        show the device information infomation\n"
00112                         "    --fdatetime\n"
00113                         "        show the first-boot date & time settings\n"
00114                         "    --firstboot\n"
00115                         "        show the fist-boot language settings\n"
00116                         "    --flightmode\n"
00117                         "        show the flightmode settings\n"
00118                         "    --flipbar\n"
00119                         "        show the flipbar settings\n"
00120                         "    --help\n"
00121                         "        print help text and quit\n"
00122                         "    --language\n"
00123                         "        show the language settings\n"
00124                         "    --margins\n"
00125                         "        show the uds settings\n"
00126                         "    --myirex\n"
00127                         "        show the download myirex account settings\n"
00128                         "    --power\n"
00129                         "        show the power management settings\n"
00130                         "    --reset\n"
00131                         "        show the reset screen\n"
00132                         "    --rotation\n"
00133                         "        show the rotation settings\n"
00134                         "    --sdcard\n"
00135                         "        show the sd card settings\n"
00136                         "    --sensors\n"
00137                         "        show the sensor settings\n"
00138                         "    --stylus\n"
00139                         "        show the stylus calibration screen\n";
00140                            
00141     printf(usage_text, argv_0);
00142  
00143     exit(1);
00144 }
00145 
00146 // parse command-line options
00147 void parse_arguments(int argc, char **argv)
00148 {
00149     LOGPRINTF("entry");
00150 
00151     SettingType type = SETTING_NONE;
00152 
00153     // only a single argument allowed
00154     if (argc == 2)
00155     {
00156         if( strcmp(argv[1], "--advanced") == 0 )
00157         {
00158             type = SETTING_ADVANCED;
00159         }
00160         else if( strcmp(argv[1], "--datetime") == 0 )
00161         {
00162             type = SETTING_DATETIME;
00163         }
00164         else if( strcmp(argv[1], "--devinfo") == 0 )
00165         {
00166             type = SETTING_DEVINFO;
00167         }
00168         else if( strcmp(argv[1], "--fdatetime") == 0 )
00169         {
00170             type = SETTING_FDATETIME;
00171         }
00172         else if( strcmp(argv[1], "--firstboot") == 0 )
00173         {
00174             type = SETTING_FIRSTBOOT;
00175         }
00176         else if( strcmp(argv[1], "--flightmode") == 0 )
00177         {
00178             type = SETTING_FLIGHTMODE;
00179         }
00180         else if( strcmp(argv[1], "--flipbar") == 0 )
00181         {
00182             type = SETTING_FLIPBAR;
00183         }
00184         else if( strcmp(argv[1], "--language") == 0 )
00185         {
00186             type = SETTING_LANGUAGE;
00187         }
00188         else if( strcmp(argv[1], "--margins") == 0 )
00189         {
00190             type = SETTING_MARGINS;
00191         }
00192         else if( strcmp(argv [1],"--myirex") == 0)
00193         {
00194             type = SETTING_MYIREX;
00195         }        
00196         else if( strcmp(argv[1], "--power") == 0 )
00197         {
00198             type = SETTING_POWER;
00199         }
00200         else if( strcmp(argv[1], "--reset") == 0 )
00201         {
00202             type = SETTING_RESET;
00203         }
00204         else if( strcmp(argv[1], "--rotation") == 0 )
00205         {
00206             type = SETTING_ROTATION;
00207         }
00208         else if( strcmp(argv[1], "--sdcard") == 0 )
00209         {
00210             type = SETTING_SD;
00211         }
00212         else if( strcmp(argv[1], "--sensors") == 0 )
00213         {
00214             type = SETTING_SENSORS;
00215         }
00216         else if( strcmp(argv[1], "--stylus") == 0 )
00217         {
00218             type = SETTING_STYLUS;
00219         }
00220     }
00221 
00222     g_select_type = type;
00223     if ( type == SETTING_NONE )
00224     {
00225         usage(argv[0]);
00226     }
00227 }
00228 
00229 void create_concrete_win(void)
00230 {
00231     LOGPRINTF("entry");
00232 
00233     //load settings based on the g_select_type which assigned in function get_select_type;
00234     //and then create the concrete window
00235     switch( g_select_type )
00236     {
00237         case SETTING_MARGINS:
00238             load_margins_settings();
00239             create_margins_window(g_main_window);
00240             break;
00241         case SETTING_SD:
00242             create_sdcapacity_window(g_main_window);
00243             break;
00244         case SETTING_DEVINFO:
00245             load_device_info();
00246             create_device_info_window(g_main_window);
00247             break;
00248         case SETTING_MYIREX:
00249             load_account_settings();
00250             create_account_window(g_main_window);
00251             break;
00252         case SETTING_FLIPBAR:
00253             load_flipbar_settings();
00254             create_flipbar_window(g_main_window);
00255             break;
00256         case SETTING_STYLUS:
00257             load_stylus_settings();
00258             create_stylus_window(g_main_window);
00259             break;
00260         case SETTING_FLIGHTMODE:
00261             load_flightmode_settings();
00262             create_flightmode_window(g_main_window);
00263             break;
00264         case SETTING_RESET:
00265             load_reset_settings();
00266             create_reset_window(g_main_window);
00267             break;
00268         case SETTING_DATETIME:
00269             load_datetime_settings();
00270             create_datetime_window(g_main_window);
00271             break;
00272         case SETTING_FDATETIME:
00273             load_fdatetime_settings();
00274             create_fdatetime_window(g_main_window);
00275             break;
00276         case SETTING_LANGUAGE:
00277             load_language_settings();
00278             create_language_window(g_main_window, FALSE);
00279             break;
00280         case SETTING_FIRSTBOOT:
00281             load_language_settings();
00282             create_language_window(g_main_window, TRUE);
00283             break;
00284         case SETTING_POWER:
00285             load_power_settings();
00286             create_power_window(g_main_window);
00287             break;
00288         case SETTING_ROTATION:
00289             load_rotation_settings();
00290             create_rotation_window(g_main_window);
00291             break;
00292         case SETTING_SENSORS:
00293             load_sensor_settings();
00294             create_sensors_window(g_main_window);
00295             break;            
00296         case SETTING_ADVANCED:
00297             load_advanced_settings();
00298             create_advanced_window(g_main_window);
00299             break;            
00300         default:    
00301             break;
00302     }
00303 
00304     gtk_widget_show(g_main_window);
00305 }
00306 
00307 int main(int argc, char* argv[])
00308 {
00309     LOGPRINTF("entry");
00310 
00311     parse_arguments(argc, argv);
00312     
00313     struct sigaction on_term;
00314     // catch the SIGTERM signal
00315     memset(&on_term, 0x00, sizeof(on_term));
00316     on_term.sa_handler = on_sigterm;
00317     sigaction(SIGTERM, &on_term, NULL);
00318 #if LOGGING_ON
00319     sigaction(SIGINT,  &on_term, NULL);
00320 #endif
00321 
00322     // Initialize domain for translations
00323     textdomain(GETTEXT_PACKAGE);
00324     
00325     // Initialize GTK
00326     gtk_init(&argc, &argv);
00327 
00328     // Initialize GConf library
00329     gconf_initialize();
00330 
00331     // Initialize ipc
00332     if (ipc_set_services() == FALSE)
00333     {
00334         return -1;
00335     }
00336     gtk_rc_parse(DATADIR"/"PACKAGE_NAME".rc");
00337 
00338     g_main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00339     
00340     create_concrete_win();
00341 
00342     // Set repeat timer very slow to make one change per arrow tab on a spinbutton
00343     // NOTE: settings are globally attached to the GDK screen; use any widget to get settings    
00344     g_object_set(gtk_widget_get_settings(GTK_WIDGET(g_main_window)), "gtk-timeout-repeat", 1000, NULL); // 1 second
00345     g_object_set(gtk_widget_get_settings(GTK_WIDGET(g_main_window)), "gtk-timeout-initial", 1000, NULL); // 1 second
00346 
00347     // Setup popup menu
00348     menu_init();
00349 
00350     // Tell system daemon we are ready to go
00351     g_idle_add(on_startup_complete, NULL);
00352     
00353     LOGPRINTF("before gtk_main");
00354     gtk_main();
00355     LOGPRINTF("after gtk_main");
00356 
00357     return 0;
00358 }
00359 
00360 
00361 // terminate application
00362 void main_quit()
00363 {
00364     LOGPRINTF("entry");
00365     
00366     ipc_remove_menu(SETTINGS_MENU);
00367     ipc_menu_unblock();
00368 
00369     // Finalize ipc services.
00370     ipc_unset_services();
00371 
00372     // Clean up GConf library.
00373     gconf_finalize();
00374     
00375     if (g_main_window)
00376     {
00377         gtk_widget_destroy(g_main_window);
00378         g_main_window = NULL;
00379     }    
00380     
00381     if (gtk_main_level() > 0)
00382     {
00383         LOGPRINTF("quit main loop");
00384         gtk_main_quit();
00385     }
00386     else
00387     {
00388         WARNPRINTF("no main loop to quit, exit directly");
00389         _exit(0);
00390     }
00391 }
00392 
00393 
00394 //============================================================================
00395 // Local Functions Implementation
00396 //============================================================================
00397 
00398 static gboolean on_startup_complete (gpointer data)
00399 {
00400     LOGPRINTF("entry");
00401     g_assert(g_main_window);
00402 
00403     int xid = GDK_WINDOW_XID(g_main_window->window);
00404     ipc_sys_startup_complete(xid);
00405 
00406     return FALSE; // remove timer source
00407 }
00408 
00409 
00410 static void on_sigterm (int signo)
00411 {
00412     WARNPRINTF("    -- entry " PACKAGE_NAME ", my pid [%d]", getpid());
00413 
00414     // stop main process, prepare to quit application
00415     main_quit();
00416 
00417     WARNPRINTF("    -- leave " PACKAGE_NAME);
00418 }
Generated by  doxygen 1.6.2-20100208