; Copyright (C) 2009 Jan Vorel
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.

; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

; If you have any questions, suggestions or bug reports about this product,
; contact me: info@ctuser.net

format PE GUI 4.0 on 'BIRadio.bin'
;heap 0 ; fmodex needs heap!
entry start
include '..\..\common\fasmw\win32a.inc' ; x for invoke createfilea...

section '.text' code import data readable writeable executable

  library kernel, 'KERNEL32.DLL',\
    user,'USER32.DLL',\
    advapi,'ADVAPI32.DLL',\
    version,'VERSION.DLL',\
    shell,'SHELL32.DLL',\
    comctl,'COMCTL32.DLL',\
    fmodex,'FMODEX.DLL'

  import kernel,\
	 kExitProcess,'ExitProcess',\
	 kFreeLibrary,'FreeLibrary',\
	 kFindResourceA,'FindResourceA',\
	 kGetModuleFileNameA,'GetModuleFileNameA',\
	 kGetModuleFileNameW,'GetModuleFileNameW',\
	 kGetModuleHandleA,'GetModuleHandleA',\
	 kGetTickCount,'GetTickCount',\
	 kGetVersion,'GetVersion',\
	 kGlobalAlloc,'GlobalAlloc',\
	 kGlobalFree,'GlobalFree',\
	 kGlobalLock,'GlobalLock',\
	 kGlobalUnlock,'GlobalUnlock',\
	 kLoadLibraryA,'LoadLibraryA',\
	 kLoadResource,'LoadResource',\
	 kLocalAlloc,'LocalAlloc',\
	 kLocalFree,'LocalFree',\
	 kLockResource,'LockResource',\
	 kMultiByteToWideChar,'MultiByteToWideChar',\
	 kRtlMoveMemory,'RtlMoveMemory',\
	 kSleep,'Sleep',\
	 klstrcpyA,'lstrcpyA',\
	 klstrlenA,'lstrlenA'

;         uDefWindowProcA,'DefWindowProcA',\

  import user,\
	 uCloseClipboard,'CloseClipboard',\
	 uCreateDialogIndirectParamA,'CreateDialogIndirectParamA',\
	 uCreateDialogIndirectParamW,'CreateDialogIndirectParamW',\
	 uDialogBoxIndirectParamA,'DialogBoxIndirectParamA',\
	 uDialogBoxIndirectParamW,'DialogBoxIndirectParamW',\
	 uDestroyWindow,'DestroyWindow',\
	 uDispatchMessageA,'DispatchMessageA',\
	 uDispatchMessageW,'DispatchMessageW',\
	 uEmptyClipboard,'EmptyClipboard',\
	 uEnableWindow,'EnableWindow',\
	 uEndDialog,'EndDialog',\
	 uGetDlgItem,'GetDlgItem',\
	 uGetMessageA,'GetMessageA',\
	 uGetMessageW,'GetMessageW',\
	 uIsDialogMessageA,'IsDialogMessageA',\
	 uIsDialogMessageW,'IsDialogMessageW',\
	 uKillTimer,'KillTimer',\
	 uLoadImageA,'LoadImageA',\
	 uLoadStringA,'LoadStringA',\
	 uLoadStringW,'LoadStringW',\
	 uMessageBoxA,'MessageBoxA',\
	 uMessageBoxW,'MessageBoxW',\
	 uOpenClipboard,'OpenClipboard',\
	 uPostQuitMessage,'PostQuitMessage',\
	 uSendMessageA,'SendMessageA',\
	 uSetClipboardData,'SetClipboardData',\
	 uSetDlgItemTextA,'SetDlgItemTextA',\
	 uSetDlgItemTextW,'SetDlgItemTextW',\
	 uSetTimer,'SetTimer',\
	 uSetWindowPos,'SetWindowPos',\
	 uSetWindowTextA,'SetWindowTextA',\
	 uShowWindow,'ShowWindow',\
	 uTranslateMessage,'TranslateMessage'

