BACKENDS: Add a default clipboard implementation

This commit is contained in:
Cameron Cawley 2019-09-19 22:20:08 +01:00 committed by Filippos Karapetis
parent bdd7b6baed
commit 014bef9eab
6 changed files with 34 additions and 37 deletions

View file

@ -457,18 +457,14 @@ Common::String OSystem_SDL::getSystemLanguage() const {
#endif // USE_DETECTLANG #endif // USE_DETECTLANG
} }
bool OSystem_SDL::hasTextInClipboard() {
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
bool OSystem_SDL::hasTextInClipboard() {
return SDL_HasClipboardText() == SDL_TRUE; return SDL_HasClipboardText() == SDL_TRUE;
#else
return false;
#endif
} }
Common::String OSystem_SDL::getTextFromClipboard() { Common::String OSystem_SDL::getTextFromClipboard() {
if (!hasTextInClipboard()) return ""; if (!hasTextInClipboard()) return "";
#if SDL_VERSION_ATLEAST(2, 0, 0)
char *text = SDL_GetClipboardText(); char *text = SDL_GetClipboardText();
// The string returned by SDL is in UTF-8. Convert to the // The string returned by SDL is in UTF-8. Convert to the
// current TranslationManager encoding or ISO-8859-1. // current TranslationManager encoding or ISO-8859-1.
@ -485,13 +481,9 @@ Common::String OSystem_SDL::getTextFromClipboard() {
SDL_free(text); SDL_free(text);
return strText; return strText;
#else
return "";
#endif
} }
bool OSystem_SDL::setTextInClipboard(const Common::String &text) { bool OSystem_SDL::setTextInClipboard(const Common::String &text) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
// The encoding we need to use is UTF-8. Assume we currently have the // The encoding we need to use is UTF-8. Assume we currently have the
// current TranslationManager encoding or ISO-8859-1. // current TranslationManager encoding or ISO-8859-1.
#ifdef USE_TRANSLATION #ifdef USE_TRANSLATION
@ -505,10 +497,8 @@ bool OSystem_SDL::setTextInClipboard(const Common::String &text) {
return status == 0; return status == 0;
} }
return SDL_SetClipboardText(text.c_str()) == 0; return SDL_SetClipboardText(text.c_str()) == 0;
#else
return false;
#endif
} }
#endif
uint32 OSystem_SDL::getMillis(bool skipRecord) { uint32 OSystem_SDL::getMillis(bool skipRecord) {
uint32 millis = SDL_GetTicks(); uint32 millis = SDL_GetTicks();

View file

@ -69,10 +69,12 @@ public:
virtual Common::String getSystemLanguage() const; virtual Common::String getSystemLanguage() const;
#if SDL_VERSION_ATLEAST(2, 0, 0)
// Clipboard // Clipboard
virtual bool hasTextInClipboard(); virtual bool hasTextInClipboard();
virtual Common::String getTextFromClipboard(); virtual Common::String getTextFromClipboard();
virtual bool setTextInClipboard(const Common::String &text); virtual bool setTextInClipboard(const Common::String &text);
#endif
virtual void setWindowCaption(const char *caption); virtual void setWindowCaption(const char *caption);
virtual void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0); virtual void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0);

View file

