mirror of https://github.com/k0kubun/xremap
Complete basic remap implementation
parent
a8de68d52a
commit
adb27362c7
@ -1,16 +1,24 @@
|
||||
module Xkremap
|
||||
class Config
|
||||
# @param [String] dsl
|
||||
def self.load(dsl)
|
||||
# @param [String] filename
|
||||
def self.load(filename)
|
||||
unless File.exist?(filename)
|
||||
raise "Config file does not exist!: #{filename.inspect}"
|
||||
exit 1
|
||||
end
|
||||
|
||||
config = self.new
|
||||
ConfigContext.new(config).instance_eval(dsl)
|
||||
ConfigDSL.new(config).instance_eval(File.read(filename))
|
||||
config
|
||||
end
|
||||
|
||||
attr_reader :remaps
|
||||
|
||||
def initialize
|
||||
@remaps = []
|
||||
end
|
||||
|
||||
attr_reader :remaps
|
||||
Key = Struct.new(:keysym, :modifier)
|
||||
Remap = Struct.new(:from_key, :to_keys)
|
||||
end
|
||||
end
|
||||
|
@ -1,8 +0,0 @@
|
||||
module Xkremap
|
||||
class ConfigContext
|
||||
# @param [Xkremap::Config] config
|
||||
def initialize(config)
|
||||
@config = config
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,22 @@
|
||||
module Xkremap
|
||||
class ConfigDSL
|
||||
# @param [Xkremap::Config] config
|
||||
def initialize(config)
|
||||
@config = config
|
||||
end
|
||||
|
||||
def remap(from_str, options = {})
|
||||
to_strs = Array(options.fetch(:to))
|
||||
@config.remaps << Config::Remap.new(
|
||||
compile_exp(from_str),
|
||||
to_strs.map { |str| compile_exp(str) }
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def compile_exp(str)
|
||||
KeyExpression.compile(str)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,13 +1,18 @@
|
||||
module Xkremap
|
||||
class GrabManager
|
||||
# @param [Xkremap::Config] config
|
||||
# @param [Xkremap::Display] display
|
||||
def initialize(display)
|
||||
def initialize(config, display)
|
||||
@config = config
|
||||
@display = display
|
||||
end
|
||||
|
||||
def grab_keys
|
||||
XlibWrapper.ungrab_keys(@display)
|
||||
XlibWrapper.grab_key(@display, 0x0062, 1<<2) # C-b
|
||||
@config.remaps.each do |remap|
|
||||
from = remap.from_key
|
||||
XlibWrapper.grab_key(@display, from.keysym, from.modifier)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -0,0 +1,40 @@
|
||||
module Xkremap
|
||||
module KeyExpression
|
||||
class << self
|
||||
# @param [String] exp
|
||||
# @return [Xkremap::Config::Key] key
|
||||
def compile(exp)
|
||||
case exp
|
||||
when /\A(?<keyexp>[^-]+)\z/
|
||||
Config::Key.new(to_keysym(Regexp.last_match[:keyexp]), X11::NoModifier)
|
||||
when /\AC-(?<keyexp>[^-]+)\z/
|
||||
Config::Key.new(to_keysym(Regexp.last_match[:keyexp]), X11::ControlMask)
|
||||
when /\A(M|Alt)-(?<keyexp>[^-]+)\z/
|
||||
Config::Key.new(to_keysym(Regexp.last_match[:keyexp]), X11::Mod1Mask)
|
||||
else
|
||||
raise "unexpected key expression pattern!: #{exp.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def to_keysym(keyexp)
|
||||
X11.const_get(x11_const_name(keyexp))
|
||||
end
|
||||
|
||||
def x11_const_name(keyexp)
|
||||
if keyexp.length == 1
|
||||
"XK_#{keyexp.downcase}"
|
||||
else
|
||||
"XK_#{capitalize(keyexp)}"
|
||||
end
|
||||
end
|
||||
|
||||
def capitalize(str)
|
||||
result = str.downcase
|
||||
result[0] = str[0].upcase
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue