Improve workspace system into a hashtable

This commit is contained in:
2026-09-10 18:25:53 +01:00
parent 6544ae5f23
commit 945ff0a6d3
12 changed files with 353 additions and 221 deletions
+55 -55
View File
@@ -1,4 +1,10 @@
let () =
let startup_script =
if Array.length Sys.argv <> 2 then (
Printf.eprintf "usage: kutu <startup.rb>\n";
exit 1)
else Sys.argv.(1)
in
let state = Kutu.State.new_state in
print_endline "Started Kutu WM";
@@ -18,59 +24,39 @@ let () =
"e" => 26,
"r" => 27,
"f" => 41,
"n" => 57,
"b" => 56,
"m" => 58,
"v" => 55,
"]" => 35,
"[" => 34,
"1" => 10,
"2" => 11,
"3" => 12,
"4" => 13,
"5" => 14,
"6" => 15,
"7" => 16,
"8" => 17,
"9" => 18,
"0" => 19
}
def bind(key, mod = :alt, &block)
key = KEY_MAP[key]
key_ = key.is_a?(String) ? KEY_MAP[key] : key
mod = MOD_MAP[mod]
raise "unknown key" unless key
raise "unknown key #{key.to_s}" unless key_
raise "unknown modifier" unless mod
_bind key, mod, &block
_bind key_, mod, &block
end
def unbind(key, mod = :alt)
key = KEY_MAP[key]
key_ = key.is_a?(String) ? KEY_MAP[key] : key
mod = MOD_MAP[mod]
raise "unknown key" unless key
raise "unknown key #{key.to_s}" unless key_
raise "unknown modifier" unless mod
_unbind key, mod
end
bind "tab" do
system "firefox >/dev/null 2>&1 &"
end
bind ?q do |x|
kill x
end
bind ?w do
system "kitty >/dev/null 2>&1 &"
end
bind ?e do
exit
end
bind ?r do
unbind ?w
end
bind ?f do |x|
toggle_float x
end
bind ?v do |_, x|
remove_ws x
end
bind ?b do |_, x|
jump_ws ((x - 1 + ws_count) % ws_count)
end
bind ?n do |_, x|
jump_ws ((x + 1) % ws_count)
end
bind ?m do
new_ws
_unbind key_, mod
end
|};
Mruby.Core.mrb_load_file state.mrb startup_script;
Mruby.Core.error_check state.mrb;
if Xcb.Connection.flush state.conn <= 0 then exit 1;
@@ -85,8 +71,7 @@ let () =
let key = Xcb.Types.Events.Key.detail req in
let modifier = Xcb.Types.Events.Key.state req in
Bindings.Keybinds.dispatch_keybind state.mrb state.blocks key
modifier state.focus
(Unsigned.UInt32.of_int state.current)
modifier state.focus state.current
| MapRequest ->
let req = Xcb.Types.Events.Map_request.from ev in
let window = Xcb.Types.Events.Map_request.window req in
@@ -94,13 +79,21 @@ let () =
Xcb.Window.focus state.conn window;
Xcb.Window.move_to_bottom state.conn window;
state.focus <- window;
let ws = List.nth state.workspaces state.current in
let ws =
match Hashtbl.find_opt state.workspaces state.current with
| Some ws -> ws
| None ->
let ws = { Kutu.Layout.ws_root = None; ws_fwindows = [] } in
Hashtbl.add state.workspaces state.current ws;
ws
in
ws.ws_root <-
Kutu.Layout.insert Kutu.Layout.Horizontal window ws.ws_root;
Some (Kutu.Layout.insert Kutu.Layout.Horizontal window ws.ws_root);
List.iter
(fun { Kutu.Layout.fw_window = window; fw_rect = rect } ->
Xcb.Window.reshape state.conn window rect)
(Kutu.Layout.calculate state.screen ws.ws_root)
(Kutu.Layout.calculate state.screen ws.ws_root);
Hashtbl.replace state.window_workspace window state.current
| Create ->
let req = Xcb.Types.Events.Create.from ev in
let window = Xcb.Types.Events.Create.window req in
@@ -117,17 +110,24 @@ let () =
| Destroy ->
let req = Xcb.Types.Events.Destroy.from ev in
let window = Xcb.Types.Events.Destroy.window req in
let ws = List.nth state.workspaces state.current in
let new_root, removed = Kutu.Layout.remove window ws.ws_root in
if removed then ws.ws_root <- new_root
else
ws.ws_fwindows <- Kutu.Layout.remove_fwindow window ws.ws_fwindows;
Xcb.Window.focus state.conn state.root;
state.focus <- state.root;
List.iter
(fun { Kutu.Layout.fw_window = window; fw_rect = rect } ->
Xcb.Window.reshape state.conn window rect)
(Kutu.Layout.calculate state.screen ws.ws_root)
begin match Hashtbl.find_opt state.window_workspace window with
| None -> ()
| Some ws_name ->
let ws = Hashtbl.find state.workspaces ws_name in
let new_root, tiled_removed =
Kutu.Layout.remove window ws.ws_root
in
if tiled_removed then ws.ws_root <- new_root
else
ws.ws_fwindows <-
Kutu.Layout.remove_fwindow window ws.ws_fwindows;
Hashtbl.remove state.window_workspace window;
if ws_name = state.current then
List.iter
(fun { Kutu.Layout.fw_window = window; fw_rect = rect } ->
Xcb.Window.reshape state.conn window rect)
(Kutu.Layout.calculate state.screen ws.ws_root)
end
| _ -> ());
Xcb.Utils.free ev);
Mruby.Core.error_check state.mrb;