@ -216,6 +216,12 @@ protected:
*/ */
FilesystemFactory *_fsFactory; FilesystemFactory *_fsFactory;
/**
* Used by the default clipboard implementation, for backends that don't
* implement clipboard support.
*/
Common::String _clipboard;
private: private:
/** /**
* Indicate if initBackend() has been called. * Indicate if initBackend() has been called.
@ -374,8 +380,11 @@ public:
kFeatureDisplayLogFile, kFeatureDisplayLogFile,
/** /**
* The presence of this feature indicates whether the hasTextInClipboard(), * The presence of this feature indicates whether the system clipboard is
* getTextFromClipboard() and setTextInClipboard() calls are supported. * available. If this feature is not present, the hasTextInClipboard(),
* getTextFromClipboard() and setTextInClipboard() calls can still be used,
* however it should not be used in scenarios where the user is expected to
* copy data outside of the application.
* *
* This feature has no associated state. * This feature has no associated state.
*/ */
@ -1451,7 +1460,7 @@ public:
* *
* @return true if there is text in the clipboard, false otherwise * @return true if there is text in the clipboard, false otherwise
*/ */
virtual bool hasTextInClipboard() { return false; } virtual bool hasTextInClipboard() { return !_clipboard.empty(); }
/** /**
* Returns clipboard contents as a String. * Returns clipboard contents as a String.
@ -1462,7 +1471,7 @@ public:
* *
* @return clipboard contents ("" if hasTextInClipboard() == false) * @return clipboard contents ("" if hasTextInClipboard() == false)
*/ */
virtual Common::String getTextFromClipboard() { return ""; } virtual Common::String getTextFromClipboard() { return _clipboard; }
/** /**
* Set the content of the clipboard to the given string. * Set the content of the clipboard to the given string.
@ -1473,7 +1482,7 @@ public:
* *
* @return true if the text was properly set in the clipboard, false otherwise * @return true if the text was properly set in the clipboard, false otherwise
*/ */
virtual bool setTextInClipboard(const Common::String &text) { return false; } virtual bool setTextInClipboard(const Common::String &text) { _clipboard = text; return true; }
/** /**
* Open the given Url in the default browser (if available on the target * Open the given Url in the default browser (if available on the target

View file

@ -33,27 +33,23 @@ void Clipboard::clipboardStore(const Common::U32String &text) {
} }
void Clipboard::clipboardSend(ClipSource source) { void Clipboard::clipboardSend(ClipSource source) {
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) { // Convert unicode string to standard string, since that's all ScummVM supports
// Convert unicode string to standard string, since that's all ScummVM supports Common::String text;
Common::String text; for (uint idx = 0; idx < _text.size(); ++idx)
for (uint idx = 0; idx < _text.size(); ++idx) text += (_text[idx] <= 0x7f) ? (char)_text[idx] : '?';
text += (_text[idx] <= 0x7f) ? (char)_text[idx] : '?';
g_system->setTextInClipboard(text); g_system->setTextInClipboard(text);
}
} }
void Clipboard::clipboardReceive(ClipSource source) { void Clipboard::clipboardReceive(ClipSource source) {
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) { Windows &windows = *g_vm->_windows;
Windows &windows = *g_vm->_windows;
if (g_system->hasTextInClipboard()) { if (g_system->hasTextInClipboard()) {
Common::String text = g_system->getTextFromClipboard(); Common::String text = g_system->getTextFromClipboard();
for (uint idx = 0; idx < text.size(); ++idx) { for (uint idx = 0; idx < text.size(); ++idx) {
uint c = text[idx]; uint c = text[idx];
if (c != '\r' && c != '\n' && c != '\b' && c != '\t') if (c != '\r' && c != '\n' && c != '\b' && c != '\t')
windows.inputHandleKey(c); windows.inputHandleKey(c);
}
} }
} }
} }

View file

@ -523,7 +523,7 @@ void ConsoleDialog::specialKeys(Common::KeyCode keycode) {
g_gui.scheduleTopDialogRedraw(); g_gui.scheduleTopDialogRedraw();
break; break;
case Common::KEYCODE_v: case Common::KEYCODE_v:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport) && g_system->hasTextInClipboard()) { if (g_system->hasTextInClipboard()) {
Common::String text = g_system->getTextFromClipboard(); Common::String text = g_system->getTextFromClipboard();
insertIntoPrompt(text.c_str()); insertIntoPrompt(text.c_str());
scrollToCurrent(); scrollToCurrent();
@ -531,7 +531,7 @@ void ConsoleDialog::specialKeys(Common::KeyCode keycode) {
} }
break; break;
case Common::KEYCODE_c: case Common::KEYCODE_c:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) { {
Common::String userInput = getUserInput(); Common::String userInput = getUserInput();
if (!userInput.empty()) if (!userInput.empty())
g_system->setTextInClipboard(userInput); g_system->setTextInClipboard(userInput);

View file

@ -186,7 +186,7 @@ bool EditableWidget::handleKeyDown(Common::KeyState state) {
break; break;
case Common::KEYCODE_v: case Common::KEYCODE_v:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport) && state.flags & Common::KBD_CTRL) { if (state.flags & Common::KBD_CTRL) {
if (g_system->hasTextInClipboard()) { if (g_system->hasTextInClipboard()) {
String text = g_system->getTextFromClipboard(); String text = g_system->getTextFromClipboard();
for (uint32 i = 0; i < text.size(); ++i) { for (uint32 i = 0; i < text.size(); ++i) {
@ -201,7 +201,7 @@ bool EditableWidget::handleKeyDown(Common::KeyState state) {
break; break;
case Common::KEYCODE_c: case Common::KEYCODE_c:
if (g_system->hasFeature(OSystem::kFeatureClipboardSupport) && state.flags & Common::KBD_CTRL) { if (state.flags & Common::KBD_CTRL) {
if (!getEditString().empty()) if (!getEditString().empty())
g_system->setTextInClipboard(getEditString()); g_system->setTextInClipboard(getEditString());
} else { } else {