-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Add
cosmic-keymap-unstable-v1
protocol
Includes example client using `cosmic-client-toolkit`.
- Loading branch information
Showing
6 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
use cosmic_client_toolkit::keymap::KeymapState; | ||
use sctk::{ | ||
registry::{ProvidesRegistryState, RegistryState}, | ||
registry_handlers, | ||
seat::{ | ||
keyboard::{KeyEvent, KeyboardHandler, Keymap, Keysym, Modifiers}, | ||
Capability, SeatHandler, SeatState, | ||
}, | ||
}; | ||
use std::{ | ||
io::{self, Write}, | ||
str::FromStr, | ||
}; | ||
use wayland_client::{ | ||
globals::registry_queue_init, | ||
protocol::{wl_keyboard, wl_seat, wl_surface}, | ||
Connection, QueueHandle, | ||
}; | ||
use xkbcommon::xkb; | ||
|
||
struct AppData { | ||
registry_state: RegistryState, | ||
seat_state: SeatState, | ||
keyboard: Option<wl_keyboard::WlKeyboard>, | ||
keymap_state: KeymapState, | ||
keymap: Option<xkb::Keymap>, | ||
} | ||
|
||
impl ProvidesRegistryState for AppData { | ||
fn registry(&mut self) -> &mut RegistryState { | ||
&mut self.registry_state | ||
} | ||
|
||
registry_handlers![SeatState,]; | ||
} | ||
|
||
impl SeatHandler for AppData { | ||
fn seat_state(&mut self) -> &mut SeatState { | ||
&mut self.seat_state | ||
} | ||
|
||
fn new_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_seat::WlSeat) {} | ||
|
||
fn new_capability( | ||
&mut self, | ||
_conn: &Connection, | ||
qh: &QueueHandle<Self>, | ||
seat: wl_seat::WlSeat, | ||
capability: Capability, | ||
) { | ||
if capability == Capability::Keyboard { | ||
let keyboard = self.seat_state.get_keyboard(qh, &seat, None).unwrap(); | ||
self.keyboard = Some(keyboard); | ||
} | ||
} | ||
|
||
fn remove_capability( | ||
&mut self, | ||
_conn: &Connection, | ||
_: &QueueHandle<Self>, | ||
_: wl_seat::WlSeat, | ||
_capability: Capability, | ||
) { | ||
} | ||
|
||
fn remove_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: wl_seat::WlSeat) {} | ||
} | ||
|
||
impl KeyboardHandler for AppData { | ||
fn enter( | ||
&mut self, | ||
_: &Connection, | ||
_: &QueueHandle<Self>, | ||
_: &wl_keyboard::WlKeyboard, | ||
_surface: &wl_surface::WlSurface, | ||
_: u32, | ||
_: &[u32], | ||
_keysyms: &[Keysym], | ||
) { | ||
} | ||
|
||
fn leave( | ||
&mut self, | ||
_: &Connection, | ||
_: &QueueHandle<Self>, | ||
_: &wl_keyboard::WlKeyboard, | ||
_surface: &wl_surface::WlSurface, | ||
_: u32, | ||
) { | ||
} | ||
|
||
fn press_key( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
_: &wl_keyboard::WlKeyboard, | ||
_: u32, | ||
_event: KeyEvent, | ||
) { | ||
} | ||
|
||
fn release_key( | ||
&mut self, | ||
_: &Connection, | ||
_: &QueueHandle<Self>, | ||
_: &wl_keyboard::WlKeyboard, | ||
_: u32, | ||
_event: KeyEvent, | ||
) { | ||
} | ||
|
||
fn update_modifiers( | ||
&mut self, | ||
_: &Connection, | ||
_: &QueueHandle<Self>, | ||
_: &wl_keyboard::WlKeyboard, | ||
_serial: u32, | ||
_modifiers: Modifiers, | ||
_layout: u32, | ||
) { | ||
} | ||
|
||
fn update_keymap( | ||
&mut self, | ||
_conn: &Connection, | ||
_qh: &QueueHandle<Self>, | ||
_keyboard: &wl_keyboard::WlKeyboard, | ||
keymap: Keymap<'_>, | ||
) { | ||
let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS); | ||
let keymap = xkb::Keymap::new_from_string( | ||
&context, | ||
keymap.as_string(), | ||
xkb::KEYMAP_FORMAT_TEXT_V1, | ||
xkb::KEYMAP_COMPILE_NO_FLAGS, | ||
) | ||
.unwrap(); | ||
self.keymap = Some(keymap); | ||
} | ||
} | ||
|
||
fn main() { | ||
let conn = Connection::connect_to_env().unwrap(); | ||
let (globals, mut event_queue) = registry_queue_init(&conn).unwrap(); | ||
let qh = event_queue.handle(); | ||
|
||
let registry_state = RegistryState::new(&globals); | ||
let seat_state = SeatState::new(&globals, &qh); | ||
let keymap_state = KeymapState::new(®istry_state, &qh); | ||
let mut app_data = AppData { | ||
registry_state, | ||
seat_state, | ||
keymap_state, | ||
keyboard: None, | ||
keymap: None, | ||
}; | ||
|
||
while app_data.keymap.is_none() { | ||
event_queue.blocking_dispatch(&mut app_data).unwrap(); | ||
} | ||
|
||
let keymap = app_data.keymap.as_ref().unwrap(); | ||
for (n, name) in keymap.layouts().enumerate() { | ||
println!("{}: {}", n, name); | ||
} | ||
print!("Choose layout: "); | ||
|
||
io::stdout().flush().unwrap(); | ||
let mut line = String::new(); | ||
io::stdin().read_line(&mut line).unwrap(); | ||
let index = u32::from_str(line.trim()).unwrap(); | ||
|
||
let keymap_manager = app_data.keymap_state.keymap_manager.as_ref().unwrap(); | ||
let keyboard = app_data.keyboard.as_ref().unwrap(); | ||
keymap_manager.set_group(keyboard, index); | ||
|
||
event_queue.roundtrip(&mut app_data).unwrap(); | ||
} | ||
|
||
sctk::delegate_registry!(AppData); | ||
sctk::delegate_seat!(AppData); | ||
sctk::delegate_keyboard!(AppData); | ||
cosmic_client_toolkit::delegate_keymap!(AppData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use cosmic_protocols::keymap::v1::client::zcosmic_keymap_manager_v1; | ||
use sctk::registry::RegistryState; | ||
use wayland_client::{protocol::wl_output, Connection, Dispatch, QueueHandle}; | ||
|
||
pub struct KeymapState { | ||
pub keymap_manager: Option<zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1>, | ||
} | ||
|
||
impl KeymapState { | ||
pub fn new<D>(registry: &RegistryState, qh: &QueueHandle<D>) -> Self | ||
where | ||
D: Dispatch<zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1, ()> + 'static, | ||
{ | ||
let keymap_manager = registry | ||
.bind_one::<zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1, _, _>(qh, 1..=1, ()) | ||
.ok(); | ||
|
||
Self { keymap_manager } | ||
} | ||
} | ||
|
||
impl<D> Dispatch<zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1, (), D> for KeymapState | ||
where | ||
D: Dispatch<zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1, ()>, | ||
{ | ||
fn event( | ||
state: &mut D, | ||
_: &zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1, | ||
event: zcosmic_keymap_manager_v1::Event, | ||
_: &(), | ||
_: &Connection, | ||
_: &QueueHandle<D>, | ||
) { | ||
match event { | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! delegate_keymap { | ||
($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { | ||
$crate::wayland_client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ | ||
$crate::cosmic_protocols::keymap::v1::client::zcosmic_keymap_manager_v1::ZcosmicKeymapManagerV1: () | ||
] => $crate::keymap::KeymapState); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<protocol name="cosmic_keymap_unstable_v1"> | ||
<copyright> | ||
Copyright © 2024 System76, Inc | ||
|
||
Permission to use, copy, modify, distribute, and sell this | ||
software and its documentation for any purpose is hereby granted | ||
without fee, provided that the above copyright notice appear in | ||
all copies and that both that copyright notice and this permission | ||
notice appear in supporting documentation, and that the name of | ||
the copyright holders not be used in advertising or publicity | ||
pertaining to distribution of the software without specific, | ||
written prior permission. The copyright holders make no | ||
representations about the suitability of this software for any | ||
purpose. It is provided "as is" without express or implied | ||
warranty. | ||
|
||
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS | ||
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN | ||
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | ||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF | ||
THIS SOFTWARE. | ||
</copyright> | ||
|
||
<description summary=""> | ||
TODO | ||
|
||
Warning! The protocol described in this file is currently in the testing | ||
phase. Backward compatible changes may be added together with the | ||
corresponding interface version bump. Backward incompatible changes can | ||
only be done by creating a new major version of the extension. | ||
</description> | ||
|
||
<interface name="zcosmic_keymap_manager_v1" version="1"> | ||
<description summary=""> | ||
</description> | ||
|
||
<request name="set_group"> | ||
<description summary=""> | ||
</description> | ||
<arg name="keyboard" type="object" interface="wl_keyboard"/> | ||
<arg name="group" type="uint"/> | ||
</request> | ||
|
||
<request name="destroy" type="destructor"> | ||
<description summary="delete this object"> | ||
</description> | ||
</request> | ||
</interface> | ||
</protocol> |