Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions server/cmd/api/api/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ func isValidationErr(err error) bool {
return errors.As(err, &ve)
}

func normalizeXdotoolKeySequence(sequence string) string {
parts := strings.Split(sequence, "+")
for i, part := range parts {
part = strings.TrimSpace(part)
if part == "-" {
part = "minus"
}
parts[i] = part
}
return strings.Join(parts, "+")
}

func xdotoolKeyError(message string, output []byte) error {
msg := fmt.Sprintf("%s. out=%s", message, string(output))
if strings.Contains(string(output), "Invalid key sequence") ||
strings.Contains(string(output), "Failure converting key sequence") {
return &validationError{msg: msg}
}
return &executionError{msg: msg}
}

func (s *ApiService) doMoveMouse(ctx context.Context, body oapi.MoveMouseRequest) error {
log := logger.FromContext(ctx)

Expand Down Expand Up @@ -839,6 +860,19 @@ func (s *ApiService) doPressKey(ctx context.Context, body oapi.PressKeyRequest)
return &validationError{msg: "duration must be >= 0 milliseconds"}
}

keys := make([]string, len(body.Keys))
for i, key := range body.Keys {
keys[i] = normalizeXdotoolKeySequence(key)
}
body.Keys = keys
if body.HoldKeys != nil {
holdKeys := make([]string, len(*body.HoldKeys))
for i, key := range *body.HoldKeys {
holdKeys[i] = normalizeXdotoolKeySequence(key)
}
body.HoldKeys = &holdKeys
}

// If duration is provided and >0, hold all keys down, sleep, then release.
if body.Duration != nil && *body.Duration > 0 {
argsDown := []string{}
Expand All @@ -864,7 +898,7 @@ func (s *ApiService) doPressKey(ctx context.Context, body oapi.PressKeyRequest)
}
}
_, _ = defaultXdoTool.Run(ctx, argsUp...)
return &executionError{msg: fmt.Sprintf("failed to press keys (keydown). out=%s", string(output))}
return xdotoolKeyError("failed to press keys (keydown)", output)
}

d := time.Duration(*body.Duration) * time.Millisecond
Expand Down Expand Up @@ -916,7 +950,7 @@ func (s *ApiService) doPressKey(ctx context.Context, body oapi.PressKeyRequest)
output, err := defaultXdoTool.Run(ctx, args...)
if err != nil {
log.Error("xdotool command failed", "err", err, "output", string(output))
return &executionError{msg: fmt.Sprintf("failed to press keys. out=%s", string(output))}
return xdotoolKeyError("failed to press keys", output)
}
return nil
}
Expand Down
33 changes: 33 additions & 0 deletions server/cmd/api/api/computer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,39 @@ func TestIsValidationErr_Nil(t *testing.T) {
assert.False(t, isValidationErr(nil))
}

func TestNormalizeXdotoolKeySequence(t *testing.T) {
tests := map[string]string{
"ctrl+-": "ctrl+minus",
"Ctrl + -": "Ctrl+minus",
"minus": "minus",
"Ctrl+Shift+Tab": "Ctrl+Shift+Tab",
}
for input, expected := range tests {
t.Run(input, func(t *testing.T) {
assert.Equal(t, expected, normalizeXdotoolKeySequence(input))
})
}
}

func TestXdotoolKeyError(t *testing.T) {
tests := []struct {
name string
output string
validation bool
}{
{name: "invalid sequence", output: "Error: Invalid key sequence 'ctrl+wat'", validation: true},
{name: "conversion failure", output: "Failure converting key sequence 'ctrl+wat' to keycodes", validation: true},
{name: "execution failure", output: "xdo_send_keysequence_window reported an error", validation: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := xdotoolKeyError("failed to press keys", []byte(tt.output))
assert.Equal(t, tt.validation, isValidationErr(err))
assert.Contains(t, err.Error(), tt.output)
})
}
}

func TestGaussianDelay(t *testing.T) {
const n = 10000
meanMs := 10
Expand Down
2 changes: 2 additions & 0 deletions server/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6613,6 +6613,8 @@ components:
List of key symbols to press. Each item should be a key symbol supported by xdotool
(see X11 keysym definitions). Examples include "Return", "Shift", "Ctrl", "Alt", "F5".
Items in this list could also be combinations, e.g. "Ctrl+t" or "Ctrl+Shift+Tab".
Use X11 names for punctuation in combinations, such as "Ctrl+minus" or "Ctrl+plus".
A literal hyphen is also accepted as an alias, so "Ctrl+-" is normalized to "Ctrl+minus".
items:
type: string
duration:
Expand Down
Loading