feat(adb): remove adb resource

This commit is contained in:
AkiChase 2024-05-20 17:38:37 +08:00
parent c0d35cf02f
commit 4681287364
18 changed files with 100 additions and 128 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,5 @@
use crate::resource::{ResHelper, ResourceName};
use std::{
io::BufRead,
path::PathBuf,
process::{Child, Command, Stdio},
};
@ -18,8 +16,8 @@ pub struct Device {
impl Device {
/// execute "adb push" to push file from src to des
pub fn cmd_push(res_dir: &PathBuf, id: &str, src: &str, des: &str) -> Result<String> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_push(id: &str, src: &str, des: &str) -> Result<String> {
let mut adb_command = Adb::cmd_base();
let res = adb_command
.args(&["-s", id, "push", src, des])
.output()
@ -28,8 +26,8 @@ impl Device {
}
/// execute "adb reverse" to reverse the device port to local port
pub fn cmd_reverse(res_dir: &PathBuf, id: &str, remote: &str, local: &str) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_reverse(id: &str, remote: &str, local: &str) -> Result<()> {
let mut adb_command = Adb::cmd_base();
adb_command
.args(&["-s", id, "reverse", remote, local])
.output()
@ -38,8 +36,8 @@ impl Device {
}
/// execute "adb forward" to forward the local port to the device
pub fn cmd_forward(res_dir: &PathBuf, id: &str, local: &str, remote: &str) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_forward(id: &str, local: &str, remote: &str) -> Result<()> {
let mut adb_command = Adb::cmd_base();
adb_command
.args(&["-s", id, "forward", local, remote])
.output()
@ -48,8 +46,8 @@ impl Device {
}
/// execute "adb shell" to execute shell command on the device
pub fn cmd_shell(res_dir: &PathBuf, id: &str, shell_args: &[&str]) -> Result<Child> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_shell(id: &str, shell_args: &[&str]) -> Result<Child> {
let mut adb_command = Adb::cmd_base();
let mut args = vec!["-s", id, "shell"];
args.extend_from_slice(shell_args);
Ok(adb_command
@ -60,8 +58,8 @@ impl Device {
}
/// execute "adb shell wm size" to get screen size
pub fn cmd_screen_size(res_dir: &PathBuf, id: &str) -> Result<(u32, u32)> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_screen_size(id: &str) -> Result<(u32, u32)> {
let mut adb_command = Adb::cmd_base();
let output = adb_command
.args(&["-s", id, "shell", "wm", "size"])
.output()
@ -86,22 +84,19 @@ pub struct Adb;
/// Module to execute adb command and fetch output.
/// But some output of command won't be output, like adb service startup information.
impl Adb {
fn cmd_base(res_dir: &PathBuf) -> Command {
pub fn cmd_base() -> Command {
#[cfg(target_os = "windows")]
{
let mut cmd = Command::new(ResHelper::get_file_path(res_dir, ResourceName::Adb));
let mut cmd = Command::new("adb");
cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW
cmd
}
#[cfg(not(target_os = "windows"))]
{
Command::new(ResHelper::get_file_path(res_dir, ResourceName::Adb))
}
Command::new("adb")
}
/// execute "adb devices" and return devices list
pub fn cmd_devices(res_dir: &PathBuf) -> Result<Vec<Device>> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_devices() -> Result<Vec<Device>> {
let mut adb_command = Adb::cmd_base();
let output = adb_command
.args(&["devices"])
.output()
@ -128,8 +123,8 @@ impl Adb {
}
/// execute "adb kill-server"
pub fn cmd_kill_server(res_dir: &PathBuf) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_kill_server() -> Result<()> {
let mut adb_command = Adb::cmd_base();
adb_command
.args(&["kill-server"])
.output()
@ -138,8 +133,8 @@ impl Adb {
}
/// execute "adb reverse --remove-all"
pub fn cmd_reverse_remove(res_dir: &PathBuf) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_reverse_remove() -> Result<()> {
let mut adb_command = Adb::cmd_base();
adb_command
.args(&["reverse", " --remove-all"])
.output()
@ -148,8 +143,8 @@ impl Adb {
}
/// execute "adb forward --remove-all"
pub fn cmd_forward_remove(res_dir: &PathBuf) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_forward_remove() -> Result<()> {
let mut adb_command = Adb::cmd_base();
adb_command
.args(&["forward", " --remove-all"])
.output()
@ -158,8 +153,8 @@ impl Adb {
}
/// execute "adb start-server"
pub fn cmd_start_server(res_dir: &PathBuf) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_start_server() -> Result<()> {
let mut adb_command = Adb::cmd_base();
adb_command
.args(&["start-server"])
.output()
@ -167,8 +162,8 @@ impl Adb {
Ok(())
}
pub fn cmd_connect(res_dir: &PathBuf, address: &str) -> Result<String> {
let mut adb_command = Adb::cmd_base(res_dir);
pub fn cmd_connect(address: &str) -> Result<String> {
let mut adb_command = Adb::cmd_base();
let output = adb_command
.args(&["connect", address])
.output()
@ -178,10 +173,3 @@ impl Adb {
Ok(res)
}
}
#[test]
fn t() {
let res_dir = PathBuf::from("/Users/akichase/Projects/github/scrcpy-mask/src-tauri/resource/");
let res = Adb::cmd_connect(&res_dir, "127.0.0.1:1234").unwrap();
println!("{}", res)
}

View File

@ -3,7 +3,8 @@ use std::{io::BufRead, path::PathBuf};
use crate::{
adb::{Adb, Device},
resource::{ResHelper, ResourceName}, share,
resource::{ResHelper, ResourceName},
share,
};
/**
@ -22,35 +23,34 @@ impl ScrcpyClient {
ResHelper::get_scrcpy_version()
}
pub fn adb_devices(res_dir: &PathBuf) -> Result<Vec<Device>> {
Adb::cmd_devices(res_dir)
pub fn adb_devices() -> Result<Vec<Device>> {
Adb::cmd_devices()
}
pub fn adb_restart_server(res_dir: &PathBuf) -> Result<()> {
Adb::cmd_kill_server(res_dir)?;
Adb::cmd_start_server(res_dir)?;
pub fn adb_restart_server() -> Result<()> {
Adb::cmd_kill_server()?;
Adb::cmd_start_server()?;
Ok(())
}
pub fn adb_reverse_remove(res_dir: &PathBuf) -> Result<()> {
Adb::cmd_reverse_remove(res_dir)
pub fn adb_reverse_remove() -> Result<()> {
Adb::cmd_reverse_remove()
}
pub fn adb_forward_remove(res_dir: &PathBuf) -> Result<()> {
Adb::cmd_forward_remove(res_dir)
pub fn adb_forward_remove() -> Result<()> {
Adb::cmd_forward_remove()
}
// get the screen size of the device
pub fn get_device_screen_size(res_dir: &PathBuf, id: &str) -> Result<(u32, u32)> {
Device::cmd_screen_size(res_dir, id)
pub fn get_device_screen_size(id: &str) -> Result<(u32, u32)> {
Device::cmd_screen_size(id)
}
/// push server file to current device
pub fn push_server_file(res_dir: &PathBuf, id: &str) -> Result<()> {
pub fn push_server_file(dir: &PathBuf, id: &str) -> Result<()> {
let info = Device::cmd_push(
res_dir,
id,
&ResHelper::get_file_path(res_dir, ResourceName::ScrcpyServer).to_string_lossy(),
&ResHelper::get_file_path(dir, ResourceName::ScrcpyServer).to_string_lossy(),
"/data/local/tmp/scrcpy-server.jar",
)?;
@ -59,9 +59,8 @@ impl ScrcpyClient {
}
/// forward the local port to the device
pub fn forward_server_port(res_dir: &PathBuf, id: &str, scid: &str, port: u16) -> Result<()> {
pub fn forward_server_port(id: &str, scid: &str, port: u16) -> Result<()> {
Device::cmd_forward(
res_dir,
id,
&format!("tcp:{}", port),
&format!("localabstract:scrcpy_{}", scid),
@ -71,9 +70,8 @@ impl ScrcpyClient {
}
/// reverse the device port to the local port
pub fn reverse_server_port(res_dir: &PathBuf, id: &str, scid: &str, port: u16) -> Result<()> {
pub fn reverse_server_port(id: &str, scid: &str, port: u16) -> Result<()> {
Device::cmd_reverse(
res_dir,
id,
&format!("localabstract:scrcpy_{}", scid),
&format!("tcp:{}", port),
@ -83,14 +81,8 @@ impl ScrcpyClient {
}
/// spawn a new thread to start scrcpy server
pub fn shell_start_server(
res_dir: &PathBuf,
id: &str,
scid: &str,
version: &str,
) -> Result<()> {
pub fn shell_start_server(id: &str, scid: &str, version: &str) -> Result<()> {
let mut child = Device::cmd_shell(
res_dir,
id,
&[
"CLASSPATH=/data/local/tmp/scrcpy-server.jar",
@ -119,7 +111,7 @@ impl ScrcpyClient {
// clear string to store new line only
s.clear();
}
*share::CLIENT_INFO.lock().unwrap() = None;
println!("Scrcpy server closed");
Ok(())

View File

@ -13,9 +13,8 @@ use tauri::Manager;
#[tauri::command]
/// get devices info list
fn adb_devices(app: tauri::AppHandle) -> Result<Vec<Device>, String> {
let dir = app.path().resource_dir().unwrap().join("resource");
match Adb::cmd_devices(&dir) {
fn adb_devices() -> Result<Vec<Device>, String> {
match Adb::cmd_devices() {
Ok(devices) => Ok(devices),
Err(e) => Err(e.to_string()),
}
@ -23,15 +22,8 @@ fn adb_devices(app: tauri::AppHandle) -> Result<Vec<Device>, String> {
#[tauri::command]
/// forward local port to the device port
fn forward_server_port(
app: tauri::AppHandle,
id: String,
scid: String,
port: u16,
) -> Result<(), String> {
let dir = app.path().resource_dir().unwrap().join("resource");
match ScrcpyClient::forward_server_port(&dir, &id, &scid, port) {
fn forward_server_port(id: String, scid: String, port: u16) -> Result<(), String> {
match ScrcpyClient::forward_server_port(&id, &scid, port) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
@ -66,12 +58,11 @@ fn start_scrcpy_server(
scid.clone(),
));
let dir = app.path().resource_dir().unwrap().join("resource");
let version = ScrcpyClient::get_scrcpy_version();
// start scrcpy server
tokio::spawn(async move {
ScrcpyClient::shell_start_server(&dir, &id, &scid, &version).unwrap();
ScrcpyClient::shell_start_server(&id, &scid, &version).unwrap();
});
// connect to scrcpy server
@ -131,9 +122,8 @@ fn get_cur_client_info() -> Result<Option<share::ClientInfo>, String> {
#[tauri::command]
/// get device screen size
fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32), String> {
let dir = app.path().resource_dir().unwrap().join("resource");
match ScrcpyClient::get_device_screen_size(&dir, &id) {
fn get_device_screen_size(id: String) -> Result<(u32, u32), String> {
match ScrcpyClient::get_device_screen_size(&id) {
Ok(size) => Ok(size),
Err(e) => Err(e.to_string()),
}
@ -141,9 +131,8 @@ fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32
#[tauri::command]
/// connect to wireless device
fn adb_connect(address: String, app: tauri::AppHandle) -> Result<String, String> {
let dir = app.path().resource_dir().unwrap().join("resource");
match Adb::cmd_connect(&dir, &address) {
fn adb_connect(address: String) -> Result<String, String> {
match Adb::cmd_connect(&address) {
Ok(res) => Ok(res),
Err(e) => Err(e.to_string()),
}
@ -160,6 +149,14 @@ fn load_default_keyconfig(app: tauri::AppHandle) -> Result<String, String> {
}
}
#[tauri::command]
fn check_adb_available() -> Result<(), String> {
match Adb::cmd_base().output() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
}
#[tokio::main]
async fn main() {
tauri::Builder::default()
@ -241,7 +238,8 @@ async fn main() {
get_cur_client_info,
get_device_screen_size,
adb_connect,
load_default_keyconfig
load_default_keyconfig,
check_adb_available
])
.run(tauri::generate_context!())
.expect("error while running tauri application");

View File

@ -2,7 +2,6 @@ use anyhow::{anyhow, Ok, Result};
use std::path::PathBuf;
pub enum ResourceName {
Adb,
ScrcpyServer,
DefaultKeyConfig,
}
@ -13,7 +12,9 @@ pub struct ResHelper {
impl ResHelper {
pub fn res_init(res_dir: &PathBuf) -> Result<()> {
for name in [ResourceName::Adb, ResourceName::ScrcpyServer] {
let res = [ResourceName::ScrcpyServer, ResourceName::DefaultKeyConfig];
for name in res {
let file_path = ResHelper::get_file_path(res_dir, name);
if !file_path.exists() {
return Err(anyhow!(format!(
@ -27,13 +28,6 @@ impl ResHelper {
}
pub fn get_file_path(dir: &PathBuf, file_name: ResourceName) -> PathBuf {
match file_name {
#[cfg(target_os = "windows")]
ResourceName::Adb => dir.join("adb-win.exe"),
#[cfg(target_os = "linux")]
ResourceName::Adb => dir.join("adb-linux"),
#[cfg(target_os = "macos")]
ResourceName::Adb => dir.join("adb-mac"),
ResourceName::ScrcpyServer => dir.join("scrcpy-server-v2.4"),
ResourceName::DefaultKeyConfig => dir.join("default-key-config.json"),
}

View File

@ -30,6 +30,10 @@
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4"
]
}
}

View File

@ -1,9 +0,0 @@
{
"bundle": {
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4",
"resource/adb-linux"
]
}
}

View File

@ -1,9 +0,0 @@
{
"bundle": {
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4",
"resource/adb-mac"
]
}
}

View File

@ -1,11 +0,0 @@
{
"bundle": {
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4",
"resource/adb-win.exe",
"resource/AdbWinApi.dll",
"resource/AdbWinUsbApi.dll"
]
}
}

View File

@ -296,7 +296,12 @@ async function onMenuSelect(key: string) {
async function refreshDevices() {
store.showLoading();
devices.value = await adbDevices();
try {
devices.value = await adbDevices();
} catch (e) {
message.error(t("pages.Device.adbDeviceError"));
console.error(e);
}
store.hideLoading();
}

View File

@ -20,6 +20,7 @@ import { AndroidKeycode } from "../frontcommand/android";
import { Store } from "@tauri-apps/plugin-store";
import { useI18n } from "vue-i18n";
import { SendKeyAction, sendKey } from "../frontcommand/scrcpyMaskCmd";
import { checkAdbAvailable } from "../invoke";
const { t } = useI18n();
const store = useGlobalStore();
@ -64,12 +65,23 @@ onActivated(async () => {
});
onMounted(async () => {
await checkAdb();
await loadLocalStore();
store.checkUpdate = checkUpdate;
store.showInputBox = showInputBox;
if (store.checkUpdateAtStart) checkUpdate();
});
async function checkAdb() {
try {
await checkAdbAvailable();
} catch (e) {
message.error(t("pages.Mask.checkAdb", [e]), {
duration: 0,
});
}
}
async function loadLocalStore() {
const localStore = new Store("store.bin");
// loading screenSize from local store

View File

@ -42,7 +42,8 @@
"wsAddress": "Websocket address",
"inputWsAddress": "Please enter the Websocket address",
"wsClose": "Close",
"wsConnect": "Control"
"wsConnect": "Control",
"adbDeviceError": "Unable to get available devices"
},
"Mask": {
"inputBoxPlaceholder": "Input text and then press enter/esc",
@ -62,7 +63,8 @@
"content": "Please go to the device page to control any device",
"positiveText": "To control"
},
"sightMode": "Mouse is locked, press {0} to unlock"
"sightMode": "Mouse is locked, press {0} to unlock",
"checkAdb": "adb is not available and the software cannot run normally. Please ensure that adb is installed on the system and added to the Path environment variable correctly: {0}"
},
"Setting": {
"tabs": {

View File

@ -42,7 +42,8 @@
"externalControl": "外部控制",
"wsAddress": "Websocket 地址",
"wsClose": "断开",
"wsConnect": "控制"
"wsConnect": "控制",
"adbDeviceError": "无法获取可用设备"
},
"Mask": {
"keyconfigException": "按键方案异常,请删除此方案",
@ -62,7 +63,8 @@
"positiveText": "去控制"
},
"inputBoxPlaceholder": "输入文本后按Enter/Esc",
"sightMode": "鼠标已锁定, 按 {0} 键解锁"
"sightMode": "鼠标已锁定, 按 {0} 键解锁",
"checkAdb": "adb不可用软件无法正常运行请确保系统已安装adb并正确添加到了Path环境变量中: {0}"
},
"Setting": {
"tabs": {

View File

@ -51,4 +51,8 @@ export async function loadDefaultKeyconfig(): Promise<string> {
return await invoke("load_default_keyconfig");
}
export async function checkAdbAvailable(): Promise<void>{
return await invoke("check_adb_available");
}
export type { Device };