Add for_exec_window command and activation_token criteria#339
Conversation
|
This is related to this old feature request for sway: swaywm/sway#4045 |
- Implement `for_exec_window <commands> <command_to_exec>` to run commands on the next window opened by the executed process. - Add `activation_token` criteria to match windows by their XDG activation token. - Implement `IPC_MINT_ACTIVATION_TOKEN` (124) IPC command to allow clients to obtain a freshly generated XDG activation token. - Expose `activation_token` in the window JSON representation (get_tree). - Add documentation for the new IPC command in `scroll-ipc.7.scd` and `README.md`. - Add tests for `for_exec_window` (including PID, XDG activation, and X11 startup ID matching) and `IPC_MINT_ACTIVATION_TOKEN`.
|
I prefer to use Lua for these things instead of cluttering the code more. The Lua environment is richer and more powerful than some parsed config rule. In general The local scroll = require("scroll")
local function on_create(view, data)
if scroll.view_get_app_id(view) == "kitty" then
local container = scroll.view_get_container(view)
if container then
scroll.container_set_focus(container)
scroll.command(scroll.view_get_container(view), "set_size h 0.2")
scroll.command(scroll.view_get_container(view), "align left")
end
end
end
scroll.add_callback("view_map", on_create, nil)If you think something is missing from the Lua API to implement this feature, maybe it would be better to add it there. Currently, there are functions to retrieve information about the view and application running in it, maybe something else is missing there. Running commands is already supported. |
|
The key thing this adds is the ability to match based on an xdg activation token, which was not previously exposed either via IPC or LUA, in order to say: launch a new window, and perform certain actions on just that exact window associated with my launch action. I can certainly add a LUA API for getting the activation token of a window (which this PR also exposes via get_tree now) and for minting a new activation token (which this PR also exposes as a new IPC request), but I think the added |
|
You can already do what
local args, state = ...
local scroll = require("scroll")
local id_cmd = nil
local id_app = nil
local function on_view_map(view, data)
if id_app then
scroll.remove_callback(id_app)
end
if #args > 1 then
local container = scroll.view_get_container(view)
scroll.command(container, args[2])
end
end
local function on_command_end(cmd_data, data)
if id_cmd then
scroll.remove_callback(id_cmd)
end
id_app = scroll.add_callback("view_map", on_view_map, nil)
end
if #args > 0 then
id_cmd = scroll.add_callback("command_end", on_command_end, nil)
scroll.command(nil, args[1])
endand with the |
|
The problem with this approach of just matching the next window to map is that if there are multiple windows opening concurrently you might match the wrong one. For my specific use case of the session manager I want to be able to restore multiple windows at the same time in some cases (e.g. after restart) and also be robust to the user opening windows manually at the same time. I also can't rely on app_id because there may be multiple windows with the same app_id, e.g. multiple terminals or multiple browser windows. There are other heuristics that can be used for matching, like title, but activation token provides the most robust and easiest way by far. |
|
Sure, what I meant is we could add Lua functions to retrieve the view's launch context activation token, or whatever is necessary. There is already one to get the view's parent PID too. My main idea when I added Lua was to get rid of IPC as much as possible, because it is usually abused, slow, and needs a lot of parsing of the content tree, which is fragile and prone to errors and questions by users. |
for_exec_window allows executing a program and applying a list of sway commands to the window created by that program.
Syntax: for_exec_window ""
Matches via XDG activation token, PID (fallback), or X11 startup ID (Xwayland).
Expose activation_token in window's JSON representation (IPC). Added activation_token to sway_view and serialized it in get_tree.
Support for_window [activation_token=...] criteria. This is a one-shot criteria that binds commands to the corresponding launcher_ctx immediately, and cannot be combined with other criteria.
Changes: