start.c 5.5 KB

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