|
| 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