Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback to xclip as clipboard access method in TTY backend if far2l extensions are not available #810

Closed
unxed opened this issue Oct 20, 2020 · 8 comments

Comments

@unxed
Copy link
Contributor

unxed commented Oct 20, 2020

For running far2l --tty inside XTerm, for example.

@elfmz
Copy link
Owner

elfmz commented Oct 20, 2020

but why running inside xterm since may run in own UI framework?

@unxed
Copy link
Contributor Author

unxed commented Oct 21, 2020

but why running inside xterm since may run in own UI framework?

Why not? :) People have formed habits, their usual settings of their favorite terminals. Why force people to change their habbits? It also should be very easy to add this option — xclip just reads the buffer from the file or writes to it.

@loki1368
Copy link

If this helps the clipboard work when working with far2l in the konsole(KDE's Terminal Emulator) tab, it is very necessary

@unxed
Copy link
Contributor Author

unxed commented Oct 22, 2020

If this helps the clipboard work when working with far2l in the konsole(KDE's Terminal Emulator) tab, it is very necessary

It's only local far2l's clipboard and (limited) pasting from X11's one (via keyboard input emulation). No copy to X11 clipboard from tty far2l at all. Still to be implemented.

@unxed
Copy link
Contributor Author

unxed commented Oct 10, 2021

but why running inside xterm since may run in own UI framework?

There are still a number of possible reasons.

  1. Someone really loves his favourite terminal app, just as we love Far :)

  2. Someone wants to work with some unicode stuff like emojis, etc that may be supported by terminal better.

  3. Someone wants to have Alt+non_latin_letter quick search working (another option is to build with wx 3.1, but it's more hard for common user)

  4. Someone do not wants to install X11 on Mac just to use far2l (same with WSL/cygwin)

  5. Someone has no rights to install system-wide packages like wx, but can run far2l from his home folder

@unxed
Copy link
Contributor Author

unxed commented Oct 10, 2021

Proof-of-Concept implementation.

Known issue: pasting from tty far2l to wx far2l fails for some reason (pasting to other X apps works ok).

diff --git a/WinPort/src/Backend/FSClipboardBackend.cpp b/WinPort/src/Backend/FSClipboardBackend.cpp
index 16f5f18e..daa4e84b 100644
--- a/WinPort/src/Backend/FSClipboardBackend.cpp
+++ b/WinPort/src/Backend/FSClipboardBackend.cpp
@@ -48,6 +48,19 @@ bool FSClipboardBackend::OnClipboardIsFormatAvailable(UINT format)
 
 void *FSClipboardBackend::OnClipboardSetData(UINT format, void *data)
 {
+    if ((format == 13) && (!system("which xclip > /dev/null 2>&1"))) {
+        // text clipboard format, and xclip is available
+
+        std::string temp;
+        Wide2MB((wchar_t *)data, GetMallocSize(data), temp);
+
+        FILE* p = popen("xclip -i -selection clipboard", "w");
+        fwrite(temp.c_str(), 1, temp.length(), p);
+        pclose(p);
+
+        return data;
+    }
+
 	if (!_kfh)
 		return nullptr;
 
@@ -64,6 +77,40 @@ void *FSClipboardBackend::OnClipboardSetData(UINT format, void *data)
 
 void *FSClipboardBackend::OnClipboardGetData(UINT format)
 {
+    if ((format == 13) && (!system("which xclip > /dev/null 2>&1"))) {
+        // text clipboard format, and xclip is available
+
+        std::vector<char> buff;
+        const std::size_t delta = 1024;
+        std::size_t cursor = 0;
+        buff.resize(delta);
+
+        std::size_t n;
+
+        FILE *cmd = popen("xclip -o -selection clipboard", "r");
+
+        while ((n = fread(buff.data()+cursor, 1, buff.size()-cursor, cmd)) > 0) {
+            cursor += n;
+            if (cursor == buff.size()){
+                if (buff.capacity() == buff.size()){
+                    buff.reserve(buff.size() + delta);
+                }
+                buff.resize(buff.capacity());
+            }
+        }
+        buff[cursor] = '\0';
+        buff.resize(cursor+1);
+
+        std::wstring temp;
+        MB2Wide(buff.data(), (int)buff.size() - 1, temp);
+        int out_sz = wcslen(&temp[0]) * sizeof(wchar_t);
+        void *out = malloc(out_sz + sizeof(wchar_t));
+        memcpy(out, &temp[0], out_sz);
+        memset(out + out_sz, 0, sizeof(wchar_t));
+
+    	return out;
+    }
+
 	if (!_kfh)
 		return nullptr;
 

@unxed
Copy link
Contributor Author

unxed commented Oct 26, 2021

elfmz added a commit that referenced this issue Dec 1, 2021
TTY clipboard improvements (touch #810)
@unxed
Copy link
Contributor Author

unxed commented Dec 1, 2021

Проверил. Работает, красота! Спасибо! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants