Skip to content

Commit 896f847

Browse files
committed
xrCore/os_clipboard.cpp: use SDL2 clipboard functions
1 parent ebba48b commit 896f847

File tree

2 files changed

+55
-58
lines changed

2 files changed

+55
-58
lines changed

src/xrCore/os_clipboard.cpp

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,92 @@
33
// Created : 21.02.2008
44
// Author : Evgeniy Sokolov
55
// Description : os clipboard class implementation
6+
//
7+
// Modified : 24.07.2018
8+
// Modified by : Xottab_DUTY
69
////////////////////////////////////////////////////////////////////////////
710

811
#include "stdafx.h"
912
#pragma hdrstop
13+
#include <SDL.h>
1014
#include "os_clipboard.h"
1115
#include "xrCore/_std_extensions.h"
1216

13-
void os_clipboard::copy_to_clipboard(LPCSTR buf)
17+
void os_clipboard::copy_to_clipboard(pcstr buf)
1418
{
15-
#if defined(WINDOWS)
16-
if (!OpenClipboard(0))
17-
return;
18-
u32 handle_size = (xr_strlen(buf) + 1) * sizeof(char);
19-
HGLOBAL handle = GlobalAlloc(GHND, handle_size);
20-
if (!handle)
19+
if (SDL_SetClipboardText(buf) < 0)
2120
{
22-
CloseClipboard();
23-
return;
21+
Msg("! Failed to copy text to the clipboard: %s", SDL_GetError());
22+
Log(buf);
2423
}
25-
26-
char* memory = (char*)GlobalLock(handle);
27-
xr_strcpy(memory, handle_size, buf);
28-
GlobalUnlock(handle);
29-
EmptyClipboard();
30-
SetClipboardData(CF_TEXT, handle);
31-
CloseClipboard();
32-
#endif
3324
}
3425

35-
void os_clipboard::paste_from_clipboard(LPSTR buffer, u32 const& buffer_size)
26+
void os_clipboard::paste_from_clipboard(pstr buffer, size_t buffer_size)
3627
{
3728
VERIFY(buffer);
3829
VERIFY(buffer_size > 0);
39-
#if defined(WINDOWS)
40-
if (!OpenClipboard(0))
30+
31+
if (!SDL_HasClipboardText())
4132
return;
4233

43-
HGLOBAL hmem = GetClipboardData(CF_TEXT);
44-
if (!hmem)
34+
char* clipData = SDL_GetClipboardText();
35+
36+
if (!clipData)
37+
{
38+
Msg("! Failed to paste text from the clipboard: %s", SDL_GetError());
4539
return;
40+
}
41+
42+
strncpy_s(buffer, buffer_size, clipData, buffer_size - 1);
4643

47-
LPCSTR clipdata = (LPCSTR)GlobalLock(hmem);
48-
strncpy_s(buffer, buffer_size, clipdata, buffer_size - 1);
49-
buffer[buffer_size - 1] = 0;
50-
for (u32 i = 0; i < strlen(buffer); ++i)
44+
for (size_t i = 0; i < xr_strlen(buffer); ++i)
5145
{
52-
char c = buffer[i];
53-
if (((isprint(c) == 0) && (c != char(-1))) || c == '\t' || c == '\n') // "я" = -1
46+
const char c = buffer[i];
47+
if (isprint(c) == 0 && c != char(-1) || c == '\t' || c == '\n') // "я" = -1
5448
{
5549
buffer[i] = ' ';
5650
}
5751
}
5852

59-
GlobalUnlock(hmem);
60-
CloseClipboard();
61-
#endif
53+
SDL_free(clipData);
6254
}
6355

64-
void os_clipboard::update_clipboard(LPCSTR string)
56+
void os_clipboard::update_clipboard(pcstr string)
6557
{
66-
#if defined(WINDOWS)
67-
if (!OpenClipboard(0))
58+
if (!string)
59+
{
60+
Log("! Why are you trying to copy nullptr to the clipboard?!");
6861
return;
62+
}
6963

70-
HGLOBAL handle = GetClipboardData(CF_TEXT);
71-
if (!handle)
64+
if (!SDL_HasClipboardText())
7265
{
73-
CloseClipboard();
7466
copy_to_clipboard(string);
7567
return;
7668
}
7769

78-
LPSTR memory = (LPSTR)GlobalLock(handle);
79-
int memory_length = (int)strlen(memory);
80-
int string_length = (int)strlen(string);
81-
int buffer_size = (memory_length + string_length + 1) * sizeof(char);
82-
#ifndef _EDITOR
83-
LPSTR buffer = (LPSTR)_alloca(buffer_size);
84-
#else // #ifndef _EDITOR
85-
LPSTR buffer = (LPSTR)xr_alloc<char>(buffer_size);
86-
#endif // #ifndef _EDITOR
87-
xr_strcpy(buffer, buffer_size, memory);
88-
GlobalUnlock(handle);
89-
90-
xr_strcat(buffer, buffer_size, string);
91-
CloseClipboard();
70+
char* clipData = SDL_GetClipboardText();
71+
72+
if (!clipData)
73+
{
74+
DEBUG_BREAK;
75+
Msg("! Failed to get text from the clipboard: %s", SDL_GetError());
76+
Log("! Falling back to copy_to_clipboard()");
77+
copy_to_clipboard(string);
78+
return;
79+
}
80+
81+
const size_t clipLength = xr_strlen(clipData);
82+
const size_t stringLength = xr_strlen(string);
83+
84+
const size_t bufferSize = (clipLength + stringLength + 1) * sizeof(char);
85+
86+
pstr buffer = (pstr)_alloca(bufferSize);
87+
88+
xr_strcpy(buffer, bufferSize, clipData); // copy the clipboard
89+
xr_strcat(buffer, bufferSize, string); // copy the new string
90+
91+
SDL_free(clipData);
92+
9293
copy_to_clipboard(buffer);
93-
#ifdef _EDITOR
94-
xr_free(buffer);
95-
#endif // #ifdef _EDITOR
96-
#endif
9794
}

src/xrCore/os_clipboard.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace os_clipboard
1313
{
14-
XRCORE_API void copy_to_clipboard(LPCSTR buf);
15-
XRCORE_API void paste_from_clipboard(LPSTR buf, u32 const& buf_size);
16-
XRCORE_API void update_clipboard(LPCSTR str);
14+
XRCORE_API void copy_to_clipboard(pcstr buf);
15+
XRCORE_API void paste_from_clipboard(pstr buf, size_t buf_size);
16+
XRCORE_API void update_clipboard(pcstr str);
1717
} // namespace os_clipboard
1818

1919
#endif // OS_CLIPBOARD_H_INCLUDED

0 commit comments

Comments
 (0)