termux / termux-api

Termux add-on app which exposes device functionality as API to command line programs.
https://f-droid.org/en/packages/com.termux.api/
2.34k stars 459 forks source link

termux-clipboard-get does not work in termux-x11 #663

Closed numberokta closed 8 months ago

numberokta commented 8 months ago

Problem description

termux-clipboard-get works in proot-distro from temux cli but does not in terminal from termux-x11 Steps to reproduce

https://github.com/termux/termux-api/assets/20727200/a14d1855-18ea-4686-a28e-eaff7e93c02b

Expected behavior

it must work. Additional information

twaik commented 8 months ago

Not a bug. Android restricts background clipboard access.

numberokta commented 8 months ago

Not a bug. Android restricts background clipboard access.

So is there any way?

twaik commented 8 months ago

Currently there is no way.

twaik commented 8 months ago

You can swipe additional keyboard of termux-x11 and paste there. Better than nothing.

numberokta commented 8 months ago

I think there is some ways to do that, somethings like create an api for termux-x11 to run shell command directly through termux-x11

numberokta commented 8 months ago

It's better to use broadcast and implement a function like this

termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e list null

and use it like this

termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e exec "some code"

to execute shell code directly through termux-x11 in that environment that has already been logged in. If you agree with this, i think this issue must be moved to termux-x11 as enhancement.

twaik commented 8 months ago

Closing in prior to #452

numberokta commented 8 months ago

I think this way is worth to trying as termux-api and doesn't need to root or adb access.

twaik commented 8 months ago

452 issue topic is "Termux-clipboard-get does not work in background". Exactly about this issue, but a bit wider (because not limited only to termux-x11).

numberokta commented 8 months ago

I read all of them, and the solutions that you were talking about depend on adb or root access. I know this issue isn't limited to termux-x11, but it can be easily fixed on termux-x11 by implementing some java code to get clipboard text from android with broadcasting and also set clipboard text directly through termux-x11 with broadcasting without any api. I know that termux-x11 is xserver, but it's working on android, and it would be nice to communicate between android and linux clipboards directly.

twaik commented 8 months ago

It's better to use broadcast and implement a function like this

termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e list null

The problem is that in this case termux-clipboard-get will not be fixed, it will be other workaround. It will not be integrated to termux-api because termux plugins do not communicate this way.

If you need to get automatic way to get clipboard when Termux:X11 is in foreground I think I found a way. But implementing it will require some time.

numberokta commented 8 months ago

Yes, termux-clipboard-get will not be fixed and it will be a new feature for termux-x11 and not related to termux-api, actually i think there is no way to get clipboard with termux-api without adb or root access cause android restricted that. But in termux-x11, It's simple task, just manage (get, set) clipboard between android and termux directly through termux-x11 with broadcasting, something like this

termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e clipboard_get null
termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e clipboard_set "hello world"

My first idea was that to execute shell code directly through termux-x11 and use termux-clipboard-get this way. In this way we can use functionality that need us to be present in app to work otherwise android restricted that.

termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e exec "shell code"

But clipboard_get is also good and work

twaik commented 8 months ago

termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e clipboard_set "hello world"

That usecase is definitely out of scope of X server. AFAIK clipboard modification is not restricted, only retrieving current clipboard content. So in the case of termux-api it should work even in background.

termux-am broadcast --user 0 -a com.termux.x11.CHANGE_PREFERENCE -p com.termux.x11 -e clipboard_get null

The only way of clipboard access I can accept is mixing X11 and Android clipboard. Getting current clipboard content through broadcasts is out of scope of X server and will not be implemented. I will not repeat it again.** Mixing clipboard means to let X11 modify Android clipboard and let Android modify X11 clipboard (at least when Termux:X11 comes foreground). Currently it is not implemented because background clipboard access is restricted, so Android application can not receive notification (event) about clipboard change so termux-x11 can not decide which clipboard selection is newer and when it really should get Android's clipboard content and send it to X server. I've found some workaround and now working on it.

numberokta commented 8 months ago

Okay, do whatever you think is best. I gladly wait for it.