| 1 |
package ui |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"image" |
| 6 |
"image/draw" |
| 7 |
|
| 8 |
"syscall" |
| 9 |
|
| 10 |
"github.com/dkolbly/wl" |
| 11 |
"github.com/dkolbly/wl/xdg" |
| 12 |
) |
| 13 |
|
| 14 |
type Window struct { |
| 15 |
display *Display |
| 16 |
surface *wl.Surface |
| 17 |
shSurface *wl.ShellSurface |
| 18 |
xdgSurface *xdg.Surface |
| 19 |
buffer *wl.Buffer |
| 20 |
data []byte |
| 21 |
image *BGRA |
| 22 |
title string |
| 23 |
pending Config |
| 24 |
current Config |
| 25 |
} |
| 26 |
|
| 27 |
func (d *Display) NewWindow(width, height int32) (*Window, error) { |
| 28 |
var err error |
| 29 |
stride := width * 4 |
| 30 |
|
| 31 |
w := new(Window) |
| 32 |
pend := Config{ |
| 33 |
Width: int(width), |
| 34 |
Height: int(height), |
| 35 |
} |
| 36 |
|
| 37 |
w.pending = pend |
| 38 |
w.current = pend |
| 39 |
|
| 40 |
w.display = d |
| 41 |
|
| 42 |
w.surface, err = d.compositor.CreateSurface() |
| 43 |
if err != nil { |
| 44 |
return nil, fmt.Errorf("Surface creation failed: %s", err) |
| 45 |
} |
| 46 |
|
| 47 |
w.buffer, w.data, err = d.newBuffer(width, height, stride) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
|
| 52 |
if d.wmBase != nil { |
| 53 |
|
| 54 |
w.setupXDGTopLevel() |
| 55 |
} else { |
| 56 |
|
| 57 |
w.shSurface, err = d.shell.GetShellSurface(w.surface) |
| 58 |
if err != nil { |
| 59 |
return nil, fmt.Errorf("Shell.GetShellSurface failed: %s", err) |
| 60 |
} |
| 61 |
|
| 62 |
w.shSurface.AddPingHandler(w) |
| 63 |
w.shSurface.SetToplevel() |
| 64 |
} |
| 65 |
|
| 66 |
err = w.surface.Attach(w.buffer, width, height) |
| 67 |
if err != nil { |
| 68 |
return nil, fmt.Errorf("Surface.Attach failed: %s", err) |
| 69 |
} |
| 70 |
|
| 71 |
err = w.surface.Damage(0, 0, width, height) |
| 72 |
if err != nil { |
| 73 |
return nil, fmt.Errorf("Surface.Damage failed: %s", err) |
| 74 |
} |
| 75 |
|
| 76 |
if true { |
| 77 |
err = w.surface.Commit() |
| 78 |
if err != nil { |
| 79 |
return nil, fmt.Errorf("Surface.Commit failed: %s", err) |
| 80 |
} |
| 81 |
|
| 82 |
w.image = NewBGRAWithData( |
| 83 |
image.Rect(0, 0, int(width), int(height)), |
| 84 |
w.data) |
| 85 |
|
| 86 |
d.registerWindow(w) |
| 87 |
} |
| 88 |
|
| 89 |
return w, nil |
| 90 |
} |
| 91 |
|
| 92 |
func (w *Window) DrawUsingFunc(fn func(*BGRA)) { |
| 93 |
fn(w.image) |
| 94 |
} |
| 95 |
|
| 96 |
func (w *Window) Draw(img image.Image) { |
| 97 |
draw.Draw(w.image, img.Bounds(), img, img.Bounds().Min, draw.Src) |
| 98 |
} |
| 99 |
|
| 100 |
func (w *Window) Dispose() { |
| 101 |
if w.shSurface != nil { |
| 102 |
w.shSurface.RemovePingHandler(w) |
| 103 |
} |
| 104 |
w.surface.Destroy() |
| 105 |
w.buffer.Destroy() |
| 106 |
syscall.Munmap(w.data) |
| 107 |
w.display.unregisterWindow(w) |
| 108 |
} |
| 109 |
|
| 110 |
func (w *Window) HandleShellSurfacePing(ev wl.ShellSurfacePingEvent) { |
| 111 |
w.shSurface.Pong(ev.Serial) |
| 112 |
} |