flush scancodes when we wait

This commit is contained in:
Matthew Hooker 2018-04-19 16:44:50 -07:00
parent 11de2b9759
commit 675eae1e92
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
5 changed files with 33 additions and 11 deletions

View File

@ -42,7 +42,8 @@ type expression interface {
type expressionSequence []expression
// Do executes every expression in the sequence and then finalizes the driver.
// Do executes every expression in the sequence and then flushes remaining
// scancodes.
func (s expressionSequence) Do(ctx context.Context, b BCDriver) error {
// validate should never fail here, since it should be called before
// expressionSequence.Do. Only reason we don't panic is so we can clean up.
@ -58,10 +59,10 @@ func (s expressionSequence) Do(ctx context.Context, b BCDriver) error {
return err
}
}
return b.Finalize()
return b.Flush()
}
// Do executes every expression in the sequence and then finalizes the driver.
// Validate tells us if every expression in the sequence is valid.
func (s expressionSequence) Validate() (errs []error) {
for _, exp := range s {
if err := exp.Validate(); err != nil {
@ -91,7 +92,8 @@ type waitExpression struct {
// Do waits the amount of time described by the expression. It is cancellable
// through the context.
func (w *waitExpression) Do(ctx context.Context, _ BCDriver) error {
func (w *waitExpression) Do(ctx context.Context, driver BCDriver) error {
driver.Flush()
log.Printf("[INFO] Waiting %s", w.d)
select {
case <-time.After(w.d):

View File

@ -6,6 +6,6 @@ const shiftedChars = "~!@#$%^&*()_+{}|:\"<>?"
type BCDriver interface {
SendKey(key rune, action KeyAction) error
SendSpecial(special string, action KeyAction) error
// Finalize will be called after every expression has been processed.
Finalize() error
// Flush will be called when we want to send scancodes to the VM.
Flush() error
}

View File

@ -111,8 +111,8 @@ func NewPCXTDriver(send SendCodeFunc, chunkSize int) *pcXTDriver {
}
}
// Finalize flushes all scanecodes.
func (d *pcXTDriver) Finalize() error {
// Flush send all scanecodes.
func (d *pcXTDriver) Flush() error {
defer func() {
d.buffer = nil
}()
@ -162,6 +162,7 @@ func (d *pcXTDriver) SendSpecial(special string, action KeyAction) error {
if !ok {
return fmt.Errorf("special %s not found.", special)
}
log.Printf("Special code '%s' '<%s>' found, replacing with: %v", action.String(), special, keyCode)
switch action {
case KeyOn:
@ -174,7 +175,7 @@ func (d *pcXTDriver) SendSpecial(special string, action KeyAction) error {
return nil
}
// send stores the codes in an internal buffer. Use finalize to flush them.
// send stores the codes in an internal buffer. Use Flush to send them.
func (d *pcXTDriver) send(codes []string) {
d.buffer = append(d.buffer, codes)
}

View File

@ -86,3 +86,22 @@ func Test_pcxtSpecialLookup(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expected, codes)
}
func Test_flushes(t *testing.T) {
in := "abc123<wait>098"
expected := [][]string{
{"1e", "9e", "30", "b0", "2e", "ae", "02", "82", "03", "83", "04", "84"},
{"0b", "8b", "0a", "8a", "09", "89"},
}
var actual [][]string
sendCodes := func(c []string) error {
actual = append(actual, c)
return nil
}
d := NewPCXTDriver(sendCodes, -1)
seq, err := GenerateExpressionSequence(in)
assert.NoError(t, err)
err = seq.Do(context.Background(), d)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}

View File

@ -92,8 +92,8 @@ func (d *vncDriver) keyEvent(k uint32, down bool) error {
return nil
}
// Finalize does nothing here
func (d *vncDriver) Finalize() error {
// Flush does nothing here
func (d *vncDriver) Flush() error {
return nil
}