From ec1e3ab6b2311e796fde39a9f203980efc5f9e25 Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Sat, 5 Oct 2024 01:52:53 +0900 Subject: [PATCH] chore: bump version --- Cargo.lock | 5 +- Cargo.toml | 42 +++++++++++----- libharuhishot/Cargo.toml | 14 +++--- libharuhishot/src/convert.rs | 81 +++++++++++++++++++++++++++++++ libharuhishot/src/haruhierror.rs | 2 + libharuhishot/src/lib.rs | 1 + libharuhishot/src/wlrcopystate.rs | 15 +++++- meson.build | 42 ++++++---------- src/filewriter.rs | 8 +-- 9 files changed, 158 insertions(+), 52 deletions(-) create mode 100644 libharuhishot/src/convert.rs diff --git a/Cargo.lock b/Cargo.lock index 19c0c80..6f136ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2008,7 +2008,7 @@ dependencies = [ [[package]] name = "haruhishot" -version = "0.3.19" +version = "0.4.0" dependencies = [ "clap", "dialoguer", @@ -2643,8 +2643,9 @@ dependencies = [ [[package]] name = "libharuhishot" -version = "0.2.5" +version = "0.4.0" dependencies = [ + "image 0.25.2", "memmap2 0.9.5", "nix 0.29.0", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 2d99b03..baf0093 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,38 +1,58 @@ +[workspace.package] +version = "0.4.0" +edition = "2021" +license = "MIT" +description = "impl screencopy for wayland" +authors = ["Decodertalkers "] +homepage = "https://github.com/Decodetalkers/haruhishot" +documentation = "https://docs.rs/libharuhishot/" +keywords = ["wayland"] + [package] name = "haruhishot" -version = "0.3.19" -edition = "2021" +version.workspace = true +edition.workspace = true +license.workspace = true +description = "haruhishot" +authors.workspace = true +homepage.workspace = true +keywords.workspace = true build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [workspace] members = [".", "libharuhishot"] -[dependencies] - -libharuhishot = { path = "libharuhishot", version = "0.2.5" } - -#wayland-client = "=0.30.0-beta.13" - +[workspace.dependencies] image = { version = "0.25", default-features = false, features = [ "jpeg", "png", "pnm", ] } +wayland-client = "0.31" +tracing-subscriber = "0.3.18" +tracing = "0.1.40" + +[dependencies] + +libharuhishot = { path = "libharuhishot", version = "0.4.0" } + +#wayland-client = "=0.30.0-beta.13" +image.workspace = true sctk = { version = "0.18.1", package = "smithay-client-toolkit", optional = true } # in the feature slint = { version = "1.8.0", optional = true } -tracing-subscriber = "0.3.18" -tracing = "0.1.40" +tracing-subscriber.workspace = true +tracing.workspace = true clap = "4.5.19" once_cell = "1.20.1" dialoguer = { version = "0.11.0", features = ["fuzzy-select"] } -wayland-client = { version = "0.31", optional = true } +wayland-client = { workspace = true, optional = true } notify-rust = { version = "4.11.3", optional = true, features = ["images"] } xkbcommon = "0.8.0" swayipc = { version = "3.0.2", optional = true } diff --git a/libharuhishot/Cargo.toml b/libharuhishot/Cargo.toml index 2b1881e..a1eb424 100644 --- a/libharuhishot/Cargo.toml +++ b/libharuhishot/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "libharuhishot" -version = "0.2.5" -edition = "2021" -license = "MIT" +version.workspace = true +edition.workspace = true +license.workspace = true description = "impl screencopy for wayland" -authors = ["Decodertalkers "] -homepage = "https://github.com/Decodetalkers/haruhishot" +authors.workspace = true +homepage.workspace = true documentation = "https://docs.rs/libharuhishot/" keywords = ["wayland"] readme = "README.md" @@ -23,7 +23,7 @@ wayland-protocols = { version = "0.32.4", default-features = false, features = [ wayland-protocols-wlr = { version = "0.3.4", default-features = false, features = [ "client", ] } -wayland-client = "0.31" +wayland-client.workspace = true #wayland-client = "=0.30.0-beta.13" nix = { version = "0.29.0", features = ["fs", "mman"] } @@ -34,3 +34,5 @@ memmap2 = "0.9.5" tracing = "0.1.40" thiserror = "1.0.64" + +image.workspace = true diff --git a/libharuhishot/src/convert.rs b/libharuhishot/src/convert.rs new file mode 100644 index 0000000..8959bf5 --- /dev/null +++ b/libharuhishot/src/convert.rs @@ -0,0 +1,81 @@ +use image::ColorType; +use wayland_client::protocol::wl_shm; + +pub trait Convert { + /// Convert raw image data into output type, return said type + fn convert_inplace(&self, data: &mut [u8]) -> ColorType; +} + +#[derive(Default)] +struct ConvertBGR10; + +#[derive(Default)] +struct ConvertNone; + +#[derive(Default)] +struct ConvertRGB8; + +#[derive(Default)] +struct ConvertBGR888; + +const SHIFT10BITS_1: u32 = 20; +const SHIFT10BITS_2: u32 = 10; + +/// Creates format converter based of input format, return None if conversion +/// isn't possible. Conversion is happening inplace. +pub fn create_converter(format: wl_shm::Format) -> Option> { + match format { + wl_shm::Format::Xbgr8888 | wl_shm::Format::Abgr8888 => Some(Box::::default()), + wl_shm::Format::Xrgb8888 | wl_shm::Format::Argb8888 => Some(Box::::default()), + wl_shm::Format::Xbgr2101010 | wl_shm::Format::Abgr2101010 => { + Some(Box::::default()) + } + wl_shm::Format::Bgr888 => Some(Box::::default()), + _ => None, + } +} + +impl Convert for ConvertNone { + fn convert_inplace(&self, _data: &mut [u8]) -> ColorType { + ColorType::Rgba8 + } +} + +impl Convert for ConvertRGB8 { + fn convert_inplace(&self, data: &mut [u8]) -> ColorType { + for chunk in data.chunks_exact_mut(4) { + chunk.swap(0, 2); + } + ColorType::Rgba8 + } +} + +/// Simple conversion from 10 to 8 bits for one channel +fn convert10_to_8(color: u32) -> u8 { + ((color >> 2) & 255) as u8 +} + +impl Convert for ConvertBGR10 { + fn convert_inplace(&self, data: &mut [u8]) -> ColorType { + for chunk in data.chunks_exact_mut(4) { + let pixel = ((chunk[3] as u32) << 24) + | ((chunk[2] as u32) << 16) + | ((chunk[1] as u32) << 8) + | chunk[0] as u32; + let r = convert10_to_8(pixel >> SHIFT10BITS_1); + let g = convert10_to_8(pixel >> SHIFT10BITS_2); + let b = convert10_to_8(pixel); + chunk[0] = b; + chunk[1] = g; + chunk[2] = r; + chunk[3] = 255; + } + ColorType::Rgba8 + } +} + +impl Convert for ConvertBGR888 { + fn convert_inplace(&self, _data: &mut [u8]) -> ColorType { + ColorType::Rgb8 + } +} diff --git a/libharuhishot/src/haruhierror.rs b/libharuhishot/src/haruhierror.rs index 22d8663..899db9c 100644 --- a/libharuhishot/src/haruhierror.rs +++ b/libharuhishot/src/haruhierror.rs @@ -14,4 +14,6 @@ pub enum HaruhiError { QueueError(String), #[error("Error in write image in shm")] ShmError(#[from] io::Error), + #[error("Not Support format")] + NotSupportFormat } diff --git a/libharuhishot/src/lib.rs b/libharuhishot/src/lib.rs index f653aed..6dd40fe 100644 --- a/libharuhishot/src/lib.rs +++ b/libharuhishot/src/lib.rs @@ -24,6 +24,7 @@ pub mod haruhierror; pub mod wlrcopystate; pub mod wlrshotbasestate; +pub mod convert; pub use wlrcopystate::{FrameFormat, FrameInfo}; pub use wlrshotbasestate::HaruhiShotState; diff --git a/libharuhishot/src/wlrcopystate.rs b/libharuhishot/src/wlrcopystate.rs index 8f72666..3994e10 100644 --- a/libharuhishot/src/wlrcopystate.rs +++ b/libharuhishot/src/wlrcopystate.rs @@ -1,3 +1,4 @@ +use image::ColorType; use wayland_client::protocol::wl_buffer::WlBuffer; use wayland_client::protocol::wl_output::{self, WlOutput}; use wayland_client::protocol::wl_shm::{self, Format}; @@ -21,6 +22,7 @@ use nix::{ unistd, }; +use crate::convert; use memmap2::MmapMut; use crate::haruhierror::HaruhiError; @@ -81,6 +83,8 @@ pub struct FrameInfo { pub frameformat: FrameFormat, /// frame_mmap: contain the information of an image pub frame_mmap: MmapMut, + + pub frame_color_type: ColorType, /// transform: how the screen is layed pub transform: wl_output::Transform, /// realwidth: same to above @@ -321,11 +325,17 @@ impl HaruhiShotState { height, } => manager.capture_output_region(0, output, x, y, width, height, &qh, ()), }; - let mut frameformat = None; - let mut frame_mmap = None; + let mut frameformat: Option = None; + let mut frame_mmap: Option = None; + let frame_color_type; loop { self.block_dispatch()?; if self.finished() { + let frame_mmap = frame_mmap.as_mut().unwrap(); + let frame_format = frameformat.as_ref().unwrap(); + let frame_color_type_converter = convert::create_converter(frame_format.format) + .ok_or(HaruhiError::NotSupportFormat)?; + frame_color_type = frame_color_type_converter.convert_inplace(frame_mmap); break; } if self.is_pedding() { @@ -381,6 +391,7 @@ impl HaruhiShotState { let output = FrameInfo { frameformat: frameformat.unwrap(), frame_mmap: frame_mmap.unwrap(), + frame_color_type, transform, realwidth: realwidth as u32, realheight: realheight as u32, diff --git a/meson.build b/meson.build index 0c18d2c..80166c7 100644 --- a/meson.build +++ b/meson.build @@ -1,15 +1,12 @@ -project('haruhishot', 'rust', - version: '0.3.19', - meson_version: '>= 0.60' -) +project('haruhishot', 'rust', version: '0.4.0', meson_version: '>= 0.60') dependency('wayland-client') cargo = find_program('cargo', version: '>= 1.66') -rustc = find_program('rustc', version: '>= 1.66') +find_program('rustc', version: '>= 1.66') -command = [ cargo , 'build'] +command = [cargo, 'build'] if get_option('enable-notify') command += ['--features', 'notify'] @@ -34,7 +31,7 @@ command += [ '&&', 'cp', meson.global_source_root() / 'target' / targetdir / meson.project_name(), - '@OUTPUT@' + '@OUTPUT@', ] prefix = get_option('prefix') @@ -42,31 +39,24 @@ bindir = prefix / get_option('bindir') datadir = prefix / get_option('datadir') icondir = datadir / 'pixmaps' -haruhishot_target = custom_target(meson.project_name(), +custom_target( + meson.project_name(), output: meson.project_name(), build_by_default: true, install: true, install_dir: bindir, console: true, - command: command + command: command, ) if get_option('enable-notify') - install_data('misc/haruhi_failed.png', - install_dir: icondir - ) - install_data('misc/haruhi_successed.png', - install_dir: icondir - ) + install_data('misc/haruhi_failed.png', install_dir: icondir) + install_data('misc/haruhi_successed.png', install_dir: icondir) endif if get_option('man-pages') manpage = get_option('mandir') - scdoc = dependency( - 'scdoc', - version: '>= 1.9.7', - native: true - ) + scdoc = dependency('scdoc', version: '>= 1.9.7', native: true) if scdoc.found() custom_target( @@ -77,18 +67,16 @@ if get_option('man-pages') feed: true, capture: true, install: true, - install_dir: prefix / manpage / 'man1' + install_dir: prefix / manpage / 'man1', ) endif endif # Install desktop entry if get_option('desktop-entry') - install_data('misc/haruhishot.desktop', - install_dir: datadir / 'applications' - ) - install_data('misc/haruhishot.svg', - install_dir: datadir / 'icons' / 'hicolor' / 'scalable' / 'apps' + install_data('misc/haruhishot.desktop', install_dir: datadir / 'applications') + install_data( + 'misc/haruhishot.svg', + install_dir: datadir / 'icons' / 'hicolor' / 'scalable' / 'apps', ) endif - diff --git a/src/filewriter.rs b/src/filewriter.rs index 1a1acce..b36cc63 100644 --- a/src/filewriter.rs +++ b/src/filewriter.rs @@ -19,7 +19,7 @@ pub fn get_color(bufferdata: FrameInfo) { &bufferdata.frame_mmap, bufferdata.frameformat.width, bufferdata.frameformat.height, - image::ColorType::Rgba8.into(), + bufferdata.frame_color_type.into(), ) .unwrap(); let image = @@ -43,7 +43,7 @@ pub fn write_to_file(bufferdata: FrameInfo, usestdout: bool) { &bufferdata.frame_mmap, bufferdata.frameformat.width, bufferdata.frameformat.height, - image::ColorType::Rgba8.into(), + bufferdata.frame_color_type.into(), ) { #[cfg(feature = "notify")] let _ = Notification::new() @@ -96,7 +96,7 @@ pub fn write_to_file(bufferdata: FrameInfo, usestdout: bool) { &bufferdata.frame_mmap, bufferdata.frameformat.width, bufferdata.frameformat.height, - image::ColorType::Rgba8.into(), + bufferdata.frame_color_type.into(), ) .is_ok() { @@ -183,7 +183,7 @@ pub fn write_to_file_mutisource(bufferdatas: Vec, usestdout: bool) { &buffer.frame_mmap, buffer.frameformat.width, buffer.frameformat.height, - image::ColorType::Rgba8.into(), + buffer.frame_color_type.into(), ) .unwrap(); let image =