;         uSetWindowTextW,'SetWindowTextW',\
;        SetWindowTextA only for debug stuff

  import advapi,\
	 aRegCloseKey,'RegCloseKey',\
	 aRegCreateKeyA,'RegCreateKeyA',\
	 aRegOpenKeyA,'RegOpenKeyA',\
	 aRegQueryValueExA,'RegQueryValueExA',\
	 aRegSetValueExA,'RegSetValueExA'

  import version,\
	 vGetFileVersionInfoA,'GetFileVersionInfoA',\
	 vGetFileVersionInfoSizeA,'GetFileVersionInfoSizeA',\
	 vGetFileVersionInfoSizeW,'GetFileVersionInfoSizeW',\
	 vGetFileVersionInfoW,'GetFileVersionInfoW',\
	 vVerQueryValueA,'VerQueryValueA',\
	 vVerQueryValueW,'VerQueryValueW'

  import shell,\
	 sShellExecuteA,'ShellExecuteA'

  import comctl,\
	 cInitCommonControls,'InitCommonControls'

;    FMOD_Debug_SetLevel,'FMOD_Debug_SetLevel',\
  import fmodex,\
    FMOD_Channel_SetVolume,'FMOD_Channel_SetVolume',\
    FMOD_Channel_Stop,'FMOD_Channel_Stop',\
    FMOD_Sound_GetOpenState,'FMOD_Sound_GetOpenState',\
    FMOD_Sound_Release,'FMOD_Sound_Release',\
    FMOD_System_Close,'FMOD_System_Close',\
    FMOD_System_Create,'FMOD_System_Create',\
    FMOD_System_CreateStream,'FMOD_System_CreateStream',\
    FMOD_System_GetVersion,'FMOD_System_GetVersion',\
    FMOD_System_Init,'FMOD_System_Init',\
    FMOD_System_PlaySound,'FMOD_System_PlaySound',\
    FMOD_System_Release,'FMOD_System_Release'

shell32lnk:
  db 'shell32.dll', 0



start:
  push ebx

  push dword shell32lnk
  call dword [kLoadLibraryA]
  push eax
  call dword [cInitCommonControls]
  call dword [kFreeLibrary]

  push dword FModSystem
  call dword [FMOD_System_Create]
  or eax, eax
  jnz NoFMODCreate

  xor ebx, ebx

  push dword ebx
  push dword ebx ;FMOD_INIT_NORMAL
  push dword 1
  push dword [FModSystem]
  call dword [FMOD_System_Init]
  or eax, eax
  jnz NoFModInit

  push dword 1
  call dword [kSleep]

;  call PlayItNowEx



  mov eax, dword 1
  call LoadResEx_DataXX
  or eax, eax
  jz NoDialog

  push ebx
  push dialogproc
  push ebx
  push eax

  push ebx
  call dword [kGetModuleHandleA]
  push eax


  mov ecx, $ff
  mov edx, dword 10

  push ecx
  push SenderURL
  push edx
  push eax

  push ecx
  push KeyPathBr
  inc edx
  push edx
  push eax

  call [uLoadStringA]
  or eax, eax
  jnz NoDefBrand

  push dword KeyPathDefBr
  push dword KeyPathBr
  call dword [klstrcpyA]

NoDefBrand:
  call [uLoadStringA]
  or eax, eax
  jz NoURL




push eax
mov eax, esp
push eax
push dword KeyPath
push dword $80000001
call dword [aRegOpenKeyA]
or eax, eax
pop eax
jnz NoKey1

push eax ; for closekey
  sub esp, $c
  mov ecx, esp
  mov [ecx], dword 4

  push ecx ;size
  add ecx, 4
  push ecx
  add ecx, 4
  push ecx

  push ebx
  push dword KeyName
  push eax
  call dword [aRegQueryValueExA]
  or eax, eax
  pop edx ; type
  pop ecx ; data
  pop eax ; size
  jnz NoKeyUsed
  cmp edx, eax
  jne NoKeyUsed
  cmp edx, dword 4
  jne NoKeyUsed

  cmp ecx, ebx
  jb NoKeyUsed
  cmp ecx, 100
  ja NoKeyUsed

  mov [Vol256], ecx


