github.com/robotn/xgb@v0.0.0-20190912153532-2cb92d044934/bigreq/bigreq.go (about)

     1  // Package bigreq is the X client API for the BIG-REQUESTS extension.
     2  package bigreq
     3  
     4  // This file is automatically generated from bigreq.xml. Edit at your peril!
     5  
     6  import (
     7  	"github.com/robotn/xgb"
     8  
     9  	"github.com/robotn/xgb/xproto"
    10  )
    11  
    12  // Init must be called before using the BIG-REQUESTS extension.
    13  func Init(c *xgb.Conn) error {
    14  	reply, err := xproto.QueryExtension(c, 12, "BIG-REQUESTS").Reply()
    15  	switch {
    16  	case err != nil:
    17  		return err
    18  	case !reply.Present:
    19  		return xgb.Errorf("No extension named BIG-REQUESTS could be found on on the server.")
    20  	}
    21  
    22  	c.ExtLock.Lock()
    23  	c.Extensions["BIG-REQUESTS"] = reply.MajorOpcode
    24  	c.ExtLock.Unlock()
    25  	for evNum, fun := range xgb.NewExtEventFuncs["BIG-REQUESTS"] {
    26  		xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
    27  	}
    28  	for errNum, fun := range xgb.NewExtErrorFuncs["BIG-REQUESTS"] {
    29  		xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
    30  	}
    31  	return nil
    32  }
    33  
    34  func init() {
    35  	xgb.NewExtEventFuncs["BIG-REQUESTS"] = make(map[int]xgb.NewEventFun)
    36  	xgb.NewExtErrorFuncs["BIG-REQUESTS"] = make(map[int]xgb.NewErrorFun)
    37  }
    38  
    39  // Skipping definition for base type 'Bool'
    40  
    41  // Skipping definition for base type 'Byte'
    42  
    43  // Skipping definition for base type 'Card8'
    44  
    45  // Skipping definition for base type 'Char'
    46  
    47  // Skipping definition for base type 'Void'
    48  
    49  // Skipping definition for base type 'Double'
    50  
    51  // Skipping definition for base type 'Float'
    52  
    53  // Skipping definition for base type 'Int16'
    54  
    55  // Skipping definition for base type 'Int32'
    56  
    57  // Skipping definition for base type 'Int8'
    58  
    59  // Skipping definition for base type 'Card16'
    60  
    61  // Skipping definition for base type 'Card32'
    62  
    63  // EnableCookie is a cookie used only for Enable requests.
    64  type EnableCookie struct {
    65  	*xgb.Cookie
    66  }
    67  
    68  // Enable sends a checked request.
    69  // If an error occurs, it will be returned with the reply by calling EnableCookie.Reply()
    70  func Enable(c *xgb.Conn) EnableCookie {
    71  	c.ExtLock.RLock()
    72  	defer c.ExtLock.RUnlock()
    73  	if _, ok := c.Extensions["BIG-REQUESTS"]; !ok {
    74  		panic("Cannot issue request 'Enable' using the uninitialized extension 'BIG-REQUESTS'. bigreq.Init(connObj) must be called first.")
    75  	}
    76  	cookie := c.NewCookie(true, true)
    77  	c.NewRequest(enableRequest(c), cookie)
    78  	return EnableCookie{cookie}
    79  }
    80  
    81  // EnableUnchecked sends an unchecked request.
    82  // If an error occurs, it can only be retrieved using xgb.WaitForEvent or xgb.PollForEvent.
    83  func EnableUnchecked(c *xgb.Conn) EnableCookie {
    84  	c.ExtLock.RLock()
    85  	defer c.ExtLock.RUnlock()
    86  	if _, ok := c.Extensions["BIG-REQUESTS"]; !ok {
    87  		panic("Cannot issue request 'Enable' using the uninitialized extension 'BIG-REQUESTS'. bigreq.Init(connObj) must be called first.")
    88  	}
    89  	cookie := c.NewCookie(false, true)
    90  	c.NewRequest(enableRequest(c), cookie)
    91  	return EnableCookie{cookie}
    92  }
    93  
    94  // EnableReply represents the data returned from a Enable request.
    95  type EnableReply struct {
    96  	Sequence uint16 // sequence number of the request for this reply
    97  	Length   uint32 // number of bytes in this reply
    98  	// padding: 1 bytes
    99  	MaximumRequestLength uint32
   100  }
   101  
   102  // Reply blocks and returns the reply data for a Enable request.
   103  func (cook EnableCookie) Reply() (*EnableReply, error) {
   104  	buf, err := cook.Cookie.Reply()
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	if buf == nil {
   109  		return nil, nil
   110  	}
   111  	return enableReply(buf), nil
   112  }
   113  
   114  // enableReply reads a byte slice into a EnableReply value.
   115  func enableReply(buf []byte) *EnableReply {
   116  	v := new(EnableReply)
   117  	b := 1 // skip reply determinant
   118  
   119  	b += 1 // padding
   120  
   121  	v.Sequence = xgb.Get16(buf[b:])
   122  	b += 2
   123  
   124  	v.Length = xgb.Get32(buf[b:]) // 4-byte units
   125  	b += 4
   126  
   127  	v.MaximumRequestLength = xgb.Get32(buf[b:])
   128  	b += 4
   129  
   130  	return v
   131  }
   132  
   133  // Write request to wire for Enable
   134  // enableRequest writes a Enable request to a byte slice.
   135  func enableRequest(c *xgb.Conn) []byte {
   136  	size := 4
   137  	b := 0
   138  	buf := make([]byte, size)
   139  
   140  	c.ExtLock.RLock()
   141  	buf[b] = c.Extensions["BIG-REQUESTS"]
   142  	c.ExtLock.RUnlock()
   143  	b += 1
   144  
   145  	buf[b] = 0 // request opcode
   146  	b += 1
   147  
   148  	xgb.Put16(buf[b:], uint16(size/4)) // write request size in 4-byte units
   149  	b += 2
   150  
   151  	return buf
   152  }