stone/backends/x/x.go

425 lines
10 KiB
Go
Raw Normal View History

2022-11-09 13:52:49 -07:00
package x
2022-11-11 13:01:36 -07:00
import "os"
2022-11-09 13:52:49 -07:00
import "image"
2022-11-10 19:32:02 -07:00
import "image/draw"
2022-11-10 10:38:02 -07:00
import "golang.org/x/image/font"
2022-11-10 19:32:02 -07:00
import "golang.org/x/image/math/fixed"
2022-11-11 13:01:36 -07:00
import "golang.org/x/image/font/opentype"
2022-11-10 10:38:02 -07:00
import "golang.org/x/image/font/basicfont"
2022-11-09 13:52:49 -07:00
2022-11-09 20:33:18 -07:00
import "github.com/jezek/xgb"
2022-11-09 13:52:49 -07:00
import "github.com/jezek/xgbutil"
2022-11-09 20:33:18 -07:00
import "github.com/jezek/xgb/xproto"
2022-11-10 00:02:08 -07:00
import "github.com/jezek/xgbutil/ewmh"
2022-11-09 16:53:14 -07:00
import "github.com/jezek/xgbutil/xevent"
2022-11-09 13:52:49 -07:00
import "github.com/jezek/xgbutil/xwindow"
import "github.com/jezek/xgbutil/xgraphics"
import "git.tebibyte.media/sashakoshka/stone"
2022-11-11 13:01:36 -07:00
import "github.com/flopp/go-findfont"
2022-11-09 13:52:49 -07:00
type Backend struct {
2022-11-09 16:53:14 -07:00
application *stone.Application
config *stone.Config
connection *xgbutil.XUtil
window *xwindow.Window
canvas *xgraphics.Image
channel chan(stone.Event)
2022-11-09 13:52:49 -07:00
2022-11-09 16:53:14 -07:00
ping struct {
before chan(struct { })
after chan(struct { })
quit chan(struct { })
}
2022-11-10 10:38:02 -07:00
font struct {
face font.Face
}
2022-11-10 18:43:27 -07:00
colors [4]xgraphics.BGRA
2022-11-09 16:53:14 -07:00
metrics struct {
2022-11-09 20:33:18 -07:00
windowWidth int
windowHeight int
cellWidth int
cellHeight int
padding int
paddingX int
paddingY int
descent int
2022-11-09 16:53:14 -07:00
}
2022-11-09 13:52:49 -07:00
}
2022-11-09 16:53:14 -07:00
func (backend *Backend) Run (channel chan(stone.Event)) {
backend.channel = channel
2022-11-09 13:52:49 -07:00
2022-11-09 16:53:14 -07:00
for {
select {
case <- backend.ping.before:
2022-11-09 20:33:18 -07:00
// 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)
}
}
2022-11-09 16:53:14 -07:00
<- backend.ping.after
case <- backend.ping.quit:
backend.shutDown()
return
}
}
2022-11-09 13:52:49 -07:00
}
2022-11-10 00:02:08 -07:00
func (backend *Backend) SetTitle (title string) (err error) {
err = ewmh.WmNameSet(backend.connection, backend.window.Id, title)
return
2022-11-09 13:52:49 -07:00
}
2022-11-10 00:02:08 -07:00
func (backend *Backend) SetIcon (icons []image.Image) (err error) {
wmIcons := []ewmh.WmIcon { }
2022-11-09 13:52:49 -07:00
2022-11-10 00:02:08 -07:00
for _, icon := range icons {
width := icon.Bounds().Max.X
height := icon.Bounds().Max.Y
wmIcon := ewmh.WmIcon {
Width: uint(width),
Height: uint(height),
Data: make ([]uint, width * height),
}
// manually convert image data beacuse of course we have to do
// this
index := 0
for y := 0; y < height; y ++ {
for x := 0; x < width; x ++ {
r, g, b, a := icon.At(x, y).RGBA()
r >>= 8
g >>= 8
b >>= 8
a >>= 8
wmIcon.Data[index] =
(uint(a) << 24) |
(uint(r) << 16) |
(uint(g) << 8) |
(uint(b) << 0)
index ++
}}
wmIcons = append(wmIcons, wmIcon)
}
err = ewmh.WmIconSet(backend.connection, backend.window.Id, wmIcons)
return
2022-11-09 13:52:49 -07:00
}
2022-11-09 16:53:14 -07:00
2022-11-09 20:33:18 -07:00
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 {
// compress events
configureEvent =
backend.compressConfigureNotify(configureEvent)
// resize and rebind canvas
2022-11-10 18:43:27 -07:00
backend.reallocateCanvas()
2022-11-09 20:33:18 -07:00
}
}
}
func (backend *Backend) compressConfigureNotify (
firstEvent xproto.ConfigureNotifyEvent,
) (
lastEvent xproto.ConfigureNotifyEvent,
) {
backend.connection.Sync()
xevent.Read(backend.connection, false)
lastEvent = firstEvent
for index, untypedEvent := range xevent.Peek(backend.connection) {
if untypedEvent.Err != nil { continue }
typedEvent, ok := untypedEvent.Event.(xproto.ConfigureNotifyEvent)
if !ok { continue }
lastEvent = typedEvent
defer func (index int) {
xevent.DequeueAt(backend.connection, index)
} (index)
}
return
}
2022-11-09 16:53:14 -07:00
func (backend *Backend) shutDown () {
backend.channel <- stone.EventQuit { }
}
// calculateWindowSize calculates window bounds based on the internal buffer
// size.
func (backend *Backend) calculateWindowSize () (x, y int) {
width, height := backend.application.Size()
x =
width * backend.metrics.cellWidth +
backend.metrics.padding * 2
y =
height * backend.metrics.cellHeight +
backend.metrics.padding * 2
return
}
2022-11-10 18:43:27 -07:00
func (backend *Backend) reallocateCanvas () {
if backend.canvas != nil {
backend.canvas.Destroy()
}
backend.canvas = xgraphics.New (
backend.connection,
image.Rect (
0, 0,
backend.metrics.windowWidth,
backend.metrics.windowHeight))
backend.canvas.For (func (x, y int) xgraphics.BGRA {
return backend.colors[stone.ColorApplication]
})
2022-11-10 19:32:02 -07:00
backend.drawRune(0, 0, 'T')
backend.drawRune(1, 0, 'h')
backend.drawRune(2, 0, 'e')
backend.drawRune(4, 0, 'q')
backend.drawRune(5, 0, 'u')
backend.drawRune(6, 0, 'i')
backend.drawRune(7, 0, 'c')
backend.drawRune(8, 0, 'k')
backend.drawRune(0, 1, 'b')
backend.drawRune(1, 1, 'r')
backend.drawRune(2, 1, 'o')
backend.drawRune(3, 1, 'w')
backend.drawRune(4, 1, 'n')
backend.drawRune(6, 1, 'f')
backend.drawRune(7, 1, 'o')
backend.drawRune(8, 1, 'x')
2022-11-10 19:32:02 -07:00
2022-11-09 17:07:40 -07:00
backend.canvas.XSurfaceSet(backend.window.Id)
backend.canvas.XDraw()
backend.canvas.XPaint(backend.window.Id)
}
2022-11-09 16:53:14 -07:00
2022-11-10 10:38:02 -07:00
func (backend *Backend) drawRune (x, y int, character rune) {
// TODO: cache these draws as non-transparent buffers with the
// application background color as the background. that way, we won't
// need to redraw the characters *or* composite them.
origin := backend.originOfCell(x, y + 1)
destinationRectangle, mask, maskPoint, _, _ := backend.font.face.Glyph (
fixed.Point26_6 {
X: fixed.I(origin.X),
Y: fixed.I(origin.Y - backend.metrics.descent),
},
2022-11-10 19:32:02 -07:00
character)
fillRectangle (
&image.Uniform {
C: backend.config.Color(stone.ColorApplication),
},
backend.canvas,
backend.boundsOfCell(x, y))
// strokeRectangle (
// &image.Uniform {
// C: backend.config.Color(stone.ColorForeground),
// },
// backend.canvas,
// backend.boundsOfCell(x, y))
2022-11-10 19:32:02 -07:00
draw.DrawMask (
backend.canvas,
destinationRectangle,
&image.Uniform {
C: backend.config.Color(stone.ColorForeground),
2022-11-10 19:32:02 -07:00
},
image.Point { },
mask,
maskPoint,
draw.Over)
}
func (backend *Backend) cellSubImage (x, y int) (cell *xgraphics.Image) {
cell = backend.canvas.SubImage(backend.boundsOfCell(x, y)).(*xgraphics.Image)
return
}
func (backend *Backend) originOfCell (x, y int) (origin image.Point) {
origin = image.Point {
X: x * backend.metrics.cellWidth + backend.metrics.paddingX,
Y: y * backend.metrics.cellHeight + backend.metrics.paddingY,
}
return
}
func (backend *Backend) boundsOfCell (x, y int) (bounds image.Rectangle) {
bounds = image.Rectangle {
Min: backend.originOfCell(x, y),
Max: backend.originOfCell(x + 1, y + 1),
}
return
2022-11-10 10:38:02 -07:00
}
2022-11-09 16:53:14 -07:00
// factory instantiates an X backend.
func factory (application *stone.Application) (output stone.Backend, err error) {
backend := &Backend {
application: application,
config: application.Config(),
}
2022-11-10 10:38:02 -07:00
// load font
backend.font.face = findAndLoadFont (
backend.config.FontName(),
float64(backend.config.FontSize()))
if backend.font.face == nil {
backend.font.face = basicfont.Face7x13
}
2022-11-10 10:38:02 -07:00
2022-11-10 18:43:27 -07:00
// pre-calculate colors
for index := 0; index < len(backend.colors); index ++ {
color := backend.config.Color(stone.Color(index))
r, g, b, a := color.RGBA()
r >>= 8
g >>= 8
b >>= 8
a >>= 8
backend.colors[index] = xgraphics.BGRA {
R: uint8(r),
G: uint8(g),
B: uint8(b),
A: uint8(a),
}
}
2022-11-09 16:53:14 -07:00
// calculate metrics
2022-11-10 10:38:02 -07:00
metrics := backend.font.face.Metrics()
glyphAdvance, _ := backend.font.face.GlyphAdvance('M')
backend.metrics.cellWidth = glyphAdvance.Round()
backend.metrics.cellHeight = metrics.Height.Round()
backend.metrics.descent = metrics.Descent.Round()
2022-11-09 16:53:14 -07:00
backend.metrics.padding =
backend.config.Padding() *
backend.metrics.cellHeight
backend.metrics.paddingX = backend.metrics.padding
backend.metrics.paddingY = backend.metrics.padding
2022-11-09 20:33:18 -07:00
backend.metrics.windowWidth,
backend.metrics.windowHeight = backend.calculateWindowSize()
2022-11-09 16:53:14 -07:00
// connect to X
backend.connection, err = xgbutil.NewConn()
if err != nil { return }
backend.window, err = xwindow.Generate(backend.connection)
if err != nil { return }
// create the window
backend.window.Create (
backend.connection.RootWin(),
2022-11-09 20:33:18 -07:00
0, 0,
backend.metrics.windowWidth, backend.metrics.windowHeight,
2022-11-09 16:53:14 -07:00
0)
backend.window.Map()
2022-11-09 20:33:18 -07:00
backend.window.Listen(xproto.EventMaskStructureNotify)
2022-11-10 00:02:08 -07:00
backend.SetTitle(application.Title())
backend.SetIcon(application.Icon())
2022-11-09 16:53:14 -07:00
2022-11-09 17:07:40 -07:00
// create a canvas
2022-11-10 18:43:27 -07:00
backend.reallocateCanvas()
2022-11-09 17:07:40 -07:00
2022-11-09 16:53:14 -07:00
// attatch graceful close handler
backend.window.WMGracefulClose (func (window *xwindow.Window) {
backend.window.Destroy()
backend.shutDown()
})
// start event loop
backend.ping.before,
backend.ping.after,
backend.ping.quit = xevent.MainPing(backend.connection)
output = backend
return
}
func findAndLoadFont (name string, size float64) (face font.Face) {
if name == "" { return }
2022-11-11 13:01:36 -07:00
fontPath, err := findfont.Find(name)
if err != nil { return }
fontFile, err := os.Open(fontPath)
if err != nil { return }
fontObject, err := opentype.ParseReaderAt(fontFile)
if err != nil { return }
face, err = opentype.NewFace (fontObject, &opentype.FaceOptions {
Size: size,
DPI: 96,
Hinting: font.HintingFull,
})
if err != nil { face = nil }
return
}
func fillRectangle (
source image.Image,
destination draw.Image,
bounds image.Rectangle,
) {
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
destination.Set(x, y, source.At(x, y))
}}
}
func strokeRectangle (
source image.Image,
destination draw.Image,
bounds image.Rectangle,
) {
x := 0
y := bounds.Min.Y
for x = bounds.Min.X; x < bounds.Max.X; x ++ {
destination.Set(x, y, source.At(x, y))
}
y = bounds.Max.Y - 1
for x = bounds.Min.X; x < bounds.Max.X; x ++ {
destination.Set(x, y, source.At(x, y))
}
x = bounds.Min.X
for y = bounds.Min.Y; y < bounds.Max.Y; y ++ {
destination.Set(x, y, source.At(x, y))
}
x = bounds.Max.X - 1
for y = bounds.Min.Y; y < bounds.Max.Y; y ++ {
destination.Set(x, y, source.At(x, y))
}
}
2022-11-09 16:53:14 -07:00
// init registers this backend when the program starts.
func init () {
stone.RegisterBackend(factory)
}