[GRASS-SVN] r46994 - in grass/trunk/display: . d.mon

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 5 08:11:31 EDT 2011


Author: martinl
Date: 2011-07-05 05:11:31 -0700 (Tue, 05 Jul 2011)
New Revision: 46994

Added:
   grass/trunk/display/d.mon/
   grass/trunk/display/d.mon/Makefile
   grass/trunk/display/d.mon/check.c
   grass/trunk/display/d.mon/d.mon.html
   grass/trunk/display/d.mon/list.c
   grass/trunk/display/d.mon/main.c
   grass/trunk/display/d.mon/proto.h
   grass/trunk/display/d.mon/select.c
   grass/trunk/display/d.mon/start.c
   grass/trunk/display/d.mon/stop.c
Log:
New implementation of 'd.mon' added



Property changes on: grass/trunk/display/d.mon
___________________________________________________________________
Added: svn:ignore
   + OBJ.*


Added: grass/trunk/display/d.mon/Makefile
===================================================================
--- grass/trunk/display/d.mon/Makefile	                        (rev 0)
+++ grass/trunk/display/d.mon/Makefile	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../..
+
+PGM = d.mon
+
+LIBES = $(GISLIB)
+DEPENDENCIES= $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd


Property changes on: grass/trunk/display/d.mon/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native


Property changes on: grass/trunk/display/d.mon/check.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: grass/trunk/display/d.mon/d.mon.html
===================================================================
--- grass/trunk/display/d.mon/d.mon.html	                        (rev 0)
+++ grass/trunk/display/d.mon/d.mon.html	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,117 @@
+<h2>DESCRIPTION</h2>
+
+<em>d.mon</em> allows the user to start, select, list, release, and
+stop available graphics monitors.
+
+<h3>Start a monitor</h3> 
+
+In order to display on-screen GRASS graphics, the user must
+<b>start</b> and <b>select</b> a graphics monitor. By default,
+the <b>start</b> command actually runs two commands, to both start and
+select whatever monitor is named by the user. The user can get a list
+of running monitors by setting the <b>-l</b> flag on the command
+line. Note that some monitor drivers use environment
+<em><a href="variables.html">variables</a></em> or the specific
+<em><a href="displaydrivers.html">driver documentation</a></em>.
+
+<p>
+When a monitor is <em>started</em>, it is therefore also
+(automatically) <em>selected</em> for output, unless the
+<b>-s</b> flag is set by the user; the user can also
+explictly <b>select</b> a monitor that has been started.
+
+<p>
+The desired monitor should be started once and need not be restarted
+unless it is stopped for some reason. A monitor may continue to run
+for any length of time, even when no GRASS session is being run.
+
+<h3>Stop a monitor</h3> 
+
+A graphics monitor has two different types of status: monitor
+program <em>not running</em>, and monitor <em>running</em>. A monitor
+that has been started and/or selected will be listed as running; a
+monitor that has been stopped (or not started) will be listed as not
+running.  The <b>-l</b> flag will list all currently running monitors.
+
+<h3>Select a monitor</h3> 
+
+When the user <em>starts</em> a monitor, it is also
+(automatically) <em>selected</em> for graphics output unless the user
+sets the <b>-s</b> flag.  In order to use (direct graphics output to)
+a monitor, the user must <em>select</em> that monitor for use, either
+by simply starting the monitor without the <b>-s</b> flag or by
+explictly selecting the monitor for output. Only running monitors can
+be selected for graphics output.
+
+<p>
+The user can run multiple graphics monitors by simply starting each of
+the graphics monitors he wishes to direct output to.
+
+<h3>Release a monitor</h3>
+
+Currently <em>selected</em> a monitor can be released by <b>-r</b>
+flag.
+
+<h2>EXAMPLES</h2>
+
+To start interactive <em><a href="wxGUI.html">wxGUI</a></em> map
+display
+
+<div class="code"><pre>
+d.mon start=wx0
+</pre></div>
+
+All data will be rendered on <tt>wx0</tt> display. New monitor can be
+started (and selected ) by
+
+<div class="code"><pre>
+d.mon start=cairo output=out.pdf
+</pre></div>
+
+To list running monitors
+
+<div class="code"><pre>
+d.mon -l
+
+List of running monitors:
+wx0
+cairo
+</pre></div>
+
+To determine currently selected monitor
+
+<div class="code"><pre>
+d.mon -p
+
+cairo
+</pre></div>
+
+From this moment all data will be rendered into
+file <tt>output.pdf</tt>. To switch back to interactive wxGUI monitor
+
+
+<div class="code"><pre> d.mon select=wx0
+</pre></div>
+
+To close this monitor
+
+<div class="code"><pre>
+d.mon stop=wx0
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+  <a href="d.rast.html">d.rast</a>,
+  <a href="d.vect.html">d.vect</a>,
+</em>
+
+<p>
+  See also <em><a href="variables.html">variables</a></em> list
+
+<h2>AUTHOR</h2>
+
+Martin Landa, Czech Republic
+
+<p>
+<i>Last changed: $Date$</i>


