swayipc

package module
v0.0.0-...-244c14b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2023 License: Apache-2.0 Imports: 10 Imported by: 1

README

swayipc

swayipc is a Go library that enables interaction with the Sway compositor using its IPC protocol.

I created this library because I couldn't find anything that met my preferences (which is hard to explain). However, please note that this work is still in progress. Currently, I have only implemented the messages and events that I need for my projects. Adding new functionality should be simple, and I would be grateful to receive contributions.

Installation and Usage

To install the library, please use the following command:

$ go get gobytes.dev/swayipc

Below is an example program that subscribes to sway events. You can also find it in the cmds/subscribe directory in the source tree.

package main

import (
	"context"
	"fmt"
	"os"
	"strings"
	"time"

	"gobytes.dev/swayipc"
)

func handler(ev swayipc.Event) {
	switch ev := ev.(type) {
	case *swayipc.TickEvent:
		fmt.Println(ev.First, ev.Payload)
	case *swayipc.WindowEvent:
		if ev.Change == "focus" || ev.Change == "title" {
			appId := ev.Container.AppId
			name := ev.Container.Name
			if appId == "" {
				appId = ev.Container.WindowProperties.Class
			}
			appId = " - " + appId
			if len(name) > 80 {
				name = name[:77] + "..."
			}
			if !strings.Contains(strings.ToLower(name), strings.ToLower(appId)) {
				name = name + appId
			}
			fmt.Println(name)
		}
	}
}

func main() {
	conn, err := swayipc.Connect(context.Background())
	abortOnErr(err)
	conn.RegisterEventHandler(swayipc.HandlerFunc(handler))
	resp, err := conn.Subscribe(swayipc.WindowEventType, swayipc.TickEventType)
	abortOnErr(err)
	if resp.Success {
		fmt.Println("Successfully subscribed")
	}
	time.Sleep(3 * time.Minute)
}

func abortOnErr(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
		os.Exit(1)
	}
}

Contributions

Adding new messages and events is fairly easy. You can check this commit to see what is involved. If you have any patches, feel free to send them. I will gladly accept them.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandReply

type CommandReply struct {
	Success    bool   `json:"success"`
	ParseError bool   `json:"parse_error"`
	Error      string `json:"error"`
}

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func Connect

func Connect(ctx context.Context) (*Conn, error)

func (*Conn) GetMarks

func (c *Conn) GetMarks() (marks []string, err error)

func (*Conn) GetOutputs

func (c *Conn) GetOutputs() (outputs []OutputMode, err error)

func (*Conn) GetTree

func (c *Conn) GetTree() (tree Node, err error)

func (*Conn) GetVersion

func (c *Conn) GetVersion() (v Version, err error)

func (*Conn) GetWorkspaces

func (c *Conn) GetWorkspaces() (ws []Workspace, err error)

func (*Conn) RegisterEventHandler

func (c *Conn) RegisterEventHandler(ev Handler)

func (*Conn) RunCommand

func (c *Conn) RunCommand(cmd string) (r []CommandReply, err error)

func (*Conn) SendTick

func (c *Conn) SendTick(payload string) (status Status, err error)

func (*Conn) Subscribe

func (c *Conn) Subscribe(eventType ...EventType) (r Status, err error)

type Event

type Event interface {
	EventType() EventType
}

type EventType

type EventType uint32
const (
	WorkspaceEventType       EventType = 0x80000000
	ModeEventType            EventType = 0x80000002
	WindowEventType          EventType = 0x80000003
	BarconfigUpdateEventType EventType = 0x80000004
	BindingEventType         EventType = 0x80000005
	ShutdownEventType        EventType = 0x80000006
	TickEventType            EventType = 0x80000007
	BarStateUpdateEventType  EventType = 0x80000014
	InputEventType           EventType = 0x80000015
)

func (EventType) String

func (t EventType) String() string

type Handler

type Handler interface {
	HandleEvent(e Event)
}

type HandlerFunc

type HandlerFunc func(e Event)

func (HandlerFunc) HandleEvent

func (fn HandlerFunc) HandleEvent(e Event)
type Header struct {
	Magic [6]byte
	Len   int32
	Type  MessageType
}

type IdleInhibitors

type IdleInhibitors struct {
	Appliction string `json:"application"`
	User       string `json:"user"`
}

type MessageType

