# coding: utf-8
# python 2 only

# Copyright (c) 2026 TormachTips.com. All rights reserved.
# Licensed under the TormachTips Personal Use License.
# Permission is granted only for private personal use and private personal modification.
# No sharing, publication, distribution, resale, sublicensing, screenshots, code excerpts,
# benchmarks, or videos are permitted without prior written permission.
# Requests:         tormach.1100m@gmail.com
# Information page: https://tormachtips.com/plugins.htm

#############################################
##                                         ##
##            Jog Killer 0.95              ##
##          www.tormachtips.com            ##
##                                         ##
#############################################

# 0.95 - public beta - 1/02/2026

import os
import gtk
import glib
import constants
import singletons
from ui_hooks import plugin

CURRENT_VER      = "0.95"
SCRIPT_NAME      = "Jog Killer (Keyboard Jog Killer)"
DESCRIPTION      = "Disables the keyboard arrows from jogging the machine."
ENABLED          = 1
DEV_MACHINE      = 1
DEV_MACHINE_FLAG = "/home/operator/gcode/python/dev_machine.txt"

class UserPlugin(plugin):
    def __init__(self):
        try:
            import update_checker
            update_checker.tormachtips(__file__, CURRENT_VER)
        except Exception:
            pass                
        plugin.__init__(self, 'Disable Arrow Key Jogging')
        dev_machine_found = os.path.exists(DEV_MACHINE_FLAG)
        if dev_machine_found:
            plugin_enabled = DEV_MACHINE
        else:
            plugin_enabled = ENABLED
        if plugin_enabled:
            glib.timeout_add(3000, self.disable_arrow_jogging)
            return
        else:
            if dev_machine_found:
                self.error_handler.write("[Disable Arrow Key Jogging] Dev machine found. Plugin loaded, but disabled by DEV_MACHINE.", constants.ALARM_LEVEL_QUIET)
            else:
                self.error_handler.write("[Disable Arrow Key Jogging] Plugin loaded, but disabled.", constants.ALARM_LEVEL_QUIET)
                self.error_handler.write("[Disable Arrow Key Jogging] To enable, open script, find ENABLED = 0 and change to ENABLED = 1", constants.ALARM_LEVEL_QUIET)
            return

    def disable_arrow_jogging(self):
        try:
            ui = singletons.g_Machine
            if not ui or not hasattr(ui, "jogging_keys"):
                self.error_handler.write("[Disable Arrow Jog] UI not ready yet - retrying", constants.ALARM_LEVEL_LOW)
                return True  
            removed_count = len(ui.jogging_keys)
            ui.jogging_keys = set()
            self.error_handler.write("[Disable Arrow Jog] Disabled keyboard jogging ({} keys removed)".format(removed_count),constants.ALARM_LEVEL_QUIET)
        except Exception as e:
            self.error_handler.write("[Disable Arrow Jog] Error: {}".format(e), constants.ALARM_LEVEL_LOW)
        return False  
        
DESCRIPTION_LONG = """&nbsp;By default, PathPilot 
    allows jogging the machine using keyboard keys:<br>
&nbsp;â€¢ Arrow keys (&#8592; &#8593; &#8595; &#8594;) move the X and Y axes<br>
&nbsp;â€¢ Page Up / Page Down control the Z axis<br>
    <br>
&nbsp;While useful in some setups, this behavior can also be dangerous â€” a stray&nbsp;keystroke could unintentionally move your machine. </font></p>"""        