[APP] StaScrCpy - scrcpy starter

Post your working scripts, libraries and tools.
User avatar
cyruz
Posts: 346
Joined: 30 Sep 2013, 13:31

[APP] StaScrCpy - scrcpy starter

Post by cyruz » 06 Oct 2022, 10:30

Hi guys,

I’m not sure if any of you used scrcpy before. It's a screen mirroring tool to control an Android phone from Windows:

https://github.com/Genymobile/scrcpy

With Android >= 11 it's much more convenient, because with the wireless debug, it doesn't require to connect the phone through USB first.

This tool it's handy to pair adb and start scrcpy without messing with console commands. I just started with Ahk2 so be advised.

Requires:
[*] ConfOverride :: viewtopic.php?f=83&t=109086
[*] StdoutToVar :: viewtopic.php?f=83&t=109148

Code: Select all

; ----------------------------------------------------------------------------------------------------------------------
; Name .........: StaScrCpy
; Description ..: Starter for scrcpy in wireless mode (Android >= 11).
; AHK Version ..: AHK v2 x32/64 Unicode
; Author .......: Cyruz - http://ciroprincipe.info
; License ......: WTFPL - http://www.wtfpl.net/txt/copying/
; Changelog ....: Oct. 07, 2022 - v0.1 - First version.
; ----------------------------------------------------------------------------------------------------------------------

#NoTrayIcon
#SingleInstance Force
#Include <ConfOverride>
#Include <StdOutToVar>

; ===[ GLOBAL CONFIGURATION OBJECT ]====================================================================================

Global CONF := {}
       CONF.SCRIPT :=
       {
           NAME        : "StaScrCpy"
         , DESCRIPTION : "-= " Chr(0x00A9) "2022 Ciro Principe - http://ciroprincipe.info =-"
	     , INI         : A_ScriptDir "\StaScrCpy.ini"
         , VERSION     : 0.1
         , __LOCKED    : 1
       }
       CONF.SETTINGS :=
       {
           ADB_PATH    : A_ScriptDir "\adb.exe"
         , SCRCPY_PATH : A_ScriptDir "\scrcpy.exe"
         , SCRCPY_HIDE : 0
         , DEV_IP_PORT : ""
         , SCRCPY_ARGS : ""
       }

; Read configuration file and override configuration objects.
ConfOverride(CONF, CONF.SCRIPT.INI)

; ===[ MAIN SECTION ]===================================================================================================

TraySetIcon(CONF.SETTINGS.SCRCPY_PATH, 1, 1)

oGui := Gui("+OwnDialogs", "StaScrCpy - scrcpy starter tool")
oGui.MarginX := 20, oGui.MarginY := 20
oGui.SetFont("S8", "Fixedsys")
oGui.Add("Text", "w100 h24", "Device IP:PORT: ")
(oGui.Add("Edit", "w200 h24 x+10 yp-4", CONF.SETTINGS.DEV_IP_PORT ? CONF.SETTINGS.DEV_IP_PORT : "")).Name := "Ed1"
oGui.Add("Text", "w100 h24 xm0 y+20", "Command Args: ")
(oGui.Add("Edit", "w200 h24 x+10 yp-4", CONF.SETTINGS.SCRCPY_ARGS ? CONF.SETTINGS.SCRCPY_ARGS : "")).Name := "Ed2"
(oGui.Add("Button", "w310 h40 xm0 y+20", "START SCRCPY")).OnEvent("Click", FnStart)
oGui.Add("Text", "w310 xm0 y+20 0x10")
oGui.Add("Text", "w100 h24 xm0 y+10", "Pairing Code: ")
(oGui.Add("Edit", "w200 h24 x+10 yp-4")).Name := "Ed3"
(oGui.Add("Button", "w310 h40 xm0 y+10", "PAIR THE PHONE")).OnEvent("Click", FnPair)
oGui.Show()
Return

FnStart(oGuiCtrl, sInfo)
{
    oSubmit := FnSubmit(oGuiCtrl.Gui)
    oVar := StdOutToVar(CONF.SETTINGS.ADB_PATH " connect " oSubmit.Ed1)

    If InStr(oVar.Output, "failed  to connect") || InStr(oVar.Output, "cannot connect")
    {
        oGui.Opt("-Disabled +OwnDialogs")
        MsgBox("ADB :: " oVar.Output, CONF.SCRIPT.NAME)
        Return
    }

    Sleep(2000)
    Run( A_Comspec " /c `"" CONF.SETTINGS.SCRCPY_PATH " --tcpip=" oSubmit.Ed1 " " oSubmit.Ed2 "`""
       , RegExReplace(CONF.SETTINGS.SCRCPY_PATH, "\\[^\\]+$") ; Use SCRCPY directory as working directory.
       , CONF.SETTINGS.SCRCPY_HIDE ? "Hide" : "" )

    ; Save last data in the configuration file.
    IniWrite(oSubmit.Ed1, CONF.SCRIPT.INI, "SETTINGS", "DEV_IP_PORT")
    IniWrite(oSubmit.Ed2, CONF.SCRIPT.INI, "SETTINGS", "SCRCPY_ARGS")

    ExitApp
}

FnPair(oGuiCtrl, sInfo)
{
    oSubmit := FnSubmit(oGuiCtrl.Gui)
    oVar := StdOutToVar(CONF.SETTINGS.ADB_PATH " pair " oSubmit.Ed1 " " oSubmit.Ed3)

    oGui.Opt("-Disabled +OwnDialogs")
    MsgBox("ADB :: " oVar.Output, CONF.SCRIPT.NAME)
}

FnSubmit(oGui)
{
    oSubmit := oGui.Submit(0)
    oGui.Opt("+Disabled +OwnDialogs")

    If !RegExMatch(oSubmit.Ed1, "[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}:[\d]+")
        Throw("Wrong format, please use IP:PORT")    

    Return oSubmit
}
Ini file:

Code: Select all

[SETTINGS]
ADB_PATH=C:\scrcpy\adb.exe
SCRCPY_PATH=C:\scrcpy\scrcpy.exe
SCRCPY_HIDE=0
ABCza on the old forum.
My GitHub.

Return to “Scripts and Functions (v2)”