-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwatch_tablet
executable file
·53 lines (46 loc) · 1.07 KB
/
watch_tablet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
require 'yaml'
def die(msg); puts msg; exit 1; end
def load_config!
@config = YAML.load(File.read("#{ENV['HOME']}/.config/watch_tablet.yml"))
unless @config['input_device']
die "Please specify input_device in the config file"
end
end
def config
load_config! unless @config
@config
end
def activate_mode(mode)
puts "Switching to #{mode} mode"
if cmds = config['modes'][mode]
cmds.each{|cmd| system cmd }
end
end
def run_watcher
in_tablet_mode = 0
cmd = "stdbuf -oL -eL libinput debug-events --device #{config['input_device']}"
io = IO.popen(cmd,"r")
while s=io.gets
if m=s.match(/switch tablet-mode state (\d+)/)
case d=m[1].to_i
when 0 then activate_mode "laptop"
when 1 then activate_mode "tablet"
end
else
if m=s.match(/pressed/)
case in_tablet_mode
when 1
activate_mode "laptop"
in_tablet_mode = 0
when 0
activate_mode "tablet"
in_tablet_mode = 1
end
end
end
end
rescue Interrupt
io.close
end
run_watcher