Compare commits

14 Commits

5 changed files with 259 additions and 38 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"
@@ -8,26 +9,41 @@ import "github.com/jezek/xgbutil/xprop"
import "github.com/jezek/xgbutil/xevent" import "github.com/jezek/xgbutil/xevent"
import "github.com/jezek/xgbutil/xwindow" import "github.com/jezek/xgbutil/xwindow"
const format8bit byte = 8
const format16bit byte = 16
const format32bit byte = 32
const propertyNewValue byte = 0
const propertyDelete byte = 1
type claimRequest struct {
reader io.ReadSeekCloser
ty xproto.Atom
event xevent.SelectionRequestEvent
}
type claimRequestKey struct {
property xproto.Atom
requestor xproto.Window
}
// Claim represents a claim that a window has on a particular selection. // Claim represents a claim that a window has on a particular selection.
type Claim struct { type Claim struct {
open bool
incr bool
window *xwindow.Window window *xwindow.Window
data Data data Data
selection xproto.Atom selection xproto.Atom
timestamp xproto.Timestamp
requests map[claimRequestKey] *claimRequest
} }
// NewClaim claims ownership of a specified selection, and allows using data // NewClaim claims ownership of a specified selection, and allows using data
// passed to it to fulfill requests for the selection's contents. If the claim // passed to it to fulfill requests for the selection's contents. The timestamp
// happens because of a user input event, use NewClaimWithTimestamp instead of // should be set to that of the event that triggered the claim, such as a Ctrl+C
// this function. See the documentation of NewClaimWithTimestamp for details. // event, or a mouse motion that led to text being selected. If the claim was
func NewClaim (window *xwindow.Window, selection xproto.Atom, data Data) *Claim { // not triggered by an event, specify xproto.TimeCurrentTime.
return NewClaimWithTimestamp(window, selection, data, 0) func NewClaim (window *xwindow.Window, selection xproto.Atom, data Data, timestamp xproto.Timestamp) (*Claim, error) {
}
// NewClaimWithTimestamp claims ownership of a specified selection, and allows
// using data passed to it to fulfill requests for the selection's contents. The
// timestamp 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.
func NewClaimWithTimestamp (window *xwindow.Window, selection xproto.Atom, data Data, timestamp xproto.Timestamp) *Claim {
// 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
@@ -46,18 +62,24 @@ func NewClaimWithTimestamp (window *xwindow.Window, selection xproto.Atom, data
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 {
open: true,
incr: false, // TODO change to enable INCR once its done
window: window, window: window,
data: data, data: data,
selection: selection, selection: selection,
} timestamp: timestamp,
requests: make(map[claimRequestKey] *claimRequest),
}, nil
} }
func (claim *Claim) refuseSelectionRequest (request xevent.SelectionRequestEvent) { func (claim *Claim) refuseSelectionRequest (request xevent.SelectionRequestEvent) {
@@ -88,12 +110,10 @@ func (claim *Claim) fulfillSelectionRequest (
// property on the requestor window and should set the property's type // property on the requestor window and should set the property's type
// to some appropriate value, which need not be the same as the // to some appropriate value, which need not be the same as the
// specified target. // specified target.
err := xproto.ChangePropertyChecked ( err := claim.changePropertyChecked (
claim.window.X.Conn(),
xproto.PropModeReplace, request.Requestor, xproto.PropModeReplace, request.Requestor,
request.Property, request.Property,
ty, format, ty, format, data)
uint32(len(data) / (int(format) / 8)), data).Check()
if err != nil { die() } if err != nil { die() }
// If the property is successfully stored, the owner should acknowledge // If the property is successfully stored, the owner should acknowledge
@@ -111,6 +131,19 @@ func (claim *Claim) fulfillSelectionRequest (
false, request.Requestor, 0, string(event)) false, request.Requestor, 0, string(event))
} }
func (claim *Claim) changePropertyChecked (
mode byte,
window xproto.Window,
property xproto.Atom,
ty xproto.Atom,
format byte,
data []byte,
) error {
return xproto.ChangePropertyChecked (
claim.window.X.Conn(), mode, window, property, ty, format,
uint32(len(data) / (int(format) / 8)), data).Check()
}
// While the selection claim is active, HandleSelectionRequest should be called // While the selection claim is active, HandleSelectionRequest should be called
// when the owner window recieves a SelectionRequest event. This must be // when the owner window recieves a SelectionRequest event. This must be
// registered as an event handler manually. // registered as an event handler manually.
@@ -118,6 +151,10 @@ func (claim *Claim) HandleSelectionRequest (
connection *xgbutil.XUtil, connection *xgbutil.XUtil,
event xevent.SelectionRequestEvent, event xevent.SelectionRequestEvent,
) { ) {
// do not fulfill *new* selection requests after the claim has been
// relinquished
if !claim.open { return }
// Follow: // Follow:
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.2 // https://tronche.com/gui/x/icccm/sec-2.html#s-2.2
@@ -165,16 +202,185 @@ func (claim *Claim) HandleSelectionRequest (
// send that list to the requestor // send that list to the requestor
atomAtom, err := xprop.Atm(claim.window.X, "ATOM") atomAtom, err := xprop.Atm(claim.window.X, "ATOM")
if err != nil { die(); return } if err != nil { die(); return }
claim.fulfillSelectionRequest(data, 32, atomAtom, event) claim.fulfillSelectionRequest(data, format32bit, atomAtom, event)
default: default:
incr, err := xprop.Atm(claim.window.X, "INCR")
if err != nil { die(); return }
// respond with data // respond with data
reader, ok := claim.data.Convert(Target(targetName)) reader, ok := claim.data.Convert(Target(targetName))
if !ok { die(); return } if !ok { die(); return }
reader.Seek(0, io.SeekStart)
data, err := io.ReadAll(reader) if claim.incr {
reader.Close() // TODO: we need to subscribe to PropertyNotify events
if err != nil { die() } // on the destination window. apparently event masks as
claim.fulfillSelectionRequest(data, 8, event.Target, event) // set by ChangeWindowAttributes are client-specific.
// we also need to clean up afterwards.
// https://tronche.com/gui/x/xlib/window/XChangeWindowAttributes.html
request := &claimRequest {
reader: reader,
ty: event.Target,
event: event,
}
// Requestors may receive a property of type INCR in
// response to any target that results in selection
// data. This indicates that the owner will send the
// actual data incrementally. The contents of the INCR
// property will be an integer, which represents a lower
// bound on the number of bytes of data in the
// selection.
data := [4]byte { }
xgb.Put32(data[:], 1)
err := claim.changePropertyChecked (
xproto.PropModeReplace, event.Requestor,
event.Property, incr, format32bit, data[:])
if err != nil { die() }
claim.requests[claimRequestKey {
property: event.Property,
requestor: event.Requestor,
}] = request
} else {
reader.Seek(0, io.SeekStart)
data, err := io.ReadAll(reader)
reader.Close()
if err != nil { die() }
claim.fulfillSelectionRequest(data, format8bit, event.Target, event)
}
} }
} }
// While the selection claim is active, HandlePropertyNotify should be called
// when the requesting window recieves a PropertyNotify event. This must be
// registered as an event handler manually.
func (claim *Claim) HandlePropertyNotify (
connection *xgbutil.XUtil,
event xevent.PropertyNotifyEvent,
) {
// The selection requestor starts the transfer process by deleting the
// (type==INCR) property forming the reply to the selection.
//
// The selection owner then:
// ... Waits between each append for a PropertyNotify (state==Deleted)
// event that shows that the requestor has read the data. The reason for
// doing this is to limit the consumption of space in the server.
if event.State != propertyDelete { return }
// if the request does not exist, the property was either modified in
// error or the request has already finished. either way, we simply
// exit.
key := claimRequestKey {
property: event.Atom,
requestor: event.Window,
}
request, ok := claim.requests[key]
if !ok { return }
done := func () {
request.reader.Close()
delete(claim.requests, key)
}
die := func () {
done()
claim.refuseSelectionRequest(request.event)
}
// FIXME change the length of this if maximum-request-size is smaller
buffer := [1024]byte { }
size, err := request.reader.Read(buffer[:])
if err != nil { die(); return }
dataRead := buffer[:size]
if len(dataRead) > 0 {
// there is more data to send
// ... Appends the data in suitable-size chunks to the same property on
// the same window as the selection reply with a type corresponding to
// the actual type of the converted selection. The size should be less
// than the maximum-request-size in the connection handshake.
err := claim.changePropertyChecked (
xproto.PropModeReplace,
request.event.Requestor, request.event.Property,
request.ty, format8bit, dataRead)
if err != nil { die(); return }
} else {
// all data has been sent
// ... Waits (after the entire data has been transferred to the
// server) until a PropertyNotify (state==Deleted) event that
// shows that the data has been read by the requestor and then
// writes zero-length data to the property.
err := claim.changePropertyChecked (
xproto.PropModeReplace,
request.event.Requestor, request.event.Property,
request.ty, format8bit, nil)
if err != nil { die(); return }
done()
}
}
// 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,
) {
// When some other client acquires a selection, the previous owner
// receives a SelectionClear event. The timestamp argument is the time
// at which the ownership changed hands, and the owner argument is the
// window the previous owner specified in its SetSelectionOwner request.
// If an owner loses ownership while it has a transfer in progress (that
// is, before it receives notification that the requestor has received
// all the data), it must continue to service the ongoing transfer until
// it is complete.
claim.open = false
}
// Prune checks for requests that cannot continue and removes them. If this
// claim is to be held for an extended period of time, this method should be
// called every so often.
func (claim *Claim) Prune () {
// TODO
// check all active requests to see if anyone's window has closed, and
// if it has, close the request
}
// Open returns false if the claim has been relinquished, either from recieving
// a SelectionClear event or with the Close method.
func (claim *Claim) Open () bool {
return claim.open
}
// Active returns true if requests are currently being fulfilled. While this is
// true, the claim should still be forwarded events regardless if it is closed
// or not. It is safe to forward the same event to multiple claims.
func (claim *Claim) Active () bool {
return len(claim.requests) > 0
}
// 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 fulfilling new requests.
func (claim *Claim) Close () error {
if !claim.open { return nil }
claim.open = 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

@@ -62,11 +62,13 @@ 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) claim, _ := xgbsel.NewClaim(window, clipboard, data, xproto.TimeCurrentTime)
// listen for events // listen for events
window.Listen(xproto.EventMaskPropertyChange) window.Listen(xproto.EventMaskPropertyChange)
xevent.PropertyNotifyFun(claim.HandlePropertyNotify).Connect(X, window.Id)
xevent.SelectionRequestFun(claim.HandleSelectionRequest).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") log.Println("running main event loop")
xevent.Main(X) xevent.Main(X)

