Skip to content

Commit 5969aa9

Browse files
committed
xlib demo
1 parent fad5cb2 commit 5969aa9

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

example/window.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"github.com/martine/gocairo/cairo"
19+
"github.com/martine/gocairo/xlib"
20+
)
21+
22+
type callbacks struct{}
23+
24+
func (c *callbacks) Draw(cr *cairo.Context, surf *cairo.XlibSurface) {
25+
w, h := surf.GetWidth(), surf.GetHeight()
26+
grid := 32
27+
28+
cr.SetSourceRGB(0, 0, 0)
29+
cr.Paint()
30+
31+
cr.SetAntialias(cairo.AntialiasBest)
32+
// Offset by 0.5 to get pixel-aligned lines.
33+
cr.Translate(0.5, 0.5)
34+
cr.SetSourceRGB(1, 0, 0)
35+
for x := 0; x <= w; x += grid {
36+
for y := 0; y <= h; y += grid {
37+
ofs := x/grid + y/grid
38+
cr.Rectangle(float64(x+ofs), float64(y+ofs),
39+
float64(grid-(2*ofs)), float64(grid-(2*ofs)))
40+
cr.Fill()
41+
}
42+
}
43+
}
44+
45+
func main() {
46+
var callbacks callbacks
47+
xlib.XMain(&callbacks)
48+
}

xlib/xeventtype_string.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// generated by stringer -type=XEventType; DO NOT EDIT
2+
3+
package xlib
4+
5+
import "fmt"
6+
7+
const _XEventType_name = "KeyPressKeyReleaseButtonPressButtonReleaseMotionNotifyEnterNotifyLeaveNotifyFocusInFocusOutKeymapNotifyExposeGraphicsExposeNoExposeVisibilityNotifyCreateNotifyDestroyNotifyUnmapNotifyMapNotifyMapRequestReparentNotifyConfigureNotifyConfigureRequestGravityNotifyResizeRequestCirculateNotifyCirculateRequestPropertyNotifySelectionClearSelectionRequestSelectionNotifyColormapNotifyClientMessageMappingNotifyGenericEvent"
8+
9+
var _XEventType_index = [...]uint16{0, 8, 18, 29, 42, 54, 65, 76, 83, 91, 103, 109, 123, 131, 147, 159, 172, 183, 192, 202, 216, 231, 247, 260, 273, 288, 304, 318, 332, 348, 363, 377, 390, 403, 415}
10+
11+
func (i XEventType) String() string {
12+
i -= 2
13+
if i < 0 || i+1 >= XEventType(len(_XEventType_index)) {
14+
return fmt.Sprintf("XEventType(%d)", i+2)
15+
}
16+
return _XEventType_name[_XEventType_index[i]:_XEventType_index[i+1]]
17+
}

xlib/xlib.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package xlib
16+
17+
/*
18+
#cgo pkg-config: x11
19+
#include <X11/Xlib.h>
20+
*/
21+
import "C"
22+
23+
import (
24+
"unsafe"
25+
26+
"github.com/martine/gocairo/cairo"
27+
)
28+
29+
type Callbacks interface {
30+
Draw(*cairo.Context, *cairo.XlibSurface)
31+
}
32+
33+
type Window struct {
34+
dpy *C.Display
35+
xw C.Window
36+
}
37+
38+
// Note that stringer doesn't work in the presence of C types. I worked
39+
// around this by just commenting out all the code except for XEventType
40+
// when running "go generate".
41+
42+
//go:generate stringer -type=XEventType
43+
type XEventType int
44+
45+
const (
46+
KeyPress XEventType = 2
47+
KeyRelease XEventType = 3
48+
ButtonPress XEventType = 4
49+
ButtonRelease XEventType = 5
50+
MotionNotify XEventType = 6
51+
EnterNotify XEventType = 7
52+
LeaveNotify XEventType = 8
53+
FocusIn XEventType = 9
54+
FocusOut XEventType = 10
55+
KeymapNotify XEventType = 11
56+
Expose XEventType = 12
57+
GraphicsExpose XEventType = 13
58+
NoExpose XEventType = 14
59+
VisibilityNotify XEventType = 15
60+
CreateNotify XEventType = 16
61+
DestroyNotify XEventType = 17
62+
UnmapNotify XEventType = 18
63+
MapNotify XEventType = 19
64+
MapRequest XEventType = 20
65+
ReparentNotify XEventType = 21
66+
ConfigureNotify XEventType = 22
67+
ConfigureRequest XEventType = 23
68+
GravityNotify XEventType = 24
69+
ResizeRequest XEventType = 25
70+
CirculateNotify XEventType = 26
71+
CirculateRequest XEventType = 27
72+
PropertyNotify XEventType = 28
73+
SelectionClear XEventType = 29
74+
SelectionRequest XEventType = 30
75+
SelectionNotify XEventType = 31
76+
ColormapNotify XEventType = 32
77+
ClientMessage XEventType = 33
78+
MappingNotify XEventType = 34
79+
GenericEvent XEventType = 35
80+
)
81+
82+
func XMain(callbacks Callbacks) {
83+
dpy := C.XOpenDisplay(nil)
84+
85+
w := C.XCreateSimpleWindow(dpy, C.XDefaultRootWindow(dpy),
86+
0, 0, 600, 400,
87+
0, 0, 0)
88+
C.XSelectInput(dpy, w, C.StructureNotifyMask|C.SubstructureNotifyMask|C.ExposureMask)
89+
C.XMapWindow(dpy, w)
90+
91+
win := Window{dpy: dpy, xw: w}
92+
visual := C.XDefaultVisual(dpy, 0)
93+
94+
surf := cairo.XlibSurfaceCreate(unsafe.Pointer(dpy), uint64(win.xw), unsafe.Pointer(visual), 10, 10)
95+
96+
for {
97+
var e C.XEvent
98+
C.XNextEvent(dpy, &e)
99+
typ := XEventType(*(*C.int)(unsafe.Pointer(&e)))
100+
// log.Printf("X event: %s", typ)
101+
switch typ {
102+
case C.ConfigureNotify:
103+
e := (*C.XConfigureEvent)(unsafe.Pointer(&e))
104+
surf.SetSize(int(e.width), int(e.height))
105+
case C.Expose:
106+
cr := cairo.Create(surf.Surface)
107+
callbacks.Draw(cr, surf)
108+
default:
109+
// log.Printf("unknown X event %s", typ)
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)