main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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-2015 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, *x_flag, *sfile_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_RENDER_WIDTH");
  55. width_opt->description = _("Default value: 720");
  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_RENDER_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_CN);
  74. bgcolor_opt->key = "bgcolor";
  75. bgcolor_opt->label = _("Background color");
  76. bgcolor_opt->answer = DEFAULT_BG_COLOR;
  77. bgcolor_opt->guisection = _("Settings");
  78. output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  79. output_opt->required = NO;
  80. output_opt->label = _("Name for output file (when starting new monitor)");
  81. output_opt->description = _("Ignored for 'wx' monitors");
  82. output_opt->guisection = _("Settings");
  83. list_flag = G_define_flag();
  84. list_flag->key = 'l';
  85. list_flag->description = _("List running monitors and exit");
  86. list_flag->guisection = _("Print");
  87. selected_flag = G_define_flag();
  88. selected_flag->key = 'p';
  89. selected_flag->description = _("Print name of currently selected monitor and exit");
  90. selected_flag->guisection = _("Print");
  91. cmd_flag = G_define_flag();
  92. cmd_flag->key = 'c';
  93. cmd_flag->description = _("Print commands for currently selected monitor and exit");
  94. cmd_flag->guisection = _("Print");
  95. sfile_flag = G_define_flag();
  96. sfile_flag->key = 'g';
  97. sfile_flag->description =
  98. _("Print path to support files of currently selected monitor and exit");
  99. select_flag = G_define_flag();
  100. select_flag->key = 's';
  101. select_flag->description = _("Do not automatically select when starting");
  102. select_flag->guisection = _("Manage");
  103. release_flag = G_define_flag();
  104. release_flag->key = 'r';
  105. release_flag->description = _("Release and stop currently selected monitor and exit");
  106. release_flag->guisection = _("Manage");
  107. truecolor_flag = G_define_flag();
  108. truecolor_flag->key = 't';
  109. truecolor_flag->description = _("Disable true colors");
  110. truecolor_flag->guisection = _("Settings");
  111. update_flag = G_define_flag();
  112. update_flag->key = 'u';
  113. update_flag->label = _("Open output file in update mode");
  114. update_flag->description = _("Requires --overwrite flag");
  115. update_flag->guisection = _("Settings");
  116. x_flag = G_define_flag();
  117. x_flag->key = 'x';
  118. x_flag->label = _("Launch light-weight wx monitor without toolbars and statusbar");
  119. x_flag->description = _("Requires 'start=wx0-7'");
  120. x_flag->guisection = _("Settings");
  121. if (G_parser(argc, argv))
  122. exit(EXIT_FAILURE);
  123. if (x_flag->answer && start_opt->answer && strncmp(start_opt->answer, "wx", 2) != 0)
  124. G_warning(_("Flag -%c has effect only for wx monitors (%s=wx0-7)"),
  125. x_flag->key, start_opt->key);
  126. if (selected_flag->answer || release_flag->answer ||
  127. cmd_flag->answer || sfile_flag->answer) {
  128. if (list_flag->answer)
  129. G_warning(_("Flag -%c ignored"), list_flag->key);
  130. mon = G_getenv_nofatal("MONITOR");
  131. if (mon) {
  132. if (selected_flag->answer) {
  133. G_verbose_message(_("Currently selected monitor:"));
  134. fprintf(stdout, "%s\n", mon);
  135. }
  136. else if (cmd_flag->answer) {
  137. G_message(_("List of commands for monitor <%s>:"), mon);
  138. list_cmd(mon, stdout);
  139. }
  140. else if (sfile_flag->answer) {
  141. list_files(mon, stdout);
  142. }
  143. else if (mon) { /* release */
  144. G_unsetenv("MONITOR");
  145. G_verbose_message(_("Monitor <%s> released"), mon);
  146. ret = stop_mon(mon);
  147. }
  148. }
  149. else
  150. G_important_message(_("No monitor selected"));
  151. exit(EXIT_SUCCESS);
  152. }
  153. if (list_flag->answer) {
  154. print_list(stdout);
  155. exit(EXIT_SUCCESS);
  156. }
  157. nopts = 0;
  158. if (start_opt->answer)
  159. nopts++;
  160. if (stop_opt->answer)
  161. nopts++;
  162. if (select_opt->answer)
  163. nopts++;
  164. if (nopts != 1)
  165. G_fatal_error(_("Either <%s>, <%s> or <%s> must be given"),
  166. start_opt->key, stop_opt->key, select_opt->key);
  167. if (output_opt->answer &&
  168. (!start_opt->answer || strncmp(start_opt->answer, "wx", 2) == 0))
  169. G_warning(_("Option <%s> ignored"), output_opt->key);
  170. if (start_opt->answer) {
  171. int width, height;
  172. width = width_opt->answer ? atoi(width_opt->answer) : 0;
  173. height = height_opt->answer ? atoi(height_opt->answer) : 0;
  174. if (width < 1) {
  175. char *env_width = getenv("GRASS_RENDER_WIDTH");
  176. if (env_width)
  177. width = atoi(env_width);
  178. }
  179. if (height < 1) {
  180. char *env_height = getenv("GRASS_RENDER_HEIGHT");
  181. if (env_height)
  182. height = atoi(env_height);
  183. }
  184. if (width < 1)
  185. width = DEFAULT_WIDTH;
  186. if (height < 1)
  187. height = DEFAULT_HEIGHT;
  188. if (res_opt->answer) {
  189. int res;
  190. res = atoi(res_opt->answer);
  191. width *= res;
  192. height *= res;
  193. }
  194. G_debug(1, "Monitor width/height = %d/%d", width, height);
  195. ret = start_mon(start_opt->answer, output_opt->answer, !select_flag->answer,
  196. width, height, bgcolor_opt->answer,
  197. !truecolor_flag->answer, x_flag->answer, update_flag->answer);
  198. if (output_opt->answer && !update_flag->answer) {
  199. D_open_driver();
  200. D_setup_unity(0);
  201. D_erase(bgcolor_opt->answer);
  202. D_close_driver();
  203. }
  204. }
  205. if (stop_opt->answer)
  206. ret = stop_mon(stop_opt->answer);
  207. if (select_opt->answer)
  208. ret = select_mon(select_opt->answer);
  209. if (ret != 0)
  210. exit(EXIT_FAILURE);
  211. exit(EXIT_SUCCESS);
  212. }