#!/usr/bin/tclsh

# library procs
# Note: use linuxcnc_var script since this program can be
#       started without using the linuxcnc script and
#       ::env(HALLIB_DIR) will not exist
set hallib_dir [exec linuxcnc_var HALLIB_DIR]
source [file join $hallib_dir hal_procs_lib.tcl]
source [file join $hallib_dir util_lib.tcl]

# A gui to demonstrate the use of the moveoff component for
# applying Hal-only offsets.

# For more info:
# $ moveoff_gui --help  -- command line options
# $ man moveoff_gui     -- additional info
# $ man moveoff         -- about the moveoff component

#-----------------------------------------------------------------------
# Copyright: 2014
# Authors:   Dewey Garrett <dgarrett@panix.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#-----------------------------------------------------------------------
proc wmposition {top} {
  set geo [wm geometry $top]
  return [string range $geo [string first + $geo] end]
} ;# wmposition

proc wmrestore {w position} {
  if {[wm state $w] == "withdrawn"} {
    wm deiconify $w
    wm geometry $w $position
  }
} ;# wmrestore

proc wmcenter w {
  wm withdraw $w
  update idletasks
  set x [expr [winfo screenwidth $w]/2 \
            - [winfo reqwidth $w]/2  - [winfo vrootx [winfo parent $w]]]
  set y [expr [winfo screenheight $w]/2 \
            - [winfo reqheight $w]/2  - [winfo vrooty [winfo parent $w]]]
  wm geom $w +$x+$y
  wm deiconify $w
} ;# wmcenter

proc withdraw_with_save_loc {top} {
  set ::MV(location) [wmposition $top]
  wm withdraw $top
} ;# withdraw_with_save_loc

proc restore_using_save_loc {top} {
  wmrestore $top $::MV(location)
} ;# restore_using_save_loc $::MV(top)

proc qid {} {
  # unique identifier
  if ![info exists ::MV(qid)] { set ::MV(qid) 0 }
  return [incr ::MV(qid)]
} ;# qid

proc get_move_enable {} {
  # special case boolean, used for setting
  # ::MV(enable,offsets) which is  the -variable
  # for the Enable checkbutton and may be
  # managed externally
  # ensure it is 1|0 for comparisons always
  if {[hal getp $::m.move-enable]} {
    return 1
  } else {
    return 0
  }
} ;# get_move_enable

proc do_poll {} {
  set ::MV(enable,offsets) [get_move_enable]
  set apply_offsets        [hal getp $::m.apply-offsets]
  set offset_applied       [hal getp $::m.offset-applied]
  set at_limit             [hal getp $::m.waypoint-limit]

  if {$apply_offsets != $::MV(old,apply_offsets)} {
    if {$apply_offsets} {
      if !$::MV(no_display) {restore_using_save_loc $::MV(top)}
      if {   $::MV(control_move_enable) \
          && $::MV(auto_enable_apply_offsets) \
         } {
        set ::MV(enable,offsets) 1; hal setp $::m.move-enable 1
      }
    } else {
      # apply-offsets deasserted ==> moveoff component will remove offsets
      zero_all_offset_inputs
      withdraw_with_save_loc $::MV(top)
    }
  }

  if {$::MV(enable,offsets) != $::MV(old,enable,offsets)} {
    foreach aname $::MV(axes) {
      if $::MV(enable,offsets) {
        $::MV(button,apply,$aname) configure -state normal
      } else {
        $::MV(button,apply,$aname) configure -state disabled
      }
    }
    if {   !$::MV(enable,offsets) \
        && !$::MV(entry,keep_on_disable)} {
        foreach letter {x y z a b c u v w} {
          set ::MV(offset,$letter) [format "$::MV(offset,format)" 0]
        }
    }
  }

  set status_msg ""
  if {$at_limit} {
    set status_msg "Waypoint limit (Disable required)"; set bg orange
  } else {
    if $offset_applied {
       if $::MV(enable,offsets) {
         set status_msg "OFFSETS ACTIVE";   set bg red
         $::MV(button,enable) conf -state normal
       } else {
         set status_msg "Removing offsets"; set bg yellow
         $::MV(button,enable) conf -state disabled
       }
    } else {
       if $::MV(enable,offsets) {
         set status_msg "Offsets Enabled"
         set bg cyan
         $::MV(button,enable) conf -state normal
       } else {
         set status_msg "Offsets Disabled"; set bg green
         $::MV(button,enable) conf -state normal
       }
    }
  }

  # move_enable deasserted while apply_offsets true
  # Note: apply_offsets included in case external connection
  #       deasserts it
  if {   ($::MV(enable,offsets) != $::MV(old,enable,offsets)) \
      && !$::MV(enable,offsets) \
      &&  $::MV(opt,resume_withdelay) \
      && ($::MV(old,enable,offsets) != -1) \
      && $apply_offsets \
     } {
    # move_enable deasserted   ==> moveoff component will remove offsets
    after 0 request_resume_after_delay
  }

  if { ![hal getp motion.motion-enabled]} {
    set bg white
    set status_msg "${status_msg} --- Motion Off"
    if { $::MV(control_move_enable) } {
      set ::MV(enable,offsets) 0; hal setp $::m.move-enable 0
      $::MV(button,enable) conf -state disabled
      zero_all_offset_inputs
    }
  }

  if {"$status_msg" != $::MV(old,status_msg)} {
    set ::MV(label,applied,text) $status_msg
    $::MV(label,applied) configure -state normal -bg $bg
    if { !$::MV(opt,no_resume_inhibit) } {
      if $offset_applied {
        disallow_resume
      } else {
        allow_resume
      }
    }
  }

  foreach aname $::MV(axes) {
    set jnum $::MV($aname,jnum)
    set ::MV(current,$aname) [format "$::MV(current,format)" \
                             [hal getp $::m.offset-current-${jnum}]]
  }

  set waypoint_pct [hal getp $::m.waypoint-percent-used]
  set waypoint_msg "Waypoint Usage: ${waypoint_pct} %"
  set ::MV(label,message,text) "$waypoint_msg"

  if {"$status_msg" != $::MV(old,waypoint_msg)} {
    if {$waypoint_pct >= $::MV(waypoint,threshold,low)} {
       pack $::MV(label,message,frame) -expand 1 -fill x
       if {$waypoint_pct > $::MV(waypoint,threshold,high)} {
         $::MV(label,message) conf -bg red
       } else {
         $::MV(label,message) conf -bg "#d9d9d9"
       }
    } else {
       $::MV(label,message) conf -bg "#d9d9d9"
       pack forget $::MV(label,message,frame)
    }
  }

  set ::MV(old,apply_offsets) $apply_offsets
  set ::MV(ol                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                