github.com/robotn/xgb@v0.0.0-20190912153532-2cb92d044934/xgbgen/protocol.go (about) 1 package main 2 3 import ( 4 "log" 5 "strings" 6 ) 7 8 // Protocol is a type that encapsulates all information about one 9 // particular XML file. It also contains links to other protocol types 10 // if this protocol imports other other extensions. The import relationship 11 // is recursive. 12 type Protocol struct { 13 Parent *Protocol 14 Name string 15 ExtXName string 16 ExtName string 17 MajorVersion string 18 MinorVersion string 19 20 Imports []*Protocol 21 Types []Type 22 Requests []*Request 23 } 24 25 type Protocols []*Protocol 26 27 func (ps Protocols) Len() int { return len(ps) } 28 func (ps Protocols) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] } 29 func (ps Protocols) Less(i, j int) bool { return ps[i].ExtName < ps[j].ExtName } 30 31 // Initialize traverses all structures, looks for 'Translation' type, 32 // and looks up the real type in the namespace. It also sets the source 33 // name for all relevant fields/structures. 34 // This is necessary because we don't traverse the XML in order initially. 35 func (p *Protocol) Initialize() { 36 for _, typ := range p.Types { 37 typ.Initialize(p) 38 } 39 for _, req := range p.Requests { 40 req.Initialize(p) 41 } 42 } 43 44 // PkgName returns the name of this package. 45 // i.e., 'xproto' for the core X protocol, 'randr' for the RandR extension, etc. 46 func (p *Protocol) PkgName() string { 47 return strings.Replace(p.Name, "_", "", -1) 48 } 49 50 // ProtocolGet searches the current context for the protocol with the given 51 // name. (i.e., the current protocol and its imports.) 52 // It is an error if one is not found. 53 func (p *Protocol) ProtocolFind(name string) *Protocol { 54 if p.Name == name { 55 return p // that was easy 56 } 57 for _, imp := range p.Imports { 58 if imp.Name == name { 59 return imp 60 } 61 } 62 log.Panicf("Could not find protocol with name '%s'.", name) 63 panic("unreachable") 64 } 65 66 // isExt returns true if this protocol is an extension. 67 // i.e., it's name isn't "xproto". 68 func (p *Protocol) isExt() bool { 69 return strings.ToLower(p.Name) != "xproto" 70 }