; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: SimpleDebug library.
;
;  Target OS: Linux
;
;  Dependencies: Not dependent on other libraries.
;
;  Notes:
;_________________________________________________________________________________________

DBG_CHAR_NEWLINE equ $0a



proc Output, .ptrStr
begin
        pushad

        stdcall __simpStrLength, [.ptrStr]
        mov     edx, ecx

        mov     eax, 4
        mov     ebx, 2
        mov     ecx, [.ptrStr]
        int     $80

        popad
        return
endp



proc GetTimestamp
begin
        pushad
        mov     eax, $4e        ; sys_gettimeofday
        mov     ebx, _timestamp
        xor     ecx, ecx
        int     $80

        mov     eax,dword [_timestamp]  ; seconds
        mov     ecx,1000
        mul     ecx
        mov     ebx,eax
        mov     eax,dword [_timestamp+4] ; us
        div     ecx
        add     eax, ebx

        mov     [esp+4*regEAX], eax
        popad
        return

endp


; returns dword timestamp in uS

proc GetFineTimestamp
begin
        pushad
        mov     eax, $4e        ; sys_gettimeofday
        mov     ebx, _timestamp
        xor     ecx, ecx
        int     $80

        mov     eax,dword [_timestamp]  ; seconds
        mov     ecx,1000000
        mul     ecx
        mov     ebx,eax
        mov     eax,dword [_timestamp+4]
        add     eax, ebx

        mov     [esp+4*regEAX], eax
        popad
        return

endp


uglobal
  if used _timestamp
   _timestamp dq 0
  end if
endg


macro OutputEvent event {
if defined options.DebugMode & options.DebugMode
        stdcall __OutputEvent, event
end if
}

proc __OutputEvent, .pEvent
begin
        pushad
        stdcall Output, cEventLabel
        mov     esi, [.pEvent]
        mov     eax, [esi+XEvent.type]
        cmp     eax, 2
        jl      .bynumber
        cmp     eax, 35
        jg      .bynumber

        sub     eax, 2
        mov     eax, [__EventStrings+4*eax]
        stdcall Output, eax
        stdcall Output, cEventLabel1

.window:
        mov     eax, [esi+XEvent.type]
        stdcall OutputNumber, eax, 10, 3
        stdcall Output, cEventLabel2
        stdcall OutputNumber, [esi+XEvent.window], 16, 8
        stdcall Output, cEventLabel3

        popad
        return

.bynumber:
        stdcall OutputNumber, eax, 10, 3
        jmp     .window

endp

if used __OutputEvent
  cEventLabel db 'X event: "', 0
  cEventLabel1 db ' (', 0
  cEventLabel2 db ')" for window: $', 0
  cEventLabel3 db 10, 0

  ; From number 2..35
  __EventStrings:
  StringTable              \
  `KeyPress, `KeyRelease, `ButtonPress, `ButtonRelease, `MotionNotify, `EnterNotify, \
  `LeaveNotify, `FocusIn, `FocusOut, `KeymapNotify, `Expose, `GraphicsExpose, `NoExpose, \
  `VisibilityNotify, `CreateNotify, `DestroyNotify, `UnmapNotify, `MapNotify, `MapRequest, \
  `ReparentNotify, `ConfigureNotify, `ConfigureRequest, `GravityNotify, `ResizeRequest,    \
  `CirculateNotify, `CirculateRequest, `PropertyNotify, `SelectionClear, `SelectionRequest, \
  `SelectionNotify, `ColormapNotify, `ClientMessage, `MappingNotify, `LASTEvent
end if