| 1 |
package wl |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"log" |
| 8 |
"net" |
| 9 |
"os" |
| 10 |
"sync" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
func init() { |
| 15 |
log.SetFlags(0) |
| 16 |
} |
| 17 |
|
| 18 |
type Context struct { |
| 19 |
mu sync.RWMutex |
| 20 |
conn *net.UnixConn |
| 21 |
currentId ProxyId |
| 22 |
objects map[ProxyId]Proxy |
| 23 |
dispatchChan chan struct{} |
| 24 |
exitChan chan struct{} |
| 25 |
} |
| 26 |
|
| 27 |
func (ctx *Context) Register(proxy Proxy) { |
| 28 |
ctx.mu.Lock() |
| 29 |
defer ctx.mu.Unlock() |
| 30 |
ctx.currentId += 1 |
| 31 |
proxy.SetId(ctx.currentId) |
| 32 |
proxy.SetContext(ctx) |
| 33 |
ctx.objects[ctx.currentId] = proxy |
| 34 |
} |
| 35 |
|
| 36 |
func (ctx *Context) lookupProxy(id ProxyId) Proxy { |
| 37 |
ctx.mu.RLock() |
| 38 |
defer ctx.mu.RUnlock() |
| 39 |
proxy, ok := ctx.objects[id] |
| 40 |
if !ok { |
| 41 |
return nil |
| 42 |
} |
| 43 |
return proxy |
| 44 |
} |
| 45 |
|
| 46 |
func (ctx *Context) unregister(proxy Proxy) { |
| 47 |
ctx.mu.Lock() |
| 48 |
defer ctx.mu.Unlock() |
| 49 |
delete(ctx.objects, proxy.Id()) |
| 50 |
} |
| 51 |
|
| 52 |
func (c *Context) Close() { |
| 53 |
c.conn.Close() |
| 54 |
c.exitChan <- struct{}{} |
| 55 |
close(c.dispatchChan) |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
func (c *Context) Dispatch() chan<- struct{} { |
| 60 |
return c.dispatchChan |
| 61 |
} |
| 62 |
|
| 63 |
func Connect(addr string) (ret *Display, err error) { |
| 64 |
runtime_dir := os.Getenv("XDG_RUNTIME_DIR") |
| 65 |
if runtime_dir == "" { |
| 66 |
return nil, errors.New("XDG_RUNTIME_DIR not set in the environment.") |
| 67 |
} |
| 68 |
if addr == "" { |
| 69 |
addr = os.Getenv("WAYLAND_DISPLAY") |
| 70 |
} |
| 71 |
if addr == "" { |
| 72 |
addr = "wayland-0" |
| 73 |
} |
| 74 |
addr = runtime_dir + "/" + addr |
| 75 |
c := new(Context) |
| 76 |
c.objects = make(map[ProxyId]Proxy) |
| 77 |
c.currentId = 0 |
| 78 |
c.dispatchChan = make(chan struct{}) |
| 79 |
c.exitChan = make(chan struct{}) |
| 80 |
c.conn, err = net.DialUnix("unix", nil, &net.UnixAddr{Name: addr, Net: "unix"}) |
| 81 |
if err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
c.conn.SetReadDeadline(time.Time{}) |
| 85 |
|
| 86 |
go c.run() |
| 87 |
return NewDisplay(c), nil |
| 88 |
} |
| 89 |
|
| 90 |
func (c *Context) run() { |
| 91 |
ctx := context.Background() |
| 92 |
|
| 93 |
loop: |
| 94 |
for { |
| 95 |
select { |
| 96 |
case <-c.dispatchChan: |
| 97 |
ev, err := c.readEvent() |
| 98 |
if err != nil { |
| 99 |
if err == io.EOF { |
| 100 |
|
| 101 |
break loop |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
if neterr, ok := err.(net.Error); ok && neterr.Timeout() { |
| 106 |
log.Print("Timeout Error") |
| 107 |
continue |
| 108 |
} |
| 109 |
|
| 110 |
log.Fatal(err) |
| 111 |
} |
| 112 |
|
| 113 |
proxy := c.lookupProxy(ev.pid) |
| 114 |
if proxy != nil { |
| 115 |
if dispatcher, ok := proxy.(Dispatcher); ok { |
| 116 |
dispatcher.Dispatch(ctx, ev) |
| 117 |
bytePool.Give(ev.data) |
| 118 |
} else { |
| 119 |
log.Print("Not dispatched") |
| 120 |
} |
| 121 |
} else { |
| 122 |
log.Print("Proxy NULL") |
| 123 |
} |
| 124 |
|
| 125 |
case <-c.exitChan: |
| 126 |
break loop |
| 127 |
} |
| 128 |
} |
| 129 |
} |