main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. module->description = _("Controls graphics display monitors from the command line.");
  33. start_opt = G_define_option();
  34. start_opt->key = "start";
  35. start_opt->type = TYPE_STRING;
  36. start_opt->description = _("Name of monitor to start");
  37. start_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
  38. start_opt->guisection = _("Manage");
  39. stop_opt = G_define_option();
  40. stop_opt->key = "stop";
  41. stop_opt->type = TYPE_STRING;
  42. stop_opt->description = _("Name of monitor to stop");
  43. stop_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
  44. stop_opt->guisection = _("Manage");
  45. select_opt = G_define_option();
  46. select_opt->key = "select";
  47. select_opt->type = TYPE_STRING;
  48. select_opt->description = _("Name of monitor to select");
  49. select_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
  50. select_opt->guisection = _("Manage");
  51. width_opt = G_define_option();
  52. width_opt->key = "width";
  53. width_opt->description = _("Width for display monitor if not set by GRASS_WIDTH");
  54. width_opt->type = TYPE_INTEGER;
  55. width_opt->key_desc = "value";
  56. width_opt->guisection = _("Settings");
  57. height_opt = G_define_option();
  58. height_opt->key = "height";
  59. height_opt->description = _("Height for display monitor if not set by GRASS_HEIGHT");
  60. height_opt->type = TYPE_INTEGER;
  61. height_opt->key_desc = "value";
  62. height_opt->guisection = _("Settings");
  63. bgcolor_opt = G_define_standard_option(G_OPT_C_BG);
  64. bgcolor_opt->guisection = _("Settings");
  65. output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  66. output_opt->required = NO;
  67. output_opt->label = _("Name for output file (when starting new monitor)");
  68. output_opt->description = _("Ignored for 'wx' monitors");
  69. output_opt->guisection = _("Settings");
  70. list_flag = G_define_flag();
  71. list_flag->key = 'l';
  72. list_flag->description = _("List running monitors and exit");
  73. list_flag->guisection = _("Print");
  74. selected_flag = G_define_flag();
  75. selected_flag->key = 'p';
  76. selected_flag->description = _("Print name of currently selected monitor and exit");
  77. selected_flag->guisection = _("Print");
  78. cmd_flag = G_define_flag();
  79. cmd_flag->key = 'c';
  80. cmd_flag->description = _("Print commands for currently selected monitor and exit");
  81. cmd_flag->guisection = _("Print");
  82. select_flag = G_define_flag();
  83. select_flag->key = 's';
  84. select_flag->description = _("Do not automatically select when starting");
  85. select_flag->guisection = _("Manage");
  86. release_flag = G_define_flag();
  87. release_flag->key = 'r';
  88. release_flag->description = _("Release currently selected monitor and exit");
  89. release_flag->guisection = _("Manage");
  90. truecolor_flag = G_define_flag();
  91. truecolor_flag->key = 't';
  92. truecolor_flag->description = _("Disable true colors");
  93. truecolor_flag->guisection = _("Manage");
  94. if (G_parser(argc, argv))
  95. exit(EXIT_FAILURE);
  96. if (selected_flag->answer || release_flag->answer || cmd_flag->answer) {
  97. if (list_flag->answer)
  98. G_warning(_("Flag -%c ignored"), list_flag->key);
  99. mon = G__getenv("MONITOR");
  100. if (mon) {
  101. if (selected_flag->answer) {
  102. G_verbose_message(_("Currently selected monitor:"));
  103. fprintf(stdout, "%s\n", mon);
  104. }
  105. else if (cmd_flag->answer) {
  106. G_message(_("List of commands for monitor <%s>:"), mon);
  107. list_cmd(mon, stdout);
  108. }
  109. else if (mon) { /* release */
  110. G_unsetenv("MONITOR");
  111. G_verbose_message(_("Monitor <%s> released"), mon);
  112. }
  113. }
  114. else
  115. G_important_message(_("No monitor selected"));
  116. exit(EXIT_SUCCESS);
  117. }
  118. if (list_flag->answer) {
  119. print_list(stdout);
  120. exit(EXIT_SUCCESS);
  121. }
  122. nopts = 0;
  123. if (start_opt->answer)
  124. nopts++;
  125. if (stop_opt->answer)
  126. nopts++;
  127. if (select_opt->answer)
  128. nopts++;
  129. if (nopts != 1)
  130. G_fatal_error(_("Either <%s>, <%s> or <%s> must be given"),
  131. start_opt->key, stop_opt->key, select_opt->key);
  132. if (output_opt->answer &&
  133. (!start_opt->answer || strncmp(start_opt->answer, "wx", 2) == 0))
  134. G_warning(_("Option <%s> ignored"), output_opt->key);
  135. if (start_opt->answer)
  136. ret = start_mon(start_opt->answer, output_opt->answer, !select_flag->answer,
  137. width_opt->answer, height_opt->answer, bgcolor_opt->answer,
  138. !truecolor_flag->answer);
  139. if (stop_opt->answer)
  140. ret = stop_mon(stop_opt->answer);
  141. if (select_opt->answer)
  142. ret = select_mon(select_opt->answer);
  143. if (ret != 0)
  144. exit(EXIT_FAILURE);
  145. exit(EXIT_SUCCESS);
  146. }