aboutsummaryrefslogtreecommitdiff
path: root/prototypes/xgb-window.go
blob: 3944fa430775f72f86183eff8e3933e74b7dbfe7 (plain)
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
package main

import (
	"github.com/BurntSushi/xgb"
	"github.com/BurntSushi/xgb/xproto"
	"log"
	"math"
	"math/rand"
)

func main() {
	X, err := xgb.NewConn()
	if err != nil {
		log.Fatalln(err)
	}

	setup := xproto.Setup(X)
	screen := setup.DefaultScreen(X)

	var visual xproto.Visualid
	var depth byte
	for _, i := range screen.AllowedDepths {
		if i.Depth == 32 {
			// TODO: Could/should check other parameters.
			for _, v := range i.Visuals {
				if v.Class == xproto.VisualClassTrueColor {
					visual = v.VisualId
					depth = i.Depth
					break
				}
			}
		}
	}
	if visual == 0 {
		log.Fatalln("cannot find an RGBA TrueColor visual")
	}

	mid, err := xproto.NewColormapId(X)
	if err != nil {
		log.Fatalln(err)
	}

	_ = xproto.CreateColormap(
		X, xproto.ColormapAllocNone, mid, screen.Root, visual)

	wid, err := xproto.NewWindowId(X)
	if err != nil {
		log.Fatalln(err)
	}

	// Border pixel and colormap are required when depth differs from parent.
	_ = xproto.CreateWindow(X, depth, wid, screen.Root,
		0, 0, 500, 500, 0, xproto.WindowClassInputOutput,
		visual, xproto.CwBorderPixel|xproto.CwColormap,
		[]uint32{0, uint32(mid)})

	// This could be included in CreateWindow parameters.
	_ = xproto.ChangeWindowAttributes(X, wid,
		xproto.CwBackPixel|xproto.CwEventMask, []uint32{0x80808080,
			xproto.EventMaskStructureNotify | xproto.EventMaskKeyPress |
				xproto.EventMaskExposure})

	title := "Gradient"
	_ = xproto.ChangeProperty(X, xproto.PropModeReplace, wid, xproto.AtomWmName,
		xproto.AtomString, 8, uint32(len(title)), []byte(title))

	_ = xproto.MapWindow(X, wid)

	cid, err := xproto.NewGcontextId(X)
	if err != nil {
		log.Fatalln(err)
	}

	_ = xproto.CreateGC(X, cid, xproto.Drawable(wid),
		xproto.GcGraphicsExposures, []uint32{0})

	blend := func(a, b uint32, ratio, gamma float64) uint32 {
		iratio := 1 - ratio

		fa := math.Pow(float64(a)/255, gamma)
		fb := math.Pow(float64(b)/255, gamma)

		return uint32(math.Pow(ratio*fa+iratio*fb, 1/gamma)*255) & 0xff
	}

	// TODO: We could show some text just like we intend to with xgb-xrender.go.

	var w, h uint16
	var start, end uint32 = 0xabcdef, 0x32ab54
	gradient := func() {
		ra, ga, ba := (start>>16)&0xff, (start>>8)&0xff, start&0xff
		rb, gb, bb := (end>>16)&0xff, (end>>8)&0xff, end&0xff

		var low, high uint16 = 50, h - 50
		if high > h {
			return
		}

		for y := low; y < high; y++ {
			ratio := float64(y-low) / (float64(high) - float64(low))

			rR := blend(ra, rb, ratio, 2.2)
			gG := blend(ga, gb, ratio, 2.2)
			bB := blend(ba, bb, ratio, 2.2)

			_ = xproto.ChangeGC(X, cid, xproto.GcForeground,
				[]uint32{0xff000000 | rR<<16 | gG<<8 | bB})
			_ = xproto.PolyLine(X, xproto.CoordModeOrigin, xproto.Drawable(wid),
				cid, []xproto.Point{
					{X: 50, Y: int16(y)},
					{X: int16(w / 2), Y: int16(y)},
				})

			rR = blend(ra, rb, ratio, 1)
			gG = blend(ga, gb, ratio, 1)
			bB = blend(ba, bb, ratio, 1)

			_ = xproto.ChangeGC(X, cid, xproto.GcForeground,
				[]uint32{0xff000000 | rR<<16 | gG<<8 | bB})
			_ = xproto.PolyLine(X, xproto.CoordModeOrigin, xproto.Drawable(wid),
				cid, []xproto.Point{
					{X: int16(w / 2), Y: int16(y)},
					{X: int16(w - 50), Y: int16(y)},
				})
		}
	}

	for {
		ev, xerr := X.WaitForEvent()
		if xerr != nil {
			log.Printf("Error: %s\n", xerr)
			return
		}
		if ev == nil {
			return
		}

		log.Printf("Event: %s\n", ev)
		switch e := ev.(type) {
		case xproto.UnmapNotifyEvent:
			return

		case xproto.ConfigureNotifyEvent:
			w, h = e.Width, e.Height

		case xproto.KeyPressEvent:
			start = rand.Uint32() & 0xffffff
			end = rand.Uint32() & 0xffffff
			gradient()

		case xproto.ExposeEvent:
			gradient()
		}
	}

}