Skip to content

Commit

Permalink
WIP Add cosmic-keymap-unstable-v1 protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Nov 14, 2024
1 parent 4db2e3e commit b76a1c9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ bytemuck = "1.12"
calloop = {version = "0.14.1", features = ["executor"]}
cosmic-comp-config = {path = "cosmic-comp-config"}
cosmic-config = {git = "https://github.com/pop-os/libcosmic/", features = ["calloop", "macro"]}
cosmic-protocols = {git = "https://github.com/pop-os/cosmic-protocols", branch = "main", default-features = false, features = ["server"]}
# cosmic-protocols = {git = "https://github.com/pop-os/cosmic-protocols", branch = "main", default-features = false, features = ["server"]}
cosmic-protocols = {git = "https://github.com/pop-os/cosmic-protocols", branch = "keymap", default-features = false, features = ["server"]}
cosmic-settings-config = { git = "https://github.com/pop-os/cosmic-settings-daemon" }
edid-rs = {version = "0.1"}
egui = {version = "0.29.0", optional = true}
Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
atspi::AtspiState,
drm::WlDrmState,
image_source::ImageSourceState,
keymap::KeymapState,
output_configuration::OutputConfigurationState,
output_power::OutputPowerState,
screencopy::ScreencopyState,
Expand Down Expand Up @@ -522,6 +523,7 @@ impl State {
VirtualKeyboardManagerState::new::<State, _>(&dh, client_is_privileged);
AlphaModifierState::new::<Self>(&dh);
SinglePixelBufferState::new::<Self>(&dh);
KeymapState::new::<State, _>(&dh, client_is_privileged);

let idle_notifier_state = IdleNotifierState::<Self>::new(&dh, handle.clone());
let idle_inhibit_manager_state = IdleInhibitManagerState::new::<State>(&dh);
Expand Down
5 changes: 5 additions & 0 deletions src/wayland/handlers/keymap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: GPL-3.0-only
//
use crate::{state::State, wayland::protocols::keymap::delegate_keymap};

delegate_keymap!(State);
1 change: 1 addition & 0 deletions src/wayland/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod idle_notify;
pub mod image_source;
pub mod input_method;
pub mod keyboard_shortcuts_inhibit;
pub mod keymap;
pub mod layer_shell;
pub mod output;
pub mod output_configuration;
Expand Down
100 changes: 100 additions & 0 deletions src/wayland/protocols/keymap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: GPL-3.0-only

use cosmic_protocols::keymap::v1::server::zcosmic_keymap_manager_v1::{
self, ZcosmicKeymapManagerV1,
};
use smithay::{
input::{
keyboard::{KeyboardHandle, Layout},
SeatHandler,
},
reexports::wayland_server::{Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New},
};
use wayland_backend::server::GlobalId;

pub struct KeymapState {
pub global: GlobalId,
}

impl KeymapState {
pub fn new<D, F>(dh: &DisplayHandle, client_filter: F) -> Self
where
D: GlobalDispatch<ZcosmicKeymapManagerV1, KeymapGlobalData> + 'static,
F: for<'a> Fn(&'a Client) -> bool + Send + Sync + 'static,
{
let global = dh.create_global::<D, ZcosmicKeymapManagerV1, _>(
1,
KeymapGlobalData {
filter: Box::new(client_filter),
},
);
KeymapState { global }
}
}

pub struct KeymapGlobalData {
filter: Box<dyn for<'a> Fn(&'a Client) -> bool + Send + Sync>,
}

impl<D> GlobalDispatch<ZcosmicKeymapManagerV1, KeymapGlobalData, D> for KeymapState
where
D: GlobalDispatch<ZcosmicKeymapManagerV1, KeymapGlobalData>
+ Dispatch<ZcosmicKeymapManagerV1, ()>
+ 'static,
{
fn bind(
_state: &mut D,
_handle: &DisplayHandle,
_client: &Client,
resource: New<ZcosmicKeymapManagerV1>,
_global_data: &KeymapGlobalData,
data_init: &mut DataInit<'_, D>,
) {
data_init.init(resource, ());
}

fn can_view(client: Client, global_data: &KeymapGlobalData) -> bool {
(global_data.filter)(&client)
}
}

impl<D> Dispatch<ZcosmicKeymapManagerV1, (), D> for KeymapState
where
D: Dispatch<ZcosmicKeymapManagerV1, ()> + 'static,
D: SeatHandler,
{
fn request(
state: &mut D,
_client: &Client,
_resource: &ZcosmicKeymapManagerV1,
request: zcosmic_keymap_manager_v1::Request,
_data: &(),
_dhandle: &DisplayHandle,
_data_init: &mut DataInit<'_, D>,
) {
match request {
zcosmic_keymap_manager_v1::Request::SetGroup { keyboard, group } => {
if let Some(handle) = KeyboardHandle::<D>::from_resource(&keyboard) {
handle.with_xkb_state(state, |mut context| {
context.set_layout(Layout(group));
// TODO is `modifiers` sent?
});
}
}
zcosmic_keymap_manager_v1::Request::Destroy => {}
_ => unreachable!(),
}
}
}

macro_rules! delegate_keymap {
($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
cosmic_protocols::keymap::v1::server::zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1: $crate::wayland::protocols::keymap::KeymapGlobalData
] => $crate::wayland::protocols::keymap::KeymapState);
smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
cosmic_protocols::keymap::v1::server::zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1: ()
] => $crate::wayland::protocols::keymap::KeymapState);
};
}
pub(crate) use delegate_keymap;
1 change: 1 addition & 0 deletions src/wayland/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod atspi;
pub mod drm;
pub mod image_source;
pub mod keymap;
pub mod output_configuration;
pub mod output_power;
pub mod overlap_notify;
Expand Down

0 comments on commit b76a1c9

Please sign in to comment.