2016-04-13 05:18:09 +02:00
|
|
|
local spawn = require("awful.spawn")
|
2021-10-29 06:12:07 +02:00
|
|
|
local gtimer = require("gears.timer")
|
2016-04-13 05:18:09 +02:00
|
|
|
|
2021-03-08 16:25:54 +01:00
|
|
|
local lua_executable = os.getenv("LUA")
|
2021-03-06 19:48:32 +01:00
|
|
|
if lua_executable == nil or lua_executable == "" then
|
|
|
|
lua_executable = "lua"
|
|
|
|
end
|
|
|
|
|
2016-04-13 05:18:09 +02:00
|
|
|
-- This file provide a simple, yet flexible, test client.
|
2019-10-07 03:53:08 +02:00
|
|
|
-- It is used to test the `ruled.client`
|
2016-04-13 05:18:09 +02:00
|
|
|
|
2016-09-16 18:16:28 +02:00
|
|
|
local test_client_source = [[
|
2019-09-30 04:19:41 +02:00
|
|
|
pcall(require, 'luarocks.loader')
|
2021-10-29 05:24:47 +02:00
|
|
|
local lgi = require 'lgi'
|
|
|
|
local GLib = lgi.require('GLib')
|
|
|
|
local Gdk = lgi.require('Gdk')
|
2021-10-29 07:12:52 +02:00
|
|
|
local Gtk = lgi.require('Gtk', '3.0')
|
2021-10-29 05:24:47 +02:00
|
|
|
local Gio = lgi.require('Gio')
|
2016-09-16 18:16:28 +02:00
|
|
|
Gtk.init()
|
|
|
|
|
2017-01-08 15:14:08 +01:00
|
|
|
local function open_window(class, title, options)
|
2016-09-16 18:16:28 +02:00
|
|
|
local window = Gtk.Window {
|
2018-09-12 07:20:53 +02:00
|
|
|
default_width = options.default_width or 100,
|
|
|
|
default_height = options.default_height or 100,
|
2016-09-16 18:16:28 +02:00
|
|
|
title = title
|
|
|
|
}
|
2017-02-26 20:20:08 +01:00
|
|
|
if options.gravity then
|
|
|
|
window:set_gravity(tonumber(options.gravity))
|
|
|
|
end
|
2017-01-08 15:14:08 +01:00
|
|
|
if options.snid and options.snid ~= "" then
|
|
|
|
window:set_startup_id(options.snid)
|
|
|
|
end
|
|
|
|
if options.resize_increment then
|
2017-02-02 22:30:04 +01:00
|
|
|
-- This requires Gtk3, but fails with an obscure message with Gtk2.
|
|
|
|
-- Produce a better error message instead.
|
|
|
|
assert(tonumber(require("lgi").Gtk._version) >= 3, "Gtk 3 required, but not found")
|
2017-01-08 15:14:08 +01:00
|
|
|
local geom = Gdk.Geometry {
|
|
|
|
width_inc = 200,
|
|
|
|
height_inc = 200,
|
|
|
|
}
|
|
|
|
window:set_geometry_hints(nil, geom, Gdk.WindowHints.RESIZE_INC)
|
2016-09-16 18:16:28 +02:00
|
|
|
end
|
2017-04-23 10:50:12 +02:00
|
|
|
if options.maximize_before then
|
|
|
|
window:maximize()
|
2021-10-29 05:24:47 +02:00
|
|
|
elseif options.unminimize_after then
|
|
|
|
window:iconify()
|
2017-04-23 10:50:12 +02:00
|
|
|
end
|
2016-09-16 18:16:28 +02:00
|
|
|
window:set_wmclass(class, class)
|
|
|
|
window:show_all()
|
2017-04-23 10:50:12 +02:00
|
|
|
if options.maximize_after then
|
|
|
|
window:maximize()
|
2019-10-16 01:14:31 +02:00
|
|
|
elseif options.unmaximize_after then
|
|
|
|
window:unmaximize()
|
2021-10-29 05:24:47 +02:00
|
|
|
elseif options.minimize_after then
|
|
|
|
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 500, function()
|
|
|
|
window:iconify()
|
|
|
|
end)
|
|
|
|
elseif options.unminimize_after then
|
|
|
|
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 500, function()
|
|
|
|
window:deiconify()
|
|
|
|
end)
|
2017-04-23 10:50:12 +02:00
|
|
|
end
|
2017-05-14 22:45:50 +02:00
|
|
|
if options.resize_after_width and options.resize_after_height then
|
|
|
|
window:resize(
|
|
|
|
tonumber(options.resize_after_width), tonumber(options.resize_after_height)
|
|
|
|
)
|
|
|
|
end
|
2016-09-16 18:16:28 +02:00
|
|
|
end
|
|
|
|
|
2017-01-08 15:14:08 +01:00
|
|
|
local function parse_options(options)
|
|
|
|
local result = {}
|
|
|
|
for word in string.gmatch(options, "([^,]+)") do
|
|
|
|
local key, value = string.match(word, "([^=]+)=?(.*)")
|
|
|
|
result[key] = value
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2016-09-16 18:16:28 +02:00
|
|
|
-- Start a coroutine for nicer input handling
|
|
|
|
local coro = coroutine.wrap(function()
|
|
|
|
while true do
|
|
|
|
local class = coroutine.yield()
|
|
|
|
local title = coroutine.yield()
|
2017-01-08 15:14:08 +01:00
|
|
|
local options = coroutine.yield()
|
|
|
|
open_window(class, title, parse_options(options))
|
2016-09-16 18:16:28 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
coro()
|
|
|
|
|
|
|
|
-- Read lines from stdin and feed them to the coroutine
|
|
|
|
local stdin = Gio.UnixInputStream.new(0, false)
|
|
|
|
stdin = Gio.DataInputStream.new(stdin)
|
|
|
|
|
|
|
|
local read_start, read_finish
|
|
|
|
read_start = function()
|
|
|
|
stdin:read_line_async(0, nil, read_finish)
|
|
|
|
end
|
|
|
|
read_finish = function(...)
|
|
|
|
local line, length = stdin.read_line_finish(...)
|
|
|
|
if type(length) ~= "number" then
|
|
|
|
error("Error reading line: " .. tostring(length))
|
|
|
|
end
|
|
|
|
|
|
|
|
local eof = not line -- Behaviour of up-to-date LGI
|
|
|
|
or (tostring(line) == "" and #line ~= length) -- (Ab)use uninitialized variable
|
|
|
|
if eof then
|
|
|
|
Gtk.main_quit()
|
|
|
|
else
|
|
|
|
coro(tostring(line)) -- tostring() needed for older LGI versions
|
|
|
|
read_start()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
read_start()
|
|
|
|
Gtk:main{...}
|
|
|
|
]]
|
|
|
|
|
|
|
|
local lgi = require("lgi")
|
|
|
|
local Gio = lgi.require("Gio")
|
|
|
|
|
|
|
|
local initialized = false
|
2021-10-29 06:12:07 +02:00
|
|
|
local pipe, pid
|
|
|
|
|
2016-09-16 18:16:28 +02:00
|
|
|
local function init()
|
|
|
|
if initialized then return end
|
|
|
|
initialized = true
|
2021-03-06 19:48:32 +01:00
|
|
|
local cmd = { lua_executable, "-e", test_client_source }
|
2021-10-29 06:12:07 +02:00
|
|
|
local _pid, _, stdin, stdout, stderr = awesome.spawn(cmd, false, true, true, true)
|
|
|
|
pipe = Gio.UnixOutputStream.new(stdin, true)
|
|
|
|
pid = _pid
|
2016-09-16 18:16:28 +02:00
|
|
|
stdout = Gio.UnixInputStream.new(stdout, true)
|
|
|
|
stderr = Gio.UnixInputStream.new(stderr, true)
|
|
|
|
spawn.read_lines(stdout, function(...) print("_client", ...) end)
|
|
|
|
spawn.read_lines(stderr, function(...) print("_client", ...) end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Hack needed for awesome's Startup Notification machinery
|
2016-11-28 20:58:28 +01:00
|
|
|
local function get_snid(sn_rules, callback)
|
|
|
|
local success, snid = spawn({ "/bin/true" }, sn_rules, callback)
|
2016-09-16 18:16:28 +02:00
|
|
|
assert(success)
|
|
|
|
assert(snid)
|
|
|
|
return snid
|
|
|
|
end
|
|
|
|
|
2021-10-29 06:12:07 +02:00
|
|
|
local module = {}
|
|
|
|
|
|
|
|
function module.terminate()
|
|
|
|
if not initialized then return end
|
|
|
|
|
|
|
|
for _, c in ipairs(client.get()) do
|
|
|
|
c:kill()
|
|
|
|
end
|
|
|
|
|
|
|
|
pipe:close()
|
|
|
|
initialized, pipe = false, nil
|
|
|
|
|
|
|
|
-- Make a copy to avoid the re-initialized race condition.
|
|
|
|
local original_pid = pid
|
|
|
|
|
|
|
|
gtimer.delayed_call(function()
|
|
|
|
awesome.kill(original_pid, 9)
|
|
|
|
end)
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function new(_, class, title, sn_rules, callback, resize_increment, args)
|
2017-02-26 20:20:08 +01:00
|
|
|
args = args or {}
|
2016-09-16 18:16:28 +02:00
|
|
|
class = class or "test_app"
|
|
|
|
title = title or "Awesome test client"
|
|
|
|
|
|
|
|
init()
|
2017-01-08 15:14:08 +01:00
|
|
|
local options = ""
|
|
|
|
local snid
|
|
|
|
if sn_rules or callback then
|
|
|
|
snid = get_snid(sn_rules, callback)
|
|
|
|
options = options .. "snid=" .. snid .. ","
|
|
|
|
end
|
|
|
|
if resize_increment then
|
|
|
|
options = options .. "resize_increment,"
|
|
|
|
end
|
2017-04-23 10:50:12 +02:00
|
|
|
if args.maximize_before then
|
|
|
|
options = options .. "maximize_before,"
|
|
|
|
end
|
|
|
|
if args.maximize_after then
|
|
|
|
options = options .. "maximize_after,"
|
|
|
|
end
|
2019-10-16 01:14:31 +02:00
|
|
|
if args.unmaximize_after then
|
|
|
|
options = options .. "unmaximize_after,"
|
|
|
|
end
|
2021-10-29 05:24:47 +02:00
|
|
|
if args.unminimize_after then
|
|
|
|
options = options .. "unminimize_after,"
|
|
|
|
end
|
|
|
|
if args.minimize_after then
|
|
|
|
options = options .. "minimize_after,"
|
|
|
|
end
|
|
|
|
|
2018-09-12 07:20:53 +02:00
|
|
|
if args.size then
|
|
|
|
options = table.concat {
|
|
|
|
options,
|
|
|
|
"default_width=",
|
|
|
|
args.size.width, ",",
|
|
|
|
"default_height=",
|
|
|
|
args.size.height, ","
|
|
|
|
}
|
|
|
|
end
|
2017-05-14 22:45:50 +02:00
|
|
|
if args.resize then
|
|
|
|
options = table.concat {
|
|
|
|
options,
|
|
|
|
"resize_after_width=",
|
2018-09-12 07:15:38 +02:00
|
|
|
args.resize.width, ",",
|
2017-05-14 22:45:50 +02:00
|
|
|
"resize_after_height=",
|
2018-09-12 07:15:38 +02:00
|
|
|
args.resize.height, ","
|
2017-05-14 22:45:50 +02:00
|
|
|
}
|
|
|
|
end
|
2017-02-26 20:20:08 +01:00
|
|
|
if args.gravity then
|
|
|
|
assert(type(args.gravity)=="number","Use `lgi.Gdk.Gravity.NORTH_WEST`")
|
|
|
|
options = options .. "gravity=" .. args.gravity .. ","
|
|
|
|
end
|
2017-05-14 22:45:50 +02:00
|
|
|
|
2017-01-08 15:14:08 +01:00
|
|
|
local data = class .. "\n" .. title .. "\n" .. options .. "\n"
|
2016-09-16 18:16:28 +02:00
|
|
|
local success, msg = pipe:write_all(data)
|
2017-01-02 16:10:38 +01:00
|
|
|
assert(success, tostring(msg))
|
2016-09-16 18:16:28 +02:00
|
|
|
|
2016-11-28 21:02:27 +01:00
|
|
|
return snid
|
2016-04-13 05:18:09 +02:00
|
|
|
end
|
2016-05-01 19:23:22 +02:00
|
|
|
|
2021-10-29 06:12:07 +02:00
|
|
|
return setmetatable(module, {__call = new })
|
|
|
|
|
2016-05-01 19:23:22 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|