Merge branch 'dev-1.x' of github.com:federico-terzi/espanso into dev-1.x

This commit is contained in:
Federico Terzi 2021-03-23 22:09:00 +01:00
commit 8257e9f400
20 changed files with 994 additions and 4 deletions

14
Cargo.lock generated
View File

@ -233,6 +233,7 @@ dependencies = [
"espanso-clipboard",
"espanso-config",
"espanso-detect",
"espanso-info",
"espanso-inject",
"espanso-match",
"espanso-ui",
@ -289,6 +290,19 @@ dependencies = [
"widestring",
]
[[package]]
name = "espanso-info"
version = "0.1.0"
dependencies = [
"anyhow",
"cc",
"lazy_static",
"lazycell",
"log",
"thiserror",
"widestring",
]
[[package]]
name = "espanso-inject"
version = "0.1.0"

View File

@ -10,4 +10,5 @@ members = [
"espanso-match",
"espanso-clipboard",
"espanso-render",
"espanso-info",
]

23
espanso-info/Cargo.toml Normal file
View File

@ -0,0 +1,23 @@
[package]
name = "espanso-info"
version = "0.1.0"
authors = ["Federico Terzi <federico-terzi@users.noreply.github.com>"]
edition = "2018"
build="build.rs"
[features]
# If the wayland feature is enabled, all X11 dependencies will be dropped
wayland = []
[dependencies]
log = "0.4.14"
lazycell = "1.3.0"
anyhow = "1.0.38"
thiserror = "1.0.23"
lazy_static = "1.4.0"
[target.'cfg(windows)'.dependencies]
widestring = "0.4.3"
[build-dependencies]
cc = "1.0.66"

72
espanso-info/build.rs Normal file
View File

@ -0,0 +1,72 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#[cfg(target_os = "windows")]
fn cc_config() {
println!("cargo:rerun-if-changed=src/win32/native.cpp");
println!("cargo:rerun-if-changed=src/win32/native.h");
cc::Build::new()
.cpp(true)
.include("src/win32/native.h")
.file("src/win32/native.cpp")
.compile("espansoinfo");
println!("cargo:rustc-link-lib=static=espansoinfo");
println!("cargo:rustc-link-lib=dylib=user32");
#[cfg(target_env = "gnu")]
println!("cargo:rustc-link-lib=dylib=stdc++");
}
#[cfg(target_os = "linux")]
fn cc_config() {
if cfg!(not(feature = "wayland")) {
println!("cargo:rerun-if-changed=src/x11/native.h");
println!("cargo:rerun-if-changed=src/x11/native.c");
cc::Build::new()
.cpp(true)
.include("src/x11/native.h")
.file("src/x11/native.cpp")
.compile("espansoinfo");
println!("cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu/");
println!("cargo:rustc-link-lib=static=espansoinfo");
println!("cargo:rustc-link-lib=dylib=stdc++");
println!("cargo:rustc-link-lib=dylib=X11");
} else {
// Nothing to compile on wayland
}
}
#[cfg(target_os = "macos")]
fn cc_config() {
println!("cargo:rerun-if-changed=src/cocoa/native.mm");
println!("cargo:rerun-if-changed=src/cocoa/native.h");
cc::Build::new()
.cpp(true)
.include("src/cocoa/native.h")
.file("src/cocoa/native.mm")
.compile("espansoinfo");
println!("cargo:rustc-link-lib=dylib=c++");
println!("cargo:rustc-link-lib=static=espansoinfo");
println!("cargo:rustc-link-lib=framework=Cocoa");
}
fn main() {
cc_config();
}

View File

@ -0,0 +1,27 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::os::raw::c_char;
#[link(name = "espansoinfo", kind = "static")]
extern "C" {
pub fn info_get_title(buffer: *mut c_char, buffer_size: i32) -> i32;
pub fn info_get_exec(buffer: *mut c_char, buffer_size: i32) -> i32;
pub fn info_get_class(buffer: *mut c_char, buffer_size: i32) -> i32;
}

View File

@ -0,0 +1,91 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::{ffi::CStr, os::raw::c_char};
use crate::{AppInfo, AppInfoProvider};
use self::ffi::{info_get_class, info_get_exec, info_get_title};
mod ffi;
pub struct CocoaAppInfoProvider {}
impl CocoaAppInfoProvider {
pub fn new() -> Self {
Self {}
}
}
impl AppInfoProvider for CocoaAppInfoProvider {
fn get_info(&self) -> AppInfo {
AppInfo {
title: self.get_title(),
class: self.get_class(),
exec: self.get_exec(),
}
}
}
impl CocoaAppInfoProvider {
fn get_exec(&self) -> Option<String> {
let mut buffer: [c_char; 2048] = [0; 2048];
if unsafe { info_get_exec(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } > 0 {
let string = unsafe { CStr::from_ptr(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string.to_string())
} else {
None
}
} else {
None
}
}
fn get_class(&self) -> Option<String> {
let mut buffer: [c_char; 2048] = [0; 2048];
if unsafe { info_get_class(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } > 0 {
let string = unsafe { CStr::from_ptr(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string.to_string())
} else {
None
}
} else {
None
}
}
fn get_title(&self) -> Option<String> {
let mut buffer: [c_char; 2048] = [0; 2048];
if unsafe { info_get_title(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } > 0 {
let string = unsafe { CStr::from_ptr(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string.to_string())
} else {
None
}
} else {
None
}
}
}

View File

@ -0,0 +1,29 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef ESPANSO_INFO_H
#define ESPANSO_INFO_H
#include <stdint.h>
extern "C" int32_t info_get_title(char * buffer, int32_t buffer_size);
extern "C" int32_t info_get_exec(char * buffer, int32_t buffer_size);
extern "C" int32_t info_get_class(char * buffer, int32_t buffer_size);
#endif //ESPANSO_INFO_H

View File

@ -0,0 +1,76 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#include "native.h"
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
int32_t info_get_title(char *buffer, int32_t buffer_size)
{
CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
int32_t result = 0;
if (windows) {
for (NSDictionary *window in (NSArray *)windows) {
NSNumber *ownerPid = window[(id) kCGWindowOwnerPID];
NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
if ([currentApp isActive]) {
NSString *name = window[(id) kCGWindowName];
if (name.length > 0) {
const char * title = [name UTF8String];
snprintf(buffer, buffer_size, "%s", title);
result = 1;
}
break;
}
}
CFRelease(windows);
}
return result;
}
int32_t info_get_exec(char *buffer, int32_t buffer_size)
{
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSString *bundlePath = [frontApp bundleURL].path;
const char * path = [bundlePath UTF8String];
snprintf(buffer, buffer_size, "%s", path);
[bundlePath release];
return 1;
}
int32_t info_get_class(char *buffer, int32_t buffer_size)
{
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSString *bundleId = frontApp.bundleIdentifier;
const char * bundle = [bundleId UTF8String];
snprintf(buffer, buffer_size, "%s", bundle);
[bundleId release];
return 1;
}

72
espanso-info/src/lib.rs Normal file
View File

@ -0,0 +1,72 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use anyhow::Result;
use log::info;
#[cfg(target_os = "windows")]
mod win32;
#[cfg(target_os = "linux")]
#[cfg(not(feature = "wayland"))]
mod x11;
#[cfg(target_os = "linux")]
#[cfg(feature = "wayland")]
mod wayland;
#[cfg(target_os = "macos")]
mod cocoa;
pub trait AppInfoProvider {
fn get_info(&self) -> AppInfo;
}
#[derive(Debug, Clone)]
pub struct AppInfo {
pub title: Option<String>,
pub exec: Option<String>,
pub class: Option<String>,
}
#[cfg(target_os = "windows")]
pub fn get_provider() -> Result<Box<dyn AppInfoProvider>> {
info!("using Win32AppInfoProvider");
Ok(Box::new(win32::WinAppInfoProvider::new()))
}
#[cfg(target_os = "macos")]
pub fn get_provider() -> Result<Box<dyn AppInfoProvider>> {
info!("using CocoaAppInfoProvider");
Ok(Box::new(cocoa::CocoaAppInfoProvider::new()))
}
#[cfg(target_os = "linux")]
#[cfg(not(feature = "wayland"))]
pub fn get_provider() -> Result<Box<dyn AppInfoProvider>> {
info!("using X11AppInfoProvider");
Ok(Box::new(x11::X11AppInfoProvider::new()))
}
#[cfg(target_os = "linux")]
#[cfg(feature = "wayland")]
pub fn get_provider() -> Result<Box<dyn AppInfoProvider>> {
info!("using WaylandAppInfoProvider");
Ok(Box::new(wayland::WaylandAppInfoProvider::new()))
}

View File

@ -0,0 +1,39 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::{AppInfoProvider, AppInfo};
pub(crate) struct WaylandAppInfoProvider {}
impl WaylandAppInfoProvider {
pub fn new() -> Self {
Self {}
}
}
impl AppInfoProvider for WaylandAppInfoProvider {
// TODO: can we read these info on Wayland?
fn get_info(&self) -> AppInfo {
AppInfo {
title: None,
exec: None,
class: None,
}
}
}

View File

@ -0,0 +1,24 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#[link(name = "espansoinfo", kind = "static")]
extern "C" {
pub fn info_get_title(buffer: *mut u16, buffer_size: i32) -> i32;
pub fn info_get_exec(buffer: *mut u16, buffer_size: i32) -> i32;
}

View File

@ -0,0 +1,76 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use widestring::U16CStr;
use crate::{AppInfo, AppInfoProvider};
use self::ffi::{info_get_exec, info_get_title};
mod ffi;
pub struct WinAppInfoProvider {}
impl WinAppInfoProvider {
pub fn new() -> Self {
Self {}
}
}
impl AppInfoProvider for WinAppInfoProvider {
fn get_info(&self) -> AppInfo {
AppInfo {
title: self.get_title(),
class: None,
exec: self.get_exec(),
}
}
}
impl WinAppInfoProvider {
fn get_exec(&self) -> Option<String> {
let mut buffer: [u16; 2048] = [0; 2048];
if unsafe { info_get_exec(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } != 0 {
let string = unsafe { U16CStr::from_ptr_str(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string)
} else {
None
}
} else {
None
}
}
fn get_title(&self) -> Option<String> {
let mut buffer: [u16; 2048] = [0; 2048];
if unsafe { info_get_title(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } > 0 {
let string = unsafe { U16CStr::from_ptr_str(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string)
} else {
None
}
} else {
None
}
}
}

View File

@ -0,0 +1,65 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#include "native.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <memory>
#include <array>
#define UNICODE
#ifdef __MINGW32__
#ifndef WINVER
#define WINVER 0x0606
#endif
#define STRSAFE_NO_DEPRECATE
#endif
#include <windows.h>
#include <winuser.h>
#include <strsafe.h>
#include <Windows.h>
int32_t info_get_title(wchar_t *buffer, int32_t buffer_size)
{
HWND hwnd = GetForegroundWindow();
return GetWindowText(hwnd, buffer, buffer_size);
}
int32_t info_get_exec(wchar_t *buffer, int32_t buffer_size)
{
HWND hwnd = GetForegroundWindow();
// Extract the window PID
DWORD windowPid;
GetWindowThreadProcessId(hwnd, &windowPid);
DWORD dsize = (DWORD)buffer_size;
// Extract the process executable file path
HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, windowPid);
int res = QueryFullProcessImageNameW(process, 0, buffer, &dsize);
CloseHandle(process);
return res;
}

View File

@ -0,0 +1,28 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef ESPANSO_INFO_H
#define ESPANSO_INFO_H
#include <stdint.h>
extern "C" int32_t info_get_title(wchar_t * buffer, int32_t buffer_size);
extern "C" int32_t info_get_exec(wchar_t * buffer, int32_t buffer_size);
#endif //ESPANSO_INFO_H

View File

@ -0,0 +1,27 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::os::raw::c_char;
#[link(name = "espansoinfo", kind = "static")]
extern "C" {
pub fn info_get_title(buffer: *mut c_char, buffer_size: i32) -> i32;
pub fn info_get_exec(buffer: *mut c_char, buffer_size: i32) -> i32;
pub fn info_get_class(buffer: *mut c_char, buffer_size: i32) -> i32;
}

View File

@ -0,0 +1,91 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::{ffi::CStr, os::raw::c_char};
use crate::{AppInfo, AppInfoProvider};
use self::ffi::{info_get_class, info_get_exec, info_get_title};
mod ffi;
pub struct X11AppInfoProvider {}
impl X11AppInfoProvider {
pub fn new() -> Self {
Self {}
}
}
impl AppInfoProvider for X11AppInfoProvider {
fn get_info(&self) -> AppInfo {
AppInfo {
title: self.get_title(),
class: self.get_class(),
exec: self.get_exec(),
}
}
}
impl X11AppInfoProvider {
fn get_exec(&self) -> Option<String> {
let mut buffer: [c_char; 2048] = [0; 2048];
if unsafe { info_get_exec(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } > 0 {
let string = unsafe { CStr::from_ptr(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string.to_string())
} else {
None
}
} else {
None
}
}
fn get_class(&self) -> Option<String> {
let mut buffer: [c_char; 2048] = [0; 2048];
if unsafe { info_get_class(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } > 0 {
let string = unsafe { CStr::from_ptr(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string.to_string())
} else {
None
}
} else {
None
}
}
fn get_title(&self) -> Option<String> {
let mut buffer: [c_char; 2048] = [0; 2048];
if unsafe { info_get_title(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) } > 0 {
let string = unsafe { CStr::from_ptr(buffer.as_ptr()) };
let string = string.to_string_lossy();
if !string.is_empty() {
Some(string.to_string())
} else {
None
}
} else {
None
}
}
}

View File

@ -0,0 +1,204 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#include "native.h"
#include <X11/Xlibint.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysymdef.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Function taken from the wmlib tool source code
char *get_property(Display *disp, Window win,
Atom xa_prop_type, char *prop_name, unsigned long *size)
{
unsigned long ret_nitems, ret_bytes_after, tmp_size;
Atom xa_prop_name, xa_ret_type;
unsigned char *ret_prop;
int ret_format;
char *ret;
int size_in_byte;
xa_prop_name = XInternAtom(disp, prop_name, False);
if (XGetWindowProperty(disp, win, xa_prop_name, 0, 4096 / 4, False,
xa_prop_type, &xa_ret_type, &ret_format, &ret_nitems,
&ret_bytes_after, &ret_prop) != Success)
return NULL;
if (xa_ret_type != xa_prop_type)
{
XFree(ret_prop);
return NULL;
}
switch (ret_format)
{
case 8:
size_in_byte = sizeof(char);
break;
case 16:
size_in_byte = sizeof(short);
break;
case 32:
size_in_byte = sizeof(long);
break;
}
tmp_size = size_in_byte * ret_nitems;
ret = (char *)malloc(tmp_size + 1);
memcpy(ret, ret_prop, tmp_size);
ret[tmp_size] = '\0';
if (size)
*size = tmp_size;
XFree(ret_prop);
return ret;
}
// Function taken from Window Management Library for Ruby
char *xwm_get_win_title(Display *disp, Window win)
{
char *wname = (char *)get_property(disp, win, XA_STRING, "WM_NAME", NULL);
char *nwname = (char *)get_property(disp, win, XInternAtom(disp, "UTF8_STRING", False), "_NET_WM_NAME", NULL);
return nwname ? nwname : (wname ? wname : NULL);
}
int32_t info_get_title(char *buffer, int32_t buffer_size)
{
Display *display = XOpenDisplay(0);
if (!display)
{
return -1;
}
Window focused;
int revert_to;
int ret = XGetInputFocus(display, &focused, &revert_to);
int result = 1;
if (!ret)
{
fprintf(stderr, "xdo_get_active_window reported an error\n");
result = -2;
}
else
{
char *title = xwm_get_win_title(display, focused);
snprintf(buffer, buffer_size, "%s", title);
XFree(title);
}
XCloseDisplay(display);
return result;
}
int32_t info_get_exec(char *buffer, int32_t buffer_size)
{
Display *display = XOpenDisplay(0);
if (!display)
{
return -1;
}
Window focused;
int revert_to;
int ret = XGetInputFocus(display, &focused, &revert_to);
int result = 1;
if (!ret)
{
fprintf(stderr, "xdo_get_active_window reported an error\n");
result = -2;
}
else
{
// Get the window process PID
char *pid_raw = (char *)get_property(display, focused, XA_CARDINAL, "_NET_WM_PID", NULL);
if (pid_raw == NULL)
{
result = -3;
}
else
{
int pid = pid_raw[0] | pid_raw[1] << 8 | pid_raw[2] << 16 | pid_raw[3] << 24;
// Get the executable path from it
char proc_path[250];
snprintf(proc_path, 250, "/proc/%d/exe", pid);
readlink(proc_path, buffer, buffer_size);
XFree(pid_raw);
}
}
XCloseDisplay(display);
return result;
}
int32_t info_get_class(char *buffer, int32_t buffer_size)
{
Display *display = XOpenDisplay(0);
if (!display)
{
return -1;
}
Window focused;
int revert_to;
int ret = XGetInputFocus(display, &focused, &revert_to);
int result = 1;
if (!ret)
{
fprintf(stderr, "xdo_get_active_window reported an error\n");
result = -2;
}
else
{
XClassHint hint;
if (XGetClassHint(display, focused, &hint))
{
snprintf(buffer, buffer_size, "%s", hint.res_class);
XFree(hint.res_name);
XFree(hint.res_class);
}
}
XCloseDisplay(display);
return result;
}

View File

@ -0,0 +1,29 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso 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 3 of the License, or
* (at your option) any later version.
*
* espanso 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 espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef ESPANSO_INFO_H
#define ESPANSO_INFO_H
#include <stdint.h>
extern "C" int32_t info_get_title(char * buffer, int32_t buffer_size);
extern "C" int32_t info_get_exec(char * buffer, int32_t buffer_size);
extern "C" int32_t info_get_class(char * buffer, int32_t buffer_size);
#endif //ESPANSO_INFO_H

View File

@ -11,7 +11,7 @@ edition = "2018"
[features]
# If the wayland feature is enabled, all X11 dependencies will be dropped
# and only methods suitable for Wayland will be used
wayland = ["espanso-detect/wayland", "espanso-inject/wayland", "espanso-clipboard/wayland"]
wayland = ["espanso-detect/wayland", "espanso-inject/wayland", "espanso-clipboard/wayland", "espanso-info/wayland"]
[dependencies]
espanso-detect = { path = "../espanso-detect" }
@ -20,5 +20,6 @@ espanso-inject = { path = "../espanso-inject" }
espanso-config = { path = "../espanso-config" }
espanso-match = { path = "../espanso-match" }
espanso-clipboard = { path = "../espanso-clipboard" }
espanso-info = { path = "../espanso-info" }
maplit = "1.0.2"
simplelog = "0.9.0"

View File

@ -23,9 +23,6 @@ fn main() {
])
.unwrap();
let clipboard = espanso_clipboard::get_clipboard(Default::default()).unwrap();
println!("clipboard: {:?}", clipboard.get_text());
// let icon_paths = vec![
// (
// espanso_ui::icons::TrayIcon::Normal,
@ -80,6 +77,7 @@ fn main() {
})
.unwrap();
let clipboard = espanso_clipboard::get_clipboard(Default::default()).unwrap();
let provider = espanso_info::get_provider().unwrap();
source.initialize().unwrap();
source
.eventloop(Box::new(move |event: InputEvent| {
@ -96,6 +94,9 @@ fn main() {
//std::thread::sleep(std::time::Duration::from_secs(2));
//injector.send_key_combination(&[keys::Key::Control, keys::Key::V], Default::default()).unwrap();
}
if evt.key == espanso_detect::event::Key::Shift && evt.status == Status::Released {
println!("info {:?}", provider.get_info());
}
}
InputEvent::HotKey(hotkey) => {
if hotkey.hotkey_id == 2 {