; FILE: Source:modules/NewAlert.ASM          REV: 2 --- Display more info on Alert

*
* Modified by Harry Sintonen. Changes by me are marked with ;hs comment.
*

; NewAlert - Shows the Taskname within an Alert
; © Jens Lorenz 1997
;
; very strongly based upon:

;
; Example BlizKick Module
; ~~~~~~~~~~~~~~~~~~~~~~~
; This code shows how to create replacing BlizKick "Module".
; Should be quite self-explonary... (?)
;
; !CODE MUST BE FULLY PC-RELATIVE!
;
; Written by Harry Sintonen.
; This source code is Public Domain.

        incdir  "include:"
        include "exec/execbase.i"
        include "blizkickmodule.i"              ; Some required...

_LVORawDoFmt            EQU   -$20A
_LVOOpenLibraryTagged   EQU   -$32A
_LVOTimedDisplayAlert   EQU   -$336
_LVOCloseLibrary        EQU   -$19E


        SECTION MODULE,CODE

_DUMMY_LABEL
        BK_MOD BKMF_ReplaceMode!BKMF_ExtResBuf,_end,(RTF_COLDSTART)<<24+39<<16+NT_UNKNOWN<<8+$C9,_name,_name,_init

; Replace mode on, requires EXTRESBUF buffer, COLDSTART module, requires KS V39.x or better,
; module type NT_UNKNOWN, priority -55.

STACKBUF        EQU   300                       ; +100 - just to be safe ...

_init   clr.l   (0).w
        movem.l d2/d7/a2/a3/a6,-(sp)
        move.l  LastAlert(a6),d2
        moveq   #-1,d0                          ; Display no alert?
        cmp.l   d0,d2
        beq     .exit

        lea     -STACKBUF(sp),sp
        move.l  sp,a3
        lea     SoftFail(pc),a0
        move.l  d2,d0
        swap    d0
        cmp.b   #1,d0
        bne.b   .skip0
        lea     NoMem(pc),a0
        bra.b   .skip1

.skip0  btst    #$1F,d2
        bne.b   .skip1
        tst.w   d0
        beq.b   .skip1
        lea     Recoverable(pc),a0
.skip1  bsr     .dostr
        lea     PressLeftie(pc),a0
        bsr     .dostr
        lea     Copyright(pc),a0
        bsr     .dostr
        clr.b   (a3)+

        lea     ErrorFmt(pc),a0
        lea     LastAlert(a6),a1
        lea     .PutChar(pc),a2
        jsr     _LVORawDoFmt(a6)

        lea     37(a3),a3                       ; Adjust a3  ;hs
        st      (a3)+

* decode the TaskName / CLICommandName *
        move.l  (LastAlert+4)(a6),d0            ; ^Task -> d0
        beq     .notask                         ; if 0
        move.l  d0,a0                           ; Task/Process
        move.b  8(a0),d0                        ; LN_TYPE
        cmp.b   #NT_TASK,d0                     ; NT_TASK ???
        beq.b   .task                           ; no CLI-Checking needed
        cmp.b   #NT_PROCESS,d0                  ; NT_PROCESS
        bne.b   .error                          ; neither Task nor Process ?!?!

        move.l  172(a0),d0                      ; process.cli -> d0
        beq.b   .task                           ; no cli-struct -> get the Taskname
        subq.l  #8,sp                           ;hs
        move.l  140(a0),d1                      ; process.tasknum -> d1
        move.l  d1,(sp)                         ; store process.tasknum  ;hs

        lsl.l   #2,d0                           ; d0 was a BPTR !!!
        move.l  d0,a0                           ; BADDR(process.cli) -> a0
        move.l  16(a0),d0                       ; cli.commandname -> a0
        lsl.l   #2,d0                           ; d0 was a BSTR
        move.l  d0,a0                           ; d0 -> a0
        tst.b   (a0)                            ; Length = 0 ?
        beq.b   .nocliname                      ;  -> no CommandName there

        addq.l  #1,a0                           ; skip Length of BSTR
        move.l  a0,4(sp)                        ; BADDR(cli.commandname) -> ArgArray  ;hs
        bra.b   .fmtcliname

