updated and refactored

This commit is contained in:
Bryan Ndjeutcha 2021-08-19 13:21:13 -04:00
parent 2bba24ddb9
commit 4f0170f8fd
3 changed files with 268 additions and 189 deletions

View File

@ -16,7 +16,7 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
</copyright>
<interface name="zriver_status_manager_v1" version="1">
<interface name="zriver_status_manager_v1" version="2">
<description summary="manage river status objects">
A global factory for objects that receive status information specific
to river. It could be used to implement, for example, a status bar.
@ -47,7 +47,7 @@
</request>
</interface>
<interface name="zriver_output_status_v1" version="1">
<interface name="zriver_output_status_v1" version="2">
<description summary="track output tags and focus">
This interface allows clients to receive information about the current
windowing state of an output.
@ -75,6 +75,14 @@
</description>
<arg name="tags" type="array" summary="array of 32-bit bitfields"/>
</event>
<event name="urgent_tags" since="2">
<description summary="tags of the output with an urgent view">
Sent once on binding the interface and again whenever the set of
tags with at least one urgent view changes.
</description>
<arg name="tags" type="uint" summary="32-bit bitfield"/>
</event>
</interface>
<interface name="zriver_seat_status_v1" version="1">

View File

@ -1,57 +1,84 @@
mod wayland;
use crate::wayland::river_status_unstable_v1::{
zriver_output_status_v1,
zriver_seat_status_v1,
zriver_status_manager_v1::ZriverStatusManagerV1,
};
use wayland_client::protocol::{
wl_seat,
wl_output::WlOutput,
wl_seat::WlSeat,
zriver_output_status_v1, zriver_seat_status_v1, zriver_status_manager_v1::ZriverStatusManagerV1,
};
use std::collections::HashMap;
use wayland_client::protocol::{wl_output, wl_output::WlOutput, wl_seat, wl_seat::WlSeat};
use wayland_client::{Display, GlobalManager, Main};
struct Globals {
seats: Vec<Main<WlSeat>>,
outputs: Vec<Main<WlOutput>>,
#[derive(Debug)]
enum Value {
Tags(u32),
Title(String),
ViewsTag(Vec<u32>),
}
#[derive(Debug)]
struct Flags {
tags: bool,
title: bool,
urgency: bool,
viewstag: bool,
output: Option<String>,
seat: Option<String>,
}
impl Flags {
fn default() -> Flags {
Flags {
tags: false,
title: false,
urgency: false,
viewstag: false,
output: None,
seat: None,
}
}
}
struct Env {
flags: Flags,
hashmap: HashMap<String, Value>,
status_manager: Option<Main<ZriverStatusManagerV1>>,
}
struct Status {
seat_name: String,
keypair: Vec<Keypair>,
}
struct Keypair {
key: String,
value: String,
}
impl Keypair {
fn to_string(&self) {
print!(r#""{}": "{}""#, self.key, self.value)
}
}
impl Status {
fn mod_value(&mut self, key: String, value: String) {
for keypair in &mut self.keypair {
if keypair.key.eq(&key) { keypair.value = value; break }
impl Env {
fn new() -> Env {
Env {
status_manager: None,
flags: configuration(),
hashmap: HashMap::new(),
}
}
fn add_keypair(&mut self, key: String) {
self.keypair.push({ Keypair {
key: key,
value: String::new()
} });
fn set_value(&mut self, key: &str, value: Value) {
if let Some(inner_value) = self.hashmap.get_mut(key) {
(*inner_value) = value;
} else {
self.hashmap.insert(key.to_string(), value);
}
}
fn to_string(&self) {
let len = self.keypair.len();
fn fmt(&self) {
print!("{{");
for (i,keypair) in self.keypair.iter().enumerate() {
keypair.to_string();
if i+1 < len {
let mut i = 0;
let len = self.hashmap.len();
for (key, val) in self.hashmap.iter() {
print!("{:?} :", key);
match val {
Value::Tags(tags) => {
print!("\"[");
fmt_tags(*tags);
print!("]\"");
}
Value::Title(title) => {
print!("{:?}", title);
}
Value::ViewsTag(tags) => {
print!("\"{:?}\"", tags);
}
}
i += 1;
if i < len {
print!(", ");
}
}
@ -60,154 +87,134 @@ impl Status {
}
fn main() {
let mut env = Env::new();
let display = Display::connect_to_env().unwrap();
let mut event_queue = display.create_event_queue();
let mut globals = {
Globals {
seats: Vec::new(),
outputs: Vec::new(),
status_manager: None,
}
};
let mut args = std::env::args();
let mut status = { Status {
seat_name: String::new(),
keypair: Vec::new()
} };
let mut monitor = None;
let mut enable_tag = false;
let mut enable_title = false;
let mut enable_views_tag = false;
args.next();
loop {
match args.next() {
Some(flag) => match flag.as_str() {
"--seat" | "-s" => status.seat_name = args.next().unwrap_or(String::new()),
"--monitor" | "-m" => {
monitor = match args.next().unwrap_or(String::new()).parse::<usize>() {
Ok(i) => Some(i),
Err(_) => None,
}
}
"--window-title" | "-w" => enable_title = true,
"--tag" | "-t" => enable_tag = true,
"--view-tags" | "-vt" => enable_views_tag = true,
"--help" | "-h" | "--h" => {
print!("Usage: ristate [option]\n\n");
print!(" -s | --seat <string> select the seat\n");
print!(" -m | --monitor <uint> select the monitor\n");
print!(" -t | --tag displays the focused tag\n");
print!(" -vt | --view-tags displays the tag of all views\n");
println!(" -w | --window-title displays the title of the focused view\n");
std::process::exit(0);
}
_ => break,
},
None => break,
}
}
let attached_display = (*display).clone().attach(event_queue.token());
let _ = GlobalManager::new_with_cb(
GlobalManager::new_with_cb(
&attached_display,
wayland_client::global_filter!(
[
ZriverStatusManagerV1,
1,
|status_manager_obj: Main<ZriverStatusManagerV1>, mut globals: DispatchData| {
globals.get::<Globals>().unwrap().status_manager = Some(status_manager_obj);
|status_manager: Main<ZriverStatusManagerV1>, mut env: DispatchData| {
if let Some(env) = env.get::<Env>() {
env.status_manager = Some(status_manager);
}
}
],
[
WlSeat,
7,
|seat: Main<WlSeat>, mut globals: DispatchData| {
globals.get::<Globals>().unwrap().seats.push(seat);
}
],
[
WlOutput,
3,
|output: Main<WlOutput>, mut globals: DispatchData| {
output.quick_assign(move |_, _, _| {});
globals.get::<Globals>().unwrap().outputs.push(output);
}
]
[WlSeat, 7, |seat: Main<WlSeat>, _env: DispatchData| {
seat.quick_assign(move |seat, event, mut env| match event {
wl_seat::Event::Name { name } => {
if let Some(env) = env.get::<Env>() {
if env.flags.title
&& (env.flags.seat.is_none()
|| name.eq(env.flags.seat.as_ref().unwrap()))
{
if let Some(status_manager) = &env.status_manager {
let seat_status = status_manager.get_river_seat_status(&seat);
seat_status.quick_assign(
move |_, event, mut env| match event {
zriver_seat_status_v1::Event::FocusedView { title } => {
if let Some(env) = env.get::<Env>() {
env.set_value("Title", Value::Title(title));
}
}
_ => {}
},
);
}
}
}
}
_ => {}
});
}],
[WlOutput, 3, |output: Main<WlOutput>, _env: DispatchData| {
output.quick_assign(move |output, event, mut env| match event {
wl_output::Event::Geometry {
x: _,
y: _,
physical_width: _,
physical_height: _,
subpixel: _,
mut make,
model: _,
transform: _,
} => {
if let Some(env) = env.get::<Env>() {
if env.flags.output.is_none()
|| env.flags.output.as_ref().unwrap().eq(&make)
{
if let Some(status_manager) = &env.status_manager {
make = make.replace(' ', "").to_string();
let tags_key = format!("Tags.{}", make);
let urgent_key = format!("UrgentTags.{}", make);
let views_key = format!("ViewsTags.{}", make);
let output_status =
status_manager.get_river_output_status(&output);
output_status.quick_assign(move |_, event, mut env| {
if let Some(env) = env.get::<Env>() {
match event {
zriver_output_status_v1::Event::FocusedTags {
tags,
} => {
if env.flags.tags {
env.set_value(&tags_key, Value::Tags(tags));
}
}
zriver_output_status_v1::Event::ViewTags {
tags,
} => {
if env.flags.viewstag {
let tags: Vec<u32> = tags[0..]
.chunks(4)
.map(|s| {
let buf = [s[0], s[1], s[2], s[3]];
let tagmask =
u32::from_le_bytes(buf);
for i in 0..32 {
if 1 << i == tagmask {
return i;
}
}
0
})
.collect();
env.set_value(
&views_key,
Value::ViewsTag(tags),
);
}
}
zriver_output_status_v1::Event::UrgentTags {
tags,
} => {
if env.flags.urgency {
env.set_value(
&urgent_key,
Value::Tags(tags),
);
}
}
}
}
});
}
}
}
}
_ => {}
});
}]
),
);
event_queue
.sync_roundtrip(&mut globals, |_, _, _| unreachable!())
.unwrap();
for seat in globals.seats {
if enable_title {
let seat_status = globals
.status_manager
.as_ref()
.expect("Compositor doesn't implement river_status_unstable_v1")
.get_river_seat_status(&seat);
status.add_keypair("title".to_owned());
seat.quick_assign(move |_, event, mut status| {
let seat_name = &status.get::<Status>().unwrap().seat_name;
match event {
wl_seat::Event::Name{ name } => if seat_name.len() == 0 || name.eq(seat_name) {
seat_status.quick_assign(move |_, event, mut status| match event {
zriver_seat_status_v1::Event::FocusedView { title } => {
status.get::<Status>().unwrap().mod_value("title".to_owned(), title);
},
_ => {}
})
} else { seat_status.quick_assign(move |_, _, _| {}) },
_ => {}
}
})
} else { seat.quick_assign(move |_, _, _| {}) }
}
if enable_tag || enable_views_tag {
for (i, output) in globals
.outputs
.iter()
.enumerate()
.filter(|(i, _)| if let Some(index) = monitor {
if *i == index { true } else { false }
} else { true })
{
if enable_tag { status.add_keypair(format!("tag{}",i)); }
if enable_views_tag { status.add_keypair(format!("views_tag{}",i)); }
let output_status = globals
.status_manager
.as_ref()
.expect("Compositor doesn't implement river_status_unstable_v1")
.get_river_output_status(&output);
output_status.quick_assign(move |_, event, mut status| match event {
zriver_output_status_v1::Event::FocusedTags { tags } => {
if enable_tag {
status.get::<Status>().unwrap().mod_value(format!("tag{}",i), base10(tags).trim_end().to_owned());
}
}
zriver_output_status_v1::Event::ViewTags { tags } => {
if enable_views_tag {
let len = tags.len();
let mut views_tag = String::new();
for i in (0..len).into_iter().step_by(4) {
let buf: [u8; 4] = [tags[i], tags[i + 1], tags[i + 2], tags[i + 3]];
views_tag.push_str(&base10(u32::from_le_bytes(buf)));
}
status.get::<Status>().unwrap().mod_value(format!("views_tag{}",i), views_tag.trim_end().to_owned());
}
}
});
}
}
loop {
event_queue
.dispatch(&mut status, |event, object, _| {
.dispatch(&mut env, |event, object, _| {
panic!(
"[callop] Encountered an orphan event: {}@{}: {}",
event.interface,
@ -216,12 +223,42 @@ fn main() {
);
})
.unwrap();
status.to_string()
env.fmt();
}
}
fn base10(tagmask: u32) -> String {
let mut format = String::new();
fn configuration() -> Flags {
let mut default = Flags::default();
let mut args = std::env::args();
loop {
match args.next() {
Some(flag) => match flag.as_str() {
"--seat" | "-s" => default.seat = args.next(),
"--output" | "-o" => default.output = args.next(),
"--urgency" | "-u" => default.urgency = true,
"--title" | "-w" => default.title = true,
"--tags" | "-t" => default.tags = true,
"--views-tag" | "-vt" => default.viewstag = true,
"--help" | "-h" => {
print!("Usage: ristate [option]\n\n");
print!(" --tag | -t the focused tag\n");
print!(" --title | -w the title of the focused view\n");
print!(" --urgency | -u urgent tag\n");
print!(" --views-tag | -vt the tag of all views\n");
print!(" --seat | -s <string> select the seat\n");
print!(" --output | -o <string> select the output\n");
std::process::exit(0);
}
_ => {}
}
None => break
}
}
default
}
fn fmt_tags(tagmask: u32) {
let mut tag = 0;
let mut current: u32;
while {
@ -230,11 +267,12 @@ fn base10(tagmask: u32) -> String {
} {
tag += 1;
if current != tagmask && (tagmask / current) % 2 != 0 {
format.push_str(&(base10(tagmask - current)));
fmt_tags(tagmask - current);
print!(" ");
break;
} else if tag == 32 { break; }
} else if tag == 32 {
break;
}
}
format.push_str(&tag.to_string());
format.push(' ');
format
print!("{}", tag);
}

View File

@ -208,7 +208,7 @@ pub mod zriver_status_manager_v1 {
type Request = Request;
type Event = Event;
const NAME: &'static str = "zriver_status_manager_v1";
const VERSION: u32 = 1;
const VERSION: u32 = 2;
fn c_interface() -> *const wl_interface {
unsafe { &zriver_status_manager_v1_interface }
}
@ -284,7 +284,7 @@ pub mod zriver_status_manager_v1 {
#[doc = r" C representation of this interface, for interop"]
pub static mut zriver_status_manager_v1_interface: wl_interface = wl_interface {
name: b"zriver_status_manager_v1\0" as *const u8 as *const c_char,
version: 1,
version: 2,
request_count: 3,
requests: unsafe { &zriver_status_manager_v1_requests as *const _ },
event_count: 0,
@ -376,6 +376,8 @@ pub mod zriver_output_status_v1 {
FocusedTags { tags: u32 },
#[doc = "tag state of an output's views\n\nSent once on binding the interface and again whenever the tag state\nof the output changes."]
ViewTags { tags: Vec<u8> },
#[doc = "tags of the output with an urgent view\n\nSent once on binding the interface and again whenever the set of\ntags with at least one urgent view changes.\n\nOnly available since version 2 of the interface"]
UrgentTags { tags: u32 },
}
impl super::MessageGroup for Event {
const MESSAGES: &'static [super::MessageDesc] = &[
@ -391,6 +393,12 @@ pub mod zriver_output_status_v1 {
signature: &[super::ArgumentType::Array],
destructor: false,
},
super::MessageDesc {
name: "urgent_tags",
since: 2,
signature: &[super::ArgumentType::Uint],
destructor: false,
},
];
type Map = super::ProxyMap;
fn is_destructor(&self) -> bool {
@ -402,12 +410,14 @@ pub mod zriver_output_status_v1 {
match *self {
Event::FocusedTags { .. } => 0,
Event::ViewTags { .. } => 1,
Event::UrgentTags { .. } => 2,
}
}
fn since(&self) -> u32 {
match *self {
Event::FocusedTags { .. } => 1,
Event::ViewTags { .. } => 1,
Event::UrgentTags { .. } => 2,
}
}
fn child<Meta: ObjectMetadata>(
@ -445,6 +455,18 @@ pub mod zriver_output_status_v1 {
},
})
}
2 => {
let mut args = msg.args.into_iter();
Ok(Event::UrgentTags {
tags: {
if let Some(Argument::Uint(val)) = args.next() {
val
} else {
return Err(());
}
},
})
}
_ => Err(()),
}
}
@ -471,6 +493,10 @@ pub mod zriver_output_status_v1 {
},
})
}
2 => {
let _args = ::std::slice::from_raw_parts(args, 1);
Ok(Event::UrgentTags { tags: _args[0].u })
}
_ => return Err(()),
}
}
@ -510,7 +536,7 @@ pub mod zriver_output_status_v1 {
type Request = Request;
type Event = Event;
const NAME: &'static str = "zriver_output_status_v1";
const VERSION: u32 = 1;
const VERSION: u32 = 2;
fn c_interface() -> *const wl_interface {
unsafe { &zriver_output_status_v1_interface }
}
@ -528,6 +554,8 @@ pub mod zriver_output_status_v1 {
pub const EVT_FOCUSED_TAGS_SINCE: u32 = 1u32;
#[doc = r" The minimal object version supporting this event"]
pub const EVT_VIEW_TAGS_SINCE: u32 = 1u32;
#[doc = r" The minimal object version supporting this event"]
pub const EVT_URGENT_TAGS_SINCE: u32 = 2u32;
#[doc = r" C-representation of the messages of this interface, for interop"]
pub static mut zriver_output_status_v1_requests: [wl_message; 1] = [wl_message {
name: b"destroy\0" as *const u8 as *const c_char,
@ -535,7 +563,7 @@ pub mod zriver_output_status_v1 {
types: unsafe { &types_null as *const _ },
}];
#[doc = r" C-representation of the messages of this interface, for interop"]
pub static mut zriver_output_status_v1_events: [wl_message; 2] = [
pub static mut zriver_output_status_v1_events: [wl_message; 3] = [
wl_message {
name: b"focused_tags\0" as *const u8 as *const c_char,
signature: b"u\0" as *const u8 as *const c_char,
@ -546,14 +574,19 @@ pub mod zriver_output_status_v1 {
signature: b"a\0" as *const u8 as *const c_char,
types: unsafe { &types_null as *const _ },
},
wl_message {
name: b"urgent_tags\0" as *const u8 as *const c_char,
signature: b"2u\0" as *const u8 as *const c_char,
types: unsafe { &types_null as *const _ },
},
];
#[doc = r" C representation of this interface, for interop"]
pub static mut zriver_output_status_v1_interface: wl_interface = wl_interface {
name: b"zriver_output_status_v1\0" as *const u8 as *const c_char,
version: 1,
version: 2,
request_count: 1,
requests: unsafe { &zriver_output_status_v1_requests as *const _ },
event_count: 2,
event_count: 3,
events: unsafe { &zriver_output_status_v1_events as *const _ },
};
}