/*****************************************************************************
FILE:               mouse_motion.c
LANGUAGE:           c
SYSTEM:             POSIX
USER-INTERFACE:     NONE
DESCRIPTION
    
    An uncompilation of process_mouse_event.
    
AUTHORS
MODIFICATIONS
    2003-11-24 <PJB> Disassembled and uncompiled.
BUGS
LEGAL
    Reserved
    
    Copyright NeXT Computer Inc. 1988 - 1998
    
    All Rights Reserved.
    
    This program may not be included in any commercial product
    without the author written permission. It may be used freely
    for any non-commercial purpose, provided that this header is
    always included.
*****************************************************************************/
    
/*
0406a12e T _process_mouse_event
0406a2fa T _mouse_motion
0406a5f8 T _InitMouseVars
0406a6c6 T _InitMouse
0406a806 T _TermMouse
0406a828 T _ResetMouse


040c36fc S _mouseDelX
040c3700 S _mouseDelY
040c3704 S _mouseHandedness
040c3708 S _mouseScaleFactors
040c3730 S _mouseScaleThresholds
040c3758 S _numMouseScales


040658f8 T _adb_mouse_init
04065af4 T _adb_mouse
04065bf0 T _adb_mouse_present
04065c02 T _adb_keybd_present
*/


/******************************************************************************
    EvGlobals
    This structures defines the portion of the events driver data structure
    that is exported to the PostScript server. It contains the event queue
    which is in memory shared between the driver and the PostScript server.
    All the variables necessary to read and process events from the queue are
    contained here.
******************************************************************************/

//typedef volatile struct _evGlobals 
//{
//    /*00*/short LLEHead;      /* The next event to be read */
//    /*02*/short LLETail;      /* Where the next event will go */
//    /*04*/short LLELast;      /* The last event entered */
//    /*06*/short eNum;         /* Unique id for mouse events */
//    /*08*/int buttons;        /* State of the mouse buttons 1==down, 0==up */
//    /*0c*/int eventFlags;     /* The current value of event.flags */
//    /*10*/int VertRetraceClock;   /* The current value of event.time */
//    /*14*/int cursorSema;         /* set to disable interrupt code */
//    /*18*/Point cursorLoc;        /* The current location of the cursor */
//    /*1c*/int frame;              /* current cursor frame */
//    /*20*/Bounds workBounds;      /* bounding box of all screens */
//    /*28*/Bounds mouseRect;       /* Rect for mouse-exited events */
//    /*30*/unsigned reserved:27;
//          unsigned wantPressure:1;    /* pressure in current mouseRect? */
//          unsigned wantPrecision:1;   /* precise coord in current mouseRect? */
//          unsigned dontWantCoalesce:1;/* coalesce within  current mouseRect? */
//          unsigned dontCoalesce:1;    /* flag which determines coalescing */
//          unsigned mouseRectValid:1;  /* If nonzero, post a mouse-exited
//                                         whenever mouse outside mouseRect. */
//    /*34*/int movedMask;        /* This contains an event mask for the
//                                   three events MOUSEMOVED,
//                                   LMOUSEDRAGGED,  and RMOUSEDRAGGED.
//                                   It says whether driver should
//                                   generate those events. */
//    /*38*/int AALastEventSent;    /* timestamp for wait cursor */
//    /*3c*/int AALastEventConsumed;    /* timestamp for wait cursor */ 
//    /*40*/int waitCursorUp;       /* Is wait cursor up? */
//    /*44*/char ctxtTimedOut;      /* Has wait cursor timer expired? */
//    /*45*/char waitCursorEnabled; /* Play wait cursor game (per ctxt)? */
//    /*46*/char globalWaitCursorEnabled; /* Play wait cursor game (global)? */
//    /*47*/char waitCursorSema;    /* protects wait cursor fields */
//    /*48*/short waitThreshold;    /* time before wait cursor appears */
//    /*4a*/NXEQElement lleq[LLEQSIZE]; /* The event queue itself */
//} EvGlobals;


    typedef struct {
            char a_00;
            char a_01;
            char del_y:7,right:1;
            char del_x:7,left:1;
    } pme_arg_t;


    int process_mouse_event(pme_arg_t* a2)
    {
        if(buttonsTied){
            if(0==a2->right){
                a2->left=0;
            }
            a2->right=1;
        }else if(mouseHandedness){
            int temp=a2->left;
            a2->left=a2->right;
            a2->right=temp;
        }            
        if((0!=(evg->buttons&4))!=(!(a2->left))){
            if(!(a2->left)){
                lastPrefssure=0xff;
                LLEventPost(NX_LMOUSEDOWN,evg->cursorLoc,0);
                evg->buttons|=4;
            }else{
                lastPressure=0;
                LLEventPost(NX_LMOUSEUP,evg->cursorLoc,0);
                evg->buttons&=(~4);
            }
            evg=dontCoalesce=evg->dontWantCoalesce;
            if(evg->mouseRectValid){
                evg->eventFlags|=NX_NONCOALSESCEDMASK;
            }else{
                evg->eventFlags&=(~NX_NONCOALSESCEDMASK);
            }
        }
        if((evg->buttons&1)==(a2->right)){
            if(!(a2->right)){
                LLEventPost(NX_RMOUSEDOWN,evg->cursorLoc,0);
                evg->buttons|=1;
            }else{
                LLEventPost(NX_RMOUSEUP,evg->cursorLoc,0);
                evg->buttons&=(~1);
            }
        }
        evg->cursorSema=1;
        if((a2->del_x!=0)||(a2->del_y!=0)){
            if((a2->del_x)&0x40){
                mouseDelX-=(a2->del_x|0xffffff80);
            }else{
                mouseDelX-=(a2->del_x);
            }
            if((a2->del_y)&0x40){
                mouseDelY-=(a2->del_y|0xffffff80);
            }else{
                mouseDelY-=(a2->del_y);
            }
        }
        evg->cursorSema=0;
    }



