-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace.lua
264 lines (231 loc) · 7.28 KB
/
workspace.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
-- This project is licensed under the MIT License (see LICENSE).
--- Create new workspaces and launch accompanying clients.
--
-- @author James Reed <[email protected]>
-- @copyright 2019-2020 James Reed
-- @module awesome-launch.workspace
local awful = require("awful")
local naughty = require("naughty")
local gtable = require("gears.table")
local gtimer = require("gears.timer")
local protected_call = require("gears.protected_call")
local lgi = require("lgi")
local Gio, GLib, GObject = lgi.Gio, lgi.GLib, lgi.GObject
local launch = require("awesome-launch")
local shared = require("awesome-launch.shared")
local ws = {}
ws.client = {}
ws.clients = {}
local function add_clients(cs, tag)
for _, c in ipairs(cs) do
local cmd
local cmdargs
if type(c) == "table" then
cmd = c[1]
if c[2] then
cmdargs = gtable.clone(c[2], false)
end
end
ws.client.add(cmd or c, cmdargs, tag)
end
end
local function handle_args(tag, args)
args = args or {}
if args.pwd then
tag.pwd = args.pwd
end
if args.replace and not tag.volatile then
for _, c in ipairs(tag:clients()) do
c:kill()
end
end
if args.clients then
add_clients(args.clients, tag)
end
if args.callback then
args.callback(tag)
end
return tag
end
--- Spawn a command and add the client to a tag.
--
-- @param cmd The command.
-- @param args Table containing the single instance ID and additional arguments for spawn
-- @param args.id Single instance ID.
-- @param args.props Properties to apply to the client.
-- @param args.pwd Pathname to the working directory for new clients.
-- @param args.timeout Seconds after which to stop waiting for a client to spawn.
-- @param args.callback Function to call with client when it spawns.
-- @param args.factory The factory to use (see wm-launch's -f flag).
-- @param args.systemd If true, run cmd with systemd-run.
-- @param args.firejail If true, run cmd with firejail.
-- @param tag The tag.
-- @function client.add
function ws.client.add(cmd, args, tag)
args = args and gtable.clone(args) or {}
tag = tag or awful.screen.focused().selected_tag
args.props = args.props or {}
args.props.tag = tag
args.props.tags = nil
if tag.pwd then
args.pwd = tag.pwd
end
launch.spawn(cmd, args)
end
--- Create a new workspace and underlying (volatile) tag.
--
-- @param name The tag name.
-- @param args Table containing tag properties and additional workspace options
-- @param args.props Properties to apply to the tag.
-- @param args.pwd Pathname to the working directory for new clients.
-- @param args.clients Table containing client commands to spawn.
--
-- Example: `args.clients = { "xterm",
-- {"qutebrowser", {factory="qutebrowser"}} }`
--
-- @param args.load_workspace Path to directory containing workspace file to
-- load. Implies args.pwd.
-- @param args.callback Function to call with newly created tag.
-- @return The new tag.
-- @function new
function ws.new(name, args)
local props = {
screen = awful.screen.focused(),
layout = awful.layout.layouts[1],
}
if args and args.props then
gtable.crush(props, args.props)
end
local tag = awful.tag.add(name, props)
local function delete()
gtimer.delayed_call(function ()
for _, data in pairs(shared.pending) do
if data.props.tag and data.props.tag == tag then
return
end
end
if not tag.selected and #tag:clients() == 0 then
tag:delete()
end
end)
end
tag:connect_signal("property::selected", delete)
tag:connect_signal("untagged", delete)
return handle_args(tag, args)
end
--- Add to or replace a given tag's clients.
--
-- @param tag The tag to affect.
-- @param args Table containing tag properties and additional workspace options
-- @param args.pwd Pathname to the working directory for new clients.
-- @param args.replace Kill tag's existing clients if true.
-- @param args.clients Table containing client commands to spawn.
--
-- Example: `args.clients = { "xterm",
-- {"qutebrowser", {factory="qutebrowser"}} }`
--
-- @param args.load_workspace Path to directory containing workspace file to
-- load. Implies args.pwd.
-- @return The affected tag.
-- @function add
function ws.add(tag, args)
return handle_args(tag, args)
end
--- Add to or replace the selected tag's clients.
--
-- @param args Table containing tag properties and additional workspace options
-- @param args.pwd Pathname to the working directory for new clients.
-- @param args.replace Kill tag's existing clients if true.
-- @param args.clients Table containing client commands to spawn.
--
-- Example: `args.clients = { "xterm",
-- {"qutebrowser", {factory="qutebrowser"}} }`
--
-- @param args.load_workspace Path to directory containing workspace file to
-- load. Implies args.pwd.
-- @return The affected tag.
-- @function selected_tag
function ws.selected_tag(args)
return ws.add(awful.screen.focused().selected_tag, args)
end
local methods = {}
local function parse_client(s)
local c = {}
local i = 1
for t in string.gmatch(s, '[^%s]+') do
if i == 1 then
if t:sub(1, 1) == '@' then
local n = t:sub(2)
c = ws.clients[n]
if not c then
naughty.notify {
preset = naughty.config.presets.critical,
title = 'Client not defined',
text = n,
}
return
end
else
c[1] = t
end
else
c[1] = string.format('%s %s', c[1], t)
end
i = i + 1
end
return c
end
function methods.NewWorkspace(params, i)
local args = {
clients = {},
callback = function (t)
t:view_only()
end,
}
if params.value[2] ~= '' then
args.pwd = params.value[2]
end
for _, s in params:get_child_value(3 - 1):ipairs() do
local c = parse_client(s)
if c then
table.insert(args.clients, c)
end
end
ws.new(params.value[1], args)
i:return_value(GLib.Variant('()'))
end
local function method_call(_, _, _, _, method, params, invocation)
if methods[method] then
protected_call(methods[method], params, invocation)
end
end
local function on_bus_acquired(conn, _)
local function arg(name, sig)
return Gio.DBusArgInfo {
name = name,
signature = sig,
}
end
local method = Gio.DBusMethodInfo
local iface = Gio.DBusInterfaceInfo {
name = 'com.github.jcrd.wm_launch.WindowManager',
methods = {
method {
name = 'NewWorkspace',
in_args = {
arg('name', 's'),
arg('pwd', 's'),
arg('clients', 'as'),
},
},
},
}
conn:register_object('/com/github/jcrd/wm_launch/WindowManager',
iface,
GObject.Closure(method_call))
end
Gio.bus_own_name(Gio.BusType.SESSION,
'com.github.jcrd.wm_launch',
Gio.BusNameOwnerFlags.NONE,
GObject.Closure(on_bus_acquired))
return ws