-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
175 lines (152 loc) · 3.79 KB
/
cursor.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
// Copyright 2018 The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux,!android dragonfly openbsd
// +build !3d
package x11driver
import (
"log"
"sync"
"github.com/BurntSushi/xgb/xproto"
"github.com/goki/gi/oswin/cursor"
)
// https://xcb.freedesktop.org/tutorial/mousecursors/
// https://tronche.com/gui/x/xlib/appendix/b/
var cursorMap = map[cursor.Shapes]int{
cursor.Arrow: 68, // XC_arrow is 2 but std seems to be left-up = 68
cursor.Cross: 34,
cursor.DragCopy: 90, // XC_plus
cursor.DragMove: 50, // XC_exchange
cursor.DragLink: 68, // todo: not special
cursor.HandPointing: 58, // XC_hand1
cursor.HandOpen: 60, // XC_hand2
cursor.HandClosed: 60, // XC_hand2
cursor.Help: 92,
cursor.IBeam: 152,
cursor.Not: 0, // X cursor?
cursor.UpDown: 116,
cursor.LeftRight: 108,
cursor.UpRight: 136,
cursor.UpLeft: 134,
cursor.AllArrows: 52,
cursor.Wait: 150,
}
type cursorImpl struct {
cursor.CursorBase
cursors map[cursor.Shapes]xproto.Cursor
cursFont xproto.Font
mu sync.Mutex
}
var theCursor = cursorImpl{CursorBase: cursor.CursorBase{Vis: true}}
func (c *cursorImpl) openCursorFont() {
var err error
c.cursFont, err = xproto.NewFontId(theApp.xc)
if err != nil {
log.Printf("x11driver: xproto.NewFontId failed: %v", err)
return
}
fnm := "cursor"
err = xproto.OpenFontChecked(theApp.xc, c.cursFont, uint16(len(fnm)), fnm).Check()
if err != nil {
log.Printf("x11driver: xproto.OpenFont for cursor font failed: %v", err)
return
}
}
func (c *cursorImpl) createCursor(curid int) xproto.Cursor {
cur, err := xproto.NewCursorId(theApp.xc)
if err != nil {
log.Printf("x11driver: xproto.NewCursorId failed: %v", err)
return 0
}
// 0's are all colors -- black by default
err = xproto.CreateGlyphCursorChecked(theApp.xc, cur, c.cursFont, c.cursFont, uint16(curid), uint16(curid+1), 65535, 65535, 65535, 0, 0, 0).Check()
if err != nil {
log.Printf("x11driver: xproto.CreateGlyphCursor for cursor id %v failed: %v", curid, err)
return 0
}
return cur
}
func (c *cursorImpl) setCursor(cur xproto.Cursor) {
fw := theApp.ctxtwin
vallist := []uint32{uint32(cur)}
err := xproto.ChangeWindowAttributesChecked(theApp.xc, fw.xw, xproto.CwCursor, vallist).Check()
if err != nil {
log.Printf("x11driver: xproto.ChangeWindowAttributes for cursor failed: %v", err)
}
}
func (c *cursorImpl) cursorHandle(sh cursor.Shapes) xproto.Cursor {
c.mu.Lock()
if c.cursors == nil {
c.cursors = make(map[cursor.Shapes]xproto.Cursor, cursor.ShapesN)
c.openCursorFont()
}
ch, ok := c.cursors[sh]
if !ok {
curid := cursorMap[sh]
ch = c.createCursor(curid)
c.cursors[sh] = ch
}
c.mu.Unlock()
return ch
}
func (c *cursorImpl) setImpl(sh cursor.Shapes) {
c.setCursor(c.cursorHandle(sh))
}
func (c *cursorImpl) Set(sh cursor.Shapes) {
c.mu.Lock()
c.Cur = sh
c.mu.Unlock()
c.setImpl(sh)
}
func (c *cursorImpl) Push(sh cursor.Shapes) {
c.mu.Lock()
c.PushStack(sh)
c.mu.Unlock()
c.setImpl(sh)
}
func (c *cursorImpl) Pop() {
c.mu.Lock()
sh, _ := c.PopStack()
c.mu.Unlock()
c.setImpl(sh)
}
func (c *cursorImpl) Hide() {
c.mu.Lock()
if c.Vis == false {
c.mu.Unlock()
return
}
c.Vis = false
c.mu.Unlock()
// _ShowCursor(false) // todo: create blank cursor
}
func (c *cursorImpl) Show() {
c.mu.Lock()
if c.Vis {
c.mu.Unlock()
return
}
c.mu.Unlock()
c.Vis = true
// _ShowCursor(true)
}
func (c *cursorImpl) PushIfNot(sh cursor.Shapes) bool {
c.mu.Lock()
if c.Cur == sh {
c.mu.Unlock()
return false
}
c.mu.Unlock()
c.Push(sh)
return true
}
func (c *cursorImpl) PopIf(sh cursor.Shapes) bool {
c.mu.Lock()
if c.Cur == sh {
c.mu.Unlock()
c.Pop()
return true
}
c.mu.Unlock()
return false
}