#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA634_A31E_A31M_X31J
extern BOOLEAN G3450_B_24KHzVideoMode;

int16_t G3456_ = 1;
BOOLEAN G3457_B_MouseInterruptHandlerInstalled;
int16_t G3458_i_PreviousMouseButtons;
#ifdef MEDIA692_X31J
int16_t G3459 = 160;
int16_t G3460_ = 184;
int16_t G3461_;
#endif
int16_t G3462_[4] = { 0, 0, 16, 16 };
int16_t G3463_i_MouseButtons;
int16_t G3464_i_MouseX;
int16_t G3465_i_MouseY;
int16_t G3466_;
int16_t G3272_ = 1;
#ifdef MEDIA692_X31J
void (*G3467_)();
int16_t G3468_i_AddInterruptHandlerResult;
int16_t G3469_;
INT1_HANDLER G3470_s_InterruptHandler;
#endif


void F1160_USIO_04_Empty(
)
{
        ASM_STUB_PREFIX
        ASM_STUB_SUFFIX
}

void F1161_USIO_05_Empty(
)
{
        ASM_STUB_PREFIX
        ASM_STUB_SUFFIX
}

void F1162_USIO_06_HidePointer(
)
{
        ASM_STUB_PREFIX
        if (G3272_++ == 0) {
#ifdef MEDIA692_X31J
                asm {
                        move.l  #0xC2,d0        /* SP_OFF ​​sprite off */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                }
#endif
#ifdef MEDIA619_A31E_A31M
                SetPointer(G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window, G3191_ps_MsgPort_FTL_DRIA_Video->_0x148_SpriteImage, 1L, 16L, -1L, 0L);
#endif
        }
        ASM_STUB_SUFFIX
}

void F1163_USIO_07_ShowPointer(
)
{
        ASM_STUB_PREFIX
        if (G3272_-- == 1) {
#ifdef MEDIA692_X31J
                asm {
                        move.l  #0xC1,d0        /* SP_ON ​​sprite on */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                }
#endif
#ifdef MEDIA619_A31E_A31M
                SetPointer(G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window, G3191_ps_MsgPort_FTL_DRIA_Video->_ai_0x154, 16L, 16L, -1L, 0L);
#endif
        }
        ASM_STUB_SUFFIX
}

#ifdef MEDIA692_X31J
int16_t F1684_GetMouseStatus(int16_t* P5028_pi_MouseX, int16_t* P5029_pi_MouseY) {
        *P5028_pi_MouseX = G3464_i_MouseX;
        *P5029_pi_MouseY = G3465_i_MouseY;
        return G3463_i_MouseButtons;
}

int16_t F1693_(int16_t P5033_i_Unreferenced) {
        if (G3272_ > 0) {
                return -1;
        } else {
                return G3456_;
        }
}

void F1686_(
)
{
        asm {
                        move.l  #0xC2,d0        /* SP_OFF ​​sprite off */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        clr.l   -(sp)
                        dc.w    0xFF20          /* DOS CALL SUPER */
                        lea     @Tbyte_B9E,a0
                        movea.l #0xEBC000,a1
                        bsr.s   @Tloc_B74
                        movea.l #0xEB0000,a0
                        move.w  #0x1FF,d1
                        moveq   #0,d2
                @Tloc_B40:
                        move.w  d2,(a0)+
                        dbf     d1,@Tloc_B40
                        lea     (0xE823E0).l,a0
                        move.w  #0,(a0)
                        move.w  #0,0x18(a0)
                        move.w  #0xFFC1,8(a0)
                        move.l  d0,-(sp)
                        dc.w    0xFF20          /* DOS CALL SUPER */
                        addq.l  #4,sp
                        moveq   #0x70,d0        /* MS_INIT */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        moveq   #0x72,d0        /* MS_CUROF */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        dc.w    0x3B7C
                        ori.b   #0xD0,d1
                        bra.w   @Tlocret_C9E
                @Tloc_B74:
                        move.w  #0x3F,d1
                @Tloc_B78:
                        move.w  (a0)+,(a1)+
                        dbf     d1,@Tloc_B78
                        move.w  #0x3F,d1
                @Tloc_B82:
                        clr.w   (a1)+
                        dbf     d1,@Tloc_B82
                        move.w  #0x3F,d1
                @Tloc_B8C:
                        move.w  (a0)+,(a1)+
                        dbf     d1,@Tloc_B8C
                        move.w  #0xBF,d1
                @Tloc_B96:
                        clr.w   (a1)+
                        dbf     d1,@Tloc_B96
                        rts
                @Tbyte_B9E:
                        dc.b 0xCC, 0x00, 0x00, 0x00, 0xC4, 0xC0, 0x00, 0x00, 0xC4, 0xC0, 0x00, 0x00, 0xC4, 0x4C, 0x00, 0x00
                        dc.b 0xC4, 0x4C, 0x00, 0x00, 0xC4, 0x44, 0xC0, 0x00, 0xC4, 0x44, 0xC0, 0x00, 0xC4, 0x44, 0x4C, 0x00
                        dc.b 0xC4, 0x44, 0x4C, 0x00, 0xC4, 0x44, 0x44, 0xC0, 0xC4, 0x44, 0x44, 0xC0, 0xC4, 0x44, 0x44, 0x4C
                        dc.b 0xC4, 0x44, 0x44, 0x4C, 0xC4, 0x44, 0x44, 0x44, 0xC4, 0x44, 0x44, 0x44, 0xC4, 0x44, 0x44, 0x44
                        dc.b 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                        dc.b 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                        dc.b 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                        dc.b 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00
                        dc.b 0xC4, 0x44, 0x44, 0x44, 0xC4, 0x44, 0x44, 0x44, 0xC4, 0x44, 0x44, 0x44, 0xC4, 0x44, 0x44, 0x4C
                        dc.b 0xC4, 0x44, 0xC4, 0x4C, 0xC4, 0x44, 0xC4, 0x4C, 0xC4, 0x4C, 0x0C, 0x44, 0xC4, 0x4C, 0x0C, 0x44
                        dc.b 0xC4, 0xC0, 0x0C, 0x44, 0xC4, 0xC0, 0x00, 0xC4, 0xCC, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x00, 0xC4
                        dc.b 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x0C
                        dc.b 0x4C, 0x00, 0x00, 0x00, 0x44, 0xC0, 0x00, 0x00, 0x44, 0xC0, 0x00, 0x00, 0xCC, 0xC0, 0x00, 0x00
                        dc.b 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00
                        dc.b 0xC0, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00
                        dc.b 0x44, 0xC0, 0x00, 0x00, 0x44, 0xC0, 0x00, 0x00, 0x44, 0xC0, 0x00, 0x00, 0xCC, 0xC0, 0x00, 0x00
                @Tlocret_C9E:
        }
}

void F1694_AddMouseInputToQueue(int16_t P5030_i_MouseX, int16_t P5031_MouseY, int16_t P5032_MouseButtons) {
        USIODATA L5162_;


        if (P5032_MouseButtons & 0x03) {
                L5162_.UsioType = 17;
        } else {
                L5162_.UsioType = 18;
        }
        L5162_.UsioValue = 0;
        L5162_.Data.UsioMouse.Buttons = P5032_MouseButtons;
        L5162_.Data.UsioMouse.X = P5030_i_MouseX;
        L5162_.Data.UsioMouse.Y = P5031_MouseY;
        F1173_AddUsioDataToInputQueue(&L5162_);
}

void F1685_InstallMouseInterruptHandler(
)
{
        if (G3457_B_MouseInterruptHandlerInstalled) {
                return;
        }
        G3457_B_MouseInterruptHandlerInstalled = C1_TRUE;
        asm {
                lea     @Tdword_F4C,a0
                move.l  a5,(a0)
                lea     @Tloc_D24,a0
                move.l  a0,G3467_(a5)
        }
        G3470_s_InterruptHandler.Code = G3467_;
        G3470_s_InterruptHandler.Priority = 127;
        G3470_s_InterruptHandler.Type = 0; /* Vertical Blank */
        G3470_s_InterruptHandler.CurrentInterruptCount = 1; /* Runs on every interrupt */
        G3468_i_AddInterruptHandlerResult = INT1_04_InstallInterruptHandler(&G3470_s_InterruptHandler);
        return;
        asm {
                @Tloc_D24:
                        movem.l d0-d7/a0-a6,-(sp)
                        movea.l @Tdword_F4C,a5
                        tst.w   G3461_(a5)
                        beq.w   @Tloc_F46
                        movea.l #0xEB0808,a6
                        andi.w  #0xFDFF,(a6)
                        moveq   #0x74,d0        /* MS_GETDT */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        not.w   G3469_(a5)
                        beq.s   @Tloc_D66
                        moveq   #0,d5           /* Clear buttons state */
                        lsr.l   #8,d0
                        bcc.s   @Tloc_D50       /* If right button is not pressed */
                        moveq   #1,d5           /* Set bit 0 = right button is pressed */
                @Tloc_D50:
                        lsr.l   #8,d0
                        bcc.s   @Tloc_D58
                        ori.w   #2,d5           /* Set bit 1 = left button is pressed */
                @Tloc_D58:
                        move.w  G3458_i_PreviousMouseButtons(a5),d4
                        move.w  d5,G3458_i_PreviousMouseButtons(a5)
                        move.w  d5,G3463_i_MouseButtons(a5)
                        swap    d0
                @Tloc_D66:
                        swap    d0
                        tst.w   G3466_(a5)
                        bne.w   @Tloc_EF4
                        move.w  d0,d3
                        andi.w  #0xFF,d3
                        ext.w   d3
                        add.w   G3460_(a5),d3
                        cmpi.w  #1,G3450_B_24KHzVideoMode(a5)
                        bne.s   @Tloc_D8A
                        cmpi.w  #0x1BF,d3
                        bra.s   @Tloc_D8E
                @Tloc_D8A:
                        cmpi.w  #0x1FF,d3
                @Tloc_D8E:
                        bhi.s   @Tloc_DAA
                        move.w  d3,G3460_(a5)
                        move.w  d3,G3465_i_MouseY(a5)
                        lsr     G3465_i_MouseY(a5)
                        cmpi.w  #1,G3450_B_24KHzVideoMode(a5)
                        beq.s   @Tloc_DAA
                        subi.w  #0x1C,G3465_i_MouseY(a5)
                @Tloc_DAA:
                        move.w  d0,d3
                        lsr.w   #8,d3
                        ext.w   d3
                        add.w   G3459(a5),d3
                        cmpi.w  #1,G3450_B_24KHzVideoMode(a5)
                        bne.s   @Tloc_DC2
                        cmpi.w  #0x13F,d3
                        bra.s   @Tloc_DC6
                @Tloc_DC2:
                        cmpi.w  #0x1C7,d3
                @Tloc_DC6:
                        bhi.s   @Tloc_DDE
                        move.w  d3,G3459(a5)
                        move.w  d3,G3464_i_MouseX(a5)
                        cmpi.w  #1,G3450_B_24KHzVideoMode(a5)
                        beq.s   @Tloc_DDE
                        subi.w  #0x48,G3464_i_MouseX(a5)
                @Tloc_DDE:
                        move.w  G3465_i_MouseY(a5),-(sp)
                        move.w  G3464_i_MouseX(a5),-(sp)
                        jsr     F1693_
                        addq.l  #4,sp
                        moveq   #0,d1
                        move.w  G3456_(a5),d1
                        ext.l   d0
                        bmi.s   @Tloc_E06
                        subq.w  #1,d0
                        subq.w  #1,d1
                        cmp.w   d1,d0
                        beq.s   @Tloc_E28
                        move.w  d0,G3456_(a5)
                        addq.w  #1,G3456_(a5)
                @Tloc_E06:
                        movea.l #0xEB0000,a0
                        lsl.l   #6,d1
                        adda.l  d1,a0
                        clr.w   6(a0)
                        clr.w   0xE(a0)
                        clr.w   0x16(a0)
                        clr.w   0x1E(a0)
                        clr.w   0x26(a0)
                        clr.w   0x2E(a0)
                @Tloc_E28:
                        move.l  d0,d1
                        bmi.w   @Tloc_EF4
                        lea     G3462_(a5),a1
                        lsl.l   #3,d0
                        adda.l  d0,a1
                        movea.l #0xEB0000,a0
                        lsl.l   #3,d0
                        adda.l  d0,a0
                        move.w  G3460_(a5),d3
                        cmpi.w  #1,G3450_B_24KHzVideoMode(a5)
                        beq.s   @Tloc_E50
                        addi.w  #0x10,d3
                @Tloc_E50:
                        sub.w   2(a1),d3
                        sub.w   2(a1),d3
                        move.w  d3,2(a0)
                        move.w  d3,0xA(a0)
                        addi.w  #0x10,d3
                        move.w  d3,0x12(a0)
                        move.w  d3,0x1A(a0)
                        addi.w  #0x10,d3
                        move.w  d3,0x22(a0)
                        move.w  d3,0x2A(a0)
                        move.w  G3459(a5),d3
                        cmpi.w  #1,G3450_B_24KHzVideoMode(a5)
                        beq.s   @Tloc_E88
                        addi.w  #0x18,d3
                @Tloc_E88:
                        addi.w  #0x10,d3
                        sub.w   (a1),d3
                        move.w  d3,(a0)
                        move.w  d3,0x10(a0)
                        move.w  d3,0x20(a0)
                        addi.w  #0x10,d3
                        move.w  d3,8(a0)
                        move.w  d3,0x18(a0)
                        move.w  d3,0x28(a0)
                        lsl.w   #3,d1
                        addi.w  #0xF80,d1
                        move.w  d1,4(a0)
                        addq.w  #1,d1
                        move.w  d1,0xC(a0)
                        addq.w  #1,d1
                        move.w  d1,0x14(a0)
                        addq.w  #1,d1
                        move.w  d1,0x1C(a0)
                        addq.w  #1,d1
                        move.w  d1,0x24(a0)
                        addq.w  #1,d1
                        move.w  d1,0x2C(a0)
                        move.w  #3,6(a0)
                        move.w  #3,0xE(a0)
                        move.w  #3,0x16(a0)
                        move.w  #3,0x1E(a0)
                        move.w  #3,0x26(a0)
                        move.w  #3,0x2E(a0)
                @Tloc_EF4:
                        tst.w   G3469_(a5)
                        beq.s   @Tloc_F42
                        eor.w   d5,d4           /* Get changes in buttons status */
                        andi.w  #3,d4
                        beq.s   @Tloc_F42
                        btst    #0,d4           
                        beq.s   @Tloc_F22       /* If right button status did not change */
                        moveq   #1,d3           /* Assume right button was pressed */
                        btst    #0,d5
                        bne.s   @Tloc_F12       /* If right button was pressed */
                        moveq   #4,d3           /* Right button was released */
                @Tloc_F12:
                        move.w  d3,-(sp)
                        move.w  G3465_i_MouseY(a5),-(sp)
                        move.w  G3464_i_MouseX(a5),-(sp)
                        jsr     F1694_AddMouseInputToQueue
                        addq.l  #6,sp
                @Tloc_F22:
                        btst    #1,d4
                        beq.s   @Tloc_F42       /* If left button status did not change */
                        moveq   #2,d3           /* Assume left button was pressed */
                        btst    #1,d5
                        bne.s   @Tloc_F32       /* If left button was pressed */
                        moveq   #8,d3           /* Left button was released */
                @Tloc_F32:
                        move.w  d3,-(sp)
                        move.w  G3465_i_MouseY(a5),-(sp)
                        move.w  G3464_i_MouseX(a5),-(sp)
                        jsr     F1694_AddMouseInputToQueue
                        addq.l  #6,sp
                @Tloc_F42:
                        ori.w   #0x200,(a6)
                @Tloc_F46:
                        movem.l (sp)+,d0-d7/a0-a6
                        bra.s   @Tlocret_F50
                @Tdword_F4C:
                        dc.l 0
                @Tlocret_F50:
        }
}

void F1687_UninstallMouseInterruptHandler(
)
{
        if (G3457_B_MouseInterruptHandlerInstalled == C1_TRUE) {
                G3457_B_MouseInterruptHandlerInstalled = C0_FALSE;
                INT1_05_UninstallInterruptHandler(&G3470_s_InterruptHandler);
        }
}
#endif
#endif
