Create a gist now

Instantly share code, notes, and snippets.

io = require("io")
os = require("os")
function read_all(file)
local f = io.open(file, "rb")
if not f then
return nil
end
local content = f:read("*a")
f:close()
return content
end
function proc_get_name(pid)
cmdline = read_all("/proc/" .. pid .. "/cmdline")
if not cmdline then
return nil
end
for match in string.gmatch(cmdline, '[^\0]+') do
proc_path = match
end
proc_name = string.match(proc_path, '[^/]+$')
return proc_name
end
WindowMapping = {}
WindowMapping.__index = WindowMapping
function WindowMapping.new(args)
if not args.target then
error("Must provide a target")
end
if not args.class and not args.instance and not args.executables then
error("Must provide one of class, instance or executables")
end
local self = setmetatable({}, WindowMapping)
self.target = args.target
self.class = args.class
self.instance = args.instance
if args.executables then
self.executables = {}
for index, value in ipairs(args.executables) do
self.executables[value] = true
end
else
self.executables = nil
end
return self
end
function WindowMapping.check_class(self)
if self.class then
return string.match(self.class, get_window_class())
end
return true
end
function WindowMapping.check_instance(self)
if self.instance then
return string.match(self.instance, get_class_instance_name())
end
return true
end
function WindowMapping.check_executables(self)
if self.executables then
pid = get_window_property("_NET_WM_PID")
proc_name = pid and proc_get_name(pid)
return proc_name and (self.executables[proc_name] or false)
end
return true
end
local m = WindowMapping.new
class_mappings = {
m {
target = "League of Legends",
executables = {
'League of Legends.exe',
'LolClient.exe',
'LoLLauncher.exe',
'LoLPatcher.exe'
}
},
m {
target = "Battle.net",
executables = {'Battle.net.exe'}
},
m {
target = "Hearthstone",
executables = {'Hearthstone.exe'}
},
m {
target = "Starcraft II",
executables = {'SC2.exe', 'Starcraft II.exe', 'Blizzard Launcher.exe'}
},
m {
target = "South Park: The Stick of Truth",
executables = {'South Park - The Stick of Truth.exe'}
},
m {
target = "Steam (Wine)",
executables = {'Steam.exe'}
},
m {
target = "Counter-Strike: Global Offensive",
executables = {'csgo.exe'}
}
}
for i, mapping in ipairs(class_mappings) do
target = mapping.target
matched = true
if mapping:check_executables()
and mapping:check_class()
and mapping:check_instance()
then
debug_print(string.format("Matched window %q to target %q",
get_window_name(), target))
cmd = string.format("xprop -f WM_CLASS 8s -set WM_CLASS %q -id %d",
target, get_window_xid())
debug_print(cmd)
os.execute(cmd)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment