tony / oxwm

Added keycode -> string map in enum for reverse lookup, to avoid unsafe call to C FFI bind. Sorry bugsy Browse files
tonybtw committed 2025-10-31 parent aa4066d commit 7a4cc10097f7e496001b99152f167bff0eea92f0
Showing 2 changed files with 88 additions and 23 deletions. UnifiedSplit
+85 −0 src/config/mod.rs
@@ -157,6 +157,91 @@ impl KeyData {
157 157 KeyData::Slash => keycodes::SLASH,
158 158 }
159 159 }
160 +
161 + // Adding this reverse lookup for now so converting to KeySyms is no longer
162 + // an 'unsafe' call to a C FFI (Sorry Bugsy...)
163 + // This will set us up in the future to move away from hardcoded keycodes,
164 + // and directly convert keysyms to keydata at runtime for international
165 + // keyboard support.
166 + pub fn from_keycode(keycode: Keycode) -> Option<&'static str> {
167 + match keycode {
168 + keycodes::RETURN => Some("Return"),
169 + keycodes::Q => Some("q"),
170 + keycodes::ESCAPE => Some("Esc"),
171 + keycodes::SPACE => Some("Space"),
172 + keycodes::TAB => Some("Tab"),
173 + keycodes::BACKSPACE => Some("Backspace"),
174 + keycodes::DELETE => Some("Del"),
175 + keycodes::F1 => Some("F1"),
176 + keycodes::F2 => Some("F2"),
177 + keycodes::F3 => Some("F3"),
178 + keycodes::F4 => Some("F4"),
179 + keycodes::F5 => Some("F5"),
180 + keycodes::F6 => Some("F6"),
181 + keycodes::F7 => Some("F7"),
182 + keycodes::F8 => Some("F8"),
183 + keycodes::F9 => Some("F9"),
184 + keycodes::F10 => Some("F10"),
185 + keycodes::F11 => Some("F11"),
186 + keycodes::F12 => Some("F12"),
187 + keycodes::A => Some("a"),
188 + keycodes::B => Some("b"),
189 + keycodes::C => Some("c"),
190 + keycodes::D => Some("d"),
191 + keycodes::E => Some("e"),
192 + keycodes::F => Some("f"),
193 + keycodes::G => Some("g"),
194 + keycodes::H => Some("h"),
195 + keycodes::I => Some("i"),
196 + keycodes::J => Some("j"),
197 + keycodes::K => Some("k"),
198 + keycodes::L => Some("l"),
199 + keycodes::M => Some("m"),
200 + keycodes::N => Some("n"),
201 + keycodes::O => Some("o"),
202 + keycodes::P => Some("p"),
203 + keycodes::R => Some("r"),
204 + keycodes::S => Some("s"),
205 + keycodes::T => Some("t"),
206 + keycodes::U => Some("u"),
207 + keycodes::V => Some("v"),
208 + keycodes::W => Some("w"),
209 + keycodes::X => Some("x"),
210 + keycodes::Y => Some("y"),
211 + keycodes::Z => Some("z"),
212 + keycodes::KEY_0 => Some("0"),
213 + keycodes::KEY_1 => Some("1"),
214 + keycodes::KEY_2 => Some("2"),
215 + keycodes::KEY_3 => Some("3"),
216 + keycodes::KEY_4 => Some("4"),
217 + keycodes::KEY_5 => Some("5"),
218 + keycodes::KEY_6 => Some("6"),
219 + keycodes::KEY_7 => Some("7"),
220 + keycodes::KEY_8 => Some("8"),
221 + keycodes::KEY_9 => Some("9"),
222 + keycodes::LEFT => Some("Left"),
223 + keycodes::RIGHT => Some("Right"),
224 + keycodes::UP => Some("Up"),
225 + keycodes::DOWN => Some("Down"),
226 + keycodes::HOME => Some("Home"),
227 + keycodes::END => Some("End"),
228 + keycodes::PAGE_UP => Some("PgUp"),
229 + keycodes::PAGE_DOWN => Some("PgDn"),
230 + keycodes::INSERT => Some("Ins"),
231 + keycodes::MINUS => Some("-"),
232 + keycodes::EQUAL => Some("="),
233 + keycodes::LEFT_BRACKET => Some("["),
234 + keycodes::RIGHT_BRACKET => Some("]"),
235 + keycodes::SEMICOLON => Some(";"),
236 + keycodes::APOSTROPHE => Some("'"),
237 + keycodes::GRAVE => Some("`"),
238 + keycodes::BACKSLASH => Some("\\"),
239 + keycodes::COMMA => Some(","),
240 + keycodes::PERIOD => Some("."),
241 + keycodes::SLASH => Some("/"),
242 + _ => None,
243 + }
244 + }
160 245 }
161 246
162 247 fn preprocess_variables(input: &str) -> Result<String, ConfigError> {
+3 −23 src/window_manager.rs
@@ -842,29 +842,9 @@ impl WindowManager {
842 842 }
843 843
844 844 fn format_keycode(&self, keycode: Keycode) -> String {
845 - unsafe {
846 - let keysym = x11::xlib::XKeycodeToKeysym(self.display, keycode, 0);
847 - if keysym == 0 {
848 - return "?".to_string();
849 - }
850 -
851 - let name_ptr = x11::xlib::XKeysymToString(keysym);
852 - if name_ptr.is_null() {
853 - return "?".to_string();
854 - }
855 -
856 - let c_str = std::ffi::CStr::from_ptr(name_ptr);
857 - match c_str.to_str() {
858 - Ok(s) => {
859 - if s.len() == 1 {
860 - s.to_lowercase()
861 - } else {
862 - s.to_string()
863 - }
864 - }
865 - Err(_) => "?".to_string(),
866 - }
867 - }
845 + crate::config::KeyData::from_keycode(keycode)
846 + .unwrap_or("?")
847 + .to_string()
868 848 }
869 849
870 850 fn update_bar(&mut self) -> WmResult<()> {