NoKeyUsed:
  call dword [aRegCloseKey]
NoKey1:






;jmp NoURL
  call PlayItNowEx



  call [kGetVersion]
  and eax, $ff
  cmp eax, $04
  jnz UnicodeMsgStuff1

;--- Ansi ---
  call dword [uCreateDialogIndirectParamA]
  jmp Shared1

; --- Wide ---
UnicodeMsgStuff1:
  mov [IsUnicode], al
  call dword [uCreateDialogIndirectParamW]
;  jmp Shared1

Shared1:
  or eax, eax
  jz NoDialog

  push edi
  mov edi, eax

  push ebx
  push 100 ;ms!
  push ebx
  push eax




  call dword [uSetTimer]
  or eax, eax
  jz NoTimer

;  mov [hTimer], eax


  push ebp


  push ebx
  call dword [kGetModuleHandleA]
  mov ebp, eax


  push 16
  call LoadIcon1
  push 32
  call LoadIcon1

  push dword $27 ; SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED
  sub esp, $14
  push edi
  call dword [uSetWindowPos]





  push edi
  mov edi, lblMsgBox

  cmp [IsUnicode], byte 0
  jne LoadStringsWLoop

;--- Ansi ---
LoadStringsALoop:
  inc ebx
  push dword $ff
  push edi
  push ebx
  push ebp
  call [uLoadStringA]
  add edi, $100
  cmp edi, lblFinal
  jne LoadStringsALoop
  add eax, lbldebugbuffer
  mov [lblbufferi], eax
  pop edi

  xor ebx, ebx
  mov ebp, esp
  sub esp, $1c

MessageLoopA:
;while (GetMessage(&msg, (HWND) NULL, 0, 0))
  push ebx
  push ebx
  push ebx
  push ebp
  call dword [uGetMessageA]
  or eax, eax
  jz ExitMessageLoopA

;    IsDialogMessage(hwndDlgModeless, &msg)
  push ebp
  push edi
  call dword [uIsDialogMessageA]
  or eax, eax
;  jnz NoDispatch
  jnz MessageLoopA

;        TranslateMessage(&msg);
  push ebp
  call dword [uTranslateMessage]
;        DispatchMessage(&msg);
  push ebp
  call dword [uDispatchMessageA]
;NoDispatch:
  jmp MessageLoopA
;    }

; --- wide ---
LoadStringsWLoop:
  inc ebx
  push dword $7f ;  !!!!!!!!!!! unicode!
  push edi
  push ebx
  push ebp
  call [uLoadStringW]
  add edi, $100
  cmp edi, lblFinal
  jne LoadStringsWLoop
  shl eax, 1 ; x 2  !!!!!!!!!!! unicode!
  add eax, lbldebugbuffer
  mov [lblbufferi], eax
  pop edi

  xor ebx, ebx
  mov ebp, esp
  sub esp, $1c

MessageLoopW:
;while (GetMessage(&msg, (HWND) NULL, 0, 0))
  push ebx
  push ebx
  push ebx
  push ebp
  call dword [uGetMessageW]
  or eax, eax
  jz ExitMessageLoopW

;    IsDialogMessage(hwndDlgModeless, &msg)
  push ebp
  push edi
  call dword [uIsDialogMessageW]
  or eax, eax
;  jnz NoDispatch
  jnz MessageLoopW

;        TranslateMessage(&msg);
  push ebp
  call dword [uTranslateMessage]
;        DispatchMessage(&msg);
  push ebp
  call dword [uDispatchMessageW]
;NoDispatch:
  jmp MessageLoopW
;    }

; --- shared ---










