import sys
from ctypes import *

pyos_inputhook_ptr = c_void_p.in_dll(pythonapi, "PyOS_InputHook")
orig_pyos_inputhook_ptr_value = pyos_inputhook_ptr.value

def inputhook_wx():
    import wx
    app = wx.GetApp()
    if app is not None:
        assert wx.Thread_IsMain()

        # Make a temporary event loop and process system events until
        # there are no more waiting, then allow idle events (which
        # will also deal with pending or posted wx events.)
        evtloop = wx.EventLoop()
        ea = wx.EventLoopActivator(evtloop)
        while evtloop.Pending():
            evtloop.Dispatch()
        app.ProcessIdle()
        del ea
    return 0

callback_wx = CFUNCTYPE(c_int)(inputhook_wx)


def set_inputhook_wx():
    """Set PyOS_InputHook for interactive wx usage.
    
    This can be called before or after the main wx.App() is created,
    but if this is called, App.MainLoop() should not be called ever.
    """
    # These must be global or it doesn't work
    global pyos_inputhook_ptr
    global callback_wx
    pyos_inputhook_ptr.value = cast(callback_wx, c_void_p).value

def remove_inputhook():
    """
    Remove the PyOS_InputHook returning it back to its original state.
    """
    pyos_inputhook_ptr.value = orig_pyos_inputhook_ptr_value