package rex import ( "context" "strings" ) // KeyEvent is a key press or release, as client.key and block.event take // it. The server encodes it for the program, using its own terminal's modes. type KeyEvent struct { // Action is press, release, or repeat. Action string `json:"action"` // Key is the physical key, named after its W3C KeyboardEvent code in // snake_case, such as key_a or arrow_up, or unidentified. Key string `json:"key"` // Mods are the modifiers held, as Mods returns them. Mods string `json:"mods"` // ConsumedMods are the modifiers the keyboard layout used to produce // Text, such as shift for a capital letter. ConsumedMods string `json:"consumed_mods,omitempty"` // Text is the text the key typed; empty for keys like arrows. Text string `json:"text,omitempty"` // UnshiftedCodepoint is what the key types without shift, such as 'n', // or 0. Key bindings such as ctrl+n match on it. UnshiftedCodepoint rune `json:"unshifted_codepoint,omitempty"` // Composing is true while the key is part of an unfinished composition. Composing bool `json:"composing,omitempty"` } // Modifier names, in the order Mods joins them. const ( ModShift = "shift" ModCtrl = "ctrl" ModAlt = "alt" ModSuper = "super" ) // Mods joins modifier names the way the server reads them: with +, in the // order shift, ctrl, alt, super. func Mods(shift, ctrl, alt, super bool) string { var names []string for _, m := range []struct { on bool name string }{{shift, ModShift}, {ctrl, ModCtrl}, {alt, ModAlt}, {super, ModSuper}} { if m.on { names = append(names, m.name) } } return strings.Join(names, "+") } // Key sends a key to a block, giving Lua key bindings the first chance at it. // For the last key of a sequence, prefix holds the keys before it, as the // binding spells them. It reports whether a binding took the key. Keys sent // on one connection reach the block in order. func (c *Conn) Key(ctx context.Context, sessionID, blockID string, k KeyEvent, prefix ...string) (handled bool, err error) { var result struct { Handled bool `json:"handled"` } args := map[string]any{ "session_id": sessionID, "block_id": blockID, "key": k, } if len(prefix) > 0 { args["prefix"] = prefix } _, err = c.Call(ctx, "client.key", args, &result) return result.Handled, err } // SendKey sends a key straight to a block, as if it had been pressed there. // Lua key bindings never see it. func (c *Conn) SendKey(ctx context.Context, sessionID, blockID string, k KeyEvent) error { return c.blockEvent(ctx, sessionID, blockID, "key", k) } // Paste sends pasted text to a block. func (c *Conn) Paste(ctx context.Context, sessionID, blockID, text string) error { return c.blockEvent(ctx, sessionID, blockID, "paste", map[string]string{"text": text}) } // Wheel sends one wheel turn to a block, at a cell: dy is -1 for up and 1 // for down. func (c *Conn) Wheel(ctx context.Context, sessionID, blockID string, col, row uint16, dy int) error { return c.blockEvent(ctx, sessionID, blockID, "wheel", map[string]any{"x": col, "y": row, "delta_x": 0, "delta_y": dy}) } // MouseEvent is a mouse button press or release, or motion, as a // block.event of type mouse takes it. The server encodes it for the // program, in whatever mouse mode the program asked for, or drops it if // the program doesn't want it. type MouseEvent struct { // Action is press, release, or motion. Action string `json:"action"` // Button is the button pressed or released: left, right, middle, or // an extra button spelled as a word, such as four. It is unknown for // motion. Button string `json:"button"` // Buttons are the buttons held after the event. Motion with one held // is a drag. Buttons map[string]bool `json:"buttons"` // X and Y are the cell, counting from 0 at the top left. X uint16 `json:"x"` Y uint16 `json:"y"` // Mods are the modifiers held, as Mods returns them. Mods string `json:"mods,omitempty"` } // Mouse sends a mouse event to a block. func (c *Conn) Mouse(ctx context.Context, sessionID, blockID string, e MouseEvent) error { return c.blockEvent(ctx, sessionID, blockID, "mouse", e) } // Focus tells a block whether this client's view of it has focus. func (c *Conn) Focus(ctx context.Context, sessionID, blockID string, in bool) error { action := "out" if in { action = "in" } return c.blockEvent(ctx, sessionID, blockID, "focus", map[string]string{"action": action}) } func (c *Conn) blockEvent(ctx context.Context, sessionID, blockID, typ string, payload any) error { _, err := c.Call(ctx, "block.event", map[string]any{ "session_id": sessionID, "block_id": blockID, "event": map[string]any{"type": typ, "payload": payload}, }, nil) return err } // Owner is a block and the client that owns it, or "" if none does. type Owner struct { BlockID string `json:"block_id"` Owner string `json:"owner"` } // OwnBlocks claims blocks for this client. Mode "take" makes it the owner at // once; "request" makes it the owner only of blocks with none, and puts it in // line for the rest. It returns each block's owner afterwards. func (c *Conn) OwnBlocks(ctx context.Context, sessionID, mode string, blockIDs ...string) ([]Owner, error) { var result struct { Blocks []Owner `json:"blocks"` } _, err := c.Call(ctx, "session.own_block", map[string]any{ "session_id": sessionID, "block_ids": blockIDs, "mode": mode, }, &result) return result.Blocks, err } // ResizeTerminal asks for a terminal size. It applies at once if this client // owns the block or no client does, and otherwise when this client becomes // the owner. Cell sizes of 0 keep the current cell size. func (c *Conn) ResizeTerminal(ctx context.Context, sessionID, blockID string, cols, rows uint16, cellWidthPx, cellHeightPx int) error { args := map[string]any{"columns": cols, "rows": rows} if cellWidthPx > 0 && cellHeightPx > 0 { args["cell_width_px"], args["cell_height_px"] = cellWidthPx, cellHeightPx } return c.BlockMethod(ctx, sessionID, blockID, "com.superlogical.terminal.resize", args, nil) } // WriteTerminal types text into a terminal as if the user had typed it. func (c *Conn) WriteTerminal(ctx context.Context, sessionID, blockID, text string) error { return c.BlockMethod(ctx, sessionID, blockID, "com.superlogical.terminal.write", map[string]string{"data": text}, nil) } // BlockMethod calls a method that a block provides, such as // com.superlogical.terminal.resize, with the method's own arguments in args. func (c *Conn) BlockMethod(ctx context.Context, sessionID, blockID, method string, args, result any) error { payload := map[string]any{"session_id": sessionID, "block_id": blockID} if args != nil { payload["args"] = args } _, err := c.Call(ctx, method, payload, result) return err } // TerminalTheme is a terminal's default colours, as RRGGBB, with palette // entries written index=RRGGBB. Scheme is light or dark, or "" to decide from // the background. type TerminalTheme struct { Foreground string `json:"foreground,omitempty"` Background string `json:"background,omitempty"` Cursor string `json:"cursor,omitempty"` Palette []string `json:"palette,omitempty"` Scheme string `json:"scheme,omitempty"` } // SetTerminalTheme replaces a terminal's default colours. Like a resize, it // applies at once if this client owns the block or no client does, and // otherwise when this client becomes the owner. func (c *Conn) SetTerminalTheme(ctx context.Context, sessionID, blockID string, t TerminalTheme) error { return c.BlockMethod(ctx, sessionID, blockID, "com.superlogical.terminal.set_theme", t, nil) }