2018-04-11 02:05:46 -04:00
|
|
|
package bootcommand
|
|
|
|
|
|
|
|
import (
|
2018-04-12 17:42:53 -04:00
|
|
|
"fmt"
|
2018-04-11 02:05:46 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const KeyLeftShift uint32 = 0xFFE1
|
|
|
|
|
2018-04-13 17:41:17 -04:00
|
|
|
type VNCKeyEvent interface {
|
|
|
|
KeyEvent(uint32, bool) error
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:00:09 -04:00
|
|
|
type vncDriver struct {
|
2018-04-13 17:41:17 -04:00
|
|
|
c VNCKeyEvent
|
2018-04-12 18:59:07 -04:00
|
|
|
interval time.Duration
|
|
|
|
specialMap map[string]uint32
|
|
|
|
// keyEvent can set this error which will prevent it from continuing
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-08-23 16:16:01 -04:00
|
|
|
func NewVNCDriver(c VNCKeyEvent, interval time.Duration) *vncDriver {
|
2018-04-11 02:05:46 -04:00
|
|
|
// We delay (default 100ms) between each key event to allow for CPU or
|
|
|
|
// network latency. See PackerKeyEnv for tuning.
|
|
|
|
keyInterval := common.PackerKeyDefault
|
|
|
|
if delay, err := time.ParseDuration(os.Getenv(common.PackerKeyEnv)); err == nil {
|
|
|
|
keyInterval = delay
|
|
|
|
}
|
2018-08-22 14:25:43 -04:00
|
|
|
// override interval based on builder-specific override.
|
2018-08-23 16:16:01 -04:00
|
|
|
if interval > time.Duration(0) {
|
|
|
|
keyInterval = interval
|
2018-08-22 14:25:43 -04:00
|
|
|
}
|
2018-04-11 02:05:46 -04:00
|
|
|
|
|
|
|
// Scancodes reference: https://github.com/qemu/qemu/blob/master/ui/vnc_keysym.h
|
|
|
|
sMap := make(map[string]uint32)
|
|
|
|
sMap["bs"] = 0xFF08
|
|
|
|
sMap["del"] = 0xFFFF
|
2018-04-18 19:54:48 -04:00
|
|
|
sMap["down"] = 0xFF54
|
|
|
|
sMap["end"] = 0xFF57
|
2018-04-11 02:05:46 -04:00
|
|
|
sMap["enter"] = 0xFF0D
|
|
|
|
sMap["esc"] = 0xFF1B
|
|
|
|
sMap["f1"] = 0xFFBE
|
|
|
|
sMap["f2"] = 0xFFBF
|
|
|
|
sMap["f3"] = 0xFFC0
|
|
|
|
sMap["f4"] = 0xFFC1
|
|
|
|
sMap["f5"] = 0xFFC2
|
|
|
|
sMap["f6"] = 0xFFC3
|
|
|
|
sMap["f7"] = 0xFFC4
|
|
|
|
sMap["f8"] = 0xFFC5
|
|
|
|
sMap["f9"] = 0xFFC6
|
|
|
|
sMap["f10"] = 0xFFC7
|
|
|
|
sMap["f11"] = 0xFFC8
|
|
|
|
sMap["f12"] = 0xFFC9
|
|
|
|
sMap["home"] = 0xFF50
|
2018-04-18 19:54:48 -04:00
|
|
|
sMap["insert"] = 0xFF63
|
|
|
|
sMap["left"] = 0xFF51
|
2018-04-13 17:41:17 -04:00
|
|
|
sMap["leftalt"] = 0xFFE9
|
|
|
|
sMap["leftctrl"] = 0xFFE3
|
|
|
|
sMap["leftshift"] = 0xFFE1
|
|
|
|
sMap["leftsuper"] = 0xFFEB
|
2018-04-18 19:55:32 -04:00
|
|
|
sMap["menu"] = 0xFF67
|
2018-04-18 19:54:48 -04:00
|
|
|
sMap["pagedown"] = 0xFF56
|
|
|
|
sMap["pageup"] = 0xFF55
|
|
|
|
sMap["return"] = 0xFF0D
|
|
|
|
sMap["right"] = 0xFF53
|
2018-04-13 17:41:17 -04:00
|
|
|
sMap["rightalt"] = 0xFFEA
|
|
|
|
sMap["rightctrl"] = 0xFFE4
|
|
|
|
sMap["rightshift"] = 0xFFE2
|
|
|
|
sMap["rightsuper"] = 0xFFEC
|
2018-04-18 19:54:48 -04:00
|
|
|
sMap["spacebar"] = 0x020
|
|
|
|
sMap["tab"] = 0xFF09
|
|
|
|
sMap["up"] = 0xFF52
|
2018-04-11 02:05:46 -04:00
|
|
|
|
2018-04-12 20:00:09 -04:00
|
|
|
return &vncDriver{
|
2018-04-11 02:05:46 -04:00
|
|
|
c: c,
|
|
|
|
interval: keyInterval,
|
|
|
|
specialMap: sMap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:00:09 -04:00
|
|
|
func (d *vncDriver) keyEvent(k uint32, down bool) error {
|
2018-04-11 02:05:46 -04:00
|
|
|
if d.err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := d.c.KeyEvent(k, down); err != nil {
|
|
|
|
d.err = err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(d.interval)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:44:50 -04:00
|
|
|
// Flush does nothing here
|
|
|
|
func (d *vncDriver) Flush() error {
|
2018-04-12 20:00:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *vncDriver) SendKey(key rune, action KeyAction) error {
|
2018-04-11 02:05:46 -04:00
|
|
|
keyShift := unicode.IsUpper(key) || strings.ContainsRune(shiftedChars, key)
|
|
|
|
keyCode := uint32(key)
|
|
|
|
log.Printf("Sending char '%c', code 0x%X, shift %v", key, keyCode, keyShift)
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case KeyOn:
|
|
|
|
if keyShift {
|
|
|
|
d.keyEvent(KeyLeftShift, true)
|
|
|
|
}
|
|
|
|
d.keyEvent(keyCode, true)
|
|
|
|
case KeyOff:
|
|
|
|
if keyShift {
|
|
|
|
d.keyEvent(KeyLeftShift, false)
|
|
|
|
}
|
|
|
|
d.keyEvent(keyCode, false)
|
|
|
|
case KeyPress:
|
|
|
|
if keyShift {
|
|
|
|
d.keyEvent(KeyLeftShift, true)
|
|
|
|
}
|
|
|
|
d.keyEvent(keyCode, true)
|
|
|
|
d.keyEvent(keyCode, false)
|
|
|
|
if keyShift {
|
|
|
|
d.keyEvent(KeyLeftShift, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.err
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:00:09 -04:00
|
|
|
func (d *vncDriver) SendSpecial(special string, action KeyAction) error {
|
2018-04-12 17:42:53 -04:00
|
|
|
keyCode, ok := d.specialMap[special]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("special %s not found.", special)
|
|
|
|
}
|
2018-04-11 02:05:46 -04:00
|
|
|
log.Printf("Special code '<%s>' found, replacing with: 0x%X", special, keyCode)
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case KeyOn:
|
|
|
|
d.keyEvent(keyCode, true)
|
|
|
|
case KeyOff:
|
|
|
|
d.keyEvent(keyCode, false)
|
|
|
|
case KeyPress:
|
|
|
|
d.keyEvent(keyCode, true)
|
|
|
|
d.keyEvent(keyCode, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.err
|
|
|
|
}
|