2023-03-29 21:24:42 -06:00
|
|
|
package x
|
|
|
|
|
2023-03-30 11:10:58 -06:00
|
|
|
import "io"
|
|
|
|
import "github.com/jezek/xgb"
|
|
|
|
import "github.com/jezek/xgbutil"
|
|
|
|
import "github.com/jezek/xgb/xproto"
|
|
|
|
import "github.com/jezek/xgbutil/xprop"
|
|
|
|
import "github.com/jezek/xgbutil/xevent"
|
2023-03-29 21:24:42 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/data"
|
|
|
|
|
|
|
|
type selectionClaim struct {
|
2023-03-30 11:10:58 -06:00
|
|
|
window *window
|
2023-03-29 21:24:42 -06:00
|
|
|
data data.Data
|
2023-03-30 11:10:58 -06:00
|
|
|
name xproto.Atom
|
2023-03-29 21:24:42 -06:00
|
|
|
}
|
|
|
|
|
2023-03-30 11:10:58 -06:00
|
|
|
func (window *window) claimSelection (name xproto.Atom, data data.Data) *selectionClaim {
|
|
|
|
// Follow:
|
|
|
|
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.1
|
2023-03-30 16:05:29 -06:00
|
|
|
|
2023-03-30 11:10:58 -06:00
|
|
|
// A client wishing to acquire ownership of a particular selection
|
|
|
|
// should call SetSelectionOwner. The client should set the specified
|
|
|
|
// selection to the atom that represents the selection, set the
|
|
|
|
// specified owner to some window that the client created, and set the
|
|
|
|
// specified time to some time between the current last-change time of
|
|
|
|
// the selection concerned and the current server time. This time value
|
|
|
|
// usually will be obtained from the timestamp of the event that
|
|
|
|
// triggers the acquisition of the selection. Clients should not set the
|
|
|
|
// time value to CurrentTime, because if they do so, they have no way of
|
|
|
|
// finding when they gained ownership of the selection. Clients must use
|
|
|
|
// a window they created so that requestors can route events to the
|
|
|
|
// owner of the selection.
|
|
|
|
err := xproto.SetSelectionOwnerChecked (
|
|
|
|
window.backend.connection.Conn(),
|
|
|
|
window.xWindow.Id, name, 0).Check() // FIXME: should not be zero
|
|
|
|
if err != nil { return nil }
|
|
|
|
|
2023-03-30 16:05:29 -06:00
|
|
|
ownerReply, err := xproto.GetSelectionOwner (
|
|
|
|
window.backend.connection.Conn(), name).Reply()
|
|
|
|
if err != nil { return nil }
|
2023-03-30 18:51:11 -06:00
|
|
|
if ownerReply.Owner != window.xWindow.Id { return nil }
|
2023-03-30 16:05:29 -06:00
|
|
|
|
2023-03-30 11:10:58 -06:00
|
|
|
return &selectionClaim {
|
|
|
|
window: window,
|
2023-03-29 21:24:42 -06:00
|
|
|
data: data,
|
2023-03-30 11:10:58 -06:00
|
|
|
name: name,
|
2023-03-29 21:24:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:10:58 -06:00
|
|
|
func (window *window) refuseSelectionRequest (request xevent.SelectionRequestEvent) {
|
|
|
|
// ... refuse the SelectionRequest by sending the requestor window a
|
|
|
|
// SelectionNotify event with the property set to None (by means of a
|
|
|
|
// SendEvent request with an empty event mask).
|
|
|
|
event := xproto.SelectionNotifyEvent {
|
|
|
|
Requestor: request.Requestor,
|
|
|
|
Selection: request.Selection,
|
|
|
|
Target: request.Target,
|
|
|
|
Property: 0,
|
|
|
|
}.Bytes()
|
|
|
|
xproto.SendEvent (
|
|
|
|
window.backend.connection.Conn(),
|
|
|
|
false, request.Requestor, 0, string(event))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *window) fulfillSelectionRequest (
|
2023-03-30 18:51:11 -06:00
|
|
|
data []byte,
|
|
|
|
format byte,
|
|
|
|
ty xproto.Atom,
|
2023-03-30 11:10:58 -06:00
|
|
|
request xevent.SelectionRequestEvent,
|
|
|
|
) {
|
|
|
|
die := func () { window.refuseSelectionRequest(request) }
|
|
|
|
|
|
|
|
// If the specified property is not None, the owner should place the
|
|
|
|
// data resulting from converting the selection into the specified
|
|
|
|
// property on the requestor window and should set the property's type
|
|
|
|
// to some appropriate value, which need not be the same as the
|
|
|
|
// specified target.
|
|
|
|
err := xproto.ChangePropertyChecked (
|
|
|
|
window.backend.connection.Conn(),
|
2023-03-30 16:52:29 -06:00
|
|
|
xproto.PropModeReplace, request.Requestor,
|
2023-03-30 11:10:58 -06:00
|
|
|
request.Property,
|
2023-03-30 18:51:11 -06:00
|
|
|
ty, format,
|
2023-03-30 11:10:58 -06:00
|
|
|
uint32(len(data) / (int(format) / 8)), data).Check()
|
|
|
|
if err != nil { die() }
|
|
|
|
|
|
|
|
// If the property is successfully stored, the owner should acknowledge
|
|
|
|
// the successful conversion by sending the requestor window a
|
|
|
|
// SelectionNotify event (by means of a SendEvent request with an empty
|
|
|
|
// mask).
|
|
|
|
event := xproto.SelectionNotifyEvent {
|
|
|
|
Requestor: request.Requestor,
|
|
|
|
Selection: request.Selection,
|
|
|
|
Target: request.Target,
|
|
|
|
Property: request.Property,
|
|
|
|
}.Bytes()
|
|
|
|
xproto.SendEvent (
|
|
|
|
window.backend.connection.Conn(),
|
|
|
|
false, request.Requestor, 0, string(event))
|
2023-03-29 21:24:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (claim *selectionClaim) handleSelectionRequest (
|
2023-03-30 11:10:58 -06:00
|
|
|
connection *xgbutil.XUtil,
|
|
|
|
event xevent.SelectionRequestEvent,
|
2023-03-29 21:24:42 -06:00
|
|
|
) {
|
2023-03-30 11:10:58 -06:00
|
|
|
// Follow:
|
|
|
|
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.2
|
|
|
|
|
|
|
|
die := func () { claim.window.refuseSelectionRequest(event) }
|
|
|
|
|
|
|
|
// When a requestor wants the value of a selection, the owner receives a
|
|
|
|
// SelectionRequest event. The specified owner and selection will be the
|
|
|
|
// values that were specified in the SetSelectionOwner request. The
|
|
|
|
// owner should compare the timestamp with the period it has owned the
|
|
|
|
// selection and, if the time is outside, refuse the SelectionRequest.
|
|
|
|
if event.Selection != claim.name { die(); return }
|
|
|
|
|
|
|
|
// If the specified property is None , the requestor is an obsolete
|
|
|
|
// client. Owners are encouraged to support these clients by using the
|
|
|
|
// specified target atom as the property name to be used for the reply.
|
|
|
|
if event.Property == 0 {
|
|
|
|
event.Property = event.Target
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, the owner should use the target to decide the form into
|
|
|
|
// which the selection should be converted. Some targets may be defined
|
|
|
|
// such that requestors can pass parameters along with the request. The
|
|
|
|
// owner will find these parameters in the property named in the
|
|
|
|
// selection request. The type, format, and contents of this property
|
|
|
|
// are dependent upon the definition of the target. If the target is not
|
|
|
|
// defined to have parameters, the owner should ignore the property if
|
|
|
|
// it is present. If the selection cannot be converted into a form based
|
|
|
|
// on the target (and parameters, if any), the owner should refuse the
|
|
|
|
// SelectionRequest as previously described.
|
|
|
|
targetName, err := xprop.AtomName (
|
|
|
|
claim.window.backend.connection, event.Target)
|
|
|
|
if err != nil { die(); return }
|
|
|
|
|
|
|
|
switch targetName {
|
|
|
|
case "TARGETS":
|
2023-03-30 18:51:11 -06:00
|
|
|
targetNames := []string { "TARGETS", }
|
2023-03-30 11:10:58 -06:00
|
|
|
for mime := range claim.data {
|
|
|
|
targetNames = append(targetNames, mimeToTargets(mime)...)
|
|
|
|
}
|
|
|
|
data := make([]byte, len(targetNames) * 4)
|
|
|
|
for index, name := range targetNames {
|
|
|
|
atom, err := xprop.Atm(claim.window.backend.connection, name)
|
|
|
|
if err != nil { die(); return }
|
2023-03-30 16:32:14 -06:00
|
|
|
xgb.Put32(data[(index) * 4:], uint32(atom))
|
2023-03-30 11:10:58 -06:00
|
|
|
}
|
2023-03-30 18:51:11 -06:00
|
|
|
atomAtom, err := xprop.Atm(claim.window.backend.connection, "ATOM")
|
|
|
|
if err != nil { die(); return }
|
|
|
|
claim.window.fulfillSelectionRequest(data, 32, atomAtom, event)
|
2023-03-30 11:10:58 -06:00
|
|
|
|
|
|
|
default:
|
|
|
|
mime, confidence := targetToMime(targetName)
|
|
|
|
if confidence == confidenceNone { die(); return }
|
|
|
|
reader, ok := claim.data[mime]
|
|
|
|
if !ok { die(); return }
|
2023-03-30 16:42:40 -06:00
|
|
|
reader.Seek(0, io.SeekStart)
|
2023-03-30 11:10:58 -06:00
|
|
|
data, err := io.ReadAll(reader)
|
|
|
|
if err != nil { die() }
|
2023-03-30 18:51:11 -06:00
|
|
|
claim.window.fulfillSelectionRequest(data, 8, event.Target, event)
|
2023-03-30 11:10:58 -06:00
|
|
|
}
|
2023-03-29 21:24:42 -06:00
|
|
|
}
|