Property changes on: grass/trunk/display/d.mon/d.mon.html
___________________________________________________________________
Added: svn:mime-type
   + text/html
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: grass/trunk/display/d.mon/list.c
===================================================================
--- grass/trunk/display/d.mon/list.c	                        (rev 0)
+++ grass/trunk/display/d.mon/list.c	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,68 @@
+#include <string.h>
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "proto.h"
+
+/* get list of running monitors */
+void list_mon(char ***list, int *n)
+{
+    int i;
+    const char *name;
+    const char *env_prefix = "MONITOR_";
+    int env_prefix_len;
+    char **tokens;
+    
+    env_prefix_len = strlen(env_prefix);
+    
+    *list = NULL;
+    *n    = 0;
+    tokens = NULL;
+    for (i = 0; (name = G__env_name(i)); i++) {
+	if (strncmp(env_prefix, name, env_prefix_len) == 0) {
+	    tokens = G_tokenize(name, "_");
+	    if (G_number_of_tokens(tokens) != 3 ||
+		strcmp(tokens[2], "ENVFILE") != 0)
+		continue;
+	    *list = G_realloc(*list, (*n + 1) * sizeof(char *));
+	    (*list)[*n] = G_store(tokens[1]);
+	    (*n)++;
+	    G_free_tokens(tokens);
+	    tokens = NULL;
+	}
+    }
+    
+}
+
+/* print list of running monitors */
+void print_list(FILE *fd)
+{
+    char **list;
+    int   i, n;
+
+    list_mon(&list, &n);
+    if (n > 0)
+	G_message(_("List of running monitors:"));
+    else {
+	G_important_message(_("No monitors running"));
+	return;
+    }
+    
+    for (i = 0; i < n; i++)
+	fprintf(fd, "%s\n", list[i]);
+}
+
+/* check if monitor is running */
+int check_mon(const char *name)
+{
+    char *env_name;
+    const char *str;
+    
+    env_name = NULL;
+    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", name);
+    str = G__getenv(env_name);
+    if (!str)
+	return FALSE;
+    
+    return TRUE;
+}


