home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch18 / stamper.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  16.2 KB  |  418 lines

  1.         title   STAMPER: a simple keyboard monitor
  2.         page    55,132
  3.         .286
  4.         .sall
  5. ; STAMPER.EXE: a simple OS/2 monitor that inserts
  6. ; a date or time stamp into the keyboard data stream.
  7. ; Copyright (C) 1987 Ray Duncan
  8. ;
  9. ; Alt-D  is the hot-key for a date-stamp.
  10. ; Alt-T  is the hot-key for a time-stamp.
  11. ; Alt-X  causes the STAMPER.EXE monitor to exit.
  12. ;
  13. ; In order to keep this example program simple, it
  14. ; contains minimal error handling and does not check
  15. ; for previously loaded copies of itself in the same
  16. ; screen group etc.  For a more complete example, see
  17. ; the SNAP.ASM or SNAP.C program.
  18. ;
  19. ; Build with:   C>MASM STAMPER;
  20. ;               C>LINK STAMPER,,,OS2,STAMPER;
  21. ;
  22. ; Usage:        C>DETACH STAMPER
  23.  
  24. stdin   equ     0                       ; standard device handles
  25. stdout  equ     1
  26. stderr  equ     2
  27.  
  28. cr      equ     0dh                     ; ASCII carriage return
  29. lf      equ     0ah                     ; ASCII line feed
  30.  
  31.                                         ; Hot-key definitions:
  32. datekey equ     20h                     ; Alt-D = insert date
  33. timekey equ     14h                     ; Alt-T = insert time
  34. exitkey equ     2dh                     ; Alt-X = exit program
  35.  
  36.         extrn   DosExit:far             ; references to OS/2 API
  37.         extrn   DosGetInfoSeg:far          
  38.         extrn   DosMonClose:far
  39.         extrn   DosMonOpen:far          
  40.         extrn   DosMonRead:far
  41.         extrn   DosMonReg:far
  42.         extrn   DosMonWrite:far
  43.         extrn   DosSetPrty:far
  44.         extrn   DosSleep:far
  45.         extrn   DosWrite:far
  46.         extrn   VioEndPopUp:far
  47.         extrn   VioGetAnsi:far
  48.         extrn   VioPopUp:far
  49.         extrn   VioWrtCharStr:far
  50.  
  51. jerr    macro   target                  ;; Macro to test return code
  52.         local   zero                    ;; in AX and jump if non-zero.
  53.         or      ax,ax                   ;; Uses JMP DISP16 to avoid
  54.         jz      zero                    ;; branch out of range errors
  55.         jmp     target
  56. zero:
  57.         endm
  58.  
  59. DGROUP  group   _DATA
  60.  
  61. _DATA   segment word public 'DATA'
  62.  
  63. kname   db      'KBD$',0                ; device name of keyboard
  64. khandle dw      0                       ; handle from DosMonOpen
  65.  
  66. gseg    dw      ?                       ; global information segment
  67. lseg    dw      ?                       ; local information segment
  68. scrgrp  dw      ?                       ; foreground screen group 
  69. ansi    dw      ?                       ; receives ANSI state
  70. popflag dw      1                       ; wait for PopUp window
  71. wlen    dw      ?                       ; receives length written
  72.  
  73. dstr    db      'mm/dd/yy',0            ; strings used by time and
  74. tstr    db      'hh:mm',0               ; date formatting routines
  75.  
  76. monin   dw      128,64 dup (0)          ; buffers for monitor
  77. monout  dw      128,64 dup (0)
  78.  
  79. packet  db      128 dup (0)             ; buffer for kbd data packet
  80. pktlen  dw      ?                       ; contains max buffer length; 
  81.                                         ; receives actual data length
  82.  
  83. msg1    db      cr,lf,'Start STAMPER with DETACH!',cr,lf
  84. msg1_len equ $-msg1             
  85.  
  86. msg2    db      'STAMPER utility installed'
  87. msg2_len equ $-msg2
  88.  
  89. msg3    db      'Alt-D to insert date stamp,'
  90. msg3_len equ $-msg3
  91.  
  92. msg4    db      'Alt-T to insert time stamp,'
  93. msg4_len equ $-msg4
  94.  
  95. msg5    db      'Alt-X to shut down STAMPER.'
  96. msg5_len equ $-msg5
  97.  
  98. msg6    db      'STAMPER utility deactivated.'
  99. msg6_len equ $-msg6
  100.  
  101. _DATA   ends
  102.  
  103.  
  104. _TEXT   segment word public 'CODE'
  105.  
  106.         assume  cs:_TEXT,ds:DGROUP,ss:DGROUP
  107.  
  108. main    proc    far                     ; entry point from OS/2
  109.  
  110.                                         ; get info segment selectors...
  111.         push    ds                      ; receives global selector
  112.         push    offset DGROUP:gseg      
  113.         push    ds                      ; receives local selector
  114.         push    offset DGROUP:lseg
  115.         call    DosGetInfoSeg           ; transfer to OS/2
  116.         jerr    main6                   ; give up if can't get selectors
  117.  
  118.                                         ; make sure we are detached...
  119.         push    ds                      ; receives ANSI state flag
  120.         push    offset DGROUP:ansi
  121.         push    0                       ; VIO handle
  122.         call    VioGetAnsi
  123.         or      ax,ax                   ; call should fail, otherwise
  124.         jnz     main1                   ; we are not detached.
  125.  
  126.                                         ; not detached, display error 
  127.                                         ; message and exit...
  128.         push    stderr                  ; handle for standard error
  129.         push    ds                      ; address of message
  130.         push    offset DGROUP:msg1
  131.         push    msg1_len                ; length of message
  132.         push    ds                      ; receives bytes written
  133.         push    offset DGROUP:wlen
  134.         call    DosWrite                ; transfer to OS/2
  135.         jmp     main6                   ; go exit with error code
  136.  
  137. main1:  mov     es,gseg                 ; get foreground screen group 
  138.         mov     al,byte ptr es:[0018h]  ; from global info segment
  139.         cbw
  140.         mov     scrgrp,ax               ; and save it   
  141.  
  142.                                         ; open monitor connection... 
  143.         push    ds                      ; address of device name KBD$ 
  144.         push    offset DGROUP:kname
  145.         push    ds                      ; receives monitor handle
  146.         push    offset DGROUP:khandle
  147.         call    DosMonOpen              ; transfer to OS/2
  148.         jerr    main6                   ; give up if can't open it
  149.  
  150.                                         ; register as keyboard monitor...
  151.         push    khandle                 ; handle from DosMonOpen
  152.         push    ds                      ; addr of monitor input buffer
  153.         push    offset DGROUP:monin
  154.         push    ds                      ; addr of monitor output buffer
  155.         push    offset DGROUP:monout
  156.         push    1                       ; request front of list
  157.         push    scrgrp                  ; index = screen group 
  158.         call    DosMonReg               ; transfer to OS/2
  159.         jerr    main6                   ; give up if can't register
  160.  
  161.         call    signon                  ; else announce our presence 
  162.  
  163.                                         ; promote main thread to time-
  164.                                         ; critical priority...
  165.         push    2                       ; scope = single thread this process
  166.         push    3                       ; class = time-critical
  167.         push    0                       ; level = 0 within class
  168.         push    1                       ; thread ID 
  169.         call    DosSetPrty              ; transfer to OS/2
  170.  
  171. main2:                                  ; monitor the keyboard character
  172.                                         ; stream; when hot key detected,
  173.                                         ; insert the appropriate date or
  174.                                         ; time stamp, or exit.
  175.  
  176.         mov     pktlen,pktlen-packet    ; set max buffer length for read
  177.  
  178.                                         ; get next keyboard data packet
  179.         push    ds                      ; address of monitor input buffer
  180.         push    offset DGROUP:monin
  181.         push    0                       ; wait until data available
  182.         push    ds
  183.         push    offset DGROUP:packet    ; buffer for keyboard data packet
  184.         push    ds
  185.         push    offset DGROUP:pktlen    ; receives length of data packet
  186.         call    DosMonRead              ; transfer to OS/2
  187.  
  188.         cmp     byte ptr packet+2,0     ; is this extended code?
  189.         jnz     main4                   ; no, just pass it on
  190.  
  191.         cmp     byte ptr packet+3,exitkey
  192.         jz      main5                   ; jump if exit hot-key
  193.  
  194.         cmp     byte ptr packet+3,timekey
  195.         jnz     main3                   ; jump if not time hot-key
  196.  
  197.         cmp     word ptr packet+12,0    ; discard break packets
  198.         jnz     main2
  199.  
  200.         call    time                    ; insert the time stamp
  201.  
  202.         jmp     main2                   ; discard this key
  203.  
  204. main3:                                  ; is it datestamp hot-key?
  205.         cmp     byte ptr packet+3,datekey
  206.         jnz     main4                   ; no, jump
  207.  
  208.         cmp     word ptr packet+12,0    ; discard break packets
  209.         jnz     main2
  210.  
  211.         call    date                    ; insert the date stamp
  212.  
  213.         jmp     main2                   ; discard this key
  214.  
  215. main4:                                  ; Not hot-key, pass packet on.
  216.         push    ds                      ; address of monitor output buffer
  217.         push    offset DGROUP:monout
  218.         push    ds                      ; address of keyboard data packet
  219.         push    offset DGROUP:packet    
  220.         push    pktlen                  ; length of data packet
  221.         call    DosMonWrite             ; transfer to OS/2      
  222.         
  223.         jmp     main2                   ; wait for another packet
  224.  
  225. main5:                                  ; hotkey for de-install detected
  226.  
  227.         cmp     word ptr packet+12,0    ; make sure it's Break packet
  228.         jz      main2                   ; if not just discard it
  229.  
  230.         push    khandle                 ; close the monitor connection
  231.         call    DosMonClose             ; transfer to OS/2
  232.  
  233.         call    signoff                 ; announce STAMPER exit 
  234.  
  235.         push    1                       ; terminate all threads
  236.         push    0                       ; return success code
  237.         call    DosExit                 ; final exit to OS/2
  238.  
  239. main6:                                  ; common error exit point...
  240.         push    1                       ; terminate all threads
  241.         push    1                       ; return error code
  242.         call    DosExit                 ; final exit to OS/2
  243.  
  244. main    endp
  245.  
  246.  
  247. date    proc    near                    ; format and insert date stamp
  248.  
  249.         mov     es,gseg                 ; get selector for global
  250.                                         ; read-only information segment 
  251.  
  252.         mov     al,byte ptr es:[11h]    ; convert month to ASCII
  253.         aam
  254.         add     ax,'00'
  255.         xchg    al,ah
  256.         mov     word ptr dstr,ax
  257.  
  258.         mov     al,byte ptr es:[10h]    ; convert day to ASCII
  259.         aam
  260.         add     ax,'00'
  261.         xchg    al,ah
  262.         mov     word ptr dstr+3,ax
  263.  
  264.         mov     ax,word ptr es:[12h]    ; convert year to ASCII
  265.         sub     ax,1900
  266.         aam
  267.         add     ax,'00'
  268.         xchg    al,ah
  269.         mov     word ptr dstr+6,ax
  270.  
  271.         mov     si,offset DGROUP:dstr   ; insert date stamp string
  272.         call    stuff                   ; into keyboard data stream
  273.  
  274.         ret                             ; back to caller
  275.  
  276. date    endp
  277.  
  278.  
  279. time    proc    near                    ; format and insert time stamp
  280.  
  281.         mov     es,gseg                 ; get selector for global
  282.                                         ; read-only information segment 
  283.  
  284.         mov     al,byte ptr es:[8]      ; convert hours to ASCII
  285.         aam
  286.         add     ax,'00'
  287.         xchg    al,ah
  288.         mov     word ptr tstr,ax
  289.  
  290.         mov     al,byte ptr es:[9]      ; convert minutes to ASCII
  291.         aam
  292.         add     ax,'00'
  293.         xchg    al,ah
  294.         mov     word ptr tstr+3,ax
  295.  
  296.         mov     si,offset DGROUP:tstr   ; insert time stamp string
  297.         call    stuff                   ; into keyboard data stream
  298.  
  299.         ret                             ; back to caller
  300.  
  301. time    endp
  302.  
  303.  
  304. stuff   proc    near                    ; insert string into keyboard
  305.                                         ; data stream.  Call with
  306.                                         ; SI = ASCIIZ string (null
  307.                                         ;      is discarded)
  308.                                         ; AL, SI destroyed.
  309.  
  310. stuff1: lodsb                           ; get next character
  311.         or      al,al                   ; is it null?
  312.         jnz     stuff2                  ; no, use it
  313.         ret                             ; yes, exit
  314.  
  315. stuff2: mov     packet+2,al             ; place ASCII code into packet
  316.  
  317.                                         ; now send this character
  318.                                         ; to the keyboard driver...
  319.         push    ds                      ; monitor output buffer address
  320.         push    offset DGROUP:monout
  321.         push    ds                      ; keyboard data packet address
  322.         push    offset DGROUP:packet    
  323.         push    pktlen                  ; data packet length
  324.         call    DosMonWrite             ; transfer to OS/2
  325.  
  326.         jmp     stuff1                  ; do another character  
  327.  
  328. stuff   endp
  329.  
  330.  
  331. signon  proc    near                    ; use pop-up window to 
  332.                                         ; display help message
  333.  
  334.         push    ds                      ; put up PopUp window
  335.         push    offset DGROUP:popflag   ; (wait until available)
  336.         push    0                       ; VIO handle
  337.         call    VioPopUp                ; transfer to OS/2
  338.  
  339.         mov     dx,offset DGROUP:msg2   ; message address
  340.         mov     cx,msg2_len             ; length
  341.         mov     ax,9                    ; Y coordinate
  342.         call    center                  ; display it
  343.  
  344.         mov     dx,offset DGROUP:msg3   ; message address
  345.         mov     cx,msg3_len             ; length
  346.         mov     ax,13                   ; Y coordinate
  347.         call    center                  ; display it
  348.  
  349.         mov     dx,offset DGROUP:msg4   ; message address
  350.         mov     cx,msg4_len             ; length
  351.         mov     ax,15                   ; Y coordinate
  352.         call    center                  ; display it
  353.  
  354.         mov     dx,offset DGROUP:msg5   ; message address
  355.         mov     cx,msg5_len             ; length
  356.         mov     ax,17                   ; Y coordinate
  357.         call    center                  ; display it
  358.  
  359.         push    0                       ; pause for 3 seconds   
  360.         push    3000                    ; (user must be quick reader!)
  361.         call    DosSleep                ; transfer to OS/2
  362.  
  363.         push    0                       ; take down PopUp window
  364.         call    VioEndPopUp             ; transfer to OS/2
  365.  
  366.         ret                             ; back to caller
  367.  
  368. signon  endp
  369.  
  370.  
  371. signoff proc    near                    ; use pop-up window to
  372.                                         ; announce exit
  373.  
  374.         push    ds                      ; put up PopUp window
  375.         push    offset DGROUP:popflag   ; (wait until available)
  376.         push    0                       ; VIO handle
  377.         call    VioPopUp                ; transfer to OS/2
  378.  
  379.         mov     dx,offset DGROUP:msg6   ; address of signoff message
  380.         mov     cx,msg6_len             ; length
  381.         mov     ax,12                   ; Y coordinate
  382.         call    center                  ; display it
  383.  
  384.         push    0                       ; pause for 1 second so
  385.         push    1000                    ; user can read message
  386.         call    DosSleep                ; transfer to OS/2
  387.  
  388.         push    0                       ; take down PopUp window
  389.         call    VioEndPopUp             ; transfer to OS/2
  390.  
  391.         ret                             ; back to caller
  392.  
  393. signoff endp
  394.  
  395.  
  396. center  proc    near                    ; center a message on screen
  397.                                         ; call DX = msg offset,
  398.                                         ; CX = length, AX = Y coordinate
  399.  
  400.         push    ds                      ; address of message
  401.         push    dx
  402.         push    cx                      ; length of message
  403.         push    ax                      ; Y
  404.         sub     cx,80                   ; X=((80-length)/2)
  405.         neg     cx                      ; to center message
  406.         shr     cx,1
  407.         push    cx
  408.         push    0                       ; VIO handle
  409.         call    VioWrtCharStr           ; transfer to OS/2
  410.         ret                             ; back to caller
  411.  
  412. center  endp
  413.  
  414. _TEXT   ends
  415.  
  416.         end     main
  417.