main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/display.h>
  17. #include <grass/glocale.h>
  18. #include "proto.h"
  19. int main(int argc, char *argv[])
  20. {
  21. struct GModule *module;
  22. struct Option *start_opt, *select_opt, *stop_opt, *output_opt,
  23. *width_opt, *height_opt, *bgcolor_opt, *res_opt;
  24. struct Flag *list_flag, *selected_flag, *select_flag, *release_flag,
  25. *cmd_flag, *truecolor_flag, *update_flag;
  26. int nopts, ret;
  27. const char *mon;
  28. G_gisinit(argv[0]);
  29. module = G_define_module();
  30. G_add_keyword(_("display"));
  31. G_add_keyword(_("graphics"));
  32. G_add_keyword(_("monitors"));
  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->label = _("Width for display monitor if not set by GRASS_WIDTH");
  55. width_opt->description = _("Default value: 640");
  56. width_opt->type = TYPE_INTEGER;
  57. width_opt->key_desc = "value";
  58. width_opt->guisection = _("Settings");
  59. height_opt = G_define_option();
  60. height_opt->key = "height";
  61. height_opt->label = _("Height for display monitor if not set by GRASS_HEIGHT");
  62. height_opt->description = _("Default value: 480");
  63. height_opt->type = TYPE_INTEGER;
  64. height_opt->key_desc = "value";
  65. height_opt->guisection = _("Settings");
  66. res_opt = G_define_option();
  67. res_opt->key = "resolution";
  68. res_opt->label = _("Dimensions of display monitor versus current size");
  69. res_opt->description = _("Example: resolution=2 enlarge display monitor twice to 1280x960");
  70. res_opt->type = TYPE_INTEGER;
  71. res_opt->key_desc = "value";
  72. res_opt->guisection = _("Settings");
  73. bgcolor_opt = G_define_standard_option(G_OPT_C_BG);
  74. bgcolor_opt->guisection = _("Settings");
  75. output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  76. output_opt->required = NO;
  77. output_opt->label = _("Name for output file (when starting new monitor)");
  78. output_opt->description = _("Ignored for 'wx' monitors");
  79. output_opt->guisection = _("Settings");
  80. list_flag = G_define_flag();
  81. list_flag->key = 'l';
  82. list_flag->description = _("List running monitors and exit");
  83. list_flag->guisection = _("Print");
  84. selected_flag = G_define_flag();
  85. selected_flag->key = 'p';
  86. selected_flag->description = _("Print name of currently selected monitor and exit");
  87. selected_flag->guisection = _("Print");
  88. cmd_flag = G_define_flag();
  89. cmd_flag->key = 'c';
  90. cmd_flag->description = _("Print commands for currently selected monitor and exit");
  91. cmd_flag->guisection = _("Print");
  92. select_flag = G_define_flag();
  93. select_flag->key = 's';
  94. select_flag->description = _("Do not automatically select when starting");
  95. select_flag->guisection = _("Manage");
  96. release_flag = G_define_flag();
  97. release_flag->key = 'r';
  98. release_flag->description = _("Release and stop currently selected monitor and exit");
  99. release_flag->guisection = _("Manage");
  100. truecolor_flag = G_define_flag();
  101. truecolor_flag->key = 't';
  102. truecolor_flag->description = _("Disable true colors");
  103. truecolor_flag->guisection = _("Settings");
  104. update_flag = G_define_flag();
  105. update_flag->key = 'u';
  106. update_flag->label = _("Open output file in update mode");
  107. update_flag->description = _("Requires --overwrite flag. If not given the output file is overwritten.");
  108. update_flag->guisection = _("Settings");
  109. if (G_parser(argc, argv))
  110. exit(EXIT_FAILURE);
  111. if (selected_flag->answer || release_flag->answer || cmd_flag->answer) {
  112. if (list_flag->answer)
  113. G_warning(_("Flag -%c ignored"), list_flag->key);
  114. mon = G__getenv("MONITOR");
  115. if (mon) {
  116. if (selected_flag->answer) {
  117. G_verbose_message(_("Currently selected monitor:"));
  118. fprintf(stdout, "%s\n", mon);
  119. }
  120. else if (cmd_flag->answer) {
  121. G_message(_("List of commands for monitor <%s>:"), mon);
  122. list_cmd(mon, stdout);
  123. }
  124. else if (mon) { /* release */
  125. G_unsetenv("MONITOR");
  126. G_verbose_message(_("Monitor <%s> released"), mon);
  127. ret = stop_mon(mon);
  128. }
  129. }
  130. else
  131. G_important_message(_("No monitor selected"));
  132. exit(EXIT_SUCCESS);
  133. }
  134. if (list_flag->answer) {
  135. print_list(stdout);
  136. exit(EXIT_SUCCESS);
  137. }
  138. nopts = 0;
  139. if (start_opt->answer)
  140. nopts++;
  141. if (stop_opt->answer)
  142. nopts++;
  143. if (select_opt->answer)
  144. nopts++;
  145. if (nopts != 1)
  146. G_fatal_error(_("Either <%s>, <%s> or <%s> must be given"),
  147. start_opt->key, stop_opt->key, select_opt->key);
  148. if (output_opt->answer &&
  149. (!start_opt->answer || strncmp(start_opt->answer, "wx", 2) == 0))
  150. G_warning(_("Option <%s> ignored"), output_opt->key);
  151. if (start_opt->answer) {
  152. int width, height;
  153. width = width_opt->answer ? atoi(width_opt->answer) : 640;
  154. height = height_opt->answer ? atoi(height_opt->answer) : 480;
  155. if (res_opt->answer) {
  156. int res;
  157. res = atoi(res_opt->answer);
  158. width *= res;
  159. height *= res;
  160. }
  161. ret = start_mon(start_opt->answer, output_opt->answer, !select_flag->answer,
  162. width, height, bgcolor_opt->answer,
  163. !truecolor_flag->answer);
  164. if (output_opt->answer && !update_flag->answer) {
  165. if (D_open_driver() != 0)
  166. G_fatal_error(_("No graphics device selected. "
  167. "Use d.mon to select graphics device."));
  168. D_setup_unity(0);
  169. D_erase(bgcolor_opt->answer);
  170. D_close_driver();
  171. }
  172. }
  173. if (stop_opt->answer)
  174. ret = stop_mon(stop_opt->answer);
  175. if (select_opt->answer)
  176. ret = select_mon(select_opt->answer);
  177. if (ret != 0)
  178. exit(EXIT_FAILURE);
  179. exit(EXIT_SUCCESS);
  180. }