aboutsummaryrefslogtreecommitdiffhomepage
path: root/xcb/export.go
diff options
context:
space:
mode:
authorOphestra Umiker <cat@ophivana.moe>2024-12-17 12:46:36 +0900
committerOphestra Umiker <cat@ophivana.moe>2024-12-17 12:46:36 +0900
commitc2b178e626ed18955aeada3180e9e4cb9bf2694b (patch)
tree84a1bdbcc2d456c82d9dd2508b75e38105f97afe /xcb/export.go
parentaeda40fc92f7a569eda58323daf13a86c50574fb (diff)
xcb: refactor and clean up
No clean way to write Go tests for this package. Will rely on NixOS tests for now. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'xcb/export.go')
-rw-r--r--xcb/export.go61
1 files changed, 10 insertions, 51 deletions
diff --git a/xcb/export.go b/xcb/export.go
index 35af9aa5..1ed9997f 100644
--- a/xcb/export.go
+++ b/xcb/export.go
@@ -1,63 +1,22 @@
// Package xcb implements X11 ChangeHosts via libxcb.
package xcb
-//#include <stdlib.h>
-//#include <xcb/xcb.h>
-//#cgo linux LDFLAGS: -lxcb
-import "C"
import (
"errors"
- "unsafe"
)
-const (
- HostModeInsert = C.XCB_HOST_MODE_INSERT
- HostModeDelete = C.XCB_HOST_MODE_DELETE
+var ErrChangeHosts = errors.New("xcb_change_hosts() failed")
- FamilyInternet = C.XCB_FAMILY_INTERNET
- FamilyDecnet = C.XCB_FAMILY_DECNET
- FamilyChaos = C.XCB_FAMILY_CHAOS
- FamilyServerInterpreted = C.XCB_FAMILY_SERVER_INTERPRETED
- FamilyInternet6 = C.XCB_FAMILY_INTERNET_6
-)
-
-type ConnectionError struct {
- err error
-}
-
-func (e *ConnectionError) Error() string {
- return e.err.Error()
-}
-
-func (e *ConnectionError) Unwrap() error {
- return e.err
-}
-
-var (
- ErrChangeHosts = errors.New("xcb_change_hosts() failed")
-)
-
-func ChangeHosts(mode, family C.uint8_t, address string) error {
- c := C.xcb_connect(nil, nil)
- defer C.xcb_disconnect(c)
-
- if err := xcbHandleConnectionError(c); err != nil {
- return &ConnectionError{err}
- }
-
- addr := C.CString(address)
- cookie := C.xcb_change_hosts_checked(c, mode, family, C.ushort(len(address)), (*C.uchar)(unsafe.Pointer(addr)))
- C.free(unsafe.Pointer(addr))
-
- if err := xcbHandleConnectionError(c); err != nil {
- return &ConnectionError{err}
- }
+func ChangeHosts(mode HostMode, family Family, address string) error {
+ var conn *connection
- e := C.xcb_request_check(c, cookie)
- if e != nil {
- defer C.free(unsafe.Pointer(e))
- return ErrChangeHosts
+ if c, err := connect(); err != nil {
+ c.disconnect()
+ return err
+ } else {
+ defer c.disconnect()
+ conn = c
}
- return nil
+ return conn.changeHostsChecked(mode, family, address)
}