Property changes on: grass/trunk/display/d.mon/list.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: grass/trunk/display/d.mon/main.c
===================================================================
--- grass/trunk/display/d.mon/main.c	                        (rev 0)
+++ grass/trunk/display/d.mon/main.c	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,140 @@
+
+/****************************************************************************
+ *
+ * MODULE:       d.mon
+ * AUTHOR(S):    Martin Landa <landa.martin gmail.com>
+ * PURPOSE:      Controls map displays
+ * COPYRIGHT:    (C) 2011 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "proto.h"
+
+int main(int argc, char *argv[])
+{
+    struct GModule *module;
+    struct Option *start_opt, *select_opt, *stop_opt, *output_opt;
+    struct Flag *list_flag, *selected_flag, *select_flag, *release_flag;
+    
+    int nopts, ret;
+    const char *mon;
+    
+    G_gisinit(argv[0]);
+    
+    module = G_define_module();
+    G_add_keyword(_("display"));
+    G_add_keyword(_("graphics"));
+    G_add_keyword(_("monitors"));
+    module->description = _("Controls graphics monitors.");
+    
+    start_opt = G_define_option();
+    start_opt->key = "start";
+    start_opt->type = TYPE_STRING;
+    start_opt->description = _("Name of monitor to start");
+    start_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
+    start_opt->guisection = _("Manage");
+    
+    stop_opt = G_define_option();
+    stop_opt->key = "stop";
+    stop_opt->type = TYPE_STRING;
+    stop_opt->description = _("Name of monitor to stop");
+    stop_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
+    stop_opt->guisection = _("Manage");
+
+    select_opt = G_define_option();
+    select_opt->key = "select";
+    select_opt->type = TYPE_STRING;
+    select_opt->description = _("Name of monitor to select");
+    select_opt->options = "wx0,wx1,wx2,wx3,wx4,wx5,wx6,wx7,png,ps,html,cairo";
+    select_opt->guisection = _("Manage");
+
+    output_opt = G_define_standard_option(G_OPT_F_OUTPUT);
+    output_opt->required = NO;
+    output_opt->label = _("Name for output file (when starting new monitor)");
+    output_opt->description = _("Ignored for 'wx' monitors");
+
+    list_flag = G_define_flag();
+    list_flag->key = 'l';
+    list_flag->description = _("List running monitors and exit");
+    list_flag->guisection = _("Print");
+
+    selected_flag = G_define_flag();
+    selected_flag->key = 'p';
+    selected_flag->description = _("Print name of currently selected monitor and exit");
+    selected_flag->guisection = _("Print");
+
+    select_flag = G_define_flag();
+    select_flag->key = 's';
+    select_flag->description = _("Do not automatically select when starting");
+    select_flag->guisection = _("Manage");
+
+    release_flag = G_define_flag();
+    release_flag->key = 'r';
+    release_flag->description = _("Release currently selected monitor and exit");
+    release_flag->guisection = _("Manage");
+
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    
+    if (selected_flag->answer || release_flag->answer) {
+	if (list_flag->answer)
+	    G_warning(_("Flag -%c ignored"), list_flag->key);
+	mon = G__getenv("MONITOR");
+	if (selected_flag->answer) {
+	    if (mon)
+		fprintf(stdout, "%s\n", mon);
+	}
+	else {
+	    G_unsetenv("MONITOR");
+	}
+	if (!mon)
+	    G_important_message(_("No monitor selected"));
+	
+	exit(EXIT_SUCCESS);
+    }
+
+    if (list_flag->answer) {
+	print_list(stdout);
+	exit(EXIT_SUCCESS);
+    }
+	
+    nopts = 0;
+    if (start_opt->answer)
+	nopts++;
+    if (stop_opt->answer)
+	nopts++;
+    if (select_opt->answer)
+	nopts++;
+
+    if (nopts != 1)
+	G_fatal_error(_("Either <%s>, <%s> or <%s> must be given"),
+		      start_opt->key, stop_opt->key, select_opt->key);
+    
+    if (output_opt->answer &&
+	(!start_opt->answer || strncmp(start_opt->answer, "wx", 2) == 0))
+	G_warning(_("Option <%s> ignored"), output_opt->key);
+    
+    if (start_opt->answer)
+	ret = start_mon(start_opt->answer, output_opt->answer, !select_flag->answer);
+    
+    if (stop_opt->answer)
+	ret = stop_mon(stop_opt->answer);
+    
+    if (select_opt->answer)
+	ret = select_mon(select_opt->answer);
+    
+    if (ret != 0)
+	exit(EXIT_FAILURE);
+    
+    exit(EXIT_SUCCESS);
+}


Property changes on: grass/trunk/display/d.mon/main.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: grass/trunk/display/d.mon/proto.h
===================================================================
--- grass/trunk/display/d.mon/proto.h	                        (rev 0)
+++ grass/trunk/display/d.mon/proto.h	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,13 @@
+/* start */
+int start_mon(const char *, const char *, int);
+
+/* select.c */
+int select_mon(const char *);
+
+/* stop.c */
+int stop_mon(const char *);
+
+/* list.c */
+void list_mon();
+void print_list(FILE *);
+int check_mon(const char *);


Property changes on: grass/trunk/display/d.mon/proto.h
___________________________________________________________________
Added: svn:mime-type
   + text/x-chdr
Added: svn:eol-style
   + native

Added: grass/trunk/display/d.mon/select.c
===================================================================
--- grass/trunk/display/d.mon/select.c	                        (rev 0)
+++ grass/trunk/display/d.mon/select.c	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,33 @@
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "proto.h"
+
+/* select monitor */
+int select_mon(const char *name)
+{
+    const char *curr_mon;
+    char **list;
+    int   i, n, found;
+
+    curr_mon = G__getenv("MONITOR");
+    if (G_strcasecmp(name, curr_mon) == 0) {
+	G_warning(_("Monitor <%s> is already selected"), name);
+	return 0;
+    }
+
+    list_mon(&list, &n);
+    found = FALSE;
+    for (i = 0; i < n; i++) {
+	if (G_strcasecmp(list[i], name) == 0) {
+	    found = TRUE;
+	    break;
+	}
+    }
+    
+    if (found)
+	G_setenv("MONITOR", name);
+    else
+	G_fatal_error(_("Monitor <%s> is not running"), name);
+    
+    return 0;
+}


