aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/xcb/xcb.go
blob: 3dc62c3f9dc8f2ccff03d6ef6cc367517f256d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Package xcb implements X11 ChangeHosts via libxcb.
package xcb

import (
	"runtime"
	"unsafe"
)

/*
#cgo linux pkg-config: --static xcb

#include <stdlib.h>
#include <xcb/xcb.h>

static int hakurei_xcb_change_hosts_checked(xcb_connection_t *c,
                                            uint8_t mode, uint8_t family,
                                            uint16_t address_len, const uint8_t *address) {
  int ret;
  xcb_generic_error_t *e;
  xcb_void_cookie_t cookie;

  cookie = xcb_change_hosts_checked(c, mode, family, address_len, address);
  free((void *)address);

  ret = xcb_connection_has_error(c);
  if (ret != 0)
    return ret;

  e = xcb_request_check(c, cookie);
  if (e != NULL) {
    // don't want to deal with xcb errors
    free((void *)e);
    ret = -1;
  }

  return ret;
}
*/
import "C"

const (
	HostModeInsert = C.XCB_HOST_MODE_INSERT
	HostModeDelete = C.XCB_HOST_MODE_DELETE

	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 (
	HostMode = C.xcb_host_mode_t
	Family   = C.xcb_family_t
)

func (conn *connection) changeHostsChecked(mode HostMode, family Family, address string) error {
	ret := C.hakurei_xcb_change_hosts_checked(
		conn.c,
		C.uint8_t(mode),
		C.uint8_t(family),
		C.uint16_t(len(address)),
		(*C.uint8_t)(unsafe.Pointer(C.CString(address))),
	)
	switch ret {
	case 0:
		return nil
	case -1:
		return ErrChangeHosts
	default:
		return ConnectionError(ret)
	}
}

type connection struct{ c *C.xcb_connection_t }

func (conn *connection) connect() error {
	conn.c = C.xcb_connect(nil, nil)
	runtime.SetFinalizer(conn, (*connection).disconnect)
	return conn.hasError()
}

func (conn *connection) hasError() error {
	ret := C.xcb_connection_has_error(conn.c)
	if ret == 0 {
		return nil
	}
	return ConnectionError(ret)
}

func (conn *connection) disconnect() {
	C.xcb_disconnect(conn.c)

	// no need for a finalizer anymore
	runtime.SetFinalizer(conn, nil)
}

const (
	ConnError                 ConnectionError = C.XCB_CONN_ERROR
	ConnClosedExtNotSupported ConnectionError = C.XCB_CONN_CLOSED_EXT_NOTSUPPORTED
	ConnClosedMemInsufficient ConnectionError = C.XCB_CONN_CLOSED_MEM_INSUFFICIENT
	ConnClosedReqLenExceed    ConnectionError = C.XCB_CONN_CLOSED_REQ_LEN_EXCEED
	ConnClosedParseErr        ConnectionError = C.XCB_CONN_CLOSED_PARSE_ERR
	ConnClosedInvalidScreen   ConnectionError = C.XCB_CONN_CLOSED_INVALID_SCREEN
)

// ConnectionError represents an error returned by xcb_connection_has_error.
type ConnectionError int

func (ce ConnectionError) Error() string {
	switch ce {
	case ConnError:
		return "connection error"
	case ConnClosedExtNotSupported:
		return "extension not supported"
	case ConnClosedMemInsufficient:
		return "memory not available"
	case ConnClosedReqLenExceed:
		return "request length exceeded"
	case ConnClosedParseErr:
		return "invalid display string"
	case ConnClosedInvalidScreen:
		return "server has no screen matching display"
	default:
		return "generic X11 failure"
	}
}