main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /****************************************************************************
  2. *
  3. * MODULE: g.gui
  4. *
  5. * AUTHOR(S): Martin Landa <landa.martin gmail.com>
  6. * Hamish Bowman <hamish_b yahoo com> (fine tuning)
  7. *
  8. * PURPOSE: Start GRASS GUI from command line.
  9. *
  10. * COPYRIGHT: (C) 2008-2015 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <grass/gis.h>
  21. #include <grass/glocale.h>
  22. #include <grass/spawn.h>
  23. int main(int argc, char *argv[])
  24. {
  25. struct Option *type, *rc_file;
  26. struct Flag *update_ui, *fglaunch, *nolaunch;
  27. struct GModule *module;
  28. const char *gui_type_env;
  29. char progname[GPATH_MAX];
  30. char *desc;
  31. G_gisinit(argv[0]);
  32. module = G_define_module();
  33. G_add_keyword(_("general"));
  34. G_add_keyword(_("GUI"));
  35. G_add_keyword(_("user interface"));
  36. module->label =
  37. _("Launches a GRASS graphical user interface (GUI) session.");
  38. module->description = _("Optionally updates default user interface settings.");
  39. type = G_define_option();
  40. type->key = "ui";
  41. type->type = TYPE_STRING;
  42. type->description = _("User interface");
  43. desc = NULL;
  44. G_asprintf(&desc,
  45. "wxpython;%s;text;%s;gtext;%s;",
  46. _("wxPython based GUI (wxGUI)"),
  47. _("command line interface only"),
  48. _("command line interface with GUI startup screen"));
  49. type->descriptions = desc;
  50. type->options = "wxpython,text,gtext";
  51. type->answer = "wxpython";
  52. type->guisection = _("Type");
  53. rc_file = G_define_standard_option(G_OPT_F_INPUT);
  54. rc_file->key = "workspace";
  55. rc_file->required = NO;
  56. rc_file->key_desc = "name.gxw";
  57. rc_file->label = _("Name of workspace file to load on start-up");
  58. rc_file->description = _("This is valid only for wxGUI (wxpython)");
  59. fglaunch = G_define_flag();
  60. fglaunch->key = 'f';
  61. fglaunch->label = _("Start GUI in the foreground");
  62. fglaunch->description = _("By default the GUI starts in the background"
  63. " and control is immediately returned to the caller."
  64. " When GUI runs in foregreound, it blocks the command line");
  65. update_ui = G_define_flag();
  66. update_ui->key = 'd';
  67. update_ui->description = _("Update default user interface settings");
  68. update_ui->guisection = _("Default");
  69. nolaunch = G_define_flag();
  70. nolaunch->key = 'n';
  71. nolaunch->description =
  72. _("Do not launch GUI after updating the default user interface settings");
  73. nolaunch->guisection = _("Default");
  74. if (G_parser(argc, argv))
  75. exit(EXIT_FAILURE);
  76. gui_type_env = G_getenv_nofatal("GUI");
  77. G_debug(1, "GUI: %s", gui_type_env ? gui_type_env : "unset");
  78. if (update_ui->answer) {
  79. if (!gui_type_env || strcmp(type->answer, gui_type_env)) {
  80. G_setenv("GUI", type->answer);
  81. G_message(_("<%s> is now the default GUI"), type->answer);
  82. }
  83. }
  84. if(strcmp(type->answer, "wxpython") != 0 || nolaunch->answer) {
  85. if (!update_ui->answer)
  86. G_warning(_("Nothing to do. For setting up <%s> as default UI use -%c flag."),
  87. type->answer, update_ui->key);
  88. exit(EXIT_SUCCESS);
  89. }
  90. sprintf(progname, "%s/gui/wxpython/wxgui.py", G_gisbase());
  91. if (access(progname, F_OK) == -1)
  92. G_fatal_error(_("Your installation doesn't include GUI, exiting."));
  93. if (fglaunch->answer) {
  94. G_message(_("Launching <%s> GUI, please wait..."), type->answer);
  95. if (rc_file->answer) {
  96. G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
  97. "--workspace", rc_file->answer, NULL);
  98. }
  99. else {
  100. G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
  101. NULL);
  102. }
  103. }
  104. else {
  105. G_message(_("Launching <%s> GUI in the background, please wait..."), type->answer);
  106. if (rc_file->answer) {
  107. G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
  108. "--workspace", rc_file->answer, SF_BACKGROUND, NULL);
  109. }
  110. else {
  111. G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
  112. SF_BACKGROUND, NULL);
  113. }
  114. /* stop the impatient from starting it again
  115. before the splash screen comes up */
  116. G_sleep(3);
  117. }
  118. exit(EXIT_SUCCESS);
  119. }