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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
package main
import (
"janouch.name/haven/nexgb"
"janouch.name/haven/nexgb/render"
"janouch.name/haven/nexgb/xproto"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/gomono"
"golang.org/x/image/math/fixed"
//"golang.org/x/image/font/gofont/gomonobold"
//"golang.org/x/image/font/gofont/gomonoitalic"
//"golang.org/x/image/font/gofont/gomonobolditalic"
"flag"
"image"
"image/color"
"image/draw"
"log"
"os"
"strings"
)
// #include <fcntl.h>
// #include <stdlib.h>
//import "C"
// -----------------------------------------------------------------------------
// Text rendering
// glyphListBytes serializes a run of runes for use in an X11 request.
func glyphListBytes(buf []byte, runes []rune, size int) int {
b := 0
for _, r := range runes {
switch size {
default:
buf[b] = byte(r)
b += 1
case 2:
nexgb.Put16(buf[b:], uint16(r))
b += 2
case 4:
nexgb.Put32(buf[b:], uint32(r))
b += 4
}
}
return nexgb.Pad(b)
}
// When the len is 255, a GLYPHABLE follows, otherwise a list of CARD8/16/32.
func glyphEltHeaderBytes(buf []byte, len byte, deltaX, deltaY int16) int {
b := 0
buf[b] = len
b += 4
nexgb.Put16(buf[b:], uint16(deltaX))
b += 2
nexgb.Put16(buf[b:], uint16(deltaY))
b += 2
return nexgb.Pad(b)
}
type xgbCookie interface{ Check() error }
// compositeString makes an appropriate render.CompositeGlyphs request,
// assuming that glyphs equal Unicode codepoints.
func compositeString(c *nexgb.Conn, op byte, src, dst render.Picture,
maskFormat render.Pictformat, glyphset render.Glyphset, srcX, srcY int16,
destX, destY int16, text string) xgbCookie {
runes := []rune(text)
var highest rune
for _, r := range runes {
if r > highest {
highest = r
}
}
size := 1
switch {
case highest > 1<<16:
size = 4
case highest > 1<<8:
size = 2
}
// They gave up on the XCB protocol API and we need to serialize explicitly.
// To spare us from caring about the padding, use the largest number lesser
// than 255 that is divisible by 4 (for size 2 and 4 the requirements are
// less strict but this works in the general case).
const maxPerChunk = 252
buf := make([]byte, (len(runes)+maxPerChunk-1)/maxPerChunk*8+len(runes)*size)
b := 0
for len(runes) > maxPerChunk {
b += glyphEltHeaderBytes(buf[b:], maxPerChunk, 0, 0)
b += glyphListBytes(buf[b:], runes[:maxPerChunk], size)
runes = runes[maxPerChunk:]
}
if len(runes) > 0 {
b += glyphEltHeaderBytes(buf[b:], byte(len(runes)), destX, destY)
b += glyphListBytes(buf[b:], runes, size)
}
switch size {
default:
return render.CompositeGlyphs8(c, op, src, dst, maskFormat, glyphset,
srcX, srcY, buf)
case 2:
return render.CompositeGlyphs16(c, op, src, dst, maskFormat, glyphset,
srcX, srcY, buf)
case 4:
return render.CompositeGlyphs32(c, op, src, dst, maskFormat, glyphset,
srcX, srcY, buf)
}
}
type textRenderer struct {
f *truetype.Font
opts *truetype.Options
face font.Face
bounds fixed.Rectangle26_6 // outer bounds for all the font's glyphs
buf *image.RGBA // rendering buffer
X *nexgb.Conn
gsid render.Glyphset
loaded map[rune]bool
}
func newTextRenderer(X *nexgb.Conn, ttf []byte, opts *truetype.Options) (
*textRenderer, error) {
pformats, err := render.QueryPictFormats(X).Reply()
if err != nil {
return nil, err
}
// We use RGBA here just so that lines are padded to 32 bits.
// Since there's no subpixel antialiasing and alpha is premultiplied,
// it doesn't even mater that RGBA is interpreted as ARGB or BGRA.
var rgbFormat render.Pictformat
for _, pf := range pformats.Formats {
if pf.Depth == 32 && pf.Direct.AlphaMask != 0 {
rgbFormat = pf.Id
break
}
}
tr := &textRenderer{opts: opts, X: X, loaded: make(map[rune]bool)}
if tr.f, err = freetype.ParseFont(ttf); err != nil {
return nil, err
}
tr.face = truetype.NewFace(tr.f, opts)
tr.bounds = tr.f.Bounds(fixed.Int26_6(opts.Size * float64(opts.DPI) *
(64.0 / 72.0)))
if tr.gsid, err = render.NewGlyphsetId(X); err != nil {
return nil, err
}
if err := render.CreateGlyphSetChecked(X, tr.gsid, rgbFormat).
Check(); err != nil {
return nil, err
}
tr.buf = image.NewRGBA(image.Rect(
+tr.bounds.Min.X.Floor(),
-tr.bounds.Min.Y.Floor(),
+tr.bounds.Max.X.Ceil(),
-tr.bounds.Max.Y.Ceil(),
))
return tr, nil
}
func (tr *textRenderer) addRune(r rune) bool {
dr, mask, maskp, advance, ok := tr.face.Glyph(
fixed.P(0, 0) /* subpixel destination location */, r)
if !ok {
return false
}
for i := 0; i < len(tr.buf.Pix); i++ {
tr.buf.Pix[i] = 0
}
// Copying, since there are absolutely no guarantees.
draw.Draw(tr.buf, dr, mask, maskp, draw.Src)
_ = render.AddGlyphs(tr.X, tr.gsid, 1, []uint32{uint32(r)},
[]render.Glyphinfo{{
Width: uint16(tr.buf.Rect.Size().X),
Height: uint16(tr.buf.Rect.Size().Y),
X: int16(-tr.bounds.Min.X.Floor()),
Y: int16(+tr.bounds.Max.Y.Ceil()),
XOff: int16(advance.Ceil()),
YOff: int16(0),
}}, []byte(tr.buf.Pix))
return true
}
func (tr *textRenderer) render(src, dst render.Picture,
srcX, srcY, destX, destY int16, text string) xgbCookie {
// XXX: You're really supposed to handle tabs differently from this.
text = strings.Replace(text, "\t", " ", -1)
for _, r := range text {
if !tr.loaded[r] {
tr.addRune(r)
tr.loaded[r] = true
}
}
return compositeString(tr.X, render.PictOpOver, src, dst,
0 /* TODO: mask Pictureformat? */, tr.gsid,
srcX, srcY, destX, destY, text)
}
// -----------------------------------------------------------------------------
// Keys
//go:generate sh -c "./gen-keysyms.sh > keysyms.go"
//go:generate sh -c "./gen-unicode-map.sh > unicode_map.go"
type keyMapper struct {
X *nexgb.Conn
setup *xproto.SetupInfo
mapping *xproto.GetKeyboardMappingReply
modeSwitchMask uint16
}
func newKeyMapper(X *nexgb.Conn) (*keyMapper, error) {
m := &keyMapper{X: X, setup: xproto.Setup(X)}
if err := m.update(); err != nil {
return nil, err
}
return m, nil
}
func (km *keyMapper) update() error {
var err error
km.mapping, err = xproto.GetKeyboardMapping(km.X, km.setup.MinKeycode,
byte(km.setup.MaxKeycode-km.setup.MinKeycode+1)).Reply()
if err != nil {
return err
}
km.modeSwitchMask = 0
// The order is "Shift, Lock, Control, Mod1, Mod2, Mod3, Mod4, and Mod5."
mm, err := xproto.GetModifierMapping(km.X).Reply()
if err != nil {
return err
}
perMod := int(mm.KeycodesPerModifier)
perKc := int(km.mapping.KeysymsPerKeycode)
for mod := 0; mod < 8; mod++ {
for _, kc := range mm.Keycodes[mod*perMod : (mod+1)*perMod] {
if kc == 0 {
continue
}
k := int(kc - km.setup.MinKeycode)
for _, ks := range km.mapping.Keysyms[k*perKc : (k+1)*perKc] {
if ks == XK_Mode_switch {
km.modeSwitchMask |= 1 << uint(mod)
}
}
}
}
return nil
}
func (km *keyMapper) decode(e xproto.KeyPressEvent) (result xproto.Keysym) {
step := int(km.mapping.KeysymsPerKeycode)
from := int(e.Detail-km.setup.MinKeycode) * step
ks := km.mapping.Keysyms[from : from+step]
// Strip trailing NoSymbol entries.
for len(ks) > 0 && ks[len(ks)-1] == 0 {
ks = ks[:len(ks)-1]
}
// Expand back to at least 4.
switch {
case len(ks) == 1:
ks = append(ks, 0, ks[0], 0)
case len(ks) == 2:
ks = append(ks, ks[0], ks[1])
case len(ks) == 3:
ks = append(ks, 0)
}
// Other silly expansion rules, only applied to basic ASCII since we
// don't have translation tables to Unicode here for brevity.
// TODO: Now that we have scripts for the mapping, finish this.
if ks[1] == 0 {
ks[1] = ks[0]
if ks[0] >= 'A' && ks[0] <= 'Z' ||
ks[0] >= 'a' && ks[0] <= 'z' {
ks[0] = ks[0] | 32
ks[1] = ks[0] &^ 32
}
}
if ks[3] == 0 {
ks[3] = ks[2]
if ks[2] >= 'A' && ks[2] <= 'Z' ||
ks[2] >= 'a' && ks[2] <= 'z' {
ks[2] = ks[2] | 32
ks[3] = ks[2] &^ 32
}
}
offset := 0
if e.State&km.modeSwitchMask != 0 {
offset += 2
}
shift := e.State&xproto.ModMaskShift != 0
lock := e.State&xproto.ModMaskLock != 0
switch {
case !shift && !lock:
result = ks[offset+0]
case !shift && lock:
if ks[offset+0] >= 'a' && ks[offset+0] <= 'z' {
result = ks[offset+1]
} else {
result = ks[offset+0]
}
case shift && lock:
if ks[offset+1] >= 'a' && ks[offset+1] <= 'z' {
result = ks[offset+1] &^ 32
} else {
result = ks[offset+1]
}
case shift:
result = ks[offset+1]
}
return
}
// -----------------------------------------------------------------------------
// Utilities
func getShell() string {
shell := os.Getenv("SHELL")
if shell == "" {
// TODO: Find out another way, os/user won't give it to us,
// easiest to just use Cgo again like it does internally.
shell = "/bin/sh"
}
return shell
}
// -----------------------------------------------------------------------------
// Program
//go:generate sh -c "./gen-rune-width.sh > rune_width.go"
type cell struct {
// TODO: We want to NFC-normalize the runes.
// https://blog.golang.org/normalization
runes []rune
// TODO: attributes
fg, bg color.Color
}
type row struct {
cells []cell
}
const (
winPadding = 5
)
var (
scrollback []row
scroll int // index of the top line
curX, curY int // cursor position
selX0, selY0 int // selection start
selX1, selY1 int // selection end
wid xproto.Window
winW, winH uint16 // inner window size
pid, blackid render.Picture
X *nexgb.Conn
setup *xproto.SetupInfo
screen *xproto.ScreenInfo
tr *textRenderer
km *keyMapper
)
func initFont() {
trOptions := truetype.Options{
Size: 10,
DPI: float64(screen.WidthInPixels) /
float64(screen.WidthInMillimeters) * 25.4,
Hinting: font.HintingFull,
}
// TODO: We will also want to make renderers for bold, italic, bold+italic.
var err error
tr, err = newTextRenderer(X, gomono.TTF, &trOptions)
if err != nil {
log.Fatalln(err)
}
}
func initWindow(name string) {
var err error
wid, err = xproto.NewWindowId(X)
if err != nil {
log.Fatalln(err)
}
// Compute window size according to our font.
cellW := tr.bounds.Max.X.Ceil() - tr.bounds.Min.X.Floor()
cellH := tr.bounds.Max.Y.Ceil() - tr.bounds.Min.Y.Floor()
winW = 80 * uint16(cellW)
winH = 24 * uint16(cellH)
_ = xproto.CreateWindow(X, screen.RootDepth, wid, screen.Root,
0, 0, winW, winH, 0, xproto.WindowClassInputOutput,
screen.RootVisual, xproto.CwBackPixel|xproto.CwEventMask,
[]uint32{0xffffffff, /* xproto.EventMaskButtonPress |
xproto.EventMaskButtonMotion | xproto.EventMaskButtonRelease | */
xproto.EventMaskStructureNotify | xproto.EventMaskExposure |
xproto.EventMaskKeyPress})
title := []byte(name)
_ = xproto.ChangeProperty(X, xproto.PropModeReplace, wid, xproto.AtomWmName,
xproto.AtomString, 8, uint32(len(title)), title)
_ = xproto.MapWindow(X, wid)
}
func initRender() {
pformats, err := render.QueryPictFormats(X).Reply()
if err != nil {
log.Fatalln(err)
}
var pformat render.Pictformat
VisualFormat:
for _, pd := range pformats.Screens[X.DefaultScreen].Depths {
for _, pv := range pd.Visuals {
if pv.Visual == screen.RootVisual {
pformat = pv.Format
break VisualFormat
}
}
}
if pid, err = render.NewPictureId(X); err != nil {
log.Fatalln(err)
}
_ = render.CreatePicture(X,
pid, xproto.Drawable(wid), pformat, 0, []uint32{})
if blackid, err = render.NewPictureId(X); err != nil {
log.Fatalln(err)
}
_ = render.CreateSolidFill(X, blackid, render.Color{Alpha: 0xffff})
}
func initKeyMapper() {
var err error
if km, err = newKeyMapper(X); err != nil {
log.Fatalln(err)
}
}
func redraw() {
y, ascent, step := winPadding, tr.bounds.Max.Y.Ceil(),
tr.bounds.Max.Y.Ceil()-tr.bounds.Min.Y.Floor()
for _, line := range scrollback[scroll:] {
if uint16(y) >= winH {
break
}
text := ""
for _, c := range line.cells {
for _, r := range c.runes {
text = text + string(r)
}
}
_ = tr.render(blackid, pid, 0, 0, winPadding, int16(y+ascent), text)
y += step
}
vis := float64(winH-2*winPadding) / float64(step)
if vis < float64(len(scrollback)) {
length := float64(step) * (vis + 1) * vis / float64(len(scrollback))
start := float64(step) * float64(scroll) * vis / float64(len(scrollback))
_ = render.FillRectangles(X, render.PictOpSrc, pid,
render.Color{Alpha: 0xffff}, []xproto.Rectangle{{
X: int16(winW - 2*winPadding), Y: int16(start),
Width: 2 * winPadding, Height: uint16(length + 2*winPadding)}})
}
}
func onResize(width, height uint16) {
winW, winH = width, height
// We should receive an ExposeEvent soon.
// TODO: Track the bottom of the virtual terminal screen.
// - Adjust the top line index, if necessary (screen got higher).
}
func write(r rune) {
// TODO: Write over the current scrollback position,
// with the current attributes.
// TODO: Make space in the scrollback so that we have somewhere to write to.
// TODO: Cause a redraw.
// - How to invalidate a region?
}
func onKeyPress(ks xproto.Keysym) {
switch ks {
case XK_Escape:
// Just as an example, this doesn't make sense anymore.
return
default:
if r := KeysymToRune(ks); r > 0 {
write(r)
}
}
}
func handleEvent(ev nexgb.Event) bool {
switch e := ev.(type) {
case xproto.UnmapNotifyEvent:
return false
case xproto.ConfigureNotifyEvent:
onResize(e.Width, e.Height)
case xproto.MappingNotifyEvent:
_ = km.update()
case xproto.KeyPressEvent:
onKeyPress(km.decode(e))
case xproto.ButtonPressEvent:
switch e.Detail {
case xproto.ButtonIndex4:
if scroll > 0 {
scroll--
}
case xproto.ButtonIndex5:
if scroll+1 < len(scrollback) {
scroll++
}
}
case xproto.ExposeEvent:
// TODO: If the count is non-zero, just add rectangles to
// a structure and flush it later. But maybe this doesn't make
// any sense anymore with today's compositing.
if e.Count == 0 {
redraw()
}
}
return true
}
func main() {
flag.Parse()
shell := getShell()
if flag.NArg() > 0 {
shell = flag.Arg(0)
}
var err error
if X, err = nexgb.NewConn(); err != nil {
log.Fatalln(err)
}
if err := render.Init(X); err != nil {
log.Fatalln(err)
}
setup = xproto.Setup(X)
screen = setup.DefaultScreen(X)
if screen.RootDepth < 24 {
log.Fatalln("need more colors")
}
initFont()
initWindow(shell)
initRender()
initKeyMapper()
// TODO: Set normal hints so that the window is resized in steps.
// https://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html
// xgbutils seems to contain code (WmNormalHintsSet)
// - The xgbutil.XUtil object is only used for xprop functions.
// - Seems like I want to import half of xgbutil.
//
// "Window managers are encouraged to use i and j instead of width and
// height in reporting window sizes to users"!
for {
ev, xerr := X.WaitForEvent()
if xerr != nil {
log.Printf("Error: %s\n", xerr)
return
}
if ev != nil && !handleEvent(ev) {
break
}
}
// TODO: Any post-close cleanup.
}
|