-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapplication.go
500 lines (410 loc) · 12.1 KB
/
application.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package x11ui
import (
"fmt"
"image/color"
"log"
"os"
"strings"
"github.com/lucasb-eyer/go-colorful"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/ewmh"
"github.com/BurntSushi/xgbutil/keybind"
"github.com/BurntSushi/xgbutil/mousebind"
"github.com/BurntSushi/xgbutil/xevent"
"github.com/BurntSushi/xgbutil/xwindow"
)
type LayoutDirection int
const (
LayoutNone LayoutDirection = iota
LayoutHor
LayoutVer
)
type Handler func()
type Application struct {
xu *xgbutil.XUtil
appWin *Window
NWindows int
Childs []*xwindow.Window
keycallbacks keybind.KeyPressFun
KeyMaps map[string]Handler
ClickMaps map[xproto.Window]OnClickFn
w, h int
title string
Debug bool
Dark bool
bg, fg color.Color
l LayoutDirection
pvsChildRect Rect
defW, defH int
hspacing, vspacing int
}
func (a *Application) X() *xgbutil.XUtil {
return a.xu
}
func (a *Application) Width() int {
return int(a.xu.Screen().WidthInPixels)
}
func (a *Application) Height() int {
return int(a.xu.Screen().HeightInPixels)
}
func (a *Application) AutoLayout(l LayoutDirection, newpos ...int) {
a.l = l
if len(newpos) > 0 {
a.pvsChildRect = newRect(newpos...)
if l == LayoutHor {
a.pvsChildRect.ShiftRight(-(a.pvsChildRect.Width + a.hspacing))
}
if l == LayoutVer {
a.pvsChildRect.ShiftDown(-(a.pvsChildRect.Height + a.vspacing))
}
}
}
func NewApp(fullscreen bool, width, height int) *Application {
return NewApplication("X11 Application", width, height, true, fullscreen)
}
func NewApplication(title string, width, height int, resizeable, fullApplication bool) *Application {
s := Application{w: width, h: height, title: title, Dark: false}
s.KeyMaps = make(map[string]Handler)
s.title = title
s.Debug = true
s.defW, s.defH = width, height
var err error
s.xu, err = xgbutil.NewConn()
if err != nil {
log.Fatal("Application: ", err)
}
keybind.Initialize(s.xu)
mousebind.Initialize(s.xu)
s.defaultWindow()
if fullApplication {
s.FullScreen()
}
s.title = title
s.appWin.SetTitle(title)
s.keycallbacks = s.keybHandler
xevent.KeyPressFun(s.keybHandler).Connect(s.xu, s.AppWin().Id)
mousebind.ButtonPressFun(s.mouseHandler).Connect(s.xu, s.appWin.Id, "1", false, false)
// cb1 := keybind.KeyPressFun(
// func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
// log.Println("Key press!")
// })
// root, err := xwindow.Generate(X)
// if err != nil {
// log.Fatal(err)
// }
return &s
}
func (a *Application) AppWin() *Window {
return a.appWin
}
func (s *Application) mouseHandler(X *xgbutil.XUtil, e xevent.ButtonPressEvent) {
// log.Println("Application ", s.appWin.Title(), " clicked at ", e.EventX, e.EventY)
if s.appWin.clkAdv != nil {
s.appWin.clkAdv(s.appWin, int(e.EventX), int(e.EventY))
}
if s.appWin.clk != nil {
s.appWin.clk()
}
}
func (s *Application) keybHandler(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
if s.xu != X {
log.Println("\n I am not the write handler !!")
return
}
modStr := keybind.ModifierString(e.State)
keyStr := keybind.LookupString(X, e.State, e.Detail)
modStr = strings.Replace(modStr, "lock-", "", -1)
modStr = strings.Replace(modStr, "mod2", "", -1)
finalstr := keyStr
if modStr != "" {
finalstr = fmt.Sprint(modStr, keyStr)
}
// log.Println("Event code is ", e.Detail)
// log.Printf("%s MAPS to Keycode %v ", finalstr, keybind.StrToKeycodes(s.xu, finalstr))
if fn, ok := s.KeyMaps[finalstr]; ok {
if s.Debug {
log.Printf("Caught Key : %s", finalstr)
}
fn()
} else {
log.Printf("Caught Key : %s", finalstr)
}
}
func (s *Application) RegisterGlobalKey(keyname string, fn Handler) bool {
if s.RegisterKey(keyname, fn) {
if err := keybind.GrabKeyboard(s.xu, s.xu.RootWin()); err != nil {
log.Fatalf("Could not grab keyboard: %s", err)
return false
}
log.Println("WARNING: We are taking *complete* control of the root " +
"window. The only way out is to press 'Control + Escape' or to " +
"close the window with the mouse.")
} else {
return false
}
return true
}
//RegisterKey registers a key with a function
func (s *Application) RegisterKey(keyname string, fn Handler) bool {
// log.Println("Current Root ", s.xu)
s.KeyMaps[keyname] = fn
if s.Debug {
log.Println("Registering ", keyname, " with this ", fn)
}
// if len(s.KeyMaps) == 0 {
// xevent.KeyPressFun(s.keybHandler).Connect(s.xu, s.bgWin.Id)
// }
return true
}
func (s *Application) Show() {
xevent.Main(s.xu)
}
func (s *Application) DefaultKeys(enable bool) {
if enable {
s.RegisterKey("q", s.Close)
s.RegisterKey("f", s.FullScreen)
}
}
func (s *Application) Close() {
xevent.Quit(s.xu)
}
func (s *Application) XWin() *xwindow.Window {
return s.appWin.Window
}
func (s *Application) Empty() *Window {
var w Window
w.Id = s.xu.RootWin()
return &w
}
func (s *Application) defaultWindow() {
// s.NWindows++
// s.appWin = s.NewFloatingWindow("Application ")
// s.appWin.WMMoveResize(0, 0, 1024, 523)
var err error
s.appWin = new(Window)
s.appWin.Window, err = xwindow.Generate(s.xu)
if err != nil {
log.Fatalln(err)
}
sinfo := s.xu.Screen()
w, h := int(sinfo.WidthInPixels)/2, int(sinfo.HeightInPixels)/2
w, h = s.defW, s.defH
s.appWin.Create(s.xu.RootWin(), 0, 0, w, h, xproto.CwBackPixel, 0x101010)
// Listen for Key{Press,Release} events.
s.appWin.Listen(xproto.EventMaskKeyPress, xproto.EventMaskKeyRelease, xproto.EventMaskButtonPress, xproto.EventMaskButtonPress, xproto.EventMaskSubstructureNotify, xproto.EventMaskStructureNotify)
// xevent.ResizeRequestFun(
// func(p *xgbutil.XUtil, e xevent.ResizeRequestEvent) {
// log.Printf("2. Received event ", e)
//
// }).Connect(s.xu, s.appWin.Id)
//
xevent.ConfigureRequestFun(
func(p *xgbutil.XUtil, e xevent.ConfigureRequestEvent) {
//
// log.Printf("4. CONFIGURE REEQUEST ", e)
}).Connect(s.xu, s.appWin.Id)
xevent.ConfigureNotifyFun(
func(p *xgbutil.XUtil, e xevent.ConfigureNotifyEvent) {
if e.SequenceId() != 126 {
// log.Printf("3. Received CONFIGNOTIFICATION ", e)
}
}).Connect(s.xu, s.appWin.Id)
// Make this window close gracefully.
s.appWin.WMGracefulClose(
func(w *xwindow.Window) {
xevent.Detach(w.X, w.Id)
keybind.Detach(w.X, w.Id)
mousebind.Detach(w.X, w.Id)
w.Destroy()
// if quit {
xevent.Quit(w.X)
// }
})
// Map the window.
s.appWin.Map()
// // Get a random background color, create the window (ask to receive button
// // release events while we're at it) and map the window.
// bgColor := rand.Intn(0xffffff + 1)
// win.Create(X.RootWin(), 0, 0, 200, 200,
// xproto.CwBackPixel|xproto.CwEventMask,
// uint32(bgColor), xproto.EventMaskButtonRelease)
// // WMGracefulClose does all of the work for us. It sets the appropriate
// // values for WM_PROTOCOLS, and listens for ClientMessages that implement
// // the WM_DELETE_WINDOW protocol. When one is found, the provided callback
// // is executed.
// win.WMGracefulClose(
// func(w *xwindow.Window) {
// // Detach all event handlers.
// // This should always be done when a window can no longer
// // receive events.
// xevent.Detach(w.X, w.Id)
// mousebind.Detach(w.X, w.Id)
// w.Destroy()
// // Exit if there are no more windows left.
// counter--
// if counter == 0 {
// os.Exit(0)
// }
// })
// // It's important that the map comes after setting WMGracefulClose, since
// // the WM isn't obliged to watch updates to the WM_PROTOCOLS property.
// win.Map()
// // A mouse binding so that a left click will spawn a new window.
// // Note that we don't issue a grab here. Typically, window managers will
// // grab a button press on the client window (which usually activates the
// // window), so that we'd end up competing with the window manager if we
// // tried to grab it.
// // Instead, we set a ButtonRelease mask when creating the window and attach
// // a mouse binding *without* a grab.
// err = mousebind.ButtonReleaseFun(
// func(X *xgbutil.XUtil, ev xevent.ButtonReleaseEvent) {
// newWindow(X)
// }).Connect(X, win.Id, "1", false, false)
// if err != nil {
// log.Fatal(err)
// }
}
func (s *Application) NewFloatingWindow(title string, dims ...int) *Window {
// X := s.xu
w := s.newWindow(0, newRect(dims...))
w.SetTitle(title)
if len(dims) < 3 {
w.Resize(700, 500)
}
return w
}
func (s *Application) FullScreen() {
err := ewmh.WmStateReq(s.xu, s.appWin.Id, ewmh.StateToggle, "_NET_WM_STATE_FULLSCREEN")
// ewmh.WmStateGet(s.xu, s.appWin.Id)
// s.appWin.ClearAll()
if err != nil {
log.Fatal(err)
}
}
func (s *Application) InvertBackGround() {
if s.appWin != nil {
if s.Dark {
s.appWin.Change(xproto.CwBackPixel, 0x265170)
} else {
s.appWin.Change(xproto.CwBackPixel, 0x1f1f1f)
}
s.Dark = !s.Dark
s.appWin.ClearAll()
}
}
func toUint32(colorful.Color) uint32 {
return 0
}
func (a *Application) SetDefaultKeys() {
a.RegisterKey("q", a.Close)
a.RegisterKey("i", a.InvertBackGround)
a.RegisterKey("f", a.FullScreen)
}
func (s *Application) newWindow(p xproto.Window, r Rect) *Window {
w := new(Window)
var parent xproto.Window
if p == 0 {
parent = s.xu.RootWin()
} else {
parent = p
}
win, err := xwindow.Generate(s.xu)
win.Create(parent, r.X, r.Y, r.Width, r.Height, xproto.CwBackPixel, 0xfffff)
if err != nil {
log.Fatal(err)
}
// // win.MoveResize(r.X, r.Y, r.Width, r.Height)
// if p == 0 {
// win.Change(xproto.CwBackPixel, 0x1f1f1f)
// } else {
// // win.Change(xproto.CwBackPixel, 0xC0FFC0)
// win.Change(xproto.CwBackPixel)
// }
w.Rect = r
w.Window = win
cb1 := mousebind.ButtonPressFun(w.mouseHandler)
if p == 0 {
log.Println("Registering right click also")
err = cb1.Connect(s.xu, win.Id, "2", false, true)
} else {
err = cb1.Connect(s.xu, win.Id, "1", false, true)
}
if err != nil {
log.Println("Error in binding mouse press ", err)
}
win.WMGracefulClose(
func(w *xwindow.Window) {
// Detach all event handlers.
// This should always be done when a window can no longer
// receive events.
xevent.Detach(w.X, w.Id)
mousebind.Detach(w.X, w.Id)
w.Destroy()
// Exit if there are no more windows left.
s.NWindows--
if s.NWindows == 0 {
os.Exit(0)
}
})
// It's important that the map comes after setting WMGracefulClose, since
// the WM isn't obliged to watch updates to the WM_PROTOCOLS property.
win.Map()
return w
}
func (a *Application) AddButton(caption string, geo ...int) *Window {
switch a.l {
case LayoutVer:
a.pvsChildRect.ShiftDown(a.pvsChildRect.Height + a.vspacing)
obj := NewButton(caption, a.AppWin(), a.pvsChildRect.array()...)
return obj
case LayoutHor:
a.pvsChildRect.ShiftRight(a.pvsChildRect.Width + a.hspacing)
obj := NewButton(caption, a.AppWin(), a.pvsChildRect.array()...)
return obj
default:
obj := NewButton(caption, a.AppWin(), geo...)
a.pvsChildRect = obj.Rect
return obj
}
}
func (a *Application) AddToggleBtn(caption string, geo ...int) *Window {
switch a.l {
case LayoutVer:
a.pvsChildRect.ShiftDown(a.pvsChildRect.Height + a.vspacing)
obj := NewToggleButton(caption, a.AppWin(), a.pvsChildRect.array()...)
return obj
case LayoutHor:
a.pvsChildRect.ShiftRight(a.pvsChildRect.Width + a.hspacing)
obj := NewToggleButton(caption, a.AppWin(), a.pvsChildRect.array()...)
return obj
default:
obj := NewToggleButton(caption, a.AppWin(), geo...)
a.pvsChildRect = obj.Rect
return obj
}
}
func (a *Application) SetLayoutSpacing(dx, dy int) {
a.hspacing, a.vspacing = dx, dy
}
func (a *Application) NewChildWindow(title string, dims ...int) *Window {
var w *Window
switch a.l {
case LayoutVer:
a.pvsChildRect.ShiftDown(a.pvsChildRect.Height + a.vspacing)
w = a.newWindow(a.appWin.Id, a.pvsChildRect)
case LayoutHor:
a.pvsChildRect.ShiftRight(a.pvsChildRect.Width + a.hspacing)
w = a.newWindow(a.appWin.Id, a.pvsChildRect)
default:
w = a.newWindow(a.appWin.Id, newRect(dims...))
a.pvsChildRect = w.Rect
}
// w.SetBackGround(colorful.LinearRgb(0, 0, 0))
w.bgcolor = color.RGBA{100, 100, 100, 255}
g := w.drawView(StateNormal)
w.finishPaint(g)
// w.SetTitle(title)
w.Detach()
return w
}