From 726c07b87a141ba39b8f4f16391ea13d7c52f3f4 Mon Sep 17 00:00:00 2001 From: milarin Date: Fri, 5 Jan 2024 18:42:16 +0100 Subject: [PATCH] refactor --- get_clients.go | 30 ++++++++++++++++++++++++++++++ get_workspaces.go | 30 ++++++++++++++++++++++++++++++ monitor.go | 40 ++++++++++++++++++++-------------------- window.go | 32 ++++++++++++++++++++++++++++++++ workspace.go | 5 +++++ 5 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 get_clients.go create mode 100644 get_workspaces.go create mode 100644 window.go diff --git a/get_clients.go b/get_clients.go new file mode 100644 index 0000000..d26cd12 --- /dev/null +++ b/get_clients.go @@ -0,0 +1,30 @@ +package hypr + +import ( + "encoding/json" + "os/exec" +) + +func GetWindows() ([]*Window, error) { + cmd := exec.Command("hyprctl", "-j", "clients") + + stdout, err := cmd.StdoutPipe() + if err != nil { + return nil, err + } + + if err := cmd.Start(); err != nil { + return nil, err + } + + windows := make([]*Window, 0, 5) + if err := json.NewDecoder(stdout).Decode(&windows); err != nil { + return nil, err + } + + if err := cmd.Wait(); err != nil { + return nil, err + } + + return windows, nil +} diff --git a/get_workspaces.go b/get_workspaces.go new file mode 100644 index 0000000..00be2dd --- /dev/null +++ b/get_workspaces.go @@ -0,0 +1,30 @@ +package hypr + +import ( + "encoding/json" + "os/exec" +) + +func GetWorkspaces() ([]*Workspace, error) { + cmd := exec.Command("hyprctl", "-j", "workspaces") + + stdout, err := cmd.StdoutPipe() + if err != nil { + return nil, err + } + + if err := cmd.Start(); err != nil { + return nil, err + } + + workspaces := make([]*Workspace, 0, 5) + if err := json.NewDecoder(stdout).Decode(&workspaces); err != nil { + return nil, err + } + + if err := cmd.Wait(); err != nil { + return nil, err + } + + return workspaces, nil +} diff --git a/monitor.go b/monitor.go index d9d645f..af4aacf 100644 --- a/monitor.go +++ b/monitor.go @@ -3,26 +3,26 @@ package hypr import "encoding/json" type Monitor struct { - ID int `json:"id"` - Name string `json:"name"` - Description string `json:"description"` - Make string `json:"make"` - Model string `json:"model"` - Serial string `json:"serial"` - Width int `json:"width"` - Height int `json:"height"` - RefreshRate float64 `json:"refreshRate"` - X int `json:"x"` - Y int `json:"y"` - ActiveWorkspace *Workspace `json:"activeWorkspace"` - SpecialWorkspace *Workspace `json:"specialWorkspace"` - Reserved [4]int `json:"reserved"` - Scale float64 `json:"scale"` - Transform int `json:"transform"` - Focused bool `json:"focused"` - DPMSStatus bool `json:"dpmsStatus"` - VRR bool `json:"vrr"` - ActivelyTearing bool `json:"activelyTearing"` + ID int `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Make string `json:"make"` + Model string `json:"model"` + Serial string `json:"serial"` + Width int `json:"width"` + Height int `json:"height"` + RefreshRate float64 `json:"refreshRate"` + X int `json:"x"` + Y int `json:"y"` + ActiveWorkspace WorkspaceIdent `json:"activeWorkspace"` + SpecialWorkspace WorkspaceIdent `json:"specialWorkspace"` + Reserved [4]int `json:"reserved"` + Scale float64 `json:"scale"` + Transform int `json:"transform"` + Focused bool `json:"focused"` + DPMSStatus bool `json:"dpmsStatus"` + VRR bool `json:"vrr"` + ActivelyTearing bool `json:"activelyTearing"` } func (m Monitor) String() string { diff --git a/window.go b/window.go new file mode 100644 index 0000000..53d8ce9 --- /dev/null +++ b/window.go @@ -0,0 +1,32 @@ +package hypr + +import "encoding/json" + +type Window struct { + Address string `json:"address"` + Mapped bool `json:"mapped"` + Hidden bool `json:"hidden"` + At [2]int `json:"at"` + Size [2]int `json:"size"` + Workspace WorkspaceIdent `json:"workspace"` + Floating bool `json:"floating"` + MonitorID int `json:"monitor"` + Class string `json:"class"` + Title string `json:"title"` + InitialClass string `json:"initialClass"` + InitialTitle string `json:"initialTitle"` + PID int `json:"pid"` + Xwayland bool `json:"xwayland"` + Pinned bool `json:"pinned"` + Fullscreen bool `json:"fullscreen"` + FullscreenMode int `json:"fullscreenMode"` + FakeFullscreen bool `json:"fakeFullscreen"` + Grouped []interface{} `json:"grouped"` // TODO + Swallowing string `json:"swallowing"` + FocusHistoryID int `json:"focusHistoryID"` +} + +func (w Window) String() string { + data, _ := json.MarshalIndent(w, "", "\t") + return string(data) +} diff --git a/workspace.go b/workspace.go index 25c1f7b..1990478 100644 --- a/workspace.go +++ b/workspace.go @@ -2,6 +2,11 @@ package hypr import "encoding/json" +type WorkspaceIdent struct { + ID int `json:"id"` + Name string `json:"name"` +} + type Workspace struct { ID int `json:"id"` Name string `json:"name"`