Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix switching width and height #292

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn main() -> Result<(), String> {
let output = globals
.registry()
.bind(global.name, global.version, &qh, ());
daemon.new_output(&qh, output);
daemon.new_output(&qh, output, global.name);
} else {
error!("wl_output must be at least version 2 for swww-daemon!")
}
Expand Down Expand Up @@ -469,7 +469,7 @@ impl Daemon {
.collect()
}

fn new_output(&mut self, qh: &QueueHandle<Self>, output: wl_output::WlOutput) {
fn new_output(&mut self, qh: &QueueHandle<Self>, output: wl_output::WlOutput, name: u32) {
if PIXEL_FORMAT.get().is_none() {
assert!(PIXEL_FORMAT.set(self.pixel_format).is_ok());
log::info!("Selected wl_shm format: {:?}", self.shm_format);
Expand Down Expand Up @@ -501,6 +501,7 @@ impl Daemon {
debug!("New output: {}", output.id());
self.wallpapers.push(Arc::new(Wallpaper::new(
output,
name,
surface,
wp_viewport,
wp_fractional,
Expand All @@ -521,10 +522,28 @@ impl Dispatch<wl_output::WlOutput, ()> for Daemon {
_qhandle: &QueueHandle<Self>,
) {
for wallpaper in state.wallpapers.iter_mut() {
if wallpaper.has_id(proxy.id().protocol_id()) {
if wallpaper.has_output(proxy) {
match event {
wl_output::Event::Geometry { x, y, .. } => {
debug!("output {} position: {x},{y}", proxy.id())
wl_output::Event::Geometry {
x, y, transform, ..
} => {
debug!("output {} position: {x},{y}", proxy.id());
match transform {
wayland_client::WEnum::Value(v) => match v {
wl_output::Transform::_90
| wl_output::Transform::_270
| wl_output::Transform::Flipped90
| wl_output::Transform::Flipped270 => wallpaper.set_vertical(),
wl_output::Transform::Normal
| wl_output::Transform::_180
| wl_output::Transform::Flipped
| wl_output::Transform::Flipped180 => wallpaper.set_horizontal(),
e => warn!("unprocessed transform: {e:?}"),
},
wayland_client::WEnum::Unknown(u) => {
error!("received unknown transfrom from compositor: {u}")
}
}
}
wl_output::Event::Mode {
flags: _flags,
Expand Down Expand Up @@ -691,13 +710,13 @@ impl Dispatch<WlRegistry, GlobalListContents> for Daemon {
error!("your compositor must support at least version 2 of wl_output");
} else {
let output = proxy.bind(name, version, qh, ());
state.new_output(qh, output)
state.new_output(qh, output, name)
}
}
}

wayland_client::protocol::wl_registry::Event::GlobalRemove { name } => {
state.wallpapers.retain(|w| !w.has_id(name));
state.wallpapers.retain(|w| !w.has_output_name(name));

debug!("Destroyed output with id: {name}");
}
Expand Down
41 changes: 39 additions & 2 deletions daemon/src/wallpaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct WallpaperInner {
width: NonZeroI32,
height: NonZeroI32,
scale_factor: Scale,
is_vertical: bool,
}

impl Default for WallpaperInner {
Expand All @@ -73,12 +74,14 @@ impl Default for WallpaperInner {
width: unsafe { NonZeroI32::new_unchecked(4) },
height: unsafe { NonZeroI32::new_unchecked(4) },
scale_factor: Scale::Whole(unsafe { NonZeroI32::new_unchecked(1) }),
is_vertical: false,
}
}
}

pub(super) struct Wallpaper {
output: WlOutput,
output_name: u32,
wl_surface: WlSurface,
wp_viewport: WpViewport,
#[allow(unused)]
Expand All @@ -98,8 +101,10 @@ pub(super) struct Wallpaper {
}

impl Wallpaper {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
output: WlOutput,
output_name: u32,
wl_surface: WlSurface,
wp_viewport: WpViewport,
wp_fractional: Option<WpFractionalScaleV1>,
Expand Down Expand Up @@ -131,6 +136,7 @@ impl Wallpaper {

Self {
output,
output_name,
wl_surface,
wp_viewport,
wp_fractional,
Expand Down Expand Up @@ -197,6 +203,32 @@ impl Wallpaper {
)
}
}

if lock.is_vertical {
if lock.width > lock.height {
let t = lock.width;
lock.width = lock.height;
lock.height = t;
}
} else {
if lock.width < lock.height {
let t = lock.width;
lock.width = lock.height;
lock.height = t;
}
}
}

#[inline]
pub fn set_vertical(&self) {
let mut lock = self.inner_staging.lock().unwrap();
lock.is_vertical = true;
}

#[inline]
pub fn set_horizontal(&self) {
let mut lock = self.inner_staging.lock().unwrap();
lock.is_vertical = false;
}

#[inline]
Expand Down Expand Up @@ -306,8 +338,13 @@ impl Wallpaper {
}

#[inline]
pub(super) fn has_id(&self, id: u32) -> bool {
wayland_client::Proxy::id(&self.output).protocol_id() == id
pub(super) fn has_output(&self, output: &WlOutput) -> bool {
self.output == *output
}

#[inline]
pub(super) fn has_output_name(&self, name: u32) -> bool {
self.output_name == name
}

#[inline]
Expand Down
14 changes: 12 additions & 2 deletions utils/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ impl Scale {
pub fn mul_dim(&self, width: i32, height: i32) -> (i32, i32) {
match self {
Scale::Whole(i) => (width * i.get(), height * i.get()),
Scale::Fractional(f) => ((width * f.get() + 60) / 120, (height * f.get() + 60) / 120),
Scale::Fractional(f) => {
let scale = f.get() as f64 / 120.0;
let width = (width as f64 * scale).round() as i32;
let height = (height as f64 * scale).round() as i32;
(width, height)
}
}
}

Expand All @@ -156,7 +161,12 @@ impl Scale {
pub fn div_dim(&self, width: i32, height: i32) -> (i32, i32) {
match self {
Scale::Whole(i) => (width / i.get(), height / i.get()),
Scale::Fractional(f) => ((width * 120) / f.get(), (height * 120) / f.get()),
Scale::Fractional(f) => {
let scale = 120.0 / f.get() as f64;
let width = (width as f64 * scale).round() as i32;
let height = (height as f64 * scale).round() as i32;
(width, height)
}
}
}
}
Expand Down
Loading