2012-04-29 05:25:57 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2013-08-12 02:42:36 +02:00
|
|
|
"sort"
|
2012-04-29 05:25:57 +02:00
|
|
|
)
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Context represents the protocol we're converting to Go, and a writer
|
|
|
|
// buffer to write the Go source to.
|
2012-04-29 05:25:57 +02:00
|
|
|
type Context struct {
|
2012-04-30 08:40:55 +02:00
|
|
|
protocol *Protocol
|
2012-04-30 08:44:31 +02:00
|
|
|
out *bytes.Buffer
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newContext() *Context {
|
|
|
|
return &Context{
|
|
|
|
out: bytes.NewBuffer([]byte{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Putln calls put and adds a new line to the end of 'format'.
|
|
|
|
func (c *Context) Putln(format string, v ...interface{}) {
|
2012-04-30 08:44:31 +02:00
|
|
|
c.Put(format+"\n", v...)
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put is a short alias to write to 'out'.
|
|
|
|
func (c *Context) Put(format string, v ...interface{}) {
|
|
|
|
_, err := c.out.WriteString(fmt.Sprintf(format, v...))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("There was an error writing to context buffer: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 08:40:55 +02:00
|
|
|
// Morph is the big daddy of them all. It takes in an XML byte slice,
|
|
|
|
// parse it, transforms the XML types into more usable types,
|
2012-04-29 05:25:57 +02:00
|
|
|
// and writes Go code to the 'out' buffer.
|
2012-04-30 08:40:55 +02:00
|
|
|
func (c *Context) Morph(xmlBytes []byte) {
|
|
|
|
parsedXml := &XML{}
|
|
|
|
err := xml.Unmarshal(xmlBytes, parsedXml)
|
2012-04-29 05:25:57 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse all imports
|
2012-04-30 08:40:55 +02:00
|
|
|
parsedXml.Imports.Eval()
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-04-30 08:40:55 +02:00
|
|
|
// Translate XML types to nice types
|
2012-05-10 23:01:42 +02:00
|
|
|
c.protocol = parsedXml.Translate(nil)
|
2018-09-21 08:35:18 +02:00
|
|
|
|
2017-01-18 09:53:26 +01:00
|
|
|
// For backwards compatibility we patch the type of the send_event field of
|
|
|
|
// PutImage to be byte
|
|
|
|
if c.protocol.Name == "shm" {
|
|
|
|
for _, req := range c.protocol.Requests {
|
|
|
|
if req.xmlName != "PutImage" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, ifield := range req.Fields {
|
|
|
|
field, ok := ifield.(*SingleField)
|
|
|
|
if !ok || field.xmlName != "send_event" {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-21 08:35:18 +02:00
|
|
|
field.Type = &Base{srcName: "byte", xmlName: "CARD8", size: newFixedSize(1, true)}
|
2017-01-18 09:53:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 15:09:23 +02:00
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
// Start with Go header.
|
2012-05-11 02:06:22 +02:00
|
|
|
c.Putln("// Package %s is the X client API for the %s extension.",
|
|
|
|
c.protocol.PkgName(), c.protocol.ExtXName)
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("package %s", c.protocol.PkgName())
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("")
|
2013-08-12 02:45:36 +02:00
|
|
|
c.Putln("// This file is automatically generated from %s.xml. "+
|
|
|
|
"Edit at your peril!", c.protocol.Name)
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("")
|
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// Write imports. We always need to import at least xgb.
|
|
|
|
// We also need to import xproto if it's an extension.
|
|
|
|
c.Putln("import (")
|
2018-09-08 19:57:27 +02:00
|
|
|
c.Putln("xgb \"janouch.name/haven/nexgb\"")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("")
|
|
|
|
if c.protocol.isExt() {
|
2018-09-08 19:57:27 +02:00
|
|
|
c.Putln("\"janouch.name/haven/nexgb/xproto\"")
|
2012-05-10 23:01:42 +02:00
|
|
|
}
|
2013-08-12 02:42:36 +02:00
|
|
|
|
|
|
|
sort.Sort(Protocols(c.protocol.Imports))
|
2012-05-10 23:01:42 +02:00
|
|
|
for _, imp := range c.protocol.Imports {
|
|
|
|
// We always import xproto, so skip it if it's explicitly imported
|
|
|
|
if imp.Name == "xproto" {
|
|
|
|
continue
|
2012-05-03 07:00:01 +02:00
|
|
|
}
|
2018-09-08 19:57:27 +02:00
|
|
|
c.Putln("\"janouch.name/haven/nexgb/%s\"", imp.Name)
|
2012-05-03 07:00:01 +02:00
|
|
|
}
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln(")")
|
|
|
|
c.Putln("")
|
2012-05-03 07:00:01 +02:00
|
|
|
|
2012-05-06 23:48:40 +02:00
|
|
|
// If this is an extension, create a function to initialize the extension
|
|
|
|
// before it can be used.
|
|
|
|
if c.protocol.isExt() {
|
|
|
|
xname := c.protocol.ExtXName
|
|
|
|
|
2018-09-21 08:37:21 +02:00
|
|
|
c.Putln("const (")
|
|
|
|
c.Putln("MajorVersion = %s", c.protocol.MajorVersion)
|
|
|
|
c.Putln("MinorVersion = %s", c.protocol.MinorVersion)
|
|
|
|
c.Putln(")")
|
|
|
|
c.Putln("")
|
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("// Init must be called before using the %s extension.",
|
|
|
|
xname)
|
|
|
|
c.Putln("func Init(c *xgb.Conn) error {")
|
|
|
|
c.Putln("reply, err := xproto.QueryExtension(c, %d, \"%s\").Reply()",
|
2012-05-06 23:48:40 +02:00
|
|
|
len(xname), xname)
|
|
|
|
c.Putln("switch {")
|
|
|
|
c.Putln("case err != nil:")
|
|
|
|
c.Putln("return err")
|
|
|
|
c.Putln("case !reply.Present:")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("return xgb.Errorf(\"No extension named %s could be found on "+
|
2012-05-06 23:48:40 +02:00
|
|
|
"on the server.\")", xname)
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
2016-03-01 15:41:38 +01:00
|
|
|
c.Putln("c.ExtLock.Lock()")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("c.Extensions[\"%s\"] = reply.MajorOpcode", xname)
|
2016-03-01 15:41:38 +01:00
|
|
|
c.Putln("c.ExtLock.Unlock()")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("for evNum, fun := range xgb.NewExtEventFuncs[\"%s\"] {",
|
|
|
|
xname)
|
|
|
|
c.Putln("xgb.NewEventFuncs[int(reply.FirstEvent) + evNum] = fun")
|
2012-05-06 23:48:40 +02:00
|
|
|
c.Putln("}")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("for errNum, fun := range xgb.NewExtErrorFuncs[\"%s\"] {",
|
|
|
|
xname)
|
|
|
|
c.Putln("xgb.NewErrorFuncs[int(reply.FirstError) + errNum] = fun")
|
2012-05-08 03:58:33 +02:00
|
|
|
c.Putln("}")
|
2012-05-06 23:48:40 +02:00
|
|
|
c.Putln("return nil")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
|
|
|
|
// Make sure newExtEventFuncs["EXT_NAME"] map is initialized.
|
2012-05-08 03:58:33 +02:00
|
|
|
// Same deal for newExtErrorFuncs["EXT_NAME"]
|
2012-05-06 23:48:40 +02:00
|
|
|
c.Putln("func init() {")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("xgb.NewExtEventFuncs[\"%s\"] = make(map[int]xgb.NewEventFun)",
|
|
|
|
xname)
|
|
|
|
c.Putln("xgb.NewExtErrorFuncs[\"%s\"] = make(map[int]xgb.NewErrorFun)",
|
|
|
|
xname)
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
} else {
|
|
|
|
// In the xproto package, we must provide a Setup function that uses
|
|
|
|
// SetupBytes in xgb.Conn to return a SetupInfo structure.
|
|
|
|
c.Putln("// Setup parses the setup bytes retrieved when")
|
|
|
|
c.Putln("// connecting into a SetupInfo struct.")
|
|
|
|
c.Putln("func Setup(c *xgb.Conn) *SetupInfo {")
|
|
|
|
c.Putln("setup := new(SetupInfo)")
|
|
|
|
c.Putln("SetupInfoRead(c.SetupBytes, setup)")
|
|
|
|
c.Putln("return setup")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("// DefaultScreen gets the default screen info from SetupInfo.")
|
|
|
|
c.Putln("func (s *SetupInfo) DefaultScreen(c *xgb.Conn) *ScreenInfo {")
|
|
|
|
c.Putln("return &s.Roots[c.DefaultScreen]")
|
2012-05-06 23:48:40 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
2012-04-30 08:40:55 +02:00
|
|
|
// Now write Go source code
|
2013-08-12 02:42:36 +02:00
|
|
|
sort.Sort(Types(c.protocol.Types))
|
|
|
|
sort.Sort(Requests(c.protocol.Requests))
|
2012-04-30 08:40:55 +02:00
|
|
|
for _, typ := range c.protocol.Types {
|
|
|
|
typ.Define(c)
|
|
|
|
}
|
2012-05-02 07:46:30 +02:00
|
|
|
for _, req := range c.protocol.Requests {
|
|
|
|
req.Define(c)
|
|
|
|
}
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|