Handle actual X events

This commit is contained in:
Sasha Koshka 2022-11-09 22:33:18 -05:00
parent 01f43a03a4
commit f807c8df35

View File

@ -1,8 +1,13 @@
package x package x
import "image" import "image"
import "image/color"
// import "golang.org/x/image/font"
// import "golang.org/x/image/font/basicfont"
import "github.com/jezek/xgb"
import "github.com/jezek/xgbutil" import "github.com/jezek/xgbutil"
import "github.com/jezek/xgb/xproto"
// import "github.com/jezek/xgbutil/ewmh" // import "github.com/jezek/xgbutil/ewmh"
import "github.com/jezek/xgbutil/xevent" import "github.com/jezek/xgbutil/xevent"
import "github.com/jezek/xgbutil/xwindow" import "github.com/jezek/xgbutil/xwindow"
@ -25,12 +30,14 @@ type Backend struct {
} }
metrics struct { metrics struct {
cellWidth int windowWidth int
cellHeight int windowHeight int
padding int cellWidth int
paddingX int cellHeight int
paddingY int padding int
descent int paddingX int
paddingY int
descent int
} }
} }
@ -40,6 +47,19 @@ func (backend *Backend) Run (channel chan(stone.Event)) {
for { for {
select { select {
case <- backend.ping.before: case <- backend.ping.before:
// if the queue is empty, don't dequeue anything because
// it would cause a fucking segfault lmao (???)
if !xevent.Empty(backend.connection) {
event, err := xevent.Dequeue(backend.connection)
if err != nil {
// TODO: do something with err
}
if event != nil {
backend.handleXEvent(event)
}
}
<- backend.ping.after <- backend.ping.after
case <- backend.ping.quit: case <- backend.ping.quit:
@ -57,6 +77,25 @@ func (backend *Backend) SetIcon (icons []image.Image) {
} }
func (backend *Backend) handleXEvent (event xgb.Event) {
switch event.(type) {
case xproto.ConfigureNotifyEvent:
configureEvent := event.(xproto.ConfigureNotifyEvent)
newWidth := int(configureEvent.Width)
newHeight := int(configureEvent.Height)
sizeChanged :=
backend.metrics.windowWidth != newWidth ||
backend.metrics.windowHeight != newHeight
backend.metrics.windowWidth = newWidth
backend.metrics.windowHeight = newHeight
if sizeChanged {
// TODO: resize and rebind canvas
}
}
}
func (backend *Backend) shutDown () { func (backend *Backend) shutDown () {
backend.channel <- stone.EventQuit { } backend.channel <- stone.EventQuit { }
} }
@ -96,6 +135,8 @@ func factory (application *stone.Application) (output stone.Backend, err error)
backend.metrics.cellHeight backend.metrics.cellHeight
backend.metrics.paddingX = backend.metrics.padding backend.metrics.paddingX = backend.metrics.padding
backend.metrics.paddingY = backend.metrics.padding backend.metrics.paddingY = backend.metrics.padding
backend.metrics.windowWidth,
backend.metrics.windowHeight = backend.calculateWindowSize()
// connect to X // connect to X
backend.connection, err = xgbutil.NewConn() backend.connection, err = xgbutil.NewConn()
@ -104,20 +145,25 @@ func factory (application *stone.Application) (output stone.Backend, err error)
if err != nil { return } if err != nil { return }
// create the window // create the window
windowWidth, windowHeight := backend.calculateWindowSize()
backend.window.Create ( backend.window.Create (
backend.connection.RootWin(), backend.connection.RootWin(),
0, 0, windowWidth, windowHeight, 0, 0,
backend.metrics.windowWidth, backend.metrics.windowHeight,
0) 0)
backend.window.Map() backend.window.Map()
backend.window.Listen(xproto.EventMaskStructureNotify)
// create a canvas // create a canvas
backend.canvas = xgraphics.New ( backend.canvas = xgraphics.New (
backend.connection, backend.connection,
image.Rect ( image.Rect (
0, 0, 0, 0,
windowWidth, backend.metrics.windowWidth,
windowHeight)) backend.metrics.windowHeight))
for i := 8; i < 64; i ++ {
backend.canvas.Set(8, i, color.RGBA { R: 0xFF, A: 0xFF })
}
backend.bindCanvas() backend.bindCanvas()
// attatch graceful close handler // attatch graceful close handler