From 781d1436a4a5a7d648e91be175d740ea4b5aa02b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 11 Jul 2024 02:12:53 -0400 Subject: [PATCH] NewClaim, NewRequest can return an error --- v2/claim.go | 13 ++++++++----- v2/examples/copy/main.go | 2 +- v2/examples/paste/main.go | 2 +- v2/request.go | 6 +++--- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/v2/claim.go b/v2/claim.go index 01b194c..b024cdd 100644 --- a/v2/claim.go +++ b/v2/claim.go @@ -1,6 +1,7 @@ package xgbsel import "io" +import "errors" import "github.com/jezek/xgb" import "github.com/jezek/xgbutil" 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 // event, or a mouse motion that led to text being selected. If the claim was // 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: // 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 ( window.X.Conn(), window.Id, selection, timestamp).Check() - if err != nil { return nil } + if err != nil { return nil, err } ownerReply, err := xproto.GetSelectionOwner ( window.X.Conn(), selection).Reply() - if err != nil { return nil } - if ownerReply.Owner != window.Id { return nil } + if err != nil { return nil, err } + if ownerReply.Owner != window.Id { + return nil, errors.New("someone else took the selection") + } return &Claim { window: window, data: data, selection: selection, - } + }, nil } func (claim *Claim) refuseSelectionRequest (request xevent.SelectionRequestEvent) { diff --git a/v2/examples/copy/main.go b/v2/examples/copy/main.go index ba3d421..21726f0 100644 --- a/v2/examples/copy/main.go +++ b/v2/examples/copy/main.go @@ -62,7 +62,7 @@ func main () { // obtain claim on CLIPBOARD log.Println("obtaining claim") clipboard, _ := xprop.Atm(X, "CLIPBOARD") - claim := xgbsel.NewClaim(window, clipboard, data, xproto.TimeCurrentTime) + claim, _ := xgbsel.NewClaim(window, clipboard, data, xproto.TimeCurrentTime) // listen for events window.Listen(xproto.EventMaskPropertyChange) diff --git a/v2/examples/paste/main.go b/v2/examples/paste/main.go index bcd181a..20d34ea 100644 --- a/v2/examples/paste/main.go +++ b/v2/examples/paste/main.go @@ -79,7 +79,7 @@ func main () { log.Println("creating request") clipboard, _ := xprop.Atm(X, "CLIPBOARD") property, _ := xprop.Atm(X, "DESTINATION") - request := xgbsel.NewRequest ( + request, _ := xgbsel.NewRequest ( requestor { window: window }, clipboard, property) diff --git a/v2/request.go b/v2/request.go index dcd02f0..e30bd70 100644 --- a/v2/request.go +++ b/v2/request.go @@ -48,7 +48,7 @@ type Request struct { } // 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 { source: source, destination: destination, @@ -56,9 +56,9 @@ func NewRequest (requestor Requestor, source, destination xproto.Atom) *Request } 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) - return request + return request, nil } func (request *Request) convertSelection (target xproto.Atom, switchTo selReqState) {