2016-07-05 00:20:17 +02:00
package main
import (
2016-07-12 22:52:48 +02:00
"log"
"os"
"path"
2016-07-05 00:20:17 +02:00
2016-07-12 22:52:48 +02:00
"github.com/fsnotify/fsnotify"
2016-07-05 00:20:17 +02:00
)
2016-07-12 23:38:55 +02:00
func stop ( ) {
log . Println ( "Have a nice day." )
// TODO: Make the error code optional (as a parameter)
os . Exit ( 1 )
}
2016-07-12 23:48:01 +02:00
func waitAndExecuteCommandsFromDevice ( ) {
// TODO: Establish connection to the device
// TODO: Wait for action loop
// TODO: Execute action
// TODO: Wait for action loop ...
}
2016-07-05 00:20:17 +02:00
func watchForConfigChanges ( ) {
2016-07-12 22:52:48 +02:00
watcher , err := fsnotify . NewWatcher ( )
if err != nil {
log . Fatal ( err )
}
defer watcher . Close ( )
2016-07-05 00:20:17 +02:00
2016-07-12 22:52:48 +02:00
done := make ( chan bool )
go func ( ) {
for {
select {
case event := <- watcher . Events :
2016-07-12 23:38:55 +02:00
// The file is changed. This is the only event we are interested
// in. If the file is renamed, removed or something else, we drop
// an error to the user.
2016-07-12 22:52:48 +02:00
if event . Op & fsnotify . Write == fsnotify . Write {
2016-07-12 23:38:55 +02:00
log . Println ( "Reload the config file ..." )
2016-07-12 23:48:01 +02:00
// TODO: Parse the config file
// TODO: Handle errors in the config file
// TODO: Write a mapping for the mapping of the keys to the binary format the avr wants.
// TODO: Establish a connection to the device
// TODO: Transfer the new key mappings
// TODO: Close the connection?
2016-07-12 23:38:55 +02:00
}
if event . Op & fsnotify . Rename == fsnotify . Rename ||
event . Op & fsnotify . Remove == fsnotify . Remove {
log . Println ( "The 'keymap.conf' configuration file is renamed or removed. We cant do any further changes without the config file. So create the file or rename it back and restart the agent." )
stop ( )
2016-07-12 22:52:48 +02:00
}
case err := <- watcher . Errors :
2016-07-12 23:38:55 +02:00
log . Println ( "We have the following problem with the 'keymap.conf' configuration file: " , err )
log . Println ( "Try the fix this problem by yourself and restart the agent." )
stop ( )
2016-07-12 22:52:48 +02:00
}
}
} ( )
2016-07-05 00:20:17 +02:00
2016-07-12 22:52:48 +02:00
// The configuration file need to be in the same directory as the binary.
// Later on, we can add other paths, where the file can be placed (like as a
// dotfile in the home directory or in the .config/n3rdpad/ folder or other
// places where windows will put it).
config_path , err := os . Getwd ( )
config_file := path . Join ( config_path , "keymap.conf" )
err = watcher . Add ( config_file )
if err != nil {
log . Fatal ( "Can't find the 'keymap.conf' configuration file. Please create one." )
}
<- done
2016-07-05 00:20:17 +02:00
}
func main ( ) {
2016-07-12 22:52:48 +02:00
watchForConfigChanges ( )
2016-07-12 23:48:01 +02:00
waitAndExecuteCommandsFromDevice ( )
2016-07-12 23:38:55 +02:00
stop ( )
2016-07-05 00:20:17 +02:00
}