Add support for giving up the selection, forcibly and voluntarily

This commit is contained in:
Sasha Koshka 2024-07-11 02:38:30 -04:00
parent 751ec20833
commit 0180a79b25
2 changed files with 37 additions and 0 deletions

View File

@ -14,6 +14,8 @@ type Claim struct {
window *xwindow.Window
data Data
selection xproto.Atom
timestamp xproto.Timestamp
active bool
}
// NewClaim claims ownership of a specified selection, and allows using data
@ -50,9 +52,11 @@ func NewClaim (window *xwindow.Window, selection xproto.Atom, data Data, timesta
}
return &Claim {
active: true,
window: window,
data: data,
selection: selection,
timestamp: timestamp,
}, nil
}
@ -184,3 +188,35 @@ func (claim *Claim) HandlePropertyNotify (
) {
// TODO
}
// While the selection claim is active, HandleSelectionClear should be called
// when the requesting window recieves a SelectionClear event. This must be
// registered as an event handler manually.
func (claim *Claim) HandleSelectionClear (
connection *xgbutil.XUtil,
event xevent.SelectionClearEvent,
) {
// If a client gives up ownership of a selection or if some other client
// executes a SetSelectionOwner for it and thus reassigns it forcibly,
// the previous owner will receive a SelectionClear event.
claim.active = false
}
// Close voluntarily relinquishes the selection claim. This will inform the X
// server that the selection is being voluntarily given up, and cause the claim
// to stop responding to events.
func (claim *Claim) Close () error {
if !claim.active { return nil }
claim.active = false
// To relinquish ownership of a selection voluntarily, a client should
// execute a SetSelectionOwner request for that selection atom, with
// owner specified as None and the time specified as the timestamp that
// was used to acquire the selection.
err := xproto.SetSelectionOwnerChecked (
claim.window.X.Conn(),
0, claim.selection, claim.timestamp).Check()
if err != nil { return err }
return nil
}

View File

@ -68,6 +68,7 @@ func main () {
window.Listen(xproto.EventMaskPropertyChange)
xevent.PropertyNotifyFun(claim.HandlePropertyNotify).Connect(X, window.Id)
xevent.SelectionRequestFun(claim.HandleSelectionRequest).Connect(X, window.Id)
xevent.SelectionClearFun(claim.HandleSelectionClear).Connect(X, window.Id)
log.Println("running main event loop")
xevent.Main(X)