aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/system/xhost.go
blob: f4a520ea1a07d01118f0ce717eaaa00dca1b59a8 (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
package system

import (
	"hakurei.app/hst"
	"hakurei.app/internal/xcb"
)

// ChangeHosts inserts the target user into X11 hosts and deletes it once its
// [hst.Enablements] is no longer satisfied.
func (sys *I) ChangeHosts(username string) *I {
	sys.ops = append(sys.ops, xhostOp(username))
	return sys
}

// xhostOp implements [I.ChangeHosts].
type xhostOp string

func (x xhostOp) Type() hst.Enablements { return hst.EX11 }

func (x xhostOp) apply(sys *I) error {
	sys.msg.Verbosef("inserting entry %s to X11", x)
	return newOpError("xhost",
		sys.xcbChangeHosts(xcb.HostModeInsert, xcb.FamilyServerInterpreted, "localuser\x00"+string(x)), false)
}

func (x xhostOp) revert(sys *I, ec *Criteria) error {
	if ec.hasType(x.Type()) {
		sys.msg.Verbosef("deleting entry %s from X11", x)
		return newOpError("xhost",
			sys.xcbChangeHosts(xcb.HostModeDelete, xcb.FamilyServerInterpreted, "localuser\x00"+string(x)), true)
	} else {
		sys.msg.Verbosef("skipping entry %s in X11", x)
		return nil
	}
}

func (x xhostOp) Is(o Op) bool   { target, ok := o.(xhostOp); return ok && x == target }
func (x xhostOp) Path() string   { return "/tmp/.X11-unix" }
func (x xhostOp) String() string { return string("SI:localuser:" + x) }