Compare commits
No commits in common. "534a8fd0ad0e43bc6aaa019e83eb94c14b3df765" and "f4fadc0d3b03f7db3b125f5cee3565de1b99461d" have entirely different histories.
534a8fd0ad
...
f4fadc0d3b
143
v2/claim.go
143
v2/claim.go
@ -9,39 +9,25 @@ 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 format8bit byte = 8
|
||||||
const format16bit byte = 16
|
const format16bit byte = 16
|
||||||
const format32bit byte = 32
|
const format32bit byte = 32
|
||||||
const propertyNewValue byte = 0
|
|
||||||
const propertyDelete byte = 1
|
|
||||||
|
|
||||||
type claimRequest struct {
|
type claimRequest struct {
|
||||||
reader io.ReadSeekCloser
|
reader io.ReadSeekCloser
|
||||||
ty xproto.Atom
|
ty xproto.Atom // must be "INCR"
|
||||||
event xevent.SelectionRequestEvent
|
event xevent.SelectionRequestEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the request's reader and does other cleanup. It does not remove
|
|
||||||
// the request from the claim.
|
|
||||||
func (this *claimRequest) Close () error {
|
|
||||||
return this.reader.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
timestamp xproto.Timestamp
|
||||||
|
open bool
|
||||||
|
|
||||||
requests map[claimRequestKey] *claimRequest
|
requests map[xproto.Atom] claimRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClaim claims ownership of a specified selection, and allows using data
|
// NewClaim claims ownership of a specified selection, and allows using data
|
||||||
@ -79,12 +65,11 @@ func NewClaim (window *xwindow.Window, selection xproto.Atom, data Data, timesta
|
|||||||
|
|
||||||
return &Claim {
|
return &Claim {
|
||||||
open: true,
|
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,
|
timestamp: timestamp,
|
||||||
requests: make(map[claimRequestKey] *claimRequest),
|
requests: make(map[xproto.Atom] claimRequest),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,10 +101,12 @@ 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 := claim.changePropertyChecked (
|
err := xproto.ChangePropertyChecked (
|
||||||
|
claim.window.X.Conn(),
|
||||||
xproto.PropModeReplace, request.Requestor,
|
xproto.PropModeReplace, request.Requestor,
|
||||||
request.Property,
|
request.Property,
|
||||||
ty, format, data)
|
ty, format,
|
||||||
|
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
|
||||||
@ -137,19 +124,6 @@ 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.
|
||||||
@ -211,52 +185,14 @@ func (claim *Claim) HandleSelectionRequest (
|
|||||||
claim.fulfillSelectionRequest(data, format32bit, 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)
|
||||||
if claim.incr {
|
data, err := io.ReadAll(reader)
|
||||||
// TODO: we need to subscribe to PropertyNotify events
|
reader.Close()
|
||||||
// on the destination window. apparently event masks as
|
if err != nil { die() }
|
||||||
// set by ChangeWindowAttributes are client-specific.
|
claim.fulfillSelectionRequest(data, format8bit, event.Target, event)
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,28 +203,21 @@ func (claim *Claim) HandlePropertyNotify (
|
|||||||
connection *xgbutil.XUtil,
|
connection *xgbutil.XUtil,
|
||||||
event xevent.PropertyNotifyEvent,
|
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:
|
// The selection owner then:
|
||||||
// ... Waits between each append for a PropertyNotify (state==Deleted)
|
// ... Waits between each append for a PropertyNotify (state==Deleted)
|
||||||
// event that shows that the requestor has read the data. The reason for
|
// 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.
|
// doing this is to limit the consumption of space in the server.
|
||||||
if event.State != propertyDelete { return }
|
|
||||||
|
|
||||||
|
// FIXME check for state==Deleted
|
||||||
// if the request does not exist, the property was either modified in
|
// if the request does not exist, the property was either modified in
|
||||||
// error or the request has already finished. either way, we simply
|
// error or the request has already finished. either way, we simply
|
||||||
// exit.
|
// exit.
|
||||||
key := claimRequestKey {
|
request, ok := claim.requests[event.Atom]
|
||||||
property: event.Atom,
|
|
||||||
requestor: event.Window,
|
|
||||||
}
|
|
||||||
request, ok := claim.requests[key]
|
|
||||||
if !ok { return }
|
if !ok { return }
|
||||||
|
|
||||||
done := func () {
|
done := func () {
|
||||||
request.Close()
|
request.reader.Close()
|
||||||
delete(claim.requests, key)
|
delete(claim.requests, request.event.Property)
|
||||||
}
|
}
|
||||||
|
|
||||||
die := func () {
|
die := func () {
|
||||||
@ -309,10 +238,13 @@ func (claim *Claim) HandlePropertyNotify (
|
|||||||
// the same window as the selection reply with a type corresponding to
|
// the same window as the selection reply with a type corresponding to
|
||||||
// the actual type of the converted selection. The size should be less
|
// the actual type of the converted selection. The size should be less
|
||||||
// than the maximum-request-size in the connection handshake.
|
// than the maximum-request-size in the connection handshake.
|
||||||
err := claim.changePropertyChecked (
|
format := format8bit
|
||||||
xproto.PropModeReplace,
|
err = xproto.ChangePropertyChecked (
|
||||||
request.event.Requestor, request.event.Property,
|
claim.window.X.Conn(),
|
||||||
request.ty, format8bit, dataRead)
|
xproto.PropModeAppend, request.event.Requestor,
|
||||||
|
request.event.Property,
|
||||||
|
request.ty, format,
|
||||||
|
uint32(len(dataRead) / (int(format) / 8)), dataRead).Check()
|
||||||
if err != nil { die(); return }
|
if err != nil { die(); return }
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -322,10 +254,14 @@ func (claim *Claim) HandlePropertyNotify (
|
|||||||
// server) until a PropertyNotify (state==Deleted) event that
|
// server) until a PropertyNotify (state==Deleted) event that
|
||||||
// shows that the data has been read by the requestor and then
|
// shows that the data has been read by the requestor and then
|
||||||
// writes zero-length data to the property.
|
// writes zero-length data to the property.
|
||||||
err := claim.changePropertyChecked (
|
|
||||||
xproto.PropModeReplace,
|
format := format8bit
|
||||||
request.event.Requestor, request.event.Property,
|
err := xproto.ChangePropertyChecked (
|
||||||
request.ty, format8bit, nil)
|
claim.window.X.Conn(),
|
||||||
|
xproto.PropModeAppend, request.event.Requestor,
|
||||||
|
request.event.Property,
|
||||||
|
request.ty, format,
|
||||||
|
0, nil).Check()
|
||||||
if err != nil { die(); return }
|
if err != nil { die(); return }
|
||||||
|
|
||||||
done()
|
done()
|
||||||
@ -350,15 +286,6 @@ func (claim *Claim) HandleSelectionClear (
|
|||||||
claim.open = false
|
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
|
// Open returns false if the claim has been relinquished, either from recieving
|
||||||
// a SelectionClear event or with the Close method.
|
// a SelectionClear event or with the Close method.
|
||||||
func (claim *Claim) Open () bool {
|
func (claim *Claim) Open () bool {
|
||||||
|
@ -255,10 +255,6 @@ 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 }
|
||||||
|
Loading…
Reference in New Issue
Block a user