; Taskswitching with <Right Amiga> & <Left Arrow>

   include "includes/MACS.ASM"

KEY:  EQU   $BFEC01

   INIT_AMIGA
   
   BSR      PRINT
   DC.B     'Easy Taskswitching with <Right Amiga> & <Left Arrow>',10
   DC.B     '(C) 8.88 by RAKO, based on a program from Jorgo Schimanski',10,0
   NOP

   MOVE.L   intuition(PC),A6
   MOVE.L   $34(A6),A0  ; get List of windows !!!!
   STO      A0,active_window
   LINKSYS  WindowToFront,intuition
   LEA      ctrlflg,A0
   MOVE.B   #0,(A0)

POLL:
   MOVEQ    #5,D1       ; 5/50 seconds delay
   LINKSYS  Delay,dos
   CMP.B    #$31,KEY    ; Amiga pressed ?
   BNE.S    POLL_0
   LEA      ctrlflg,A0
   MOVE.B   #$FF,(A0)
   BRA.S    POLL
POLL_0:
   CMP.B    #$30,KEY    ; Amiga released ?
   BNE.S    POLL_1
   LEA      ctrlflg,A0
   MOVE.B   #$00,(A0)
   BRA.S    POLL
POLL_1:
   LEA      ctrlflg,A0
   TST.B    (A0)        ; Amiga active ?
   BEQ.S    POLL        ; if not, give up
   CMP.B    #$61,KEY    ; Left arrow pressed ?
   BNE.S    POLL        ; if not, give up
POLL_2:
   MOVEQ    #5,D1       ; 5/50 seconds delay
   LINKSYS  Delay,dos
   CMP.B    #$60,KEY    ; Left Arrow released ?
   BNE.S    POLL_3
   JSR      NXTWIN
   BRA.S    POLL
POLL_3:
   CMP.B    #30,KEY     ; Amiga released ?
   BNE.S    POLL_2
   LEA      ctrlflg,A0
   MOVE.B   #$00,(A0)
   BRA.S    POLL_2

NXTWIN:
   MOVE.L   intuition(PC),A6
   LEA      active_window,A5
   MOVE.L   (A5),A0
   CMP.L    #0,(A0)     ; more windows present ?
   BEQ.S    WIN_1
   MOVE.L   (A0),(A5)
   BRA.S    WIN_2
WIN_1:
   MOVE.L   $38(A6),A1  ; get first window
   MOVE.L   4(A1),(A5)
WIN_2:
   MOVE.L   active_window(PC),A0
   LINKSYS  WindowToFront,intuition
   MOVE.L   active_window(PC),A0
   LINKSYS  ActivateWindow,intuition
   RTS

   EXIT_AMIGA

active_window: DS.L  1
ctrlflg:  DS.L  1
   END
