Skip to content

Commit

Permalink
ref: restructure variables
Browse files Browse the repository at this point in the history
  • Loading branch information
leukipp committed Apr 7, 2024
1 parent 7afadcc commit 8720467
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 203 deletions.
18 changes: 3 additions & 15 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"fmt"
"reflect"
"regexp"
"strconv"
Expand All @@ -10,15 +9,11 @@ import (
"crypto/sha1"
"encoding/hex"

"github.com/jezek/xgb/render"

"github.com/jezek/xgbutil/xrect"
)

type Pointer struct {
X int16 // Pointer X position relative to root
Y int16 // Pointer Y position relative to root
Button uint16 // Pointer button states of device
}

func Hash(text string) string {
hash := sha1.New()
hash.Write([]byte(text))
Expand All @@ -36,13 +31,6 @@ func IsType(a interface{}, b interface{}) bool {
return reflect.TypeOf(a) == reflect.TypeOf(b)
}

func IsInt(s string) bool {
s = strings.TrimPrefix(s, "-")
re := fmt.Sprintf("\\d{%d}", len(s))
match, err := regexp.MatchString(re, s)
return match && err == nil
}

func IsZero(items []uint) bool {
mask := uint(0)
for _, s := range items {
Expand All @@ -60,7 +48,7 @@ func IsInList(item string, items []string) bool {
return false
}

func IsInsideRect(p *Pointer, r xrect.Rect) bool {
func IsInsideRect(p render.Pointfix, r xrect.Rect) bool {
x, y, w, h := r.Pieces()
xInRect := int(p.X) >= x && int(p.X) <= (x+w)
yInRect := int(p.Y) >= y && int(p.Y) <= (y+h)
Expand Down
53 changes: 32 additions & 21 deletions desktop/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import (
)

type Tracker struct {
Clients map[xproto.Window]*store.Client // List of clients that are being tracked
Clients map[xproto.Window]*store.Client // List of tracked clients
Workspaces map[store.Location]*Workspace // List of workspaces per location
Action chan string // Event channel for actions
Channel *Channel // Helper for channel communication
Handler *Handler // Helper for event handlers

}
type Channel struct {
Event chan string // Channel for events
Action chan string // Channel for actions
}

type Handler struct {
Expand All @@ -42,7 +47,10 @@ func CreateTracker() *Tracker {
tr := Tracker{
Clients: make(map[xproto.Window]*store.Client),
Workspaces: CreateWorkspaces(),
Action: make(chan string),
Channel: &Channel{
Event: make(chan string),
Action: make(chan string),
},
Handler: &Handler{
ResizeClient: &HandlerClient{},
MoveClient: &HandlerClient{},
Expand All @@ -63,11 +71,11 @@ func (tr *Tracker) Update() {
if ws.Disabled() {
return
}
log.Debug("Update trackable clients [", len(tr.Clients), "/", len(store.Windows), "]")
log.Debug("Update trackable clients [", len(tr.Clients), "/", len(store.Windows.Stacked), "]")

// Map trackable windows
trackable := make(map[xproto.Window]bool)
for _, w := range store.Windows {
for _, w := range store.Windows.Stacked {
trackable[w] = tr.isTrackable(w)
}

Expand All @@ -79,15 +87,15 @@ func (tr *Tracker) Update() {
}

// Add trackable windows
for _, w := range store.Windows {
for _, w := range store.Windows.Stacked {
if trackable[w] {
tr.trackWindow(w)
}
}
}

func (tr *Tracker) Reset() {
log.Debug("Reset trackable clients [", len(tr.Clients), "/", len(store.Windows), "]")
log.Debug("Reset trackable clients [", len(tr.Clients), "/", len(store.Windows.Stacked), "]")

// Reset client list
for w := range tr.Clients {
Expand All @@ -112,7 +120,7 @@ func (tr *Tracker) Write() {
}

func (tr *Tracker) ActiveWorkspace() *Workspace {
location := store.Location{DeskNum: store.CurrentDesk, ScreenNum: store.CurrentScreen}
location := store.Location{DeskNum: store.Workplace.CurrentDesk, ScreenNum: store.Workplace.CurrentScreen}

// Validate active workspace
ws := tr.Workspaces[location]
Expand Down Expand Up @@ -209,7 +217,7 @@ func (tr *Tracker) handleMaximizedClient(c *store.Client) {

// Set fullscreen layout
c.UnMaximize()
tr.Action <- "layout_fullscreen"
tr.Channel.Action <- "layout_fullscreen"
c.Activate()

break
Expand Down Expand Up @@ -315,7 +323,7 @@ func (tr *Tracker) handleMoveClient(c *store.Client) {
// Check position changes
moved := cx != px || cy != py
resized := cw != pw || ch != ph
active := c.Win.Id == store.ActiveWindow
active := c.Win.Id == store.Windows.Active

if active && moved && !resized && !tr.Handler.ResizeClient.Active {
mg := ws.ActiveLayout().GetManager()
Expand All @@ -335,7 +343,7 @@ func (tr *Tracker) handleMoveClient(c *store.Client) {
}

// Store moved client and hovered client
if common.IsInsideRect(pt, co.Latest.Dimensions.Geometry.Rect) {
if common.IsInsideRect(pt.Position, co.Latest.Dimensions.Geometry.Rect) {
tr.Handler.SwapClient = &HandlerClient{Active: true, Source: c, Target: co}
log.Debug("Client move handler active [", c.Latest.Class, "-", co.Latest.Class, "]")
break
Expand All @@ -344,7 +352,7 @@ func (tr *Tracker) handleMoveClient(c *store.Client) {

// Check if pointer moves to another screen
tr.Handler.SwapScreen.Active = false
if c.Latest.Location.ScreenNum != store.CurrentScreen {
if c.Latest.Location.ScreenNum != store.Workplace.CurrentScreen {
tr.Handler.SwapScreen = &HandlerClient{Active: true, Source: c}
}
}
Expand Down Expand Up @@ -413,12 +421,12 @@ func (tr *Tracker) handleWorkspaceChange(c *store.Client) {
tr.Handler.SwapScreen.Active = false
}

func (tr *Tracker) onStateUpdate(aname string) {
viewportChanged := common.IsInList(aname, []string{"_NET_NUMBER_OF_DESKTOPS", "_NET_DESKTOP_LAYOUT", "_NET_DESKTOP_GEOMETRY", "_NET_DESKTOP_VIEWPORT", "_NET_WORKAREA"})
clientsChanged := common.IsInList(aname, []string{"_NET_CLIENT_LIST_STACKING", "_NET_ACTIVE_WINDOW"})
func (tr *Tracker) onStateUpdate(state string, desk uint, screen uint) {
viewportChanged := common.IsInList(state, []string{"_NET_NUMBER_OF_DESKTOPS", "_NET_DESKTOP_LAYOUT", "_NET_DESKTOP_GEOMETRY", "_NET_DESKTOP_VIEWPORT", "_NET_WORKAREA"})
clientsChanged := common.IsInList(state, []string{"_NET_CLIENT_LIST_STACKING", "_NET_ACTIVE_WINDOW"})

workspacesChanged := store.DeskCount*store.ScreenCount != uint(len(tr.Workspaces))
workspaceChanged := common.IsInList(aname, []string{"_NET_CURRENT_DESKTOP"})
workspacesChanged := store.Workplace.DeskCount*store.Workplace.ScreenCount != uint(len(tr.Workspaces))
workspaceChanged := common.IsInList(state, []string{"_NET_CURRENT_DESKTOP"})

// Number of desktops or screens changed
if workspacesChanged {
Expand All @@ -429,7 +437,7 @@ func (tr *Tracker) onStateUpdate(aname string) {
if workspaceChanged {
for _, c := range tr.Clients {
sticky := common.IsInList("_NET_WM_STATE_STICKY", c.Latest.States)
if sticky && c.Latest.Location.DeskNum != store.CurrentDesk {
if sticky && c.Latest.Location.DeskNum != store.Workplace.CurrentDesk {
ewmh.WmDesktopSet(store.X, c.Win.Id, ^uint(0))
}
}
Expand All @@ -453,9 +461,12 @@ func (tr *Tracker) onStateUpdate(aname string) {

// Write client and workspace cache
tr.Write()

// Communicate state update
tr.Channel.Event <- "state_update"
}

func (tr *Tracker) onPointerUpdate(button uint16) {
func (tr *Tracker) onPointerUpdate(button uint16, desk uint, screen uint) {
release := button == 0

// Reset timer
Expand Down Expand Up @@ -529,15 +540,15 @@ func (tr *Tracker) attachHandlers(c *store.Client) {
log.Trace("Client focus in event [", c.Latest.Class, "]")

// Update active window
store.ActiveWindow = store.ActiveWindowGet(store.X)
store.Windows.Active = store.ActiveWindowGet(store.X)
}).Connect(store.X, c.Win.Id)

// Attach focus out events
xevent.FocusOutFun(func(x *xgbutil.XUtil, ev xevent.FocusOutEvent) {
log.Trace("Client focus out event [", c.Latest.Class, "]")

// Update active window
store.ActiveWindow = store.ActiveWindowGet(store.X)
store.Windows.Active = store.ActiveWindowGet(store.X)
}).Connect(store.X, c.Win.Id)
}

Expand Down
6 changes: 3 additions & 3 deletions desktop/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type Workspace struct {
func CreateWorkspaces() map[store.Location]*Workspace {
workspaces := make(map[store.Location]*Workspace)

for deskNum := uint(0); deskNum < store.DeskCount; deskNum++ {
for screenNum := uint(0); screenNum < store.ScreenCount; screenNum++ {
for deskNum := uint(0); deskNum < store.Workplace.DeskCount; deskNum++ {
for screenNum := uint(0); screenNum < store.Workplace.ScreenCount; screenNum++ {
location := store.Location{DeskNum: deskNum, ScreenNum: screenNum}

// Create layouts for each desktop and screen
Expand Down Expand Up @@ -234,7 +234,7 @@ func (ws *Workspace) Cache() common.Cache[*Workspace] {
hash := fmt.Sprintf("%s-%d", name, ws.Location.ScreenNum)

// Create workspace cache folder
folder := filepath.Join(common.Args.Cache, store.Displays.Name, "workspaces", name)
folder := filepath.Join(common.Args.Cache, store.Workplace.Displays.Name, "workspaces", name)
if _, err := os.Stat(folder); os.IsNotExist(err) {
os.MkdirAll(folder, 0755)
}
Expand Down
31 changes: 15 additions & 16 deletions input/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

var (
executeCallbacksFun []func(string) // Execute events callback functions
executeCallbacksFun []func(string, uint, uint) // Execute events callback functions
)

func Execute(action string, mod string, tr *desktop.Tracker) bool {
Expand Down Expand Up @@ -99,20 +99,16 @@ func Execute(action string, mod string, tr *desktop.Tracker) bool {
return false
}

// Notify socket
type Action struct {
DeskNum uint
ScreenNum uint
}
NotifySocket(Message[Action]{
// Notify socket (deprecated)
NotifySocket(Message[store.Location]{
Type: "Action",
Name: action,
Data: Action{DeskNum: ws.Location.DeskNum, ScreenNum: ws.Location.ScreenNum},
Data: ws.Location,
})
}

// Execute callbacks
executeCallbacks(action)
// Execute callbacks
executeCallbacks(action, ws.Location.DeskNum, ws.Location.ScreenNum)
}

return true
}
Expand All @@ -135,20 +131,23 @@ func Query(state string, tr *desktop.Tracker) bool {
ScreenNum uint
Workspaces []*desktop.Workspace
}
// Notify socket (deprecated)
NotifySocket(Message[Workspaces]{
Type: "State",
Name: state,
Data: Workspaces{DeskNum: ws.Location.DeskNum, ScreenNum: ws.Location.ScreenNum, Workspaces: maps.Values(tr.Workspaces)},
})
success = true
case "arguments":
// Notify socket (deprecated)
NotifySocket(Message[common.Arguments]{
Type: "State",
Name: state,
Data: common.Args,
})
success = true
case "configs":
// Notify socket (deprecated)
NotifySocket(Message[common.Configuration]{
Type: "State",
Name: state,
Expand Down Expand Up @@ -317,7 +316,7 @@ func MakeMaster(tr *desktop.Tracker, ws *desktop.Workspace) bool {
if ws.Disabled() {
return false
}
if c, ok := tr.Clients[store.ActiveWindow]; ok {
if c, ok := tr.Clients[store.Windows.Active]; ok {
ws.ActiveLayout().MakeMaster(c)
ws.Tile()
return true
Expand Down Expand Up @@ -505,14 +504,14 @@ func External(command string) bool {
return true
}

func OnExecute(fun func(string)) {
func OnExecute(fun func(string, uint, uint)) {
executeCallbacksFun = append(executeCallbacksFun, fun)
}

func executeCallbacks(arg string) {
log.Info("Execute event ", arg)
func executeCallbacks(action string, desk uint, screen uint) {
log.Info("Execute event ", action)

for _, fun := range executeCallbacksFun {
fun(arg)
fun(action, desk, screen)
}
}
4 changes: 2 additions & 2 deletions input/mousebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func BindMouse(tr *desktop.Tracker) {
}

// Evaluate corner states
for i := range store.Corners {
hc := store.Corners[i]
for i := range store.Workplace.Displays.Corners {
hc := store.Workplace.Displays.Corners[i]

wasActive := hc.Active
isActive := hc.IsActive(pt)
Expand Down
9 changes: 0 additions & 9 deletions input/signalbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,9 @@ func BindSignal(tr *desktop.Tracker) {
// Bind signal channel
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go exit(ch, tr)

// Bind action channel
go action(tr.Action, tr)
}

func exit(ch chan os.Signal, tr *desktop.Tracker) {
<-ch
Execute("exit", "current", tr)
}

func action(ch chan string, tr *desktop.Tracker) {
for {
Execute(<-ch, "current", tr)
}
}
4 changes: 2 additions & 2 deletions input/traybinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func BindTray(tr *desktop.Tracker) {
}, func() {})

// Attach execute events
OnExecute(func(action string) {
OnExecute(func(action string, desk uint, screen uint) {
onExecute(tr, action)
})

// Attach pointer events
store.OnPointerUpdate(func(button uint16) {
store.OnPointerUpdate(func(button uint16, desk uint, screen uint) {
onPointerClick(tr, button)
})
}
Expand Down
Loading

0 comments on commit 8720467

Please sign in to comment.