feat: implement linux

pull/72/head
amrbashir 3 years ago
parent ff1035740e
commit c86f4dc024
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33

@ -22,3 +22,5 @@ features = [
"Win32_Graphics_Gdi"
]
[target.'cfg(target_os = "linux")'.dependencies]
zbus = "2.2"

@ -1,106 +0,0 @@
lockfileVersion: 5.3
specifiers:
'@tauri-apps/cli': ^1.0.0-rc.0
dependencies:
'@tauri-apps/cli': 1.0.0-rc.8
packages:
/@tauri-apps/cli-darwin-arm64/1.0.0-rc.8:
resolution: {integrity: sha512-sbV4fkd3YBlX0gEBldEVmN/NbBdHzrn8wCRRiFaNBocDWC6Ji6DNvpp1Vy1xgY0RgtGOisGD+3+HelmKP4Mh/A==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [darwin]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-darwin-x64/1.0.0-rc.8:
resolution: {integrity: sha512-VmuYbBZKKal/YxSTbzHTaM04C9zUlTXq7ePoXc1PRAa/Xafo16N37OGzhzBlsKHrvh6BtPp9ZWk5UhHIDiJBsw==}
engines: {node: '>= 10'}
cpu: [x64]
os: [darwin]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-linux-arm-gnueabihf/1.0.0-rc.8:
resolution: {integrity: sha512-2VKkS1c2pK56sgtGxtsm9nKKWefG7dgPnynTuCoEngkgQfNIlZ1ns/eAIZpBmwhvOMOw2OesKvK9ZQo0/lE/IA==}
engines: {node: '>= 10'}
cpu: [arm]
os: [linux]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-linux-arm64-gnu/1.0.0-rc.8:
resolution: {integrity: sha512-jykbrTJyx0uJhhL/3PPYGx5ETqEzacJ3YChnHzVClSzPkrdoGC61/xY9dMD9ZFJsLMJkV4VJj8sGRd1OPKCa/w==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-linux-arm64-musl/1.0.0-rc.8:
resolution: {integrity: sha512-HAIm8BKpLIWkvYgxoRzDIaqWIW3UtH2HO1qDd0aov5FTx/9dtCTtylpvd/6mWs5LGaEmsLoSokfJloI2sMZ/WQ==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-linux-x64-gnu/1.0.0-rc.8:
resolution: {integrity: sha512-fnPrkX6Uy9Qz2vqnT3D3sgbJzXTR4arNUIQl8HIYi2YCZZVmDWczz4x4++ilZWwk0I3a7d612/WzTeBzQjn3kg==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-linux-x64-musl/1.0.0-rc.8:
resolution: {integrity: sha512-kHrpBsJFRAwqFL7xnsrKdNxnYjzI/J9/Dr7T3rl+IIPbi1sIojh6DydxFOYepnuoINA6zWYQJpo0FQeASJsXrw==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-win32-ia32-msvc/1.0.0-rc.8:
resolution: {integrity: sha512-y8uT7AaIZVdLAWL4pxj/GXitwWn828er23yniOvTo1mJMCZsSFdwiOGiRSXI6OZsVCOPiJmvqq7eWaskQKYA0A==}
engines: {node: '>= 10'}
cpu: [ia32]
os: [win32]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli-win32-x64-msvc/1.0.0-rc.8:
resolution: {integrity: sha512-PiDr/iAKoL9nQSiWHkUSjRVHbvCJaa7Xj3P9UnGkjJQ9LGTr7AYZdLk4zISegF+/fZnOmaqUdZW9yWPPyJAzPw==}
engines: {node: '>= 10'}
cpu: [x64]
os: [win32]
requiresBuild: true
dev: false
optional: true
/@tauri-apps/cli/1.0.0-rc.8:
resolution: {integrity: sha512-FWpNwbgGMPgDb41oO7wdBhBsBE6FMG9yHKbtbrkJ/8d/hWcT6e3UUTfFL9cR2VGdp1pdWlul5+BKH0PTf8rB9w==}
engines: {node: '>= 10'}
hasBin: true
optionalDependencies:
'@tauri-apps/cli-darwin-arm64': 1.0.0-rc.8
'@tauri-apps/cli-darwin-x64': 1.0.0-rc.8
'@tauri-apps/cli-linux-arm-gnueabihf': 1.0.0-rc.8
'@tauri-apps/cli-linux-arm64-gnu': 1.0.0-rc.8
'@tauri-apps/cli-linux-arm64-musl': 1.0.0-rc.8
'@tauri-apps/cli-linux-x64-gnu': 1.0.0-rc.8
'@tauri-apps/cli-linux-x64-musl': 1.0.0-rc.8
'@tauri-apps/cli-win32-ia32-msvc': 1.0.0-rc.8
'@tauri-apps/cli-win32-x64-msvc': 1.0.0-rc.8
dev: false

@ -66,13 +66,14 @@ dependencies = [
[[package]]
name = "async-broadcast"
version = "0.3.4"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90622698a1218e0b2fb846c97b5f19a0831f6baddee73d9454156365ccfa473b"
checksum = "1bbd92a9bd0e9c1298118ecf8a2f825e86b12c3ec9e411573e34aaf3a0c03cdd"
dependencies = [
"easy-parallel",
"event-listener",
"futures-core",
"parking_lot 0.11.2",
]
[[package]]
@ -862,6 +863,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "futf"
version = "0.1.5"
@ -2293,6 +2300,19 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi",
]
[[package]]
name = "rand"
version = "0.7.3"
@ -2338,6 +2358,21 @@ dependencies = [
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.5.1"
@ -2383,6 +2418,15 @@ dependencies = [
"cty",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "redox_syscall"
version = "0.2.13"
@ -3110,6 +3154,7 @@ version = "0.0.0"
dependencies = [
"tauri",
"windows-sys 0.35.0",
"zbus",
]
[[package]]
@ -3172,6 +3217,16 @@ dependencies = [
"walkdir",
]
[[package]]
name = "tempdir"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8"
dependencies = [
"rand 0.4.6",
"remove_dir_all",
]
[[package]]
name = "tempfile"
version = "3.3.0"
@ -3361,6 +3416,16 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
name = "uds_windows"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "486992108df0fe0160680af1941fe856c521be931d5a5ecccefe0de86dc47e4a"
dependencies = [
"tempdir",
"winapi",
]
[[package]]
name = "unicode-bidi"
version = "0.3.7"
@ -4022,9 +4087,9 @@ checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3"
[[package]]
name = "zbus"
version = "2.1.1"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bb86f3d4592e26a48b2719742aec94f8ae6238ebde20d98183ee185d1275e9a"
checksum = "53819092b9db813b2c6168b097b4b13ad284d81c9f2b0165a0a1b190e505a1f3"
dependencies = [
"async-broadcast",
"async-channel",
@ -4051,6 +4116,7 @@ dependencies = [
"serde_repr",
"sha1",
"static_assertions",
"uds_windows",
"winapi",
"zbus_macros",
"zbus_names",
@ -4059,9 +4125,9 @@ dependencies = [
[[package]]
name = "zbus_macros"
version = "2.1.1"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36823cc10fddc3c6b19f048903262dacaf8274170e9a255784bdd8b4570a8040"
checksum = "c7174ebe6722c280d6d132d694bb5664ce50a788cb70eeb518e7fc1ca095a114"
dependencies = [
"proc-macro-crate 1.1.3",
"proc-macro2",

@ -3,11 +3,20 @@ use tauri::{plugin::TauriPlugin, Runtime};
#[cfg(target_os = "windows")]
#[path = "platform_impl/windows.rs"]
mod platform_impl;
#[cfg(target_os = "linux")]
#[path = "platform_impl/linux.rs"]
mod platform_impl;
#[cfg(target_os = "macos")]
#[path = "platform_impl/macos.rs"]
mod platform_impl;
pub(crate) type SingleInstanceCallback =
dyn FnMut(Vec<String>, String, Box<dyn FnOnce()>) + Send + 'static;
dyn FnMut(Vec<String>, String, Box<dyn FnOnce()>) + Send + Sync + 'static;
pub fn init<R: Runtime, F: FnMut(Vec<String>, String, Box<dyn FnOnce()>) + Send + 'static>(
pub fn init<
R: Runtime,
F: FnMut(Vec<String>, String, Box<dyn FnOnce()>) + Send + Sync + 'static,
>(
f: F,
) -> TauriPlugin<R> {
platform_impl::init(Box::new(f))

@ -0,0 +1,94 @@
#![cfg(target_os = "linux")]
use crate::SingleInstanceCallback;
use std::{cell::RefCell, rc::Rc};
use tauri::{
plugin::{self, TauriPlugin},
Manager, RunEvent, Runtime,
};
use zbus::{
blocking::{Connection, ConnectionBuilder},
dbus_interface,
};
struct ConnectionHandle(Connection);
const CLOSE_NEW_INSTANCE_ID: u32 = 1542;
struct SingleInstanceDBus {
callback: Box<SingleInstanceCallback>,
}
#[dbus_interface(name = "org.SingleInstance.DBus")]
impl SingleInstanceDBus {
fn execute_callback(&mut self, argv: Vec<String>, cwd: String) -> u32 {
let ret = Rc::new(RefCell::new(1));
let ret_c = Rc::clone(&ret);
(self.callback)(
argv,
cwd,
Box::new(move || {
let mut ret = ret_c.borrow_mut();
*ret = CLOSE_NEW_INSTANCE_ID;
}),
);
ret.take()
}
}
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app| {
let app_name = app.package_info().name.clone();
let single_instance_dbus = SingleInstanceDBus { callback: f };
let dbus_name = format!("org.{}.SingleInstance", app_name);
let dbus_path = format!("/org/{}/SingleInstance", app_name);
match ConnectionBuilder::session()
.unwrap()
.name(dbus_name.as_str())
.unwrap()
.serve_at(dbus_path.as_str(), single_instance_dbus)
.unwrap()
.build()
{
Ok(connection) => {
app.manage(ConnectionHandle(connection));
}
Err(zbus::Error::NameTaken) => {
let connection = Connection::session().unwrap();
if let Ok(m) = connection.call_method(
Some(dbus_name.as_str()),
dbus_path.as_str(),
Some("org.SingleInstance.DBus"),
"ExecuteCallback",
&(
std::env::args().collect::<Vec<String>>(),
std::env::current_dir()
.unwrap_or_default()
.to_str()
.unwrap_or_default(),
),
) {
let reply: u32 = m.body().unwrap_or_default();
if reply == CLOSE_NEW_INSTANCE_ID {
std::process::exit(0);
}
}
}
_ => {}
}
Ok(())
})
.on_event(|app, event| {
if let RunEvent::Exit = event {
if let Some(connection) = app.try_state::<ConnectionHandle>() {
let app_name = app.package_info().name.clone();
let dbus_name = format!("org.{}.SingleInstance", app_name);
let _ = connection.0.release_name(dbus_name);
}
}
})
.build()
}

@ -0,0 +1,10 @@
#![cfg(target_os = "macos")]
use crate::SingleInstanceCallback;
use tauri::{
plugin::{self, TauriPlugin},
Runtime,
};
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance").build()
}

@ -1,8 +1,7 @@
#![cfg(target_os = "windows")]
use std::{cell::RefCell, ffi::CStr, rc::Rc};
use crate::SingleInstanceCallback;
use std::{cell::RefCell, ffi::CStr, rc::Rc};
use tauri::{
plugin::{self, TauriPlugin},
Manager, RunEvent, Runtime,
@ -22,9 +21,19 @@ use windows_sys::Win32::{
},
};
struct MutexHandle(isize);
struct TargetWindowHandle(isize);
const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
const CLOSE_NEW_INSTANCE_ID: isize = 1542;
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app| {
let app_name = &app.package_info().name;
let class_name = format!("{}-single-instance-class", app_name);
let window_name = format!("{}-single-instance-window", app_name);
let hmutex = unsafe {
CreateMutexW(
std::ptr::null(),
@ -33,10 +42,6 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
)
};
let app_name = &app.package_info().name;
let class_name = format!("{}-single-instance-class", app_name);
let window_name = format!("{}-single-instance-window", app_name);
if unsafe { GetLastError() } == ERROR_ALREADY_EXISTS {
unsafe {
let hwnd = FindWindowW(
@ -60,82 +65,33 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
lpData: bytes.as_ptr() as _,
};
let ret = SendMessageW(hwnd, WM_COPYDATA, 0, &cds as *const _ as _);
if ret == CLOSE_NEW_INSTANCE {
if ret == CLOSE_NEW_INSTANCE_ID {
std::process::exit(0);
}
}
}
}
app.manage(MutexHandle(hmutex));
unsafe {
let class = WNDCLASSEXW {
cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
style: 0,
lpfnWndProc: Some(single_instance_window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: GetModuleHandleW(std::ptr::null()),
hIcon: 0,
hCursor: 0,
hbrBackground: 0,
lpszMenuName: std::ptr::null(),
lpszClassName: encode_wide(&class_name).as_ptr(),
hIconSm: 0,
};
RegisterClassExW(&class);
let hwnd = CreateWindowExW(
WS_EX_NOACTIVATE
| WS_EX_TRANSPARENT
| WS_EX_LAYERED
// WS_EX_TOOLWINDOW prevents this window from ever showing up in the taskbar, which
// we want to avoid. If you remove this style, this window won't show up in the
// taskbar *initially*, but it can show up at some later point. This can sometimes
// happen on its own after several hours have passed, although this has proven
// difficult to reproduce. Alternatively, it can be manually triggered by killing
// `explorer.exe` and then starting the process back up.
// It is unclear why the bug is triggered by waiting for several hours.
| WS_EX_TOOLWINDOW,
encode_wide(&class_name).as_ptr(),
encode_wide(&window_name).as_ptr(),
WS_OVERLAPPED,
0,
0,
0,
0,
0,
0,
GetModuleHandleW(std::ptr::null()),
std::ptr::null(),
);
SetWindowLongPtrW(
hwnd,
GWL_STYLE,
// The window technically has to be visible to receive WM_PAINT messages (which are used
// for delivering events during resizes), but it isn't displayed to the user because of
// the LAYERED style.
(WS_VISIBLE | WS_POPUP) as isize,
);
} else {
app.manage(MutexHandle(hmutex));
SetWindowLongPtrW(hwnd, GWL_USERDATA, Box::into_raw(Box::new(f)) as _);
let hwnd = create_event_target_window(&class_name, &window_name);
unsafe { SetWindowLongPtrW(hwnd, GWL_USERDATA, Box::into_raw(Box::new(f)) as _) };
app.manage(TargetWindowHandle(hwnd));
}
Ok(())
})
.on_event(|app, event| {
if let RunEvent::Exit = event {
let hmutex = app.state::<MutexHandle>().0;
unsafe {
ReleaseMutex(hmutex);
CloseHandle(hmutex);
};
let hwnd = app.state::<TargetWindowHandle>().0;
unsafe { DestroyWindow(hwnd) };
if let Some(hmutex) = app.try_state::<MutexHandle>() {
unsafe {
ReleaseMutex(hmutex.0);
CloseHandle(hmutex.0);
}
}
if let Some(hwnd) = app.try_state::<TargetWindowHandle>() {
unsafe { DestroyWindow(hwnd.0) };
}
}
})
.build()
@ -165,7 +121,7 @@ unsafe extern "system" fn single_instance_window_proc(
cwd.to_string(),
Box::new(move || {
let mut ret = ret_c.borrow_mut();
*ret = CLOSE_NEW_INSTANCE;
*ret = CLOSE_NEW_INSTANCE_ID;
}),
);
}
@ -180,10 +136,60 @@ unsafe extern "system" fn single_instance_window_proc(
}
}
struct MutexHandle(isize);
struct TargetWindowHandle(isize);
const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
const CLOSE_NEW_INSTANCE: isize = 1542;
fn create_event_target_window(class_name: &str, window_name: &str) -> HWND {
unsafe {
let class = WNDCLASSEXW {
cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
style: 0,
lpfnWndProc: Some(single_instance_window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: GetModuleHandleW(std::ptr::null()),
hIcon: 0,
hCursor: 0,
hbrBackground: 0,
lpszMenuName: std::ptr::null(),
lpszClassName: encode_wide(&class_name).as_ptr(),
hIconSm: 0,
};
RegisterClassExW(&class);
let hwnd = CreateWindowExW(
WS_EX_NOACTIVATE
| WS_EX_TRANSPARENT
| WS_EX_LAYERED
// WS_EX_TOOLWINDOW prevents this window from ever showing up in the taskbar, which
// we want to avoid. If you remove this style, this window won't show up in the
// taskbar *initially*, but it can show up at some later point. This can sometimes
// happen on its own after several hours have passed, although this has proven
// difficult to reproduce. Alternatively, it can be manually triggered by killing
// `explorer.exe` and then starting the process back up.
// It is unclear why the bug is triggered by waiting for several hours.
| WS_EX_TOOLWINDOW,
encode_wide(&class_name).as_ptr(),
encode_wide(&window_name).as_ptr(),
WS_OVERLAPPED,
0,
0,
0,
0,
0,
0,
GetModuleHandleW(std::ptr::null()),
std::ptr::null(),
);
SetWindowLongPtrW(
hwnd,
GWL_STYLE,
// The window technically has to be visible to receive WM_PAINT messages (which are used
// for delivering events during resizes), but it isn't displayed to the user because of
// the LAYERED style.
(WS_VISIBLE | WS_POPUP) as isize,
);
hwnd
}
}
pub fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
std::os::windows::prelude::OsStrExt::encode_wide(string.as_ref())
@ -193,24 +199,24 @@ pub fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
#[cfg(target_pointer_width = "32")]
#[allow(non_snake_case)]
pub fn SetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX, value: isize) -> isize {
unsafe { w32wm::SetWindowLongW(hwnd, index, value as _) as _ }
unsafe fn SetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX, value: isize) -> isize {
w32wm::SetWindowLongW(hwnd, index, value as _) as _
}
#[cfg(target_pointer_width = "64")]
#[allow(non_snake_case)]
pub fn SetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX, value: isize) -> isize {
unsafe { w32wm::SetWindowLongPtrW(hwnd, index, value) }
unsafe fn SetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX, value: isize) -> isize {
w32wm::SetWindowLongPtrW(hwnd, index, value)
}
#[cfg(target_pointer_width = "32")]
#[allow(non_snake_case)]
pub fn GetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX) -> isize {
unsafe { w32wm::GetWindowLongW(hwnd, index) as _ }
unsafe fn GetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX) -> isize {
w32wm::GetWindowLongW(hwnd, index) as _
}
#[cfg(target_pointer_width = "64")]
#[allow(non_snake_case)]
pub fn GetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX) -> isize {
unsafe { w32wm::GetWindowLongPtrW(hwnd, index) }
unsafe fn GetWindowLongPtrW(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX) -> isize {
w32wm::GetWindowLongPtrW(hwnd, index)
}

Loading…
Cancel
Save