File "vlcrun.bas"

Path: /vlcrun/vlcrun.bas
File size: 5.89 KB
MIME-type:
Charset: utf-8

#COMPILE EXE "vlcrun.exe"
#RESOURCE "vlcrun.pbr"

MACRO MyMsgBox(hd,m,t,st) = MessageBox(hd,m,t,st)

#INCLUDE ONCE "Win32Api.inc"
#INCLUDE ONCE "inc/Registry.inc"
#INCLUDE ONCE "inc/Ini.inc"
#INCLUDE ONCE "inc/Exe2Unix.inc"

$VER = "1.2"

'-------------------------------------------------------------------------------------------
' v1.2 (2025-10-10)
' - improved VLC detection
' - improved behavior in Linux, display exe icon in Nautilus
' v1.1 (2025-08-27)
' - vlcrun can now launch native vlc in Linux by specifying its unix path e.g. /usr/bin/vlc
' v1.0 (2025-07-03)
' - Initial release: support Windows and Linux/Wine/VLC Portable
'-------------------------------------------------------------------------------------------

'-------------------------------------------------------------------------------------------
GLOBAL g_vlcpath AS STRING
GLOBAL g_inifile AS STRING
GLOBAL g_onunix AS LONG
'-------------------------------------------------------------------------------------------

'-------------------------------------------------------------------------------------------
SUB AutoDetectVlc()
    LOCAL e AS STRING

    ' Specific if we run from Unix/Wine
    IF g_onunix THEN
        LET g_vlcpath = GetUnixRet("whereis vlc")
        IF LEN(g_vlcpath) THEN
            ' Parse the vlc path from the string "vlc: /usr/bin/vlc"
            g_vlcpath = TRIM$(MID$(g_vlcpath, INSTR(g_vlcpath,":") + 1))
        END IF
        EXIT SUB
    END IF

    ' Run from Windows: get VLC path from registry
    LET g_vlcpath = GETREGVALUE(%HKEY_LOCAL_MACHINE, "SOFTWARE\VideoLAN\VLC", "")
    IF g_vlcpath <> "" THEN EXIT SUB

    ' Not found in default key > try another key
    LET g_vlcpath = GETREGVALUE(%HKEY_LOCAL_MACHINE, "SOFTWARE\VideoLAN\VLC", "InstallDir")
    IF g_vlcpath <> "" THEN g_vlcpath += "\vlc.exe" : EXIT SUB

    ' Not found via registry > try a manual method
    g_vlcpath = "C:\Program Files (x86)\VideoLAN\VLC\"
    e = DIR$(g_vlcpath + "vlc.exe") : DIR$ CLOSE
    IF e <> "" THEN g_vlcpath += e : EXIT SUB

    ' Last attempt...
    g_vlcpath = "C:\Program Files\VideoLAN\VLC\"
    e = DIR$(g_vlcpath + "vlc.exe") : DIR$ CLOSE
    IF e <> "" THEN g_vlcpath += e : EXIT SUB

    ' Definitely not found :(
    g_vlcpath = ""

END SUB
'-------------------------------------------------------------------------------------------

'-------------------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
    LOCAL found AS LONG
    LOCAL arg AS STRING

    ' Create exe icon in Unix dock and Unix file explorer
    Me2Unix 0, 0 ' askConfirmation=%FALSE, showInAppMenu=%FALSE

    ' Are we on Unix/Wine?
    g_onunix = IsOnUnix()

    ' Set path to ini file
    g_inifile = EXE.PATH$ + "vlcrun.ini"

    ' First time launch
    IF NOT EXISTS(g_inifile) THEN
        IF MSGBOX("This is your first time launching this program."+$CR+$CR _
                 +"Do you want to auto-detect VLC Media Player on your system?" _
                 ,%MB_ICONQUESTION OR %MB_YESNO, "vlcrun "+$VER) = %IDYES THEN
            AutoDetectVlc()
        END IF
        WriteIni()
    END IF

    ' Get VLC path from ini file
    ReadIni()
    IF g_onunix AND LEFT$(g_vlcpath,1) = "/" THEN
        found = UnixExists(g_vlcpath)
    ELSE
        found = (g_vlcpath <> "" AND EXISTS(g_vlcpath))
    END IF

    IF NOT found THEN
        MSGBOX "VLC Media Player not found."+$CR+$CR _
              +"Please manually set the VLC filepath"+$CR _
              +"into "+$DQ+g_inifile+$DQ _
              ,%MB_ICONINFORMATION, "vlcrun "+$VER
        EXIT FUNCTION
    END IF

    IF LCASE$(COMMAND$) = "/hidden" THEN
        ' Special command to run a hidden instance of vlc to fix perf issue: "vlc.exe /hidden"
        IF LEFT$(g_vlcpath,1) <> "/" THEN
            ShellExecute %NULL, "open", (g_vlcpath), "", "", %SW_SHOWMINNOACTIVE
        END IF
    ELSE
        ' Any other call: run vlc with the requested parameters
        IF g_onunix AND LEFT$(g_vlcpath,1) = "/" THEN
            IF COMMAND$ = "" THEN
                arg = "--random " + $DQ + UnixPath(CURDIR$) + $DQ
            ELSEIF LEFT$(COMMAND$(1),1) = "-" THEN  ' vlcrun --random c:\path\to\vid.mp4
                arg = COMMAND$(1) + $SPC + $DQ + UnixPath(COMMAND$(2)) + $DQ
            ELSE                                    ' vlcrun c:\path\to\vid.mp4
                arg = UnixPath(COMMAND$)
            END IF
            UnixShell g_vlcpath + " " + arg
        ELSE
            ShellExecute %NULL, "open", (g_vlcpath), COMMAND$, "", %SW_SHOW
        END IF
    END IF

END FUNCTION
'-------------------------------------------------------------------------------------------

'-------------------------------------------------------------------------------------------
SUB WriteIni()
    LOCAL ff AS LONG
    ff = FREEFILE
    OPEN g_inifile FOR OUTPUT AS #ff
        PRINT #ff, "[vlcrun]"
        PRINT #ff, "# Replace the following with any 'vlc.exe' filepath you want to use:"
        PRINT #ff, "# If you are running under Unix/Wine, Unix paths also work e.g. /usr/bin/vlc"
        PRINT #ff, "vlcpath=" + g_vlcpath
    CLOSE #ff
END SUB
'-------------------------------------------------------------------------------------------

'-------------------------------------------------------------------------------------------
SUB ReadIni()
    g_vlcpath = GetIniS(g_inifile, "vlcrun", "vlcpath")
END SUB
'-------------------------------------------------------------------------------------------

'-------------------------------------------------------------------------------------------
#IF NOT %DEF(%FN_EXISTS)
%FN_EXISTS = -1
FUNCTION EXISTS(BYVAL f AS STRING) AS LONG
 LOCAL i AS LONG
 i = GETATTR(f)
 FUNCTION = (ERRCLEAR = 0)
END FUNCTION
#ENDIF
'-------------------------------------------------------------------------------------------