[GRASS-dev] [grass-code P][515] Close any open gis.m windows on GRASS exit

grass-dev@grass.itc.it wrote:

code P item #515, was opened at 2007-10-19 15:02
Status: Open
Priority: 3
Submitted By: M�ris Nartišs (marisn)
Assigned to: Nobody (None)
Summary: Close any open gis.m windows on GRASS exit
Patch status: None
Patch type: enhancement
GRASS component: gis.m
GRASS version: CVS HEAD
GRASS CVS checkout date, if applies (YYMMDD):

Here's the patch:

Index: lib/init/init.sh

RCS file: /home/grass/grassrepository/grass6/lib/init/init.sh,v
retrieving revision 1.120
diff -u -u -r1.120 init.sh
--- lib/init/init.sh 6 Oct 2007 05:04:58 -0000 1.120
+++ lib/init/init.sh 19 Oct 2007 12:01:27 -0000
@@ -947,6 +947,16 @@
     d.mon stop=$MON
done

+
+if [ $TCLTKGRASSBASE ] ; then
+ # [ $TCLTKGRASSBASE && !$MINGW && !$CGWIN ] - send does NOT work on windows.
+ echo "Closing open gis.m sessions....."
+ echo 'foreach gwin [lsearch -all -inline [winfo interps] gm_tcl_$env(GIS_LOCK)*] {
+ catch {send -async $gwin exit}
+ }
+ exit' | $GRASS_WISH >/dev/null 2>&1
+fi
+
echo "Cleaning up temporary files....."

"$ETC/clean_temp" > /dev/null
Index: gui/tcltk/gis.m/gis.m

RCS file: /home/grass/grassrepository/grass6/gui/tcltk/gis.m/gis.m,v
retrieving revision 1.10
diff -u -u -r1.10 gis.m
--- gui/tcltk/gis.m/gis.m 9 May 2007 23:20:50 -0000 1.10
+++ gui/tcltk/gis.m/gis.m 19 Oct 2007 12:01:28 -0000
@@ -12,9 +12,9 @@

if [ $# -eq 0 ] ; then
# if [ "$HOSTTYPE" = "macintosh" -o "$HOSTTYPE" = "powermac" -o "$HOSTTYPE" = "powerpc" -o "$HOSTTYPE" = "intel-pc" ] ; then
-# exec "$GRASS_WISH" "$GISBASE/etc/gm/gm.tcl" -name gm_tcl
+# exec "$GRASS_WISH" "$GISBASE/etc/gm/gm.tcl" -name gm_tcl_$GIS_LOCK
# else
- exec "$GRASS_WISH" "$GISBASE/etc/gm/gm.tcl" -name gm_tcl &
+ exec "$GRASS_WISH" "$GISBASE/etc/gm/gm.tcl" -name gm_tcl_$GIS_LOCK &
# fi
   exit
fi
@@ -23,4 +23,4 @@
   exec g.parser "$0" "$@"
fi

-exec "$GRASS_WISH" "$GISBASE/etc/gm/gm.tcl" -name gm_tcl "$GIS_OPT_DMRC" sh &
+exec "$GRASS_WISH" "$GISBASE/etc/gm/gm.tcl" -name gm_tcl_$GIS_LOCK "$GIS_OPT_DMRC" sh &

Initial Comment:
This patch does two things:
1) Every gis.m app gets GRASS session ID encoded in it's name;

Encoding the session PID in the application name prevents users from
setting X resources specific to gis.m.

I suggest moving the session check into gis.m itself, e.g. adding the
following to gis.m:

  proc exitif {session_id} {
    global env
    if {$env(GIS_LOCK) == $session_id} exit
  }

and changing the init.sh code to:

  foreach gwin [lsearch -all -inline [winfo interps] gm_tcl*] {
    catch {send -async $gwin exitif $env(GIS_LOCK)}
  }

2) On exit, all gis.m windows with current session ID gets closed.

What needs to be tested:
what will happen on windows? Somebody with Cygwin and MinGW versions please test it.

+ # [ $TCLTKGRASSBASE && !$MINGW && !$CGWIN ] - send does NOT work on windows.

Note that send doesn't work on some current Linux systems either, due
to X security mechanisms.

--
Glynn Clements <glynn@gclements.plus.com>

Hi Glynn.
Thanks for response. Some comments inline.
2007/10/19, Glynn Clements <glynn@gclements.plus.com>:

> Initial Comment:
> This patch does two things:
> 1) Every gis.m app gets GRASS session ID encoded in it's name;

Encoding the session PID in the application name prevents users from
setting X resources specific to gis.m.

Well - this is true ONLY for first gis.m instance (per X user?). Wish
automatic adds #num to second gis.m instance (gm_tcl == first gis.m;
gm_tcl #2 = second one etc.). If setting those X resources is not done
by using wildcards (gm_tcl*), then this is problem also in current
implementation.

I suggest moving the session check into gis.m itself, e.g. adding the
following to gis.m:

Some EXIT procedure could be really good, as gis.m also could do some
stuff on exit i.e. ask to save current workspace (anyone interested in
it?) or offer to exit from whole grass, when called from GUI.

  proc exitif {session_id} {
    global env
    if {$env(GIS_LOCK) == $session_id} exit
  }

and changing the init.sh code to:

  foreach gwin [lsearch -all -inline [winfo interps] gm_tcl*] {
    catch {send -async $gwin exitif $env(GIS_LOCK)}
  }

> 2) On exit, all gis.m windows with current session ID gets closed.
>
> What needs to be tested:
> what will happen on windows? Somebody with Cygwin and MinGW versions
please test it.

+ # [ $TCLTKGRASSBASE && !$MINGW && !$CGWIN ] - send does NOT work on
windows.

Note that send doesn't work on some current Linux systems either, due
to X security mechanisms.

OK. Well - but it will work on some (mine :wink: systems. IMHO wrapping
send with catch should prevent from any bad side effects. For windows
there exists some dde mechanism instead of send, but as being lazy, I
have not implemented it. If somebody wants - go for it.

--
Glynn Clements <glynn@gclements.plus.com>

Thanks!

Maris.

This complex shell programming is a bit much for me, but I found this interesting hack for bash that looks useful.

http://glyf.livejournal.com/63106.html

Summary - zsh has a feature called preexec() to intercept the start of any shell command. This 'glyf' person worked out a way to get that feature in bash (I don't know if it could work in other shells).

The reference I found, that I thought would be nice in GRASS, is to use that feature to track a command and fire off a system notification event when it finishes (in the OSX case, with the Growl notification tool), including the elapsed time. Nice for those long-running GRASS commands.

http://www.macosxhints.com/article.php?story=20071009124425468

The hint as it is set up only works at the top level of the bash shell, so it doesn't work inside the GRASS shell. It would have to be added to init.sh. There is currently no author/license info in the preexec script, so we'd have to ask about that.

There's Growl (3rd-party extension) on OSX. To be useful on other platforms, some system notification mechanism would be needed. Possibly make it configurable, if there are multiple choices of notification extensions. And definitely make it optional.

I'll see if I can get the OSX/Growl preexec working, but I wonder if there is already a similar feature in init.sh that could be used?

-----
William Kyngesburye <kyngchaos*at*kyngchaos*dot*com>
http://www.kyngchaos.com/

"This is a question about the past, is it? ... How can I tell that the past isn't a fiction designed to account for the discrepancy between my immediate physical sensations and my state of mind?"

- The Ruler of the Universe