LoadIcon1: ;
  mov eax, [esp + $4]
  push ebx
  push eax
  push eax
  mov eax, 1
  push eax
  push eax
  push ebp
  call dword [uLoadImageA]
  or eax, eax
  jz DoneIcon1

  mov ecx, [esp + 4]
  push eax
  shr ecx, 5
  push ecx
  push $80
  push edi
  call dword [uSendMessageA]

DoneIcon1:
  ret 4








NoURL:
;NoURL
  add esp, $14
  jmp NoDialog
;  pop ebp
;  jmp NoURL2




ExitMessageLoopA:
ExitMessageLoopW:
  add esp, $1c
  pop ebp


;NoURL2:
  push ebx
  push edi
  call dword [uKillTimer]


NoTimer:
  call StopItNow



  push edi
  call dword [uDestroyWindow]
  pop edi

  push dword 100
  call dword [kSleep]

  push dword [FModSystem]
  call dword [FMOD_System_Close]

NoFModInit:
  push dword 100
  call dword [kSleep]

  push dword [FModSystem]
  call dword [FMOD_System_Release]

NoFMODCreate:

;cmp [KeyPathBr], byte 0
;je NoKey2
push eax
mov eax, esp
push eax
push dword KeyPath
push dword $80000001
call dword [aRegCreateKeyA]
or eax, eax
pop eax
jnz NoKey2

push eax ; for closekey
  mov ecx, dword 4
  push dword ecx
  push dword Vol256
  push dword ecx
  push dword ebx
  push dword KeyName
  push eax
  call dword [aRegSetValueExA]

call dword [aRegCloseKey]


NoKey2:
NoDialog:
  pop ebx
  push dword 0
  call dword [kExitProcess]
  ret $c


KeyPathDefBr:
  db 'ctuser\BIRadio', 0

KeyPath:
  db 'Software\'
KeyPathBr:
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
KeyName:
  db 'Volume', 0

proc dialogproc
  xor eax, eax
  mov ecx, [esp + 8]

  cmp ecx, $113
  je lbl_WM_TIMER
  cmp ecx, $115
  je lbl_WM_VSCROLL
  cmp ecx, $111
  je lbl_WM_COMMAND
  cmp ecx, $10
  je lbl_WM_QUIT
  cmp ecx, $110
  je lbl_WM_INITDIALOG
  ret $10

lbl_WM_VSCROLL:
  cmp [FModGo], byte 0
  je NoQuitNow

  mov eax, [esp + $10]
  or eax, eax
  jz NoScroll
  cmp eax, [VolHWnd]
  jne NoScroll
  mov ax, [esp + $e]
  and eax, $000000ff
  mov [Vol256], eax

  call SetVolume
  jmp ApplyVolume

lbl_WM_INITDIALOG:
  push dword 3
  push dword [esp + 8]
  call dword [uGetDlgItem]
  mov [VolMute], eax

  push dword 5
  push dword [esp + 8]
  call dword [uGetDlgItem]
  mov [VolHWnd], eax

  push dword 7
  push dword [esp + 8]
  call dword [uGetDlgItem]
  mov [ReConHWnd], eax

  push dword 9
  push dword [esp + 8]
  call dword [uGetDlgItem]
  mov [QuitHWnd], eax

  call EnableInterface
  mov eax, [VolHWnd]

  or eax, eax
  jz NoInitUpDown

  xor ecx, ecx
  mov edx, [Vol256]
;  shl edx, 16
  push edx
  push ecx
  push dword $467
  push eax

  push dword 100 ; $ff
  push dword ecx ;0
  push dword $465
  push eax

  call dword [uSendMessageA]
  call dword [uSendMessageA]

NoInitUpDown:
;  push dword 16
;  call LoadIcon1
;  push dword 32
;  call LoadIcon1

ApplyVolume:
  mov eax, [Vol256]
;  mov cl, 100
;  mul cl
;  mov cl, 255
;  div cl
;  and eax, $000000ff

  call dword EaxToPerc
  mov [VolTxt], eax
  push dword VolTxt
  push dword 4
  jmp PushAndGo2
  mov eax, 1
  ret $10

