NewClaim, NewRequest can return an error
This commit is contained in:
parent
36b3e4f0ed
commit
781d1436a4
13
v2/claim.go
13
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) {
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user