mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-01-28 23:42:51 -05:00
wlcontext: use poll with wake pipe instead of read deadlines
This commit is contained in:
@@ -500,14 +500,13 @@ func (m *Manager) receiveData(offer *ext_data_control.ExtDataControlOfferV1, mim
|
|||||||
receiveErr := make(chan error, 1)
|
receiveErr := make(chan error, 1)
|
||||||
m.post(func() {
|
m.post(func() {
|
||||||
err := offer.Receive(mimeType, int(w.Fd()))
|
err := offer.Receive(mimeType, int(w.Fd()))
|
||||||
|
w.Close()
|
||||||
receiveErr <- err
|
receiveErr <- err
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := <-receiveErr; err != nil {
|
if err := <-receiveErr; err != nil {
|
||||||
w.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
w.Close()
|
|
||||||
|
|
||||||
type result struct {
|
type result struct {
|
||||||
data []byte
|
data []byte
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package wlcontext
|
package wlcontext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/errdefs"
|
"github.com/AvengeMedia/DankMaterialShell/core/internal/errdefs"
|
||||||
"github.com/AvengeMedia/DankMaterialShell/core/internal/log"
|
"github.com/AvengeMedia/DankMaterialShell/core/internal/log"
|
||||||
@@ -27,6 +27,8 @@ type SharedContext struct {
|
|||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
fatalError chan error
|
fatalError chan error
|
||||||
cmdQueue chan func()
|
cmdQueue chan func()
|
||||||
|
wakeR int
|
||||||
|
wakeW int
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
started bool
|
started bool
|
||||||
@@ -38,11 +40,31 @@ func New() (*SharedContext, error) {
|
|||||||
return nil, fmt.Errorf("%w: %v", errdefs.ErrNoWaylandDisplay, err)
|
return nil, fmt.Errorf("%w: %v", errdefs.ErrNoWaylandDisplay, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fds := make([]int, 2)
|
||||||
|
if err := unix.Pipe(fds); err != nil {
|
||||||
|
display.Context().Close()
|
||||||
|
return nil, fmt.Errorf("failed to create wake pipe: %w", err)
|
||||||
|
}
|
||||||
|
if err := unix.SetNonblock(fds[0], true); err != nil {
|
||||||
|
unix.Close(fds[0])
|
||||||
|
unix.Close(fds[1])
|
||||||
|
display.Context().Close()
|
||||||
|
return nil, fmt.Errorf("failed to set wake pipe nonblock: %w", err)
|
||||||
|
}
|
||||||
|
if err := unix.SetNonblock(fds[1], true); err != nil {
|
||||||
|
unix.Close(fds[0])
|
||||||
|
unix.Close(fds[1])
|
||||||
|
display.Context().Close()
|
||||||
|
return nil, fmt.Errorf("failed to set wake pipe nonblock: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
sc := &SharedContext{
|
sc := &SharedContext{
|
||||||
display: display,
|
display: display,
|
||||||
stopChan: make(chan struct{}),
|
stopChan: make(chan struct{}),
|
||||||
fatalError: make(chan error, 1),
|
fatalError: make(chan error, 1),
|
||||||
cmdQueue: make(chan func(), 256),
|
cmdQueue: make(chan func(), 256),
|
||||||
|
wakeR: fds[0],
|
||||||
|
wakeW: fds[1],
|
||||||
started: false,
|
started: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +91,9 @@ func (sc *SharedContext) Display() *wlclient.Display {
|
|||||||
func (sc *SharedContext) Post(fn func()) {
|
func (sc *SharedContext) Post(fn func()) {
|
||||||
select {
|
select {
|
||||||
case sc.cmdQueue <- fn:
|
case sc.cmdQueue <- fn:
|
||||||
|
if _, err := unix.Write(sc.wakeW, []byte{1}); err != nil && err != unix.EAGAIN {
|
||||||
|
log.Errorf("wake pipe write error: %v", err)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +114,14 @@ func (sc *SharedContext) eventDispatcher() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx := sc.display.Context()
|
ctx := sc.display.Context()
|
||||||
|
wlFd := ctx.Fd()
|
||||||
|
|
||||||
|
pollFds := []unix.PollFd{
|
||||||
|
{Fd: int32(wlFd), Events: unix.POLLIN},
|
||||||
|
{Fd: int32(sc.wakeR), Events: unix.POLLIN},
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -100,20 +132,33 @@ func (sc *SharedContext) eventDispatcher() {
|
|||||||
|
|
||||||
sc.drainCmdQueue()
|
sc.drainCmdQueue()
|
||||||
|
|
||||||
if err := ctx.SetReadDeadline(time.Now().Add(50 * time.Millisecond)); err != nil {
|
n, err := unix.Poll(pollFds, 50)
|
||||||
log.Errorf("Failed to set read deadline: %v", err)
|
if err != nil {
|
||||||
}
|
if err == unix.EINTR {
|
||||||
err := ctx.Dispatch()
|
continue
|
||||||
if err := ctx.SetReadDeadline(time.Time{}); err != nil {
|
}
|
||||||
log.Errorf("Failed to clear read deadline: %v", err)
|
log.Errorf("Poll error: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
if n == 0 {
|
||||||
case err == nil:
|
continue
|
||||||
case errors.Is(err, os.ErrDeadlineExceeded):
|
}
|
||||||
default:
|
|
||||||
log.Errorf("Wayland connection error: %v", err)
|
if pollFds[1].Revents&unix.POLLIN != 0 {
|
||||||
return
|
var buf [64]byte
|
||||||
|
if _, err := unix.Read(sc.wakeR, buf[:]); err != nil && err != unix.EAGAIN {
|
||||||
|
log.Errorf("wake pipe read error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if pollFds[0].Revents&unix.POLLIN != 0 {
|
||||||
|
if err := ctx.Dispatch(); err != nil {
|
||||||
|
if !os.IsTimeout(err) {
|
||||||
|
log.Errorf("Wayland connection error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,6 +178,9 @@ func (sc *SharedContext) Close() {
|
|||||||
close(sc.stopChan)
|
close(sc.stopChan)
|
||||||
sc.wg.Wait()
|
sc.wg.Wait()
|
||||||
|
|
||||||
|
unix.Close(sc.wakeR)
|
||||||
|
unix.Close(sc.wakeW)
|
||||||
|
|
||||||
if sc.display != nil {
|
if sc.display != nil {
|
||||||
sc.display.Context().Close()
|
sc.display.Context().Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,18 @@ func (ctx *Context) SetReadDeadline(t time.Time) error {
|
|||||||
return ctx.conn.SetReadDeadline(t)
|
return ctx.conn.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *Context) Fd() int {
|
||||||
|
rawConn, err := ctx.conn.SyscallConn()
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
var fd int
|
||||||
|
rawConn.Control(func(f uintptr) {
|
||||||
|
fd = int(f)
|
||||||
|
})
|
||||||
|
return fd
|
||||||
|
}
|
||||||
|
|
||||||
// Dispatch reads and processes incoming messages and calls [client.Dispatcher.Dispatch] on the
|
// Dispatch reads and processes incoming messages and calls [client.Dispatcher.Dispatch] on the
|
||||||
// respective wayland protocol.
|
// respective wayland protocol.
|
||||||
// Dispatch must be called on the same goroutine as other interactions with the Context.
|
// Dispatch must be called on the same goroutine as other interactions with the Context.
|
||||||
|
|||||||
@@ -215,4 +215,3 @@ done
|
|||||||
echo "=========================================="
|
echo "=========================================="
|
||||||
echo "Status check complete!"
|
echo "Status check complete!"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user