Add gtk_ids field

This commit is contained in:
Alexander Rosenberg 2024-04-27 22:17:49 -07:00
parent 380c72f0c3
commit 3de24bc47f
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

@ -8,7 +8,7 @@ use river_protocols::{
use serde::ser::{SerializeSeq, SerializeStruct, Serializer}; use serde::ser::{SerializeSeq, SerializeStruct, Serializer};
use serde::Serialize; use serde::Serialize;
use wayland_client::protocol::wl_registry::WlRegistry; use wayland_client::protocol::wl_registry::WlRegistry;
use std::collections::HashMap; use std::collections::{BTreeMap, HashMap};
use wayland_client::protocol::{wl_output, wl_output::WlOutput, wl_seat, use wayland_client::protocol::{wl_output, wl_output::WlOutput, wl_seat,
wl_seat::WlSeat}; wl_seat::WlSeat};
use wayland_client::{Attached, DispatchData, Display, GlobalEvent, GlobalManager, Main}; use wayland_client::{Attached, DispatchData, Display, GlobalEvent, GlobalManager, Main};
@ -22,6 +22,7 @@ struct Flags {
layout: bool, layout: bool,
mode: bool, mode: bool,
focused: bool, focused: bool,
gtk_nums: bool,
seat: Option<String>, seat: Option<String>,
} }
@ -35,6 +36,7 @@ impl Flags {
layout: false, layout: false,
mode: false, mode: false,
focused: false, focused: false,
gtk_nums: false,
seat: None, seat: None,
} }
} }
@ -45,7 +47,7 @@ struct Tags(u32);
struct Env { struct Env {
flags: Flags, flags: Flags,
output_names: HashMap<u32, String>, output_names: BTreeMap<u32, String>,
focused: Option<u32>, focused: Option<u32>,
layout: Option<String>, layout: Option<String>,
titles: Option<HashMap<u32, String>>, titles: Option<HashMap<u32, String>>,
@ -89,7 +91,7 @@ impl Serialize for Tags {
impl Serialize for Env { impl Serialize for Env {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer { where S: Serializer {
let mut state = serializer.serialize_struct("Env", 7)?; let mut state = serializer.serialize_struct("Env", 8)?;
if let Some(layout) = self.layout.as_ref() { if let Some(layout) = self.layout.as_ref() {
state.serialize_field("layout", layout)?; state.serialize_field("layout", layout)?;
} else { } else {
@ -128,6 +130,18 @@ impl Serialize for Env {
} else { } else {
state.skip_field("viewtags")?; state.skip_field("viewtags")?;
} }
if self.flags.gtk_nums {
let mut name_id_map = HashMap::<&str, u32>::new();
name_id_map.reserve(self.output_names.len());
let mut id = 0;
for (_, value) in &self.output_names {
name_id_map.insert(value, id);
id += 1;
}
state.serialize_field("gtk_ids", &name_id_map)?;
} else {
state.skip_field("gtk_ids")?;
}
state.end() state.end()
} }
} }
@ -145,7 +159,7 @@ impl Env {
viewtags: flags.viewtags.then(Default::default), viewtags: flags.viewtags.then(Default::default),
status_manager: None, status_manager: None,
flags, flags,
output_names: HashMap::default(), output_names: Default::default(),
} }
} }
@ -157,6 +171,7 @@ impl Env {
|| self.viewtags.is_some() || self.viewtags.is_some()
|| self.layout.is_some() || self.layout.is_some()
|| self.focused.is_some() || self.focused.is_some()
|| self.flags.gtk_nums
{ {
println!("{}", serde_json::to_string(self).unwrap()); println!("{}", serde_json::to_string(self).unwrap());
} }
@ -360,6 +375,7 @@ fn configuration() -> Flags {
"--tags" | "-t" => default.tags = true, "--tags" | "-t" => default.tags = true,
"--layout" | "-l" => default.layout = true, "--layout" | "-l" => default.layout = true,
"--view-tags" | "-vt" => default.viewtags = true, "--view-tags" | "-vt" => default.viewtags = true,
"--gtk-ids" | "-g" => default.gtk_nums = true,
"--help" | "-h" => { "--help" | "-h" => {
print!("Usage: ristate [option]\n\n"); print!("Usage: ristate [option]\n\n");
print!(" --tag | -t the focused tag of each output\n"); print!(" --tag | -t the focused tag of each output\n");
@ -369,6 +385,7 @@ fn configuration() -> Flags {
print!(" --focused | -f the name of the focused output\n"); print!(" --focused | -f the name of the focused output\n");
print!(" --urgency | -u tags with urgent views on them\n"); print!(" --urgency | -u tags with urgent views on them\n");
print!(" --view-tags | -vt the tags with views on them\n"); print!(" --view-tags | -vt the tags with views on them\n");
print!(" --gtk-ids | -g attempt to map output names to their gtk id\n");
print!(" --seat | -s <string> select the seat\n"); print!(" --seat | -s <string> select the seat\n");
std::process::exit(0); std::process::exit(0);
} }
@ -378,7 +395,7 @@ fn configuration() -> Flags {
} }
} }
if !default.urgency && !default.titles && !default.mode && !default.focused if !default.urgency && !default.titles && !default.mode && !default.focused
&& !default.tags && !default.layout && !default.viewtags { && !default.tags && !default.layout && !default.viewtags && !default.gtk_nums {
println!("error: You must specify at least one thing to listen for!"); println!("error: You must specify at least one thing to listen for!");
std::process::exit(1); std::process::exit(1);
} }