Skip to content

Instantly share code, notes, and snippets.

@danielkza
Created February 11, 2014 20:32
Show Gist options
  • Save danielkza/8943485 to your computer and use it in GitHub Desktop.
Save danielkza/8943485 to your computer and use it in GitHub Desktop.
require("luarocks.loader")
io = require("io")
os = require("os")
rex = require("rex_pcre")
function read_all(file)
local f = io.open(file, "rb")
if not f then
return nil
end
local content = f:read("*all")
f:close()
return content
end
function proc_get_name(pid)
return read_all("/proc/" .. pid .. "/comm")
end
WindowMapping = {}
WindowMapping.__index = WindowMapping
function WindowMapping.new(args)
local self = setmetatable({}, WindowMapping)
self.target = args.target
self.class = args.class and rex.new(args.class)
self.instance = args.instance and rex.new(args.instance)
self.executable = args.executable and rex.new(args.executable)
if not self.target then
error("Must provide a target")
end
if not self.class and not self.instance and not self.executable then
error("Must provide one of class, instance, executable")
end
return self
end
function WindowMapping.check_class(self)
if self.class then
return self.class:match(get_window_class())
end
return true
end
function WindowMapping.check_instance(self)
if self.instance then
return self.instance:match(get_class_instance_name())
end
return true
end
function WindowMapping.check_executable(self)
if self.executable then
pid = get_window_property("_NET_WM_PID")
proc_name = get_proc_name(pid)
return proc_name and self.executable:match(proc_name)
end
return true
end
local m = WindowMapping.new
class_mappings = {
m {
target = "League of Legends",
class = [[Wine]],
instance = [[(League of Legends|LolClient|LoLLauncher)\.exe]]
},
m {
target = "Battle.net",
class = [[^Wine$]],
instance = [[Battle.net\.exe]]
},
m {
target = "Hearthstone",
class = [[^Wine$]],
instance = [[Hearthstone\.exe]]
},
m {
target = "Starcraft II",
class = [[^Wine$]],
instance = [[(SC2|Starcraft II|Blizzard Launcher)\.exe]]
}
}
for i, mapping in ipairs(class_mappings) do
target = mapping.target
matched = true
if mapping:check_executable()
and mapping:check_class()
and mapping:check_instance()
then
debug_print(string.format("Matched window %q to target %q",
get_window_name(), target))
os.execute(string.format("xprop -f WM_CLASS 8s -set WM_CLASS %q -id %d",
target, get_window_xid()))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment