Skip to content

Commit

Permalink
Add binds
Browse files Browse the repository at this point in the history
  • Loading branch information
labi-le committed Apr 27, 2024
1 parent f826340 commit a4ea952
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
51 changes: 29 additions & 22 deletions ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
)

var _ IPC = (*IPCClient)(nil)

const (
BufSize = 8192
Separator = ">>"
Expand All @@ -20,13 +22,13 @@ type ReceivedData struct {
Data string
}

type ipc struct {
type IPCClient struct {
conn net.Conn
wconn *net.UnixAddr
sign string
}

func NewClient(sign string) IPC {
func NewClient(sign string) *IPCClient {
if sign == "" {
panic("sign is empty")
}
Expand All @@ -36,7 +38,7 @@ func NewClient(sign string) IPC {
panic(err)
}

return &ipc{
return &IPCClient{
conn: conn,
wconn: &net.UnixAddr{
Net: "unix",
Expand All @@ -46,7 +48,7 @@ func NewClient(sign string) IPC {
}
}

func (c *ipc) request(q *ByteQueue) ([]byte, error) {
func (c *IPCClient) request(q *ByteQueue) ([]byte, error) {
if q.Len() == 0 {
return nil, errors.New("wtfuq man you need to pass some args")
}
Expand Down Expand Up @@ -100,7 +102,7 @@ func (c *ipc) request(q *ByteQueue) ([]byte, error) {
// wrapreq
// a command without arguments can be safely wrapped in one method so as not to write the same thing every time
// v is a pointer to a struct
func (c *ipc) wrapreq(command string, v any, q *ByteQueue) error {
func (c *IPCClient) wrapreq(command string, v any, q *ByteQueue) error {
if reflect.ValueOf(v).Kind() != reflect.Ptr {
panic("v must be a pointer to a structure")
}
Expand All @@ -123,7 +125,7 @@ func (c *ipc) wrapreq(command string, v any, q *ByteQueue) error {
return nil
}

func (c *ipc) Receive() ([]ReceivedData, error) {
func (c *IPCClient) Receive() ([]ReceivedData, error) {
buf := make([]byte, BufSize)
n, err := c.conn.Read(buf)
if err != nil {
Expand Down Expand Up @@ -154,48 +156,48 @@ func (c *ipc) Receive() ([]ReceivedData, error) {
return recv, nil
}

func (c *ipc) Dispatch(a *ByteQueue) ([]byte, error) {
func (c *IPCClient) Dispatch(a *ByteQueue) ([]byte, error) {
a.Back([]byte("dispatch"))

return c.request(a)
}

func (c *ipc) Workspaces() ([]Workspace, error) {
func (c *IPCClient) Workspaces() ([]Workspace, error) {
var workspaces []Workspace
return workspaces, c.wrapreq("workspaces", &workspaces, nil)
}

func (c *ipc) ActiveWorkspace() (Workspace, error) {
func (c *IPCClient) ActiveWorkspace() (Workspace, error) {
var workspace Workspace
return workspace, c.wrapreq("activeworkspace", &workspace, nil)
}

func (c *ipc) Monitors() ([]Monitor, error) {
func (c *IPCClient) Monitors() ([]Monitor, error) {
var monitors []Monitor
return monitors, c.wrapreq("monitors", &monitors, nil)
}

func (c *ipc) Clients() ([]Client, error) {
func (c *IPCClient) Clients() ([]Client, error) {
var clients []Client
return clients, c.wrapreq("clients", &clients, nil)
}

func (c *ipc) ActiveWindow() (Window, error) {
func (c *IPCClient) ActiveWindow() (Window, error) {
var window Window
return window, c.wrapreq("activewindow", &window, nil)
}

func (c *ipc) Layers() (Layers, error) {
func (c *IPCClient) Layers() (Layers, error) {
var layers Layers
return layers, c.wrapreq("layers", &layers, nil)
}

func (c *ipc) Devices() (Devices, error) {
func (c *IPCClient) Devices() (Devices, error) {
var devices Devices
return devices, c.wrapreq("devices", &devices, nil)
}

func (c *ipc) Keyword(args *ByteQueue) error {
func (c *IPCClient) Keyword(args *ByteQueue) error {
args.Back([]byte("keyword"))

response, err := c.request(args)
Expand All @@ -210,28 +212,28 @@ func (c *ipc) Keyword(args *ByteQueue) error {
return nil
}

func (c *ipc) Version() (Version, error) {
func (c *IPCClient) Version() (Version, error) {
var version Version
return version, c.wrapreq("version", &version, nil)
}

func (c *ipc) Kill() error {
func (c *IPCClient) Kill() error {
q := NewByteQueue()
q.Add([]byte("kill"))

_, err := c.Dispatch(q)
return err
}

func (c *ipc) Reload() error {
func (c *IPCClient) Reload() error {
q := NewByteQueue()
q.Add([]byte("reload"))

_, err := c.request(q)
return err
}

func (c *ipc) SetCursor(theme, size string) error {
func (c *IPCClient) SetCursor(theme, size string) error {
q := NewByteQueue()
q.Add(UnsafeBytes(theme))
q.Add(UnsafeBytes(size))
Expand All @@ -241,7 +243,7 @@ func (c *ipc) SetCursor(theme, size string) error {
return err
}

func (c *ipc) GetOption(name string) (string, error) {
func (c *IPCClient) GetOption(name string) (string, error) {
q := NewByteQueue()
q.Add(UnsafeBytes(name))
q.Back([]byte("getoption"))
Expand All @@ -254,7 +256,7 @@ func (c *ipc) GetOption(name string) (string, error) {
return string(buf), nil
}

func (c *ipc) Splash() (string, error) {
func (c *IPCClient) Splash() (string, error) {
q := NewByteQueue()
q.Back([]byte("splash"))

Expand All @@ -266,7 +268,12 @@ func (c *ipc) Splash() (string, error) {
return string(buf), nil
}

func (c *ipc) CursorPos() (CursorPos, error) {
func (c *IPCClient) CursorPos() (CursorPos, error) {
var cursorpos CursorPos
return cursorpos, c.wrapreq("cursorpos", &cursorpos, nil)
}

func (c *IPCClient) Binds() ([]Bind, error) {
var binds []Bind
return binds, c.wrapreq("binds", &binds, nil)
}
4 changes: 4 additions & 0 deletions ipc_dummy.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package client

var _ IPC = (*DummyIPC)(nil)

type DummyIPC struct{}

func (d DummyIPC) Receive() ([]ReceivedData, error) {
Expand Down Expand Up @@ -69,3 +71,5 @@ func (d DummyIPC) GetOption(string) (string, error) {
func (d DummyIPC) CursorPos() (CursorPos, error) {
return CursorPos{}, nil
}

func (d DummyIPC) Binds() ([]Bind, error) { return []Bind{}, nil }
20 changes: 18 additions & 2 deletions ipc_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package client

import (
"os"
"reflect"
"testing"
)

var ipctest = NewClient(os.Getenv("HYPRLAND_INSTANCE_SIGNATURE"))
var ipctest = NewClient("fe7b748eb668136dd0558b7c8279bfcd7ab4d759_1714258749")

func Test_ipc_Clients(t *testing.T) {
got, err := ipctest.Clients()
Expand Down Expand Up @@ -188,3 +187,20 @@ func TestIpc_Dispatch(t *testing.T) {
t.Error(err)
}
}

func Test_ipc_Binds(t *testing.T) {
got, err := ipctest.Binds()
if err != nil {
t.Error(err)
}

if len(got) == 0 {
t.Error("got is empty")
}

for _, bind := range got {
if reflect.DeepEqual(bind, Bind{}) {
t.Errorf("got empty struct")
}
}
}
15 changes: 15 additions & 0 deletions ipc_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type IPC interface {
SetCursor(theme, size string) error
GetOption(name string) (string, error)
CursorPos() (CursorPos, error)
Binds() ([]Bind, error)
}

type Workspace struct {
Expand Down Expand Up @@ -132,3 +133,17 @@ type CursorPos struct {
X int `json:"x"`
Y int `json:"y"`
}

type Bind struct {
Locked bool `json:"locked"`
Mouse bool `json:"mouse"`
Release bool `json:"release"`
Repeat bool `json:"repeat"`
NoConsuming bool `json:"non_consuming"`
ModMask int `json:"modmask"`
SubMap string `json:"submap"`
Key string `json:"key"`
KeyCode int `json:"keycode"`
Dispatcher string `json:"dispatcher"`
Arg string `json:"arg"`
}

0 comments on commit a4ea952

Please sign in to comment.