Giter VIP home page Giter VIP logo

Comments (10)

HuakunShen avatar HuakunShen commented on September 4, 2024

It seems like doing this makes image writing works on Linux Ubuntu, even without triggering changed. Why does this happen?

And this will block the program.

changed := clipboard.Write(clipboard.FmtImage, sampleImgBuf)
select {
case <-changed:
    println(`Image Updated`)
}

from clipboard.

changkun avatar changkun commented on September 4, 2024

On Linux, you must keep the application running so that others can read your data. See #15 for another discussion. Closing because this is not a bug, and feel free to reopen the issue if this is actually a bug.

from clipboard.

HuakunShen avatar HuakunShen commented on September 4, 2024

@changkun Thanks, I kind of noticed that the program has to be running. I did a work-around with xclip.
Is it the same on Windows? I also can't write image on Windows.

from clipboard.

changkun avatar changkun commented on September 4, 2024

That's not a requirement on Windows. Is the problem reproducible on latest release?

from clipboard.

HuakunShen avatar HuakunShen commented on September 4, 2024

@changkun Yes I tried to write an image on Windows on the latest release and it doesn't work.

Here is my sample code.

package main

import (
	"encoding/base64"
	"golang.design/x/clipboard"
	"log"
)

func main() {
	if err := clipboard.Init(); err != nil {
		log.Fatal(err)
	}
	base64Str := "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDumIVSx6AZrD0LxPba9d3sFujD7K+xie5rVvpPJsJ5P7qE/pXn3woXdY6rfdWluWNcEYpxbOlvVI9JormvDPioa/eahbGHy3tJNh966WpaadmUnfYy/ESzP4fvVgUtK0TBQOpOK8s8Kaj4q8P6MbC38PPJlifMbjOa9npMD0FXGdlaxLjd3ueFeDr/AMTReJtRjsrSPzpJd1yr/wAHPavc4S5hQyDD4G761mWXh6xsNWudSgQrPcffPY1rUVJqT0CEWkf/2Q=="
	imgBuf, err := base64.StdEncoding.DecodeString(base64Str)
	if err != nil {
		log.Fatal(err)
	}
	clipboard.Write(clipboard.FmtImage, imgBuf)
}

from clipboard.

HuakunShen avatar HuakunShen commented on September 4, 2024

And if I use select to monitor changed, there is a deadlock.

changed := clipboard.Write(clipboard.FmtImage, imgBuf)
select {
case <-changed:
    println(`"text data" is no longer available from clipboard.`)
}
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive (nil chan)]:
main.main()
	C:/Users/username/Desktop/clipboard-app/main.go:20 +0xce

from clipboard.

changkun avatar changkun commented on September 4, 2024

Is your image png encoded?

from clipboard.

HuakunShen avatar HuakunShen commented on September 4, 2024

@changkun Yes, if you copy the base64 string to https://onlinepngtools.com/convert-base64-to-png
The image can be rendered.

from clipboard.

HuakunShen avatar HuakunShen commented on September 4, 2024

Wait, it seems like it's not png encoded even if it can be displayed.
I tried to take a screenshot and read it to string with go clipboard, then write it back. It works!

So does this mean other type of image encoding are not supported?
I will experiment a few more things, thanks!

from clipboard.

changkun avatar changkun commented on September 4, 2024

As documented in the API, the image buffer must be png encoded: https://pkg.go.dev/golang.design/x/clipboard#Write

It is recommended that you encode it using image/png package to encode the bytes buffer

There are underlying implementation reasons, but it is not important for users who are not interested in the details.

from clipboard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.