# for switching tabs via HAL buttons in GladeVCP panel

from subprocess import Popen, PIPE

scanNumber = 0

_h = hal.component("AXISRC")
_h.newpin("preview-request", hal.HAL_BIT, hal.HAL_IO)
_h.ready()

hal.new_sig("preview-request-signal",hal.HAL_BIT)
hal.connect("AXISRC.preview-request","preview-request-signal")

def user_live_update():

    global scanNumber
    global _h

    if _h['preview-request'] == True:
        root_window.tk.call('.pane.top.right','raise','preview')
        _h['preview-request'] = False

    try:

        happy = hal.component_exists('gladevcp')

        if happy:

            if scanNumber == 0:

                if Popen('halcmd getp gladevcp.backplot',stdout=PIPE,shell=True).communicate()[0].strip() == 'TRUE':
                    root_window.tk.call('.pane.top.right','raise','preview')

                if Popen('halcmd getp gladevcp.probe',stdout=PIPE,shell=True).communicate()[0].strip() == 'TRUE':
                    root_window.tk.call('.pane.top.right','raise','user_0')

                if Popen('halcmd getp gladevcp.toolchange',stdout=PIPE,shell=True).communicate()[0].strip() == 'TRUE':
                    root_window.tk.call('.pane.top.right','raise','user_1')

                scanNumber = 1

            else:

                if Popen('halcmd getp gladevcp.file',stdout=PIPE,shell=True).communicate()[0].strip() == 'TRUE':
                    root_window.tk.call('.pane.top.right','raise','user_2')

                scanNumber = 0

    except Exception:

        traceback.print_exc()


#remove "do you really want to exit?"
root_window.tk.call("wm","protocol",".","WM_DELETE_WINDOW","destroy .")

#start fullscreen

root_window.tk.call("wm","geometry",".","1920x1000")


#root_window.attributes("-fullscreen",1)


