NewClaim, NewRequest can return an error

This commit is contained in:
Sasha Koshka 2024-07-11 02:12:53 -04:00
parent 36b3e4f0ed
commit 781d1436a4
4 changed files with 13 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package xgbsel package xgbsel
import "io" import "io"
import "errors"
import "github.com/jezek/xgb" import "github.com/jezek/xgb"
import "github.com/jezek/xgbutil" import "github.com/jezek/xgbutil"
import "github.com/jezek/xgb/xproto" import "github.com/jezek/xgb/xproto"
@ -20,7 +21,7 @@ type Claim struct {
// should be set to that of the event that triggered the claim, such as a Ctrl+C // should be set to that of the event that triggered the claim, such as a Ctrl+C
// event, or a mouse motion that led to text being selected. If the claim was // event, or a mouse motion that led to text being selected. If the claim was
// not triggered by an event, specify xproto.TimeCurrentTime. // not triggered by an event, specify xproto.TimeCurrentTime.
func NewClaim (window *xwindow.Window, selection xproto.Atom, data Data, timestamp xproto.Timestamp) *Claim { func NewClaim (window *xwindow.Window, selection xproto.Atom, data Data, timestamp xproto.Timestamp) (*Claim, error) {
// Follow: // Follow:
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.1 // https://tronche.com/gui/x/icccm/sec-2.html#s-2.1
@ -39,18 +40,20 @@ func NewClaim (window *xwindow.Window, selection xproto.Atom, data Data, timesta
err := xproto.SetSelectionOwnerChecked ( err := xproto.SetSelectionOwnerChecked (
window.X.Conn(), window.X.Conn(),
window.Id, selection, timestamp).Check() window.Id, selection, timestamp).Check()
if err != nil { return nil } if err != nil { return nil, err }
ownerReply, err := xproto.GetSelectionOwner ( ownerReply, err := xproto.GetSelectionOwner (
window.X.Conn(), selection).Reply() window.X.Conn(), selection).Reply()
if err != nil { return nil } if err != nil { return nil, err }
if ownerReply.Owner != window.Id { return nil } if ownerReply.Owner != window.Id {
return nil, errors.New("someone else took the selection")
}
return &Claim { return &Claim {
window: window, window: window,
data: data, data: data,
selection: selection, selection: selection,
} }, nil
} }
func (claim *Claim) refuseSelectionRequest (request xevent.SelectionRequestEvent) { func (claim *Claim) refuseSelectionRequest (request xevent.SelectionRequestEvent) {

View File

@ -62,7 +62,7 @@ func main () {
// obtain claim on CLIPBOARD // obtain claim on CLIPBOARD
log.Println("obtaining claim") log.Println("obtaining claim")
clipboard, _ := xprop.Atm(X, "CLIPBOARD") clipboard, _ := xprop.Atm(X, "CLIPBOARD")
claim := xgbsel.NewClaim(window, clipboard, data, xproto.TimeCurrentTime) claim, _ := xgbsel.NewClaim(window, clipboard, data, xproto.TimeCurrentTime)
// listen for events // listen for events
window.Listen(xproto.EventMaskPropertyChange) window.Listen(xproto.EventMaskPropertyChange)

View File

@ -79,7 +79,7 @@ func main () {
log.Println("creating request") log.Println("creating request")
clipboard, _ := xprop.Atm(X, "CLIPBOARD") clipboard, _ := xprop.Atm(X, "CLIPBOARD")
property, _ := xprop.Atm(X, "DESTINATION") property, _ := xprop.Atm(X, "DESTINATION")
request := xgbsel.NewRequest ( request, _ := xgbsel.NewRequest (
requestor { window: window }, requestor { window: window },
clipboard, clipboard,
property) property)

View File

@ -48,7 +48,7 @@ type Request struct {
} }
// NewRequest sends a new selection request. // NewRequest sends a new selection request.
func NewRequest (requestor Requestor, source, destination xproto.Atom) *Request { func NewRequest (requestor Requestor, source, destination xproto.Atom) (*Request, error) {
request := &Request { request := &Request {
source: source, source: source,
destination: destination, destination: destination,
@ -56,9 +56,9 @@ func NewRequest (requestor Requestor, source, destination xproto.Atom) *Request
} }
targets, err := xprop.Atm(requestor.Window().X, "TARGETS") targets, err := xprop.Atm(requestor.Window().X, "TARGETS")
if err != nil { request.die(err); return nil } if err != nil { return nil, err }
request.convertSelection(targets, selReqStateAwaitTargets) request.convertSelection(targets, selReqStateAwaitTargets)
return request return request, nil
} }
func (request *Request) convertSelection (target xproto.Atom, switchTo selReqState) { func (request *Request) convertSelection (target xproto.Atom, switchTo selReqState) {