Skip to content

Instantly share code, notes, and snippets.

@lucasluitjes
Created September 10, 2014 15:06
Show Gist options
  • Save lucasluitjes/01967792f712dc10f325 to your computer and use it in GitHub Desktop.
Save lucasluitjes/01967792f712dc10f325 to your computer and use it in GitHub Desktop.
class Controller
BANKS = %w{wmii chromium thunderbird misc vim}
DIRECTIONS = [nil, 'left', 'down', 'right', nil, nil, 'up']
def process bank_index, value
bank = BANKS[bank_index]
puts "bank #{bank || bank_index}, value #{value}"
begin
puts send(:"#{bank}_bank", value) || 'command not found'
rescue => e
puts e.inspect
end
end
WMII_TAGS = [nil, nil,nil,nil,1,2,nil,3, 4,5]
def wmii_bank value
if direction = DIRECTIONS[value]
wmiir "/tag/sel/ctl select #{direction}"
elsif tag = WMII_TAGS[value]
wmiir("/ctl view \"#{tag}\"")
else
@wmii_toggle ||= false
@wmii_toggle = !@wmii_toggle
if @wmii_toggle
wmiir('/tag/sel/ctl colmode sel default-max')
else
wmiir('/tag/sel/ctl colmode sel stack-max')
end
end
end
CHROMIUM_KEYS = [nil, nil, 'Return', 'Right', 'Down', 'Page_Down', 'Tab', 'Escape', 'H', 'Up', 'Page_Up']
def chromium_bank value
if @ascii_buffer
@ascii_buffer << (value == 10 ? 0 : value)
if @ascii_buffer.size == 2
index = @ascii_buffer.map(&:to_s).join.to_i
if @first_key_pressed
@ascii_buffer = nil
else
@ascii_buffer = []
@first_key_pressed = true
end
puts index
character = ('a'..'z').to_a[index % 30]
character.upcase! if index > 29
xdo_key character unless index == 99
else
"#{@ascii_buffer.last} added to buffer"
end
else
if value == 1
xdo_key 'f'
@ascii_buffer = []
@first_key_pressed = false
'created buffer'
else key = CHROMIUM_KEYS[value]
xdo_key key
end
end
end
THUNDERBIRD_KEYS = [
nil,
'Return',
'Delete',
'Control_L+F6',
'Down',
'Page_Down',
'Control_L+Tab',
'Control_L+F4',
'Menu+m+r',
'Up',
'Page_Up'
]
def thunderbird_bank value
xdo_key THUNDERBIRD_KEYS[value]
end
def misc_bank value
'TODO: misc bank'
end
VIM_KEYS = [
nil,
'Control_L+h',
'j',
'Control_L+l',
nil,
'Page_Down',
'k',
nil,
'Return',
nil,
'Page_Up'
]
def vim_bank value
if @number
@number = false
xdo_key value.to_s
else
if key = VIM_KEYS[value]
xdo_key key
elsif [4, 7].include? value
xdo_type(value == 4 ? ':vnew .' : ':q')
xdo_key 'Return'
else
@number = true
'number mode on'
end
end
end
def _bank value
'bank not found'
end
private
def wmiir str
`DISPLAY=':0.0' wmiir xwrite #{str}`
"wmiir xwrite #{str}"
end
def xdo_key key
`DISPLAY=':0.0' xdotool key #{key}`
"xdotool key #{key}"
end
def xdo_type str
`DISPLAY=':0.0' xdotool type '#{str}'`
"xdotool type '#{str}'"
end
def xdotool str
`DISPLAY=':0.0' xdotool #{str}`
"xdotool #{str}"
end
end
require 'unimidi'
# patch
module AlsaRawMIDI
class Input
def gets
until queued_messages?
sleep 0.01
end
msgs = queued_messages
@pointer = @buffer.length
msgs
end
end
end
class Reader
def initialize
reload_controller
@input = UniMIDI::Input.gets
start_loop
end
def reload_controller
@mtime = File.mtime('controller.rb')
load('./controller.rb')
@controller = Controller.new
end
def start_loop
loop do
begin
reload_controller unless File.mtime('controller.rb') == @mtime
rescue SyntaxError => e
puts e.inspect
puts e.backtrace
end
data = @input.gets_data
puts "\n" + data.inspect
raw = data[1] || 0
@controller.process(raw / 10, (raw % 10) + 1)
end
end
end
Reader.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment