/* This patch is a quite ugly way to solve the problem
** that occurs when Intuition tries to draw something
** in a screen which has it's layers locked.
** The problem is solved by disabling dangerous functions.
** Some functions remain because they require more work.
** An example of this is OpenWindow(), since OpenWindow()
** can't be as easily ignored as the functions that ARE
** patched here. OpenWindow() would require some sort of
** semaphore locking and waiting - and so we're back where
** we started.
*/

IMPORT struct IntuitionBase *IntuitionBase;

IMPORT SHORT lvoActivateWindow, lvoWindowToFront, lvoWindowToBack;
VOID myActivateWindow(), myWindowToFront(), myWindowToBack();

LONG oldActivateWindow, oldWindowToFront, oldWindowToBack;

STATIC WORD patched = 0;

VOID SafePatch()
{
    if (!patched) {
        Forbid();     /* I don't expect interrupts to do much intuition */
        oldActivateWindow =
          SetFunction(IntuitionBase, (LONG)lvoActivateWindow, myActivateWindow);
        oldWindowToFront =
          SetFunction(IntuitionBase, (LONG)lvoWindowToFront, myWindowToFront);
        oldWindowToBack =
          SetFunction(IntuitionBase, (LONG)lvoWindowToBack, myWindowToBack);
        Permit();
        patched = 1;
    }
}

VOID SafeRestore()
{
    if (patched) {
        Forbid();
        (VOID)SetFunction(IntuitionBase, (LONG)lvoActivateWindow, oldActivateWindow);
        (VOID)SetFunction(IntuitionBase, (LONG)lvoWindowToFront, oldWindowToFront);
        (VOID)SetFunction(IntuitionBase, (LONG)lvoWindowToBack, oldWindowToBack);
        Permit();
        patched = 0;
    }
}
