2023-08-01 11:06:35 -05:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2023-06-30 21:34:07 -05:00
|
|
|
|
2023-07-09 17:48:46 -05:00
|
|
|
use std::{
|
2024-04-20 21:25:53 -05:00
|
|
|
cell::RefCell,
|
2023-07-09 17:48:46 -05:00
|
|
|
hash::Hash,
|
2024-04-20 21:25:53 -05:00
|
|
|
rc::Rc,
|
|
|
|
sync::atomic::{AtomicU32, Ordering},
|
2023-07-09 17:48:46 -05:00
|
|
|
};
|
|
|
|
|
2023-09-20 15:18:26 -05:00
|
|
|
use smithay::output::Output;
|
2023-07-11 16:10:31 -05:00
|
|
|
|
2024-03-14 19:21:51 -05:00
|
|
|
use crate::state::{State, WithState};
|
2023-07-11 16:10:31 -05:00
|
|
|
|
2023-07-09 17:48:46 -05:00
|
|
|
static TAG_ID_COUNTER: AtomicU32 = AtomicU32::new(0);
|
2023-06-30 21:34:07 -05:00
|
|
|
|
2023-12-16 21:20:29 -06:00
|
|
|
/// A unique id for a [`Tag`].
|
2023-07-11 16:10:31 -05:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, serde::Serialize, serde::Deserialize)]
|
2024-03-12 19:18:27 -05:00
|
|
|
pub struct TagId(pub u32);
|
2023-07-09 17:48:46 -05:00
|
|
|
|
|
|
|
impl TagId {
|
2023-12-16 21:20:29 -06:00
|
|
|
/// Get the next available `TagId`.
|
2023-07-09 17:48:46 -05:00
|
|
|
fn next() -> Self {
|
2024-03-12 19:18:27 -05:00
|
|
|
Self(TAG_ID_COUNTER.fetch_add(1, Ordering::Relaxed))
|
2023-07-09 17:48:46 -05:00
|
|
|
}
|
2023-07-19 18:55:22 -05:00
|
|
|
|
2023-12-16 21:20:29 -06:00
|
|
|
/// Get the tag associated with this id.
|
2023-08-28 22:53:24 -05:00
|
|
|
pub fn tag(&self, state: &State) -> Option<Tag> {
|
2023-07-19 18:55:22 -05:00
|
|
|
state
|
|
|
|
.space
|
|
|
|
.outputs()
|
|
|
|
.flat_map(|op| op.with_state(|state| state.tags.clone()))
|
|
|
|
.find(|tag| &tag.id() == self)
|
|
|
|
}
|
2023-08-15 17:26:17 -05:00
|
|
|
|
|
|
|
/// Reset the global TagId counter.
|
|
|
|
///
|
|
|
|
/// This is used, for example, when a config is reloaded and you want to keep
|
|
|
|
/// windows on the same tags.
|
|
|
|
pub fn reset() {
|
|
|
|
TAG_ID_COUNTER.store(0, Ordering::SeqCst);
|
|
|
|
}
|
2023-07-09 17:48:46 -05:00
|
|
|
}
|
2023-06-30 21:34:07 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-07-11 16:10:31 -05:00
|
|
|
struct TagInner {
|
2023-07-09 17:48:46 -05:00
|
|
|
/// The internal id of this tag.
|
2023-07-11 16:10:31 -05:00
|
|
|
id: TagId,
|
2023-07-09 17:48:46 -05:00
|
|
|
/// The name of this tag.
|
2023-07-11 16:10:31 -05:00
|
|
|
name: String,
|
2023-07-09 17:48:46 -05:00
|
|
|
/// Whether this tag is active or not.
|
2023-07-11 16:10:31 -05:00
|
|
|
active: bool,
|
2023-06-30 21:34:07 -05:00
|
|
|
}
|
|
|
|
|
2023-07-11 16:10:31 -05:00
|
|
|
impl PartialEq for TagInner {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for TagInner {}
|
|
|
|
|
2023-12-16 21:20:29 -06:00
|
|
|
/// A marker for windows.
|
|
|
|
///
|
|
|
|
/// A window may have 0 or more tags, and you can display 0 or more tags
|
|
|
|
/// on each output at a time.
|
2024-04-20 21:25:53 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct Tag(Rc<RefCell<TagInner>>);
|
2023-07-11 16:10:31 -05:00
|
|
|
|
2023-09-02 02:46:26 -05:00
|
|
|
// RefCell Safety: These methods should never panic because they are all self-contained or Copy.
|
2023-07-11 16:10:31 -05:00
|
|
|
impl Tag {
|
|
|
|
pub fn id(&self) -> TagId {
|
2024-04-20 21:25:53 -05:00
|
|
|
self.0.borrow().id
|
2023-07-11 16:10:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self) -> String {
|
2024-04-20 21:25:53 -05:00
|
|
|
self.0.borrow().name.clone()
|
2023-07-11 16:10:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn active(&self) -> bool {
|
2024-04-20 21:25:53 -05:00
|
|
|
self.0.borrow().active
|
2023-07-11 16:10:31 -05:00
|
|
|
}
|
|
|
|
|
2024-04-18 14:42:49 -05:00
|
|
|
pub fn set_active(&self, active: bool, state: &mut State) {
|
2024-04-20 21:25:53 -05:00
|
|
|
self.0.borrow_mut().active = active;
|
2024-04-18 14:42:49 -05:00
|
|
|
|
|
|
|
state.signal_state.tag_active.signal(|buf| {
|
|
|
|
buf.push_back(
|
|
|
|
pinnacle_api_defs::pinnacle::signal::v0alpha1::TagActiveResponse {
|
|
|
|
tag_id: Some(self.id().0),
|
|
|
|
active: Some(self.active()),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
})
|
2023-07-11 16:10:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:48:46 -05:00
|
|
|
impl Tag {
|
2023-07-11 11:59:38 -05:00
|
|
|
pub fn new(name: String) -> Self {
|
2024-04-20 21:25:53 -05:00
|
|
|
Self(Rc::new(RefCell::new(TagInner {
|
2023-07-09 17:48:46 -05:00
|
|
|
id: TagId::next(),
|
|
|
|
name,
|
|
|
|
active: false,
|
2023-07-11 16:10:31 -05:00
|
|
|
})))
|
|
|
|
}
|
2023-08-08 15:22:50 -05:00
|
|
|
|
2023-12-16 21:20:29 -06:00
|
|
|
/// Get the output this tag is on.
|
|
|
|
///
|
|
|
|
/// RefCell Safety: This uses RefCells on every mapped output.
|
2023-08-28 22:53:24 -05:00
|
|
|
pub fn output(&self, state: &State) -> Option<Output> {
|
2023-07-19 18:55:22 -05:00
|
|
|
state
|
|
|
|
.space
|
2023-07-11 16:10:31 -05:00
|
|
|
.outputs()
|
2023-07-19 18:55:22 -05:00
|
|
|
.find(|output| output.with_state(|state| state.tags.iter().any(|tg| tg == self)))
|
2023-07-11 16:10:31 -05:00
|
|
|
.cloned()
|
2023-06-30 21:34:07 -05:00
|
|
|
}
|
|
|
|
}
|