-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
external clipboard feature (--clipboard command line or ~/.config/far…
…2l/clipboard script)
- Loading branch information
Showing
10 changed files
with
217 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include "ExtClipboardBackend.h" | ||
#include "WinPort.h" | ||
#include <utils.h> | ||
#include <stdio.h> | ||
#include <UtfConvert.hpp> | ||
|
||
ExtClipboardBackend::ExtClipboardBackend(const char *exec) | ||
: | ||
_exec(exec) | ||
{ | ||
QuoteCmdArgIfNeed(_exec); | ||
} | ||
|
||
|
||
ExtClipboardBackend::~ExtClipboardBackend() | ||
{ | ||
} | ||
|
||
bool ExtClipboardBackend::RunSimpleCommand(const char *arg) | ||
{ | ||
std::string cmd = _exec; | ||
cmd+= ' '; | ||
cmd+= arg; | ||
|
||
std::string result; | ||
POpen(result, cmd.c_str()); | ||
return atoi(result.c_str()) > 0; | ||
} | ||
|
||
bool ExtClipboardBackend::OnClipboardOpen() | ||
{ | ||
return true; | ||
} | ||
|
||
void ExtClipboardBackend::OnClipboardClose() | ||
{ | ||
} | ||
|
||
void ExtClipboardBackend::OnClipboardEmpty() | ||
{ | ||
RunSimpleCommand("empty"); | ||
} | ||
|
||
bool ExtClipboardBackend::OnClipboardIsFormatAvailable(UINT format) | ||
{ | ||
return format == CF_UNICODETEXT || format == CF_TEXT; | ||
} | ||
|
||
void *ExtClipboardBackend::OnClipboardSetData(UINT format, void *data) | ||
{ | ||
if (format != CF_UNICODETEXT && format != CF_TEXT) | ||
return data; | ||
|
||
std::string cmd = _exec; | ||
cmd+= " set"; | ||
|
||
FILE *f = popen(cmd.c_str(), "w"); | ||
if (!f) { | ||
return data; | ||
} | ||
|
||
const size_t len = WINPORT(ClipboardSize)(data); | ||
|
||
if (format == CF_UNICODETEXT) { | ||
UtfConverter<wchar_t, unsigned char> cvt((const wchar_t *)data, len / sizeof(wchar_t)); | ||
fwrite(cvt.data(), 1, cvt.size(), f); | ||
} else { | ||
fwrite(data, 1, len, f); | ||
} | ||
pclose(f); | ||
return data; | ||
} | ||
|
||
void *ExtClipboardBackend::OnClipboardGetData(UINT format) | ||
{ | ||
if (format != CF_UNICODETEXT && format != CF_TEXT) | ||
return nullptr; | ||
|
||
std::string cmd = _exec; | ||
cmd+= " get"; | ||
|
||
FILE *f = popen(cmd.c_str(), "r"); | ||
if (!f) { | ||
return nullptr; | ||
} | ||
|
||
std::vector<unsigned char> data; | ||
unsigned char buf[0x800]; | ||
for (;;) { | ||
size_t n = fread(buf, 1, sizeof(buf), f); | ||
if (n) { | ||
data.resize(data.size() + n); | ||
memcpy(data.data() + data.size() - n, buf, n); | ||
} | ||
if (n < sizeof(buf)) break; | ||
} | ||
|
||
void *out; | ||
if (format == CF_UNICODETEXT) { | ||
size_t utf8_len = data.size(); | ||
while (utf8_len && !data[utf8_len - 1]) { | ||
--utf8_len; | ||
} | ||
|
||
std::wstring ws; | ||
MB2Wide((const char *)data.data(), utf8_len, ws); | ||
|
||
out = WINPORT(ClipboardAlloc)( (ws.size() + 1) * sizeof(wchar_t)); | ||
if (out) { | ||
memcpy(out, ws.c_str(), (ws.size() + 1) * sizeof(wchar_t)); | ||
} | ||
|
||
} else { | ||
out = WINPORT(ClipboardAlloc)(data.empty() ? 1 : data.size()); | ||
if (out && !data.empty()) { | ||
memcpy(out, data.data(), data.size()); | ||
} | ||
} | ||
|
||
return out; | ||
} | ||
|
||
UINT ExtClipboardBackend::OnClipboardRegisterFormat(const wchar_t *lpszFormat) | ||
{ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
#include "WinCompat.h" | ||
#include "Backend.h" | ||
|
||
|
||
class ExtClipboardBackend : public IClipboardBackend | ||
{ | ||
std::string _exec; | ||
|
||
bool RunSimpleCommand(const char *arg); | ||
|
||
public: | ||
ExtClipboardBackend(const char *exec); | ||
virtual ~ExtClipboardBackend(); | ||
|
||
virtual bool OnClipboardOpen(); | ||
virtual void OnClipboardClose(); | ||
virtual void OnClipboardEmpty(); | ||
virtual bool OnClipboardIsFormatAvailable(UINT format); | ||
virtual void *OnClipboardSetData(UINT format, void *data); | ||
virtual void *OnClipboardGetData(UINT format); | ||
virtual UINT OnClipboardRegisterFormat(const wchar_t *lpszFormat); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.