Debian Codegraph @

shiny/driver/x11driver/window.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package x11driver
6
7 // TODO: implement a back buffer.
8
9 import (
10 "image"
11 "image/color"
12 "image/draw"
13 "sync"
14
15 "github.com/jezek/xgb"
16 "github.com/jezek/xgb/render"
17 "github.com/jezek/xgb/xproto"
18
19 "golang.org/x/exp/shiny/driver/internal/drawer"
20 "golang.org/x/exp/shiny/driver/internal/event"
21 "golang.org/x/exp/shiny/driver/internal/lifecycler"
22 "golang.org/x/exp/shiny/driver/internal/x11key"
23 "golang.org/x/exp/shiny/screen"
24 "golang.org/x/image/math/f64"
25 "golang.org/x/mobile/event/key"
26 "golang.org/x/mobile/event/mouse"
27 "golang.org/x/mobile/event/paint"
28 "golang.org/x/mobile/event/size"
29 "golang.org/x/mobile/geom"
30 )
31
32 type windowImpl struct {
33 s *screenImpl
34
35 xw xproto.Window
36 xg xproto.Gcontext
37 xp render.Picture
38
39 event.Deque
40 xevents chan xgb.Event
41
42 // This next group of variables are mutable, but are only modified in the
43 // screenImpl.run goroutine.
44 width, height int
45
46 lifecycler lifecycler.State
47
48 mu sync.Mutex
49 released bool
50 }
51
52 func (w *windowImpl) Release() {
53 w.mu.Lock()
54 released := w.released
55 w.released = true
56 w.mu.Unlock()
57
58 // TODO: call w.lifecycler.SetDead and w.lifecycler.SendEvent, a la
59 // handling atomWMDeleteWindow?
60
61 if released {
62 return
63 }
64 render.FreePicture(w.s.xc, w.xp)
65 xproto.FreeGC(w.s.xc, w.xg)
66 xproto.DestroyWindow(w.s.xc, w.xw)
67 }
68
69 func (w *windowImpl) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle) {
70 src.(bufferUploader).upload(xproto.Drawable(w.xw), w.xg, w.s.xsi.RootDepth, dp, sr)
71 }
72
73 func (w *windowImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) {
74 fill(w.s.xc, w.xp, dr, src, op)
75 }
76
77 func (w *windowImpl) DrawUniform(src2dst f64.Aff3, src color.Color, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
78 w.s.drawUniform(w.xp, &src2dst, src, sr, op, opts)
79 }
80
81 func (w *windowImpl) Draw(src2dst f64.Aff3, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
82 src.(*textureImpl).draw(w.xp, &src2dst, sr, op, opts)
83 }
84
85 func (w *windowImpl) Copy(dp image.Point, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
86 drawer.Copy(w, dp, src, sr, op, opts)
87 }
88
89 func (w *windowImpl) Scale(dr image.Rectangle, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
90 drawer.Scale(w, dr, src, sr, op, opts)
91 }
92
93 func (w *windowImpl) Publish() screen.PublishResult {
94 // TODO: implement a back buffer, and copy or flip that here to the front
95 // buffer.
96
97 // This sync isn't needed to flush the outgoing X11 requests. Instead, it
98 // acts as a form of flow control. Outgoing requests can be quite small on
99 // the wire, e.g. draw this texture ID (an integer) to this rectangle (four
100 // more integers), but much more expensive on the server (blending a
101 // million source and destination pixels). Without this sync, the Go X11
102 // client could easily end up sending work at a faster rate than the X11
103 // server can serve.
104 w.s.xc.Sync()
105
106 return screen.PublishResult{}
107 }
108
109 func (w *windowImpl) handleConfigureNotify(ev xproto.ConfigureNotifyEvent) {
110 // TODO: does the order of these lifecycle and size events matter? Should
111 // they really be a single, atomic event?
112 w.lifecycler.SetVisible((int(ev.X)+int(ev.Width)) > 0 && (int(ev.Y)+int(ev.Height)) > 0)
113 w.lifecycler.SendEvent(w, nil)
114
115 newWidth, newHeight := int(ev.Width), int(ev.Height)
116 if w.width == newWidth && w.height == newHeight {
117 return
118 }
119 w.width, w.height = newWidth, newHeight
120 w.Send(size.Event{
121 WidthPx: newWidth,
122 HeightPx: newHeight,
123 WidthPt: geom.Pt(newWidth),
124 HeightPt: geom.Pt(newHeight),
125 PixelsPerPt: w.s.pixelsPerPt,
126 })
127 }
128
129 func (w *windowImpl) handleExpose() {
130 w.Send(paint.Event{})
131 }
132
133 func (w *windowImpl) handleKey(detail xproto.Keycode, state uint16, dir key.Direction) {
134 r, c := w.s.keysyms.Lookup(uint8(detail), state)
135 w.Send(key.Event{
136 Rune: r,
137 Code: c,
138 Modifiers: x11key.KeyModifiers(state),
139 Direction: dir,
140 })
141 }
142
143 func (w *windowImpl) handleMouse(x, y int16, b xproto.Button, state uint16, dir mouse.Direction) {
144 // TODO: should a mouse.Event have a separate MouseModifiers field, for
145 // which buttons are pressed during a mouse move?
146 btn := mouse.Button(b)
147 switch btn {
148 case 4:
149 btn = mouse.ButtonWheelUp
150 case 5:
151 btn = mouse.ButtonWheelDown
152 case 6:
153 btn = mouse.ButtonWheelLeft
154 case 7:
155 btn = mouse.ButtonWheelRight
156 }
157 if btn.IsWheel() {
158 if dir != mouse.DirPress {
159 return
160 }
161 dir = mouse.DirStep
162 }
163 w.Send(mouse.Event{
164 X: float32(x),
165 Y: float32(y),
166 Button: btn,
167 Modifiers: x11key.KeyModifiers(state),
168 Direction: dir,
169 })
170 }