go-tg-screenshot-bot
146 строк · 4.5 Кб
1/*
2Package XGB provides the X Go Binding, which is a low-level API to communicate
3with the core X protocol and many of the X extensions.
4
5It is *very* closely modeled on XCB, so that experience with XCB (or xpyb) is
6easily translatable to XGB. That is, it uses the same cookie/reply model
7and is thread safe. There are otherwise no major differences (in the API).
8
9Most uses of XGB typically fall under the realm of window manager and GUI kit
10development, but other applications (like pagers, panels, tilers, etc.) may
11also require XGB. Moreover, it is a near certainty that if you need to work
12with X, xgbutil will be of great use to you as well:
13https://github.com/jezek/xgbutil
14
15Example
16
17This is an extremely terse example that demonstrates how to connect to X,
18create a window, listen to StructureNotify events and Key{Press,Release}
19events, map the window, and print out all events received. An example with
20accompanying documentation can be found in examples/create-window.
21
22package main
23
24import (
25"fmt"
26"github.com/jezek/xgb"
27"github.com/jezek/xgb/xproto"
28)
29
30func main() {
31X, err := xgb.NewConn()
32if err != nil {
33fmt.Println(err)
34return
35}
36
37wid, _ := xproto.NewWindowId(X)
38screen := xproto.Setup(X).DefaultScreen(X)
39xproto.CreateWindow(X, screen.RootDepth, wid, screen.Root,
400, 0, 500, 500, 0,
41xproto.WindowClassInputOutput, screen.RootVisual,
42xproto.CwBackPixel | xproto.CwEventMask,
43[]uint32{ // values must be in the order defined by the protocol
440xffffffff,
45xproto.EventMaskStructureNotify |
46xproto.EventMaskKeyPress |
47xproto.EventMaskKeyRelease})
48
49xproto.MapWindow(X, wid)
50for {
51ev, xerr := X.WaitForEvent()
52if ev == nil && xerr == nil {
53fmt.Println("Both event and error are nil. Exiting...")
54return
55}
56
57if ev != nil {
58fmt.Printf("Event: %s\n", ev)
59}
60if xerr != nil {
61fmt.Printf("Error: %s\n", xerr)
62}
63}
64}
65
66Xinerama Example
67
68This is another small example that shows how to query Xinerama for geometry
69information of each active head. Accompanying documentation for this example
70can be found in examples/xinerama.
71
72package main
73
74import (
75"fmt"
76"log"
77"github.com/jezek/xgb"
78"github.com/jezek/xgb/xinerama"
79)
80
81func main() {
82X, err := xgb.NewConn()
83if err != nil {
84log.Fatal(err)
85}
86
87// Initialize the Xinerama extension.
88// The appropriate 'Init' function must be run for *every*
89// extension before any of its requests can be used.
90err = xinerama.Init(X)
91if err != nil {
92log.Fatal(err)
93}
94
95reply, err := xinerama.QueryScreens(X).Reply()
96if err != nil {
97log.Fatal(err)
98}
99
100fmt.Printf("Number of heads: %d\n", reply.Number)
101for i, screen := range reply.ScreenInfo {
102fmt.Printf("%d :: X: %d, Y: %d, Width: %d, Height: %d\n",
103i, screen.XOrg, screen.YOrg, screen.Width, screen.Height)
104}
105}
106
107Parallelism
108
109XGB can benefit greatly from parallelism due to its concurrent design. For
110evidence of this claim, please see the benchmarks in xproto/xproto_test.go.
111
112Tests
113
114xproto/xproto_test.go contains a number of contrived tests that stress
115particular corners of XGB that I presume could be problem areas. Namely:
116requests with no replies, requests with replies, checked errors, unchecked
117errors, sequence number wrapping, cookie buffer flushing (i.e., forcing a round
118trip every N requests made that don't have a reply), getting/setting properties
119and creating a window and listening to StructureNotify events.
120
121Code Generator
122
123Both XCB and xpyb use the same Python module (xcbgen) for a code generator. XGB
124(before this fork) used the same code generator as well, but in my attempt to
125add support for more extensions, I found the code generator extremely difficult
126to work with. Therefore, I re-wrote the code generator in Go. It can be found
127in its own sub-package, xgbgen, of xgb. My design of xgbgen includes a rough
128consideration that it could be used for other languages.
129
130What works
131
132I am reasonably confident that the core X protocol is in full working form. I've
133also tested the Xinerama and RandR extensions sparingly. Many of the other
134existing extensions have Go source generated (and are compilable) and are
135included in this package, but I am currently unsure of their status. They
136*should* work.
137
138What does not work
139
140XKB is the only extension that intentionally does not work, although I suspect
141that GLX also does not work (however, there is Go source code for GLX that
142compiles, unlike XKB). I don't currently have any intention of getting XKB
143working, due to its complexity and my current mental incapacity to test it.
144
145*/
146package xgb147