.nocliname
        lea     NoCLIName(pc),a2
        move.l  a2,4(sp)                        ;hs
;        bra.b   .fmtcliname

* Format the CLICommandName + CLI-Number *
.fmtcliname
        lea     CLINameFmt(pc),a0
        move.l  sp,a1                           ;hs
        lea     .PutChar(pc),a2
        jsr     _LVORawDoFmt(a6)
        addq.l  #8,sp                           ;hs
        bra.b   .try                            ; display the alert

* It's a task *
.task
        movea.l 10(a0),a2                       ; tc.ln.name
        tst.b   (a2)                            ; Len = 0 ?
        beq.b   .notaskname                     ; -> No TaskName there
        bra.b   .fmttaskname
.error
        lea     Error(pc),a2                    ; Fatal Error
        bra.b   .fmttaskname
.notaskname
        lea     NoTaskName(pc),a2               ; No TaskName
        bra.b   .fmttaskname
.notask
        lea     NoTask(pc),a2                   ; No Task
;       bra.b   .fmttaskname
* Format the TaskName *
.fmttaskname
        lea     TaskNameFmt(pc),a0
        move.l  a2,-(sp)                        ; a2 = ^TaskName ;hs
        move.l  sp,a1                           ;hs
        lea     .PutChar(pc),a2
        jsr     _LVORawDoFmt(a6)
        addq.l  #4,sp                           ;hs

.try    moveq   #3,d0                           ; 3=intuition.library
        jsr     _LVOOpenLibraryTagged(a6)
        tst.l   d0
        beq.b   .try                            ; crash!
        move.l  (LastAlert+3*4)(a6),a1
        move.l  a6,a3
        move.l  d0,a6
        move.l  d2,d0
        move.l  sp,a0
        moveq   #50,d1                          ; we have 1 Line more to display
        jsr     _LVOTimedDisplayAlert(a6)
        move.l  d0,a2
        move.l  a6,a1
        move.l  a3,a6
        jsr     _LVOCloseLibrary(a6)

        lea     STACKBUF(sp),sp
        clr.l   (0).w
        moveq   #-1,d0                          ; No alert on next round, please!
        move.l  d0,LastAlert(a6)
.exit   move.l  a2,d0
        movem.l (sp)+,d2/d7/a2/a3/a6
        rts

* Copy the whole String *
.dostr  clr.b   (a3)+
.copy   move.b  (a0)+,(a3)+
        bne.b   .copy
        st      (a3)+
        rts

* Copy one Char *
.PutChar
        move.b  d0,(a3)+
        clr.b   (a3)
        rts

************** Data **************

_name   dc.b    'alert.hook',13,10,0
        CNOP    0,2

NoMem   dc.b    38,15
        dc.b    'not enough memory. ',0
SoftFail
        dc.b    38,15
        dc.b    'Software Failure. ',0
Recoverable
        dc.b    38,15
        dc.b    'Recoverable Alert. ',0
Copyright
        dc.b    38,40
        dc.b    '(© Jens Lorenz 1997)',0
PressLeftie
        dc.b    234,15
        dc.b    'Press left mouse button to continue.',0
ErrorFmt
        dc.b    142,30
        dc.b    'Error:  %04x %04x   Task: %08lx',0
TaskNameFmt
        dc.b    1,14                            ; dc.w 270 doesn't work !
        dc.b    40
        dc.b    'TaskName: %-30.30s',0          ;hs
CLINameFmt
        dc.b    1,14                            ; dc.w 270
        dc.b    40
        dc.b    'CLI #%-2.2ld : %-30.30s',0     ;hs
NoTask
        dc.b    '» No Task «',0
NoTaskName
        dc.b    '» No TaskName «',0
NoCLIName
        dc.b    '» No CommandName «',0
Error
        dc.b    '» FATAL ERROR «',0

        CNOP    0,2

;hs ArgArray
;hs         dc.l    0                               ; Arg #1
;hs         dc.l    0                               ; Arg #2
;hs
;hs There *can* be self modifying code, but that doesn't mean I like it... ;)

        CNOP    0,2
_end


   SECTION  VERSION,DATA

        dc.b    '$VER: alert.hook_MODULE 2.0 (2.4.97)',0   ;hs

