aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/go_event.go
blob: fe2a77e28edb0e2e6be989bad8a5fabb35b400cf (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
package main

// Event types
func (e *Event) Define(c *Context) {
	c.Putln("// Event definition %s (%d)", e.SrcName(), e.Number)
	c.Putln("// Size: %s", e.Size())
	c.Putln("")
	c.Putln("const %s = %d", e.SrcName(), e.Number)
	c.Putln("")
	c.Putln("type %s struct {", e.EvType())
	if !e.NoSequence {
		c.Putln("Sequence uint16")
	}
	for _, field := range e.Fields {
		field.Define(c)
	}
	c.Putln("}")
	c.Putln("")

	// Read defines a function that transforms a byte slice into this
	// event struct.
	e.Read(c)

	// Write defines a function that transforms this event struct into
	// a byte slice.
	e.Write(c)

	// Makes sure that this event type is an Event interface.
	c.Putln("func (v %s) ImplementsEvent() { }", e.EvType())
	c.Putln("")

	// Let's the XGB event loop read this event.
	c.Putln("func init() {")
	c.Putln("newEventFuncs[%d] = New%s", e.Number, e.EvType())
	c.Putln("}")
	c.Putln("")
}

func (e *Event) Read(c *Context) {
	c.Putln("// Event read %s", e.SrcName())
	c.Putln("func New%s(buf []byte) %s {", e.EvType(), e.EvType())
	c.Putln("v := %s{}", e.EvType())
	c.Putln("b := 1 // don't read event number")
	c.Putln("")
	for i, field := range e.Fields {
		if i == 1 && !e.NoSequence {
			c.Putln("v.Sequence = get16(buf[b:])")
			c.Putln("b += 2")
			c.Putln("")
		}
		field.Read(c)
		c.Putln("")
	}
	c.Putln("return v")
	c.Putln("}")
	c.Putln("")
}

func (e *Event) Write(c *Context) {
	c.Putln("// Event write %s", e.SrcName())
	c.Putln("func (v %s) Bytes() []byte {", e.EvType())
	c.Putln("buf := make([]byte, %s)", e.Size())
	c.Putln("b := 0")
	c.Putln("")
	c.Putln("// write event number")
	c.Putln("buf[b] = %d", e.Number)
	c.Putln("b += 1")
	c.Putln("")
	for i, field := range e.Fields {
		if i == 1 && !e.NoSequence {
			c.Putln("b += 2 // skip sequence number")
			c.Putln("")
		}
		field.Write(c)
		c.Putln("")
	}
	c.Putln("return buf")
	c.Putln("}")
	c.Putln("")
}

// EventCopy types
func (e *EventCopy) Define(c *Context) {
	c.Putln("// EventCopy definition %s (%d)", e.SrcName(), e.Number)
	c.Putln("")
	c.Putln("const %s = %d", e.SrcName(), e.Number)
	c.Putln("")
	c.Putln("type %s %s", e.EvType(), e.Old.(*Event).EvType())
	c.Putln("")

	// Read defines a function that transforms a byte slice into this
	// event struct.
	e.Read(c)

	// Write defines a function that transoforms this event struct into
	// a byte slice.
	e.Write(c)

	// Makes sure that this event type is an Event interface.
	c.Putln("func (v %s) ImplementsEvent() { }", e.EvType())
	c.Putln("")

	// Let's the XGB event loop read this event.
	c.Putln("func init() {")
	c.Putln("newEventFuncs[%d] = New%s", e.Number, e.EvType())
	c.Putln("}")
	c.Putln("")
}

func (e *EventCopy) Read(c *Context) {
	c.Putln("func New%s(buf []byte) %s {", e.EvType(), e.EvType())
	c.Putln("return %s(New%s(buf))", e.EvType(), e.Old.(*Event).EvType())
	c.Putln("}")
	c.Putln("")
}

func (e *EventCopy) Write(c *Context) {
	c.Putln("func (v %s) Bytes() []byte {", e.EvType())
	c.Putln("return %s(ev).Bytes()", e.Old.(*Event).EvType())
	c.Putln("}")
	c.Putln("")
}