Added paste example

This commit is contained in:
Sasha Koshka 2023-06-07 00:54:57 -04:00
parent 926fa7ab27
commit 1e1367ce70
2 changed files with 75 additions and 3 deletions

74
examples/paste/main.go Normal file
View File

@ -0,0 +1,74 @@
// Example paste shows how to read text data from the CLIPBOARD selection.
package main
import "os"
import "io"
import "log"
import "github.com/jezek/xgbutil"
import "github.com/jezek/xgb/xproto"
import "github.com/jezek/xgbutil/xprop"
import "github.com/jezek/xgbutil/xevent"
import "github.com/jezek/xgbutil/xwindow"
import "git.tebibyte.media/sashakoshka/xgbsel"
type requestor struct {
window *xwindow.Window
}
func (requestor requestor) Window () *xwindow.Window {
return requestor.window
}
func (requestor requestor) Success (target xgbsel.Target, data io.ReadCloser) {
defer data.Close()
text, _ := io.ReadAll(data)
log.Println("Clipboard text:", string(text))
os.Exit(0)
}
func (requestor requestor) Failure (err error) {
log.Fatalln("could not get clipboard:", err)
os.Exit(1)
}
func (requestor requestor) Choose (from []xgbsel.Target) (xgbsel.Target, bool) {
for _, target := range from {
if target == "TEXT" {
return target, true
}
}
return "", false
}
func main () {
X, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
// create window
window, err := xwindow.Generate(X)
if err != nil {
log.Fatalln("could not generate a new window X id:", err)
}
window.Create(X.RootWin(), 0, 0, 500, 500, xproto.CwBackPixel, 0xffffffff)
// get the atom for the clipboard, and the name of the property we want
// to recieve the selection contents on (which can be anything)
log.Println("creating request")
clipboard, _ := xprop.Atm(X, "CLIPBOARD")
property, _ := xprop.Atm(X, "DESTINATION")
request := xgbsel.NewRequest (
requestor { window: window },
clipboard,
property)
// listen for events
window.Listen(xproto.EventMaskPropertyChange)
xevent.PropertyNotifyFun(request.HandlePropertyNotify).Connect(X, window.Id)
xevent.SelectionNotifyFun(request.HandleSelectionNotify).Connect(X, window.Id)
log.Println("running main event loop")
xevent.Main(X)
}

View File

@ -84,9 +84,7 @@ func (request *Request) convertSelection (target xproto.Atom, switchTo selReqSta
request.destination, request.destination,
// TODO: *possibly replace this zero with an actual timestamp // TODO: *possibly replace this zero with an actual timestamp
// received from the server. this is non-trivial as we cannot // received from the server. this is non-trivial as we cannot
// rely on the timestamp of the last received event, because // rely on the timestamp of the last received event.
// there is a possibility that this method is invoked
// asynchronously from within tomo.Do().
0).Check() 0).Check()
if err != nil { request.die(err); return } if err != nil { request.die(err); return }