allow setting the x root window name

not perfect, still need to switch to byte arrays internally instead of strings
This commit is contained in:
frosty 2024-08-20 22:33:37 +00:00
parent 4653550119
commit 7a9ed4984d
3 changed files with 46 additions and 1 deletions

2
go.mod
View file

@ -1,3 +1,5 @@
module codeberg.org/frosty/modbot
go 1.22.4
require github.com/jezek/xgb v1.1.1 // indirect

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=

43
main.go
View file

@ -18,6 +18,7 @@ package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"log"
@ -26,6 +27,9 @@ import (
"sync"
"syscall"
"time"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
var (
@ -34,8 +38,16 @@ var (
mutex sync.Mutex
lastOutput string
// X connection data
x *xgb.Conn
root xproto.Window
)
type Flags struct {
SetXRootName bool
}
type Module struct {
Func func() (interface{}, error)
Interval time.Duration
@ -85,7 +97,28 @@ func (m *Module) Run() {
mutex.Unlock()
}
func parseFlags() Flags {
var flags Flags
flag.BoolVar(&flags.SetXRootName, "x", false, "set x root window name")
flag.Parse()
return flags
}
func main() {
flags := parseFlags()
// Connect to X and get the root window if requested
if flags.SetXRootName {
var err error
x, err = xgb.NewConn()
if err != nil {
log.Fatalf("X connection failed: %s\n", err.Error())
}
root = xproto.Setup(x).DefaultScreen(x).Root
}
sigChan := make(chan os.Signal, 1024)
signalMap := make(map[os.Signal][]*Module)
@ -126,8 +159,16 @@ func main() {
combinedOutput = prefix + combinedOutput + suffix
mutex.Unlock()
// Output to either X root window name or stdout based on flags
if combinedOutput != lastOutput {
fmt.Printf("%v\n", combinedOutput)
if flags.SetXRootName {
// Set the X root window name
outputBytes := []byte(combinedOutput)
xproto.ChangeProperty(x, xproto.PropModeReplace, root, xproto.AtomWmName, xproto.AtomString, 8, uint32(len(outputBytes)), outputBytes)
} else {
// Print to stdout
fmt.Printf("%v\n", combinedOutput)
}
lastOutput = combinedOutput
}
}