Property changes on: grass/trunk/display/d.mon/select.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: grass/trunk/display/d.mon/start.c
===================================================================
--- grass/trunk/display/d.mon/start.c	                        (rev 0)
+++ grass/trunk/display/d.mon/start.c	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,79 @@
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/spawn.h>
+#include <grass/glocale.h>
+
+#include "proto.h"
+
+static void start(const char *, const char *);
+static void start_wx(const char *);
+
+void start(const char *name, const char *output)
+{
+    char *tempfile;
+    char *env_name, *env_value;
+    
+    tempfile = G_tempfile();
+
+    env_name = NULL;
+    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", name);
+    G_asprintf(&env_value, "%s.env", tempfile);
+    G_setenv(env_name, env_value);
+    close(creat(env_value, 0666));
+
+    G_asprintf(&env_name, "MONITOR_%s_MAPFILE", name);
+    G_setenv(env_name, output);
+}
+
+void start_wx(const char *name)
+{
+    char progname[GPATH_MAX];
+    char *tempfile;
+    char *env_name, *map_value, *cmd_value, *env_value;
+   
+    tempfile = G_tempfile();
+
+    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", name);
+    G_asprintf(&env_value, "%s.env", tempfile);
+    G_setenv(env_name, env_value);
+    close(creat(env_value, 0666));
+    
+    G_asprintf(&env_name, "MONITOR_%s_CMDFILE", name);
+    G_asprintf(&cmd_value, "%s.cmd", tempfile);
+    G_setenv(env_name, cmd_value);
+    close(creat(cmd_value, 0666));
+
+    G_asprintf(&env_name, "MONITOR_%s_MAPFILE", name);
+    G_asprintf(&map_value, "%s.ppm", tempfile);
+    G_setenv(env_name, map_value);
+    /* close(creat(map_value, 0666)); */
+    
+    G_debug(1, "start: name=%s ", name);
+    G_debug(1, "       cmdfile = %s", cmd_value);
+    G_debug(1, "       mapfile = %s", map_value);
+    G_debug(1, "       envfile = %s", env_value);
+
+    sprintf(progname, "%s/etc/gui/wxpython/gui_modules/mapdisp.py", G_gisbase());
+    G_spawn_ex(getenv("GRASS_PYTHON"), progname, progname,
+	       name, map_value, cmd_value, env_value, SF_BACKGROUND, NULL);
+}
+
+int start_mon(const char *name, const char *output, int select)
+{
+    const char *curr_mon;
+
+    curr_mon = G__getenv("MONITOR");
+    if (curr_mon && strcmp(curr_mon, name) == 0 && check_mon(curr_mon))
+	G_fatal_error(_("Monitor <%s> already running"), name);
+    
+    if (select)
+	G_setenv("MONITOR", name);
+    
+    if (strncmp(name, "wx", 2) == 0) /* use G_strncasecmp() instead */
+	start_wx(name);
+    else
+	start(name, output);
+    
+    return 0;
+}


Property changes on: grass/trunk/display/d.mon/start.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Added: grass/trunk/display/d.mon/stop.c
===================================================================
--- grass/trunk/display/d.mon/stop.c	                        (rev 0)
+++ grass/trunk/display/d.mon/stop.c	2011-07-05 12:11:31 UTC (rev 46994)
@@ -0,0 +1,93 @@
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "proto.h"
+
+static void clean_env(const char *);
+static int stop_wx(const char *);
+static int stop(const char *);
+
+int stop_mon(const char *name)
+{
+    if (!check_mon(name)) {
+	clean_env(name);
+	G_fatal_error(_("Monitor <%s> is not running"), name);
+    }
+    
+    if (strncmp(name, "wx", 2) == 0)
+	return stop_wx(name);
+
+    return stop(name);
+}
+
+int stop(const char *name)
+{
+    char *env_name;
+    const char *env_file;
+    
+    env_name = NULL;
+    G_asprintf(&env_name, "MONITOR_%s_ENVFILE", name);
+    
+    env_file = G__getenv(env_name);
+    if (!env_file)
+	G_warning(_("Env file not found"));
+    
+    clean_env(name);
+
+    return 0;
+}
+
+int stop_wx(const char *name)
+{
+    char *env_name;
+    const char *pid;
+    
+    env_name = NULL;
+    G_asprintf(&env_name, "MONITOR_%s_PID", name);
+    
+    pid = G__getenv(env_name);
+    if (!pid) {
+	clean_env(name);
+	G_fatal_error(_("PID file not found"));
+    }
+    
+    if (kill((pid_t) atoi(pid), SIGTERM) != 0) {
+	/* G_fatal_error(_("Unable to stop monitor <%s>"), name); */
+    }
+    
+    clean_env(name);
+
+    return 0;
+}
+
+void clean_env(const char *name)
+{
+    int i;
+    const char *env_prefix = "MONITOR_";
+    const char *env;
+    int env_prefix_len;
+    char **tokens;
+    
+    env_prefix_len = strlen(env_prefix);
+    
+    tokens = NULL;
+    for (i = 0; (env = G__env_name(i)); i++) {
+	if (strncmp(env_prefix, env, env_prefix_len) != 0)
+	    continue;
+	
+	tokens = G_tokenize(env, "_");
+	if (G_number_of_tokens(tokens) != 3 ||
+	    strcmp(tokens[1], name) != 0)
+	    continue;
+	G_unsetenv(env);
+	i--; /* env has been removed for the list */
+	G_free_tokens(tokens);
+	tokens = NULL;
+    }
+
+    G_unsetenv("MONITOR");
+}


Property changes on: grass/trunk/display/d.mon/stop.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native



More information about the grass-commit mailing list