/
imgview.go
396 lines (322 loc) · 8.8 KB
/
imgview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"context"
"errors"
"fmt"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"os/signal"
wl "deedles.dev/wl/client"
xdg "deedles.dev/wl/examples/internal/xdg/client"
"deedles.dev/wl/pointer"
"deedles.dev/wl/shm"
"deedles.dev/wl/wire"
"deedles.dev/ximage/xcursor"
"deedles.dev/xsync"
_ "golang.org/x/image/bmp"
"golang.org/x/image/colornames"
"golang.org/x/image/draw"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
"golang.org/x/sys/unix"
)
type state struct {
image image.Image
stop xsync.Stopper
client *wl.Client
display *wl.Display
registry *wl.Registry
shm *wl.Shm
compositor *wl.Compositor
wmBase *xdg.WmBase
seat *wl.Seat
keyboard *wl.Keyboard
pointer *wl.Pointer
surface *wl.Surface
xsurface *xdg.Surface
toplevel *xdg.Toplevel
buffer *wl.ImageBuffer
cursorSurface *wl.Surface
cursorHot image.Point
pointerLoc image.Point
barBounds image.Rectangle
closeBounds image.Rectangle
//maxBounds image.Rectangle
//max bool
minBounds image.Rectangle
highlight *image.Rectangle
}
func (s *state) init() error {
client, err := wl.Dial()
if err != nil {
return fmt.Errorf("dial display: %w", err)
}
s.client = client
s.display = client.Display()
s.display.Listener = (*displayListener)(s)
s.registry = s.display.GetRegistry()
s.registry.Listener = (*registryListener)(s)
err = s.client.RoundTrip()
if err != nil {
return fmt.Errorf("round trip: %w", err)
}
if s.compositor == nil {
return errors.New("no compositor found")
}
if s.shm == nil {
return errors.New("no shm found")
}
if s.wmBase == nil {
return errors.New("no wmbase found")
}
if s.seat == nil {
return errors.New("no seat found")
}
s.initWindow()
s.initCursor()
return nil
}
func (s *state) initWindow() {
s.surface = s.compositor.CreateSurface()
s.xsurface = s.wmBase.GetXdgSurface(s.surface)
s.xsurface.Listener = (*xdgSurfaceListener)(s)
s.toplevel = s.xsurface.GetToplevel()
s.toplevel.SetTitle("Example")
s.toplevel.Listener = (*xdgToplevelListener)(s)
s.keyboard = s.seat.GetKeyboard()
s.pointer = s.seat.GetPointer()
s.pointer.Listener = (*pointerListener)(s)
s.surface.Commit()
buffer, err := wl.NewImageBuffer(s.shm, 1, 1)
if err != nil {
log.Fatalf("create buffer: %v", err)
}
s.buffer = buffer
}
func (s *state) initCursor() {
theme, err := xcursor.LoadTheme("")
if err != nil {
log.Fatalf("load cursor theme: %v", err)
}
cursors, ok := theme.Cursors["left_ptr"]
if !ok {
log.Fatalf("no left_ptr cursor in theme")
}
cimg := cursors.Images[cursors.BestSize(32)][0]
size := len(cimg.Image.Pix)
s.cursorHot = cimg.Hot
file, err := shm.Create()
if err != nil {
log.Fatalf("create SHM file: %v", err)
}
defer file.Close()
file.Truncate(int64(size))
mmap, err := shm.MapShared(file, size, unix.PROT_READ|unix.PROT_WRITE)
if err != nil {
log.Fatalf("mmap: %v", err)
}
defer mmap.Unmap()
s.cursorSurface = s.compositor.CreateSurface()
pool := s.shm.CreatePool(file, int32(size))
defer pool.Destroy()
buf := pool.CreateBuffer(
0,
int32(cimg.Image.Rect.Dx()),
int32(cimg.Image.Rect.Dy()),
int32(cimg.Image.Stride()),
wl.ShmFormatArgb8888,
)
copy(mmap, cimg.Image.Pix)
s.cursorSurface.Attach(buf, 0, 0)
s.cursorSurface.Commit()
}
func (s *state) run(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-s.stop.Done():
return
case ev, ok := <-s.client.Events():
if !ok {
return
}
err := ev()
if err != nil {
log.Printf("event: %v", err)
}
}
}
}
func (s *state) render(width, height int32) *wl.Buffer {
const barHeight = 30
s.barBounds = image.Rect(0, 0, int(width), barHeight)
imgBounds := image.Rect(0, 0, int(width), int(height))
if imgBounds.Empty() {
imgBounds = s.image.Bounds().Canon()
s.barBounds.Max.X = imgBounds.Max.X
}
s.closeBounds = image.Rect(
s.barBounds.Max.X-(barHeight-8)-4,
s.barBounds.Min.Y+4,
s.barBounds.Max.X-4,
s.barBounds.Max.Y-4,
)
//s.maxBounds = s.closeBounds.Sub(image.Pt(barHeight+4, 0))
s.minBounds = s.closeBounds.Sub(image.Pt(barHeight+4, 0))
imgBounds = imgBounds.Add(image.Pt(0, barHeight))
winBounds := s.barBounds.Union(imgBounds)
s.buffer.Resize(
int32(winBounds.Dx()),
int32(winBounds.Dy()),
)
img := s.buffer.Image()
fillRect(img, s.barBounds, colornames.Dimgray)
fillRect(img, s.closeBounds, colornames.Red)
//fillRect(img, s.maxBounds, colornames.Limegreen)
fillRect(img, s.minBounds, colornames.Yellow)
if s.highlight != nil {
addRect(img, *s.highlight, color.RGBA{0x10, 0x10, 0x10, 0xFF})
}
draw.ApproxBiLinear.Scale(img, imgBounds, s.image, s.image.Bounds(), draw.Src, nil)
return s.buffer.Buffer()
}
func (s *state) draw(w, h int32) {
buf := s.render(0, 0)
s.surface.Attach(buf, 0, 0)
s.surface.Commit()
}
type displayListener state
func (s *displayListener) Error(id, code uint32, msg string) {
log.Fatalf("display error: id: %v, code: %v, msg: %q", id, code, msg)
}
func (s *displayListener) DeleteId(id uint32) {
s.client.Delete(id)
}
type registryListener state
func (s *registryListener) Global(name uint32, inter string, version uint32) {
switch inter {
case wl.CompositorInterface:
s.compositor = wl.BindCompositor(s.client, s.registry, name, version)
case wl.ShmInterface:
s.shm = wl.BindShm(s.client, s.registry, name, version)
case xdg.WmBaseInterface:
s.wmBase = xdg.BindWmBase(s.client, s.registry, name, version)
s.wmBase.Listener = (*wmBaseListener)(s)
case wl.SeatInterface:
s.seat = wl.BindSeat(s.client, s.registry, name, version)
}
}
func (s *registryListener) GlobalRemove(name uint32) {}
type wmBaseListener state
func (s *wmBaseListener) Ping(serial uint32) {
s.wmBase.Pong(serial)
}
type pointerListener state
func (s *pointerListener) Enter(serial uint32, surface *wl.Surface, surfaceX wire.Fixed, surfaceY wire.Fixed) {
(*state)(s).pointer.SetCursor(serial, s.cursorSurface, int32(s.cursorHot.X), int32(s.cursorHot.Y))
}
func (s *pointerListener) Leave(serial uint32, surface *wl.Surface) {}
func (s *pointerListener) Motion(time uint32, x, y wire.Fixed) {
s.pointerLoc = image.Pt(x.Int(), y.Int())
switch {
case s.pointerLoc.In(s.closeBounds):
s.highlight = &s.closeBounds
//case s.pointerLoc.In(s.maxBounds):
// s.highlight = &s.maxBounds
case s.pointerLoc.In(s.minBounds):
s.highlight = &s.minBounds
default:
s.highlight = nil
}
//(*state)(s).draw(0, 0)
}
func (s *pointerListener) Button(serial, time uint32, button uint32, bstate wl.PointerButtonState) {
switch pointer.Button(button) {
case pointer.ButtonLeft:
switch {
case s.pointerLoc.In(s.closeBounds):
s.stop.Stop()
//case s.pointerLoc.In(s.maxBounds):
// s.toplevel.SetMaximized(!s.max)
// s.max = !s.max
case s.pointerLoc.In(s.minBounds):
s.toplevel.SetMinimized()
case s.pointerLoc.In(s.barBounds):
s.toplevel.Move(s.seat, serial)
}
}
}
func (s *pointerListener) Axis(time uint32, axis wl.PointerAxis, value wire.Fixed) {}
func (s *pointerListener) Frame() {}
func (s *pointerListener) AxisSource(axisSource wl.PointerAxisSource) {}
func (s *pointerListener) AxisStop(time uint32, axis wl.PointerAxis) {}
func (s *pointerListener) AxisDiscrete(axis wl.PointerAxis, discrete int32) {}
type xdgSurfaceListener state
func (s *xdgSurfaceListener) Configure(serial uint32) {
s.xsurface.AckConfigure(serial)
(*state)(s).draw(0, 0)
}
type xdgToplevelListener state
func (s *xdgToplevelListener) Configure(w, h int32, states []byte) {}
func (s *xdgToplevelListener) Close() {
s.stop.Stop()
}
func (s *xdgToplevelListener) ConfigureBounds(w, h int32) {}
func (s *xdgToplevelListener) WmCapabilities(capabilities []byte) {}
func fillRect(img draw.Image, r image.Rectangle, c color.Color) {
r = r.Canon()
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
img.Set(x, y, c)
}
}
}
func addRect(img draw.Image, r image.Rectangle, c color.Color) {
cr, cg, cb, ca := c.RGBA()
r = r.Canon()
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
or, og, ob, oa := img.At(x, y).RGBA()
img.Set(x, y, color.RGBA{
uint8((or + cr) * 0xFF / 0xFFFF),
uint8((og + cg) * 0xFF / 0xFFFF),
uint8((ob + cb) * 0xFF / 0xFFFF),
uint8((oa + ca) * 0xFF / 0xFFFF),
})
}
}
}
func loadImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
return img, err
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "usage: imgview <file>")
os.Exit(2)
}
img, err := loadImage(os.Args[1])
if err != nil {
log.Fatalf("load image: %v", err)
}
s := state{image: img}
err = s.init()
if err != nil {
log.Fatalf("init: %v", err)
}
defer s.client.Close()
s.run(ctx)
}