aboutsummaryrefslogtreecommitdiff
path: root/prototypes/xgb-draw.go
blob: 540783b9613565d1ea3e816029bfc9a2d5f080e3 (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
package main

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

func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) }
func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 }

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

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

	visual, depth := screen.RootVisual, screen.RootDepth
	if depth < 24 {
		log.Fatalln("need more colors")
	}

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

	_ = xproto.CreateWindow(X, depth, wid, screen.Root,
		0, 0, 500, 500, 0, xproto.WindowClassInputOutput,
		visual, xproto.CwBackPixel|xproto.CwEventMask,
		[]uint32{0xffffffff, xproto.EventMaskButtonPress |
			xproto.EventMaskButtonMotion | xproto.EventMaskButtonRelease |
			xproto.EventMaskStructureNotify})

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

	_ = xproto.MapWindow(X, wid)

	pformats, err := render.QueryPictFormats(X).Reply()
	if err != nil {
		log.Fatalln(err)
	}

	var pformat render.Pictformat
	for _, pd := range pformats.Screens[X.DefaultScreen].Depths {
		for _, pv := range pd.Visuals {
			if pv.Visual == visual {
				pformat = pv.Format
			}
		}
	}

	pid, err := render.NewPictureId(X)
	if err != nil {
		log.Fatalln(err)
	}
	render.CreatePicture(X, pid, xproto.Drawable(wid), pformat, 0, []uint32{})

	// Brush shape.
	brushid, err := render.NewPictureId(X)
	if err != nil {
		log.Fatalln(err)
	}

	cFull := render.Color{0xffff, 0xffff, 0xffff, 0xffff}
	cTrans := render.Color{0xffff, 0xffff, 0xffff, 0}
	_ = render.CreateRadialGradient(X, brushid,
		render.Pointfix{F64ToFixed(50), F64ToFixed(50)},
		render.Pointfix{F64ToFixed(50), F64ToFixed(50)},
		F64ToFixed(0),
		F64ToFixed(50),
		2, []render.Fixed{F64ToFixed(0), F64ToFixed(1)},
		[]render.Color{cFull, cTrans})

	// Brush color.
	colorid, err := render.NewPictureId(X)
	if err != nil {
		log.Fatalln(err)
	}
	_ = render.CreateSolidFill(X, colorid, render.Color{
		Red:   0xcccc,
		Green: 0xaaaa,
		Blue:  0x8888,
		Alpha: 0xffff,
	})

	// Unfortunately, XRender won't give us any /blending operators/, only
	// providing basic Porter-Duff ones with useless disjoint/conjoint variants,
	// but we need the "lighten" operator to draw brush strokes properly.
	//
	// https://www.cairographics.org/operators/
	// http://ssp.impulsetrain.com/porterduff.html
	printAt := func(x, y int16) {
		_ = render.Composite(X, render.PictOpOver,
			colorid, brushid, pid,
			0, 0, 0, 0, x-50, y-50, 100, 100)
	}

	drawing := false
	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.ButtonPressEvent:
			if e.Detail == xproto.ButtonIndex1 {
				drawing = true
				printAt(e.EventX, e.EventY)
			}

		case xproto.MotionNotifyEvent:
			if drawing {
				printAt(e.EventX, e.EventY)
			}

		case xproto.ButtonReleaseEvent:
			if e.Detail == xproto.ButtonIndex1 {
				drawing = false
			}
		}
	}
}