Update scripting system.

This commit is contained in:
2026-09-10 19:19:13 +01:00
parent 3a273d1ee8
commit 5caf6fb658
4 changed files with 74 additions and 29 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ def project_ws(n, sub)
end
def running?(process)
system("pgrep -x #{process} > /dev/null 2>&1")
system("pgrep #{process} > /dev/null 2>&1")
end
def go_project(n, sub)
+11 -4
View File
@@ -1,16 +1,23 @@
let () =
let startup_script =
if Array.length Sys.argv <> 2 then None else Some Sys.argv.(1)
if Array.length Sys.argv = 2 then Some Sys.argv.(1)
else
let default =
Filename.concat (Sys.getenv "HOME") ".config/kutu/config.rb"
in
if Sys.file_exists default then Some default else None
in
let state = Kutu.State.new_state in
print_endline "Started Kutu WM";
Bindings.Core.register_callbacks state;
Mruby.Core.mrb_load_string state.mrb Mruby.Initial.initial_code;
(match startup_script with
| None -> ()
| Some s -> Mruby.Core.mrb_load_file state.mrb s);
| Some s ->
Mruby.Core.mrb_load_string state.mrb (Mruby.Initial.initial_code s)
| None -> Mruby.Core.mrb_load_string state.mrb "");
Mruby.Core.error_check state.mrb;
if Xcb.Connection.flush state.conn <= 0 then exit 1;
-4
View File
@@ -12,10 +12,6 @@ let mrb_load_string =
foreign "mrb_load_string"
(ptr Types.Mrb_state.typ @-> string @-> returning void)
let mrb_load_file =
foreign "kutu_mrb_load_file"
(ptr Types.Mrb_state.typ @-> string @-> returning void)
let error_check mrb_ptr =
if Types.Mrb_state.has_error mrb_ptr then begin
mrb_print_error mrb_ptr;
+61 -19
View File
@@ -1,4 +1,5 @@
let initial_code =
Printf.sprintf
{|#ruby
MOD_MAP = {
none: 0,
@@ -29,21 +30,10 @@ KEY_MAP = {
"9" => 18,
"0" => 19
}
def bind(key, mod = :super, &block)
key_ = key.is_a?(String) ? KEY_MAP[key] : key
mod = MOD_MAP[mod]
raise "unknown key #{key.to_s}" unless key_
raise "unknown modifier" unless mod
_bind key_, mod, &block
end
def unbind(key, mod = :super)
key_ = key.is_a?(String) ? KEY_MAP[key] : key
mod = MOD_MAP[mod]
raise "unknown key #{key.to_s}" unless key_
raise "unknown modifier" unless mod
_unbind key_, mod
end
CONFIG_FILE = "%s"
DEFAULT = <<CODE
# ==== State ====
MAX_PROJECTS = 9
@@ -71,7 +61,7 @@ def project_ws(n, sub)
end
def running?(process)
system("pgrep -x #{process} > /dev/null 2>&1")
system("pgrep #{process} > /dev/null 2>&1")
end
def go_project(n, sub)
@@ -151,27 +141,79 @@ end
bind 86, :none do |_, ws|
next unless in_project?(ws)
n = (($current_project - 2) % MAX_PROJECTS) + 1
n = (($current_project - 2) %% MAX_PROJECTS) + 1
go_project(n, $current_sub)
end
bind 82, :none do |_, ws|
next unless in_project?(ws)
n = ($current_project % MAX_PROJECTS) + 1
n = ($current_project %% MAX_PROJECTS) + 1
go_project(n, $current_sub)
end
bind 79, :none do |_, ws|
next unless in_project?(ws)
go_project($current_project, ($current_sub + 1) % SUBS)
go_project($current_project, ($current_sub + 1) %% SUBS)
end
bind 80, :none do |_, ws|
next unless in_project?(ws)
go_project($current_project, ($current_sub - 1 + SUBS) % SUBS)
go_project($current_project, ($current_sub - 1 + SUBS) %% SUBS)
end
# ==== Startup ====
go_project(1, 0)
CODE
BINDINGS = []
SYSTEM_BINDINGS = []
def bind(key, mod = :super, system: false, &block)
key_ = key.is_a?(String) ? KEY_MAP[key] : key
mod_ = MOD_MAP[mod]
raise "unknown key #{key}" unless key_
raise "unknown modifier #{mod}" unless mod_
_bind key_, mod_, &block
(system ? SYSTEM_BINDINGS : BINDINGS) << [key_, mod_]
end
def unbind(key, mod = :super)
key_ = key.is_a?(String) ? KEY_MAP[key] : key
mod_ = MOD_MAP[mod]
raise "unknown key #{key}" unless key_
raise "unknown modifier #{mod}" unless mod_
_unbind key_, mod_
BINDINGS.delete([key_, mod_])
end
def unbind_all
BINDINGS.each do |key_, mod_|
_unbind key_, mod_
end
BINDINGS.clear
end
bind ?r, system: true do
unbind_all
if CONFIG_FILE != ""
code = File.read("script.rb")
eval code
else
eval DEFAULT
end
end
if CONFIG_FILE != ""
code = File.read("script.rb")
eval code
else
eval DEFAULT
end
|}