Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Add Xorg modifiers (#57)
Browse files Browse the repository at this point in the history
* implement additional modifiers to xorg.

* xorg modifiers to API.

* update modifiers api & add ws.

* scroll pos rename to delta and add ctrl key.
  • Loading branch information
m1k1o authored Sep 11, 2023
1 parent a392163 commit 4da7869
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 88 deletions.
27 changes: 6 additions & 21 deletions internal/api/room/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (
)

type KeyboardMapData struct {
Layout string `json:"layout"`
Variant string `json:"variant"`
types.KeyboardMap
}

type KeyboardModifiersData struct {
NumLock *bool `json:"numlock"`
CapsLock *bool `json:"capslock"`
types.KeyboardModifiers
}

func (h *RoomHandler) keyboardMapSet(w http.ResponseWriter, r *http.Request) error {
Expand All @@ -23,11 +21,7 @@ func (h *RoomHandler) keyboardMapSet(w http.ResponseWriter, r *http.Request) err
return err
}

err := h.desktop.SetKeyboardMap(types.KeyboardMap{
Layout: data.Layout,
Variant: data.Variant,
})

err := h.desktop.SetKeyboardMap(data.KeyboardMap)
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
Expand All @@ -37,14 +31,12 @@ func (h *RoomHandler) keyboardMapSet(w http.ResponseWriter, r *http.Request) err

func (h *RoomHandler) keyboardMapGet(w http.ResponseWriter, r *http.Request) error {
data, err := h.desktop.GetKeyboardMap()

if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}

return utils.HttpSuccess(w, KeyboardMapData{
Layout: data.Layout,
Variant: data.Variant,
KeyboardMap: *data,
})
}

Expand All @@ -54,19 +46,12 @@ func (h *RoomHandler) keyboardModifiersSet(w http.ResponseWriter, r *http.Reques
return err
}

h.desktop.SetKeyboardModifiers(types.KeyboardModifiers{
NumLock: data.NumLock,
CapsLock: data.CapsLock,
})

h.desktop.SetKeyboardModifiers(data.KeyboardModifiers)
return utils.HttpSuccess(w)
}

func (h *RoomHandler) keyboardModifiersGet(w http.ResponseWriter, r *http.Request) error {
data := h.desktop.GetKeyboardModifiers()

return utils.HttpSuccess(w, KeyboardModifiersData{
NumLock: data.NumLock,
CapsLock: data.CapsLock,
KeyboardModifiers: h.desktop.GetKeyboardModifiers(),
})
}
48 changes: 40 additions & 8 deletions internal/desktop/xorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func (manager *DesktopManagerCtx) GetCursorPosition() (int, int) {
return xorg.GetCursorPosition()
}

