-
Notifications
You must be signed in to change notification settings - Fork 0
/
glos_x11.go
257 lines (224 loc) · 5.69 KB
/
glos_x11.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
// 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
package glos
import (
"log"
"os/exec"
"os/user"
"path/filepath"
"sync"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/cursor"
"github.com/goki/gi/oswin/mimedata"
)
// Notes on intermixing glfw and xgb: bottom line, can't do:
//
// can include Xlib-xcb.h to get XGetXCBConnection method, which can get the
// xcb_connection from the Display: https://xcb.freedesktop.org/MixingCalls/
// which glfw can return, in GetX11Display().
// BUT, BurntSushi/xgb does NOT seem to directly use the xcb_connection
// and instead is a complete ground-up rewrite using net.Conn connection protocol
// not sure if we can have 2 separate connections..
// and really, maybe we don't need it after all!? just use the
// text-based clipboard mechanisms to write mime-encoded content, and
// cursor impl has full support for creating new cursors, so..
/////////////////////////////////////////////////////////////////
// OS-specific methods
func (app *appImpl) Platform() oswin.Platforms {
return oswin.LinuxX11
}
func (app *appImpl) OpenURL(url string) {
cmd := exec.Command("xdg-open", url)
cmd.Run()
}
func (app *appImpl) FontPaths() []string {
return []string{"/usr/share/fonts/truetype"}
}
func (app *appImpl) PrefsDir() string {
usr, err := user.Current()
if err != nil {
log.Print(err)
return "/tmp"
}
return filepath.Join(usr.HomeDir, ".config")
}
// this is the main call to create the main menu if not exist
func (w *windowImpl) MainMenu() oswin.MainMenu {
return nil
}
func (w *windowImpl) OSHandle() uintptr {
return uintptr(w.glw.GetX11Window())
}
/////////////////////////////////////////////////////////////////
// Clipboard
type clipImpl struct {
lastWrite mimedata.Mimes
}
var theClip = clipImpl{}
func (ci *clipImpl) IsEmpty() bool {
w := theApp.ctxtwin
str, err := w.glw.GetClipboardString()
if err != nil || len(str) == 0 {
return true
}
return false
}
func (ci *clipImpl) Read(types []string) mimedata.Mimes {
w := theApp.ctxtwin
str, err := w.glw.GetClipboardString()
if err != nil || len(str) == 0 {
return nil
}
wantText := mimedata.IsText(types[0])
if wantText {
str, err := w.glw.GetClipboardString()
if err != nil || len(str) == 0 {
return nil
}
bstr := []byte(str)
isMulti, mediaType, boundary, body := mimedata.IsMultipart(bstr)
if isMulti {
return mimedata.FromMultipart(body, boundary)
} else {
if mediaType != "" { // found a mime type encoding
return mimedata.NewMime(mediaType, bstr)
} else {
// we can't really figure out type, so just assume..
return mimedata.NewMime(types[0], bstr)
}
}
} else {
// todo: deal with image formats etc
}
return nil
}
func (ci *clipImpl) Write(data mimedata.Mimes) error {
if len(data) == 0 {
return nil
}
w := theApp.ctxtwin
if len(data) > 1 { // multipart
mpd := data.ToMultipart()
w.glw.SetClipboardString(string(mpd))
} else {
d := data[0]
if mimedata.IsText(d.Type) {
w.glw.SetClipboardString(string(d.Data))
}
}
return nil
}
func (ci *clipImpl) Clear() {
// nop
}
//////////////////////////////////////////////////////
// Cursor
// todo: glfw has a seriously impoverished set of standard cursors..
// need to find a good collection and install
var cursorMap = map[cursor.Shapes]glfw.StandardCursor{
cursor.Arrow: glfw.ArrowCursor,
cursor.Cross: glfw.CrosshairCursor,
cursor.DragCopy: glfw.HandCursor,
cursor.DragMove: glfw.HandCursor,
cursor.DragLink: glfw.HandCursor,
cursor.HandPointing: glfw.HandCursor,
cursor.HandOpen: glfw.HandCursor,
cursor.HandClosed: glfw.HandCursor,
cursor.Help: glfw.HandCursor,
cursor.IBeam: glfw.IBeamCursor,
cursor.Not: glfw.HandCursor,
cursor.UpDown: glfw.VResizeCursor,
cursor.LeftRight: glfw.HResizeCursor,
cursor.UpRight: glfw.HResizeCursor,
cursor.UpLeft: glfw.HResizeCursor,
cursor.AllArrows: glfw.VResizeCursor,
cursor.Wait: glfw.VResizeCursor,
}
type cursorImpl struct {
cursor.CursorBase
cursors map[cursor.Shapes]*glfw.Cursor
mu sync.Mutex
}
var theCursor = cursorImpl{CursorBase: cursor.CursorBase{Vis: true}}
func (c *cursorImpl) createCursors() {
if c.cursors != nil {
return
}
c.cursors = make(map[cursor.Shapes]*glfw.Cursor)
for cs, sc := range cursorMap {
cur := glfw.CreateStandardCursor(sc)
c.cursors[cs] = cur
}
}
func (c *cursorImpl) setImpl(sh cursor.Shapes) {
c.createCursors()
cur, ok := c.cursors[sh]
if !ok || cur == nil {
return
}
w := theApp.ctxtwin
w.glw.SetCursor(cur)
}
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
w := theApp.ctxtwin
w.glw.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
c.mu.Unlock()
}
func (c *cursorImpl) Show() {
c.mu.Lock()
if c.Vis {
c.mu.Unlock()
return
}
c.Vis = true
w := theApp.ctxtwin
w.glw.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
c.mu.Unlock()
}
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
}