00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00028 #include "config.h"
00029
00030 #include <unistd.h>
00031 #include <signal.h>
00032 #include <sys/types.h>
00033 #include <sys/wait.h>
00034 #include <string.h>
00035
00036 #include <gdk/gdkkeysyms.h>
00037 #include <gtk/gtk.h>
00038
00039 #include <liberipc/eripctoolbar.h>
00040 #include <liberdm/connectionMgrConstants.h>
00041 #include <liberdm/display.h>
00042
00043 #include "connectionMgrLog.h"
00044 #include "displayStatus.h"
00045 #include "background.h"
00046 #include "connectScreenData.h"
00047 #include "connectionMgr.h"
00048 #include "connectionMgrData.h"
00049 #include "connectScreen.h"
00050 #include "editScreen.h"
00051 #include "connectBackground.h"
00052 #include "connectScan.h"
00053 #include "pingThread.h"
00054 #include "scanThread.h"
00055 #include "languages.h"
00056 #include "erbusy.h"
00057 #include "pagebar.h"
00058 #include "system.h"
00059 #include "toolbar.h"
00060
00061
00062 static gboolean g_connect_after_reboot = FALSE;
00063 static gboolean g_stay_connected = FALSE;
00064 static gboolean g_edit_only = FALSE;
00065 static gboolean g_background = FALSE;
00066 static gboolean g_content_only = FALSE;
00067 static gchar* g_content_uuid = NULL;
00068 static gboolean g_use_last_connected = FALSE;
00069 static gboolean g_wifi_disabled = FALSE;
00070 static gboolean g_dialup_disabled = TRUE;
00071 static char* g_screen_title = NULL;
00072
00073
00074 static int g_exitValue = CONNECT_ERROR;
00075 static GtkWidget* g_main_window = NULL;
00076
00077
00078 static cmgrConnectType_t g_connect_type = cmgrConnectUnknown_e;
00079 static int exec_argc = 0;
00080 static char* exec_argv[10];
00081 static pid_t g_exec_pid = 0;
00082
00083
00084 static void parse_arguments(int argc, char **argv);
00085 static gboolean delayed_start_child_process(gpointer user_data);
00086
00087
00088 static void on_sigterm(int signo);
00089 static void on_sigchld(int signo);
00090 static void on_main_window_destroy(GtkWidget * widget, gpointer data);
00091 static gboolean on_main_window_expose_event(GtkWidget * widget,
00092 GdkEventExpose * event,
00093 gpointer p);
00094 static gboolean on_main_window_focus_out_event(GtkWidget * widget,
00095 GdkEventFocus * event,
00096 gpointer p);
00097
00098
00099
00100 static void parse_arguments(int argc, char **argv)
00101 {
00102 static const char *usage_text =
00103 "\n"
00104 "usage: %s [options]\n"
00105 "\n"
00106 "options:\n"
00107 " --help\n"
00108 " Print help text and quit\n"
00109 " --background\n"
00110 " Operate in background, no GUI\n"
00111 " --connect-ids\n"
00112 " Connect to iDS\n"
00113 " --connect-pc\n"
00114 " Connect to PC share\n"
00115 " --content-only\n"
00116 " Download content items, skip software upgrades\n"
00117 " --content-uuid <uuid>\n"
00118 " Together with --content-only, download only this content item\n"
00119 " --edit-only\n"
00120 " Edit profiles only, do connect automatically\n"
00121 " --use-last-connected\n"
00122 " Use last connected profile first\n"
00123 " --stay-connected\n"
00124 " Keep network enabled when exiting connection manager\n"
00125 " --connect-after-reboot\n"
00126 " Connect automatically after rebooting\n"
00127 " --title <text>\n"
00128 " <text> = screen title\n"
00129 " --execute\n"
00130 " When connected to IDS, execute rest of the arguments as a new command\n"
00131 "\n";
00132 int i;
00133
00134
00135 for (i = 0 ; i < argc ; i++)
00136 {
00137 if (strcmp(argv[i], "--help") == 0)
00138 {
00139 printf(usage_text, argv[0]);
00140 _exit(0);
00141 }
00142 else if(strcmp(argv[i], "--background") == 0)
00143 {
00144 g_background = TRUE;
00145 }
00146 else if(strcmp(argv[i], "--connect-ids") == 0)
00147 {
00148 g_connect_type = cmgrConnectIDS_e;
00149 }
00150 else if(strcmp(argv[i], "--connect-pc") == 0)
00151 {
00152 g_connect_type = cmgrConnectPCShare_e;
00153 }
00154 else if(strcmp(argv[i], "--content-only") == 0)
00155 {
00156 g_content_only = TRUE;
00157 }
00158 else if(strcmp(argv[i], "--content-uuid") == 0)
00159 {
00160 if (++i < argc)
00161 {
00162 g_content_uuid = argv[i];
00163 }
00164 else
00165 {
00166 i--;
00167 }
00168 }
00169 else if(strcmp(argv[i], "--edit-only") == 0)
00170 {
00171 g_edit_only = TRUE;
00172 }
00173 else if(strcmp(argv[i], "--use-last-connected") == 0)
00174 {
00175 g_use_last_connected = TRUE;
00176 }
00177 else if(strcmp(argv[i], "--stay-connected") == 0)
00178 {
00179 g_stay_connected = TRUE;
00180 }
00181 else if (strcmp(argv[i], "--connect-after-reboot") == 0)
00182 {
00183 g_connect_after_reboot = TRUE;
00184 }
00185 else if (strcmp(argv[i], "--title") == 0)
00186 {
00187 if (++i < argc)
00188 {
00189 g_screen_title = argv[i];
00190 }
00191 else
00192 {
00193 i--;
00194 }
00195 }
00196 else if (strcmp(argv[i], "--execute") == 0)
00197 {
00198 i++;
00199 if ( argc - i >= sizeof(exec_argv)/sizeof(exec_argv[0]) )
00200 {
00201 printf("\nToo many arguments for option [%s]\n", argv[i]);
00202 printf(usage_text, argv[0]);
00203 _exit(1);
00204 }
00205
00206 exec_argc = 0;
00207 while (i < argc)
00208 {
00209 exec_argv[exec_argc++] = argv[i++];
00210 }
00211 exec_argv[exec_argc] = NULL;
00212 }
00213 else if (strncmp(argv[i], "--", 2) == 0)
00214 {
00215 printf("\nInvalid option: %s\n", argv[i]);
00216 printf(usage_text, argv[0]);
00217 _exit(1);
00218 }
00219 }
00220 }
00221
00222
00223 static void load_registry(void)
00224 {
00225 gboolean b;
00226 regLoad_t regLoad;
00227
00228
00229 b = erRegRWLockInit();
00230 if (b == FALSE)
00231 {
00232 CN_ERRORPRINTF("erRegRWLockInit fails with return code [%d]", b);
00233 g_assert_not_reached();
00234 }
00235
00236
00237 b = erRegReadLock();
00238 if (b == FALSE)
00239 {
00240 CN_ERRORPRINTF("erRegReadLock fails with return code [%d]", b);
00241 g_assert_not_reached();
00242 }
00243
00244
00245 regLoad = erRegLoad(regBasis_t);
00246 if (regLoad == loadError_t)
00247 {
00248 CN_ERRORPRINTF("erRegLoad(regBasis_t) fails with return code [%d]",
00249 regLoad);
00250 g_assert_not_reached();
00251 }
00252 regLoad = erRegLoad(regNWProfiles_t);
00253 if (regLoad == loadError_t)
00254 {
00255 CN_ERRORPRINTF("erRegLoad(regNWProfiles_t) "
00256 "fails with return code [%d]",
00257 regLoad);
00258 g_assert_not_reached();
00259 }
00260
00261
00262 erRegUnlock();
00263 }
00264
00265
00266 static void release_registry(void)
00267 {
00268
00269 erRegUnload(regNWProfiles_t);
00270 erRegUnload(regBasis_t);
00271
00272
00273 erRegRWLockDestroy();
00274 }
00275
00276
00277 void prepare_registry_write( void)
00278 {
00279 gboolean b;
00280 regLoad_t regLoad;
00281
00282
00283 b = erRegWriteLock();
00284 if (b == FALSE)
00285 {
00286 CN_ERRORPRINTF("erRegWriteLock fails with return code [%d]", b);
00287 g_assert_not_reached();
00288 }
00289
00290
00291 erRegUnload(regNWProfiles_t);
00292 regLoad = erRegLoad(regNWProfiles_t);
00293 if (regLoad == loadError_t)
00294 {
00295 CN_ERRORPRINTF("erRegLoad(regNWProfiles_t) fails "
00296 "with return code [%d]",
00297 regLoad);
00298 g_assert_not_reached();
00299 }
00300 }
00301
00302
00303 void do_registry_write(void)
00304 {
00305 gboolean b;
00306
00307
00308 g_assert(lock_write == erRegGetLockState());
00309
00310
00311 b = erRegStore();
00312 if (b == FALSE)
00313 {
00314 CN_ERRORPRINTF("erRegStore fails with return code [%d]", b);
00315 }
00316
00317
00318 erRegUnlock();
00319 }
00320
00321 void get_wifi_and_dialup_status(void)
00322 {
00323 int status;
00324
00325 CN_LOGPRINTF("Checking status of WiFi...");
00326 status = system(COMMAND_STATUS_WIRELESS);
00327 if (WEXITSTATUS(status) == 0)
00328 {
00329 g_wifi_disabled = FALSE;
00330 }
00331 else
00332 {
00333 g_wifi_disabled = TRUE;
00334 }
00335 CN_LOGPRINTF("WiFi present? [%d]", g_wifi_disabled);
00336
00337 CN_LOGPRINTF("Checking status of Dialup...");
00338 status = system(COMMAND_STATUS_DIALUP);
00339 if (WEXITSTATUS(status) == 0)
00340 {
00341 g_dialup_disabled = FALSE;
00342 }
00343 else
00344 {
00345 g_dialup_disabled = TRUE;
00346 }
00347 CN_LOGPRINTF("Dialup present? [%d]", g_dialup_disabled);
00348 }
00349
00350 int main(int argc, char **argv)
00351 {
00352 GtkWidget* clientArea = NULL;
00353 GtkWidget* screens = NULL;
00354 GtkWidget* widget;
00355
00356 scanContext_t *ctxt;
00357
00358 parse_arguments(argc, argv);
00359
00360
00361 struct sigaction on_term;
00362 memset(&on_term, 0x00, sizeof(on_term));
00363 on_term.sa_handler = on_sigterm;
00364 sigaction(SIGTERM, &on_term, NULL);
00365
00366
00367 struct sigaction on_chld;
00368 memset(&on_chld, 0x00, sizeof(on_chld));
00369 on_chld.sa_handler = on_sigchld;
00370 on_chld.sa_flags = SA_NOCLDSTOP;
00371 sigaction(SIGCHLD, &on_chld, NULL);
00372
00373
00374 g_thread_init(NULL);
00375 gdk_threads_init();
00376
00377
00378 gtk_rc_parse(DATA_DIR "/connectionMgr.rc");
00379 CN_LOGPRINTF("rc file %s", DATA_DIR "/connectionMgr.rc");
00380
00381 gtk_init(&argc, &argv);
00382
00383
00384 get_wifi_and_dialup_status();
00385
00386 load_registry();
00387 cmgr_data_init();
00388 languagesInit();
00389
00390
00391
00392 if (g_connect_type != cmgrConnectUnknown_e)
00393 {
00394 CN_WARNPRINTF("g_connect_type [%d]", g_connect_type);
00395 connect_data_set_connect_type(g_connect_type);
00396 }
00397
00398
00399 cmgr_scan_ctxt_init();
00400 cmgr_ping_ctxt_init();
00401
00402 if (!g_background)
00403 {
00404 erbusy_init();
00405 pagebar_init();
00406 toolbar_init();
00407 cmgrInstallIpcServer();
00408
00409
00410 display_update_increase_level(MAIN_WINDOW_EXPOSE_LEVEL);
00411
00412
00413 widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00414 gtk_window_set_title(GTK_WINDOW(widget), PACKAGE " " VERSION);
00415 gtk_window_position(GTK_WINDOW(widget), GTK_WIN_POS_CENTER);
00416 gtk_container_set_border_width(GTK_CONTAINER(widget), 0);
00417 gtk_widget_set_size_request(GTK_WIDGET(widget),
00418 SCREEN_WIDTH, CLIENT_AREA);
00419 gtk_window_set_modal(GTK_WINDOW(widget), TRUE);
00420 gtk_window_set_resizable(GTK_WINDOW(widget), FALSE);
00421 g_signal_connect(G_OBJECT(widget), "destroy",
00422 G_CALLBACK(on_main_window_destroy), NULL);
00423 g_signal_connect_after(G_OBJECT(widget), "expose-event",
00424 G_CALLBACK(on_main_window_expose_event), NULL);
00425 g_signal_connect(G_OBJECT(widget), "focus-out-event",
00426 G_CALLBACK(on_main_window_focus_out_event), NULL);
00427 g_signal_connect(G_OBJECT(widget), "key-press-event",
00428 G_CALLBACK(on_cmgr_keypress), NULL);
00429 gtk_widget_show(widget);
00430 g_main_window = widget;
00431
00432
00433 clientArea = bg_create(g_main_window);
00434 screens = cmgr_screens_create();
00435 gtk_container_add(GTK_CONTAINER(clientArea), screens);
00436
00437
00438 if (main_get_edit_only())
00439 {
00440 cmgr_set_screen_mode(cmgrScreenEdit_e);
00441 edit_set_mode(editModeNormal_e);
00442 }
00443 else
00444 {
00445 cmgr_set_screen_mode(cmgrScreenConnect_e);
00446 }
00447 }
00448 else
00449 {
00450 connect_background_restore_scan_ctxt();
00451 ctxt = cmgr_get_scan_ctxt();
00452 connect_scan(ctxt, FALSE);
00453 }
00454
00455 gdk_threads_enter();
00456 gtk_main();
00457 gdk_threads_leave();
00458
00459 CN_WARNPRINTF("exitValue after gtk_main [%d]", g_exitValue);
00460
00461
00462 connect_data_store();
00463
00464
00465 pingThread_stop();
00466 pingThread_wait(30);
00467 scanThread_stop();
00468 scanThread_wait(30);
00469
00470 cmgr_scan_ctxt_destory();
00471 cmgr_ping_ctxt_destory();
00472
00473
00474 if (!g_stay_connected)
00475 {
00476 connect_disable_network();
00477 }
00478
00479 release_registry();
00480
00481 CN_WARNPRINTF("exit connectionMgr [%d]", g_exitValue);
00482 return g_exitValue;
00483 }
00484
00485 void main_quit(void)
00486 {
00487 CN_LOGPRINTF("entry");
00488
00489 if (g_exec_pid > 0)
00490 {
00491
00492 CN_WARNPRINTF("stop child process [%d]", g_exec_pid);
00493 kill(g_exec_pid, SIGTERM);
00494 }
00495 else if (gtk_main_level() > 0)
00496 {
00497
00498 CN_WARNPRINTF("stop GTK main");
00499 gtk_main_quit();
00500 }
00501 else
00502 {
00503
00504
00505 CN_WARNPRINTF("_exit(1)");
00506 _exit(1);
00507 }
00508 }
00509
00510
00511
00512
00513
00514 void main_start_child_process(void)
00515 {
00516 if (!g_background)
00517 {
00518
00519 display_update_increase_level(NO_DISPLAY_UPDATE_LEVEL);
00520
00521 toolbar_setIconState(iconID_keyboard, iconState_grey);
00522 }
00523
00524 g_idle_add(delayed_start_child_process, NULL);
00525 }
00526
00527 static gboolean delayed_start_child_process(gpointer user_data)
00528 {
00529 int i;
00530 pid_t pid;
00531
00532 CN_LOGPRINTF("entry");
00533
00534
00535 if (exec_argc == 0)
00536 {
00537
00538 if (connect_data_get_connect_type() == cmgrConnectIDS_e)
00539 {
00540 exec_argv[exec_argc++] = DOWNLOADMGR_EXECUTABLE;
00541 if (g_background)
00542 {
00543 exec_argv[exec_argc++] = "--background";
00544 }
00545 if (g_content_only)
00546 {
00547 exec_argv[exec_argc++] = "--content-only";
00548 }
00549 if (g_content_uuid)
00550 {
00551 exec_argv[exec_argc++] = "--content-uuid";
00552 exec_argv[exec_argc++] = g_content_uuid;
00553 }
00554 }
00555 else if (connect_data_get_connect_type() == cmgrConnectPCShare_e)
00556 {
00557 exec_argv[exec_argc++] = PCSHAREMGR_EXECUTABLE;
00558 if (g_background)
00559 {
00560 exec_argv[exec_argc++] = "--background";
00561 }
00562 }
00563 }
00564
00565
00566 if (exec_argc > 0)
00567 {
00568 exec_argv[exec_argc] = NULL;
00569 g_assert( exec_argc < (sizeof(exec_argv)/sizeof(exec_argv[0])) );
00570
00571 CN_WARNPRINTF("execute command [%s]", exec_argv[0]);
00572 for (i = 0 ; i < exec_argc ; i++)
00573 {
00574 CN_WARNPRINTF(" argv[%d] = [%s]", i, exec_argv[i]);
00575 }
00576
00577 pid = fork_exec_no_wait(exec_argc, exec_argv);
00578 if (pid > 0)
00579 {
00580 g_exec_pid = pid;
00581 }
00582 else
00583 {
00584 CN_ERRORPRINTF("Execute [%s] failed "
00585 "- fork_exec_no_wait returns [%d]",
00586 exec_argv[0], pid);
00587 g_exitValue = CONNECT_ERROR;
00588 }
00589 }
00590
00591 return FALSE;
00592 }
00593
00594
00595 gboolean main_get_background()
00596 {
00597 CN_LOGPRINTF("entry");
00598 return g_background;
00599 }
00600
00601 cmgrConnectType_t main_get_connect_type()
00602 {
00603 CN_LOGPRINTF("return [%d]", g_connect_type);
00604 return g_connect_type;
00605 }
00606
00607 gboolean main_get_edit_only()
00608 {
00609 CN_LOGPRINTF("return [%d]", g_edit_only);
00610 return g_edit_only;
00611 }
00612
00613 gboolean main_get_use_last_connected()
00614 {
00615 CN_LOGPRINTF("return [%d]", g_use_last_connected);
00616 return g_use_last_connected;
00617 }
00618
00619 gboolean main_get_connect_after_reboot(void)
00620 {
00621 CN_LOGPRINTF("return [%d]", g_connect_after_reboot);
00622 return g_connect_after_reboot;
00623 }
00624
00625 gboolean main_get_wifi_disabled(void)
00626 {
00627 CN_LOGPRINTF("return [%d]", g_wifi_disabled);
00628 return g_wifi_disabled;
00629 }
00630
00631 gboolean main_get_dialup_disabled(void)
00632 {
00633 CN_LOGPRINTF("return [%d]", g_dialup_disabled);
00634 return g_dialup_disabled;
00635 }
00636
00637 char* main_get_screen_title()
00638 {
00639 CN_LOGPRINTF("return [%s]", g_screen_title);
00640 return g_screen_title;
00641 }
00642
00643
00644
00645 static void on_sigterm(int signo)
00646 {
00647 CN_WARNPRINTF(" -- entry connectionMgr, my pid [%d]", getpid());
00648
00649
00650 gdk_threads_enter();
00651 main_quit();
00652 gdk_threads_leave();
00653
00654 show_keyboard(FALSE);
00655 g_stay_connected = FALSE;
00656
00657 CN_WARNPRINTF(" -- leave connectionMgr");
00658 }
00659
00660 static void on_sigchld(int signo)
00661 {
00662 CN_WARNPRINTF(" -- entry connectionMgr");
00663
00664
00665 pid_t pid;
00666 int status;
00667 while ( (pid = waitpid(-1, &status, WNOHANG)) > 0)
00668 {
00669 CN_WARNPRINTF("pid [%d]", pid);
00670 if (pid == g_exec_pid)
00671 {
00672 g_exec_pid = 0;
00673 if ( WIFEXITED(status) )
00674 {
00675 g_exitValue = WEXITSTATUS(status);
00676 }
00677 main_quit();
00678 }
00679 }
00680
00681 CN_WARNPRINTF(" -- leave connectionMgr");
00682 }
00683
00684 static void on_main_window_destroy(GtkWidget * widget, gpointer data)
00685 {
00686 CN_WARNPRINTF("entry");
00687
00688
00689 gtk_main_quit();
00690 }
00691
00692 static gboolean on_main_window_expose_event(GtkWidget * widget,
00693 GdkEventExpose * event,
00694 gpointer p)
00695 {
00696 CN_LOGPRINTF("entry");
00697 display_update_request_screen_refresh(MAIN_WINDOW_EXPOSE_LEVEL,
00698 WAVEFORM_FULLSCREEN);
00699
00700 if (!main_get_edit_only())
00701 {
00702 gtk_idle_add(delay_erbusy_blink, NULL);
00703 }
00704
00705 return FALSE;
00706 }
00707
00708 static gboolean on_main_window_focus_out_event(GtkWidget *widget,
00709 GdkEventFocus *event,
00710 gpointer p)
00711 {
00712 CN_LOGPRINTF("entry");
00713
00714 return FALSE;
00715 }
00716