; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Defines constants and structures for system events of FreshLib
;
;  Target OS: Any
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________
module "System events library"


mbLeft          = 1
mbMiddle        = 2
mbRight         = 3

mbScrollUp      = 4
mbScrollDn      = 5
mbScrollLeft    = 6
mbScrollRight   = 7


maskBtnLeft   = $0100
maskBtnMiddle = $0200
maskBtnRight  = $0400
maskShift     = $1
maskCapsLock  = $2
maskCtrl      = $4
maskAlt       = $8
maskScrLk     = $10


; These are scan codes for the non character keys. They are OS dependent and should be used by
; their names.
include '%TargetOS%/keycodes.inc'


scTrack         = 1
scUp            = 2
scDown          = 3
scWheelUp       = 4
scWheelDn       = 5

scrollX = 0
scrollY = 1

cerFromUser = 0         ; user pressed X button on the window header, pressed alt+F4 from the keyboard, or choose "Close" from the title menu.
cerFromProgram = 1      ; Method Close was executed from the program.





; creates and returns a string with the textual identifier of the pressed/released key.

proc CreateKeyName, .utf8, .scancode, .kbdStatus
begin
        push    ebx ecx esi edi

        stdcall StrNew
        mov     ebx, eax

        test    [.kbdStatus], maskCtrl
        jz      @f

        stdcall StrCharCat, ebx, 'Ctrl'
        stdcall StrCharCat, ebx, '+'
@@:
        test    [.kbdStatus], maskAlt
        jz      @f
        stdcall StrCharCat, ebx, 'Alt+'
@@:
        test    [.kbdStatus], maskShift
        jz      @f
        stdcall StrCharCat, ebx, 'Shif'
        stdcall StrCharCat, ebx, 't+'
@@:
        mov     ecx, [.utf8]
        jecxz   .searchlist

        cmp     ecx, ' '
        jne     @f
        mov     ecx, 'Spc'
@@:
        cmp     ecx, ' '
        jae     @f
        add     ecx, '@'
@@:
        cmp     ecx, 'a'
        jb      @f
        cmp     ecx, 'z'
        ja      @f
        xor     ecx, $20
@@:
        stdcall StrCharCat, ebx, ecx

.exitok:
        mov     eax, ebx
        clc

.finish:
        pop     edi esi ecx ebx
        return

.searchlist:
        mov     esi, __FunctionalKeyNames

.loop:
        movzx   ecx, word [esi]
        jecxz   .notfound
        cmp     ecx, [.scancode]
        je      .found
        add     esi, 4
        jmp     .loop

.found:
        movsx   eax, word [esi+2]
        add     esi, eax
        stdcall StrCat, ebx, esi
        jmp     .exitok

.notfound:
        stdcall StrDel, ebx
        xor     eax, eax
        stc
        jmp     .finish

;.strkey db 'key', 0
endp


macro keynames lbl, [scancode, name] {
common
  label lbl word
forward
  local pname
        dw scancode
        dw pname - $ + 2
common
        dw 0
forward
  pname db name, 0
}


iglobal
  if used __FunctionalKeyNames
  keynames  __FunctionalKeyNames,                        \
            keyHomeNumpad,     'Home',                   \
            keyUpNumpad,       'Up',                     \
            keyPgUpNumpad,     'PgUp',                   \
            keyLeftNumpad,     'Left',                   \
            key5Numpad,        'Num5',                   \
            keyRightNumpad,    'Right',                  \
            keyEndNumpad,      'End',                    \
            keyDownNumpad,     'Down',                   \
            keyPgDnNumpad,     'PgDn',                   \
            keyInsNumpad,      'Ins',                    \
            keyDelNumpad,      'Del',                    \
            keyEnterNumpad,    'Enter',                  \
            keyNumLock,        'NumLock',                \
            keyScrollLock,     'ScrLock',                \
            keyPause,          'Pause',                  \
            keyLeft,           'Left',                   \
            keyRight,          'Right',                  \
            keyUp,             'Up',                     \
            keyDown,           'Down',                   \
            keyInsert,         'Ins',                    \
            keyDelete,         'Del',                    \
            keyHome,           'Home',                   \
            keyEnd,            'End',                    \
            keyPgUp,           'PgUp',                   \
            keyPgDown,         'PgDn',                   \
            keyF1,             'F1',                     \
            keyF2,             'F2',                     \
            keyF3,             'F3',                     \
            keyF4,             'F4',                     \
            keyF5,             'F5',                     \
            keyF6,             'F6',                     \
            keyF7,             'F7',                     \
            keyF8,             'F8',                     \
            keyF9,             'F9',                     \
            keyF10,            'F10',                    \
            keyF11,            'F11',                    \
            keyF12,            'F12',                    \
            keyCapsLock,       'CapsLock',               \
            keyShiftLeft,      'ShiftL',                 \
            keyCtrlLeft,       'LeftCtrl',                  \
            keyAltLeft,        'LeftAlt',                   \
            keyAltRight,       'RightAlt',                   \
            keyPopupMenu,      'PopupMenu',              \
            keyShiftRight,     'RightShift',                 \
            keyCtrlRight,      'RightCtrl',                  \
            keyBackSpace,      'BkSp'
  end if
endg

endmodule