func (manager *DesktopManagerCtx) Scroll(x, y int) {
xorg.Scroll(x, y)
func (manager *DesktopManagerCtx) Scroll(deltaX, deltaY int, controlKey bool) {
xorg.Scroll(deltaX, deltaY, controlKey)
}

func (manager *DesktopManagerCtx) ButtonDown(code uint32) error {
Expand Down Expand Up @@ -140,24 +140,56 @@ func (manager *DesktopManagerCtx) GetKeyboardMap() (*types.KeyboardMap, error) {
}

func (manager *DesktopManagerCtx) SetKeyboardModifiers(mod types.KeyboardModifiers) {
if mod.NumLock != nil {
xorg.SetKeyboardModifier(xorg.KbdModNumLock, *mod.NumLock)
if mod.Shift != nil {
xorg.SetKeyboardModifier(xorg.KbdModShift, *mod.Shift)
}

if mod.CapsLock != nil {
xorg.SetKeyboardModifier(xorg.KbdModCapsLock, *mod.CapsLock)
}

if mod.Control != nil {
xorg.SetKeyboardModifier(xorg.KbdModControl, *mod.Control)
}

if mod.Alt != nil {
xorg.SetKeyboardModifier(xorg.KbdModAlt, *mod.Alt)
}

if mod.NumLock != nil {
xorg.SetKeyboardModifier(xorg.KbdModNumLock, *mod.NumLock)
}

if mod.Meta != nil {
xorg.SetKeyboardModifier(xorg.KbdModMeta, *mod.Meta)
}

if mod.Super != nil {
xorg.SetKeyboardModifier(xorg.KbdModSuper, *mod.Super)
}

if mod.AltGr != nil {
xorg.SetKeyboardModifier(xorg.KbdModAltGr, *mod.AltGr)
}
}

func (manager *DesktopManagerCtx) GetKeyboardModifiers() types.KeyboardModifiers {
modifiers := xorg.GetKeyboardModifiers()

NumLock := (modifiers & xorg.KbdModNumLock) != 0
CapsLock := (modifiers & xorg.KbdModCapsLock) != 0
isset := func(mod xorg.KbdMod) *bool {
x := modifiers&mod != 0
return &x
}

return types.KeyboardModifiers{
NumLock: &NumLock,
CapsLock: &CapsLock,
Shift: isset(xorg.KbdModShift),
CapsLock: isset(xorg.KbdModCapsLock),
Control: isset(xorg.KbdModControl),
Alt: isset(xorg.KbdModAlt),
NumLock: isset(xorg.KbdModNumLock),
Meta: isset(xorg.KbdModMeta),
Super: isset(xorg.KbdModSuper),
AltGr: isset(xorg.KbdModAltGr),
}
}

Expand Down
33 changes: 24 additions & 9 deletions internal/webrtc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,31 @@ func (manager *WebRTCManagerCtx) handle(

switch header.Event {
case payload.OP_SCROLL:
payload := &payload.Scroll{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
// TODO: remove this once the client is fixed
if header.Length == 4 {
payload := &payload.Scroll_Old{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}

manager.desktop.Scroll(int(payload.X), int(payload.Y), false)
logger.Trace().
Int16("x", payload.X).
Int16("y", payload.Y).
Msg("scroll")
} else {
payload := &payload.Scroll{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}

manager.desktop.Scroll(int(payload.DeltaX), int(payload.DeltaY), payload.ControlKey)
logger.Trace().
Int16("deltaX", payload.DeltaX).
Int16("deltaY", payload.DeltaY).
Bool("controlKey", payload.ControlKey).
Msg("scroll")
}

manager.desktop.Scroll(int(payload.X), int(payload.Y))
logger.Trace().
Int16("x", payload.X).
Int16("y", payload.Y).
Msg("scroll")
case payload.OP_KEY_DOWN:
payload := &payload.Key{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
Expand Down
9 changes: 8 additions & 1 deletion internal/webrtc/payload/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ type Move struct {
Y uint16
}

type Scroll struct {
// TODO: remove this once the client is fixed
type Scroll_Old struct {
X int16
Y int16
}

type Scroll struct {
DeltaX int16
DeltaY int16
ControlKey bool
}

type Key struct {
Key uint32
}
Expand Down
10 changes: 8 additions & 2 deletions internal/websocket/handler/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@ func (h *MessageHandlerCtx) controlMove(session types.Session, payload *message.
return nil
}

func (h *MessageHandlerCtx) controlScroll(session types.Session, payload *message.ControlPos) error {
func (h *MessageHandlerCtx) controlScroll(session types.Session, payload *message.ControlScroll) error {
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}

h.desktop.Scroll(payload.X, payload.Y)
// TOOD: remove this once the client is fixed
if payload.DeltaX == 0 && payload.DeltaY == 0 {
payload.DeltaX = payload.X
payload.DeltaY = payload.Y
}

h.desktop.Scroll(payload.DeltaX, payload.DeltaY, payload.ControlKey)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/websocket/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketM
return h.controlMove(session, payload)
})
case event.CONTROL_SCROLL:
payload := &message.ControlPos{}
payload := &message.ControlScroll{}
err = utils.Unmarshal(payload, data.Payload, func() error {
return h.controlScroll(session, payload)
})
Expand Down
11 changes: 2 additions & 9 deletions internal/websocket/handler/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ func (h *MessageHandlerCtx) keyboardMap(session types.Session, payload *message.
return errors.New("is not the host")
}

return h.desktop.SetKeyboardMap(types.KeyboardMap{
Layout: payload.Layout,
Variant: payload.Variant,
})
return h.desktop.SetKeyboardMap(payload.KeyboardMap)
}

func (h *MessageHandlerCtx) keyboardModifiers(session types.Session, payload *message.KeyboardModifiers) error {
if !session.IsHost() {
return errors.New("is not the host")
}

h.desktop.SetKeyboardModifiers(types.KeyboardModifiers{
NumLock: payload.NumLock,
CapsLock: payload.CapsLock,
})

h.desktop.SetKeyboardModifiers(payload.KeyboardModifiers)
return nil
}
14 changes: 13 additions & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,22 @@ components:
KeyboardModifiers:
type: object
properties:
numlock:
shift:
type: boolean
capslock:
type: boolean
control:
type: boolean
alt:
type: boolean
numlock:
type: boolean
meta:
type: boolean
super:
type: boolean
altgr:
type: boolean

ControlStatus:
type: object
Expand Down
16 changes: 11 additions & 5 deletions pkg/types/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ func (s ScreenSize) String() string {
}

type KeyboardModifiers struct {
NumLock *bool
CapsLock *bool
Shift *bool `json:"shift"`
CapsLock *bool `json:"capslock"`
Control *bool `json:"control"`
Alt *bool `json:"alt"`
NumLock *bool `json:"numlock"`
Meta *bool `json:"meta"`
Super *bool `json:"super"`
AltGr *bool `json:"altgr"`
}

type KeyboardMap struct {
Layout string
Variant string
Layout string `json:"layout"`
Variant string `json:"variant"`
}

type ClipboardText struct {
Expand All @@ -48,7 +54,7 @@ type DesktopManager interface {
// xorg
Move(x, y int)
GetCursorPosition() (int, int)
Scroll(x, y int)
Scroll(deltaX, deltaY int, controlKey bool)
ButtonDown(code uint32) error
KeyDown(code uint32) error
ButtonUp(code uint32) error
Expand Down
16 changes: 12 additions & 4 deletions pkg/types/message/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ type ControlHost struct {
HostID string `json:"host_id,omitempty"`
}

type ControlScroll struct {
// TOOD: remove this once the client is fixed
X int `json:"x"`
Y int `json:"y"`

DeltaX int `json:"delta_x"`
DeltaY int `json:"delta_y"`
ControlKey bool `json:"control_key"`
}

type ControlPos struct {
X int `json:"x"`
Y int `json:"y"`
Expand Down Expand Up @@ -159,13 +169,11 @@ type ClipboardData struct {
/////////////////////////////

type KeyboardMap struct {
Layout string `json:"layout"`
Variant string `json:"variant"`
types.KeyboardMap
}

type KeyboardModifiers struct {
CapsLock *bool `json:"capslock"`
NumLock *bool `json:"numlock"`
types.KeyboardModifiers
}

/////////////////////////////
Expand Down
Loading

0 comments on commit 4da7869

Please sign in to comment.