lbl_WM_COMMAND:
  mov ecx, [esp + $c]
  cmp ecx, 8
  je wmInfo

  cmp [FModGo], byte 0
  je NoQuitNow

  cmp ecx, 3
  je wmMute
  cmp ecx, 7
  je wmReConnect
  cmp ecx, 9
  je lbl_WM_QUIT
  jmp DoneMsg
;  jmp dword [uDefWindowProcA]
;  ret $10



wmMute:
  mov al, [Muted]
  neg al
  inc al
  mov [Muted], al
  call SetVolume
  xor eax, eax
  ret $10




wmReConnect:
  cmp [FModGo], byte 0
  je NoQuitNow
  call StopItNow
  call PlayItNowEx
  jmp DoneMsg
;NoScroll:
;  xor eax, eax
;  ret $10

wmInfo:
  mov eax, dword 2
  call LoadResEx_DataXX
  or eax, eax
  jz NoInfo

  xor ecx, ecx
  push ecx
  push dword InfoDialogProc
  push dword [esp + $c]
  push eax

  push ecx
  call dword [kGetModuleHandleA]
  push eax

  cmp [IsUnicode], byte 0
  jne InfoDialogW

  call dword [uDialogBoxIndirectParamA]
;  jmp InfoDialogS1
  jmp DoneMsg

InfoDialogW:
  call dword [uDialogBoxIndirectParamW]
  jmp DoneMsg
;InfoDialogS1
;NoInfo:
;  jmp DoneMsg


InfoDialogProc:
  xor eax, eax
  mov ecx, [esp + 8]

  cmp ecx, $111
  je InfoCmd
  cmp ecx, $10
  je InfoQuit
  cmp ecx, $110
  je InfoInit
  ret $10

InfoInit:
  call GetSelfExeFile
  or eax, eax
  jz FailVer

  push Var_QueryValue_Result
  push Dat_GetBuildNumber_FinalMode
  push Var_SelfExeFile
  call VerQueryValue
  or eax, eax
  jz FailVer

  push Var_QueryValue_Result
  push dword 7
  push dword [esp + $c]
  cmp byte [IsUnicode], 0
  jne VerWX
  call dword [uSetDlgItemTextA]
  jmp FailVer

VerWX:
  call dword [uSetDlgItemTextW]

FailVer:
; ecx
;  cl = counter
; eax = value
; edx = flags / stuff
; ebx = mem




  push ebx
  sub esp, 12

  push eax
  mov eax, esp
  push eax
  push dword [FModSystem]
  call dword [FMOD_System_GetVersion]
  or eax, eax
  pop eax
  jnz VerFFail

mov ebx, esp
mov cl, $04
UIntToHexIPStr_NoPreNullsLoop:
rol eax, 8
cmp cl, 1
je UIntToHexIPStr_NoPreNullsDone
;;;xor cl, cl
UIntToHexIPStr_PreNulls:
or al, al
jnz UIntToHexIPStr_NoPreNullsDoneX
dec cl
jnz UIntToHexIPStr_NoPreNullsLoop
UIntToHexIPStr_NoPreNullsDoneX:
UIntToHexIPStr_NoPreNullsDone:
ror eax, 8
UIntToHexIPStr_Loop:
rol eax, 8
mov dh, al
and dh, $f0
cmp ebx, esp
jne UIntToHexIPStr_NoStripFieldNull
or dh, dh
jz UIntToHexIPStr_LowValOnly
UIntToHexIPStr_NoStripFieldNull:
sub al, dh
shr dh, 4
call near UIntToHexIPStr_HexDH
UIntToHexIPStr_LowValOnly:
mov dh, al
call near UIntToHexIPStr_HexDH
mov [ebx], byte $2e
inc ebx
dec cl
jnz UIntToHexIPStr_Loop
dec ebx
mov [ebx], byte 0

