From 3de24bc47f37743c6b733be48e08139b4bb2a6e2 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Sat, 27 Apr 2024 22:17:49 -0700 Subject: [PATCH] Add gtk_ids field --- src/main.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index d122671..1582e8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use river_protocols::{ use serde::ser::{SerializeSeq, SerializeStruct, Serializer}; use serde::Serialize; 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, wl_seat::WlSeat}; use wayland_client::{Attached, DispatchData, Display, GlobalEvent, GlobalManager, Main}; @@ -22,6 +22,7 @@ struct Flags { layout: bool, mode: bool, focused: bool, + gtk_nums: bool, seat: Option, } @@ -35,6 +36,7 @@ impl Flags { layout: false, mode: false, focused: false, + gtk_nums: false, seat: None, } } @@ -45,7 +47,7 @@ struct Tags(u32); struct Env { flags: Flags, - output_names: HashMap, + output_names: BTreeMap, focused: Option, layout: Option, titles: Option>, @@ -89,7 +91,7 @@ impl Serialize for Tags { impl Serialize for Env { fn serialize(&self, serializer: S) -> Result 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() { state.serialize_field("layout", layout)?; } else { @@ -128,6 +130,18 @@ impl Serialize for Env { } else { 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() } } @@ -145,7 +159,7 @@ impl Env { viewtags: flags.viewtags.then(Default::default), status_manager: None, flags, - output_names: HashMap::default(), + output_names: Default::default(), } } @@ -157,6 +171,7 @@ impl Env { || self.viewtags.is_some() || self.layout.is_some() || self.focused.is_some() + || self.flags.gtk_nums { println!("{}", serde_json::to_string(self).unwrap()); } @@ -360,6 +375,7 @@ fn configuration() -> Flags { "--tags" | "-t" => default.tags = true, "--layout" | "-l" => default.layout = true, "--view-tags" | "-vt" => default.viewtags = true, + "--gtk-ids" | "-g" => default.gtk_nums = true, "--help" | "-h" => { print!("Usage: ristate [option]\n\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!(" --urgency | -u tags with urgent 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 select the seat\n"); std::process::exit(0); } @@ -378,7 +395,7 @@ fn configuration() -> Flags { } } 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!"); std::process::exit(1); }