main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /****************************************************************************
  2. *
  3. * MODULE: d.mon
  4. * AUTHOR(S): Martin Landa <landa.martin gmail.com>
  5. * PURPOSE: Controls graphics monitors for CLI
  6. * COPYRIGHT: (C) 2011-2012 by Martin Landa, and the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General
  9. * Public License (>=v2). Read the file COPYING that
  10. * comes with GRASS for details.
  11. *
  12. *****************************************************************************/
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #include "proto.h"
  18. int main(int argc, char *argv[])
  19. {
  20. struct GModule *module;
  21. struct Option *start_opt, *select_opt, *stop_opt, *output_opt,
  22. *width_opt, *height_opt, *bgcolor_opt;
  23. struct Flag *list_flag, *selected_flag, *select_flag, *release_flag,
  24. *cmd_flag, *truecolor_flag;
  25. int nopts, ret;
  26. const char *mon;
  27. G_gisinit(argv[0]);
  28. module = G_define_module();
  29. G_add_keyword(_("display"));
  30. G_add_keyword(_("graphics"));
  31. G_add_keyword(_("monitors"));
  32. G_add_keyword(_("CLI"));
  33. module->description = _("Controls graphics display monitors from the command line.");
  34. start_opt = G_define_option();
  35. start_opt->key = "start";
  36. start_opt->type = TYPE_STRING;
  37. start_opt->description = _("Name of monitor to start");
  38. start_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
  39. start_opt->guisection = _("Manage");
  40. stop_opt = G_define_option();
  41. stop_opt->key = "stop";
  42. stop_opt->type = TYPE_STRING;
  43. stop_opt->description = _("Name of monitor to stop");
  44. stop_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
  45. stop_opt->guisection = _("Manage");
  46. select_opt = G_define_option();
  47. select_opt->key = "select";
  48. select_opt->type = TYPE_STRING;
  49. select_opt->description = _("Name of monitor to select");
  50. select_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
  51. select_opt->guisection = _("Manage");
  52. width_opt = G_define_option();
  53. width_opt->key = "width";
  54. width_opt->description = _("Width for display monitor if not set by GRASS_WIDTH");
  55. width_opt->type = TYPE_INTEGER;
  56. width_opt->key_desc = "value";
  57. width_opt->guisection = _("Settings");
  58. height_opt = G_define_option();
  59. height_opt->key = "height";
  60. height_opt->description = _("Height for display monitor if not set by GRASS_HEIGHT");
  61. height_opt->type = TYPE_INTEGER;
  62. height_opt->key_desc = "value";
  63. height_opt->guisection = _("Settings");
  64. bgcolor_opt = G_define_standard_option(G_OPT_C_BG);
  65. bgcolor_opt->guisection = _("Settings");
  66. output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  67. output_opt->required = NO;
  68. output_opt->label = _("Name for output file (when starting new monitor)");
  69. output_opt->description = _("Ignored for 'wx' monitors");
  70. output_opt->guisection = _("Settings");
  71. list_flag = G_define_flag();
  72. list_flag->key = 'l';
  73. list_flag->description = _("List running monitors and exit");
  74. list_flag->guisection = _("Print");
  75. selected_flag = G_define_flag();
  76. selected_flag->key = 'p';
  77. selected_flag->description = _("Print name of currently selected monitor and exit");
  78. selected_flag->guisection = _("Print");
  79. cmd_flag = G_define_flag();
  80. cmd_flag->key = 'c';
  81. cmd_flag->description = _("Print commands for currently selected monitor and exit");
  82. cmd_flag->guisection = _("Print");
  83. select_flag = G_define_flag();
  84. select_flag->key = 's';
  85. select_flag->description = _("Do not automatically select when starting");
  86. select_flag->guisection = _("Manage");
  87. release_flag = G_define_flag();
  88. release_flag->key = 'r';
  89. release_flag->description = _("Release currently selected monitor and exit");
  90. release_flag->guisection = _("Manage");
  91. truecolor_flag = G_define_flag();
  92. truecolor_flag->key = 't';
  93. truecolor_flag->description = _("Disable true colors");
  94. truecolor_flag->guisection = _("Manage");
  95. if (G_parser(argc, argv))
  96. exit(EXIT_FAILURE);
  97. if (selected_flag->answer || release_flag->answer || cmd_flag->answer) {
  98. if (list_flag->answer)
  99. G_warning(_("Flag -%c ignored"), list_flag->key);
  100. mon = G__getenv("MONITOR");
  101. if (mon) {
  102. if (selected_flag->answer) {
  103. G_verbose_message(_("Currently selected monitor:"));
  104. fprintf(stdout, "%s\n", mon);
  105. }
  106. else if (cmd_flag->answer) {
  107. G_message(_("List of commands for monitor <%s>:"), mon);
  108. list_cmd(mon, stdout);
  109. }
  110. else if (mon) { /* release */
  111. G_unsetenv("MONITOR");
  112. G_verbose_message(_("Monitor <%s> released"), mon);
  113. }
  114. }
  115. else
  116. G_important_message(_("No monitor selected"));
  117. exit(EXIT_SUCCESS);
  118. }
  119. if (list_flag->answer) {
  120. print_list(stdout);
  121. exit(EXIT_SUCCESS);
  122. }
  123. nopts = 0;
  124. if (start_opt->answer)
  125. nopts++;
  126. if (stop_opt->answer)
  127. nopts++;
  128. if (select_opt->answer)
  129. nopts++;
  130. if (nopts != 1)
  131. G_fatal_error(_("Either <%s>, <%s> or <%s> must be given"),
  132. start_opt->key, stop_opt->key, select_opt->key);
  133. if (output_opt->answer &&
  134. (!start_opt->answer || strncmp(start_opt->answer, "wx", 2) == 0))
  135. G_warning(_("Option <%s> ignored"), output_opt->key);
  136. if (start_opt->answer)
  137. ret = start_mon(start_opt->answer, output_opt->answer, !select_flag->answer,
  138. width_opt->answer, height_opt->answer, bgcolor_opt->answer,
  139. !truecolor_flag->answer);
  140. if (stop_opt->answer)
  141. ret = stop_mon(stop_opt->answer);
  142. if (select_opt->answer)
  143. ret = select_mon(select_opt->answer);
  144. if (ret != 0)
  145. exit(EXIT_FAILURE);
  146. exit(EXIT_SUCCESS);
  147. }