mov eax, esp
;mov eax, Dat_GetBuildNumber_FinalMode
  push eax
  push dword 5
  push dword [esp + $1c]
  call dword [uSetDlgItemTextA]


VerFFail:
  add esp, 12
  pop ebx
  mov eax, dword 1
  ret $10






UIntToHexIPStr_HexDH:
cmp dh, 9
jna UIntToHexIPStr_HexDHNoHex
add dh, 7
UIntToHexIPStr_HexDHNoHex:
add dh, $30
mov [ebx], dh
inc ebx
ret


InfoCmd:
  mov ecx, [esp + $c]
  cmp ecx, 9
  je InfoQuit
  ja DoneMsg
  cmp ecx, 0
  jb DoneMsg







  and cl, 7
  add cl, 20
  push ebx
  mov ebx, ecx

  push dword $102
  push dword 2
  call dword [kGlobalAlloc]
  or eax, eax
  jz NoGAllocCB

  push eax
  push eax
  call dword [kGlobalLock] ; -> post: *Mem, *Mem
  or eax, eax
  jz NoGLockCB
  push eax ; *MemAcc, *Mem, *Mem

  push dword $102
  push eax
  push ebx
  push dword 0
  call dword [kGetModuleHandleA]
  push eax
  call dword [uLoadStringA]
; *MemAcc, *Mem, *Mem
  or eax, eax
  jz NoGCopy






  mov eax, [esp] ;*MemAcc
  push dword 5
  xor ecx, ecx
  push ecx
  push ecx
  push eax
  push ecx
  push dword [esp + $24]
  call dword [sShellExecuteA]
  mov ebx, eax
  call dword [kGlobalUnlock]
  cmp ebx, 32
  ja SExecOK
  test ebx, $80000000
  jnz SExecOK






  push dword $21; 4
  push dword 0
  push dword lblMsgBox
  push dword [esp + $18]
  mov eax, [uMessageBoxA]
  cmp [IsUnicode], byte 0
  jz URLErrorA
  mov eax, [uMessageBoxW]
URLErrorA:
  call eax
  cmp eax, 1 ;6
  jne NoCopyClip

  push dword [esp + $c]
  call dword [uOpenClipboard]
  or eax, eax
  jz NoClipOpen

  call dword [uEmptyClipboard]
  push dword 1
  call dword [uSetClipboardData]
  call dword [uCloseClipboard]
  jmp ClipBoardSet

NoGCopy:
  call dword [kGlobalUnlock]
NoCopyClip:
NoGLockCB:
NoClipOpen:
SExecOK:
  call dword [kGlobalFree]
ClipBoardSet:
NoGAllocCB:
  pop ebx


  jmp DoneMsg



InfoQuit:
  push eax
  push dword [esp + 8]
  call dword [uEndDialog]
  jmp DoneMsg




lbl_WM_QUIT:
  cmp [FModGo], byte 0
  je NoQuitNow
  push dword 0
  call dword [uPostQuitMessage]
  jmp DoneMsg
;  xor eax, eax
;  ret $10

lbl_WM_TIMER:
  mov edx, [FModStream]
  or edx, edx
  jz GetStateFailed

  push edx
  push edx
  push edx
;  sub esp, $c
  mov eax, esp
  push eax
  add eax, 4
  push eax
  add eax, 4
  push eax
  push edx
  call dword [FMOD_Sound_GetOpenState]
;  or eax, eax
;  jnz GetStateFailedEx
  pop edx ; stall
  pop eax ; buffer
  pop ecx ; openstate
  cmp ecx, 1
  je GetStateConn
  cmp ecx, 2
  je GetStateErr
  cmp ecx, 3
  je GetStateConn

  cmp [FModGo], byte 0
  je DoUnblock

  xor cl, cl
  cmp [FModMt], cl
  jz NoUnmute

  push eax
  push edx
  mov [FModMt], cl
  call SetVolume
  pop edx
  pop eax

