Added window rules such as opening a window in a specific tag, or opening it floating.
Browse files
+4 −0 resources/test-config.lua
| @@ -46,6 +46,10 @@ oxwm.gaps.set_smart(true) -- Disable outer gaps when only 1 window (dwm smartga | ||
| 46 | 46 | oxwm.gaps.set_inner(5, 5) |
| 47 | 47 | oxwm.gaps.set_outer(5, 5) |
| 48 | 48 | |
| 49 | +oxwm.rule.add({ class = "firefox", title = "Library", floating = true }) | |
| 50 | +oxwm.rule.add({ instance = "gimp", tag = 5 }) | |
| 51 | +oxwm.rule.add({ class = "mpv", floating = true }) | |
| 52 | + | |
| 49 | 53 | oxwm.bar.set_font("JetBrainsMono Nerd Font:style=Bold:size=12") |
| 50 | 54 | |
| 51 | 55 | oxwm.bar.set_scheme_normal(colors.fg, colors.bg, 0x444444) |
+1 −0 src/config/lua.rs
| @@ -42,6 +42,7 @@ pub fn parse_lua_config( | ||
| 42 | 42 | tags: builder_data.tags, |
| 43 | 43 | layout_symbols: builder_data.layout_symbols, |
| 44 | 44 | keybindings: builder_data.keybindings, |
| 45 | + window_rules: builder_data.window_rules, | |
| 45 | 46 | status_blocks: builder_data.status_blocks, |
| 46 | 47 | scheme_normal: builder_data.scheme_normal, |
| 47 | 48 | scheme_occupied: builder_data.scheme_occupied, |
+42 −0 src/config/lua_api.rs
| @@ -26,6 +26,7 @@ pub struct ConfigBuilder { | ||
| 26 | 26 | pub tags: Vec<String>, |
| 27 | 27 | pub layout_symbols: Vec<crate::LayoutSymbolOverride>, |
| 28 | 28 | pub keybindings: Vec<KeyBinding>, |
| 29 | + pub window_rules: Vec<crate::WindowRule>, | |
| 29 | 30 | pub status_blocks: Vec<BlockConfig>, |
| 30 | 31 | pub scheme_normal: ColorScheme, |
| 31 | 32 | pub scheme_occupied: ColorScheme, |
| @@ -51,6 +52,7 @@ impl Default for ConfigBuilder { | ||
| 51 | 52 | tags: vec!["1".into(), "2".into(), "3".into()], |
| 52 | 53 | layout_symbols: Vec::new(), |
| 53 | 54 | keybindings: Vec::new(), |
| 55 | + window_rules: Vec::new(), | |
| 54 | 56 | status_blocks: Vec::new(), |
| 55 | 57 | scheme_normal: ColorScheme { |
| 56 | 58 | foreground: 0xffffff, |
| @@ -86,6 +88,7 @@ pub fn register_api(lua: &Lua) -> Result<SharedBuilder, ConfigError> { | ||
| 86 | 88 | register_client_module(&lua, &oxwm_table)?; |
| 87 | 89 | register_layout_module(&lua, &oxwm_table)?; |
| 88 | 90 | register_tag_module(&lua, &oxwm_table)?; |
| 91 | + register_rule_module(&lua, &oxwm_table, builder.clone())?; | |
| 89 | 92 | register_bar_module(&lua, &oxwm_table, builder.clone())?; |
| 90 | 93 | register_misc(&lua, &oxwm_table, builder.clone())?; |
| 91 | 94 | |
| @@ -321,6 +324,45 @@ fn register_tag_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { | ||
| 321 | 324 | Ok(()) |
| 322 | 325 | } |
| 323 | 326 | |
| 327 | +fn register_rule_module(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { | |
| 328 | + let rule_table = lua.create_table()?; | |
| 329 | + | |
| 330 | + let builder_clone = builder.clone(); | |
| 331 | + let add = lua.create_function(move |_, config: Table| { | |
| 332 | + let class: Option<String> = config.get("class").ok(); | |
| 333 | + let instance: Option<String> = config.get("instance").ok(); | |
| 334 | + let title: Option<String> = config.get("title").ok(); | |
| 335 | + let is_floating: Option<bool> = config.get("floating").ok(); | |
| 336 | + let monitor: Option<usize> = config.get("monitor").ok(); | |
| 337 | + | |
| 338 | + let tags: Option<u32> = if let Ok(tag_index) = config.get::<i32>("tag") { | |
| 339 | + if tag_index > 0 { | |
| 340 | + Some(1 << (tag_index - 1)) | |
| 341 | + } else { | |
| 342 | + None | |
| 343 | + } | |
| 344 | + } else { | |
| 345 | + None | |
| 346 | + }; | |
| 347 | + | |
| 348 | + let rule = crate::WindowRule { | |
| 349 | + class, | |
| 350 | + instance, | |
| 351 | + title, | |
| 352 | + tags, | |
| 353 | + is_floating, | |
| 354 | + monitor, | |
| 355 | + }; | |
| 356 | + | |
| 357 | + builder_clone.borrow_mut().window_rules.push(rule); | |
| 358 | + Ok(()) | |
| 359 | + })?; | |
| 360 | + | |
| 361 | + rule_table.set("add", add)?; | |
| 362 | + parent.set("rule", rule_table)?; | |
| 363 | + Ok(()) | |
| 364 | +} | |
| 365 | + | |
| 324 | 366 | fn register_bar_module(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { |
| 325 | 367 | let bar_table = lua.create_table()?; |
| 326 | 368 |
+24 −0 src/lib.rs
| @@ -12,6 +12,7 @@ pub mod window_manager; | ||
| 12 | 12 | pub mod prelude { |
| 13 | 13 | pub use crate::ColorScheme; |
| 14 | 14 | pub use crate::LayoutSymbolOverride; |
| 15 | + pub use crate::WindowRule; | |
| 15 | 16 | pub use crate::bar::{BlockCommand, BlockConfig}; |
| 16 | 17 | pub use crate::keyboard::{Arg, KeyAction, handlers::KeyBinding, keysyms}; |
| 17 | 18 | pub use x11rb::protocol::xproto::KeyButMask; |
| @@ -23,6 +24,25 @@ pub struct LayoutSymbolOverride { | ||
| 23 | 24 | pub symbol: String, |
| 24 | 25 | } |
| 25 | 26 | |
| 27 | +#[derive(Clone)] | |
| 28 | +pub struct WindowRule { | |
| 29 | + pub class: Option<String>, | |
| 30 | + pub instance: Option<String>, | |
| 31 | + pub title: Option<String>, | |
| 32 | + pub tags: Option<u32>, | |
| 33 | + pub is_floating: Option<bool>, | |
| 34 | + pub monitor: Option<usize>, | |
| 35 | +} | |
| 36 | + | |
| 37 | +impl WindowRule { | |
| 38 | + pub fn matches(&self, class: &str, instance: &str, title: &str) -> bool { | |
| 39 | + let class_matches = self.class.as_ref().map_or(true, |c| class.contains(c.as_str())); | |
| 40 | + let instance_matches = self.instance.as_ref().map_or(true, |i| instance.contains(i.as_str())); | |
| 41 | + let title_matches = self.title.as_ref().map_or(true, |t| title.contains(t.as_str())); | |
| 42 | + class_matches && instance_matches && title_matches | |
| 43 | + } | |
| 44 | +} | |
| 45 | + | |
| 26 | 46 | #[derive(Clone)] |
| 27 | 47 | pub struct Config { |
| 28 | 48 | // Appearance |
| @@ -52,6 +72,9 @@ pub struct Config { | ||
| 52 | 72 | // Keybindings |
| 53 | 73 | pub keybindings: Vec<crate::keyboard::handlers::Key>, |
| 54 | 74 | |
| 75 | + // Window rules | |
| 76 | + pub window_rules: Vec<WindowRule>, | |
| 77 | + | |
| 55 | 78 | // Status bar |
| 56 | 79 | pub status_blocks: Vec<crate::bar::BlockConfig>, |
| 57 | 80 | |
| @@ -284,6 +307,7 @@ impl Default for Config { | ||
| 284 | 307 | Arg::Int(8), |
| 285 | 308 | ), |
| 286 | 309 | ], |
| 310 | + window_rules: vec![], | |
| 287 | 311 | status_blocks: vec![crate::bar::BlockConfig { |
| 288 | 312 | format: "{}".to_string(), |
| 289 | 313 | command: crate::bar::BlockCommand::DateTime("%a, %b %d - %-I:%M %P".to_string()), |
+78 −4 src/window_manager.rs
| @@ -1824,6 +1824,79 @@ impl WindowManager { | ||
| 1824 | 1824 | }) |
| 1825 | 1825 | } |
| 1826 | 1826 | |
| 1827 | + fn get_window_class_instance(&self, window: Window) -> (String, String) { | |
| 1828 | + let reply = self.connection | |
| 1829 | + .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024) | |
| 1830 | + .ok() | |
| 1831 | + .and_then(|cookie| cookie.reply().ok()); | |
| 1832 | + | |
| 1833 | + if let Some(reply) = reply { | |
| 1834 | + if !reply.value.is_empty() { | |
| 1835 | + if let Ok(text) = std::str::from_utf8(&reply.value) { | |
| 1836 | + let parts: Vec<&str> = text.split('\0').collect(); | |
| 1837 | + let instance = parts.get(0).unwrap_or(&"").to_string(); | |
| 1838 | + let class = parts.get(1).unwrap_or(&"").to_string(); | |
| 1839 | + return (instance, class); | |
| 1840 | + } | |
| 1841 | + } | |
| 1842 | + } | |
| 1843 | + | |
| 1844 | + (String::new(), String::new()) | |
| 1845 | + } | |
| 1846 | + | |
| 1847 | + fn apply_rules(&mut self, window: Window) -> WmResult<()> { | |
| 1848 | + let (instance, class) = self.get_window_class_instance(window); | |
| 1849 | + let title = self.clients.get(&window).map(|c| c.name.clone()).unwrap_or_default(); | |
| 1850 | + | |
| 1851 | + let mut rule_tags: Option<u32> = None; | |
| 1852 | + let mut rule_floating: Option<bool> = None; | |
| 1853 | + let mut rule_monitor: Option<usize> = None; | |
| 1854 | + | |
| 1855 | + for rule in &self.config.window_rules { | |
| 1856 | + if rule.matches(&class, &instance, &title) { | |
| 1857 | + if rule.tags.is_some() { | |
| 1858 | + rule_tags = rule.tags; | |
| 1859 | + } | |
| 1860 | + if rule.is_floating.is_some() { | |
| 1861 | + rule_floating = rule.is_floating; | |
| 1862 | + } | |
| 1863 | + if rule.monitor.is_some() { | |
| 1864 | + rule_monitor = rule.monitor; | |
| 1865 | + } | |
| 1866 | + } | |
| 1867 | + } | |
| 1868 | + | |
| 1869 | + if let Some(client) = self.clients.get_mut(&window) { | |
| 1870 | + if let Some(is_floating) = rule_floating { | |
| 1871 | + client.is_floating = is_floating; | |
| 1872 | + if is_floating { | |
| 1873 | + self.floating_windows.insert(window); | |
| 1874 | + } else { | |
| 1875 | + self.floating_windows.remove(&window); | |
| 1876 | + } | |
| 1877 | + } | |
| 1878 | + | |
| 1879 | + if let Some(monitor_index) = rule_monitor { | |
| 1880 | + if monitor_index < self.monitors.len() { | |
| 1881 | + client.monitor_index = monitor_index; | |
| 1882 | + } | |
| 1883 | + } | |
| 1884 | + | |
| 1885 | + let tags = rule_tags.unwrap_or_else(|| { | |
| 1886 | + self.monitors | |
| 1887 | + .get(client.monitor_index) | |
| 1888 | + .map(|m| m.tagset[m.selected_tags_index]) | |
| 1889 | + .unwrap_or(tag_mask(0)) | |
| 1890 | + }); | |
| 1891 | + | |
| 1892 | + client.tags = tags; | |
| 1893 | + self.window_tags.insert(window, tags); | |
| 1894 | + self.window_monitor.insert(window, client.monitor_index); | |
| 1895 | + } | |
| 1896 | + | |
| 1897 | + Ok(()) | |
| 1898 | + } | |
| 1899 | + | |
| 1827 | 1900 | fn manage_window(&mut self, window: Window) -> WmResult<()> { |
| 1828 | 1901 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1829 | 1902 | let mut window_x = geometry.x as i32; |
| @@ -1904,12 +1977,13 @@ impl WindowManager { | ||
| 1904 | 1977 | self.clients.insert(window, client); |
| 1905 | 1978 | self.update_size_hints(window)?; |
| 1906 | 1979 | self.update_window_title(window)?; |
| 1907 | - self.attach_aside(window, monitor_index); | |
| 1908 | - self.attach_stack(window, monitor_index); | |
| 1980 | + self.apply_rules(window)?; | |
| 1981 | + | |
| 1982 | + let updated_monitor_index = self.clients.get(&window).map(|c| c.monitor_index).unwrap_or(monitor_index); | |
| 1983 | + self.attach_aside(window, updated_monitor_index); | |
| 1984 | + self.attach_stack(window, updated_monitor_index); | |
| 1909 | 1985 | |
| 1910 | 1986 | self.windows.push(window); |
| 1911 | - self.window_tags.insert(window, window_tags); | |
| 1912 | - self.window_monitor.insert(window, monitor_index); | |
| 1913 | 1987 | |
| 1914 | 1988 | if is_transient || is_dialog { |
| 1915 | 1989 | self.floating_windows.insert(window); |