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

ref: Accept client-side wayland connection, global #8

Merged
merged 1 commit into from
Apr 7, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion libwaysip/examples/base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
use libwaysip::{get_area, SelectionType};
use wayland_client::{
globals::{registry_queue_init, GlobalListContents},
protocol::wl_registry,
Connection, Dispatch, QueueHandle,
};

struct State {}

impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for State {
fn event(
_: &mut State,
_: &wl_registry::WlRegistry,
_: wl_registry::Event,
_: &GlobalListContents,
_: &Connection,
_: &QueueHandle<State>,
) {
}
}

fn main() {
println!("{:?}", get_area(SelectionType::Area));
let connection = Connection::connect_to_env().unwrap();
let (globals, _) = registry_queue_init::<State>(&connection).unwrap();

println!("{:?}", get_area(&connection, globals, SelectionType::Area));
}
13 changes: 6 additions & 7 deletions libwaysip/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl Dispatch<zxdg_output_v1::ZxdgOutputV1, ()> for WaysipState {
_qhandle: &wayland_client::QueueHandle<Self>,
) {
let Some(info) = state
.zxdgoutputs
.zxdg_outputs
.iter_mut()
.find(|info| info.zxdgoutput == *proxy)
.find(|info| info.zxdg_output == *proxy)
else {
return;
};
Expand All @@ -84,7 +84,6 @@ impl Dispatch<zxdg_output_v1::ZxdgOutputV1, ()> for WaysipState {
}
}

