From 52a21b415ad95b2c4649254447388cb329cee1a4 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Sat, 28 Apr 2012 23:25:57 -0400 Subject: initial commit. not currently in a working state. --- nexgb/xgbgen/xml.go | 298 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100644 nexgb/xgbgen/xml.go (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go new file mode 100644 index 0000000..0f632b4 --- /dev/null +++ b/nexgb/xgbgen/xml.go @@ -0,0 +1,298 @@ +package main + +import ( + "encoding/xml" + "io/ioutil" + "log" + "time" +) + +type XML struct { + // Root 'xcb' element properties. + XMLName xml.Name `xml:"xcb"` + Header string `xml:"header,attr"` + ExtensionXName string `xml:"extension-xname,attr"` + ExtensionName string `xml:"extension-name,attr"` + MajorVersion string `xml:"major-version,attr"` + MinorVersion string `xml:"minor-version,attr"` + + // Types for all top-level elements. + // First are the simple ones. + Imports Imports `xml:"import"` + Enums Enums `xml:"enum"` + Xids Xids `xml:"xidtype"` + XidUnions Xids `xml:"xidunion"` + TypeDefs TypeDefs `xml:"typedef"` + EventCopies EventCopies `xml:"eventcopy"` + ErrorCopies ErrorCopies `xml:"errorcopy"` + + // Here are the complex ones, i.e., anything with "structure contents" + Structs Structs `xml:"struct"` + Unions Unions `xml:"union"` + Requests Requests `xml:"request"` + Events Events `xml:"event"` + Errors Errors `xml:"error"` +} + +// Morph cascades down all of the XML and calls each type's corresponding +// Morph function with itself as an argument (the context). +func (x *XML) Morph(c *Context) { + // Start the header... + c.Putln("package xgb") + c.Putln("/*") + c.Putln("\tX protocol API for '%s.xml'.", c.xml.Header) + c.Putln("\tThis file is automatically generated. Edit at your own peril!") + c.Putln("\tGenerated on %s", + time.Now().Format("Jan 2, 2006 at 3:04:05pm MST")) + c.Putln("*/") + c.Putln("") + + x.Imports.Morph(c) + c.Putln("") + + x.Enums.Morph(c) + c.Putln("") + + x.Xids.Morph(c) + c.Putln("") + + x.XidUnions.Morph(c) + c.Putln("") + + x.TypeDefs.Morph(c) + c.Putln("") + + x.Structs.Morph(c) + c.Putln("") + + x.Unions.Morph(c) + c.Putln("") + + x.Requests.Morph(c) + c.Putln("") + + x.Events.Morph(c) + c.Putln("") + + x.Errors.Morph(c) + c.Putln("") + + x.EventCopies.Morph(c) + c.Putln("") + + x.ErrorCopies.Morph(c) + c.Putln("") +} + +// IsResource returns true if the 'needle' type is a resource type. +// i.e., an "xid" +func (x *XML) IsResource(needle Type) bool { + for _, xid := range x.Xids { + if needle == xid.Name { + return true + } + } + for _, xidunion := range x.XidUnions { + if needle == xidunion.Name { + return true + } + } + for _, imp := range x.Imports { + if imp.xml.IsResource(needle) { + return true + } + } + return false +} + +// HasType returns true if the 'needle' type can be found in the protocol +// description represented by 'x'. +func (x *XML) HasType(needle Type) bool { + for _, enum := range x.Enums { + if needle == enum.Name { + return true + } + } + for _, xid := range x.Xids { + if needle == xid.Name { + return true + } + } + for _, xidunion := range x.XidUnions { + if needle == xidunion.Name { + return true + } + } + for _, typedef := range x.TypeDefs { + if needle == typedef.New { + return true + } + } + for _, evcopy := range x.EventCopies { + if needle == evcopy.Name { + return true + } + } + for _, errcopy := range x.ErrorCopies { + if needle == errcopy.Name { + return true + } + } + for _, strct := range x.Structs { + if needle == strct.Name { + return true + } + } + for _, union := range x.Unions { + if needle == union.Name { + return true + } + } + for _, ev := range x.Events { + if needle == ev.Name { + return true + } + } + for _, err := range x.Errors { + if needle == err.Name { + return true + } + } + + return false +} + +type Name string + +type Type string + +type Imports []*Import + +func (imports Imports) Eval() { + for _, imp := range imports { + xmlBytes, err := ioutil.ReadFile(*protoPath + "/" + imp.Name + ".xml") + if err != nil { + log.Fatalf("Could not read X protocol description for import " + + "'%s' because: %s", imp.Name, err) + } + + imp.xml = &XML{} + err = xml.Unmarshal(xmlBytes, imp.xml) + if err != nil { + log.Fatal("Could not parse X protocol description for import " + + "'%s' because: %s", imp.Name, err) + } + } +} + +type Import struct { + Name string `xml:",chardata"` + xml *XML `xml:"-"` +} + +type Enums []Enum + +// Eval on the list of all enum types goes through and forces every enum +// item to have a valid expression. +// This is necessary because when an item is empty, it is defined to have +// the value of "one more than that of the previous item, or 0 for the first +// item". +func (enums Enums) Eval() { + for _, enum := range enums { + nextValue := uint(0) + for _, item := range enum.Items { + if item.Expr == nil { + item.Expr = newValueExpression(nextValue) + nextValue++ + } else { + nextValue = item.Expr.Eval() + 1 + } + } + } +} + +type Enum struct { + Name Type `xml:"name,attr"` + Items []*EnumItem `xml:"item"` +} + +type EnumItem struct { + Name Name `xml:"name,attr"` + Expr *Expression `xml:",any"` +} + +type Xids []*Xid + +type Xid struct { + XMLName xml.Name + Name Type `xml:"name,attr"` +} + +type TypeDefs []*TypeDef + +type TypeDef struct { + Old Type `xml:"oldname,attr"` + New Type `xml:"newname,attr"` +} + +type EventCopies []*EventCopy + +type EventCopy struct { + Name Type `xml:"name,attr"` + Number string `xml:"number,attr"` + Ref Type `xml:"ref,attr"` +} + +type ErrorCopies []*ErrorCopy + +type ErrorCopy struct { + Name Type `xml:"name,attr"` + Number string `xml:"number,attr"` + Ref Type `xml:"ref,attr"` +} + +type Structs []*Struct + +type Struct struct { + Name Type `xml:"name,attr"` + Fields []*Field `xml:",any"` +} + +type Unions []*Union + +type Union struct { + Name Type `xml:"name,attr"` + Fields []*Field `xml:",any"` +} + +type Requests []*Request + +type Request struct { + Name Type `xml:"name,attr"` + Opcode int `xml:"opcode,attr"` + Combine bool `xml:"combine-adjacent,attr"` + Fields []*Field `xml:",any"` + Reply *Reply `xml:"reply"` +} + +type Reply struct { + Fields []*Field `xml:",any"` +} + +type Events []*Event + +type Event struct { + Name Type `xml:"name,attr"` + Number int `xml:"number,attr"` + NoSequence bool `xml:"no-sequence-number,true"` + Fields []*Field `xml:",any"` +} + +type Errors []*Error + +type Error struct { + Name Type `xml:"name,attr"` + Number int `xml:"number,attr"` + Fields []*Field `xml:",any"` +} + -- cgit v1.2.3-70-g09d2 From 6bf0191fb01f4c0b65bcd444bb5381013c627f95 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Sun, 29 Apr 2012 03:38:29 -0400 Subject: progress. still not working. this is incredibly difficult. --- nexgb/.gitignore | 2 + nexgb/xgb.go | 22 +++- nexgb/xgb_help.go | 32 ++--- nexgb/xgbgen/context.go | 36 ------ nexgb/xgbgen/go.go | 291 ++++++++++++++++++++++++++++++++++++++++++++- nexgb/xgbgen/main.go | 1 + nexgb/xgbgen/misc.go | 9 +- nexgb/xgbgen/xgbgen | Bin 2318165 -> 0 bytes nexgb/xgbgen/xml.go | 56 +++++++-- nexgb/xgbgen/xml_fields.go | 14 ++- 10 files changed, 384 insertions(+), 79 deletions(-) create mode 100644 nexgb/.gitignore delete mode 100755 nexgb/xgbgen/xgbgen (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/.gitignore b/nexgb/.gitignore new file mode 100644 index 0000000..1c0248d --- /dev/null +++ b/nexgb/.gitignore @@ -0,0 +1,2 @@ +xgbgen +.*.swp diff --git a/nexgb/xgb.go b/nexgb/xgb.go index 7e209a7..ce2cc54 100644 --- a/nexgb/xgb.go +++ b/nexgb/xgb.go @@ -87,9 +87,15 @@ func newCookie(id uint16) *Cookie { } } -// Event is an interface that can contain any of the events returned by the server. -// Use a type assertion switch to extract the Event structs. -type Event interface{} +// Event is an interface that can contain any of the events returned by the +// server. Use a type assertion switch to extract the Event structs. +type Event interface { + ImplementsEvent() +} + +// newEventFuncs is a map from event numbers to functions that create +// the corresponding event. +var newEventFuncs map[int]func(buf []byte) Event // Error contains protocol errors returned to us by the X server. type Error struct { @@ -100,6 +106,16 @@ type Error struct { Id Id } +// Error2 is an interface that can contain any of the errors returned by +// the server. Use a type assertion switch to extract the Error structs. +type Error2 interface { + ImplementsError() +} + +// newErrorFuncs is a map from error numbers to functions that create +// the corresponding error. +var newErrorFuncs map[int]func(buf []byte) Error2 + func (e *Error) Error() string { return fmt.Sprintf("Bad%s (major=%d minor=%d cookie=%d id=0x%x)", errorNames[e.Detail], e.Major, e.Minor, e.Cookie, e.Id) diff --git a/nexgb/xgb_help.go b/nexgb/xgb_help.go index adb97e0..acb35de 100644 --- a/nexgb/xgb_help.go +++ b/nexgb/xgb_help.go @@ -85,19 +85,19 @@ func (c *Conn) DefaultScreen() *ScreenInfo { return &c.Setup.Roots[c.defaultScre // ClientMessageData holds the data from a client message, // duplicated in three forms because Go doesn't have unions. -type ClientMessageData struct { - Data8 [20]byte - Data16 [10]uint16 - Data32 [5]uint32 -} - -func getClientMessageData(b []byte, v *ClientMessageData) int { - copy(v.Data8[:], b) - for i := 0; i < 10; i++ { - v.Data16[i] = get16(b[i*2:]) - } - for i := 0; i < 5; i++ { - v.Data32[i] = get32(b[i*4:]) - } - return 20 -} +// type ClientMessageData struct { + // Data8 [20]byte + // Data16 [10]uint16 + // Data32 [5]uint32 +// } +// +// func getClientMessageData(b []byte, v *ClientMessageData) int { + // copy(v.Data8[:], b) + // for i := 0; i < 10; i++ { + // v.Data16[i] = get16(b[i*2:]) + // } + // for i := 0; i < 5; i++ { + // v.Data32[i] = get32(b[i*4:]) + // } + // return 20 +// } diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go index e5acb12..712cad4 100644 --- a/nexgb/xgbgen/context.go +++ b/nexgb/xgbgen/context.go @@ -5,7 +5,6 @@ import ( "encoding/xml" "fmt" "log" - "strings" ) type Context struct { @@ -33,31 +32,6 @@ func (c *Context) Put(format string, v ...interface{}) { } } -// TypePrefix searches the parsed XML for a type matching 'needle'. -// It then returns the appropriate prefix to be used in source code. -// Note that the core X protocol *is* a namespace, but does not have a prefix. -// Also note that you should probably check the BaseTypeMap and TypeMap -// before calling this function. -func (c *Context) TypePrefix(needle Type) string { - // If this is xproto, quit. No prefixes needed. - if c.xml.Header == "xproto" { - return "" - } - - // First check for the type in the current namespace. - if c.xml.HasType(needle) { - return strings.Title(c.xml.Header) - } - - // Now check each of the imports... - for _, imp := range c.xml.Imports { - if imp.xml.Header != "xproto" && imp.xml.HasType(needle) { - return strings.Title(imp.xml.Header) - } - } - - return "" -} // Translate is the big daddy of them all. It takes in an XML byte slice // and writes Go code to the 'out' buffer. @@ -76,14 +50,4 @@ func (c *Context) Translate(xmlBytes []byte) { // It's Morphin' Time! c.xml.Morph(c) - - // for _, req := range c.xml.Requests { - // if req.Name != "CreateContext" && req.Name != "MakeCurrent" { - // continue - // } - // log.Println(req.Name) - // for _, field := range req.Fields { - // log.Println("\t", field.XMLName.Local, field.Type.Morph(c)) - // } - // } } diff --git a/nexgb/xgbgen/go.go b/nexgb/xgbgen/go.go index eb3f0fb..bfc54dd 100644 --- a/nexgb/xgbgen/go.go +++ b/nexgb/xgbgen/go.go @@ -8,12 +8,22 @@ package main * Imports and helper variables. * Manual type and name override maps. + * Constants for tweaking various morphing functions. * Helper morphing functions. + * Morphing functions for each "sub-unit." * Morphing functions for each "unit". * Morphing functions for collections of "units". + + Units can be thought of as the top-level elements in an XML protocol + description file. Namely, structs, xidtypes, imports, enums, unions, etc. + Collections of units are simply "all of the UNIT in the XML file." + Sub-units can be thought of as recurring bits like struct contents (which + is used in events, replies, requests, errors, etc.) and expression + evaluation. */ import ( + "log" "strings" ) @@ -38,6 +48,24 @@ var BaseTypeMap = map[string]string{ "BOOL": "bool", "float": "float64", "double": "float64", + "char": "byte", +} + +// BaseTypeSizes should have precisely the same keys as in BaseTypeMap, +// and the values should correspond to the size of the type in bytes. +var BaseTypeSizes = map[string]uint{ + "CARD8": 1, + "CARD16": 2, + "CARD32": 4, + "INT8": 1, + "INT16": 2, + "INT32": 4, + "BYTE": 1, + "BOOL": 1, + "float": 4, + "double": 8, + "char": 1, + "Id": 4, } // TypeMap is a map from types in the XML to type names that is used @@ -54,12 +82,26 @@ var TypeMap = map[string]string{ var NameMap = map[string]string{ } /******************************************************************************/ -// Helper functions that aide in morphing repetive constructs. -// i.e., "structure contents", expressions, type and identifier names, etc. +// Constants for changing the semantics of morphing functions. +// These are mainly used to tweaking the writing of fields. +// Namely, reading/writing is not exactly the same across events, +// requests/replies and errors. +/******************************************************************************/ +const ( + FieldsEvent = iota + FieldsRequestReply + FieldsError +) + +/******************************************************************************/ +// Helper functions that aide in morphing repetitive constructs. +// i.e., type and identifier names, etc. /******************************************************************************/ // Morph changes every TYPE (not names) into something suitable -// for your language. +// for your language. It also handles adding suffixes like 'Event' +// and 'Union'. (A 'Union' suffix is used in Go because unions aren't +// supported at the language level.) func (typ Type) Morph(c *Context) string { t := string(typ) @@ -87,7 +129,33 @@ func (typ Type) Morph(c *Context) string { // Since there is no namespace, we need to look for a namespace // in the current context. - return c.TypePrefix(typ) + splitAndTitle(t) + return typ.Prefix(c) + splitAndTitle(t) +} + +// Prefix searches the parsed XML for a type matching 'typ'. +// It then returns the appropriate prefix to be used in source code. +// Note that the core X protocol *is* a namespace, but does not have a prefix. +// Also note that you should probably check the BaseTypeMap and TypeMap +// before calling this function. +func (typ Type) Prefix(c *Context) string { + // If this is xproto, quit. No prefixes needed. + if c.xml.Header == "xproto" { + return "" + } + + // First check for the type in the current namespace. + if c.xml.HasType(typ) { + return strings.Title(c.xml.Header) + } + + // Now check each of the imports... + for _, imp := range c.xml.Imports { + if imp.xml.Header != "xproto" && imp.xml.HasType(typ) { + return strings.Title(imp.xml.Header) + } + } + + return "" } // Morph changes every identifier (NOT type) into something suitable @@ -103,6 +171,147 @@ func (name Name) Morph(c *Context) string { return splitAndTitle(n) } +/******************************************************************************/ +// Sub-unit morphing. +// Below are functions that morph sub-units. Like collections of fields, +// expressions, etc. +// Note that collections of fields can be used in three different contexts: +// definitions, reading from the wire and writing to the wire. Thus, there +// exists 'MorphDefine', 'MorphRead', 'MorphWrite' defined on Fields. +/******************************************************************************/ +func (fields Fields) MorphDefine(c *Context) { + for _, field := range fields { + field.MorphDefine(c) + } +} + +func (field *Field) MorphDefine(c *Context) { + // We omit 'pad' and 'exprfield' + switch field.XMLName.Local { + case "field": + c.Putln("%s %s", field.Name.Morph(c), field.Type.Morph(c)) + case "list": + c.Putln("%s []%s", field.Name.Morph(c), field.Type.Morph(c)) + case "localfield": + c.Putln("%s %s", field.Name.Morph(c), field.Type.Morph(c)) + case "valueparam": + c.Putln("%s %s", field.ValueMaskName.Morph(c), + field.ValueMaskType.Morph(c)) + c.Putln("%s []%s", field.ValueListName.Morph(c), + field.ValueMaskType.Morph(c)) + case "switch": + field.Bitcases.MorphDefine(c) + } +} + +func (bitcases Bitcases) MorphDefine(c *Context) { + for _, bitcase := range bitcases { + bitcase.MorphDefine(c) + } +} + +func (bitcase *Bitcase) MorphDefine(c *Context) { + bitcase.Fields.MorphDefine(c) +} + +func (fields Fields) MorphRead(c *Context, kind int, evNoSeq bool) { + var nextByte uint + + switch kind { + case FieldsEvent: + nextByte = 1 + } + + for _, field := range fields { + nextByte = field.MorphRead(c, kind, nextByte) + switch kind { + case FieldsEvent: + // Skip the sequence id + if !evNoSeq && (nextByte == 2 || nextByte == 3) { + nextByte = 4 + } + } + } +} + +func (field *Field) MorphRead(c *Context, kind int, byt uint) uint { + consumed := uint(0) + switch field.XMLName.Local { + case "pad": + consumed = uint(field.Bytes) + case "field": + if field.Type == "ClientMessageData" { + break + } + size := field.Type.Size(c) + typ := field.Type.Morph(c) + name := field.Name.Morph(c) + _, isBase := BaseTypeMap[string(field.Type)] + + c.Put("v.%s = ", name) + if !isBase { + c.Put("%s(", typ) + } + switch size { + case 1: c.Put("buf[%d]", byt) + case 2: c.Put("get16(buf[%d:])", byt) + case 4: c.Put("get32(buf[%d:])", byt) + case 8: c.Put("get64(buf[%d:])", byt) + default: + log.Fatalf("Unsupported field size '%d' for field '%s'.", + size, field) + } + if !isBase { + c.Put(")") + } + c.Putln("") + + consumed = size + case "list": + c.Putln("") + } + return byt + consumed +} + +func (fields Fields) MorphWrite(c *Context, kind int) { + var nextByte uint + + switch kind { + case FieldsEvent: + nextByte = 1 + } + + for _, field := range fields { + nextByte = field.MorphWrite(c, kind, nextByte) + } +} + +func (field *Field) MorphWrite(c *Context, kind int, byt uint) uint { + consumed := uint(0) + switch field.XMLName.Local { + case "pad": + consumed = uint(field.Bytes) + case "field": + size := field.Type.Size(c) + typ := field.Type.Morph(c) + name := field.Name.Morph(c) + switch size { + case 1: + c.Putln("v.%s = %s(buf[%d])", name, typ, byt) + case 2: + c.Putln("v.%s = %s(get16(buf[%d:]))", name, typ, byt) + case 4: + c.Putln("v.%s = %s(get32(buf[%d:]))", name, typ, byt) + case 8: + c.Putln("v.%s = %s(get64(buf[%d:]))", name, typ, byt) + } + consumed = size + case "list": + c.Putln("IDK") + } + return byt + consumed +} + /******************************************************************************/ // Per element morphing. // Below are functions that morph a single unit. @@ -133,15 +342,23 @@ func (xid *Xid) Morph(c *Context) { // TypeDef morphing. func (typedef *TypeDef) Morph(c *Context) { - c.Putln("type %s %s", typedef.Old.Morph(c), typedef.New.Morph(c)) + c.Putln("type %s %s", typedef.New.Morph(c), typedef.Old.Morph(c)) } // Struct morphing. func (strct *Struct) Morph(c *Context) { + c.Putln("type %s struct {", strct.Name.Morph(c)) + strct.Fields.MorphDefine(c) + c.Putln("}") + c.Putln("\n") } // Union morphing. func (union *Union) Morph(c *Context) { + c.Putln("type %s struct {", union.Name.Morph(c)) + union.Fields.MorphDefine(c) + c.Putln("}") + c.Putln("\n") } // Request morphing. @@ -150,10 +367,54 @@ func (request *Request) Morph(c *Context) { // Event morphing. func (ev *Event) Morph(c *Context) { + name := ev.Name.Morph(c) + + c.Putln("const %s = %d", name, ev.Number) + c.Putln("") + c.Putln("type %sEvent struct {", name) + ev.Fields.MorphDefine(c) + c.Putln("}") + c.Putln("") + c.Putln("func New%s(buf []byte) %sEvent {", name, name) + c.Putln("var v %sEvent", name) + ev.Fields.MorphRead(c, FieldsEvent, ev.NoSequence) + c.Putln("return v") + c.Putln("}") + c.Putln("") + c.Putln("func (err %sEvent) ImplementsEvent() { }", name) + c.Putln("") + c.Putln("func (ev %sEvent) Bytes() []byte {", name) + // ev.Fields.MorphWrite(c, FieldsEvent) + c.Putln("}") + c.Putln("") + c.Putln("func init() {") + c.Putln("newEventFuncs[%d] = New%s", ev.Number, name) + c.Putln("}") + c.Putln("") } // EventCopy morphing. func (evcopy *EventCopy) Morph(c *Context) { + oldName, newName := evcopy.Ref.Morph(c), evcopy.Name.Morph(c) + + c.Putln("const %s = %d", newName, evcopy.Number) + c.Putln("") + c.Putln("type %sEvent %sEvent", newName, oldName) + c.Putln("") + c.Putln("func New%s(buf []byte) %sEvent {", newName, newName) + c.Putln("return (%sEvent)(New%s(buf))", newName, oldName) + c.Putln("}") + c.Putln("") + c.Putln("func (err %sEvent) ImplementsEvent() { }", newName) + c.Putln("") + c.Putln("func (ev %sEvent) Bytes() []byte {", newName) + c.Putln("return (%sEvent)(ev).Bytes()", oldName) + c.Putln("}") + c.Putln("") + c.Putln("func init() {") + c.Putln("newEventFuncs[%d] = New%s", evcopy.Number, newName) + c.Putln("}") + c.Putln("") } // Error morphing. @@ -162,6 +423,26 @@ func (err *Error) Morph(c *Context) { // ErrorCopy morphing. func (errcopy *ErrorCopy) Morph(c *Context) { + oldName, newName := errcopy.Ref.Morph(c), errcopy.Name.Morph(c) + + c.Putln("const Bad%s = %d", newName, errcopy.Number) + c.Putln("") + c.Putln("type %sError %sError", newName, oldName) + c.Putln("") + c.Putln("func New%sError(buf []byte) %sError {", newName, newName) + c.Putln("return (%sError)(New%sError(buf))", newName, oldName) + c.Putln("}") + c.Putln("") + c.Putln("func (err %sError) ImplementsError() { }", newName) + c.Putln("") + c.Putln("func (err %sError) Bytes() []byte {", newName) + c.Putln("return (%sError)(err).Bytes()", oldName) + c.Putln("}") + c.Putln("") + c.Putln("func init() {") + c.Putln("newErrorFuncs[%d] = New%sError", errcopy.Number, newName) + c.Putln("}") + c.Putln("") } /******************************************************************************/ diff --git a/nexgb/xgbgen/main.go b/nexgb/xgbgen/main.go index 69579a4..c69c8aa 100644 --- a/nexgb/xgbgen/main.go +++ b/nexgb/xgbgen/main.go @@ -55,6 +55,7 @@ func main() { cmdGofmt := exec.Command("gofmt") cmdGofmt.Stdin = c.out cmdGofmt.Stdout = os.Stdout + cmdGofmt.Stderr = os.Stderr err = cmdGofmt.Run() if err != nil { log.Fatal(err) diff --git a/nexgb/xgbgen/misc.go b/nexgb/xgbgen/misc.go index 9adcf5d..13c4cc2 100644 --- a/nexgb/xgbgen/misc.go +++ b/nexgb/xgbgen/misc.go @@ -21,6 +21,11 @@ func popCount(mask uint) uint { return n } +// pad makes sure 'n' aligns on 4 bytes. +func pad(n int) int { + return (n + 3) & ^3 +} + // splitAndTitle takes a string, splits it by underscores, capitalizes the // first letter of each chunk, and smushes'em back together. func splitAndTitle(s string) string { @@ -29,9 +34,9 @@ func splitAndTitle(s string) string { return strings.Title(strings.ToLower(s)) } - // If the string has no underscores, leave it be. + // If the string has no underscores, capitalize it and leave it be. if i := strings.Index(s, "_"); i == -1 { - return s + return strings.Title(s) } // Now split the name at underscores, capitalize the first diff --git a/nexgb/xgbgen/xgbgen b/nexgb/xgbgen/xgbgen deleted file mode 100755 index ef33abc..0000000 Binary files a/nexgb/xgbgen/xgbgen and /dev/null differ diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go index 0f632b4..12932b2 100644 --- a/nexgb/xgbgen/xml.go +++ b/nexgb/xgbgen/xml.go @@ -71,16 +71,16 @@ func (x *XML) Morph(c *Context) { x.Requests.Morph(c) c.Putln("") - x.Events.Morph(c) + x.Errors.Morph(c) c.Putln("") - x.Errors.Morph(c) + x.ErrorCopies.Morph(c) c.Putln("") - x.EventCopies.Morph(c) + x.Events.Morph(c) c.Putln("") - x.ErrorCopies.Morph(c) + x.EventCopies.Morph(c) c.Putln("") } @@ -166,6 +166,38 @@ type Name string type Type string +// Size is a nifty function that takes any type and digs until it finds +// its underlying base type. At which point, the size can be determined. +func (typ Type) Size(c *Context) uint { + // If this is a base type, we're done. + if size, ok := BaseTypeSizes[string(typ)]; ok { + return size + } + + // If it's a resource, we're also done. + if c.xml.IsResource(typ) { + return BaseTypeSizes["Id"] + } + + // It's not, so that implies there is *some* typedef declaring it + // in terms of another type. Just follow that chain until we get to + // a base type. We also need to check imported stuff. + for _, typedef := range c.xml.TypeDefs { + if typ == typedef.New { + return typedef.Old.Size(c) + } + } + for _, imp := range c.xml.Imports { + for _, typedef := range imp.xml.TypeDefs { + if typ == typedef.New { + return typedef.Old.Size(c) + } + } + } + log.Panicf("Could not find base size of type '%s'.", typ) + panic("unreachable") +} + type Imports []*Import func (imports Imports) Eval() { @@ -239,7 +271,7 @@ type EventCopies []*EventCopy type EventCopy struct { Name Type `xml:"name,attr"` - Number string `xml:"number,attr"` + Number int `xml:"number,attr"` Ref Type `xml:"ref,attr"` } @@ -247,7 +279,7 @@ type ErrorCopies []*ErrorCopy type ErrorCopy struct { Name Type `xml:"name,attr"` - Number string `xml:"number,attr"` + Number int `xml:"number,attr"` Ref Type `xml:"ref,attr"` } @@ -255,14 +287,14 @@ type Structs []*Struct type Struct struct { Name Type `xml:"name,attr"` - Fields []*Field `xml:",any"` + Fields Fields `xml:",any"` } type Unions []*Union type Union struct { Name Type `xml:"name,attr"` - Fields []*Field `xml:",any"` + Fields Fields `xml:",any"` } type Requests []*Request @@ -271,12 +303,12 @@ type Request struct { Name Type `xml:"name,attr"` Opcode int `xml:"opcode,attr"` Combine bool `xml:"combine-adjacent,attr"` - Fields []*Field `xml:",any"` + Fields Fields `xml:",any"` Reply *Reply `xml:"reply"` } type Reply struct { - Fields []*Field `xml:",any"` + Fields Fields `xml:",any"` } type Events []*Event @@ -285,7 +317,7 @@ type Event struct { Name Type `xml:"name,attr"` Number int `xml:"number,attr"` NoSequence bool `xml:"no-sequence-number,true"` - Fields []*Field `xml:",any"` + Fields Fields `xml:",any"` } type Errors []*Error @@ -293,6 +325,6 @@ type Errors []*Error type Error struct { Name Type `xml:"name,attr"` Number int `xml:"number,attr"` - Fields []*Field `xml:",any"` + Fields Fields `xml:",any"` } diff --git a/nexgb/xgbgen/xml_fields.go b/nexgb/xgbgen/xml_fields.go index 18be6e3..c1a7240 100644 --- a/nexgb/xgbgen/xml_fields.go +++ b/nexgb/xgbgen/xml_fields.go @@ -23,6 +23,8 @@ import ( "strings" ) +type Fields []*Field + type Field struct { XMLName xml.Name @@ -30,7 +32,7 @@ type Field struct { Bytes int `xml:"bytes,attr"` // For 'field', 'list', 'localfield', 'exprfield' and 'switch' elements. - Name string `xml:"name,attr"` + Name Name `xml:"name,attr"` // For 'field', 'list', 'localfield', and 'exprfield' elements. Type Type `xml:"type,attr"` @@ -40,11 +42,11 @@ type Field struct { // For 'valueparm' element. ValueMaskType Type `xml:"value-mask-type,attr"` - ValueMaskName string `xml:"value-mask-name,attr"` - ValueListName string `xml:"value-list-name,attr"` + ValueMaskName Name `xml:"value-mask-name,attr"` + ValueListName Name `xml:"value-list-name,attr"` // For 'switch' element. - Bitcases []*Bitcase `xml:"bitcase"` + Bitcases Bitcases `xml:"bitcase"` // I don't know which elements these are for. The documentation is vague. // They also seem to be completely optional. @@ -86,6 +88,8 @@ func (f *Field) String() string { panic("unreachable") } +type Bitcases []*Bitcase + // Bitcase represents a single expression followed by any number of fields. // Namely, if the switch's expression (all bitcases are inside a switch), // and'd with the bitcase's expression is equal to the bitcase expression, @@ -95,7 +99,7 @@ func (f *Field) String() string { // it's the closest thing to a Union I can get to in Go without interfaces. // Would an '' tag have been too much to ask? :-( type Bitcase struct { - Fields []*Field `xml:",any"` + Fields Fields `xml:",any"` // All the different expressions. // When it comes time to choose one, use the 'Expr' method. -- cgit v1.2.3-70-g09d2 From 3115c13e88badfd3b6b1762f2239edbf9d0b8951 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Sun, 29 Apr 2012 14:09:03 -0400 Subject: last commit before i tear everything down --- nexgb/.gitignore | 2 +- nexgb/xgbgen/COPYING | 13 ++++++ nexgb/xgbgen/bufcount.go | 15 +++++++ nexgb/xgbgen/go.go | 102 ++++++++++++++++++++++++++++++----------------- nexgb/xgbgen/xml.go | 42 +++++++++++++++++++ 5 files changed, 136 insertions(+), 38 deletions(-) create mode 100644 nexgb/xgbgen/COPYING create mode 100644 nexgb/xgbgen/bufcount.go (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/.gitignore b/nexgb/.gitignore index 1c0248d..179f830 100644 --- a/nexgb/.gitignore +++ b/nexgb/.gitignore @@ -1,2 +1,2 @@ -xgbgen +xgbgen/xgbgen .*.swp diff --git a/nexgb/xgbgen/COPYING b/nexgb/xgbgen/COPYING new file mode 100644 index 0000000..5c93f45 --- /dev/null +++ b/nexgb/xgbgen/COPYING @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/nexgb/xgbgen/bufcount.go b/nexgb/xgbgen/bufcount.go new file mode 100644 index 0000000..c3a5645 --- /dev/null +++ b/nexgb/xgbgen/bufcount.go @@ -0,0 +1,15 @@ +package main + +/* + A buffer count is a mechanism by which to keep track of which byte one + is reading or writing to/from the wire. + + It's an abstraction over the fact that while such a counter is usually + fixed, it can be made variable based on values at run-time. +*/ + +type BufCount struct { + Fixed int + Exprs []*Expression +} + diff --git a/nexgb/xgbgen/go.go b/nexgb/xgbgen/go.go index bfc54dd..b939b3a 100644 --- a/nexgb/xgbgen/go.go +++ b/nexgb/xgbgen/go.go @@ -23,6 +23,7 @@ package main */ import ( + "fmt" "log" "strings" ) @@ -214,16 +215,12 @@ func (bitcase *Bitcase) MorphDefine(c *Context) { bitcase.Fields.MorphDefine(c) } -func (fields Fields) MorphRead(c *Context, kind int, evNoSeq bool) { - var nextByte uint - - switch kind { - case FieldsEvent: - nextByte = 1 - } +func (fields Fields) MorphRead(c *Context, kind int, evNoSeq bool, + prefix string, byt uint) uint { + nextByte := byt for _, field := range fields { - nextByte = field.MorphRead(c, kind, nextByte) + nextByte = field.MorphRead(c, kind, nextByte, prefix) switch kind { case FieldsEvent: // Skip the sequence id @@ -232,45 +229,76 @@ func (fields Fields) MorphRead(c *Context, kind int, evNoSeq bool) { } } } + return nextByte } -func (field *Field) MorphRead(c *Context, kind int, byt uint) uint { - consumed := uint(0) +func (field *Field) MorphRead(c *Context, kind int, byt uint, + prefix string) uint { + + nextByte := byt switch field.XMLName.Local { case "pad": - consumed = uint(field.Bytes) + nextByte += uint(field.Bytes) case "field": - if field.Type == "ClientMessageData" { - break - } - size := field.Type.Size(c) + nextByte = field.MorphReadField(c, kind, nextByte, prefix) + case "list": typ := field.Type.Morph(c) - name := field.Name.Morph(c) - _, isBase := BaseTypeMap[string(field.Type)] - c.Put("v.%s = ", name) - if !isBase { - c.Put("%s(", typ) - } - switch size { - case 1: c.Put("buf[%d]", byt) - case 2: c.Put("get16(buf[%d:])", byt) - case 4: c.Put("get32(buf[%d:])", byt) - case 8: c.Put("get64(buf[%d:])", byt) - default: - log.Fatalf("Unsupported field size '%d' for field '%s'.", - size, field) - } - if !isBase { - c.Put(")") + // Create a temporary Field so we can use MorphReadField. + // temp := &Field{ + // XMLName: xml.Name{Local: "field"}, + // Name: field.Name, + // Type: field.Type, + // } + + // Special case: if the list is just raw bytes, use copy! + if typ == "byte" { + c.Putln("copy(%s%s, buf[%d:])", prefix, field.Name.Morph(c), + byt) + nextByte = byt + 20 + } else { + c.Putln("//list!") } - c.Putln("") + } + return nextByte +} - consumed = size - case "list": +func (field *Field) MorphReadField(c *Context, kind int, byt uint, + prefix string) uint { + + if union := field.Type.Union(c); union != nil { c.Putln("") + c.Putln("%s%s = %s{}", prefix, field.Name.Morph(c), field.Type.Morph(c)) + union.Fields.MorphRead(c, kind, false, + fmt.Sprintf("%s%s.", prefix, field.Name.Morph(c)), byt) + c.Putln("") + return byt } - return byt + consumed + + size := field.Type.Size(c) + typ := field.Type.Morph(c) + name := field.Name.Morph(c) + _, isBase := BaseTypeMap[string(field.Type)] + + c.Put("%s%s = ", prefix, name) + if !isBase { + c.Put("%s(", typ) + } + switch size { + case 1: c.Put("buf[%d]", byt) + case 2: c.Put("get16(buf[%d:])", byt) + case 4: c.Put("get32(buf[%d:])", byt) + case 8: c.Put("get64(buf[%d:])", byt) + default: + log.Fatalf("Unsupported field size '%d' for field '%s'.", + size, field) + } + if !isBase { + c.Put(")") + } + c.Putln("") + + return byt + size } func (fields Fields) MorphWrite(c *Context, kind int) { @@ -377,7 +405,7 @@ func (ev *Event) Morph(c *Context) { c.Putln("") c.Putln("func New%s(buf []byte) %sEvent {", name, name) c.Putln("var v %sEvent", name) - ev.Fields.MorphRead(c, FieldsEvent, ev.NoSequence) + ev.Fields.MorphRead(c, FieldsEvent, ev.NoSequence, "v.", 1) c.Putln("return v") c.Putln("}") c.Putln("") diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go index 12932b2..e4202d0 100644 --- a/nexgb/xgbgen/xml.go +++ b/nexgb/xgbgen/xml.go @@ -166,6 +166,48 @@ type Name string type Type string +// Union returns the 'Union' struct corresponding to this type, if +// one exists. +func (typ Type) Union(c *Context) *Union { + // If this is a typedef, use that instead. + if oldTyp, ok := typ.TypeDef(c); ok { + return oldTyp.Union(c) + } + + // Otherwise, just look for a union type with 'typ' name. + for _, union := range c.xml.Unions { + if typ == union.Name { + return union + } + } + for _, imp := range c.xml.Imports { + for _, union := range imp.xml.Unions { + if typ == union.Name { + return union + } + } + } + return nil +} + +// TypeDef returns the 'old' type corresponding to this type, if it's found +// in a type def. If not found, the second return value is false. +func (typ Type) TypeDef(c *Context) (Type, bool) { + for _, typedef := range c.xml.TypeDefs { + if typ == typedef.New { + return typedef.Old, true + } + } + for _, imp := range c.xml.Imports { + for _, typedef := range imp.xml.TypeDefs { + if typ == typedef.New { + return typedef.Old, true + } + } + } + return "", false +} + // Size is a nifty function that takes any type and digs until it finds // its underlying base type. At which point, the size can be determined. func (typ Type) Size(c *Context) uint { -- cgit v1.2.3-70-g09d2 From 05d8ec6a16acf88c5ae7521d86131f5ea7f9b4e4 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Mon, 30 Apr 2012 02:40:55 -0400 Subject: complete and total overhaul like i promised. things are much easier to reason about. still not working yet though. --- nexgb/xgbgen/context.go | 25 +- nexgb/xgbgen/expression.go | 276 +++++++++++++++++ nexgb/xgbgen/field.go | 170 +++++++++++ nexgb/xgbgen/go.go | 661 ++++++++++++++++------------------------- nexgb/xgbgen/main.go | 2 +- nexgb/xgbgen/morph.go | 50 ++++ nexgb/xgbgen/representation.go | 56 ++++ nexgb/xgbgen/size.go | 22 ++ nexgb/xgbgen/translation.go | 426 ++++++++++++++++++++++++++ nexgb/xgbgen/type.go | 365 +++++++++++++++++++++++ nexgb/xgbgen/xml.go | 349 ++++------------------ nexgb/xgbgen/xml_expression.go | 30 +- nexgb/xgbgen/xml_fields.go | 58 ++-- 13 files changed, 1740 insertions(+), 750 deletions(-) create mode 100644 nexgb/xgbgen/expression.go create mode 100644 nexgb/xgbgen/field.go create mode 100644 nexgb/xgbgen/morph.go create mode 100644 nexgb/xgbgen/representation.go create mode 100644 nexgb/xgbgen/size.go create mode 100644 nexgb/xgbgen/translation.go create mode 100644 nexgb/xgbgen/type.go (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go index 712cad4..d3cbb81 100644 --- a/nexgb/xgbgen/context.go +++ b/nexgb/xgbgen/context.go @@ -8,13 +8,12 @@ import ( ) type Context struct { - xml *XML + protocol *Protocol out *bytes.Buffer } func newContext() *Context { return &Context{ - xml: &XML{}, out: bytes.NewBuffer([]byte{}), } } @@ -32,22 +31,24 @@ func (c *Context) Put(format string, v ...interface{}) { } } - -// Translate is the big daddy of them all. It takes in an XML byte slice +// 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, // and writes Go code to the 'out' buffer. -func (c *Context) Translate(xmlBytes []byte) { - err := xml.Unmarshal(xmlBytes, c.xml) +func (c *Context) Morph(xmlBytes []byte) { + parsedXml := &XML{} + err := xml.Unmarshal(xmlBytes, parsedXml) if err != nil { log.Fatal(err) } // Parse all imports - c.xml.Imports.Eval() + parsedXml.Imports.Eval() - // Make sure all top level enumerations have expressions - // (For when there are empty items.) - c.xml.Enums.Eval() + // Translate XML types to nice types + c.protocol = parsedXml.Translate() - // It's Morphin' Time! - c.xml.Morph(c) + // Now write Go source code + for _, typ := range c.protocol.Types { + typ.Define(c) + } } diff --git a/nexgb/xgbgen/expression.go b/nexgb/xgbgen/expression.go new file mode 100644 index 0000000..a163692 --- /dev/null +++ b/nexgb/xgbgen/expression.go @@ -0,0 +1,276 @@ +package main + +import ( + "fmt" + "log" +) + +type Expression interface { + Concrete() bool + Eval() uint + Reduce(prefix, fun string) string + String() string + Initialize(p *Protocol) +} + +type BinaryOp struct { + Op string + Expr1 Expression + Expr2 Expression +} + +func newBinaryOp(op string, expr1, expr2 Expression) Expression { + switch { + case expr1 != nil && expr2 != nil: + return &BinaryOp{ + Op: op, + Expr1: expr1, + Expr2: expr2, + } + case expr1 != nil && expr2 == nil: + return expr1 + case expr1 == nil && expr2 != nil: + return expr2 + case expr1 == nil && expr2 == nil: + return nil + } + panic("unreachable") +} + +func (e *BinaryOp) Concrete() bool { + return e.Expr1.Concrete() && e.Expr2.Concrete() +} + +func (e *BinaryOp) Eval() uint { + switch e.Op { + case "+": + return e.Expr1.Eval() + e.Expr2.Eval() + case "-": + return e.Expr1.Eval() - e.Expr2.Eval() + case "*": + return e.Expr1.Eval() * e.Expr2.Eval() + case "/": + return e.Expr1.Eval() / e.Expr2.Eval() + case "&": + return e.Expr1.Eval() & e.Expr2.Eval() + case "<<": + return e.Expr1.Eval() << e.Expr2.Eval() + } + + log.Fatalf("Invalid binary operator '%s' for expression.", e.Op) + panic("unreachable") +} + +func (e *BinaryOp) Reduce(prefix, fun string) string { + if e.Concrete() { + return fmt.Sprintf("%d", e.Eval()) + } + return fmt.Sprintf("(%s %s %s)", + e.Expr1.Reduce(prefix, fun), e.Op, e.Expr2.Reduce(prefix, fun)) +} + +func (e *BinaryOp) String() string { + return e.Reduce("", "") +} + +func (e *BinaryOp) Initialize(p *Protocol) { + e.Expr1.Initialize(p) + e.Expr2.Initialize(p) +} + +type UnaryOp struct { + Op string + Expr Expression +} + +func (e *UnaryOp) Concrete() bool { + return e.Expr.Concrete() +} + +func (e *UnaryOp) Eval() uint { + switch e.Op { + case "~": + return ^e.Expr.Eval() + } + + log.Fatalf("Invalid unary operator '%s' for expression.", e.Op) + panic("unreachable") +} + +func (e *UnaryOp) Reduce(prefix, fun string) string { + if e.Concrete() { + return fmt.Sprintf("%d", e.Eval()) + } + return fmt.Sprintf("(%s (%s))", e.Op, e.Expr.Reduce(prefix, fun)) +} + +func (e *UnaryOp) String() string { + return e.Reduce("", "") +} + +func (e *UnaryOp) Initialize(p *Protocol) { + e.Expr.Initialize(p) +} + +type PopCount struct { + Expr Expression +} + +func (e *PopCount) Concrete() bool { + return e.Expr.Concrete() +} + +func (e *PopCount) Eval() uint { + return popCount(e.Expr.Eval()) +} + +func (e *PopCount) Reduce(prefix, fun string) string { + if e.Concrete() { + return fmt.Sprintf("%d", e.Eval()) + } + return fmt.Sprintf("popCount(%s)", e.Expr.Reduce(prefix, fun)) +} + +func (e *PopCount) String() string { + return e.Reduce("", "") +} + +func (e *PopCount) Initialize(p *Protocol) { + e.Expr.Initialize(p) +} + +type Value struct { + v uint +} + +func (e *Value) Concrete() bool { + return true +} + +func (e *Value) Eval() uint { + return e.v +} + +func (e *Value) Reduce(prefix, fun string) string { + return fmt.Sprintf("%d", e.v) +} + +func (e *Value) String() string { + return e.Reduce("", "") +} + +func (e *Value) Initialize(p *Protocol) { } + +type Bit struct { + b uint +} + +func (e *Bit) Concrete() bool { + return true +} + +func (e *Bit) Eval() uint { + return 1 << e.b +} + +func (e *Bit) Reduce(prefix, fun string) string { + return fmt.Sprintf("%d", e.Eval()) +} + +func (e *Bit) String() string { + return e.Reduce("", "") +} + +func (e *Bit) Initialize(p *Protocol) { } + +type FieldRef struct { + Name string +} + +func (e *FieldRef) Concrete() bool { + return false +} + +func (e *FieldRef) Eval() uint { + log.Fatalf("Cannot evaluate a 'FieldRef'. It is not concrete.") + panic("unreachable") +} + +func (e *FieldRef) Reduce(prefix, fun string) string { + val := e.Name + if len(prefix) > 0 { + val = fmt.Sprintf("%s%s", prefix, val) + } + if len(fun) > 0 { + val = fmt.Sprintf("%s(%s)", fun, val) + } + return val +} + +func (e *FieldRef) String() string { + return e.Reduce("", "") +} + +func (e *FieldRef) Initialize(p *Protocol) { + e.Name = SrcName(e.Name) +} + +type EnumRef struct { + EnumKind Type + EnumItem string +} + +func (e *EnumRef) Concrete() bool { + return false +} + +func (e *EnumRef) Eval() uint { + log.Fatalf("Cannot evaluate an 'EnumRef'. It is not concrete.") + panic("unreachable") +} + +func (e *EnumRef) Reduce(prefix, fun string) string { + val := fmt.Sprintf("%s%s", e.EnumKind, e.EnumItem) + if len(fun) > 0 { + val = fmt.Sprintf("%s(%s)", fun, val) + } + return val +} + +func (e *EnumRef) String() string { + return e.Reduce("", "") +} + +func (e *EnumRef) Initialize(p *Protocol) { + e.EnumKind = e.EnumKind.(*Translation).RealType(p) + e.EnumItem = SrcName(e.EnumItem) +} + +type SumOf struct { + Name string +} + +func (e *SumOf) Concrete() bool { + return false +} + +func (e *SumOf) Eval() uint { + log.Fatalf("Cannot evaluate a 'SumOf'. It is not concrete.") + panic("unreachable") +} + +func (e *SumOf) Reduce(prefix, fun string) string { + if len(prefix) > 0 { + return fmt.Sprintf("sum(%s%s)", prefix, e.Name) + } + return fmt.Sprintf("sum(%s)", e.Name) +} + +func (e *SumOf) String() string { + return e.Reduce("", "") +} + +func (e *SumOf) Initialize(p *Protocol) { + e.Name = SrcName(e.Name) +} + diff --git a/nexgb/xgbgen/field.go b/nexgb/xgbgen/field.go new file mode 100644 index 0000000..a659e6e --- /dev/null +++ b/nexgb/xgbgen/field.go @@ -0,0 +1,170 @@ +package main + +type Field interface { + Initialize(p *Protocol) + SrcName() string + XmlName() string + Size() Size + + Define(c *Context) + Read(c *Context) +} + +func (pad *PadField) Initialize(p *Protocol) {} + +type PadField struct { + Bytes uint +} + +func (p *PadField) SrcName() string { + panic("illegal to take source name of a pad field") +} + +func (p *PadField) XmlName() string { + panic("illegal to take XML name of a pad field") +} + +func (p *PadField) Size() Size { + return newFixedSize(p.Bytes) +} + +type SingleField struct { + srcName string + xmlName string + Type Type +} + +func (f *SingleField) Initialize(p *Protocol) { + f.srcName = SrcName(f.XmlName()) + f.Type = f.Type.(*Translation).RealType(p) +} + +func (f *SingleField) SrcName() string { + return f.srcName +} + +func (f *SingleField) XmlName() string { + return f.xmlName +} + +func (f *SingleField) Size() Size { + return f.Type.Size() +} + +type ListField struct { + srcName string + xmlName string + Type Type + LengthExpr Expression +} + +func (f *ListField) SrcName() string { + return f.srcName +} + +func (f *ListField) XmlName() string { + return f.xmlName +} + +func (f *ListField) Size() Size { + return newExpressionSize(f.LengthExpr).Multiply(f.Type.Size()) +} + +func (f *ListField) Initialize(p *Protocol) { + f.srcName = SrcName(f.XmlName()) + f.Type = f.Type.(*Translation).RealType(p) + if f.LengthExpr != nil { + f.LengthExpr.Initialize(p) + } +} + +type LocalField struct { + *SingleField +} + +type ExprField struct { + srcName string + xmlName string + Type Type + Expr Expression +} + +func (f *ExprField) SrcName() string { + return f.srcName +} + +func (f *ExprField) XmlName() string { + return f.xmlName +} + +func (f *ExprField) Size() Size { + return f.Type.Size() +} + +func (f *ExprField) Initialize(p *Protocol) { + f.srcName = SrcName(f.XmlName()) + f.Type = f.Type.(*Translation).RealType(p) + f.Expr.Initialize(p) +} + +type ValueField struct { + MaskType Type + MaskName string + ListName string +} + +func (f *ValueField) SrcName() string { + panic("it is illegal to call SrcName on a ValueField field") +} + +func (f *ValueField) XmlName() string { + panic("it is illegal to call XmlName on a ValueField field") +} + +func (f *ValueField) Size() Size { + return f.MaskType.Size() +} + +func (f *ValueField) Initialize(p *Protocol) { + f.MaskType = f.MaskType.(*Translation).RealType(p) + f.MaskName = SrcName(f.MaskName) + f.ListName = SrcName(f.ListName) +} + +type SwitchField struct { + Name string + Expr Expression + Bitcases []*Bitcase +} + +func (f *SwitchField) SrcName() string { + panic("it is illegal to call SrcName on a SwitchField field") +} + +func (f *SwitchField) XmlName() string { + panic("it is illegal to call XmlName on a SwitchField field") +} + +// XXX: This is a bit tricky. The size has to be represented as a non-concrete +// expression that finds *which* bitcase fields are included, and sums the +// sizes of those fields. +func (f *SwitchField) Size() Size { + return newFixedSize(0) +} + +func (f *SwitchField) Initialize(p *Protocol) { + f.Name = SrcName(f.Name) + f.Expr.Initialize(p) + for _, bitcase := range f.Bitcases { + bitcase.Expr.Initialize(p) + for _, field := range bitcase.Fields { + field.Initialize(p) + } + } +} + +type Bitcase struct { + Fields []Field + Expr Expression +} + diff --git a/nexgb/xgbgen/go.go b/nexgb/xgbgen/go.go index b939b3a..ac3ed2c 100644 --- a/nexgb/xgbgen/go.go +++ b/nexgb/xgbgen/go.go @@ -1,36 +1,13 @@ package main -/* - To the best of my ability, these are all of the Go specific formatting - functions. If I've designed xgbgen correctly, this should be only the - place that you change things to generate code for a new language. - - This file is organized as follows: - - * Imports and helper variables. - * Manual type and name override maps. - * Constants for tweaking various morphing functions. - * Helper morphing functions. - * Morphing functions for each "sub-unit." - * Morphing functions for each "unit". - * Morphing functions for collections of "units". - - Units can be thought of as the top-level elements in an XML protocol - description file. Namely, structs, xidtypes, imports, enums, unions, etc. - Collections of units are simply "all of the UNIT in the XML file." - Sub-units can be thought of as recurring bits like struct contents (which - is used in events, replies, requests, errors, etc.) and expression - evaluation. -*/ import ( "fmt" "log" - "strings" ) -/******************************************************************************/ -// Manual type and name overrides. -/******************************************************************************/ +// xgbResourceIdName is the name of the type used for all resource identifiers. +// As of right now, it needs to be declared somewhere manually. +var xgbGenResourceIdName = "Id" // BaseTypeMap is a map from X base types to Go types. // X base types should correspond to the smallest set of X types @@ -50,6 +27,8 @@ var BaseTypeMap = map[string]string{ "float": "float64", "double": "float64", "char": "byte", + "void": "byte", + "Id": "Id", } // BaseTypeSizes should have precisely the same keys as in BaseTypeMap, @@ -66,6 +45,7 @@ var BaseTypeSizes = map[string]uint{ "float": 4, "double": 8, "char": 1, + "void": 1, "Id": 4, } @@ -82,483 +62,350 @@ var TypeMap = map[string]string{ // NameMap is the same as TypeMap, but for names. var NameMap = map[string]string{ } -/******************************************************************************/ -// Constants for changing the semantics of morphing functions. -// These are mainly used to tweaking the writing of fields. -// Namely, reading/writing is not exactly the same across events, -// requests/replies and errors. -/******************************************************************************/ -const ( - FieldsEvent = iota - FieldsRequestReply - FieldsError -) - -/******************************************************************************/ -// Helper functions that aide in morphing repetitive constructs. -// i.e., type and identifier names, etc. -/******************************************************************************/ - -// Morph changes every TYPE (not names) into something suitable -// for your language. It also handles adding suffixes like 'Event' -// and 'Union'. (A 'Union' suffix is used in Go because unions aren't -// supported at the language level.) -func (typ Type) Morph(c *Context) string { - t := string(typ) - - // If this is a base type, then write the raw Go type. - if newt, ok := BaseTypeMap[t]; ok { - return newt - } - - // If it's in the type map, use that translation. - if newt, ok := TypeMap[t]; ok { - return newt - } - - // If it's a resource type, just use 'Id'. - if c.xml.IsResource(typ) { - return "Id" - } +// Reading, writing and defining... - // If there's a namespace to this type, just use it and be done. - if colon := strings.Index(t, ":"); colon > -1 { - namespace := t[:colon] - rest := t[colon+1:] - return splitAndTitle(namespace) + splitAndTitle(rest) - } - - // Since there is no namespace, we need to look for a namespace - // in the current context. - return typ.Prefix(c) + splitAndTitle(t) +// Base types +func (b *Base) Define(c *Context) { + c.Putln("// Skipping definition for base type '%s'", SrcName(b.XmlName())) + c.Putln("") } -// Prefix searches the parsed XML for a type matching 'typ'. -// It then returns the appropriate prefix to be used in source code. -// Note that the core X protocol *is* a namespace, but does not have a prefix. -// Also note that you should probably check the BaseTypeMap and TypeMap -// before calling this function. -func (typ Type) Prefix(c *Context) string { - // If this is xproto, quit. No prefixes needed. - if c.xml.Header == "xproto" { - return "" - } - - // First check for the type in the current namespace. - if c.xml.HasType(typ) { - return strings.Title(c.xml.Header) - } - - // Now check each of the imports... - for _, imp := range c.xml.Imports { - if imp.xml.Header != "xproto" && imp.xml.HasType(typ) { - return strings.Title(imp.xml.Header) - } +// Enum types +func (enum *Enum) Define(c *Context) { + c.Putln("const (") + for _, item := range enum.Items { + c.Putln("%s%s = %d", enum.SrcName(), item.srcName, item.Expr.Eval()) } - - return "" + c.Putln(")") + c.Putln("") } -// Morph changes every identifier (NOT type) into something suitable -// for your language. -func (name Name) Morph(c *Context) string { - n := string(name) - - // If it's in the name map, use that translation. - if newn, ok := NameMap[n]; ok { - return newn - } - - return splitAndTitle(n) +// Resource types +func (res *Resource) Define(c *Context) { + c.Putln("// Skipping resource definition of '%s'", SrcName(res.XmlName())) + c.Putln("") } -/******************************************************************************/ -// Sub-unit morphing. -// Below are functions that morph sub-units. Like collections of fields, -// expressions, etc. -// Note that collections of fields can be used in three different contexts: -// definitions, reading from the wire and writing to the wire. Thus, there -// exists 'MorphDefine', 'MorphRead', 'MorphWrite' defined on Fields. -/******************************************************************************/ -func (fields Fields) MorphDefine(c *Context) { - for _, field := range fields { - field.MorphDefine(c) - } +// TypeDef types +func (td *TypeDef) Define(c *Context) { + c.Putln("type %s %s", td.srcName, td.Old.SrcName()) + c.Putln("") } -func (field *Field) MorphDefine(c *Context) { - // We omit 'pad' and 'exprfield' - switch field.XMLName.Local { - case "field": - c.Putln("%s %s", field.Name.Morph(c), field.Type.Morph(c)) - case "list": - c.Putln("%s []%s", field.Name.Morph(c), field.Type.Morph(c)) - case "localfield": - c.Putln("%s %s", field.Name.Morph(c), field.Type.Morph(c)) - case "valueparam": - c.Putln("%s %s", field.ValueMaskName.Morph(c), - field.ValueMaskType.Morph(c)) - c.Putln("%s []%s", field.ValueListName.Morph(c), - field.ValueMaskType.Morph(c)) - case "switch": - field.Bitcases.MorphDefine(c) +// Struct types +func (s *Struct) Define(c *Context) { + c.Putln("// '%s' struct definition", s.SrcName()) + c.Putln("// Size: %s", s.Size()) + c.Putln("type %s struct {", s.SrcName()) + for _, field := range s.Fields { + field.Define(c) } -} + c.Putln("}") + c.Putln("") -func (bitcases Bitcases) MorphDefine(c *Context) { - for _, bitcase := range bitcases { - bitcase.MorphDefine(c) - } -} + // Write function that reads bytes and produces this struct. + s.Read(c) -func (bitcase *Bitcase) MorphDefine(c *Context) { - bitcase.Fields.MorphDefine(c) -} + // Write function that reads a list of this structs. + s.ReadList(c) -func (fields Fields) MorphRead(c *Context, kind int, evNoSeq bool, - prefix string, byt uint) uint { + // Write function that writes bytes given this struct. + s.Write(c) - nextByte := byt - for _, field := range fields { - nextByte = field.MorphRead(c, kind, nextByte, prefix) - switch kind { - case FieldsEvent: - // Skip the sequence id - if !evNoSeq && (nextByte == 2 || nextByte == 3) { - nextByte = 4 - } - } - } - return nextByte -} - -func (field *Field) MorphRead(c *Context, kind int, byt uint, - prefix string) uint { - - nextByte := byt - switch field.XMLName.Local { - case "pad": - nextByte += uint(field.Bytes) - case "field": - nextByte = field.MorphReadField(c, kind, nextByte, prefix) - case "list": - typ := field.Type.Morph(c) - - // Create a temporary Field so we can use MorphReadField. - // temp := &Field{ - // XMLName: xml.Name{Local: "field"}, - // Name: field.Name, - // Type: field.Type, - // } - - // Special case: if the list is just raw bytes, use copy! - if typ == "byte" { - c.Putln("copy(%s%s, buf[%d:])", prefix, field.Name.Morph(c), - byt) - nextByte = byt + 20 - } else { - c.Putln("//list!") - } - } - return nextByte + // Write function that writes a list of this struct. + s.WriteList(c) } -func (field *Field) MorphReadField(c *Context, kind int, byt uint, - prefix string) uint { +// Read for a struct creates a function 'NewStructName' that takes a byte +// slice and produces TWO values: an instance of 'StructName' and the number +// of bytes read from the byte slice. +// 'NewStructName' should only be used to read raw reply data from the wire. +func (s *Struct) Read(c *Context) { + c.Putln("// Struct read %s", s.SrcName()) + c.Putln("func New%s(buf []byte) (%s, int) {", s.SrcName(), s.SrcName()) - if union := field.Type.Union(c); union != nil { - c.Putln("") - c.Putln("%s%s = %s{}", prefix, field.Name.Morph(c), field.Type.Morph(c)) - union.Fields.MorphRead(c, kind, false, - fmt.Sprintf("%s%s.", prefix, field.Name.Morph(c)), byt) - c.Putln("") - return byt + c.Putln("v := %s{}", s.SrcName()) + c.Putln("b := 0") + c.Putln("consumed := 0") + c.Putln("consumed = 0 + consumed // no-op") // dirty hack for a no-op + c.Putln("") + for _, field := range s.Fields { + field.Read(c) } + c.Putln("return v, b") - size := field.Type.Size(c) - typ := field.Type.Morph(c) - name := field.Name.Morph(c) - _, isBase := BaseTypeMap[string(field.Type)] - - c.Put("%s%s = ", prefix, name) - if !isBase { - c.Put("%s(", typ) - } - switch size { - case 1: c.Put("buf[%d]", byt) - case 2: c.Put("get16(buf[%d:])", byt) - case 4: c.Put("get32(buf[%d:])", byt) - case 8: c.Put("get64(buf[%d:])", byt) - default: - log.Fatalf("Unsupported field size '%d' for field '%s'.", - size, field) - } - if !isBase { - c.Put(")") - } + c.Putln("}") c.Putln("") - - return byt + size } -func (fields Fields) MorphWrite(c *Context, kind int) { - var nextByte uint +// ReadList for a struct creates a function 'ReadStructNameList' that takes +// a byte slice and a length and produces TWO values: an slice of StructName +// and the number of bytes read from the byte slice. +func (s *Struct) ReadList(c *Context) { + c.Putln("// Struct list read %s", s.SrcName()) + c.Putln("func Read%sList(buf []byte, length int) ([]%s, int) {", + s.SrcName(), s.SrcName()) + + c.Putln("v := make([]%s, length)", s.SrcName()) + c.Putln("b := 0") + c.Putln("consumed := 0") + c.Putln("consumed = 0 + consumed // no-op") // dirty hack for a no-op + c.Putln("for i := 0; i < length; i++ {") + c.Putln("v[i], consumed = New%s(buf[b:])", s.SrcName()) + c.Putln("b += consumed") + c.Putln("}") - switch kind { - case FieldsEvent: - nextByte = 1 - } + c.Putln("return v, pad(b)") - for _, field := range fields { - nextByte = field.MorphWrite(c, kind, nextByte) - } + c.Putln("}") + c.Putln("") } -func (field *Field) MorphWrite(c *Context, kind int, byt uint) uint { - consumed := uint(0) - switch field.XMLName.Local { - case "pad": - consumed = uint(field.Bytes) - case "field": - size := field.Type.Size(c) - typ := field.Type.Morph(c) - name := field.Name.Morph(c) - switch size { - case 1: - c.Putln("v.%s = %s(buf[%d])", name, typ, byt) - case 2: - c.Putln("v.%s = %s(get16(buf[%d:]))", name, typ, byt) - case 4: - c.Putln("v.%s = %s(get32(buf[%d:]))", name, typ, byt) - case 8: - c.Putln("v.%s = %s(get64(buf[%d:]))", name, typ, byt) - } - consumed = size - case "list": - c.Putln("IDK") - } - return byt + consumed +func (s *Struct) Write(c *Context) { + c.Putln("// Struct write %s", s.SrcName()) + c.Putln("") } -/******************************************************************************/ -// Per element morphing. -// Below are functions that morph a single unit. -/******************************************************************************/ - -// Import morphing. -func (imp *Import) Morph(c *Context) { - c.Putln("// import \"%s\"", imp.Name) +func (s *Struct) WriteList(c *Context) { + c.Putln("// Write struct list %s", s.SrcName()) + c.Putln("") } -// Enum morphing. -func (enum *Enum) Morph(c *Context) { - c.Putln("const (") - for _, item := range enum.Items { - c.Putln("%s%s = %d", enum.Name.Morph(c), item.Name.Morph(c), - item.Expr.Eval()) - } - c.Putln(")\n") +// Union types +func (u *Union) Define(c *Context) { + c.Putln("// Union definition %s", u.SrcName()) } -// Xid morphing. -func (xid *Xid) Morph(c *Context) { - // Don't emit anything for xid types for now. - // We're going to force them all to simply be 'Id' - // to avoid excessive type converting. - // c.Putln("type %s Id", xid.Name.Morph(c)) +func (u *Union) Read(c *Context, prefix string) { + c.Putln("// Union read %s", u.SrcName()) } -// TypeDef morphing. -func (typedef *TypeDef) Morph(c *Context) { - c.Putln("type %s %s", typedef.New.Morph(c), typedef.Old.Morph(c)) +func (u *Union) Write(c *Context, prefix string) { + c.Putln("// Union write %s", u.SrcName()) } -// Struct morphing. -func (strct *Struct) Morph(c *Context) { - c.Putln("type %s struct {", strct.Name.Morph(c)) - strct.Fields.MorphDefine(c) - c.Putln("}") - c.Putln("\n") +// Event types +func (e *Event) Define(c *Context) { + c.Putln("// Event definition %s (%d)", e.SrcName(), e.Number) } -// Union morphing. -func (union *Union) Morph(c *Context) { - c.Putln("type %s struct {", union.Name.Morph(c)) - union.Fields.MorphDefine(c) - c.Putln("}") - c.Putln("\n") +func (e *Event) Read(c *Context, prefix string) { + c.Putln("// Event read %s", e.SrcName()) } -// Request morphing. -func (request *Request) Morph(c *Context) { +func (e *Event) Write(c *Context, prefix string) { + c.Putln("// Event write %s", e.SrcName()) } -// Event morphing. -func (ev *Event) Morph(c *Context) { - name := ev.Name.Morph(c) - - c.Putln("const %s = %d", name, ev.Number) +// EventCopy types +func (e *EventCopy) Define(c *Context) { + c.Putln("// EventCopy definition %s (%d)", e.SrcName(), e.Number) c.Putln("") - c.Putln("type %sEvent struct {", name) - ev.Fields.MorphDefine(c) - 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("") - c.Putln("func New%s(buf []byte) %sEvent {", name, name) - c.Putln("var v %sEvent", name) - ev.Fields.MorphRead(c, FieldsEvent, ev.NoSequence, "v.", 1) - c.Putln("return v") + c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.EvType()) + c.Putln("return (%s)(New%s(buf))", e.EvType(), e.Old.SrcName()) c.Putln("}") c.Putln("") - c.Putln("func (err %sEvent) ImplementsEvent() { }", name) + c.Putln("func (ev %s) ImplementsEvent() { }", e.EvType()) c.Putln("") - c.Putln("func (ev %sEvent) Bytes() []byte {", name) - // ev.Fields.MorphWrite(c, FieldsEvent) + c.Putln("func (ev %s) Bytes() []byte {", e.EvType()) + c.Putln("return (%s)(ev).Bytes()", e.Old.(*Event).EvType()) c.Putln("}") c.Putln("") c.Putln("func init() {") - c.Putln("newEventFuncs[%d] = New%s", ev.Number, name) + c.Putln("newEventFuncs[%d] = New%s", e.Number, e.SrcName()) c.Putln("}") c.Putln("") } -// EventCopy morphing. -func (evcopy *EventCopy) Morph(c *Context) { - oldName, newName := evcopy.Ref.Morph(c), evcopy.Name.Morph(c) - - c.Putln("const %s = %d", newName, evcopy.Number) - c.Putln("") - c.Putln("type %sEvent %sEvent", newName, oldName) - c.Putln("") - c.Putln("func New%s(buf []byte) %sEvent {", newName, newName) - c.Putln("return (%sEvent)(New%s(buf))", newName, oldName) - c.Putln("}") - c.Putln("") - c.Putln("func (err %sEvent) ImplementsEvent() { }", newName) - c.Putln("") - c.Putln("func (ev %sEvent) Bytes() []byte {", newName) - c.Putln("return (%sEvent)(ev).Bytes()", oldName) - c.Putln("}") - c.Putln("") - c.Putln("func init() {") - c.Putln("newEventFuncs[%d] = New%s", evcopy.Number, newName) - c.Putln("}") +// Error types +func (e *Error) Define(c *Context) { + c.Putln("// Error definition %s (%d)", e.SrcName(), e.Number) c.Putln("") } -// Error morphing. -func (err *Error) Morph(c *Context) { +func (e *Error) Read(c *Context, prefix string) { + c.Putln("// Error read %s", e.SrcName()) } -// ErrorCopy morphing. -func (errcopy *ErrorCopy) Morph(c *Context) { - oldName, newName := errcopy.Ref.Morph(c), errcopy.Name.Morph(c) +func (e *Error) Write(c *Context, prefix string) { + c.Putln("// Error write %s", e.SrcName()) +} - c.Putln("const Bad%s = %d", newName, errcopy.Number) +// ErrorCopy types +func (e *ErrorCopy) Define(c *Context) { + c.Putln("// ErrorCopy definition %s (%d)", e.SrcName(), e.Number) c.Putln("") - c.Putln("type %sError %sError", newName, oldName) + c.Putln("const %s = %d", e.ErrConst(), e.Number) c.Putln("") - c.Putln("func New%sError(buf []byte) %sError {", newName, newName) - c.Putln("return (%sError)(New%sError(buf))", newName, oldName) + c.Putln("type %s %s", e.ErrType(), e.Old.(*Error).ErrType()) + c.Putln("") + c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.ErrType()) + c.Putln("return (%s)(New%s(buf))", e.ErrType(), e.Old.SrcName()) c.Putln("}") c.Putln("") - c.Putln("func (err %sError) ImplementsError() { }", newName) + c.Putln("func (err %s) ImplementsError() { }", e.ErrType()) c.Putln("") - c.Putln("func (err %sError) Bytes() []byte {", newName) - c.Putln("return (%sError)(err).Bytes()", oldName) + c.Putln("func (err %s) Bytes() []byte {", e.ErrType()) + c.Putln("return (%s)(err).Bytes()", e.Old.(*Error).ErrType()) c.Putln("}") c.Putln("") c.Putln("func init() {") - c.Putln("newErrorFuncs[%d] = New%sError", errcopy.Number, newName) + c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.SrcName()) c.Putln("}") c.Putln("") } -/******************************************************************************/ -// Collection morphing. -// Below are functions that morph a collections of units. -// Most of these can probably remain unchanged, but they are useful if you -// need to group all of some "unit" in a single block or something. -/******************************************************************************/ -func (imports Imports) Morph(c *Context) { - if len(imports) == 0 { - return - } +// Field definitions, reads and writes. - c.Putln("// Imports are not required for XGB since everything is in") - c.Putln("// a single package. Still these may be useful for ") - c.Putln("// reference purposes.") - for _, imp := range imports { - imp.Morph(c) - } +// Pad fields +func (f *PadField) Define(c *Context) { + c.Putln("// padding: %d bytes", f.Bytes) } -func (enums Enums) Morph(c *Context) { - c.Putln("// Enums\n") - for _, enum := range enums { - enum.Morph(c) - } +func (f *PadField) Read(c *Context) { + c.Putln("b += %s // padding", f.Size()) + c.Putln("") } -func (xids Xids) Morph(c *Context) { - c.Putln("// Xids\n") - for _, xid := range xids { - xid.Morph(c) - } +// Single fields +func (f *SingleField) Define(c *Context) { + c.Putln("%s %s", f.SrcName(), f.Type.SrcName()) } -func (typedefs TypeDefs) Morph(c *Context) { - c.Putln("// TypeDefs\n") - for _, typedef := range typedefs { - typedef.Morph(c) +func ReadSimpleSingleField(c *Context, name string, typ Type) { + switch t := typ.(type) { + case *Resource: + c.Putln("%s = get32(buf[b:])", name) + case *TypeDef: + switch t.Size().Eval() { + case 1: + c.Putln("%s = %s(buf[b])", name, t.SrcName()) + case 2: + c.Putln("%s = %s(get16(buf[b:]))", name, t.SrcName()) + case 4: + c.Putln("%s = %s(get32(buf[b:]))", name, t.SrcName()) + case 8: + c.Putln("%s = %s(get64(buf[b:]))", name, t.SrcName()) + } + case *Base: + var val string + switch t.Size().Eval() { + case 1: + val = fmt.Sprintf("buf[b]") + case 2: + val = fmt.Sprintf("get16(buf[b:])") + case 4: + val = fmt.Sprintf("get32(buf[b:])") + case 8: + val = fmt.Sprintf("get64(buf[b:])") + } + + // We need to convert base types if they aren't uintXX or byte + ty := t.SrcName() + if ty != "byte" && ty != "uint16" && ty != "uint32" && ty != "uint64" { + val = fmt.Sprintf("%s(%s)", ty, val) + } + c.Putln("%s = %s", name, val) + default: + log.Fatalf("Cannot read field '%s' as a simple field with %T type.", + name, typ) } + + c.Putln("b += %s", typ.Size()) } -func (strct Structs) Morph(c *Context) { - c.Putln("// Structs\n") - for _, typedef := range strct { - typedef.Morph(c) +func (f *SingleField) Read(c *Context) { + switch t := f.Type.(type) { + case *Resource: + ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t) + case *TypeDef: + ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t) + case *Base: + ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t) + case *Struct: + c.Putln("v.%s, consumed = New%s(buf[b:])", f.SrcName(), t.SrcName()) + c.Putln("b += consumed") + c.Putln("") + default: + log.Fatalf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type) } } -func (union Unions) Morph(c *Context) { - c.Putln("// Unions\n") - for _, typedef := range union { - typedef.Morph(c) - } +// List fields +func (f *ListField) Define(c *Context) { + c.Putln("%s []%s // length: %s", + f.SrcName(), f.Type.SrcName(), f.Size()) } -func (request Requests) Morph(c *Context) { - c.Putln("// Requests\n") - for _, typedef := range request { - typedef.Morph(c) +func (f *ListField) Read(c *Context) { + switch t := f.Type.(type) { + case *Resource: + length := f.LengthExpr.Reduce("v.", "") + c.Putln("v.%s = make([]Id, %s)", f.SrcName(), length) + c.Putln("for i := 0; i < %s; i++ {", length) + ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t) + c.Putln("}") + c.Putln("") + case *Base: + length := f.LengthExpr.Reduce("v.", "") + c.Putln("v.%s = make([]%s, %s)", f.SrcName(), t.SrcName(), length) + c.Putln("for i := 0; i < %s; i++ {", length) + ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t) + c.Putln("}") + c.Putln("") + case *Struct: + c.Putln("v.%s, consumed = Read%sList(buf[b:], %s)", + f.SrcName(), t.SrcName(), f.LengthExpr.Reduce("v.", "")) + c.Putln("b += consumed") + c.Putln("") + default: + log.Fatalf("Cannot read list field '%s' with %T type.", + f.XmlName(), f.Type) } } -func (event Events) Morph(c *Context) { - c.Putln("// Events\n") - for _, typedef := range event { - typedef.Morph(c) - } +// Local fields +func (f *LocalField) Define(c *Context) { + c.Putln("// local field: %s %s", f.SrcName(), f.Type.SrcName()) } -func (evcopy EventCopies) Morph(c *Context) { - c.Putln("// Event Copies\n") - for _, typedef := range evcopy { - typedef.Morph(c) - } +func (f *LocalField) Read(c *Context) { + c.Putln("// reading local field: %s (%s) :: %s", + f.SrcName(), f.Size(), f.Type.SrcName()) } -func (err Errors) Morph(c *Context) { - c.Putln("// Errors\n") - for _, typedef := range err { - typedef.Morph(c) - } +// Expr fields +func (f *ExprField) Define(c *Context) { + c.Putln("// expression field: %s %s (%s)", + f.SrcName(), f.Type.SrcName(), f.Expr) } -func (errcopy ErrorCopies) Morph(c *Context) { - c.Putln("// Error copies\n") - for _, typedef := range errcopy { - typedef.Morph(c) - } +func (f *ExprField) Read(c *Context) { + c.Putln("// reading expression field: %s (%s) (%s) :: %s", + f.SrcName(), f.Size(), f.Expr, f.Type.SrcName()) +} + +// Value field +func (f *ValueField) Define(c *Context) { + c.Putln("// valueparam field: type: %s, mask name: %s, list name: %s", + f.MaskType.SrcName(), f.MaskName, f.ListName) +} + +func (f *ValueField) Read(c *Context) { + c.Putln("// reading valueparam: type: %s, mask name: %s, list name: %s", + f.MaskType.SrcName(), f.MaskName, f.ListName) +} + +// Switch field +func (f *SwitchField) Define(c *Context) { + c.Putln("// switch field: %s (%s)", f.Name, f.Expr) +} + +func (f *SwitchField) Read(c *Context) { + c.Putln("// reading switch field: %s (%s)", f.Name, f.Expr) } diff --git a/nexgb/xgbgen/main.go b/nexgb/xgbgen/main.go index c69c8aa..33f7971 100644 --- a/nexgb/xgbgen/main.go +++ b/nexgb/xgbgen/main.go @@ -47,7 +47,7 @@ func main() { // Initialize the buffer, parse it, and filter it through gofmt. c := newContext() - c.Translate(xmlBytes) + c.Morph(xmlBytes) if !*gofmt { c.out.WriteTo(os.Stdout) diff --git a/nexgb/xgbgen/morph.go b/nexgb/xgbgen/morph.go new file mode 100644 index 0000000..c39b333 --- /dev/null +++ b/nexgb/xgbgen/morph.go @@ -0,0 +1,50 @@ + +// Morph cascades down all of the XML and calls each type's corresponding +// Morph function with itself as an argument (the context). +func (x *XML) Morph(c *Context) { + // Start the header... + c.Putln("package xgb") + c.Putln("/*") + c.Putln("\tX protocol API for '%s.xml'.", c.xml.Header) + c.Putln("\tThis file is automatically generated. Edit at your own peril!") + c.Putln("\tGenerated on %s", + time.Now().Format("Jan 2, 2006 at 3:04:05pm MST")) + c.Putln("*/") + c.Putln("") + + x.Imports.Morph(c) + c.Putln("") + + x.Enums.Morph(c) + c.Putln("") + + x.Xids.Morph(c) + c.Putln("") + + x.XidUnions.Morph(c) + c.Putln("") + + x.TypeDefs.Morph(c) + c.Putln("") + + x.Structs.Morph(c) + c.Putln("") + + x.Unions.Morph(c) + c.Putln("") + + x.Requests.Morph(c) + c.Putln("") + + x.Errors.Morph(c) + c.Putln("") + + x.ErrorCopies.Morph(c) + c.Putln("") + + x.Events.Morph(c) + c.Putln("") + + x.EventCopies.Morph(c) + c.Putln("") +} diff --git a/nexgb/xgbgen/representation.go b/nexgb/xgbgen/representation.go new file mode 100644 index 0000000..928e219 --- /dev/null +++ b/nexgb/xgbgen/representation.go @@ -0,0 +1,56 @@ +package main + +type Protocol struct { + Name string + ExtXName string + ExtName string + MajorVersion string + MinorVersion string + + Imports []*Protocol + Types []Type + Requests []*Request +} + +// Initialize traverses all structures, looks for 'Translation' type, +// and looks up the real type in the namespace. It also sets the source +// name for all relevant fields/structures. +// This is necessary because we don't traverse the XML in order initially. +func (p *Protocol) Initialize() { + for _, typ := range p.Types { + typ.Initialize(p) + } + for _, req := range p.Requests { + req.Initialize(p) + } +} + +type Request struct { + srcName string + xmlName string + Opcode int + Combine bool + Fields []Field + Reply *Reply +} + +func (r *Request) Initialize(p *Protocol) { + r.srcName = SrcName(r.xmlName) + if r.Reply != nil { + r.Reply.Initialize(p) + } + for _, field := range r.Fields { + field.Initialize(p) + } +} + +type Reply struct { + Fields []Field +} + +func (r *Reply) Initialize(p *Protocol) { + for _, field := range r.Fields { + field.Initialize(p) + } +} + diff --git a/nexgb/xgbgen/size.go b/nexgb/xgbgen/size.go new file mode 100644 index 0000000..d00e297 --- /dev/null +++ b/nexgb/xgbgen/size.go @@ -0,0 +1,22 @@ +package main + +type Size struct { + Expression +} + +func newFixedSize(fixed uint) Size { + return Size{&Value{v: fixed}} +} + +func newExpressionSize(variable Expression) Size { + return Size{variable} +} + +func (s1 Size) Add(s2 Size) Size { + return Size{newBinaryOp("+", s1, s2)} +} + +func (s1 Size) Multiply(s2 Size) Size { + return Size{newBinaryOp("*", s1, s2)} +} + diff --git a/nexgb/xgbgen/translation.go b/nexgb/xgbgen/translation.go new file mode 100644 index 0000000..85e756d --- /dev/null +++ b/nexgb/xgbgen/translation.go @@ -0,0 +1,426 @@ +package main +/* + translation.go provides a 'Translate' method on every XML type that converts + the XML type into our "better" representation. + + i.e., the representation of Fields and Expressions is just too general. + We end up losing a lot of the advantages of static typing if we keep + the types that encoding/xml forces us into. + + Please see 'representation.go' for the type definitions that we're + translating to. +*/ + +import ( + "log" + "strconv" + "strings" +) + +func (xml *XML) Translate() *Protocol { + protocol := &Protocol{ + Name: xml.Header, + ExtXName: xml.ExtensionXName, + ExtName: xml.ExtensionName, + MajorVersion: xml.MajorVersion, + MinorVersion: xml.MinorVersion, + + Imports: make([]*Protocol, 0), + Types: make([]Type, 0), + Requests: make([]*Request, len(xml.Requests)), + } + + for _, imp := range xml.Imports { + if imp.xml != nil { + protocol.Imports = append(protocol.Imports, imp.xml.Translate()) + } + } + + for xmlName, srcName := range BaseTypeMap { + newBaseType := &Base{ + srcName: srcName, + xmlName: xmlName, + size: newFixedSize(BaseTypeSizes[xmlName]), + } + protocol.Types = append(protocol.Types, newBaseType) + } + for _, enum := range xml.Enums { + protocol.Types = append(protocol.Types, enum.Translate()) + } + for _, xid := range xml.Xids { + protocol.Types = append(protocol.Types, xid.Translate()) + } + for _, xidunion := range xml.XidUnions { + protocol.Types = append(protocol.Types, xidunion.Translate()) + } + for _, typedef := range xml.TypeDefs { + protocol.Types = append(protocol.Types, typedef.Translate()) + } + for _, s := range xml.Structs { + protocol.Types = append(protocol.Types, s.Translate()) + } + for _, union := range xml.Unions { + protocol.Types = append(protocol.Types, union.Translate()) + } + for _, ev := range xml.Events { + protocol.Types = append(protocol.Types, ev.Translate()) + } + for _, evcopy := range xml.EventCopies { + protocol.Types = append(protocol.Types, evcopy.Translate()) + } + for _, err := range xml.Errors { + protocol.Types = append(protocol.Types, err.Translate()) + } + for _, errcopy := range xml.ErrorCopies { + protocol.Types = append(protocol.Types, errcopy.Translate()) + } + + for i, request := range xml.Requests { + protocol.Requests[i] = request.Translate() + } + + // Now load all of the type and source name information. + protocol.Initialize() + + // Make sure all enums have concrete values. + for _, typ := range protocol.Types { + enum, ok := typ.(*Enum) + if !ok { + continue + } + nextValue := uint(0) + for _, item := range enum.Items { + if item.Expr == nil { + item.Expr = &Value{v: nextValue} + nextValue++ + } else { + nextValue = item.Expr.Eval() + 1 + } + } + } + + return protocol +} + +func (x *XMLEnum) Translate() *Enum { + enum := &Enum{ + xmlName: x.Name, + Items: make([]*EnumItem, len(x.Items)), + } + for i, item := range x.Items { + enum.Items[i] = &EnumItem{ + xmlName: item.Name, + Expr: item.Expr.Translate(), + } + } + return enum +} + +func (x *XMLXid) Translate() *Resource { + return &Resource{ + xmlName: x.Name, + } +} + +func (x *XMLTypeDef) Translate() *TypeDef { + return &TypeDef{ + xmlName: x.New, + Old: newTranslation(x.Old), + } +} + +func (x *XMLEvent) Translate() *Event { + ev := &Event{ + xmlName: x.Name, + Number: x.Number, + NoSequence: x.NoSequence, + Fields: make([]Field, len(x.Fields)), + } + for i, field := range x.Fields { + ev.Fields[i] = field.Translate() + } + return ev +} + +func (x *XMLEventCopy) Translate() *EventCopy { + return &EventCopy{ + xmlName: x.Name, + Number: x.Number, + Old: newTranslation(x.Ref), + } +} + +func (x *XMLError) Translate() *Error { + err := &Error{ + xmlName: x.Name, + Number: x.Number, + Fields: make([]Field, len(x.Fields)), + } + for i, field := range x.Fields { + err.Fields[i] = field.Translate() + } + return err +} + +func (x *XMLErrorCopy) Translate() *ErrorCopy { + return &ErrorCopy{ + xmlName: x.Name, + Number: x.Number, + Old: newTranslation(x.Ref), + } +} + +func (x *XMLStruct) Translate() *Struct { + s := &Struct{ + xmlName: x.Name, + Fields: make([]Field, len(x.Fields)), + } + for i, field := range x.Fields { + s.Fields[i] = field.Translate() + } + return s +} + +func (x *XMLUnion) Translate() *Union { + u := &Union{ + xmlName: x.Name, + Fields: make([]Field, len(x.Fields)), + } + for i, field := range x.Fields { + u.Fields[i] = field.Translate() + } + return u +} + +func (x *XMLRequest) Translate() *Request { + r := &Request{ + xmlName: x.Name, + Opcode: x.Opcode, + Combine: x.Combine, + Fields: make([]Field, len(x.Fields)), + Reply: x.Reply.Translate(), + } + for i, field := range x.Fields { + r.Fields[i] = field.Translate() + } + + // Address bug (or legacy code) in QueryTextExtents. + // The XML protocol description references 'string_len' in the + // computation of the 'odd_length' field. However, 'string_len' is not + // defined. Therefore, let's forcefully add it as a 'local field'. + // (i.e., a parameter in the caller but does not get send over the wire.) + stringLenLocal := &LocalField{&SingleField{ + xmlName: "string_len", + Type: newTranslation("CARD16"), + }} + r.Fields = append(r.Fields, stringLenLocal) + + return r +} + +func (x *XMLReply) Translate() *Reply { + if x == nil { + return nil + } + + r := &Reply{ + Fields: make([]Field, len(x.Fields)), + } + for i, field := range x.Fields { + r.Fields[i] = field.Translate() + } + return r +} + +func (x *XMLExpression) Translate() Expression { + if x == nil { + return nil + } + + switch x.XMLName.Local { + case "op": + if len(x.Exprs) != 2 { + log.Panicf("'op' found %d expressions; expected 2.", len(x.Exprs)) + } + return &BinaryOp{ + Op: x.Op, + Expr1: x.Exprs[0].Translate(), + Expr2: x.Exprs[1].Translate(), + } + case "unop": + if len(x.Exprs) != 1 { + log.Panicf("'unop' found %d expressions; expected 1.", len(x.Exprs)) + } + return &UnaryOp{ + Op: x.Op, + Expr: x.Exprs[0].Translate(), + } + case "popcount": + if len(x.Exprs) != 1 { + log.Panicf("'popcount' found %d expressions; expected 1.", + len(x.Exprs)) + } + return &PopCount{ + Expr: x.Exprs[0].Translate(), + } + case "value": + val, err := strconv.Atoi(x.Data) + if err != nil { + log.Panicf("Could not convert '%s' in 'value' expression to int.", + x.Data) + } + return &Value{ + v: uint(val), + } + case "bit": + bit, err := strconv.Atoi(x.Data) + if err != nil { + log.Panicf("Could not convert '%s' in 'bit' expression to int.", + x.Data) + } + if bit < 0 || bit > 31 { + log.Panicf("A 'bit' literal must be in the range [0, 31], but " + + " is %d", bit) + } + return &Bit{ + b: uint(bit), + } + case "fieldref": + return &FieldRef{ + Name: x.Data, + } + case "enumref": + return &EnumRef{ + EnumKind: newTranslation(x.Ref), + EnumItem: x.Data, + } + case "sumof": + return &SumOf{ + Name: x.Ref, + } + } + + log.Panicf("Unrecognized tag '%s' in expression context. Expected one of " + + "op, fieldref, value, bit, enumref, unop, sumof or popcount.", + x.XMLName.Local) + panic("unreachable") +} + +func (x *XMLField) Translate() Field { + switch x.XMLName.Local { + case "pad": + return &PadField{ + Bytes: x.Bytes, + } + case "field": + return &SingleField{ + xmlName: x.Name, + Type: newTranslation(x.Type), + } + case "list": + return &ListField{ + xmlName: x.Name, + Type: newTranslation(x.Type), + LengthExpr: x.Expr.Translate(), + } + case "localfield": + return &LocalField{&SingleField{ + xmlName: x.Name, + Type: newTranslation(x.Type), + }} + case "exprfield": + return &ExprField{ + xmlName: x.Name, + Type: newTranslation(x.Type), + Expr: x.Expr.Translate(), + } + case "valueparam": + return &ValueField{ + MaskType: newTranslation(x.ValueMaskType), + MaskName: x.ValueMaskName, + ListName: x.ValueListName, + } + case "switch": + swtch := &SwitchField{ + Name: x.Name, + Expr: x.Expr.Translate(), + Bitcases: make([]*Bitcase, len(x.Bitcases)), + } + for i, bitcase := range x.Bitcases { + swtch.Bitcases[i] = bitcase.Translate() + } + return swtch + } + + log.Panicf("Unrecognized field element: %s", x.XMLName.Local) + panic("unreachable") +} + +func (x *XMLBitcase) Translate() *Bitcase { + b := &Bitcase{ + Expr: x.Expr().Translate(), + Fields: make([]Field, len(x.Fields)), + } + for i, field := range x.Fields { + b.Fields[i] = field.Translate() + } + return b +} + +// SrcName is used to translate any identifier into a Go name. +// Mostly used for fields, but used in a couple other places too (enum items). +func SrcName(name string) string { + // If it's in the name map, use that translation. + if newn, ok := NameMap[name]; ok { + return newn + } + + return splitAndTitle(name) +} + +func TypeSrcName(p *Protocol, typ Type) string { + t := typ.XmlName() + + // If this is a base type, then write the raw Go type. + if baseType, ok := typ.(*Base); ok { + return baseType.SrcName() + } + + // If it's in the type map, use that translation. + if newt, ok := TypeMap[t]; ok { + return newt + } + + // If it's a resource type, just use 'Id'. + if _, ok := typ.(*Resource); ok { + return xgbGenResourceIdName + } + + // If there's a namespace to this type, just use it and be done. + if colon := strings.Index(t, ":"); colon > -1 { + namespace := t[:colon] + rest := t[colon+1:] + return splitAndTitle(namespace) + splitAndTitle(rest) + } + + // Since there is no namespace, we need to look for a namespace + // in the current context. + niceType := splitAndTitle(t) + if p.Name != "xproto" { + for _, typ2 := range p.Types { + if t == typ2.XmlName() { + return strings.Title(p.Name) + niceType + } + } + for _, imp := range p.Imports { + for _, typ2 := range imp.Types { + if t == typ2.XmlName() { + return strings.Title(imp.Name) + niceType + } + } + } + } + + // We couldn't find one, so return it without a prefix. + return niceType +} diff --git a/nexgb/xgbgen/type.go b/nexgb/xgbgen/type.go new file mode 100644 index 0000000..1574922 --- /dev/null +++ b/nexgb/xgbgen/type.go @@ -0,0 +1,365 @@ +package main + +import ( + "fmt" + "strings" +) + +type Type interface { + Initialize(p *Protocol) + SrcName() string + XmlName() string + Size() Size + + Define(c *Context) +} + +// Translation is used *only* when transitioning from XML types to +// our better representation. They are placeholders for the real types (below) +// that will replace them. +type Translation struct { + xmlName string +} + +func newTranslation(name string) *Translation { + return &Translation{xmlName: name} +} + +// RealType takes 'XmlName' and finds its real concrete type in our Protocol. +// It is an error if we can't find such a type. +func (t *Translation) RealType(p *Protocol) Type { + // Check to see if there is a namespace. If so, strip it and use it to + // make sure we only look for a type in that protocol. + namespace, typeName := "", t.XmlName() + if ni := strings.Index(t.XmlName(), ":"); ni > -1 { + namespace, typeName = strings.ToLower(typeName[:ni]), typeName[ni+1:] + } + + if len(namespace) == 0 || namespace == strings.ToLower(p.Name) { + for _, typ := range p.Types { + if typeName == typ.XmlName() { + return typ + } + } + } + for _, imp := range p.Imports { + if len(namespace) == 0 || namespace == strings.ToLower(imp.Name) { + for _, typ := range imp.Types { + if typeName == typ.XmlName() { + return typ + } + } + } + } + panic("Could not find real type for translation type: " + t.XmlName()) +} + +func (t *Translation) SrcName() string { + panic("it is illegal to call SrcName on a translation type") +} + +func (t *Translation) XmlName() string { + return t.xmlName +} + +func (t *Translation) Size() Size { + panic("it is illegal to call Size on a translation type") +} + +func (t *Translation) Define(c *Context) { + panic("it is illegal to call Define on a translation type") +} + +func (t *Translation) Initialize(p *Protocol) { + panic("it is illegal to call Initialize on a translation type") +} + +type Base struct { + srcName string + xmlName string + size Size +} + +func (b *Base) SrcName() string { + return b.srcName +} + +func (b *Base) XmlName() string { + return b.xmlName +} + +func (b *Base) Size() Size { + return b.size +} + +func (b *Base) Initialize(p *Protocol) { + b.srcName = TypeSrcName(p, b) +} + +type Enum struct { + srcName string + xmlName string + Items []*EnumItem +} + +type EnumItem struct { + srcName string + xmlName string + Expr Expression +} + +func (enum *Enum) SrcName() string { + return enum.srcName +} + +func (enum *Enum) XmlName() string { + return enum.xmlName +} + +func (enum *Enum) Size() Size { + panic("Cannot take size of enum") +} + +func (enum *Enum) Initialize(p *Protocol) { + enum.srcName = TypeSrcName(p, enum) + for _, item := range enum.Items { + item.srcName = SrcName(item.xmlName) + if item.Expr != nil { + item.Expr.Initialize(p) + } + } +} + +type Resource struct { + srcName string + xmlName string +} + +func (r *Resource) SrcName() string { + return r.srcName +} + +func (r *Resource) XmlName() string { + return r.xmlName +} + +func (r *Resource) Size() Size { + return newFixedSize(BaseTypeSizes["Id"]) +} + +func (r *Resource) Initialize(p *Protocol) { + r.srcName = TypeSrcName(p, r) +} + +type TypeDef struct { + srcName string + xmlName string + Old Type +} + +func (t *TypeDef) SrcName() string { + return t.srcName +} + +func (t *TypeDef) XmlName() string { + return t.xmlName +} + +func (t *TypeDef) Size() Size { + return t.Old.Size() +} + +func (t *TypeDef) Initialize(p *Protocol) { + t.Old = t.Old.(*Translation).RealType(p) + t.srcName = TypeSrcName(p, t) +} + +type Event struct { + srcName string + xmlName string + Number int + NoSequence bool + Fields []Field +} + +func (e *Event) SrcName() string { + return e.srcName +} + +func (e *Event) XmlName() string { + return e.xmlName +} + +func (e *Event) Size() Size { + panic("Cannot take size of Event type.") +} + +func (e *Event) Initialize(p *Protocol) { + e.srcName = TypeSrcName(p, e) + for _, field := range e.Fields { + field.Initialize(p) + } +} + +func (e *Event) EvType() string { + return fmt.Sprintf("%sEvent", e.srcName) +} + +type EventCopy struct { + srcName string + xmlName string + Old Type + Number int +} + +func (e *EventCopy) SrcName() string { + return e.srcName +} + +func (e *EventCopy) XmlName() string { + return e.xmlName +} + +func (e *EventCopy) Size() Size { + panic("Cannot take size of EventCopy type.") +} + +func (e *EventCopy) Initialize(p *Protocol) { + e.srcName = TypeSrcName(p, e) + e.Old = e.Old.(*Translation).RealType(p) + if _, ok := e.Old.(*Event); !ok { + panic("an EventCopy's old type *must* be *Event") + } +} + +func (e *EventCopy) EvType() string { + return fmt.Sprintf("%sEvent", e.srcName) +} + +type Error struct { + srcName string + xmlName string + Number int + Fields []Field +} + +func (e *Error) SrcName() string { + return e.srcName +} + +func (e *Error) XmlName() string { + return e.xmlName +} + +func (e *Error) Size() Size { + panic("Cannot take size of Error type.") +} + +func (e *Error) Initialize(p *Protocol) { + e.srcName = TypeSrcName(p, e) +} + +func (e *Error) ErrConst() string { + return fmt.Sprintf("Bad%s", e.srcName) +} + +func (e *Error) ErrType() string { + return fmt.Sprintf("%sError", e.srcName) +} + +type ErrorCopy struct { + srcName string + xmlName string + Old Type + Number int +} + +func (e *ErrorCopy) SrcName() string { + return e.srcName +} + +func (e *ErrorCopy) XmlName() string { + return e.xmlName +} + +func (e *ErrorCopy) Size() Size { + panic("Cannot take size of ErrorCopy type.") +} + +func (e *ErrorCopy) Initialize(p *Protocol) { + e.srcName = TypeSrcName(p, e) + e.Old = e.Old.(*Translation).RealType(p) + if _, ok := e.Old.(*Error); !ok { + panic("an ErrorCopy's old type *must* be *Event") + } +} + +func (e *ErrorCopy) ErrConst() string { + return fmt.Sprintf("Bad%s", e.srcName) +} + +func (e *ErrorCopy) ErrType() string { + return fmt.Sprintf("%sError", e.srcName) +} + +type Struct struct { + srcName string + xmlName string + Fields []Field +} + +func (s *Struct) SrcName() string { + return s.srcName +} + +func (s *Struct) XmlName() string { + return s.xmlName +} + +func (s *Struct) Size() Size { + size := newFixedSize(0) + for _, field := range s.Fields { + size = size.Add(field.Size()) + } + return size +} + +func (s *Struct) Initialize(p *Protocol) { + s.srcName = TypeSrcName(p, s) + for _, field := range s.Fields { + field.Initialize(p) + } +} + +type Union struct { + srcName string + xmlName string + Fields []Field +} + +func (u *Union) SrcName() string { + return u.srcName +} + +func (u *Union) XmlName() string { + return u.xmlName +} + +// Size for Union is broken. At least, it's broken for XKB. +// It *looks* like the protocol inherently relies on some amount of +// memory unsafety, since some members of unions in XKB are *variable* in +// length! The only thing I can come up with, maybe, is when a union has +// variable size, simply return the raw bytes. Then it's up to the user to +// pass those raw bytes into the appropriate New* constructor. GROSS! +// As of now, just pluck out the first field and return that size. This +// should work for union elements in randr.xml and xproto.xml. +func (u *Union) Size() Size { + return u.Fields[0].Size() +} + +func (u *Union) Initialize(p *Protocol) { + u.srcName = TypeSrcName(p, u) + for _, field := range u.Fields { + field.Initialize(p) + } +} diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go index e4202d0..7e50831 100644 --- a/nexgb/xgbgen/xml.go +++ b/nexgb/xgbgen/xml.go @@ -4,7 +4,6 @@ import ( "encoding/xml" "io/ioutil" "log" - "time" ) type XML struct { @@ -18,231 +17,25 @@ type XML struct { // Types for all top-level elements. // First are the simple ones. - Imports Imports `xml:"import"` - Enums Enums `xml:"enum"` - Xids Xids `xml:"xidtype"` - XidUnions Xids `xml:"xidunion"` - TypeDefs TypeDefs `xml:"typedef"` - EventCopies EventCopies `xml:"eventcopy"` - ErrorCopies ErrorCopies `xml:"errorcopy"` + Imports XMLImports `xml:"import"` + Enums XMLEnums `xml:"enum"` + Xids XMLXids `xml:"xidtype"` + XidUnions XMLXids `xml:"xidunion"` + TypeDefs XMLTypeDefs `xml:"typedef"` + EventCopies XMLEventCopies `xml:"eventcopy"` + ErrorCopies XMLErrorCopies `xml:"errorcopy"` // Here are the complex ones, i.e., anything with "structure contents" - Structs Structs `xml:"struct"` - Unions Unions `xml:"union"` - Requests Requests `xml:"request"` - Events Events `xml:"event"` - Errors Errors `xml:"error"` + Structs XMLStructs `xml:"struct"` + Unions XMLUnions `xml:"union"` + Requests XMLRequests `xml:"request"` + Events XMLEvents `xml:"event"` + Errors XMLErrors `xml:"error"` } -// Morph cascades down all of the XML and calls each type's corresponding -// Morph function with itself as an argument (the context). -func (x *XML) Morph(c *Context) { - // Start the header... - c.Putln("package xgb") - c.Putln("/*") - c.Putln("\tX protocol API for '%s.xml'.", c.xml.Header) - c.Putln("\tThis file is automatically generated. Edit at your own peril!") - c.Putln("\tGenerated on %s", - time.Now().Format("Jan 2, 2006 at 3:04:05pm MST")) - c.Putln("*/") - c.Putln("") +type XMLImports []*XMLImport - x.Imports.Morph(c) - c.Putln("") - - x.Enums.Morph(c) - c.Putln("") - - x.Xids.Morph(c) - c.Putln("") - - x.XidUnions.Morph(c) - c.Putln("") - - x.TypeDefs.Morph(c) - c.Putln("") - - x.Structs.Morph(c) - c.Putln("") - - x.Unions.Morph(c) - c.Putln("") - - x.Requests.Morph(c) - c.Putln("") - - x.Errors.Morph(c) - c.Putln("") - - x.ErrorCopies.Morph(c) - c.Putln("") - - x.Events.Morph(c) - c.Putln("") - - x.EventCopies.Morph(c) - c.Putln("") -} - -// IsResource returns true if the 'needle' type is a resource type. -// i.e., an "xid" -func (x *XML) IsResource(needle Type) bool { - for _, xid := range x.Xids { - if needle == xid.Name { - return true - } - } - for _, xidunion := range x.XidUnions { - if needle == xidunion.Name { - return true - } - } - for _, imp := range x.Imports { - if imp.xml.IsResource(needle) { - return true - } - } - return false -} - -// HasType returns true if the 'needle' type can be found in the protocol -// description represented by 'x'. -func (x *XML) HasType(needle Type) bool { - for _, enum := range x.Enums { - if needle == enum.Name { - return true - } - } - for _, xid := range x.Xids { - if needle == xid.Name { - return true - } - } - for _, xidunion := range x.XidUnions { - if needle == xidunion.Name { - return true - } - } - for _, typedef := range x.TypeDefs { - if needle == typedef.New { - return true - } - } - for _, evcopy := range x.EventCopies { - if needle == evcopy.Name { - return true - } - } - for _, errcopy := range x.ErrorCopies { - if needle == errcopy.Name { - return true - } - } - for _, strct := range x.Structs { - if needle == strct.Name { - return true - } - } - for _, union := range x.Unions { - if needle == union.Name { - return true - } - } - for _, ev := range x.Events { - if needle == ev.Name { - return true - } - } - for _, err := range x.Errors { - if needle == err.Name { - return true - } - } - - return false -} - -type Name string - -type Type string - -// Union returns the 'Union' struct corresponding to this type, if -// one exists. -func (typ Type) Union(c *Context) *Union { - // If this is a typedef, use that instead. - if oldTyp, ok := typ.TypeDef(c); ok { - return oldTyp.Union(c) - } - - // Otherwise, just look for a union type with 'typ' name. - for _, union := range c.xml.Unions { - if typ == union.Name { - return union - } - } - for _, imp := range c.xml.Imports { - for _, union := range imp.xml.Unions { - if typ == union.Name { - return union - } - } - } - return nil -} - -// TypeDef returns the 'old' type corresponding to this type, if it's found -// in a type def. If not found, the second return value is false. -func (typ Type) TypeDef(c *Context) (Type, bool) { - for _, typedef := range c.xml.TypeDefs { - if typ == typedef.New { - return typedef.Old, true - } - } - for _, imp := range c.xml.Imports { - for _, typedef := range imp.xml.TypeDefs { - if typ == typedef.New { - return typedef.Old, true - } - } - } - return "", false -} - -// Size is a nifty function that takes any type and digs until it finds -// its underlying base type. At which point, the size can be determined. -func (typ Type) Size(c *Context) uint { - // If this is a base type, we're done. - if size, ok := BaseTypeSizes[string(typ)]; ok { - return size - } - - // If it's a resource, we're also done. - if c.xml.IsResource(typ) { - return BaseTypeSizes["Id"] - } - - // It's not, so that implies there is *some* typedef declaring it - // in terms of another type. Just follow that chain until we get to - // a base type. We also need to check imported stuff. - for _, typedef := range c.xml.TypeDefs { - if typ == typedef.New { - return typedef.Old.Size(c) - } - } - for _, imp := range c.xml.Imports { - for _, typedef := range imp.xml.TypeDefs { - if typ == typedef.New { - return typedef.Old.Size(c) - } - } - } - log.Panicf("Could not find base size of type '%s'.", typ) - panic("unreachable") -} - -type Imports []*Import - -func (imports Imports) Eval() { +func (imports XMLImports) Eval() { for _, imp := range imports { xmlBytes, err := ioutil.ReadFile(*protoPath + "/" + imp.Name + ".xml") if err != nil { @@ -256,117 +49,101 @@ func (imports Imports) Eval() { log.Fatal("Could not parse X protocol description for import " + "'%s' because: %s", imp.Name, err) } + + // recursive imports... + imp.xml.Imports.Eval() } } -type Import struct { +type XMLImport struct { Name string `xml:",chardata"` xml *XML `xml:"-"` } -type Enums []Enum - -// Eval on the list of all enum types goes through and forces every enum -// item to have a valid expression. -// This is necessary because when an item is empty, it is defined to have -// the value of "one more than that of the previous item, or 0 for the first -// item". -func (enums Enums) Eval() { - for _, enum := range enums { - nextValue := uint(0) - for _, item := range enum.Items { - if item.Expr == nil { - item.Expr = newValueExpression(nextValue) - nextValue++ - } else { - nextValue = item.Expr.Eval() + 1 - } - } - } -} +type XMLEnums []XMLEnum -type Enum struct { - Name Type `xml:"name,attr"` - Items []*EnumItem `xml:"item"` +type XMLEnum struct { + Name string `xml:"name,attr"` + Items []*XMLEnumItem `xml:"item"` } -type EnumItem struct { - Name Name `xml:"name,attr"` - Expr *Expression `xml:",any"` +type XMLEnumItem struct { + Name string `xml:"name,attr"` + Expr *XMLExpression `xml:",any"` } -type Xids []*Xid +type XMLXids []*XMLXid -type Xid struct { +type XMLXid struct { XMLName xml.Name - Name Type `xml:"name,attr"` + Name string `xml:"name,attr"` } -type TypeDefs []*TypeDef +type XMLTypeDefs []*XMLTypeDef -type TypeDef struct { - Old Type `xml:"oldname,attr"` - New Type `xml:"newname,attr"` +type XMLTypeDef struct { + Old string `xml:"oldname,attr"` + New string `xml:"newname,attr"` } -type EventCopies []*EventCopy +type XMLEventCopies []*XMLEventCopy -type EventCopy struct { - Name Type `xml:"name,attr"` +type XMLEventCopy struct { + Name string `xml:"name,attr"` Number int `xml:"number,attr"` - Ref Type `xml:"ref,attr"` + Ref string `xml:"ref,attr"` } -type ErrorCopies []*ErrorCopy +type XMLErrorCopies []*XMLErrorCopy -type ErrorCopy struct { - Name Type `xml:"name,attr"` +type XMLErrorCopy struct { + Name string `xml:"name,attr"` Number int `xml:"number,attr"` - Ref Type `xml:"ref,attr"` + Ref string `xml:"ref,attr"` } -type Structs []*Struct +type XMLStructs []*XMLStruct -type Struct struct { - Name Type `xml:"name,attr"` - Fields Fields `xml:",any"` +type XMLStruct struct { + Name string `xml:"name,attr"` + Fields XMLFields `xml:",any"` } -type Unions []*Union +type XMLUnions []*XMLUnion -type Union struct { - Name Type `xml:"name,attr"` - Fields Fields `xml:",any"` +type XMLUnion struct { + Name string `xml:"name,attr"` + Fields XMLFields `xml:",any"` } -type Requests []*Request +type XMLRequests []*XMLRequest -type Request struct { - Name Type `xml:"name,attr"` +type XMLRequest struct { + Name string `xml:"name,attr"` Opcode int `xml:"opcode,attr"` Combine bool `xml:"combine-adjacent,attr"` - Fields Fields `xml:",any"` - Reply *Reply `xml:"reply"` + Fields XMLFields `xml:",any"` + Reply *XMLReply `xml:"reply"` } -type Reply struct { - Fields Fields `xml:",any"` +type XMLReply struct { + Fields XMLFields `xml:",any"` } -type Events []*Event +type XMLEvents []*XMLEvent -type Event struct { - Name Type `xml:"name,attr"` +type XMLEvent struct { + Name string `xml:"name,attr"` Number int `xml:"number,attr"` NoSequence bool `xml:"no-sequence-number,true"` - Fields Fields `xml:",any"` + Fields XMLFields `xml:",any"` } -type Errors []*Error +type XMLErrors []*XMLError -type Error struct { - Name Type `xml:"name,attr"` +type XMLError struct { + Name string `xml:"name,attr"` Number int `xml:"number,attr"` - Fields Fields `xml:",any"` + Fields XMLFields `xml:",any"` } diff --git a/nexgb/xgbgen/xml_expression.go b/nexgb/xgbgen/xml_expression.go index dd32512..57ff62e 100644 --- a/nexgb/xgbgen/xml_expression.go +++ b/nexgb/xgbgen/xml_expression.go @@ -7,25 +7,25 @@ import ( "strconv" ) -type Expression struct { +type XMLExpression struct { XMLName xml.Name - Exprs []*Expression `xml:",any"` + Exprs []*XMLExpression `xml:",any"` Data string `xml:",chardata"` Op string `xml:"op,attr"` Ref string `xml:"ref,attr"` } -func newValueExpression(v uint) *Expression { - return &Expression{ +func newValueExpression(v uint) *XMLExpression { + return &XMLExpression{ XMLName: xml.Name{Local: "value"}, Data: fmt.Sprintf("%d", v), } } // String is for debugging. For actual use, please use 'Morph'. -func (e *Expression) String() string { +func (e *XMLExpression) String() string { switch e.XMLName.Local { case "op": return fmt.Sprintf("(%s %s %s)", e.Exprs[0], e.Op, e.Exprs[1]) @@ -55,7 +55,7 @@ func (e *Expression) String() string { // empty items in enums. // We can't compute a concrete value for expressions that rely on a context, // i.e., some field value. -func (e *Expression) Eval() uint { +func (e *XMLExpression) Eval() uint { switch e.XMLName.Local { case "op": if len(e.Exprs) != 2 { @@ -108,7 +108,7 @@ func (e *Expression) Eval() uint { panic("unreachable") } -func (e *Expression) BinaryOp(operand1, operand2 *Expression) *Expression { +func (e *XMLExpression) BinaryOp(oprnd1, oprnd2 *XMLExpression) *XMLExpression { if e.XMLName.Local != "op" { log.Panicf("Cannot perform binary operation on non-op expression: %s", e.XMLName.Local) @@ -121,17 +121,17 @@ func (e *Expression) BinaryOp(operand1, operand2 *Expression) *Expression { wrap := newValueExpression switch e.Op { case "+": - return wrap(operand1.Eval() + operand2.Eval()) + return wrap(oprnd1.Eval() + oprnd2.Eval()) case "-": - return wrap(operand1.Eval() + operand2.Eval()) + return wrap(oprnd1.Eval() + oprnd2.Eval()) case "*": - return wrap(operand1.Eval() * operand2.Eval()) + return wrap(oprnd1.Eval() * oprnd2.Eval()) case "/": - return wrap(operand1.Eval() / operand2.Eval()) + return wrap(oprnd1.Eval() / oprnd2.Eval()) case "&": - return wrap(operand1.Eval() & operand2.Eval()) + return wrap(oprnd1.Eval() & oprnd2.Eval()) case "<<": - return wrap(operand1.Eval() << operand2.Eval()) + return wrap(oprnd1.Eval() << oprnd2.Eval()) } log.Panicf("Invalid binary operator '%s' for '%s' expression.", @@ -139,7 +139,7 @@ func (e *Expression) BinaryOp(operand1, operand2 *Expression) *Expression { panic("unreachable") } -func (e *Expression) UnaryOp(operand *Expression) *Expression { +func (e *XMLExpression) UnaryOp(oprnd *XMLExpression) *XMLExpression { if e.XMLName.Local != "unop" { log.Panicf("Cannot perform unary operation on non-unop expression: %s", e.XMLName.Local) @@ -151,7 +151,7 @@ func (e *Expression) UnaryOp(operand *Expression) *Expression { switch e.Op { case "~": - return newValueExpression(^operand.Eval()) + return newValueExpression(^oprnd.Eval()) } log.Panicf("Invalid unary operator '%s' for '%s' expression.", diff --git a/nexgb/xgbgen/xml_fields.go b/nexgb/xgbgen/xml_fields.go index c1a7240..d6d99c5 100644 --- a/nexgb/xgbgen/xml_fields.go +++ b/nexgb/xgbgen/xml_fields.go @@ -23,40 +23,40 @@ import ( "strings" ) -type Fields []*Field +type XMLFields []*XMLField -type Field struct { +type XMLField struct { XMLName xml.Name // For 'pad' element - Bytes int `xml:"bytes,attr"` + Bytes uint `xml:"bytes,attr"` // For 'field', 'list', 'localfield', 'exprfield' and 'switch' elements. - Name Name `xml:"name,attr"` + Name string `xml:"name,attr"` // For 'field', 'list', 'localfield', and 'exprfield' elements. - Type Type `xml:"type,attr"` + Type string `xml:"type,attr"` // For 'list', 'exprfield' and 'switch' elements. - Expr *Expression `xml:",any"` + Expr *XMLExpression `xml:",any"` // For 'valueparm' element. - ValueMaskType Type `xml:"value-mask-type,attr"` - ValueMaskName Name `xml:"value-mask-name,attr"` - ValueListName Name `xml:"value-list-name,attr"` + ValueMaskType string `xml:"value-mask-type,attr"` + ValueMaskName string `xml:"value-mask-name,attr"` + ValueListName string `xml:"value-list-name,attr"` // For 'switch' element. - Bitcases Bitcases `xml:"bitcase"` + Bitcases XMLBitcases `xml:"bitcase"` // I don't know which elements these are for. The documentation is vague. // They also seem to be completely optional. - OptEnum Type `xml:"enum,attr"` - OptMask Type `xml:"mask,attr"` - OptAltEnum Type `xml:"altenum,attr"` + OptEnum string `xml:"enum,attr"` + OptMask string `xml:"mask,attr"` + OptAltEnum string `xml:"altenum,attr"` } // String is for debugging purposes. -func (f *Field) String() string { +func (f *XMLField) String() string { switch f.XMLName.Local { case "pad": return fmt.Sprintf("pad (%d bytes)", f.Bytes) @@ -88,7 +88,7 @@ func (f *Field) String() string { panic("unreachable") } -type Bitcases []*Bitcase +type XMLBitcases []*XMLBitcase // Bitcase represents a single expression followed by any number of fields. // Namely, if the switch's expression (all bitcases are inside a switch), @@ -98,24 +98,24 @@ type Bitcases []*Bitcase // siblings, we must exhaustively search for one of them. Essentially, // it's the closest thing to a Union I can get to in Go without interfaces. // Would an '' tag have been too much to ask? :-( -type Bitcase struct { - Fields Fields `xml:",any"` +type XMLBitcase struct { + Fields XMLFields `xml:",any"` // All the different expressions. // When it comes time to choose one, use the 'Expr' method. - ExprOp *Expression `xml:"op"` - ExprUnOp *Expression `xml:"unop"` - ExprField *Expression `xml:"fieldref"` - ExprValue *Expression `xml:"value"` - ExprBit *Expression `xml:"bit"` - ExprEnum *Expression `xml:"enumref"` - ExprSum *Expression `xml:"sumof"` - ExprPop *Expression `xml:"popcount"` + ExprOp *XMLExpression `xml:"op"` + ExprUnOp *XMLExpression `xml:"unop"` + ExprField *XMLExpression `xml:"fieldref"` + ExprValue *XMLExpression `xml:"value"` + ExprBit *XMLExpression `xml:"bit"` + ExprEnum *XMLExpression `xml:"enumref"` + ExprSum *XMLExpression `xml:"sumof"` + ExprPop *XMLExpression `xml:"popcount"` } // StringPrefix is for debugging purposes only. // StringPrefix takes a string to prefix to every extra line for formatting. -func (b *Bitcase) StringPrefix(prefix string) string { +func (b *XMLBitcase) StringPrefix(prefix string) string { fields := make([]string, len(b.Fields)) for i, field := range b.Fields { fields[i] = fmt.Sprintf("%s%s", prefix, field) @@ -126,13 +126,13 @@ func (b *Bitcase) StringPrefix(prefix string) string { // Expr chooses the only non-nil Expr* field from Bitcase. // Panic if there is more than one non-nil expression. -func (b *Bitcase) Expr() *Expression { - choices := []*Expression{ +func (b *XMLBitcase) Expr() *XMLExpression { + choices := []*XMLExpression{ b.ExprOp, b.ExprUnOp, b.ExprField, b.ExprValue, b.ExprBit, b.ExprEnum, b.ExprSum, b.ExprPop, } - var choice *Expression = nil + var choice *XMLExpression = nil numNonNil := 0 for _, c := range choices { if c != nil { -- cgit v1.2.3-70-g09d2 From 2a2d8653b3a7918dfb00dcca8937b0e878279c70 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Mon, 30 Apr 2012 02:44:31 -0400 Subject: gofmt --- nexgb/xgbgen/bufcount.go | 15 -------- nexgb/xgbgen/context.go | 4 +-- nexgb/xgbgen/expression.go | 11 +++--- nexgb/xgbgen/field.go | 19 +++++----- nexgb/xgbgen/go.go | 57 +++++++++++++++-------------- nexgb/xgbgen/main.go | 1 - nexgb/xgbgen/morph.go | 50 -------------------------- nexgb/xgbgen/representation.go | 17 +++++---- nexgb/xgbgen/size.go | 1 - nexgb/xgbgen/translation.go | 75 +++++++++++++++++++------------------- nexgb/xgbgen/type.go | 32 ++++++++--------- nexgb/xgbgen/xml.go | 81 +++++++++++++++++++++--------------------- nexgb/xgbgen/xml_expression.go | 16 ++++----- nexgb/xgbgen/xml_fields.go | 17 ++++----- 14 files changed, 163 insertions(+), 233 deletions(-) delete mode 100644 nexgb/xgbgen/bufcount.go delete mode 100644 nexgb/xgbgen/morph.go (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/xgbgen/bufcount.go b/nexgb/xgbgen/bufcount.go deleted file mode 100644 index c3a5645..0000000 --- a/nexgb/xgbgen/bufcount.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -/* - A buffer count is a mechanism by which to keep track of which byte one - is reading or writing to/from the wire. - - It's an abstraction over the fact that while such a counter is usually - fixed, it can be made variable based on values at run-time. -*/ - -type BufCount struct { - Fixed int - Exprs []*Expression -} - diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go index d3cbb81..67801c7 100644 --- a/nexgb/xgbgen/context.go +++ b/nexgb/xgbgen/context.go @@ -9,7 +9,7 @@ import ( type Context struct { protocol *Protocol - out *bytes.Buffer + out *bytes.Buffer } func newContext() *Context { @@ -20,7 +20,7 @@ func newContext() *Context { // Putln calls put and adds a new line to the end of 'format'. func (c *Context) Putln(format string, v ...interface{}) { - c.Put(format + "\n", v...) + c.Put(format+"\n", v...) } // Put is a short alias to write to 'out'. diff --git a/nexgb/xgbgen/expression.go b/nexgb/xgbgen/expression.go index a163692..7099c25 100644 --- a/nexgb/xgbgen/expression.go +++ b/nexgb/xgbgen/expression.go @@ -14,7 +14,7 @@ type Expression interface { } type BinaryOp struct { - Op string + Op string Expr1 Expression Expr2 Expression } @@ -23,7 +23,7 @@ func newBinaryOp(op string, expr1, expr2 Expression) Expression { switch { case expr1 != nil && expr2 != nil: return &BinaryOp{ - Op: op, + Op: op, Expr1: expr1, Expr2: expr2, } @@ -79,7 +79,7 @@ func (e *BinaryOp) Initialize(p *Protocol) { } type UnaryOp struct { - Op string + Op string Expr Expression } @@ -159,7 +159,7 @@ func (e *Value) String() string { return e.Reduce("", "") } -func (e *Value) Initialize(p *Protocol) { } +func (e *Value) Initialize(p *Protocol) {} type Bit struct { b uint @@ -181,7 +181,7 @@ func (e *Bit) String() string { return e.Reduce("", "") } -func (e *Bit) Initialize(p *Protocol) { } +func (e *Bit) Initialize(p *Protocol) {} type FieldRef struct { Name string @@ -273,4 +273,3 @@ func (e *SumOf) String() string { func (e *SumOf) Initialize(p *Protocol) { e.Name = SrcName(e.Name) } - diff --git a/nexgb/xgbgen/field.go b/nexgb/xgbgen/field.go index a659e6e..ed113e0 100644 --- a/nexgb/xgbgen/field.go +++ b/nexgb/xgbgen/field.go @@ -31,7 +31,7 @@ func (p *PadField) Size() Size { type SingleField struct { srcName string xmlName string - Type Type + Type Type } func (f *SingleField) Initialize(p *Protocol) { @@ -52,9 +52,9 @@ func (f *SingleField) Size() Size { } type ListField struct { - srcName string - xmlName string - Type Type + srcName string + xmlName string + Type Type LengthExpr Expression } @@ -85,8 +85,8 @@ type LocalField struct { type ExprField struct { srcName string xmlName string - Type Type - Expr Expression + Type Type + Expr Expression } func (f *ExprField) SrcName() string { @@ -132,8 +132,8 @@ func (f *ValueField) Initialize(p *Protocol) { } type SwitchField struct { - Name string - Expr Expression + Name string + Expr Expression Bitcases []*Bitcase } @@ -165,6 +165,5 @@ func (f *SwitchField) Initialize(p *Protocol) { type Bitcase struct { Fields []Field - Expr Expression + Expr Expression } - diff --git a/nexgb/xgbgen/go.go b/nexgb/xgbgen/go.go index ac3ed2c..014b76b 100644 --- a/nexgb/xgbgen/go.go +++ b/nexgb/xgbgen/go.go @@ -16,37 +16,37 @@ var xgbGenResourceIdName = "Id" // XML protocol description will produce an invalid Go program. // The types on the left *never* show themselves in the source. var BaseTypeMap = map[string]string{ - "CARD8": "byte", + "CARD8": "byte", "CARD16": "uint16", "CARD32": "uint32", - "INT8": "int8", - "INT16": "int16", - "INT32": "int32", - "BYTE": "byte", - "BOOL": "bool", - "float": "float64", + "INT8": "int8", + "INT16": "int16", + "INT32": "int32", + "BYTE": "byte", + "BOOL": "bool", + "float": "float64", "double": "float64", - "char": "byte", - "void": "byte", - "Id": "Id", + "char": "byte", + "void": "byte", + "Id": "Id", } // BaseTypeSizes should have precisely the same keys as in BaseTypeMap, // and the values should correspond to the size of the type in bytes. var BaseTypeSizes = map[string]uint{ - "CARD8": 1, + "CARD8": 1, "CARD16": 2, "CARD32": 4, - "INT8": 1, - "INT16": 2, - "INT32": 4, - "BYTE": 1, - "BOOL": 1, - "float": 4, + "INT8": 1, + "INT16": 2, + "INT32": 4, + "BYTE": 1, + "BOOL": 1, + "float": 4, "double": 8, - "char": 1, - "void": 1, - "Id": 4, + "char": 1, + "void": 1, + "Id": 4, } // TypeMap is a map from types in the XML to type names that is used @@ -54,13 +54,13 @@ var BaseTypeSizes = map[string]uint{ // type is replaced with the value type. var TypeMap = map[string]string{ "VISUALTYPE": "VisualInfo", - "DEPTH": "DepthInfo", - "SCREEN": "ScreenInfo", - "Setup": "SetupInfo", + "DEPTH": "DepthInfo", + "SCREEN": "ScreenInfo", + "Setup": "SetupInfo", } // NameMap is the same as TypeMap, but for names. -var NameMap = map[string]string{ } +var NameMap = map[string]string{} // Reading, writing and defining... @@ -151,8 +151,8 @@ func (s *Struct) ReadList(c *Context) { c.Putln("consumed := 0") c.Putln("consumed = 0 + consumed // no-op") // dirty hack for a no-op c.Putln("for i := 0; i < length; i++ {") - c.Putln("v[i], consumed = New%s(buf[b:])", s.SrcName()) - c.Putln("b += consumed") + c.Putln("v[i], consumed = New%s(buf[b:])", s.SrcName()) + c.Putln("b += consumed") c.Putln("}") c.Putln("return v, pad(b)") @@ -347,14 +347,14 @@ func (f *ListField) Read(c *Context) { length := f.LengthExpr.Reduce("v.", "") c.Putln("v.%s = make([]Id, %s)", f.SrcName(), length) c.Putln("for i := 0; i < %s; i++ {", length) - ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t) + ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t) c.Putln("}") c.Putln("") case *Base: length := f.LengthExpr.Reduce("v.", "") c.Putln("v.%s = make([]%s, %s)", f.SrcName(), t.SrcName(), length) c.Putln("for i := 0; i < %s; i++ {", length) - ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t) + ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t) c.Putln("}") c.Putln("") case *Struct: @@ -408,4 +408,3 @@ func (f *SwitchField) Define(c *Context) { func (f *SwitchField) Read(c *Context) { c.Putln("// reading switch field: %s (%s)", f.Name, f.Expr) } - diff --git a/nexgb/xgbgen/main.go b/nexgb/xgbgen/main.go index 33f7971..fd5eac7 100644 --- a/nexgb/xgbgen/main.go +++ b/nexgb/xgbgen/main.go @@ -62,4 +62,3 @@ func main() { } } } - diff --git a/nexgb/xgbgen/morph.go b/nexgb/xgbgen/morph.go deleted file mode 100644 index c39b333..0000000 --- a/nexgb/xgbgen/morph.go +++ /dev/null @@ -1,50 +0,0 @@ - -// Morph cascades down all of the XML and calls each type's corresponding -// Morph function with itself as an argument (the context). -func (x *XML) Morph(c *Context) { - // Start the header... - c.Putln("package xgb") - c.Putln("/*") - c.Putln("\tX protocol API for '%s.xml'.", c.xml.Header) - c.Putln("\tThis file is automatically generated. Edit at your own peril!") - c.Putln("\tGenerated on %s", - time.Now().Format("Jan 2, 2006 at 3:04:05pm MST")) - c.Putln("*/") - c.Putln("") - - x.Imports.Morph(c) - c.Putln("") - - x.Enums.Morph(c) - c.Putln("") - - x.Xids.Morph(c) - c.Putln("") - - x.XidUnions.Morph(c) - c.Putln("") - - x.TypeDefs.Morph(c) - c.Putln("") - - x.Structs.Morph(c) - c.Putln("") - - x.Unions.Morph(c) - c.Putln("") - - x.Requests.Morph(c) - c.Putln("") - - x.Errors.Morph(c) - c.Putln("") - - x.ErrorCopies.Morph(c) - c.Putln("") - - x.Events.Morph(c) - c.Putln("") - - x.EventCopies.Morph(c) - c.Putln("") -} diff --git a/nexgb/xgbgen/representation.go b/nexgb/xgbgen/representation.go index 928e219..2d33a45 100644 --- a/nexgb/xgbgen/representation.go +++ b/nexgb/xgbgen/representation.go @@ -1,14 +1,14 @@ package main type Protocol struct { - Name string - ExtXName string - ExtName string + Name string + ExtXName string + ExtName string MajorVersion string MinorVersion string - Imports []*Protocol - Types []Type + Imports []*Protocol + Types []Type Requests []*Request } @@ -28,10 +28,10 @@ func (p *Protocol) Initialize() { type Request struct { srcName string xmlName string - Opcode int + Opcode int Combine bool - Fields []Field - Reply *Reply + Fields []Field + Reply *Reply } func (r *Request) Initialize(p *Protocol) { @@ -53,4 +53,3 @@ func (r *Reply) Initialize(p *Protocol) { field.Initialize(p) } } - diff --git a/nexgb/xgbgen/size.go b/nexgb/xgbgen/size.go index d00e297..70edb8f 100644 --- a/nexgb/xgbgen/size.go +++ b/nexgb/xgbgen/size.go @@ -19,4 +19,3 @@ func (s1 Size) Add(s2 Size) Size { func (s1 Size) Multiply(s2 Size) Size { return Size{newBinaryOp("*", s1, s2)} } - diff --git a/nexgb/xgbgen/translation.go b/nexgb/xgbgen/translation.go index 85e756d..36daa8b 100644 --- a/nexgb/xgbgen/translation.go +++ b/nexgb/xgbgen/translation.go @@ -1,4 +1,5 @@ package main + /* translation.go provides a 'Translate' method on every XML type that converts the XML type into our "better" representation. @@ -19,14 +20,14 @@ import ( func (xml *XML) Translate() *Protocol { protocol := &Protocol{ - Name: xml.Header, - ExtXName: xml.ExtensionXName, - ExtName: xml.ExtensionName, + Name: xml.Header, + ExtXName: xml.ExtensionXName, + ExtName: xml.ExtensionName, MajorVersion: xml.MajorVersion, MinorVersion: xml.MinorVersion, - Imports: make([]*Protocol, 0), - Types: make([]Type, 0), + Imports: make([]*Protocol, 0), + Types: make([]Type, 0), Requests: make([]*Request, len(xml.Requests)), } @@ -40,7 +41,7 @@ func (xml *XML) Translate() *Protocol { newBaseType := &Base{ srcName: srcName, xmlName: xmlName, - size: newFixedSize(BaseTypeSizes[xmlName]), + size: newFixedSize(BaseTypeSizes[xmlName]), } protocol.Types = append(protocol.Types, newBaseType) } @@ -105,12 +106,12 @@ func (xml *XML) Translate() *Protocol { func (x *XMLEnum) Translate() *Enum { enum := &Enum{ xmlName: x.Name, - Items: make([]*EnumItem, len(x.Items)), + Items: make([]*EnumItem, len(x.Items)), } for i, item := range x.Items { enum.Items[i] = &EnumItem{ xmlName: item.Name, - Expr: item.Expr.Translate(), + Expr: item.Expr.Translate(), } } return enum @@ -125,16 +126,16 @@ func (x *XMLXid) Translate() *Resource { func (x *XMLTypeDef) Translate() *TypeDef { return &TypeDef{ xmlName: x.New, - Old: newTranslation(x.Old), + Old: newTranslation(x.Old), } } func (x *XMLEvent) Translate() *Event { ev := &Event{ - xmlName: x.Name, - Number: x.Number, + xmlName: x.Name, + Number: x.Number, NoSequence: x.NoSequence, - Fields: make([]Field, len(x.Fields)), + Fields: make([]Field, len(x.Fields)), } for i, field := range x.Fields { ev.Fields[i] = field.Translate() @@ -145,16 +146,16 @@ func (x *XMLEvent) Translate() *Event { func (x *XMLEventCopy) Translate() *EventCopy { return &EventCopy{ xmlName: x.Name, - Number: x.Number, - Old: newTranslation(x.Ref), + Number: x.Number, + Old: newTranslation(x.Ref), } } func (x *XMLError) Translate() *Error { err := &Error{ xmlName: x.Name, - Number: x.Number, - Fields: make([]Field, len(x.Fields)), + Number: x.Number, + Fields: make([]Field, len(x.Fields)), } for i, field := range x.Fields { err.Fields[i] = field.Translate() @@ -165,15 +166,15 @@ func (x *XMLError) Translate() *Error { func (x *XMLErrorCopy) Translate() *ErrorCopy { return &ErrorCopy{ xmlName: x.Name, - Number: x.Number, - Old: newTranslation(x.Ref), + Number: x.Number, + Old: newTranslation(x.Ref), } } func (x *XMLStruct) Translate() *Struct { s := &Struct{ xmlName: x.Name, - Fields: make([]Field, len(x.Fields)), + Fields: make([]Field, len(x.Fields)), } for i, field := range x.Fields { s.Fields[i] = field.Translate() @@ -184,7 +185,7 @@ func (x *XMLStruct) Translate() *Struct { func (x *XMLUnion) Translate() *Union { u := &Union{ xmlName: x.Name, - Fields: make([]Field, len(x.Fields)), + Fields: make([]Field, len(x.Fields)), } for i, field := range x.Fields { u.Fields[i] = field.Translate() @@ -195,10 +196,10 @@ func (x *XMLUnion) Translate() *Union { func (x *XMLRequest) Translate() *Request { r := &Request{ xmlName: x.Name, - Opcode: x.Opcode, + Opcode: x.Opcode, Combine: x.Combine, - Fields: make([]Field, len(x.Fields)), - Reply: x.Reply.Translate(), + Fields: make([]Field, len(x.Fields)), + Reply: x.Reply.Translate(), } for i, field := range x.Fields { r.Fields[i] = field.Translate() @@ -211,7 +212,7 @@ func (x *XMLRequest) Translate() *Request { // (i.e., a parameter in the caller but does not get send over the wire.) stringLenLocal := &LocalField{&SingleField{ xmlName: "string_len", - Type: newTranslation("CARD16"), + Type: newTranslation("CARD16"), }} r.Fields = append(r.Fields, stringLenLocal) @@ -243,7 +244,7 @@ func (x *XMLExpression) Translate() Expression { log.Panicf("'op' found %d expressions; expected 2.", len(x.Exprs)) } return &BinaryOp{ - Op: x.Op, + Op: x.Op, Expr1: x.Exprs[0].Translate(), Expr2: x.Exprs[1].Translate(), } @@ -252,7 +253,7 @@ func (x *XMLExpression) Translate() Expression { log.Panicf("'unop' found %d expressions; expected 1.", len(x.Exprs)) } return &UnaryOp{ - Op: x.Op, + Op: x.Op, Expr: x.Exprs[0].Translate(), } case "popcount": @@ -279,7 +280,7 @@ func (x *XMLExpression) Translate() Expression { x.Data) } if bit < 0 || bit > 31 { - log.Panicf("A 'bit' literal must be in the range [0, 31], but " + + log.Panicf("A 'bit' literal must be in the range [0, 31], but "+ " is %d", bit) } return &Bit{ @@ -300,7 +301,7 @@ func (x *XMLExpression) Translate() Expression { } } - log.Panicf("Unrecognized tag '%s' in expression context. Expected one of " + + log.Panicf("Unrecognized tag '%s' in expression context. Expected one of "+ "op, fieldref, value, bit, enumref, unop, sumof or popcount.", x.XMLName.Local) panic("unreachable") @@ -315,24 +316,24 @@ func (x *XMLField) Translate() Field { case "field": return &SingleField{ xmlName: x.Name, - Type: newTranslation(x.Type), + Type: newTranslation(x.Type), } case "list": return &ListField{ - xmlName: x.Name, - Type: newTranslation(x.Type), + xmlName: x.Name, + Type: newTranslation(x.Type), LengthExpr: x.Expr.Translate(), } case "localfield": return &LocalField{&SingleField{ xmlName: x.Name, - Type: newTranslation(x.Type), + Type: newTranslation(x.Type), }} case "exprfield": return &ExprField{ xmlName: x.Name, - Type: newTranslation(x.Type), - Expr: x.Expr.Translate(), + Type: newTranslation(x.Type), + Expr: x.Expr.Translate(), } case "valueparam": return &ValueField{ @@ -342,8 +343,8 @@ func (x *XMLField) Translate() Field { } case "switch": swtch := &SwitchField{ - Name: x.Name, - Expr: x.Expr.Translate(), + Name: x.Name, + Expr: x.Expr.Translate(), Bitcases: make([]*Bitcase, len(x.Bitcases)), } for i, bitcase := range x.Bitcases { @@ -358,7 +359,7 @@ func (x *XMLField) Translate() Field { func (x *XMLBitcase) Translate() *Bitcase { b := &Bitcase{ - Expr: x.Expr().Translate(), + Expr: x.Expr().Translate(), Fields: make([]Field, len(x.Fields)), } for i, field := range x.Fields { diff --git a/nexgb/xgbgen/type.go b/nexgb/xgbgen/type.go index 1574922..9fbef65 100644 --- a/nexgb/xgbgen/type.go +++ b/nexgb/xgbgen/type.go @@ -77,7 +77,7 @@ func (t *Translation) Initialize(p *Protocol) { type Base struct { srcName string xmlName string - size Size + size Size } func (b *Base) SrcName() string { @@ -99,13 +99,13 @@ func (b *Base) Initialize(p *Protocol) { type Enum struct { srcName string xmlName string - Items []*EnumItem + Items []*EnumItem } type EnumItem struct { srcName string xmlName string - Expr Expression + Expr Expression } func (enum *Enum) SrcName() string { @@ -154,7 +154,7 @@ func (r *Resource) Initialize(p *Protocol) { type TypeDef struct { srcName string xmlName string - Old Type + Old Type } func (t *TypeDef) SrcName() string { @@ -175,11 +175,11 @@ func (t *TypeDef) Initialize(p *Protocol) { } type Event struct { - srcName string - xmlName string - Number int + srcName string + xmlName string + Number int NoSequence bool - Fields []Field + Fields []Field } func (e *Event) SrcName() string { @@ -208,8 +208,8 @@ func (e *Event) EvType() string { type EventCopy struct { srcName string xmlName string - Old Type - Number int + Old Type + Number int } func (e *EventCopy) SrcName() string { @@ -239,8 +239,8 @@ func (e *EventCopy) EvType() string { type Error struct { srcName string xmlName string - Number int - Fields []Field + Number int + Fields []Field } func (e *Error) SrcName() string { @@ -270,8 +270,8 @@ func (e *Error) ErrType() string { type ErrorCopy struct { srcName string xmlName string - Old Type - Number int + Old Type + Number int } func (e *ErrorCopy) SrcName() string { @@ -305,7 +305,7 @@ func (e *ErrorCopy) ErrType() string { type Struct struct { srcName string xmlName string - Fields []Field + Fields []Field } func (s *Struct) SrcName() string { @@ -334,7 +334,7 @@ func (s *Struct) Initialize(p *Protocol) { type Union struct { srcName string xmlName string - Fields []Field + Fields []Field } func (u *Union) SrcName() string { diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go index 7e50831..f219c2d 100644 --- a/nexgb/xgbgen/xml.go +++ b/nexgb/xgbgen/xml.go @@ -8,29 +8,29 @@ import ( type XML struct { // Root 'xcb' element properties. - XMLName xml.Name `xml:"xcb"` - Header string `xml:"header,attr"` - ExtensionXName string `xml:"extension-xname,attr"` - ExtensionName string `xml:"extension-name,attr"` - MajorVersion string `xml:"major-version,attr"` - MinorVersion string `xml:"minor-version,attr"` + XMLName xml.Name `xml:"xcb"` + Header string `xml:"header,attr"` + ExtensionXName string `xml:"extension-xname,attr"` + ExtensionName string `xml:"extension-name,attr"` + MajorVersion string `xml:"major-version,attr"` + MinorVersion string `xml:"minor-version,attr"` // Types for all top-level elements. // First are the simple ones. - Imports XMLImports `xml:"import"` - Enums XMLEnums `xml:"enum"` - Xids XMLXids `xml:"xidtype"` - XidUnions XMLXids `xml:"xidunion"` - TypeDefs XMLTypeDefs `xml:"typedef"` + Imports XMLImports `xml:"import"` + Enums XMLEnums `xml:"enum"` + Xids XMLXids `xml:"xidtype"` + XidUnions XMLXids `xml:"xidunion"` + TypeDefs XMLTypeDefs `xml:"typedef"` EventCopies XMLEventCopies `xml:"eventcopy"` ErrorCopies XMLErrorCopies `xml:"errorcopy"` // Here are the complex ones, i.e., anything with "structure contents" - Structs XMLStructs `xml:"struct"` - Unions XMLUnions `xml:"union"` + Structs XMLStructs `xml:"struct"` + Unions XMLUnions `xml:"union"` Requests XMLRequests `xml:"request"` - Events XMLEvents `xml:"event"` - Errors XMLErrors `xml:"error"` + Events XMLEvents `xml:"event"` + Errors XMLErrors `xml:"error"` } type XMLImports []*XMLImport @@ -39,14 +39,14 @@ func (imports XMLImports) Eval() { for _, imp := range imports { xmlBytes, err := ioutil.ReadFile(*protoPath + "/" + imp.Name + ".xml") if err != nil { - log.Fatalf("Could not read X protocol description for import " + + log.Fatalf("Could not read X protocol description for import "+ "'%s' because: %s", imp.Name, err) } imp.xml = &XML{} err = xml.Unmarshal(xmlBytes, imp.xml) if err != nil { - log.Fatal("Could not parse X protocol description for import " + + log.Fatal("Could not parse X protocol description for import "+ "'%s' because: %s", imp.Name, err) } @@ -57,18 +57,18 @@ func (imports XMLImports) Eval() { type XMLImport struct { Name string `xml:",chardata"` - xml *XML `xml:"-"` + xml *XML `xml:"-"` } type XMLEnums []XMLEnum type XMLEnum struct { - Name string `xml:"name,attr"` + Name string `xml:"name,attr"` Items []*XMLEnumItem `xml:"item"` } type XMLEnumItem struct { - Name string `xml:"name,attr"` + Name string `xml:"name,attr"` Expr *XMLExpression `xml:",any"` } @@ -76,7 +76,7 @@ type XMLXids []*XMLXid type XMLXid struct { XMLName xml.Name - Name string `xml:"name,attr"` + Name string `xml:"name,attr"` } type XMLTypeDefs []*XMLTypeDef @@ -89,41 +89,41 @@ type XMLTypeDef struct { type XMLEventCopies []*XMLEventCopy type XMLEventCopy struct { - Name string `xml:"name,attr"` - Number int `xml:"number,attr"` - Ref string `xml:"ref,attr"` + Name string `xml:"name,attr"` + Number int `xml:"number,attr"` + Ref string `xml:"ref,attr"` } type XMLErrorCopies []*XMLErrorCopy type XMLErrorCopy struct { - Name string `xml:"name,attr"` - Number int `xml:"number,attr"` - Ref string `xml:"ref,attr"` + Name string `xml:"name,attr"` + Number int `xml:"number,attr"` + Ref string `xml:"ref,attr"` } type XMLStructs []*XMLStruct type XMLStruct struct { - Name string `xml:"name,attr"` + Name string `xml:"name,attr"` Fields XMLFields `xml:",any"` } type XMLUnions []*XMLUnion type XMLUnion struct { - Name string `xml:"name,attr"` + Name string `xml:"name,attr"` Fields XMLFields `xml:",any"` } type XMLRequests []*XMLRequest type XMLRequest struct { - Name string `xml:"name,attr"` - Opcode int `xml:"opcode,attr"` - Combine bool `xml:"combine-adjacent,attr"` - Fields XMLFields `xml:",any"` - Reply *XMLReply `xml:"reply"` + Name string `xml:"name,attr"` + Opcode int `xml:"opcode,attr"` + Combine bool `xml:"combine-adjacent,attr"` + Fields XMLFields `xml:",any"` + Reply *XMLReply `xml:"reply"` } type XMLReply struct { @@ -133,17 +133,16 @@ type XMLReply struct { type XMLEvents []*XMLEvent type XMLEvent struct { - Name string `xml:"name,attr"` - Number int `xml:"number,attr"` - NoSequence bool `xml:"no-sequence-number,true"` - Fields XMLFields `xml:",any"` + Name string `xml:"name,attr"` + Number int `xml:"number,attr"` + NoSequence bool `xml:"no-sequence-number,true"` + Fields XMLFields `xml:",any"` } type XMLErrors []*XMLError type XMLError struct { - Name string `xml:"name,attr"` - Number int `xml:"number,attr"` + Name string `xml:"name,attr"` + Number int `xml:"number,attr"` Fields XMLFields `xml:",any"` } - diff --git a/nexgb/xgbgen/xml_expression.go b/nexgb/xgbgen/xml_expression.go index 57ff62e..2989668 100644 --- a/nexgb/xgbgen/xml_expression.go +++ b/nexgb/xgbgen/xml_expression.go @@ -13,14 +13,14 @@ type XMLExpression struct { Exprs []*XMLExpression `xml:",any"` Data string `xml:",chardata"` - Op string `xml:"op,attr"` - Ref string `xml:"ref,attr"` + Op string `xml:"op,attr"` + Ref string `xml:"ref,attr"` } func newValueExpression(v uint) *XMLExpression { return &XMLExpression{ XMLName: xml.Name{Local: "value"}, - Data: fmt.Sprintf("%d", v), + Data: fmt.Sprintf("%d", v), } } @@ -87,22 +87,22 @@ func (e *XMLExpression) Eval() uint { e.Data) } if bit < 0 || bit > 31 { - log.Panicf("A 'bit' literal must be in the range [0, 31], but " + + log.Panicf("A 'bit' literal must be in the range [0, 31], but "+ " is %d", bit) } return 1 << uint(bit) case "fieldref": - log.Panicf("Cannot compute concrete value of 'fieldref' in " + + log.Panicf("Cannot compute concrete value of 'fieldref' in "+ "expression '%s'.", e) case "enumref": - log.Panicf("Cannot compute concrete value of 'enumref' in " + + log.Panicf("Cannot compute concrete value of 'enumref' in "+ "expression '%s'.", e) case "sumof": - log.Panicf("Cannot compute concrete value of 'sumof' in " + + log.Panicf("Cannot compute concrete value of 'sumof' in "+ "expression '%s'.", e) } - log.Panicf("Unrecognized tag '%s' in expression context. Expected one of " + + log.Panicf("Unrecognized tag '%s' in expression context. Expected one of "+ "op, fieldref, value, bit, enumref, unop, sumof or popcount.", e.XMLName.Local) panic("unreachable") diff --git a/nexgb/xgbgen/xml_fields.go b/nexgb/xgbgen/xml_fields.go index d6d99c5..991141b 100644 --- a/nexgb/xgbgen/xml_fields.go +++ b/nexgb/xgbgen/xml_fields.go @@ -1,4 +1,5 @@ package main + /* A series of fields should be taken as "structure contents", and *not* just the single 'field' elements. Namely, 'fields' subsumes 'field' @@ -50,8 +51,8 @@ type XMLField struct { // I don't know which elements these are for. The documentation is vague. // They also seem to be completely optional. - OptEnum string `xml:"enum,attr"` - OptMask string `xml:"mask,attr"` + OptEnum string `xml:"enum,attr"` + OptMask string `xml:"mask,attr"` OptAltEnum string `xml:"altenum,attr"` } @@ -103,14 +104,14 @@ type XMLBitcase struct { // All the different expressions. // When it comes time to choose one, use the 'Expr' method. - ExprOp *XMLExpression `xml:"op"` - ExprUnOp *XMLExpression `xml:"unop"` + ExprOp *XMLExpression `xml:"op"` + ExprUnOp *XMLExpression `xml:"unop"` ExprField *XMLExpression `xml:"fieldref"` ExprValue *XMLExpression `xml:"value"` - ExprBit *XMLExpression `xml:"bit"` - ExprEnum *XMLExpression `xml:"enumref"` - ExprSum *XMLExpression `xml:"sumof"` - ExprPop *XMLExpression `xml:"popcount"` + ExprBit *XMLExpression `xml:"bit"` + ExprEnum *XMLExpression `xml:"enumref"` + ExprSum *XMLExpression `xml:"sumof"` + ExprPop *XMLExpression `xml:"popcount"` } // StringPrefix is for debugging purposes only. -- cgit v1.2.3-70-g09d2 From 39507f86ab0468292c24081a80f41c1799d6b477 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Wed, 2 May 2012 01:46:30 -0400 Subject: finally starting on the crescendo: requests and replies. --- nexgb/xgbgen/context.go | 3 ++ nexgb/xgbgen/field.go | 4 ++- nexgb/xgbgen/go.go | 9 ------ nexgb/xgbgen/go_error.go | 65 ++++++++++++++++++++++++++++++------------ nexgb/xgbgen/go_event.go | 21 ++++++++------ nexgb/xgbgen/go_list.go | 4 +-- nexgb/xgbgen/go_reply.go | 19 ++++++++++++ nexgb/xgbgen/representation.go | 53 ++++++++++++++++++++++++++++++++++ nexgb/xgbgen/translation.go | 12 ++++---- nexgb/xgbgen/type.go | 9 ++++-- nexgb/xgbgen/xml.go | 2 +- 11 files changed, 153 insertions(+), 48 deletions(-) create mode 100644 nexgb/xgbgen/go_reply.go (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go index 67801c7..33641b3 100644 --- a/nexgb/xgbgen/context.go +++ b/nexgb/xgbgen/context.go @@ -51,4 +51,7 @@ func (c *Context) Morph(xmlBytes []byte) { for _, typ := range c.protocol.Types { typ.Define(c) } + for _, req := range c.protocol.Requests { + req.Define(c) + } } diff --git a/nexgb/xgbgen/field.go b/nexgb/xgbgen/field.go index 6d39af2..8d8412c 100644 --- a/nexgb/xgbgen/field.go +++ b/nexgb/xgbgen/field.go @@ -102,8 +102,10 @@ func (f *ListField) Size() Size { return newExpressionSize(simpleLen) case *Resource: return newExpressionSize(simpleLen) + case *TypeDef: + return newExpressionSize(simpleLen) default: - log.Fatalf("Cannot compute list size with type '%T'.", f.Type) + log.Panicf("Cannot compute list size with type '%T'.", f.Type) } panic("unreachable") } diff --git a/nexgb/xgbgen/go.go b/nexgb/xgbgen/go.go index 11e413b..b7b153e 100644 --- a/nexgb/xgbgen/go.go +++ b/nexgb/xgbgen/go.go @@ -105,57 +105,48 @@ func (f *PadField) Write(c *Context) { // Local fields func (f *LocalField) Define(c *Context) { c.Putln("// local field: %s %s", f.SrcName(), f.Type.SrcName()) - panic("todo") } func (f *LocalField) Read(c *Context) { c.Putln("// reading local field: %s (%s) :: %s", f.SrcName(), f.Size(), f.Type.SrcName()) - panic("todo") } func (f *LocalField) Write(c *Context) { c.Putln("// writing local field: %s (%s) :: %s", f.SrcName(), f.Size(), f.Type.SrcName()) - panic("todo") } // Expr fields func (f *ExprField) Define(c *Context) { c.Putln("// expression field: %s %s (%s)", f.SrcName(), f.Type.SrcName(), f.Expr) - panic("todo") } func (f *ExprField) Read(c *Context) { c.Putln("// reading expression field: %s (%s) (%s) :: %s", f.SrcName(), f.Size(), f.Expr, f.Type.SrcName()) - panic("todo") } func (f *ExprField) Write(c *Context) { c.Putln("// writing expression field: %s (%s) (%s) :: %s", f.SrcName(), f.Size(), f.Expr, f.Type.SrcName()) - panic("todo") } // Value field func (f *ValueField) Define(c *Context) { c.Putln("// valueparam field: type: %s, mask name: %s, list name: %s", f.MaskType.SrcName(), f.MaskName, f.ListName) - panic("todo") } func (f *ValueField) Read(c *Context) { c.Putln("// reading valueparam: type: %s, mask name: %s, list name: %s", f.MaskType.SrcName(), f.MaskName, f.ListName) - panic("todo") } func (f *ValueField) Write(c *Context) { c.Putln("// writing valueparam: type: %s, mask name: %s, list name: %s", f.MaskType.SrcName(), f.MaskName, f.ListName) - panic("todo") } // Switch field diff --git a/nexgb/xgbgen/go_error.go b/nexgb/xgbgen/go_error.go index 493a8b9..17c0db5 100644 --- a/nexgb/xgbgen/go_error.go +++ b/nexgb/xgbgen/go_error.go @@ -3,15 +3,53 @@ package main // Error types func (e *Error) Define(c *Context) { c.Putln("// Error definition %s (%d)", e.SrcName(), e.Number) + c.Putln("// Size: %s", e.Size()) + c.Putln("") + c.Putln("const %s = %d", e.ErrConst(), e.Number) + c.Putln("") + c.Putln("type %s struct {", e.ErrType()) + c.Putln("Sequence uint16") + c.Putln("NiceName string") + for _, field := range e.Fields { + field.Define(c) + } + c.Putln("}") c.Putln("") -} -func (e *Error) Read(c *Context, prefix string) { - c.Putln("// Error read %s", e.SrcName()) + // Read defines a function that transforms a byte slice into this + // error struct. + e.Read(c) + + // Makes sure that this error type is an Error interface. + c.Putln("func (v %s) ImplementsError() { }", e.ErrType()) + c.Putln("") + + // Let's the XGB event loop read this error. + c.Putln("func init() {") + c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.ErrType()) + c.Putln("}") + c.Putln("") } -func (e *Error) Write(c *Context, prefix string) { - c.Putln("// Error write %s", e.SrcName()) +func (e *Error) Read(c *Context) { + c.Putln("// Error read %s", e.SrcName()) + c.Putln("func New%s(buf []byte) %s {", e.ErrType(), e.ErrType()) + c.Putln("v := %s{}", e.ErrType()) + c.Putln("v.NiceName = \"%s\"", e.SrcName()) + c.Putln("") + c.Putln("b := 1 // skip error determinant") + c.Putln("b += 1 // don't read error number") + c.Putln("") + c.Putln("v.Sequence = get16(buf[b:])") + c.Putln("b += 2") + c.Putln("") + for _, field := range e.Fields { + field.Read(c) + c.Putln("") + } + c.Putln("return v") + c.Putln("}") + c.Putln("") } // ErrorCopy types @@ -27,31 +65,20 @@ func (e *ErrorCopy) Define(c *Context) { // error struct. e.Read(c) - // Write defines a function that transoforms this error struct into - // a byte slice. - e.Write(c) - // Makes sure that this error type is an Error interface. c.Putln("func (err %s) ImplementsError() { }", e.ErrType()) c.Putln("") // Let's the XGB know how to read this error. c.Putln("func init() {") - c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.SrcName()) + c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.ErrType()) c.Putln("}") c.Putln("") } func (e *ErrorCopy) Read(c *Context) { - c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.ErrType()) - c.Putln("return (%s)(New%s(buf))", e.ErrType(), e.Old.SrcName()) - c.Putln("}") - c.Putln("") -} - -func (e *ErrorCopy) Write(c *Context) { - c.Putln("func (err %s) Bytes() []byte {", e.ErrType()) - c.Putln("return (%s)(err).Bytes()", e.Old.(*Error).ErrType()) + c.Putln("func New%s(buf []byte) %s {", e.ErrType(), e.ErrType()) + c.Putln("return %s(New%s(buf))", e.ErrType(), e.Old.(*Error).ErrType()) c.Putln("}") c.Putln("") } diff --git a/nexgb/xgbgen/go_event.go b/nexgb/xgbgen/go_event.go index e6d40b0..fe2a77e 100644 --- a/nexgb/xgbgen/go_event.go +++ b/nexgb/xgbgen/go_event.go @@ -4,8 +4,13 @@ package main 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()) - c.Putln("Sequence uint16") + if !e.NoSequence { + c.Putln("Sequence uint16") + } for _, field := range e.Fields { field.Define(c) } @@ -16,7 +21,7 @@ func (e *Event) Define(c *Context) { // event struct. e.Read(c) - // Write defines a function that transoforms this event struct into + // Write defines a function that transforms this event struct into // a byte slice. e.Write(c) @@ -26,14 +31,14 @@ func (e *Event) Define(c *Context) { // Let's the XGB event loop read this event. c.Putln("func init() {") - c.Putln("newEventFuncs[%d] = New%s", e.Number, e.SrcName()) + 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.SrcName(), e.EvType()) + 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("") @@ -97,21 +102,21 @@ func (e *EventCopy) Define(c *Context) { // Let's the XGB event loop read this event. c.Putln("func init() {") - c.Putln("newEventFuncs[%d] = New%s", e.Number, e.SrcName()) + 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.SrcName(), e.EvType()) - c.Putln("return (%s)(New%s(buf))", e.EvType(), e.Old.SrcName()) + 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("return %s(ev).Bytes()", e.Old.(*Event).EvType()) c.Putln("}") c.Putln("") } diff --git a/nexgb/xgbgen/go_list.go b/nexgb/xgbgen/go_list.go index a95ba71..67ecd34 100644 --- a/nexgb/xgbgen/go_list.go +++ b/nexgb/xgbgen/go_list.go @@ -41,7 +41,7 @@ func (f *ListField) Read(c *Context) { f.SrcName(), t.SrcName(), f.LengthExpr.Reduce("v.", "")) c.Putln("b += Read%sList(buf[b:], v.%s)", t.SrcName(), f.SrcName()) default: - log.Fatalf("Cannot read list field '%s' with %T type.", + log.Panicf("Cannot read list field '%s' with %T type.", f.XmlName(), f.Type) } } @@ -70,7 +70,7 @@ func (f *ListField) Write(c *Context) { case *Struct: c.Putln("b += %sListBytes(buf[b:], v.%s)", t.SrcName(), f.SrcName()) default: - log.Fatalf("Cannot read list field '%s' with %T type.", + log.Panicf("Cannot read list field '%s' with %T type.", f.XmlName(), f.Type) } } diff --git a/nexgb/xgbgen/go_reply.go b/nexgb/xgbgen/go_reply.go new file mode 100644 index 0000000..e561d9c --- /dev/null +++ b/nexgb/xgbgen/go_reply.go @@ -0,0 +1,19 @@ +package main + +func (r *Request) Define(c *Context) { + c.Putln("// Request %s", r.SrcName()) + c.Putln("// size: %s", r.Size(c)) + c.Putln("") + if r.Reply != nil { + c.Putln("// Request reply for %s", r.SrcName()) + c.Putln("// size: %s", r.Reply.Size()) + c.Putln("type %s struct {", r.ReplyName()) + c.Putln("Sequence uint16") + for _, field := range r.Reply.Fields { + field.Define(c) + } + c.Putln("}") + c.Putln("") + } +} + diff --git a/nexgb/xgbgen/representation.go b/nexgb/xgbgen/representation.go index 2d33a45..e5d2202 100644 --- a/nexgb/xgbgen/representation.go +++ b/nexgb/xgbgen/representation.go @@ -1,5 +1,10 @@ package main +import ( + "fmt" + "log" +) + type Protocol struct { Name string ExtXName string @@ -44,10 +49,58 @@ func (r *Request) Initialize(p *Protocol) { } } +func (r *Request) SrcName() string { + return r.srcName +} + +func (r *Request) XmlName() string { + return r.xmlName +} + +func (r *Request) ReplyName() string { + if r.Reply == nil { + log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.", + r.SrcName()) + } + return fmt.Sprintf("%sReply", r.SrcName()) +} + +// Size for Request needs a context. +// Namely, if this is an extension, we need to account for *four* bytes +// of a header (extension opcode, request opcode, and the sequence number). +// If it's a core protocol request, then we only account for *three* +// bytes of the header (remove the extension opcode). +func (r *Request) Size(c *Context) Size { + size := newFixedSize(0) + + if c.protocol.Name == "xproto" { + size = size.Add(newFixedSize(3)) + } else { + size = size.Add(newFixedSize(4)) + } + + for _, field := range r.Fields { + size = size.Add(field.Size()) + } + return size +} + type Reply struct { Fields []Field } +func (r *Reply) Size() Size { + size := newFixedSize(0) + + // Account for reply discriminant, sequence number and reply length + size = size.Add(newFixedSize(7)) + + for _, field := range r.Fields { + size = size.Add(field.Size()) + } + return size +} + func (r *Reply) Initialize(p *Protocol) { for _, field := range r.Fields { field.Initialize(p) diff --git a/nexgb/xgbgen/translation.go b/nexgb/xgbgen/translation.go index 36daa8b..9c7603b 100644 --- a/nexgb/xgbgen/translation.go +++ b/nexgb/xgbgen/translation.go @@ -210,11 +210,13 @@ func (x *XMLRequest) Translate() *Request { // computation of the 'odd_length' field. However, 'string_len' is not // defined. Therefore, let's forcefully add it as a 'local field'. // (i.e., a parameter in the caller but does not get send over the wire.) - stringLenLocal := &LocalField{&SingleField{ - xmlName: "string_len", - Type: newTranslation("CARD16"), - }} - r.Fields = append(r.Fields, stringLenLocal) + if x.Name == "QueryTextExtents" { + stringLenLocal := &LocalField{&SingleField{ + xmlName: "string_len", + Type: newTranslation("CARD16"), + }} + r.Fields = append(r.Fields, stringLenLocal) + } return r } diff --git a/nexgb/xgbgen/type.go b/nexgb/xgbgen/type.go index 1e06bda..d8e76a2 100644 --- a/nexgb/xgbgen/type.go +++ b/nexgb/xgbgen/type.go @@ -221,7 +221,7 @@ func (e *EventCopy) XmlName() string { } func (e *EventCopy) Size() Size { - panic("Cannot take size of EventCopy type.") + return newExpressionSize(&Value{v: 32}) } func (e *EventCopy) Initialize(p *Protocol) { @@ -252,11 +252,14 @@ func (e *Error) XmlName() string { } func (e *Error) Size() Size { - panic("Cannot take size of Error type.") + return newExpressionSize(&Value{v: 32}) } func (e *Error) Initialize(p *Protocol) { e.srcName = TypeSrcName(p, e) + for _, field := range e.Fields { + field.Initialize(p) + } } func (e *Error) ErrConst() string { @@ -283,7 +286,7 @@ func (e *ErrorCopy) XmlName() string { } func (e *ErrorCopy) Size() Size { - panic("Cannot take size of ErrorCopy type.") + return newExpressionSize(&Value{v: 32}) } func (e *ErrorCopy) Initialize(p *Protocol) { diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go index f219c2d..1b2f89a 100644 --- a/nexgb/xgbgen/xml.go +++ b/nexgb/xgbgen/xml.go @@ -135,7 +135,7 @@ type XMLEvents []*XMLEvent type XMLEvent struct { Name string `xml:"name,attr"` Number int `xml:"number,attr"` - NoSequence bool `xml:"no-sequence-number,true"` + NoSequence bool `xml:"no-sequence-number,attr"` Fields XMLFields `xml:",any"` } -- cgit v1.2.3-70-g09d2 From 18b2d420b092d71313f0c05210c3038ff32483e7 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Sun, 6 May 2012 02:21:31 -0400 Subject: added documentation and did some slight restructuring. it's party time. --- nexgb/xgbgen/context.go | 2 + nexgb/xgbgen/doc.go | 74 +++++++++++++++++++ nexgb/xgbgen/expression.go | 41 +++++++++++ nexgb/xgbgen/field.go | 60 ++++++++++++++-- nexgb/xgbgen/misc.go | 4 +- nexgb/xgbgen/protocol.go | 41 +++++++++++ nexgb/xgbgen/representation.go | 149 -------------------------------------- nexgb/xgbgen/request_reply.go | 149 ++++++++++++++++++++++++++++++++++++++ nexgb/xgbgen/size.go | 9 +++ nexgb/xgbgen/xml.go | 64 +++++++---------- nexgb/xgbgen/xml_expression.go | 160 ----------------------------------------- nexgb/xgbgen/xml_fields.go | 71 +----------------- 12 files changed, 402 insertions(+), 422 deletions(-) create mode 100644 nexgb/xgbgen/doc.go create mode 100644 nexgb/xgbgen/protocol.go delete mode 100644 nexgb/xgbgen/representation.go create mode 100644 nexgb/xgbgen/request_reply.go delete mode 100644 nexgb/xgbgen/xml_expression.go (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go index 3e484f3..d433531 100644 --- a/nexgb/xgbgen/context.go +++ b/nexgb/xgbgen/context.go @@ -8,6 +8,8 @@ import ( "time" ) +// Context represents the protocol we're converting to Go, and a writer +// buffer to write the Go source to. type Context struct { protocol *Protocol out *bytes.Buffer diff --git a/nexgb/xgbgen/doc.go b/nexgb/xgbgen/doc.go new file mode 100644 index 0000000..4ba6145 --- /dev/null +++ b/nexgb/xgbgen/doc.go @@ -0,0 +1,74 @@ +/* +xgbgen constructs Go source files from xproto XML description files. xgbgen +accomplishes the same task as the Python code generator for XCB and xpyb. + +Usage: + xgbgen [flags] some-protocol.xml + +The flags are: + --proto-path path + The path to a directory containing xproto XML description files. + This is only necessary when 'some-protocol.xml' imports other + protocol files. + --gofmt=true + When false, the outputted Go code will not be gofmt'd. And it won't + be very pretty at all. This is typically useful if there are syntax + errors that need to be debugged in code generation. gofmt will hiccup; + this will allow you to see the raw code. + +How it works + +xgbgen works by parsing the input XML file using Go's encoding/xml package. +The majority of this work is done in xml.go and xml_fields.go, where the +appropriate types are declared. + +Due to the nature of the XML in the protocol description files, the types +required to parse the XML are not well suited to reasoning about code +generation. Because of this, all data parsed in the XML types is translated +into more reasonable types. This translation is done in translation.go, +and is mainly grunt work. (The only interesting tidbits are the translation +of XML names to Go source names, and connecting fields with their +appropriate types.) + +The organization of these types is greatly +inspired by the description of the XML found here: +http://cgit.freedesktop.org/xcb/proto/tree/doc/xml-xcb.txt + +These types come with a lot of supporting methods to make their use in +code generation easier. They can be found in expression.go, field.go, +protocol.go, request_reply.go and type.go. Of particular interest are +expression evaluation and size calculation (in bytes). + +These types also come with supporting methods that convert their +representation into Go source code. I've quartered such methods in +go.go, go_error.go, go_event.go, go_list.go, go_request_reply.go, +go_single_field.go, go_struct.go and go_union.go. The idea is to keep +as much of the Go specific code generation in one area as possible. Namely, +while not *all* Go related code is found in the 'go*.go' files, *most* +of it is. (If there's any interest in using xgbgen for other languages, +I'd be happy to try and make xgbgen a little more friendly in this regard. +I did, however, design xgbgen with this in mind, so it shouldn't involve +anything as serious as a re-design.) + +Why + +I wrote xgbgen because I found the existing code generator that was written in +Python to be unwieldy. In particular, static and strong typing greatly helped +me reason better about the code generation task. + +What does not work + +The core X protocol should be completely working. As far as I know, most +extensions should work too, although I've only tested (and not much) the +Xinerama and RandR extensions. + +XKB does not work. I don't have any real plans of working on this unless there +is demand and I have some test cases to work with. (i.e., even if I could get +something generated for XKB, I don't have the inclination to understand it +enough to verify that it works.) XKB poses several extremely difficult +problems that XCB also has trouble with. More info on that can be found at +http://cgit.freedesktop.org/xcb/libxcb/tree/doc/xkb_issues and +http://cgit.freedesktop.org/xcb/libxcb/tree/doc/xkb_internals. + +*/ +package main diff --git a/nexgb/xgbgen/expression.go b/nexgb/xgbgen/expression.go index 2350979..721ebfd 100644 --- a/nexgb/xgbgen/expression.go +++ b/nexgb/xgbgen/expression.go @@ -5,11 +5,32 @@ import ( "log" ) +// Expression represents all the different forms of expressions possible in +// side an XML protocol description file. It's also received a few custom +// addendums to make applying special functions (like padding) easier. type Expression interface { + // Concrete determines whether this particular expression can be computed + // to some constant value inside xgbgen. (The alternative is that the + // expression can only be computed with values at run time of the + // generated code.) Concrete() bool + + // Eval evaluates a concrete expression. It is an error to call Eval + // on any expression that is not concrete (or contains any sub-expression + // that is not concrete). Eval() uint + + // Reduce attempts to evaluate any concrete sub-expressions. + // i.e., (1 + 2 * (5 + 1 + someSizeOfStruct) reduces to + // (3 * (6 + someSizeOfStruct)). + // 'prefix' is used preprended to any field reference name. Reduce(prefix string) string + + // String is an alias for Reduce("") String() string + + // Initialize makes sure all names in this expression and any subexpressions + // have been translated to Go source names. Initialize(p *Protocol) } @@ -41,12 +62,17 @@ func (e *Function) Initialize(p *Protocol) { e.Expr.Initialize(p) } +// BinaryOp is an expression that performs some operation (defined in the XML +// file) with Expr1 and Expr2 as operands. type BinaryOp struct { Op string Expr1 Expression Expr2 Expression } +// newBinaryOp constructs a new binary expression when both expr1 and expr2 +// are not nil. If one or both are nil, then the non-nil expression is +// returned unchanged or nil is returned. func newBinaryOp(op string, expr1, expr2 Expression) Expression { switch { case expr1 != nil && expr2 != nil: @@ -124,6 +150,8 @@ func (e *BinaryOp) Initialize(p *Protocol) { e.Expr2.Initialize(p) } +// UnaryOp is the same as BinaryOp, except it's a unary operator with only +// one sub-expression. type UnaryOp struct { Op string Expr Expression @@ -158,6 +186,8 @@ func (e *UnaryOp) Initialize(p *Protocol) { e.Expr.Initialize(p) } +// Padding represents the application of the 'pad' function to some +// sub-expression. type Padding struct { Expr Expression } @@ -185,6 +215,8 @@ func (e *Padding) Initialize(p *Protocol) { e.Expr.Initialize(p) } +// PopCount represents the application of the 'PopCount' function to +// some sub-expression. type PopCount struct { Expr Expression } @@ -212,6 +244,7 @@ func (e *PopCount) Initialize(p *Protocol) { e.Expr.Initialize(p) } +// Value represents some constant integer. type Value struct { v uint } @@ -234,6 +267,7 @@ func (e *Value) String() string { func (e *Value) Initialize(p *Protocol) {} +// Bit represents some bit whose value is computed by '1 << bit'. type Bit struct { b uint } @@ -256,6 +290,8 @@ func (e *Bit) String() string { func (e *Bit) Initialize(p *Protocol) {} +// FieldRef represents a reference to some variable in the generated code +// with name Name. type FieldRef struct { Name string } @@ -285,6 +321,9 @@ func (e *FieldRef) Initialize(p *Protocol) { e.Name = SrcName(p, e.Name) } +// EnumRef represents a reference to some enumeration field. +// EnumKind is the "group" an EnumItem is the name of the specific enumeration +// value inside that group. type EnumRef struct { EnumKind Type EnumItem string @@ -312,6 +351,8 @@ func (e *EnumRef) Initialize(p *Protocol) { e.EnumItem = SrcName(p, e.EnumItem) } +// SumOf represents a summation of the variable in the generated code named by +// Name. It is not currently used. (It's XKB voodoo.) type SumOf struct { Name string } diff --git a/nexgb/xgbgen/field.go b/nexgb/xgbgen/field.go index 5041f77..725f3de 100644 --- a/nexgb/xgbgen/field.go +++ b/nexgb/xgbgen/field.go @@ -6,20 +6,48 @@ import ( "strings" ) +// Field corresponds to any field described in an XML protocol description +// file. This includes struct fields, union fields, request fields, +// reply fields and so on. +// To make code generation easier, fields that have types are also stored. +// Note that not all fields support all methods defined in this interface. +// For instance, a padding field does not have a source name. type Field interface { + // Initialize sets up the source name of this field. Initialize(p *Protocol) + + // SrcName is the Go source name of this field. SrcName() string + + // XmlName is the name of this field from the XML file. XmlName() string + + // SrcType is the Go source type name of this field. SrcType() string + + // Size returns an expression that computes the size (in bytes) + // of this field. Size() Size + // Define writes the Go code to declare this field (in a struct definition). Define(c *Context) + + // Read writes the Go code to convert a byte slice to a Go value + // of this field. + // 'prefix' is the prefix of the name of the Go value. Read(c *Context, prefix string) + + // Write writes the Go code to convert a Go value to a byte slice of + // this field. + // 'prefix' is the prefix of the name of the Go value. Write(c *Context, prefix string) } func (pad *PadField) Initialize(p *Protocol) {} +// PadField represents any type of padding. It is omitted from +// definitions, but is used in Read/Write to increment the buffer index. +// It is also used in size calculation. type PadField struct { Bytes uint } @@ -40,6 +68,8 @@ func (p *PadField) Size() Size { return newFixedSize(p.Bytes) } +// SingleField represents most of the fields in an XML protocol description. +// It corresponds to any single value. type SingleField struct { srcName string xmlName string @@ -67,6 +97,7 @@ func (f *SingleField) Size() Size { return f.Type.Size() } +// ListField represents a list of values. type ListField struct { srcName string xmlName string @@ -89,6 +120,9 @@ func (f *ListField) SrcType() string { return fmt.Sprintf("[]%s", f.Type.SrcName()) } +// Length computes the *number* of values in a list. +// If this ListField does not have any length expression, we throw our hands +// up and simply compute the 'len' of the field name of this list. func (f *ListField) Length() Size { if f.LengthExpr == nil { return newExpressionSize(&Function{ @@ -101,6 +135,12 @@ func (f *ListField) Length() Size { return newExpressionSize(f.LengthExpr) } +// Size computes the *size* of a list (in bytes). +// It it typically a simple matter of multiplying the length of the list by +// the size of the type of the list. +// But if it's a list of struct where the struct has a list field, we use a +// special function written in go_struct.go to compute the size (since the +// size in this case can only be computed recursively). func (f *ListField) Size() Size { simpleLen := &Function{ Name: "pad", @@ -120,11 +160,6 @@ func (f *ListField) Size() Size { } case *Union: return newExpressionSize(simpleLen) - // sizeFun := &Function{ - // Name: fmt.Sprintf("%sListSize", f.Type.SrcName()), - // Expr: &FieldRef{Name: f.SrcName()}, - // } - // return newExpressionSize(sizeFun) case *Base: return newExpressionSize(simpleLen) case *Resource: @@ -145,10 +180,14 @@ func (f *ListField) Initialize(p *Protocol) { } } +// LocalField is exactly the same as a regular SingleField, except it isn't +// sent over the wire. (i.e., it's probably used to compute an ExprField). type LocalField struct { *SingleField } +// ExprField is a field that is not parameterized, but is computed from values +// of other fields. type ExprField struct { srcName string xmlName string @@ -178,6 +217,9 @@ func (f *ExprField) Initialize(p *Protocol) { f.Expr.Initialize(p) } +// ValueField represents two fields in one: a mask and a list of 4-byte +// integers. The mask specifies which kinds of values are in the list. +// (i.e., See ConfigureWindow, CreateWindow, ChangeWindowAttributes, etc.) type ValueField struct { Parent interface{} MaskType Type @@ -197,6 +239,10 @@ func (f *ValueField) SrcType() string { return f.MaskType.SrcName() } +// Size computes the size in bytes of the combination of the mask and list +// in this value field. +// The expression to compute this looks complicated, but it's really just +// the number of bits set in the mask multiplied 4 (and padded of course). func (f *ValueField) Size() Size { maskSize := f.MaskType.Size() listSize := newExpressionSize(&Function{ @@ -234,6 +280,8 @@ func (f *ValueField) Initialize(p *Protocol) { f.ListName = SrcName(p, f.ListName) } +// SwitchField represents a 'switch' element in the XML protocol description +// file. It is not currently used. (i.e., it is XKB voodoo.) type SwitchField struct { Name string Expr Expression @@ -270,6 +318,8 @@ func (f *SwitchField) Initialize(p *Protocol) { } } +// Bitcase represents a single bitcase inside a switch expression. +// It is not currently used. (i.e., it's XKB voodoo.) type Bitcase struct { Fields []Field Expr Expression diff --git a/nexgb/xgbgen/misc.go b/nexgb/xgbgen/misc.go index 13c4cc2..85d788f 100644 --- a/nexgb/xgbgen/misc.go +++ b/nexgb/xgbgen/misc.go @@ -7,7 +7,7 @@ import ( // AllCaps is a regex to test if a string identifier is made of // all upper case letters. -var AllCaps = regexp.MustCompile("^[A-Z0-9]+$") +var allCaps = regexp.MustCompile("^[A-Z0-9]+$") // popCount counts number of bits 'set' in mask. func popCount(mask uint) uint { @@ -30,7 +30,7 @@ func pad(n int) int { // first letter of each chunk, and smushes'em back together. func splitAndTitle(s string) string { // If the string is all caps, lower it and capitalize first letter. - if AllCaps.MatchString(s) { + if allCaps.MatchString(s) { return strings.Title(strings.ToLower(s)) } diff --git a/nexgb/xgbgen/protocol.go b/nexgb/xgbgen/protocol.go new file mode 100644 index 0000000..505b400 --- /dev/null +++ b/nexgb/xgbgen/protocol.go @@ -0,0 +1,41 @@ +package main + +import ( + "strings" +) + +// Protocol is a type that encapsulates all information about one +// particular XML file. It also contains links to other protocol types +// if this protocol imports other other extensions. The import relationship +// is recursive. +type Protocol struct { + Name string + ExtXName string + ExtName string + MajorVersion string + MinorVersion string + + Imports []*Protocol + Types []Type + Requests []*Request +} + +// Initialize traverses all structures, looks for 'Translation' type, +// and looks up the real type in the namespace. It also sets the source +// name for all relevant fields/structures. +// This is necessary because we don't traverse the XML in order initially. +func (p *Protocol) Initialize() { + for _, typ := range p.Types { + typ.Initialize(p) + } + for _, req := range p.Requests { + req.Initialize(p) + } +} + +// isExt returns true if this protocol is an extension. +// i.e., it's name isn't "xproto". +func (p *Protocol) isExt() bool { + return strings.ToLower(p.Name) == "xproto" +} + diff --git a/nexgb/xgbgen/representation.go b/nexgb/xgbgen/representation.go deleted file mode 100644 index 62a3eb2..0000000 --- a/nexgb/xgbgen/representation.go +++ /dev/null @@ -1,149 +0,0 @@ -package main - -import ( - "fmt" - "log" - "strings" - "unicode" -) - -type Protocol struct { - Name string - ExtXName string - ExtName string - MajorVersion string - MinorVersion string - - Imports []*Protocol - Types []Type - Requests []*Request -} - -// Initialize traverses all structures, looks for 'Translation' type, -// and looks up the real type in the namespace. It also sets the source -// name for all relevant fields/structures. -// This is necessary because we don't traverse the XML in order initially. -func (p *Protocol) Initialize() { - for _, typ := range p.Types { - typ.Initialize(p) - } - for _, req := range p.Requests { - req.Initialize(p) - } -} - -type Request struct { - srcName string - xmlName string - Opcode int - Combine bool - Fields []Field - Reply *Reply -} - -func (r *Request) Initialize(p *Protocol) { - r.srcName = SrcName(p, r.xmlName) - if p.Name != "xproto" { - r.srcName = strings.Title(strings.ToLower(p.Name)) + r.srcName - } - - if r.Reply != nil { - r.Reply.Initialize(p) - } - for _, field := range r.Fields { - field.Initialize(p) - } -} - -func (r *Request) SrcName() string { - return r.srcName -} - -func (r *Request) XmlName() string { - return r.xmlName -} - -func (r *Request) ReplyName() string { - if r.Reply == nil { - log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.", - r.SrcName()) - } - name := r.SrcName() - lower := string(unicode.ToLower(rune(name[0]))) + name[1:] - return fmt.Sprintf("%sReply", lower) -} - -func (r *Request) ReplyTypeName() string { - if r.Reply == nil { - log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.", - r.SrcName()) - } - return fmt.Sprintf("%sReply", r.SrcName()) -} - -func (r *Request) ReqName() string { - name := r.SrcName() - lower := string(unicode.ToLower(rune(name[0]))) + name[1:] - return fmt.Sprintf("%sRequest", lower) -} - -func (r *Request) CookieName() string { - return fmt.Sprintf("%sCookie", r.SrcName()) -} - -// Size for Request needs a context. -// Namely, if this is an extension, we need to account for *four* bytes -// of a header (extension opcode, request opcode, and the sequence number). -// If it's a core protocol request, then we only account for *three* -// bytes of the header (remove the extension opcode). -func (r *Request) Size(c *Context) Size { - size := newFixedSize(0) - - if c.protocol.Name == "xproto" { - size = size.Add(newFixedSize(3)) - } else { - size = size.Add(newFixedSize(4)) - } - - for _, field := range r.Fields { - switch field.(type) { - case *LocalField: - continue - case *SingleField: - // mofos!!! - if r.SrcName() == "ConfigureWindow" && - field.SrcName() == "ValueMask" { - - continue - } - size = size.Add(field.Size()) - default: - size = size.Add(field.Size()) - } - } - return newExpressionSize(&Padding{ - Expr: size.Expression, - }) -} - -type Reply struct { - Fields []Field -} - -func (r *Reply) Size() Size { - size := newFixedSize(0) - - // Account for reply discriminant, sequence number and reply length - size = size.Add(newFixedSize(7)) - - for _, field := range r.Fields { - size = size.Add(field.Size()) - } - return size -} - -func (r *Reply) Initialize(p *Protocol) { - for _, field := range r.Fields { - field.Initialize(p) - } -} diff --git a/nexgb/xgbgen/request_reply.go b/nexgb/xgbgen/request_reply.go new file mode 100644 index 0000000..7cd2859 --- /dev/null +++ b/nexgb/xgbgen/request_reply.go @@ -0,0 +1,149 @@ +package main + +import ( + "fmt" + "log" + "strings" + "unicode" +) + +// Request represents all XML 'request' nodes. +// If the request doesn't have a reply, Reply is nil. +type Request struct { + srcName string // The Go name of this request. + xmlName string // The XML name of this request. + Opcode int + Combine bool // Not currently used. + Fields []Field // All fields in the request. + Reply *Reply // A reply, if one exists for this request. +} + +// Initialize creates the proper Go source name for this request. +// It also initializes the reply if one exists, and all fields in this request. +func (r *Request) Initialize(p *Protocol) { + r.srcName = SrcName(p, r.xmlName) + if p.Name != "xproto" { + r.srcName = strings.Title(strings.ToLower(p.Name)) + r.srcName + } + + if r.Reply != nil { + r.Reply.Initialize(p) + } + for _, field := range r.Fields { + field.Initialize(p) + } +} + +func (r *Request) SrcName() string { + return r.srcName +} + +func (r *Request) XmlName() string { + return r.xmlName +} + +// ReplyName gets the Go source name of the function that generates a +// reply type from a slice of bytes. +// The generated function is not currently exported. +func (r *Request) ReplyName() string { + if r.Reply == nil { + log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.", + r.SrcName()) + } + name := r.SrcName() + lower := string(unicode.ToLower(rune(name[0]))) + name[1:] + return fmt.Sprintf("%sReply", lower) +} + +// ReplyTypeName gets the Go source name of the type holding all reply data +// for this request. +func (r *Request) ReplyTypeName() string { + if r.Reply == nil { + log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.", + r.SrcName()) + } + return fmt.Sprintf("%sReply", r.SrcName()) +} + +// ReqName gets the Go source name of the function that generates a byte +// slice from a list of parameters. +// The generated function is not currently exported. +func (r *Request) ReqName() string { + name := r.SrcName() + lower := string(unicode.ToLower(rune(name[0]))) + name[1:] + return fmt.Sprintf("%sRequest", lower) +} + +// CookieName gets the Go source name of the type that holds cookies for +// this request. +func (r *Request) CookieName() string { + return fmt.Sprintf("%sCookie", r.SrcName()) +} + +// Size for Request needs a context. +// Namely, if this is an extension, we need to account for *four* bytes +// of a header (extension opcode, request opcode, and the sequence number). +// If it's a core protocol request, then we only account for *three* +// bytes of the header (remove the extension opcode). +func (r *Request) Size(c *Context) Size { + size := newFixedSize(0) + + // If this is a core protocol request, we squeeze in an extra byte of + // data (from the fields below) between the opcode and the size of the + // request. In an extension request, this byte is always occupied + // by the opcode of the request (while the first byte is always occupied + // by the opcode of the extension). + if c.protocol.Name == "xproto" { + size = size.Add(newFixedSize(3)) + } else { + size = size.Add(newFixedSize(4)) + } + + for _, field := range r.Fields { + switch field.(type) { + case *LocalField: // local fields don't go over the wire + continue + case *SingleField: + // mofos!!! + if r.SrcName() == "ConfigureWindow" && + field.SrcName() == "ValueMask" { + + continue + } + size = size.Add(field.Size()) + default: + size = size.Add(field.Size()) + } + } + return newExpressionSize(&Padding{ + Expr: size.Expression, + }) +} + +// Reply encapsulates the fields associated with a 'reply' element. +type Reply struct { + Fields []Field +} + +// Size gets the number of bytes in this request's reply. +// A reply always has at least 7 bytes: +// 1 byte: A reply discriminant (first byte set to 1) +// 2 bytes: A sequence number +// 4 bytes: Number of additional bytes in 4-byte units past initial 32 bytes. +func (r *Reply) Size() Size { + size := newFixedSize(0) + + // Account for reply discriminant, sequence number and reply length + size = size.Add(newFixedSize(7)) + + for _, field := range r.Fields { + size = size.Add(field.Size()) + } + return size +} + +func (r *Reply) Initialize(p *Protocol) { + for _, field := range r.Fields { + field.Initialize(p) + } +} diff --git a/nexgb/xgbgen/size.go b/nexgb/xgbgen/size.go index 70edb8f..d8d3ac3 100644 --- a/nexgb/xgbgen/size.go +++ b/nexgb/xgbgen/size.go @@ -1,21 +1,30 @@ package main +// Size corresponds to an expression that represents the number of bytes +// in some *thing*. Generally, sizes are used to allocate buffers and to +// inform X how big requests are. +// Size is basically a thin layer over an Expression that yields easy methods +// for adding and multiplying sizes. type Size struct { Expression } +// newFixedSize creates a new Size with some fixed and known value. func newFixedSize(fixed uint) Size { return Size{&Value{v: fixed}} } +// newExpressionSize creates a new Size with some expression. func newExpressionSize(variable Expression) Size { return Size{variable} } +// Add adds s1 and s2 and returns a new Size. func (s1 Size) Add(s2 Size) Size { return Size{newBinaryOp("+", s1, s2)} } +// Multiply mupltiplies s1 and s2 and returns a new Size. func (s1 Size) Multiply(s2 Size) Size { return Size{newBinaryOp("*", s1, s2)} } diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go index 1b2f89a..df21433 100644 --- a/nexgb/xgbgen/xml.go +++ b/nexgb/xgbgen/xml.go @@ -18,19 +18,19 @@ type XML struct { // Types for all top-level elements. // First are the simple ones. Imports XMLImports `xml:"import"` - Enums XMLEnums `xml:"enum"` - Xids XMLXids `xml:"xidtype"` - XidUnions XMLXids `xml:"xidunion"` - TypeDefs XMLTypeDefs `xml:"typedef"` - EventCopies XMLEventCopies `xml:"eventcopy"` - ErrorCopies XMLErrorCopies `xml:"errorcopy"` + Enums []*XMLEnum `xml:"enum"` + Xids []*XMLXid `xml:"xidtype"` + XidUnions []*XMLXid `xml:"xidunion"` + TypeDefs []*XMLTypeDef `xml:"typedef"` + EventCopies []*XMLEventCopy `xml:"eventcopy"` + ErrorCopies []*XMLErrorCopy `xml:"errorcopy"` // Here are the complex ones, i.e., anything with "structure contents" - Structs XMLStructs `xml:"struct"` - Unions XMLUnions `xml:"union"` - Requests XMLRequests `xml:"request"` - Events XMLEvents `xml:"event"` - Errors XMLErrors `xml:"error"` + Structs []*XMLStruct `xml:"struct"` + Unions []*XMLUnion `xml:"union"` + Requests []*XMLRequest `xml:"request"` + Events []*XMLEvent `xml:"event"` + Errors []*XMLError `xml:"error"` } type XMLImports []*XMLImport @@ -60,8 +60,6 @@ type XMLImport struct { xml *XML `xml:"-"` } -type XMLEnums []XMLEnum - type XMLEnum struct { Name string `xml:"name,attr"` Items []*XMLEnumItem `xml:"item"` @@ -72,77 +70,69 @@ type XMLEnumItem struct { Expr *XMLExpression `xml:",any"` } -type XMLXids []*XMLXid - type XMLXid struct { XMLName xml.Name Name string `xml:"name,attr"` } -type XMLTypeDefs []*XMLTypeDef - type XMLTypeDef struct { Old string `xml:"oldname,attr"` New string `xml:"newname,attr"` } -type XMLEventCopies []*XMLEventCopy - type XMLEventCopy struct { Name string `xml:"name,attr"` Number int `xml:"number,attr"` Ref string `xml:"ref,attr"` } -type XMLErrorCopies []*XMLErrorCopy - type XMLErrorCopy struct { Name string `xml:"name,attr"` Number int `xml:"number,attr"` Ref string `xml:"ref,attr"` } -type XMLStructs []*XMLStruct - type XMLStruct struct { Name string `xml:"name,attr"` - Fields XMLFields `xml:",any"` + Fields []*XMLField `xml:",any"` } -type XMLUnions []*XMLUnion - type XMLUnion struct { Name string `xml:"name,attr"` - Fields XMLFields `xml:",any"` + Fields []*XMLField `xml:",any"` } -type XMLRequests []*XMLRequest - type XMLRequest struct { Name string `xml:"name,attr"` Opcode int `xml:"opcode,attr"` Combine bool `xml:"combine-adjacent,attr"` - Fields XMLFields `xml:",any"` + Fields []*XMLField `xml:",any"` Reply *XMLReply `xml:"reply"` } type XMLReply struct { - Fields XMLFields `xml:",any"` + Fields []*XMLField `xml:",any"` } -type XMLEvents []*XMLEvent - type XMLEvent struct { Name string `xml:"name,attr"` Number int `xml:"number,attr"` NoSequence bool `xml:"no-sequence-number,attr"` - Fields XMLFields `xml:",any"` + Fields []*XMLField `xml:",any"` } -type XMLErrors []*XMLError - type XMLError struct { Name string `xml:"name,attr"` Number int `xml:"number,attr"` - Fields XMLFields `xml:",any"` + Fields []*XMLField `xml:",any"` +} + +type XMLExpression struct { + XMLName xml.Name + + Exprs []*XMLExpression `xml:",any"` + + Data string `xml:",chardata"` + Op string `xml:"op,attr"` + Ref string `xml:"ref,attr"` } diff --git a/nexgb/xgbgen/xml_expression.go b/nexgb/xgbgen/xml_expression.go deleted file mode 100644 index 2989668..0000000 --- a/nexgb/xgbgen/xml_expression.go +++ /dev/null @@ -1,160 +0,0 @@ -package main - -import ( - "encoding/xml" - "fmt" - "log" - "strconv" -) - -type XMLExpression struct { - XMLName xml.Name - - Exprs []*XMLExpression `xml:",any"` - - Data string `xml:",chardata"` - Op string `xml:"op,attr"` - Ref string `xml:"ref,attr"` -} - -func newValueExpression(v uint) *XMLExpression { - return &XMLExpression{ - XMLName: xml.Name{Local: "value"}, - Data: fmt.Sprintf("%d", v), - } -} - -// String is for debugging. For actual use, please use 'Morph'. -func (e *XMLExpression) String() string { - switch e.XMLName.Local { - case "op": - return fmt.Sprintf("(%s %s %s)", e.Exprs[0], e.Op, e.Exprs[1]) - case "unop": - return fmt.Sprintf("(%s (%s))", e.Op, e.Exprs[0]) - case "popcount": - return fmt.Sprintf("popcount(%s)", e.Exprs[0]) - case "fieldref": - fallthrough - case "value": - return fmt.Sprintf("%s", e.Data) - case "bit": - return fmt.Sprintf("(1 << %s)", e.Data) - case "enumref": - return fmt.Sprintf("%s%s", e.Ref, e.Data) - case "sumof": - return fmt.Sprintf("sum(%s)", e.Ref) - default: - log.Panicf("Unrecognized expression element: %s", e.XMLName.Local) - } - - panic("unreachable") -} - -// Eval is used to *attempt* to compute a concrete value for a particular -// expression. This is used in the initial setup to instantiate values for -// empty items in enums. -// We can't compute a concrete value for expressions that rely on a context, -// i.e., some field value. -func (e *XMLExpression) Eval() uint { - switch e.XMLName.Local { - case "op": - if len(e.Exprs) != 2 { - log.Panicf("'op' found %d expressions; expected 2.", len(e.Exprs)) - } - return e.BinaryOp(e.Exprs[0], e.Exprs[1]).Eval() - case "unop": - if len(e.Exprs) != 1 { - log.Panicf("'unop' found %d expressions; expected 1.", len(e.Exprs)) - } - return e.UnaryOp(e.Exprs[0]).Eval() - case "popcount": - if len(e.Exprs) != 1 { - log.Panicf("'popcount' found %d expressions; expected 1.", - len(e.Exprs)) - } - return popCount(e.Exprs[0].Eval()) - case "value": - val, err := strconv.Atoi(e.Data) - if err != nil { - log.Panicf("Could not convert '%s' in 'value' expression to int.", - e.Data) - } - return uint(val) - case "bit": - bit, err := strconv.Atoi(e.Data) - if err != nil { - log.Panicf("Could not convert '%s' in 'bit' expression to int.", - e.Data) - } - if bit < 0 || bit > 31 { - log.Panicf("A 'bit' literal must be in the range [0, 31], but "+ - " is %d", bit) - } - return 1 << uint(bit) - case "fieldref": - log.Panicf("Cannot compute concrete value of 'fieldref' in "+ - "expression '%s'.", e) - case "enumref": - log.Panicf("Cannot compute concrete value of 'enumref' in "+ - "expression '%s'.", e) - case "sumof": - log.Panicf("Cannot compute concrete value of 'sumof' in "+ - "expression '%s'.", e) - } - - log.Panicf("Unrecognized tag '%s' in expression context. Expected one of "+ - "op, fieldref, value, bit, enumref, unop, sumof or popcount.", - e.XMLName.Local) - panic("unreachable") -} - -func (e *XMLExpression) BinaryOp(oprnd1, oprnd2 *XMLExpression) *XMLExpression { - if e.XMLName.Local != "op" { - log.Panicf("Cannot perform binary operation on non-op expression: %s", - e.XMLName.Local) - } - if len(e.Op) == 0 { - log.Panicf("Cannot perform binary operation without operator for: %s", - e.XMLName.Local) - } - - wrap := newValueExpression - switch e.Op { - case "+": - return wrap(oprnd1.Eval() + oprnd2.Eval()) - case "-": - return wrap(oprnd1.Eval() + oprnd2.Eval()) - case "*": - return wrap(oprnd1.Eval() * oprnd2.Eval()) - case "/": - return wrap(oprnd1.Eval() / oprnd2.Eval()) - case "&": - return wrap(oprnd1.Eval() & oprnd2.Eval()) - case "<<": - return wrap(oprnd1.Eval() << oprnd2.Eval()) - } - - log.Panicf("Invalid binary operator '%s' for '%s' expression.", - e.Op, e.XMLName.Local) - panic("unreachable") -} - -func (e *XMLExpression) UnaryOp(oprnd *XMLExpression) *XMLExpression { - if e.XMLName.Local != "unop" { - log.Panicf("Cannot perform unary operation on non-unop expression: %s", - e.XMLName.Local) - } - if len(e.Op) == 0 { - log.Panicf("Cannot perform unary operation without operator for: %s", - e.XMLName.Local) - } - - switch e.Op { - case "~": - return newValueExpression(^oprnd.Eval()) - } - - log.Panicf("Invalid unary operator '%s' for '%s' expression.", - e.Op, e.XMLName.Local) - panic("unreachable") -} diff --git a/nexgb/xgbgen/xml_fields.go b/nexgb/xgbgen/xml_fields.go index 991141b..fe6c5d5 100644 --- a/nexgb/xgbgen/xml_fields.go +++ b/nexgb/xgbgen/xml_fields.go @@ -1,31 +1,10 @@ package main -/* - A series of fields should be taken as "structure contents", and *not* - just the single 'field' elements. Namely, 'fields' subsumes 'field' - elements. - - More particularly, 'fields' corresponds to list, in order, of any of the - follow elements: pad, field, list, localfield, exprfield, valueparm - and switch. - - Thus, the 'Field' type must contain the union of information corresponding - to all aforementioned fields. - - This would ideally be a better job for interfaces, but I could not figure - out how to make them jive with Go's XML package. (And I don't really feel - up to type translation.) -*/ - import ( "encoding/xml" - "fmt" "log" - "strings" ) -type XMLFields []*XMLField - type XMLField struct { XMLName xml.Name @@ -47,7 +26,7 @@ type XMLField struct { ValueListName string `xml:"value-list-name,attr"` // For 'switch' element. - Bitcases XMLBitcases `xml:"bitcase"` + Bitcases []*XMLBitcase `xml:"bitcase"` // I don't know which elements these are for. The documentation is vague. // They also seem to be completely optional. @@ -56,41 +35,6 @@ type XMLField struct { OptAltEnum string `xml:"altenum,attr"` } -// String is for debugging purposes. -func (f *XMLField) String() string { - switch f.XMLName.Local { - case "pad": - return fmt.Sprintf("pad (%d bytes)", f.Bytes) - case "field": - return fmt.Sprintf("field (type = '%s', name = '%s')", f.Type, f.Name) - case "list": - return fmt.Sprintf("list (type = '%s', name = '%s', length = '%s')", - f.Type, f.Name, f.Expr) - case "localfield": - return fmt.Sprintf("localfield (type = '%s', name = '%s')", - f.Type, f.Name) - case "exprfield": - return fmt.Sprintf("exprfield (type = '%s', name = '%s', expr = '%s')", - f.Type, f.Name, f.Expr) - case "valueparam": - return fmt.Sprintf("valueparam (type = '%s', name = '%s', list = '%s')", - f.ValueMaskType, f.ValueMaskName, f.ValueListName) - case "switch": - bitcases := make([]string, len(f.Bitcases)) - for i, bitcase := range f.Bitcases { - bitcases[i] = bitcase.StringPrefix("\t") - } - return fmt.Sprintf("switch (name = '%s', expr = '%s')\n\t%s", - f.Name, f.Expr, strings.Join(bitcases, "\n\t")) - default: - log.Panicf("Unrecognized field element: %s", f.XMLName.Local) - } - - panic("unreachable") -} - -type XMLBitcases []*XMLBitcase - // Bitcase represents a single expression followed by any number of fields. // Namely, if the switch's expression (all bitcases are inside a switch), // and'd with the bitcase's expression is equal to the bitcase expression, @@ -100,7 +44,7 @@ type XMLBitcases []*XMLBitcase // it's the closest thing to a Union I can get to in Go without interfaces. // Would an '' tag have been too much to ask? :-( type XMLBitcase struct { - Fields XMLFields `xml:",any"` + Fields []*XMLField `xml:",any"` // All the different expressions. // When it comes time to choose one, use the 'Expr' method. @@ -114,17 +58,6 @@ type XMLBitcase struct { ExprPop *XMLExpression `xml:"popcount"` } -// StringPrefix is for debugging purposes only. -// StringPrefix takes a string to prefix to every extra line for formatting. -func (b *XMLBitcase) StringPrefix(prefix string) string { - fields := make([]string, len(b.Fields)) - for i, field := range b.Fields { - fields[i] = fmt.Sprintf("%s%s", prefix, field) - } - return fmt.Sprintf("%s\n\t%s%s", b.Expr(), prefix, - strings.Join(fields, "\n\t")) -} - // Expr chooses the only non-nil Expr* field from Bitcase. // Panic if there is more than one non-nil expression. func (b *XMLBitcase) Expr() *XMLExpression { -- cgit v1.2.3-70-g09d2 From dc48249e1acea54b391f53b95f16e515dead7c97 Mon Sep 17 00:00:00 2001 From: "Andrew Gallant (Ocelot)" Date: Mon, 7 May 2012 04:09:19 -0400 Subject: lots of docs and examples --- nexgb/Makefile | 6 ++ nexgb/doc.go | 152 +++++++++++++++++++++++++++++++ nexgb/examples/atom.go | 27 ------ nexgb/examples/create-window/main.go | 98 ++++++++++++++++++++ nexgb/examples/doc.go | 21 +++++ nexgb/examples/get-active-window/main.go | 57 ++++++++++++ nexgb/examples/property.go | 41 --------- nexgb/examples/randr.go | 48 ---------- nexgb/examples/randr/main.go | 84 +++++++++++++++++ nexgb/examples/seq-nowrap.go | 29 ------ nexgb/examples/seq-wrap.go | 24 ----- nexgb/examples/window.go | 61 ------------- nexgb/examples/xinerama.go | 29 ------ nexgb/examples/xinerama/main.go | 39 ++++++++ nexgb/xgb.go | 6 -- nexgb/xgb_test.go | 6 +- nexgb/xgbgen/context.go | 2 +- nexgb/xgbgen/field.go | 4 +- nexgb/xgbgen/go_error.go | 2 +- nexgb/xgbgen/go_event.go | 5 +- nexgb/xgbgen/go_list.go | 1 - nexgb/xgbgen/go_request_reply.go | 18 ++-- nexgb/xgbgen/go_union.go | 1 - nexgb/xgbgen/protocol.go | 1 - nexgb/xgbgen/request_reply.go | 4 +- nexgb/xgbgen/translation.go | 2 +- nexgb/xgbgen/xml.go | 32 +++---- 27 files changed, 495 insertions(+), 305 deletions(-) create mode 100644 nexgb/doc.go delete mode 100644 nexgb/examples/atom.go create mode 100644 nexgb/examples/create-window/main.go create mode 100644 nexgb/examples/doc.go create mode 100644 nexgb/examples/get-active-window/main.go delete mode 100644 nexgb/examples/property.go delete mode 100644 nexgb/examples/randr.go create mode 100644 nexgb/examples/randr/main.go delete mode 100644 nexgb/examples/seq-nowrap.go delete mode 100644 nexgb/examples/seq-wrap.go delete mode 100644 nexgb/examples/window.go delete mode 100644 nexgb/examples/xinerama.go create mode 100644 nexgb/examples/xinerama/main.go (limited to 'nexgb/xgbgen/xml.go') diff --git a/nexgb/Makefile b/nexgb/Makefile index c70b01c..7ba1cb4 100644 --- a/nexgb/Makefile +++ b/nexgb/Makefile @@ -1,5 +1,6 @@ # This Makefile is used by the developer. It is not needed in any way to build # a checkout of the XGB repository. +# It will be useful, however, if you are hacking at the code generator. XPROTO=/usr/share/xcb @@ -20,3 +21,8 @@ test: bench: go test -run 'nomatch' -bench '.*' -cpu 1,2,6 +gofmt: + gofmt -w *.go xgbgen/*.go examples/*.go examples/*/*.go + colcheck xgbgen/*.go examples/*.go examples/*/*.go \ + auth.go conn.go cookie.go doc.go xgb.go xgb_help.go xgb_test.go + diff --git a/nexgb/doc.go b/nexgb/doc.go new file mode 100644 index 0000000..b6c956f --- /dev/null +++ b/nexgb/doc.go @@ -0,0 +1,152 @@ +/* +Package XGB provides the X Go Binding, which is a low-level API to communicate +with the core X protocol and many of the X extensions. + +It is *very* closely modeled on XCB, so that experience with XCB (or xpyb) is +easily translatable to XGB. That is, it uses the same cookie/reply model +and is thread safe. There are otherwise no major differences (in the API). + +Most uses of XGB typically fall under the realm of window manager and GUI kit +development, but other applications (like pagers, panels, tilers, etc.) may +also require XGB. Moreover, it is a near certainty that if you need to work +with X, xgbutil will be of great use to you as well: +https://github.com/BurntSushi/xgbutil + +Example + +This is an extremely terse example that demonstrates how to connect to X, +create a window, listen to StructureNotify events and Key{Press,Release} +events, map the window, and print out all events received. An example with +accompanying documentation can be found in examples/create-window. + + package main + + import ( + "fmt" + "github.com/BurntSushi/xgb" + ) + + func main() { + X, err := xgb.NewConn() + if err != nil { + fmt.Println(err) + return + } + + wid, _ := X.NewId() + X.CreateWindow(X.DefaultScreen().RootDepth, wid, X.DefaultScreen().Root, + 0, 0, 500, 500, 0, + xgb.WindowClassInputOutput, X.DefaultScreen().RootVisual, + xgb.CwBackPixel | xgb.CwEventMask, + []uint32{ // values must be in the order defined by the protocol + 0xffffffff, + xgb.EventMaskStructureNotify | + xgb.EventMaskKeyPress | + xgb.EventMaskKeyRelease}) + + X.MapWindow(wid) + for { + ev, xerr := X.WaitForEvent() + if ev == nil && xerr == nil { + fmt.Println("Both event and error are nil. Exiting...") + return + } + + if ev != nil { + fmt.Printf("Event: %s\n", ev) + } + if xerr != nil { + fmt.Printf("Error: %s\n", xerr) + } + } + } + +Xinerama Example + +This is another small example that shows how to query Xinerama for geometry +information of each active head. Accompanying documentation for this example +can be found in examples/xinerama. + + package main + + import ( + "fmt" + "log" + "github.com/BurntSushi/xgb" + ) + + func main() { + X, err := xgb.NewConn() + if err != nil { + log.Fatal(err) + } + + // Initialize the Xinerama extension. + // The appropriate 'Init' function must be run for *every* + // extension before any of its requests can be used. + err = X.XineramaInit() + if err != nil { + log.Fatal(err) + } + + reply, err := X.XineramaQueryScreens().Reply() + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Number of heads: %d\n", reply.Number) + for i, screen := range reply.ScreenInfo { + fmt.Printf("%d :: X: %d, Y: %d, Width: %d, Height: %d\n", + i, screen.XOrg, screen.YOrg, screen.Width, screen.Height) + } + } + +Parallelism + +XGB can benefit greatly from parallelism due to its concurrent design. For +evidence of this claim, please see the benchmarks in xgb_test.go. + +Tests + +xgb_test.go contains a number of contrived tests that stress particular corners +of XGB that I presume could be problem areas. Namely: requests with no replies, +requests with replies, checked errors, unchecked errors, sequence number +wrapping, cookie buffer flushing (i.e., forcing a round trip every N requests +made that don't have a reply), getting/setting properties and creating a window +and listening to StructureNotify events. + +Code Generator + +Both XCB and xpyb use the same Python module (xcbgen) for a code generator. XGB +(before this fork) used the same code generator as well, but in my attempt to +add support for more extensions, I found the code generator extremely difficult +to work with. Therefore, I re-wrote the code generator in Go. It can be found +in its own sub-package, xgbgen, of xgb. My design of xgbgen includes a rough +consideration that it could be used for other languages. + +What works + +I am reasonably confident that the core X protocol is in full working form. I've +also tested the Xinerama and RandR extensions sparingly. Many of the other +existing extensions have Go source generated (and are compilable) and are +included in this package, but I am currently unsure of their status. They +*should* work. + +What does not work + +XKB is the only extension that intentionally does not work, although I suspect +that GLX also does not work (however, there is Go source code for GLX that +compiles, unlike XKB). I don't currently have any intention of getting XKB +working, due to its complexity and my current mental incapacity to test it. + +There are so many functions + +Indeed. Everything below this initial overview is useful insomuch as your +browser's "Find" feature is useful. The following list of types and functions +should act as a reference to the Go representation of a request, type or reply +of something you *already know about*. To search the following list in hopes +of attaining understanding is a quest in folly. For understanding, please see +the X Protocol Reference Manual: http://goo.gl/aMd2e + +*/ +package xgb diff --git a/nexgb/examples/atom.go b/nexgb/examples/atom.go deleted file mode 100644 index 2cb7132..0000000 --- a/nexgb/examples/atom.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - // "fmt" - "log" - - "github.com/BurntSushi/xgb" -) - -func init() { - log.SetFlags(0) -} - -func main() { - X, err := xgb.NewConn() - if err != nil { - log.Fatal(err) - } - - aname := "_NET_ACTIVE_WINDOW" - atom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() - if err != nil { - log.Fatal(err) - } - log.Printf("%d", atom.Atom) -} - diff --git a/nexgb/examples/create-window/main.go b/nexgb/examples/create-window/main.go new file mode 100644 index 0000000..6996f37 --- /dev/null +++ b/nexgb/examples/create-window/main.go @@ -0,0 +1,98 @@ +// Example create-window shows how to create a window, map it, resize it, +// and listen to structure and key events (i.e., when the window is resized +// by the window manager, or when key presses/releases are made when the +// window has focus). The events are printed to stdout. +package main + +import ( + "fmt" + + "github.com/BurntSushi/xgb" +) + +func main() { + X, err := xgb.NewConn() + if err != nil { + fmt.Println(err) + return + } + + // Any time a new resource (i.e., a window, pixmap, graphics context, etc.) + // is created, we need to generate a resource identifier with NewId. + wid, _ := X.NewId() + + // CreateWindow takes a boatload of parameters. + X.CreateWindow(X.DefaultScreen().RootDepth, wid, X.DefaultScreen().Root, + 0, 0, 500, 500, 0, + xgb.WindowClassInputOutput, X.DefaultScreen().RootVisual, + 0, []uint32{}) + + // This call to ChangeWindowAttributes could be factored out and + // included with the above CreateWindow call, but it is left here for + // instructive purposes. It tells X to send us events when the 'structure' + // of the window is changed (i.e., when it is resized, mapped, unmapped, + // etc.) and when a key press or a key release has been made when the + // window has focus. + // We also set the 'BackPixel' to white so that the window isn't butt ugly. + X.ChangeWindowAttributes(wid, + xgb.CwBackPixel|xgb.CwEventMask, + []uint32{ // values must be in the order defined by the protocol + 0xffffffff, + xgb.EventMaskStructureNotify | + xgb.EventMaskKeyPress | + xgb.EventMaskKeyRelease}) + + // MapWindow makes the window we've created appear on the screen. + // We demonstrated the use of a 'checked' request here. + // A checked request is a fancy way of saying, "do error handling + // synchronously." Namely, if there is a problem with the MapWindow request, + // we'll get the error *here*. If we were to do a normal unchecked + // request (like the above CreateWindow and ChangeWindowAttributes + // requests), then we would only see the error arrive in the main event + // loop. + // + // Typically, checked requests are useful when you need to make sure they + // succeed. Since they are synchronous, they incur a round trip cost before + // the program can continue, but this is only going to be noticeable if + // you're issuing tons of requests in succession. + // + // Note that requests without replies are by default unchecked while + // requests *with* replies are checked by default. + err = X.MapWindowChecked(wid).Check() + if err != nil { + fmt.Printf("Checked Error for mapping window %d: %s\n", wid, err) + } else { + fmt.Printf("Map window %d successful!\n", wid) + } + + // This is an example of an invalid MapWindow request and what an error + // looks like. + err = X.MapWindowChecked(0).Check() + if err != nil { + fmt.Printf("Checked Error for mapping window 0x1: %s\n", err) + } else { // neva + fmt.Printf("Map window 0x1 successful!\n") + } + + // Start the main event loop. + for { + // WaitForEvent either returns an event or an error and never both. + // If both are nil, then something went wrong and the loop should be + // halted. + // + // An error can only be seen here as a response to an unchecked + // request. + ev, xerr := X.WaitForEvent() + if ev == nil && xerr == nil { + fmt.Println("Both event and error are nil. Exiting...") + return + } + + if ev != nil { + fmt.Printf("Event: %s\n", ev) + } + if xerr != nil { + fmt.Printf("Error: %s\n", xerr) + } + } +} diff --git a/nexgb/examples/doc.go b/nexgb/examples/doc.go new file mode 100644 index 0000000..80ea5b7 --- /dev/null +++ b/nexgb/examples/doc.go @@ -0,0 +1,21 @@ +/* +Package examples contains a few different use cases of XGB, like creating +a window, reading properties, and querying for information about multiple +heads using the Xinerama or RandR extensions. + +If you're looking to get started quickly, I recommend checking out the +create-window example first. It is the most documented and probably covers +some of the more common bare bones cases of creating windows and responding +to events. + +If you're looking to query information about your window manager, +get-active-window is a start. However, to do anything extensive requires +a lot of boiler plate. To that end, I'd recommend use of my higher level +library, xgbutil: https://github.com/BurntSushi/xgbutil + +There are also examples of using the Xinerama and RandR extensions, if you're +interested in querying information about your active heads. In RandR's case, +you can also reconfigure your heads, but the example doesn't cover that. + +*/ +package documentation diff --git a/nexgb/examples/get-active-window/main.go b/nexgb/examples/get-active-window/main.go new file mode 100644 index 0000000..e90563c --- /dev/null +++ b/nexgb/examples/get-active-window/main.go @@ -0,0 +1,57 @@ +// Example get-active-window reads the _NET_ACTIVE_WINDOW property of the root +// window and uses the result (a window id) to get the name of the window. +package main + +import ( + "fmt" + "log" + + "github.com/BurntSushi/xgb" +) + +func main() { + X, err := xgb.NewConn() + if err != nil { + log.Fatal(err) + } + + // Get the window id of the root window. + root := X.DefaultScreen().Root + + // Get the atom id (i.e., intern an atom) of "_NET_ACTIVE_WINDOW". + aname := "_NET_ACTIVE_WINDOW" + activeAtom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() + if err != nil { + log.Fatal(err) + } + + // Get the atom id (i.e., intern an atom) of "_NET_WM_NAME". + aname = "_NET_WM_NAME" + nameAtom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() + if err != nil { + log.Fatal(err) + } + + // Get the actual value of _NET_ACTIVE_WINDOW. + // Note that 'reply.Value' is just a slice of bytes, so we use an + // XGB helper function, 'Get32', to pull an unsigned 32-bit integer out + // of the byte slice. We then convert it to an X resource id so it can + // be used to get the name of the window in the next GetProperty request. + reply, err := X.GetProperty(false, root, activeAtom.Atom, + xgb.GetPropertyTypeAny, 0, (1<<32)-1).Reply() + if err != nil { + log.Fatal(err) + } + windowId := xgb.Id(xgb.Get32(reply.Value)) + fmt.Printf("Active window id: %X\n", windowId) + + // Now get the value of _NET_WM_NAME for the active window. + // Note that this time, we simply convert the resulting byte slice, + // reply.Value, to a string. + reply, err = X.GetProperty(false, windowId, nameAtom.Atom, + xgb.GetPropertyTypeAny, 0, (1<<32)-1).Reply() + if err != nil { + log.Fatal(err) + } + fmt.Printf("Active window name: %s\n", string(reply.Value)) +} diff --git a/nexgb/examples/property.go b/nexgb/examples/property.go deleted file mode 100644 index 45144c7..0000000 --- a/nexgb/examples/property.go +++ /dev/null @@ -1,41 +0,0 @@ -package main - -import ( - "log" - - "github.com/BurntSushi/xgb" -) - -func init() { - log.SetFlags(0) -} - -func get32(buf []byte) uint32 { - v := uint32(buf[0]) - v |= uint32(buf[1]) << 8 - v |= uint32(buf[2]) << 16 - v |= uint32(buf[3]) << 24 - return v -} - -func main() { - X, err := xgb.NewConn() - if err != nil { - log.Fatal(err) - } - - root := X.DefaultScreen().Root - - aname := "_NET_ACTIVE_WINDOW" - atom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() - if err != nil { - log.Fatal(err) - } - - reply, err := X.GetProperty(false, root, atom.Atom, - xgb.GetPropertyTypeAny, 0, (1<<32)-1).Reply() - if err != nil { - log.Fatal(err) - } - log.Printf("%X", get32(reply.Value)) -} diff --git a/nexgb/examples/randr.go b/nexgb/examples/randr.go deleted file mode 100644 index 064856d..0000000 --- a/nexgb/examples/randr.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "fmt" - "log" - - "github.com/BurntSushi/xgb" -) - -func main() { - X, _ := xgb.NewConn() - - err := X.RegisterExtension("randr") - if err != nil { - log.Fatal(err) - } - - resources, err := X.RandrGetScreenResources(X.DefaultScreen().Root).Reply() - if err != nil { - log.Fatal(err) - } - - for _, output := range resources.Outputs { - info, err := X.RandrGetOutputInfo(output, 0).Reply() - if err != nil { - log.Fatal(err) - } - - bestMode := info.Modes[0] - for _, mode := range resources.Modes { - if mode.Id == uint32(bestMode) { - fmt.Printf("Width: %d, Height: %d\n", mode.Width, mode.Height) - } - } - } - - fmt.Println("\n") - - for _, crtc := range resources.Crtcs { - info, err := X.RandrGetCrtcInfo(crtc, 0).Reply() - if err != nil { - log.Fatal(err) - } - fmt.Printf("X: %d, Y: %d, Width: %d, Height: %d\n", - info.X, info.Y, info.Width, info.Height) - } -} - diff --git a/nexgb/examples/randr/main.go b/nexgb/examples/randr/main.go new file mode 100644 index 0000000..5c56609 --- /dev/null +++ b/nexgb/examples/randr/main.go @@ -0,0 +1,84 @@ +// Example randr uses the randr protocol to get information about the active +// heads. It also listens for events that are sent when the head configuration +// changes. Since it listens to events, you'll have to manually kill this +// process when you're done (i.e., ctrl+c.) +// +// While this program is running, if you use 'xrandr' to reconfigure your +// heads, you should see event information dumped to standard out. +// +// For more information, please see the RandR protocol spec: +// http://www.x.org/releases/X11R7.6/doc/randrproto/randrproto.txt +package main + +import ( + "fmt" + "log" + + "github.com/BurntSushi/xgb" +) + +func main() { + X, _ := xgb.NewConn() + + // Every extension must be initialized before it can be used. + err := X.RandrInit() + if err != nil { + log.Fatal(err) + } + + // Gets the current screen resources. Screen resources contains a list + // of names, crtcs, outputs and modes, among other things. + resources, err := X.RandrGetScreenResources(X.DefaultScreen().Root).Reply() + if err != nil { + log.Fatal(err) + } + + // Iterate through all of the outputs and show some of their info. + for _, output := range resources.Outputs { + info, err := X.RandrGetOutputInfo(output, 0).Reply() + if err != nil { + log.Fatal(err) + } + + bestMode := info.Modes[0] + for _, mode := range resources.Modes { + if mode.Id == uint32(bestMode) { + fmt.Printf("Width: %d, Height: %d\n", mode.Width, mode.Height) + } + } + } + + fmt.Println("\n") + + // Iterate through all of the crtcs and show some of their info. + for _, crtc := range resources.Crtcs { + info, err := X.RandrGetCrtcInfo(crtc, 0).Reply() + if err != nil { + log.Fatal(err) + } + fmt.Printf("X: %d, Y: %d, Width: %d, Height: %d\n", + info.X, info.Y, info.Width, info.Height) + } + + // Tell RandR to send us events. (I think these are all of them, as of 1.3.) + err = X.RandrSelectInputChecked(X.DefaultScreen().Root, + xgb.RandrNotifyMaskScreenChange| + xgb.RandrNotifyMaskCrtcChange| + xgb.RandrNotifyMaskOutputChange| + xgb.RandrNotifyMaskOutputProperty).Check() + if err != nil { + log.Fatal(err) + } + + // Listen to events and just dump them to standard out. + // A more involved approach will have to read the 'U' field of + // RandrNotifyEvent, which is a union (really a struct) of type + // RanrNotifyDataUnion. + for { + ev, err := X.WaitForEvent() + if err != nil { + log.Fatal(err) + } + fmt.Println(ev) + } +} diff --git a/nexgb/examples/seq-nowrap.go b/nexgb/examples/seq-nowrap.go deleted file mode 100644 index 994a07b..0000000 --- a/nexgb/examples/seq-nowrap.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/BurntSushi/xgb" -) - -func main() { - X, _ := xgb.NewConn() - - for i := 1; i <= 1<<16 + 10; i++ { - X.NoOperation() - // fmt.Printf("%d. No-Op\n", i) - } - - aname := "_NET_ACTIVE_WINDOW" - - for i := 1; i <= 10; i++ { - atom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() - if err != nil { - fmt.Println(err) - } else { - fmt.Printf("%d. Sequence: %d, Atom: %d\n", - i, atom.Sequence, atom.Atom) - } - } -} - diff --git a/nexgb/examples/seq-wrap.go b/nexgb/examples/seq-wrap.go deleted file mode 100644 index b5bc834..0000000 --- a/nexgb/examples/seq-wrap.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/BurntSushi/xgb" -) - -func main() { - X, _ := xgb.NewConn() - - aname := "_NET_ACTIVE_WINDOW" - - for i := 1; i <= 1<<16 + 10; i++ { - atom, err := X.InternAtom(true, uint16(len(aname)), aname).Reply() - if err != nil { - fmt.Println(err) - } else { - fmt.Printf("%d. Sequence: %d, Atom: %d\n", - i, atom.Sequence, atom.Atom) - } - } -} - diff --git a/nexgb/examples/window.go b/nexgb/examples/window.go deleted file mode 100644 index 68fe27b..0000000 --- a/nexgb/examples/window.go +++ /dev/null @@ -1,61 +0,0 @@ -package main - -import ( - "fmt" - "log" - - "github.com/BurntSushi/xgb" -) - -func main() { - X, err := xgb.NewConn() - if err != nil { - log.Fatal(err) - } - - wid, _ := X.NewId() - X.CreateWindow(X.DefaultScreen().RootDepth, wid, X.DefaultScreen().Root, - 0, 0, 500, 500, 0, - xgb.WindowClassInputOutput, X.DefaultScreen().RootVisual, - 0, []uint32{}) - X.ChangeWindowAttributes(wid, xgb.CwEventMask | xgb.CwBackPixel, - []uint32{0xffffffff, xgb.EventMaskKeyPress | xgb.EventMaskKeyRelease}) - - err = X.MapWindowChecked(wid).Check() - if err != nil { - fmt.Printf("Checked Error for mapping window %d: %s\n", wid, err) - } else { - fmt.Printf("Map window %d successful!\n", wid) - } - - err = X.MapWindowChecked(0x1).Check() - if err != nil { - fmt.Printf("Checked Error for mapping window 0x1: %s\n", err) - } else { - fmt.Printf("Map window 0x1 successful!\n") - } - - for { - ev, xerr := X.WaitForEvent() - if ev == nil && xerr == nil { - log.Fatal("Both event and error are nil. Exiting...") - } - - if ev != nil { - fmt.Printf("Event: %s\n", ev) - } - if xerr != nil { - fmt.Printf("Error: %s\n", xerr) - } - - if xerr == nil { - geom, err := X.GetGeometry(0x1).Reply() - if err != nil { - fmt.Printf("Geom Error: %#v\n", err) - } else { - fmt.Printf("Geometry: %#v\n", geom) - } - } - } -} - diff --git a/nexgb/examples/xinerama.go b/nexgb/examples/xinerama.go deleted file mode 100644 index 7fe9984..0000000 --- a/nexgb/examples/xinerama.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "fmt" - "log" - - "github.com/BurntSushi/xgb" -) - -func main() { - X, _ := xgb.NewConn() - - err := X.RegisterExtension("xinerama") - if err != nil { - log.Fatal(err) - } - - reply, err := X.XineramaQueryScreens().Reply() - if err != nil { - log.Fatal(err) - } - - fmt.Printf("Xinerama number: %d\n", reply.Number) - for i, screen := range reply.ScreenInfo { - fmt.Printf("%d :: X: %d, Y: %d, Width: %d, Height: %d\n", - i, screen.XOrg, screen.YOrg, screen.Width, screen.Height) - } -} - diff --git a/nexgb/examples/xinerama/main.go b/nexgb/examples/xinerama/main.go new file mode 100644 index 0000000..ec7f46a --- /dev/null +++ b/nexgb/examples/xinerama/main.go @@ -0,0 +1,39 @@ +// Example xinerama shows how to query the geometry of all active heads. +package main + +import ( + "fmt" + "log" + + "github.com/BurntSushi/xgb" +) + +func main() { + X, err := xgb.NewConn() + if err != nil { + log.Fatal(err) + } + + // Initialize the Xinerama extension. + // The appropriate 'Init' function must be run for *every* + // extension before any of its requests can be used. + err = X.XineramaInit() + if err != nil { + log.Fatal(err) + } + + // Issue a request to get the screen information. + reply, err := X.XineramaQueryScreens().Reply() + if err != nil { + log.Fatal(err) + } + + // reply.Number is the number of active heads, while reply.ScreenInfo + // is a slice of XineramaScreenInfo containing the rectangle geometry + // of each head. + fmt.Printf("Number of heads: %d\n", reply.Number) + for i, screen := range reply.ScreenInfo { + fmt.Printf("%d :: X: %d, Y: %d, Width: %d, Height: %d\n", + i, screen.XOrg, screen.YOrg, screen.Width, screen.Height) + } +} diff --git a/nexgb/xgb.go b/nexgb/xgb.go index 581b8fa..c9a265f 100644 --- a/nexgb/xgb.go +++ b/nexgb/xgb.go @@ -1,9 +1,3 @@ -// Copyright 2009 The XGB Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// The XGB package implements the X11 core protocol. -// It is based on XCB: http://xcb.freedesktop.org/ package xgb import ( diff --git a/nexgb/xgb_test.go b/nexgb/xgb_test.go index 9665164..b70ff5e 100644 --- a/nexgb/xgb_test.go +++ b/nexgb/xgb_test.go @@ -45,8 +45,8 @@ func init() { // Tests /******************************************************************************/ -// TestSynchronousError purposefully causes a BadLength error in an -// InternAtom request, and checks it synchronously. +// TestSynchronousError purposefully causes a BadWindow error in a +// MapWindow request, and checks it synchronously. func TestSynchronousError(t *testing.T) { err := X.MapWindowChecked(0).Check() // resource id 0 is always invalid if err == nil { @@ -205,7 +205,7 @@ func TestWindowEvents(t *testing.T) { // BenchmarkInternAtomsGood shows how many requests with replies // *should* be sent and gathered from the server. Namely, send as many // requests as you can at once, then go back and gather up all the replies. -// More importantly, this approach can exploit parallelism better when +// More importantly, this approach can exploit parallelism when // GOMAXPROCS > 1. // Run with `go test -run 'nomatch' -bench '.*' -cpu 1,2,6` if you have // multiple cores to see the improvement that parallelism brings. diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go index 35dd37e..f1762d3 100644 --- a/nexgb/xgbgen/context.go +++ b/nexgb/xgbgen/context.go @@ -86,7 +86,7 @@ func (c *Context) Morph(xmlBytes []byte) { c.Putln("case err != nil:") c.Putln("return err") c.Putln("case !reply.Present:") - c.Putln("return newError(\"No extension named %s could be found on " + + c.Putln("return newError(\"No extension named %s could be found on "+ "on the server.\")", xname) c.Putln("}") c.Putln("") diff --git a/nexgb/xgbgen/field.go b/nexgb/xgbgen/field.go index 4452408..7c83f1a 100644 --- a/nexgb/xgbgen/field.go +++ b/nexgb/xgbgen/field.go @@ -220,7 +220,7 @@ func (f *ExprField) Initialize(p *Protocol) { // integers. The mask specifies which kinds of values are in the list. // (i.e., See ConfigureWindow, CreateWindow, ChangeWindowAttributes, etc.) type ValueField struct { - Parent interface{} + Parent interface{} MaskType Type MaskName string ListName string @@ -247,7 +247,7 @@ func (f *ValueField) Size() Size { listSize := newExpressionSize(&Function{ Name: "pad", Expr: &BinaryOp{ - Op: "*", + Op: "*", Expr1: &Value{v: 4}, Expr2: &PopCount{ Expr: &Function{ diff --git a/nexgb/xgbgen/go_error.go b/nexgb/xgbgen/go_error.go index 0222289..9e01042 100644 --- a/nexgb/xgbgen/go_error.go +++ b/nexgb/xgbgen/go_error.go @@ -133,7 +133,7 @@ func (e *ErrorCopy) ImplementsError(c *Context) { func ErrorFieldString(c *Context, fields []Field, errName string) { c.Putln("fieldVals := make([]string, 0, %d)", len(fields)) c.Putln("fieldVals = append(fieldVals, \"NiceName: \" + err.NiceName)") - c.Putln("fieldVals = append(fieldVals, " + + c.Putln("fieldVals = append(fieldVals, "+ "sprintf(\"Sequence: %s\", err.Sequence))", "%d") for _, field := range fields { switch field.(type) { diff --git a/nexgb/xgbgen/go_event.go b/nexgb/xgbgen/go_event.go index ce54e19..f55e26f 100644 --- a/nexgb/xgbgen/go_event.go +++ b/nexgb/xgbgen/go_event.go @@ -165,7 +165,7 @@ func (e *EventCopy) Write(c *Context) { func EventFieldString(c *Context, fields []Field, evName string) { c.Putln("fieldVals := make([]string, 0, %d)", len(fields)) if evName != "KeymapNotify" { - c.Putln("fieldVals = append(fieldVals, " + + c.Putln("fieldVals = append(fieldVals, "+ "sprintf(\"Sequence: %s\", v.Sequence))", "%d") } for _, field := range fields { @@ -177,7 +177,8 @@ func EventFieldString(c *Context, fields []Field, evName string) { case *Base: case *Resource: case *TypeDef: - default: continue + default: + continue } switch field.SrcType() { diff --git a/nexgb/xgbgen/go_list.go b/nexgb/xgbgen/go_list.go index 41cfb76..ad859bb 100644 --- a/nexgb/xgbgen/go_list.go +++ b/nexgb/xgbgen/go_list.go @@ -107,4 +107,3 @@ func (f *ListField) Write(c *Context, prefix string) { f.XmlName(), f.Type) } } - diff --git a/nexgb/xgbgen/go_request_reply.go b/nexgb/xgbgen/go_request_reply.go index 451667f..a9e624d 100644 --- a/nexgb/xgbgen/go_request_reply.go +++ b/nexgb/xgbgen/go_request_reply.go @@ -71,14 +71,14 @@ func (r *Request) ReadReply(c *Context) { c.Putln("// Waits and reads reply data from request %s", r.SrcName()) c.Putln("func (cook %s) Reply() (*%s, error) {", r.CookieName(), r.ReplyTypeName()) - c.Putln("buf, err := cook.reply()") - c.Putln("if err != nil {") - c.Putln("return nil, err") - c.Putln("}") - c.Putln("if buf == nil {") - c.Putln("return nil, nil") - c.Putln("}") - c.Putln("return %s(buf), nil", r.ReplyName()) + c.Putln("buf, err := cook.reply()") + c.Putln("if err != nil {") + c.Putln("return nil, err") + c.Putln("}") + c.Putln("if buf == nil {") + c.Putln("return nil, nil") + c.Putln("}") + c.Putln("return %s(buf), nil", r.ReplyName()) c.Putln("}") c.Putln("") @@ -107,7 +107,7 @@ func (r *Request) ReadReply(c *Context) { func (r *Request) WriteRequest(c *Context) { writeSize := func() { - c.Putln("Put16(buf[b:], uint16(size / 4)) "+ + c.Putln("Put16(buf[b:], uint16(size / 4)) " + "// write request size in 4-byte units") c.Putln("b += 2") c.Putln("") diff --git a/nexgb/xgbgen/go_union.go b/nexgb/xgbgen/go_union.go index 9f339af..73e85f7 100644 --- a/nexgb/xgbgen/go_union.go +++ b/nexgb/xgbgen/go_union.go @@ -139,4 +139,3 @@ func (u *Union) WriteListSize(c *Context) { c.Putln("}") c.Putln("") } - diff --git a/nexgb/xgbgen/protocol.go b/nexgb/xgbgen/protocol.go index e01bc17..83dde14 100644 --- a/nexgb/xgbgen/protocol.go +++ b/nexgb/xgbgen/protocol.go @@ -38,4 +38,3 @@ func (p *Protocol) Initialize() { func (p *Protocol) isExt() bool { return strings.ToLower(p.Name) != "xproto" } - diff --git a/nexgb/xgbgen/request_reply.go b/nexgb/xgbgen/request_reply.go index c7a4cf8..4daa4ac 100644 --- a/nexgb/xgbgen/request_reply.go +++ b/nexgb/xgbgen/request_reply.go @@ -13,9 +13,9 @@ type Request struct { srcName string // The Go name of this request. xmlName string // The XML name of this request. Opcode int - Combine bool // Not currently used. + Combine bool // Not currently used. Fields []Field // All fields in the request. - Reply *Reply // A reply, if one exists for this request. + Reply *Reply // A reply, if one exists for this request. } // Initialize creates the proper Go source name for this request. diff --git a/nexgb/xgbgen/translation.go b/nexgb/xgbgen/translation.go index 592c152..e4d81bc 100644 --- a/nexgb/xgbgen/translation.go +++ b/nexgb/xgbgen/translation.go @@ -339,7 +339,7 @@ func (x *XMLField) Translate(parent interface{}) Field { } case "valueparam": return &ValueField{ - Parent: parent, + Parent: parent, MaskType: newTranslation(x.ValueMaskType), MaskName: x.ValueMaskName, ListName: x.ValueListName, diff --git a/nexgb/xgbgen/xml.go b/nexgb/xgbgen/xml.go index df21433..440d0a8 100644 --- a/nexgb/xgbgen/xml.go +++ b/nexgb/xgbgen/xml.go @@ -17,11 +17,11 @@ type XML struct { // Types for all top-level elements. // First are the simple ones. - Imports XMLImports `xml:"import"` - Enums []*XMLEnum `xml:"enum"` - Xids []*XMLXid `xml:"xidtype"` - XidUnions []*XMLXid `xml:"xidunion"` - TypeDefs []*XMLTypeDef `xml:"typedef"` + Imports XMLImports `xml:"import"` + Enums []*XMLEnum `xml:"enum"` + Xids []*XMLXid `xml:"xidtype"` + XidUnions []*XMLXid `xml:"xidunion"` + TypeDefs []*XMLTypeDef `xml:"typedef"` EventCopies []*XMLEventCopy `xml:"eventcopy"` ErrorCopies []*XMLErrorCopy `xml:"errorcopy"` @@ -93,21 +93,21 @@ type XMLErrorCopy struct { } type XMLStruct struct { - Name string `xml:"name,attr"` + Name string `xml:"name,attr"` Fields []*XMLField `xml:",any"` } type XMLUnion struct { - Name string `xml:"name,attr"` + Name string `xml:"name,attr"` Fields []*XMLField `xml:",any"` } type XMLRequest struct { - Name string `xml:"name,attr"` - Opcode int `xml:"opcode,attr"` - Combine bool `xml:"combine-adjacent,attr"` + Name string `xml:"name,attr"` + Opcode int `xml:"opcode,attr"` + Combine bool `xml:"combine-adjacent,attr"` Fields []*XMLField `xml:",any"` - Reply *XMLReply `xml:"reply"` + Reply *XMLReply `xml:"reply"` } type XMLReply struct { @@ -115,15 +115,15 @@ type XMLReply struct { } type XMLEvent struct { - Name string `xml:"name,attr"` - Number int `xml:"number,attr"` - NoSequence bool `xml:"no-sequence-number,attr"` + Name string `xml:"name,attr"` + Number int `xml:"number,attr"` + NoSequence bool `xml:"no-sequence-number,attr"` Fields []*XMLField `xml:",any"` } type XMLError struct { - Name string `xml:"name,attr"` - Number int `xml:"number,attr"` + Name string `xml:"name,attr"` + Number int `xml:"number,attr"` Fields []*XMLField `xml:",any"` } -- cgit v1.2.3-70-g09d2