NoUnmute:
  cmp [lastbuff], al
  jne BufferMisMatch

;  Not
;  or edx, edx
;  jz NoStall

  push eax
  call dword [kGetTickCount]
  sub eax, dword [laststall]
  cmp eax, 5000
  pop eax
  ja GetStateSelf
  jmp Inter1




DoUnblock:
  push eax
  cmp ecx, 0
  jne NoStallUnBlock

  call PLayPostBlock
  cmp [FModChannel], dword 0
  jne NoStallUnBlock
  pop eax
  jmp PostUnblockError






BufferMisMatch:
  mov [lastbuff], al
NoStall:
  push eax
NoStallUnBlock:
  call dword [kGetTickCount]
  mov [laststall], eax
  pop eax
Inter1:

  call dword EaxToPerc
  mov ecx, [lblbufferi]
  push dword lbldebugbuffer

  cmp [IsUnicode], byte 0
  jne PushAndGoW
  mov [ecx], eax
  jmp PushAndGoNOS

PushAndGo:
  cmp [IsUnicode], byte 0
  jne PushAndGoWNOS

PushAndGoNOS:
  push dword 1

PushAndGo2:
;  push dword [esp+$c]
  push dword [esp + $c]
  call dword [uSetDlgItemTextA]
;GetStateFailedEx2:
  jmp DoneMsg

;GetStateFailedEx:
;  add esp, $C
;  jmp GetStateFailedEx2

PushAndGoW:
  mov [ecx], al
  mov [ecx + 2], ah
  shr eax, 16
  mov [ecx + 4], al
  mov [ecx + 6], ah
PushAndGoWNOS:
  push dword 1
  push dword [esp + $c]
  call dword [uSetDlgItemTextW]
DoneMsg:
NoScroll:
NoQuitNow:
NoInfo:
  xor eax, eax
  ret $10

GetStateConn:
  push lbldebugconn
  jmp PushAndGo

GetStateSelf:
GetStateErr:
PostUnblockError:
;include 'BIRadio.inc'

  call StopItNow
  call PlayItNow
  push lbldebugerror
  jmp PushAndGo

GetStateFailed:
;  call StopItNow
  call PlayItNow
  push lbldebugdis
  jmp PushAndGo


lblMsgBox:
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
lbldebugdis:
;  db 'disconnected', 0
; 256 bytes -> 127/255
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
lbldebugerror:
;  db 'error', 0
; 256 bytes
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
lbldebugconn:
;  db 'connecting', 0
; 256 bytes
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

lbldebugbuffer:
;  db 'Buffer: '
; 256 bytes + 4
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
lblFinal:
  dd 0

lblbufferi:
  dd 0
endp

proc SetVolume
  mov ecx, [FModChannel]
  or ecx, ecx
  jz NoChannelVol


;  cmp byte [Muted], 0
;  jne IsMuted
;  cmp byte [FModMt], 0
;  jne IsMuted
  cmp word [FModMt_Muted], 0
  jne IsMuted

  fild dword [Vol256]
DoneIsMuted:


  push eax
  mov eax, esp

;  fdiv dword [Flt_255]
  fdiv dword [Flt_100]
  fstp dword [eax]
;  push eax
  push ecx
  call dword [FMOD_Channel_SetVolume]



NoChannelVol:
;  pop eax
  ret

IsMuted:
  fild dword [Vol0]
  jmp DoneIsMuted
endp

;Flt_255:
;  dd $437F0000
Flt_100:
  dd $42C80000


proc EaxToPerc
  cmp eax, 99
  ja perc100
  and eax, $0000ffff
  mov cl, 10
  div cl
  or al, al
  jz below10percent
  add eax, $253030
  ret

below10percent:
  shr ax, 8
  add eax, $2530
  ret

perc100:
  mov eax, dword $25303031
  ret
endp





proc PlayItNow
  call dword [kGetTickCount]
  sub eax, [lasttry]
  cmp eax, 2000
  jb NoDoNow

