mirror of
https://codeberg.org/scip/swayipc.git
synced 2025-12-16 12:10:57 +01:00
116 lines
2.5 KiB
Go
116 lines
2.5 KiB
Go
/*
|
|
Copyright © 2025 Thomas von Dein
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package i3ipc
|
|
|
|
import (
|
|
"net"
|
|
)
|
|
|
|
const (
|
|
VERSION = "v0.1.0"
|
|
|
|
IPC_HEADER_SIZE = 14
|
|
IPC_MAGIC = "i3-ipc"
|
|
IPC_MAGIC_LEN = 6
|
|
)
|
|
|
|
const (
|
|
// message types
|
|
RUN_COMMAND = iota
|
|
GET_WORKSPACES
|
|
SUBSCRIBE
|
|
GET_OUTPUTS
|
|
GET_TREE
|
|
GET_MARKS
|
|
GET_BAR_CONFIG
|
|
GET_VERSION
|
|
GET_BINDING_MODES
|
|
GET_CONFIG
|
|
SEND_TICK
|
|
SYNC
|
|
GET_BINDING_STATE
|
|
|
|
GET_INPUTS = 100
|
|
GET_SEATS = 101
|
|
)
|
|
|
|
// This is the primary struct to work with the i3ipc module.
|
|
type I3ipc struct {
|
|
socket net.Conn
|
|
SocketFile string // filename of the i3 IPC socket
|
|
Events *Event // store subscribed events, see i3ipc.Subscribe()
|
|
}
|
|
|
|
// A rectangle struct, used at various places for geometry etc.
|
|
type Rect struct {
|
|
X int `json:"x"` // X coordinate
|
|
Y int `json:"y"` // Y coordinate
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
}
|
|
|
|
// Stores responses retrieved via ipc
|
|
type Response struct {
|
|
Success bool `json:"success"`
|
|
ParseError bool `json:"parse_error"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
// Stores the user config for the WM
|
|
type Config struct {
|
|
Config string `json:"config"`
|
|
}
|
|
|
|
// Stores the binding state
|
|
type State struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// Create a new i3ipc.I3ipc object. Filename argument is optional and
|
|
// may denote a filename or the name of an environment variable.
|
|
//
|
|
// By default and if nothing is specified we look for the environment
|
|
// variable SWAYSOCK and use the file it points to as unix domain
|
|
// socket to communicate with sway (and possible i3).
|
|
func NewI3ipc(file ...string) *I3ipc {
|
|
ipc := &I3ipc{}
|
|
|
|
if len(file) == 0 {
|
|
ipc.SocketFile = "SWAYSOCK"
|
|
} else {
|
|
ipc.SocketFile = file[0]
|
|
}
|
|
|
|
return ipc
|
|
}
|
|
|
|
// internal convenience wrapper
|
|
func (ipc *I3ipc) get(command uint32) (*RawResponse, error) {
|
|
err := ipc.sendHeader(command, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
payload, err := ipc.readResponse()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return payload, nil
|
|
}
|