; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Provides unified access to standard mouse cursors.
;
;  Target OS: Linux
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________

iglobal
  if used _Xcursors
    _Xcursors db 68, 152, 34, 108, 116, 40, 40, 150, 58, 52, 86   ; X standard cursor font
  end if
endg


uglobal
  if used StockCursors
    StockCursors rd mcCount
  end if
endg


proc InitMouseCursors
begin
        push    eax ecx edx ebx
        mov     ebx, mcCount-1

.create_cursor_loop:
        movzx   eax, [_Xcursors+ebx]
        cinvoke XCreateFontCursor, [hApplicationDisplay], eax
        mov     [StockCursors+4*ebx], eax

        dec     ebx
        jns     .create_cursor_loop
        pop     ebx edx ecx eax
        return
endp



if used InitMouseCursors
finalize FinalizeMouseCursors
begin
        mov     ebx, mcCount-1
        cmp     [hApplicationDisplay], 0
        je      .exit

.free_cursor_loop:
        cinvoke XFreeCursor, [hApplicationDisplay], [StockCursors+4*ebx]
        dec     ebx
        jns     .free_cursor_loop

.exit:
        return
endp
end if


proc SetMouseCursor, .hWindow, .hCursor
begin
        push    eax ecx edx
        cinvoke XDefineCursor, [hApplicationDisplay], [.hWindow], [.hCursor]
        pop     edx ecx eax
        return
endp



proc GetStockCursor, .index
begin
        mov     eax, [.index]
        cmp     eax, mcCount
        jb      @f
        xor     eax, eax
@@:
        mov     eax, [StockCursors+4*eax]
        return
endp



proc __MouseCapture, .hwnd
begin
        push    eax ecx edx
        cmp     [.hwnd], 0
        je      .release

        cinvoke XGrabPointer, [hApplicationDisplay], [.hwnd], FALSE, PointerMotionMask or ButtonPressMask or ButtonReleaseMask, \
                              GrabModeAsync, GrabModeAsync, None, None, CurrentTime

.finish:
        pop     edx ecx eax
        return

.release:
        cinvoke XUngrabPointer, [hApplicationDisplay], CurrentTime
        jmp     .finish
endp