#if 0
_process_mouse_event:
0406a12e    pea a6@
0406a130    movel   sp,a6
0406a132    moveml  d2/d3/a2,sp@-
0406a136    movel   a6@(0x8:w),a2
0406a13a    tstl    _buttonsTied:l
0406a140    beq 0x406a158:b
0406a142    btst    #0,a2@(0x2:w)
0406a148    bne 0x406a150:b
0406a14a    andb    #0xfe,a2@(0x3:w)
0406a150    orb #0x1,a2@(0x2:w)
0406a156    bra 0x406a188:b
0406a158    tstl    _mouseHandedness:l
0406a15e    beq 0x406a188:b
0406a160    moveb   a2@(0x3:w),d0
0406a164    moveq   #1,d2
0406a166    andl    d0,d2
0406a168    moveb   a2@(0x2:w),d1
0406a16c    moveq   #1,d3
0406a16e    andl    d3,d1
0406a170    andb    #0xfe,d0
0406a174    orb d1,d0
0406a176    moveb   d0,a2@(0x3:w)
0406a17a    moveb   a2@(0x2:w),d0
0406a17e    andb    #0xfe,d0
0406a182    orb d2,d0
0406a184    moveb   d0,a2@(0x2:w)
0406a188    movel   _evg:l,a0
0406a18e    movel   a0@(0x8:w),d1
0406a192    bfextu  d1{#29,#1},d1
0406a196    moveb   a2@(0x3:w),d2
0406a19a    movel   d2,d0
0406a19c    notl    d0
0406a19e    moveq   #1,d3
0406a1a0    andl    d3,d0
0406a1a2    cmpl    d1,d0
0406a1a4    beq 0x406a242:w
0406a1a8    btst    #0,d2
0406a1ac    bne 0x406a1d6:b
0406a1ae    moveb   #0xff:b,_lastPressure:l
0406a1b6    clrl    sp@-
0406a1b8    movel   a0@(0x18:w),sp@-
0406a1bc    pea 0x1:w
0406a1c0    bsr _LLEventPost:l
0406a1c6    movel   _evg:l,a0
0406a1cc    movel   a0@(0x8:w),d0
0406a1d0    moveq   #4,d3
0406a1d2    orl d3,d0
0406a1d4    bra 0x406a1fa:b
0406a1d6    clrb    _lastPressure:l
0406a1dc    clrl    sp@-
0406a1de    movel   a0@(0x18:w),sp@-
0406a1e2    pea 0x2:w
0406a1e6    bsr _LLEventPost:l
0406a1ec    movel   _evg:l,a0
0406a1f2    movel   a0@(0x8:w),d0
0406a1f6    moveq   #-5,d3
0406a1f8    andl    d3,d0
0406a1fa    movel   d0,a0@(0x8:w)
0406a1fe    addqw   #0x8,sp
0406a200    addqw   #0x4,sp
0406a202    movel   _evg:l,a0
0406a208    moveb   a0@(0x33:w),d1
0406a20c    bfextu  d1{#29,#1},d1
0406a210    moveb   a0@(0x33:w),d0
0406a214    bfins   d1,d0{#30,#1}
0406a218    moveb   d0,a0@(0x33:w)
0406a21c    movel   _evg:l,a0
0406a222    moveb   a0@(0x33:w),d0
0406a226    btst    #1,d0
0406a22a    beq 0x406a236:b
0406a22c    movel   a0@(0xc:w),d0
0406a230    orw #0x100,d0
0406a234    bra 0x406a23e:b
0406a236    movel   a0@(0xc:w),d0
0406a23a    andw    #0xfeff,d0
0406a23e    movel   d0,a0@(0xc:w)
0406a242    movel   _evg:l,a0
0406a248    movel   a0@(0x8:w),d1
0406a24c    moveq   #1,d3
0406a24e    andl    d3,d1
0406a250    moveb   a2@(0x2:w),d2
0406a254    movel   d2,d0
0406a256    notl    d0
0406a258    andl    d3,d0
0406a25a    cmpl    d1,d0
0406a25c    beq 0x406a2a4:b
0406a25e    btst    #0,d2
0406a262    bne 0x406a282:b
0406a264    clrl    sp@-
0406a266    movel   a0@(0x18:w),sp@-
0406a26a    pea 0x3:w
0406a26e    bsr _LLEventPost:l
0406a274    movel   _evg:l,a0
0406a27a    movel   a0@(0x8:w),d0
0406a27e    orl d3,d0
0406a280    bra 0x406a2a0:b
0406a282    clrl    sp@-
0406a284    movel   a0@(0x18:w),sp@-
0406a288    pea 0x4:w
0406a28c    bsr _LLEventPost:l
0406a292    movel   _evg:l,a0
0406a298    movel   a0@(0x8:w),d0
0406a29c    moveq   #-2,d3
0406a29e    andl    d3,d0
0406a2a0    movel   d0,a0@(0x8:w)
0406a2a4    movel   _evg:l,a0
0406a2aa    moveq   #1,d3
0406a2ac    movel   d3,a0@(0x14:w)
0406a2b0    movew   a2@(0x2:w),d0
0406a2b4    andw    #0xfefe,d0
0406a2b8    beq 0x406a2e6:b
0406a2ba    bfextu  a2@(0x3:w){#0,#7},d0
0406a2c0    btst    #6,d0
0406a2c4    beq 0x406a2ca:b
0406a2c6    moveq   #-128,d3
0406a2c8    orl d3,d0
0406a2ca    subl    d0,_mouseDelX:l
0406a2d0    bfextu  a2@(0x2:w){#0,#7},d0
0406a2d6    btst    #6,d0
0406a2da    beq 0x406a2e0:b
0406a2dc    moveq   #-128,d3
0406a2de    orl d3,d0
0406a2e0    subl    d0,_mouseDelY:l
0406a2e6    movel   _evg:l,a0
0406a2ec    clrl    a0@(0x14:w)
0406a2f0    moveml  a6@(0xfff4:w),d2/d3/a2
0406a2f6    unlk    a6
0406a2f8    rts
#endif


    void mouse_motion(void)
    {
        if(evg.f1==0){
            if((evg.f3&0x20000)==0){
                int d3=(0!=var_0x40b5050);
                int d2=abs(mouseDelX)+abs(mouseDelY);
                if((mouseScaleThreasholds*d3)<=d2){
                    int i=1;
                    while((i<numMouseScales)
                          &&(d2<mouseScaleThreasholds[i]*d3)){
                        i++;
                    }
                    i--;
                    mouseDelX*=mouseScaleFactors[i];
                    mouseDelY*=mouseScaleFactors[i];
                }

                evg.f3&=(~0x30000);
                
                MoveTheCursor((curMouseX+mouseDelX)<<16+(curMouseY+mouseDelY),
                              evg.f3);
                mouseDelX=0;
                mouseDelY=0
            }
        }


        /* 0x406a2fa <mm>:     pea fp@                                     */
        /* 0x406a2fc <mm+2>:   moveal sp,fp                                */
        /* 0x406a2fe <mm+4>:   moveml d2-d4,sp@-                           */
        
        /* 0x406a302 <mm+8>:   moveal @#0x40c36dc <evg>,a0                 */
        /* 0x406a308 <mm+14>:  movel a0@(20),d0                            */
        /* 0x406a30c <mm+18>:  bne 0x406a3e8 <mm+238>                      */
        /* 0x406a310 <mm+22>:  movel a0@(52),d0                            */
        /* 0x406a314 <mm+26>:  btst #17,d0                                 */
        /* 0x406a318 <mm+30>:  bne 0x406a3a2 <mm+168>                      */

        /* 0x406a31c <mm+34>:  movel @#0x40c36fc <mouseDelX>,d2            */
        /* 0x406a322 <mm+40>:  bges 0x406a326 <mm+44>                      */
        /* 0x406a324 <mm+42>:  negl d2                                     */

        /* 0x406a326 <mm+44>:  movel @#0x40c3700 <mouseDelY>,d0            */
        /* 0x406a32c <mm+50>:  bges 0x406a330 <mm+54>                      */
        /* 0x406a32e <mm+52>:  negl d0                                     */

                
                
        /* 0x406a330 <mm+54>:  addl d0,d2                                  */
        /* 0x406a332 <mm+56>:  tstl @#0x40b5050 <version+7839>             */
        /* 0x406a338 <mm+62>:  sne d0                                      */
        /* 0x406a33a <mm+64>:  moveb d0,d3                                 */
        /* 0x406a33c <mm+66>:  extbl d3                                    */
        /* 0x406a33e <mm+68>:  negl d3                                     */
                
        /* 0x406a340 <mm+70>:  movew @#0x40c3730 <mouseScaleThresholds>,d0 */
        /* 0x406a346 <mm+76>:  extl d0                                     */
        /* 0x406a348 <mm+78>:  asll d3,d0                                  */
        /* 0x406a34a <mm+80>:  cmpl d2,d0                                  */
        /* 0x406a34c <mm+82>:  bges 0x406a3a2 <mm+168>                     */

                    
        /* 0x406a34e <mm+84>:  movel #1,d1                                 */
        /* 0x406a350 <mm+86>:  moveal @#0x40c3758 <numMouseScales>,a1      */
        /* 0x406a356 <mm+92>:  cmpal d1,a1                                 */
        /* 0x406a358 <mm+94>:  bles 0x406a372 <mm+120>                     */

        /* 0x406a35a <mm+96>:  lea @#0x40c3732 <mouseScaleThresholds+2>,a0 */

        /* 0x406a360 <mm+102>: movew a0@,d0                                */
        /* 0x406a362 <mm+104>: extl d0                                     */
        /* 0x406a364 <mm+106>: asll d3,d0                                  */
        /* 0x406a366 <mm+108>: cmpl d2,d0                                  */
        /* 0x406a368 <mm+110>: bges 0x406a372 <mm+120>                     */

        /* 0x406a36a <mm+112>: addqw #2,a0                                 */
        /* 0x406a36c <mm+114>: addql #1,d1                                 */
        /* 0x406a36e <mm+116>: cmpal d1,a1                                 */
        /* 0x406a370 <mm+118>: bgts 0x406a360 <mm+102>                     */


        /* 0x406a372 <mm+120>: subl #1,d1                                  */
        /* 0x406a374 <mm+122>: lea @#0x40c3708 <mouseScaleFactors>,a0      */
        /* 0x406a37a <mm+128>: movew 0(a0)[d1.l*2],d0                      */
        /* 0x406a37e <mm+132>: extl d0                                     */
        /* 0x406a380 <mm+134>: mulsl @#0x40c36fc <mouseDelX>,d0            */
        /* 0x406a388 <mm+142>: movel d0,@#0x40c36fc <mouseDelX>            */
        /* 0x406a38e <mm+148>: movew 0(a0)[d1.l*2],d0                      */
        /* 0x406a392 <mm+152>: extl d0                                     */
        /* 0x406a394 <mm+154>: mulsl @#0x40c3700 <mouseDelY>,d0            */
        /* 0x406a39c <mm+162>: movel d0,@#0x40c3700 <mouseDelY>            */


        /* 0x406a3a2 <mm+168>: moveal @#0x40c36dc <evg>,a0                 */
        /* 0x406a3a8 <mm+174>: movel a0@(52),d0                            */
        /* 0x406a3ac <mm+178>: andil #-196609,d0                           */
        /* 0x406a3b2 <mm+184>: movel d0,a0@(52)                            */

        /* 0x406a3b6 <mm+188>: movew a0@(24),d0                            */
        /* 0x406a3ba <mm+192>: addw @#0x40c36fe <mouseDelX+2>,d0           */
        /* 0x406a3c0 <mm+198>: bfins d0,d4,0,16                            */
        /* 0x406a3c4 <mm+202>: movew a0@(26),d0                            */

        /* 0x406a3c8 <mm+206>: addw @#0x40c3702 <mouseDelY+2>,d0           */
        /* 0x406a3ce <mm+212>: movew d0,d4                                 */
        /* 0x406a3d0 <mm+214>: movel a0@(52),sp@-                          */
        /* 0x406a3d4 <mm+218>: movel d4,sp@-                               */
        /* 0x406a3d6 <mm+220>: bsrl 0x4069f16 <MoveTheCursor>              */
        /* 0x406a3dc <mm+226>: clrl @#0x40c3700 <mouseDelY>                */
        /* 0x406a3e2 <mm+232>: clrl @#0x40c36fc <mouseDelX>                */

        /* 0x406a3e8 <mm+238>: moveml fp@(-12),d2-d4                       */
        /* 0x406a3ee <mm+244>: unlk fp                                     */
        /* 0x406a3f0 <mm+246>: rts                                         */
    }

#if 0
_mouse_motion:
0406a2fa    pea a6@
0406a2fc    movel   sp,a6
0406a2fe    moveml  d2/d3/d4,sp@-
0406a302    movel   _evg:l,a0
0406a308    movel   a0@(0x14:w),d0
0406a30c    bne 0x406a3e8:w
0406a310    movel   a0@(0x34:w),d0
0406a314    btst    #17,d0
0406a318    bne 0x406a3a2:w
0406a31c    movel   _mouseDelX:l,d2
0406a322    bge 0x406a326:b
0406a324    negl    d2
0406a326    movel   _mouseDelY:l,d0
0406a32c    bge 0x406a330:b
0406a32e    negl    d0
0406a330    addl    d0,d2
0406a332    tstl    0x40b5050:l
0406a338    sne d0
0406a33a    moveb   d0,d3
0406a33c    extbl   d3
0406a33e    negl    d3
0406a340    movew   _mouseScaleThresholds:l,d0
0406a346    extl    d0
0406a348    asll    d3,d0
0406a34a    cmpl    d2,d0
0406a34c    bge 0x406a3a2:b
0406a34e    moveq   #1,d1
0406a350    movel   _numMouseScales:l,a1
0406a356    cmpl    d1,a1
0406a358    ble 0x406a372:b
0406a35a    lea 0x40c3732:l,a0
0406a360    movew   a0@,d0
0406a362    extl    d0
0406a364    asll    d3,d0
0406a366    cmpl    d2,d0
0406a368    bge 0x406a372:b
0406a36a    addqw   #0x2,a0
0406a36c    addql   #0x1,d1
0406a36e    cmpl    d1,a1
0406a370    bgt 0x406a360:b
0406a372    subql   #0x1,d1
0406a374    lea _mouseScaleFactors:l,a0
0406a37a    movew   a0@(0x0:b,d1:l:2),d0
0406a37e    extl    d0
0406a380    mulsl   _mouseDelX:l,d0
0406a388    movel   d0,_mouseDelX:l
0406a38e    movew   a0@(0x0:b,d1:l:2),d0
0406a392    extl    d0
0406a394    mulsl   _mouseDelY:l,d0
0406a39c    movel   d0,_mouseDelY:l
0406a3a2    movel   _evg:l,a0
0406a3a8    movel   a0@(0x34:w),d0
0406a3ac    andl    #0xfffcffff,d0
0406a3b2    movel   d0,a0@(0x34:w)
0406a3b6    movew   a0@(0x18:w),d0
0406a3ba    addw    0x40c36fe:l,d0
0406a3c0    bfins   d0,d4{#0,#16}
0406a3c4    movew   a0@(0x1a:w),d0
0406a3c8    addw    0x40c3702:l,d0
0406a3ce    movew   d0,d4
0406a3d0    movel   a0@(0x34:w),sp@-
0406a3d4    movel   d4,sp@-
0406a3d6    bsr _MoveTheCursor:l
0406a3dc    clrl    _mouseDelY:l
0406a3e2    clrl    _mouseDelX:l
0406a3e8    moveml  a6@(0xfff4:w),d2/d3/d4
0406a3ee    unlk    a6
0406a3f0    rts
#endif


/*** mouse_motion.c                   --                     --          ***/
