start.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/spawn.h>
  5. #include <grass/display.h>
  6. #include <grass/glocale.h>
  7. #include "proto.h"
  8. static void start(const char *, const char *);
  9. static void start_wx(const char *, const char *, const char *,
  10. const char *, int, int);
  11. static void error_handler(void *);
  12. /* start file-based monitor */
  13. void start(const char *name, const char *output)
  14. {
  15. char *env_name, output_path[GPATH_MAX];
  16. const char *output_name;
  17. /* stop monitor on failure */
  18. G_add_error_handler(error_handler, (char *)name);
  19. if (!output) {
  20. D_open_driver();
  21. output_name = D_get_file();
  22. if (!output_name)
  23. return;
  24. if (access(output_name, F_OK) == 0) {
  25. if (G_get_overwrite()) {
  26. G_warning(_("File '%s' already exists and will be overwritten"), output_name);
  27. }
  28. else {
  29. D_close_driver();
  30. G_fatal_error(_("option <%s>: <%s> exists."),
  31. "output", output_name);
  32. }
  33. }
  34. D_close_driver(); /* must be called after check because this
  35. * function produces default map file */
  36. }
  37. else {
  38. output_name = output;
  39. }
  40. if (!strchr(output_name, HOST_DIRSEP)) { /* relative path */
  41. char *ptr;
  42. if (!getcwd(output_path, GPATH_MAX))
  43. G_fatal_error(_("Unable to get current working directory"));
  44. ptr = output_path + strlen(output_path) - 1;
  45. if (*(ptr++) != HOST_DIRSEP) {
  46. *(ptr++) = HOST_DIRSEP;
  47. *(ptr) = '\0';
  48. }
  49. strcat(output_path, output_name);
  50. G_message(_("Output file: %s"), output_path);
  51. }
  52. else {
  53. strcpy(output_path, output_name); /* already full path */
  54. }
  55. env_name = NULL;
  56. G_asprintf(&env_name, "MONITOR_%s_MAPFILE", G_store_upper(name));
  57. G_setenv(env_name, output_path);
  58. }
  59. /* start wxGUI display monitor */
  60. void start_wx(const char *name, const char *tempfile,
  61. const char *env_value, const char *cmd_value,
  62. int width, int height)
  63. {
  64. char progname[GPATH_MAX];
  65. char *env_name, *map_value, str_width[1024], str_height[1024];
  66. env_name = NULL;
  67. G_asprintf(&env_name, "MONITOR_%s_MAPFILE", G_store_upper(name));
  68. G_asprintf(&map_value, "%s.ppm", tempfile);
  69. G_setenv(env_name, map_value);
  70. /* close(creat(map_value, 0666)); */
  71. G_debug(3, " mapfile = %s", map_value);
  72. sprintf(progname, "%s/gui/wxpython/mapdisp/main.py", G_gisbase());
  73. if (width > 0)
  74. sprintf(str_width, "%d", width);
  75. else
  76. str_width[0] = '\0';
  77. if (height > 0)
  78. sprintf(str_height, "%d", height);
  79. else
  80. str_height[0] = '\0';
  81. G_spawn_ex(getenv("GRASS_PYTHON"), progname, progname,
  82. name, map_value, cmd_value, env_value,
  83. str_width, str_height, SF_BACKGROUND, NULL);
  84. }
  85. int start_mon(const char *name, const char *output, int select,
  86. int width, int height, const char *bgcolor,
  87. int truecolor)
  88. {
  89. char *u_name;
  90. char *env_name, *env_value, *cmd_value;
  91. char *tempfile, buf[1024];
  92. int env_fd;
  93. if (check_mon(name)) {
  94. const char *curr_mon;
  95. curr_mon = G__getenv("MONITOR");
  96. if (select && (!curr_mon || strcmp(curr_mon, name) != 0))
  97. G_setenv("MONITOR", name);
  98. G_fatal_error(_("Monitor <%s> already running"), name);
  99. }
  100. tempfile = G_tempfile();
  101. u_name = G_store_upper(name);
  102. env_name = env_value = NULL;
  103. G_asprintf(&env_name, "MONITOR_%s_ENVFILE", u_name);
  104. G_asprintf(&env_value, "%s.env", tempfile);
  105. G_setenv(env_name, env_value);
  106. env_fd = creat(env_value, 0666);
  107. if (env_fd < 0)
  108. G_fatal_error(_("Unable to create file '%s'"), env_value);
  109. sprintf(buf, "GRASS_RENDER_FILE_READ=TRUE\n");
  110. write(env_fd, buf, strlen(buf));
  111. if (width) {
  112. sprintf(buf, "GRASS_RENDER_WIDTH=%d\n", width);
  113. write(env_fd, buf, strlen(buf));
  114. }
  115. if (height) {
  116. sprintf(buf, "GRASS_RENDER_HEIGHT=%d\n", height);
  117. write(env_fd, buf, strlen(buf));
  118. }
  119. if (bgcolor) {
  120. if (strcmp(bgcolor, "none") == 0)
  121. sprintf(buf, "GRASS_RENDER_TRANSPARENT=TRUE\n");
  122. else
  123. sprintf(buf, "GRASS_RENDER_BACKGROUNDCOLOR=%s\n", bgcolor);
  124. write(env_fd, buf, strlen(buf));
  125. }
  126. if (truecolor) {
  127. sprintf(buf, "GRASS_RENDER_TRUECOLOR=TRUE\n");
  128. write(env_fd, buf, strlen(buf));
  129. }
  130. close(env_fd);
  131. cmd_value = NULL;
  132. G_asprintf(&env_name, "MONITOR_%s_CMDFILE", u_name);
  133. G_asprintf(&cmd_value, "%s.cmd", tempfile);
  134. G_setenv(env_name, cmd_value);
  135. close(creat(cmd_value, 0666));
  136. G_verbose_message(_("Starting monitor <%s> with env file '%s'"), name, env_value);
  137. if (G_verbose() > G_verbose_std()) {
  138. FILE *fd;
  139. fd = fopen(env_value, "r");
  140. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  141. fprintf(stderr, " %s\n", buf);
  142. }
  143. fclose(fd);
  144. }
  145. G_debug(1, "start: name=%s ", name);
  146. G_debug(3, " envfile = %s", env_value);
  147. G_debug(3, " cmdfile = %s", cmd_value);
  148. if (select)
  149. G_setenv("MONITOR", name);
  150. if (strncmp(name, "wx", 2) == 0)
  151. start_wx(name, tempfile, env_value, cmd_value,
  152. width, height);
  153. else
  154. start(name, output);
  155. return 0;
  156. }
  157. void error_handler(void *p)
  158. {
  159. const char *name = (const char *) p;
  160. stop_mon(name);
  161. }