PlayItNowEx:
  push dword FModStream
  push dword 0 ; exinfo
;  push dword $C8
  push dword $100c8
  push dword SenderURL
  push dword [FModSystem]
  call dword [FMOD_System_CreateStream]
  or eax, eax
  jnz FModNoStream

  mov [FModGo], al
  call EnableInterface
  ret


PLayPostBlock:
  mov [FModGo_FmodMt], word $0101
;  mov [FModGo], byte 1
;  mov [FModMt], byte 1
  call EnableInterface

  push dword FModChannel
  push dword 0 ;not paused
  push dword [FModStream]
  push dword $ffffffff ; FMOD_CHANNEL_FREE = -1
  push dword [FModSystem]
  call dword [FMOD_System_PlaySound]
  or eax, eax
  jnz FModNoChannel

  Call SetVolume

  push dword 1
  call dword [kSleep]
  ret

StopItNow:
  cmp [FModGo], byte 1
  je NoEnable
  mov [FModGo], byte 1
  call EnableInterface

NoEnable:
  mov eax, [FModChannel]
  or eax, eax
  jz FModNoChannel
  push eax

  push dword 0
  push eax
  call dword [FMOD_Channel_SetVolume]



;StopItNowEx:
  call dword [FMOD_Channel_Stop]

  push dword 1
  call dword [kSleep]

FModNoChannel:
  mov [FModChannel], dword 0
  push dword [FModStream]
  call dword [FMOD_Sound_Release]

  push dword 1
  call dword [kSleep]

FModNoStream:
  mov [FModStream], dword 0
FModNoUnload:

  call dword [kGetTickCount]
  mov [lasttry], eax

NoDoNow:
  ret
endp

proc EnableInterface
  push ebx
  movzx ebx, byte [FModGo]
  push edi
  mov edi, EIE
  mov eax, dword [VolMute]
  call edi
  mov eax, dword [VolHWnd]
  call edi
  mov eax, dword [ReConHWnd]
  call edi
  mov eax, dword [QuitHWnd]
  call edi
  pop edi
  pop ebx
  ret

EIE:
  or eax, eax
  jz NoEIE
  push ebx
  push eax
  call dword [uEnableWindow]
NoEIE:
  ret
endp

proc LoadResEx_DataXX
  push ebx
  xor ebx, ebx
  push dword 5
  push eax ;dword 1 / 2
  push ebx
  call dword [kFindResourceA]
  or eax, eax ; xlong1
  jz LoadResEx_Data_Fail

  push eax
  push ebx
  call dword [kLoadResource]
  or eax, eax
  jz LoadResEx_Data_Fail
  push eax
  call dword [kLockResource]

LoadResEx_Data_Fail:
  pop ebx
  ret
endp

include 'gen_VersionInfoQueryValue.fasm'
include 'gen_GetSelfExeFile.fasm'


Dat_GetBuildNumber_FinalMode:
  db '\StringFileInfo\040904B0\ProductVersion', 0


Var_QueryValue_Result:
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; $  40 (16 * 4 = 64)
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; $  80
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; $  C0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ; $ 100




  SenderURL:
  ; 256 bytes!
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

;  hTimer:
;    dd 0
  FModSystem:
    dd 0
  FModStream:
    dd 0
  FModChannel:
    dd 0
FModGo_FmodMt:
  FModGo:
    db 1
FModMt_Muted:
  FModMt:
    db 0
  Muted:
    db 0

  IsUnicode:
    db 0

  Vol256:
;    dd 200
    dd 75

  VolMute:
    dd 0
  VolHWnd:
    dd 0
  VolTxt:
    dd 0
;    db 0 ;!!!!!!
  Vol0:   ;!!!!!!
    dd 0

  ReConHWnd:
    dd 0
  QuitHWnd:
    dd 0


  laststall:
    dd 0
  lastbuff:
    db 0
  lasttry:
    dd 0

section '.rsrc' data readable resource from 'BIRadio.res'