View File

@@ -79,10 +79,11 @@ 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,
xproto.TimeCurrentTime)
// listen for events // listen for events
window.Listen(xproto.EventMaskPropertyChange) window.Listen(xproto.EventMaskPropertyChange)

6
v2/go.sum Normal file
View File

@@ -0,0 +1,6 @@
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ=
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966/go.mod h1:Mid70uvE93zn9wgF92A/r5ixgnvX8Lh68fxp9KQBaI0=
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/jezek/xgbutil v0.0.0-20230603163917-04188eb39cf0 h1:Pf/0BAbppEOq4azPH6fnvUX2dycAwZdGkdxFn25j44c=
github.com/jezek/xgbutil v0.0.0-20230603163917-04188eb39cf0/go.mod h1:AHecLyFNy6AN9f/+0AH/h1MI7X1+JL5bmCz4XlVZk7Y=

View File

@@ -45,20 +45,25 @@ type Request struct {
destination xproto.Atom destination xproto.Atom
incrBuffer []byte incrBuffer []byte
incrTarget Target incrTarget Target
timestamp xproto.Timestamp
} }
// NewRequest sends a new selection request. // NewRequest sends a new selection request. The timestamp should be set to that
func NewRequest (requestor Requestor, source, destination xproto.Atom) *Request { // of the event that triggered the request, such as a Ctrl+V event, or a mouse
// button event that led to text being pasted. If the claim was not triggered by
// an event, and *only* in this scenario, specify xproto.TimeCurrentTime.
func NewRequest (requestor Requestor, source, destination xproto.Atom, timestamp xproto.Timestamp) (*Request, error) {
request := &Request { request := &Request {
source: source, source: source,
destination: destination, destination: destination,
requestor: requestor, requestor: requestor,
timestamp: timestamp,
} }
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) {
@@ -86,17 +91,14 @@ func (request *Request) convertSelection (target xproto.Atom, switchTo selReqSta
// to a window that it created; the owner will place the reply property // to a window that it created; the owner will place the reply property
// there. The requestor should set the time argument to the timestamp on // there. The requestor should set the time argument to the timestamp on
// the event that triggered the request for the selection value. Note // the event that triggered the request for the selection value. Note
// that clients should not specify CurrentTime*. // that clients should not specify CurrentTime.
err = xproto.ConvertSelectionChecked ( err = xproto.ConvertSelectionChecked (
request.requestor.Window().X.Conn(), request.requestor.Window().X.Conn(),
request.requestor.Window().Id, request.requestor.Window().Id,
request.source, request.source,
target, target,
request.destination, request.destination,
// TODO: *possibly replace this zero with an actual timestamp request.timestamp).Check()
// received from the server. this is non-trivial as we cannot
// rely on the timestamp of the last received event.
0).Check()
if err != nil { request.die(err); return } if err != nil { request.die(err); return }
request.state = switchTo request.state = switchTo
@@ -255,6 +257,10 @@ func (request *Request) HandlePropertyNotify (
connection *xgbutil.XUtil, connection *xgbutil.XUtil,
event xevent.PropertyNotifyEvent, event xevent.PropertyNotifyEvent,
) { ) {
// ignore events that don't apply to our window's destination property
if event.Window != request.requestor.Window().Id { return }
if event.Atom != request.destination { return }
// the only valid state that we can process a PropertyNotify event in // the only valid state that we can process a PropertyNotify event in
if request.state != selReqStateAwaitChunk { return } if request.state != selReqStateAwaitChunk { return }
if event.State != xproto.PropertyNewValue { return } if event.State != xproto.PropertyNewValue { return }