// so interesting, it is just need to invoke once, it just used to get the globals
impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for state::WaysipState {
fn event(
_state: &mut Self,
Expand Down Expand Up @@ -259,8 +258,8 @@ impl Dispatch<wl_pointer::WlPointer, ()> for state::WaysipState {
.position(|info| info.wl_surface == surface)
.unwrap();
dispatch_state.current_screen = current_screen;
let start_x = dispatch_state.zxdgoutputs[dispatch_state.current_screen].start_x;
let start_y = dispatch_state.zxdgoutputs[dispatch_state.current_screen].start_y;
let start_x = dispatch_state.zxdg_outputs[dispatch_state.current_screen].start_x;
let start_y = dispatch_state.zxdg_outputs[dispatch_state.current_screen].start_y;
dispatch_state.current_pos =
(surface_x + start_x as f64, surface_y + start_y as f64);

Expand Down Expand Up @@ -291,8 +290,8 @@ impl Dispatch<wl_pointer::WlPointer, ()> for state::WaysipState {
surface_y,
..
} => {
let start_x = dispatch_state.zxdgoutputs[dispatch_state.current_screen].start_x;
let start_y = dispatch_state.zxdgoutputs[dispatch_state.current_screen].start_y;
let start_x = dispatch_state.zxdg_outputs[dispatch_state.current_screen].start_x;
let start_y = dispatch_state.zxdg_outputs[dispatch_state.current_screen].start_y;
dispatch_state.current_pos =
(surface_x + start_x as f64, surface_y + start_y as f64);
if dispatch_state.is_area() {
Expand Down
25 changes: 9 additions & 16 deletions libwaysip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use error::WaySipError;
pub use state::SelectionType;
use std::os::unix::prelude::AsFd;
use wayland_client::{
globals::registry_queue_init,
globals::GlobalList,
protocol::{
wl_compositor::WlCompositor,
wl_seat::WlSeat,
Expand Down Expand Up @@ -36,19 +36,12 @@ fn get_cursor_buffer(connection: &Connection, shm: &WlShm) -> Option<CursorImage
}

/// get the selected area
pub fn get_area(kind: SelectionType) -> Result<Option<state::AreaInfo>, WaySipError> {
let connection =
Connection::connect_to_env().map_err(|e| WaySipError::InitFailed(e.to_string()))?;
let (globals, _) = registry_queue_init::<state::WaysipState>(&connection)
.map_err(|e| WaySipError::InitFailed(e.to_string()))?; // We just need the
// global, the
// event_queue is
// not needed, we
// do not need
// state::BaseState after
// this anymore

let mut state = state::WaysipState::new(kind);
pub fn get_area(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the origin api should be reserved.Sometimes I do not want to induce wayland-client

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add like get_area_with_connection like this kind , maybe add one extra function will be better I think.

Copy link
Collaborator

@Decodetalkers Decodetalkers Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or make connection and globals optional I think will be better. if it is none, make a new connection and globals

like create a struct

WaysipWaylandInfo<'a> {
    globals: GlobalList,
   connection: &'a Connection
}

connection: &Connection,
globals: GlobalList,
selection_type: SelectionType,
) -> Result<Option<state::AreaInfo>, WaySipError> {
let mut state = state::WaysipState::new(selection_type);

let mut event_queue = connection.new_event_queue::<state::WaysipState>();
let qh = event_queue.handle();
Expand Down Expand Up @@ -93,7 +86,7 @@ pub fn get_area(kind: SelectionType) -> Result<Option<state::AreaInfo>, WaySipEr
for wloutput in state.outputs.iter() {
let zwloutput = xdg_output_manager.get_xdg_output(wloutput.get_output(), &qh, ());
state
.zxdgoutputs
.zxdg_outputs
.push(state::ZXdgOutputInfo::new(zwloutput));
}

Expand All @@ -115,7 +108,7 @@ pub fn get_area(kind: SelectionType) -> Result<Option<state::AreaInfo>, WaySipEr
for (index, (wloutput, zwlinfo)) in state
.outputs
.iter()
.zip(state.zxdgoutputs.iter())
.zip(state.zxdg_outputs.iter())
.enumerate()
{
let wl_surface = wmcompositer.create_surface(&qh, ()); // and create a surface. if two or more,
Expand Down
24 changes: 12 additions & 12 deletions libwaysip/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum SelectionType {

#[derive(Debug)]
pub struct ZXdgOutputInfo {
pub zxdgoutput: zxdg_output_v1::ZxdgOutputV1,
pub zxdg_output: zxdg_output_v1::ZxdgOutputV1,
pub width: i32,
pub height: i32,
pub start_x: i32,
Expand All @@ -30,7 +30,7 @@ pub struct ZXdgOutputInfo {
impl ZXdgOutputInfo {
pub fn new(zxdgoutput: zxdg_output_v1::ZxdgOutputV1) -> Self {
Self {
zxdgoutput,
zxdg_output: zxdgoutput,
width: 0,
height: 0,
start_x: 0,
Expand Down Expand Up @@ -130,9 +130,9 @@ impl ScreenInfo {
#[derive(Debug)]
pub struct WaysipState {
pub outputs: Vec<WlOutputInfo>,
pub zxdgoutputs: Vec<ZXdgOutputInfo>,
pub zxdg_outputs: Vec<ZXdgOutputInfo>,
pub running: bool,
pub waysipkind: SelectionType,
pub selection_type: SelectionType,
pub wl_surfaces: Vec<LayerSurfaceInfo>,
pub current_pos: (f64, f64),
pub start_pos: Option<(f64, f64)>,
Expand All @@ -142,12 +142,12 @@ pub struct WaysipState {
}

impl WaysipState {
pub fn new(waysipkind: SelectionType) -> Self {
pub fn new(selection_type: SelectionType) -> Self {
WaysipState {
outputs: Vec::new(),
zxdgoutputs: Vec::new(),
zxdg_outputs: Vec::new(),
running: true,
waysipkind,
selection_type,
wl_surfaces: Vec::new(),
current_pos: (0., 0.),
start_pos: None,
Expand All @@ -158,11 +158,11 @@ impl WaysipState {
}

pub fn is_area(&self) -> bool {
matches!(self.waysipkind, SelectionType::Area)
matches!(self.selection_type, SelectionType::Area)
}

pub fn is_screen(&self) -> bool {
matches!(self.waysipkind, SelectionType::Screen)
matches!(self.selection_type, SelectionType::Screen)
}

pub fn redraw_screen(&self) {
Expand All @@ -181,7 +181,7 @@ impl WaysipState {
.wl_surfaces
.iter()
.enumerate()
.zip(self.zxdgoutputs.iter())
.zip(self.zxdg_outputs.iter())
{
info.redraw_select_screen(
self.current_screen == index,
Expand All @@ -206,7 +206,7 @@ impl WaysipState {
..
},
layershell_info,
) in self.zxdgoutputs.iter().zip(self.wl_surfaces.iter())
) in self.zxdg_outputs.iter().zip(self.wl_surfaces.iter())
{
layershell_info.redraw(
(pos_x, pos_y),
Expand All @@ -224,7 +224,7 @@ impl WaysipState {
let (start_x, start_y) = self.start_pos.unwrap();
let (end_x, end_y) = self.end_pos.unwrap();
let output = self.outputs[self.current_screen].clone();
let info = &self.zxdgoutputs[self.current_screen];
let info = &self.zxdg_outputs[self.current_screen];
Some(AreaInfo {
start_x,
start_y,
Expand Down
1 change: 1 addition & 0 deletions waysip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ clap = { version = "4.4.10", features = ["derive"] }
libwaysip.workspace = true
tracing.workspace = true
tracing-subscriber = "0.3"
wayland-client = "0.31.2"
11 changes: 9 additions & 2 deletions waysip/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use libwaysip::{get_area, SelectionType};
use libwaysip::{error::WaySipError, get_area, state, SelectionType};
use std::str::FromStr;
use wayland_client::{globals::registry_queue_init, Connection};

#[derive(Debug, Parser)]
#[command(name = "waysip")]
Expand All @@ -22,14 +23,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_writer(std::io::stderr)
.init();

let connection =
Connection::connect_to_env().map_err(|e| WaySipError::InitFailed(e.to_string()))?;

let (globals, _) = registry_queue_init::<state::WaysipState>(&connection)
.map_err(|e| WaySipError::InitFailed(e.to_string()))?;

let args = Cli::parse();

// TODO: Enable tracing
// TODO: Make errors go through the cli into output

macro_rules! get_info {
($x: expr) => {
match get_area($x) {
match get_area(&connection, globals, $x) {
Ok(Some(info)) => info,
Ok(None) => {
eprintln!("Get None, you cancel it");
Expand Down
Loading