type MessageType uint32

type ModeEvent

type ModeEvent struct {
	Change      string `json:"change"`
	PangoMarkup bool   `json:"pango_markup"`
}

func (*ModeEvent) EventType

func (e *ModeEvent) EventType() EventType

type Node

type Node struct {
	Id                 int              `json:"id"`
	Name               string           `json:"name"`
	Type               string           `json:"type"`
	Border             string           `json:"border"`
	CurrentBorderWidth int              `json:"current_border_width"`
	Layout             string           `json:"layout"`
	Orientation        string           `json:"orientation"`
	Percent            float64          `json:"percent"`
	Rect               Rect             `json:"rect"`
	WindowRect         Rect             `json:"window_rect"`
	DecoRect           Rect             `json:"deco_rect"`
	Geometry           Rect             `json:"geometry"`
	Urgent             bool             `json:"urgent"`
	Sticky             bool             `json:"sticky"`
	Marks              []string         `json:"marks"`
	Focused            bool             `json:"focused"`
	Focus              []int            `json:"focus"`
	Nodes              []Node           `json:"nodes"`
	FloatingNodes      []Node           `json:"floating_nodes"`
	Representation     string           `json:"representation"`
	FullscreenMode     int              `json:"fullscreen_mode"`
	AppId              string           `json:"app_id"`
	Pid                int              `json:"pid"`
	Visible            bool             `json:"visible"`
	Shell              string           `json:"shell"`
	InhibitIdle        bool             `json:"inhibit_idle"`
	IdleInhibitors     IdleInhibitors   `json:"idle_inhibitors"`
	Window             int              `json:"window"`
	WindowProperties   WindowProperties `json:"window_properties"`
}

type Output

type Output struct {
	Name             string       `json:"name"`
	Make             string       `json:"make"`
	Model            string       `json:"model"`
	Serial           string       `json:"serial"`
	Active           bool         `json:"active"`
	Dpms             bool         `json:"dpms"`
	Primary          bool         `json:"primary"`
	Scale            float64      `json:"scale"`
	SubpixelHinting  string       `json:"subpixel_hinting"`
	Transform        string       `json:"transform"`
	CurrentWorkspace string       `json:"current_workspace"`
	Modes            []OutputMode `json:"modes"`
	CurrentMode      OutputMode   `json:"current_mode"`
	Rect             Rect         `json:"rect"`
}

type OutputMode

type OutputMode struct {
	Width   int `json:"width"`
	Height  int `json:"height"`
	Refresh int `json:"refresh"`
}

type Rect

type Rect struct {
	X int `json:"x"`
	Y int `json:"y"`
	W int `json:"width"`
	H int `json:"height"`
}

type Status

type Status struct {
	Success bool `json:"success"`
}

type TickEvent

type TickEvent struct {
	First   bool   `json:"first"`
	Payload string `json:"payload"`
}

func (*TickEvent) EventType

func (e *TickEvent) EventType() EventType

type Version

type Version struct {
	Major      int    `json:"major"`
	Minor      int    `json:"minor"`
	Patch      int    `json:"patch"`
	Version    string `json:"human_readable"`
	ConfigPath string `json:"loaded_config_file_name"`
}

type WindowEvent

type WindowEvent struct {
	Change    string `json:"change"`
	Container Node   `json:"container"`
}

func (*WindowEvent) EventType

func (e *WindowEvent) EventType() EventType

type WindowProperties

type WindowProperties struct {
	Title         string `json:"title"`
	Class         string `json:"class"`
	Instance      string `json:"instance"`
	WindowRole    string `json:"window_role"`
	WindowType    string `json:"window_type"`
	TrasitientFor string `json:"transitient_for"`
}

type Workspace

type Workspace struct {
	Num     int    `json:"num"`
	Name    string `json:"name"`
	Visible bool   `json:"visible"`
	Focused bool   `json:"focused"`
	Urgent  bool   `json:"urgent"`
	Rect    Rect   `json:"rect"`
	Output  string `json:"output"`
}

type WorkspaceEvent

type WorkspaceEvent struct {
	Change  string    `json:"change"`
	Current Workspace `json:"current"`
	Old     Workspace `json:"old"`
}

func (*WorkspaceEvent) EventType

func (e *WorkspaceEvent) EventType() EventType

Directories

Path Synopsis
cmds

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL