-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathDFSDL.cpp
More file actions
328 lines (277 loc) · 10.4 KB
/
DFSDL.cpp
File metadata and controls
328 lines (277 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "Error.h"
#include "Internal.h"
#include "modules/DFSDL.h"
#include "Debug.h"
#include "PluginManager.h"
#include <SDL_stdinc.h>
#include <SDL_keycode.h>
#include <vector>
#ifdef WIN32
# include <regex>
#endif
namespace DFHack {
DBG_DECLARE(core, dfsdl, DebugCategory::LINFO);
}
using namespace DFHack;
using std::string;
using std::vector;
static DFLibrary *g_sdl_handle = nullptr;
static DFLibrary *g_sdl_image_handle = nullptr;
static const vector<string> SDL_LIBS {
#ifdef WIN32
"SDL2.dll"
#elif defined(_DARWIN)
"SDL.framework/Versions/A/SDL",
"SDL.framework/SDL"
#else
"libSDL2-2.0.so.0"
#endif
};
static const vector<string> SDL_IMAGE_LIBS {
#ifdef WIN32
"SDL2_image.dll"
#elif defined(_DARWIN)
"SDL_image.framework/Versions/A/SDL_image",
"SDL_image.framework/SDL_image"
#else
"libSDL2_image-2.0.so.0"
#endif
};
SDL_Surface * (*g_IMG_Load)(const char *) = nullptr;
SDL_Surface * (*g_SDL_CreateRGBSurface)(uint32_t, int, int, int, uint32_t, uint32_t, uint32_t, uint32_t) = nullptr;
SDL_Surface * (*g_SDL_CreateRGBSurfaceFrom)(void *pixels, int width, int height, int depth, int pitch, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) = nullptr;
int (*g_SDL_UpperBlit)(SDL_Surface *, const SDL_Rect *, SDL_Surface *, SDL_Rect *) = nullptr;
SDL_Surface * (*g_SDL_ConvertSurface)(SDL_Surface *, const SDL_PixelFormat *, uint32_t) = nullptr;
void (*g_SDL_FreeSurface)(SDL_Surface *) = nullptr;
// int (*g_SDL_SemWait)(DFSDL_sem *) = nullptr;
// int (*g_SDL_SemPost)(DFSDL_sem *) = nullptr;
int (*g_SDL_PushEvent)(SDL_Event *) = nullptr;
SDL_bool (*g_SDL_HasClipboardText)();
int (*g_SDL_SetClipboardText)(const char *text);
char * (*g_SDL_GetClipboardText)();
void (*g_SDL_free)(void *);
SDL_PixelFormat* (*g_SDL_AllocFormat)(uint32_t pixel_format) = nullptr;
SDL_Surface* (*g_SDL_CreateRGBSurfaceWithFormat)(uint32_t flags, int width, int height, int depth, uint32_t format) = nullptr;
int (*g_SDL_ShowSimpleMessageBox)(uint32_t flags, const char *title, const char *message, SDL_Window *window) = nullptr;
char* (*g_SDL_GetPrefPath)(const char* org, const char* app) = nullptr;
char* (*g_SDL_GetBasePath)() = nullptr;
uint32_t (*g_SDL_GetMouseState)(int* x, int* y) = nullptr;
void (*g_SDL_RenderWindowToLogical)(SDL_Renderer* renderer, int windowX, int windowY, float* logicalX, float* logicalY);
void (*g_SDL_RenderLogicalToWindow)(SDL_Renderer* renderer, float logicalX, float logicalY, int* windowX, int* windowY);
SDL_Keycode (*g_SDL_GetKeyFromName)(const char* name) = nullptr;
const char* (*g_SDL_GetKeyName)(SDL_Keycode key) = nullptr;
bool DFSDL::init(color_ostream &out) {
for (auto &lib_str : SDL_LIBS) {
if ((g_sdl_handle = OpenPlugin(lib_str.c_str())))
break;
}
if (!g_sdl_handle) {
out.printerr("DFHack could not find SDL\n");
return false;
}
for (auto &lib_str : SDL_IMAGE_LIBS) {
if ((g_sdl_image_handle = OpenPlugin(lib_str.c_str())))
break;
}
if (!g_sdl_image_handle) {
out.printerr("DFHack could not find SDL_image\n");
return false;
}
#define bind(handle, name) \
g_##name = (decltype(g_##name))LookupPlugin(handle, #name); \
if (!g_##name) { \
out.printerr("DFHack could not find: " #name "\n"); \
return false; \
}
bind(g_sdl_image_handle, IMG_Load);
bind(g_sdl_handle, SDL_CreateRGBSurface);
bind(g_sdl_handle, SDL_CreateRGBSurfaceFrom);
bind(g_sdl_handle, SDL_UpperBlit);
bind(g_sdl_handle, SDL_ConvertSurface);
bind(g_sdl_handle, SDL_FreeSurface);
// bind(g_sdl_handle, SDL_SemWait);
// bind(g_sdl_handle, SDL_SemPost);
bind(g_sdl_handle, SDL_PushEvent);
bind(g_sdl_handle, SDL_HasClipboardText);
bind(g_sdl_handle, SDL_SetClipboardText);
bind(g_sdl_handle, SDL_GetClipboardText);
bind(g_sdl_handle, SDL_free);
bind(g_sdl_handle, SDL_AllocFormat);
bind(g_sdl_handle, SDL_CreateRGBSurfaceWithFormat);
bind(g_sdl_handle, SDL_ShowSimpleMessageBox);
bind(g_sdl_handle, SDL_GetPrefPath);
bind(g_sdl_handle, SDL_GetBasePath);
bind(g_sdl_handle, SDL_GetKeyFromName);
bind(g_sdl_handle, SDL_GetKeyName);
bind(g_sdl_handle, SDL_GetMouseState);
bind(g_sdl_handle, SDL_RenderWindowToLogical);
bind(g_sdl_handle, SDL_RenderLogicalToWindow);
#undef bind
DEBUG(dfsdl,out).print("sdl successfully loaded\n");
return true;
}
// It's ok to leave NULLs in the raws list (according to usage in g_src)
void DFSDL::cleanup() {
if (g_sdl_handle) {
ClosePlugin(g_sdl_handle);
g_sdl_handle = nullptr;
}
if (g_sdl_image_handle) {
ClosePlugin(g_sdl_image_handle);
g_sdl_image_handle = nullptr;
}
}
SDL_Surface * DFSDL::DFIMG_Load(const char *file) {
return g_IMG_Load(file);
}
SDL_Surface * DFSDL::DFSDL_CreateRGBSurface(uint32_t flags, int width, int height, int depth, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) {
return g_SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
}
SDL_Surface * DFSDL::DFSDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask) {
return g_SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask);
}
int DFSDL::DFSDL_UpperBlit(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect) {
return g_SDL_UpperBlit(src, srcrect, dst, dstrect);
}
SDL_Surface * DFSDL::DFSDL_ConvertSurface(SDL_Surface *src, const SDL_PixelFormat *fmt, uint32_t flags) {
return g_SDL_ConvertSurface(src, fmt, flags);
}
void DFSDL::DFSDL_FreeSurface(SDL_Surface *surface) {
g_SDL_FreeSurface(surface);
}
// int DFSDL::DFSDL_SemWait(DFSDL_sem *sem) {
// return g_SDL_SemWait(sem);
// }
// int DFSDL::DFSDL_SemPost(DFSDL_sem *sem) {
// return g_SDL_SemPost(sem);
// }
int DFSDL::DFSDL_PushEvent(SDL_Event *event) {
return g_SDL_PushEvent(event);
}
void DFSDL::DFSDL_free(void *ptr) {
g_SDL_free(ptr);
}
char * DFSDL::DFSDL_GetClipboardText() {
return g_SDL_GetClipboardText();
}
int DFSDL::DFSDL_SetClipboardText(const char *text) {
return g_SDL_SetClipboardText(text);
}
SDL_PixelFormat* DFSDL::DFSDL_AllocFormat(uint32_t pixel_format) {
return g_SDL_AllocFormat(pixel_format);
}
SDL_Surface* DFSDL::DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width, int height, int depth, uint32_t format) {
return g_SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format);
}
char* DFSDL::DFSDL_GetPrefPath(const char* org, const char* app)
{
return g_SDL_GetPrefPath(org, app);
}
char* DFSDL::DFSDL_GetBasePath()
{
return g_SDL_GetBasePath();
}
uint32_t DFSDL::DFSDL_GetMouseState(int* x, int* y) {
return g_SDL_GetMouseState(x, y);
}
void DFSDL::DFSDL_RenderWindowToLogical(SDL_Renderer *renderer, int windowX, int windowY, float *logicalX, float *logicalY) {
g_SDL_RenderWindowToLogical(renderer, windowX, windowY, logicalX, logicalY);
}
void DFSDL::DFSDL_RenderLogicalToWindow(SDL_Renderer *renderer, float logicalX, float logicalY, int *windowX, int *windowY) {
g_SDL_RenderLogicalToWindow(renderer, logicalX, logicalY, windowX, windowY);
}
int DFSDL::DFSDL_ShowSimpleMessageBox(uint32_t flags, const char *title, const char *message, SDL_Window *window) {
if (!g_SDL_ShowSimpleMessageBox)
return -1;
return g_SDL_ShowSimpleMessageBox(flags, title, message, window);
}
SDL_Keycode DFSDL::DFSDL_GetKeyFromName(const char* name) {
if (!g_SDL_GetKeyFromName)
return SDLK_UNKNOWN;
return g_SDL_GetKeyFromName(name);
}
const char* DFSDL::DFSDL_GetKeyName(SDL_Keycode key) {
if (!g_SDL_GetKeyName)
return "";
return g_SDL_GetKeyName(key);
}
// convert tabs to spaces so they don't get converted to '?'
static char * tabs_to_spaces(char *str) {
for (char *c = str; *c; ++c) {
if (*c == '\t')
*c = ' ';
}
return str;
}
static string normalize_newlines(const string & str, bool to_spaces = false) {
string normalized_str = str;
#ifdef WIN32
static const std::regex CRLF("\r\n");
normalized_str = std::regex_replace(normalized_str, CRLF, "\n");
#endif
if (to_spaces)
std::replace(normalized_str.begin(), normalized_str.end(), '\n', ' ');
return normalized_str;
}
DFHACK_EXPORT string DFHack::getClipboardTextCp437() {
if (!g_sdl_handle || g_SDL_HasClipboardText() != SDL_TRUE)
return "";
char *text = tabs_to_spaces(g_SDL_GetClipboardText());
string textcp437 = UTF2DF(normalize_newlines(text, true));
DFHack::DFSDL::DFSDL_free(text);
return textcp437;
}
DFHACK_EXPORT bool DFHack::getClipboardTextCp437Multiline(vector<string> * lines) {
CHECK_NULL_POINTER(lines);
if (!g_sdl_handle || g_SDL_HasClipboardText() != SDL_TRUE)
return false;
char *text = tabs_to_spaces(g_SDL_GetClipboardText());
vector<string> utf8_lines;
split_string(&utf8_lines, normalize_newlines(text), "\n");
DFHack::DFSDL::DFSDL_free(text);
for (auto utf8_line : utf8_lines)
lines->emplace_back(UTF2DF(utf8_line));
return true;
}
DFHACK_EXPORT bool DFHack::setClipboardTextCp437(string text) {
if (!g_sdl_handle)
return false;
return 0 == DFHack::DFSDL::DFSDL_SetClipboardText(DF2UTF(text).c_str());
}
DFHACK_EXPORT bool DFHack::setClipboardTextCp437Multiline(string text) {
if (!g_sdl_handle)
return false;
vector<string> lines;
split_string(&lines, text, "\n");
std::ostringstream str;
for (size_t idx = 0; idx < lines.size(); ++idx) {
str << DF2UTF(lines[idx]);
if (idx < lines.size() - 1) {
#ifdef WIN32
str << "\r\n";
#else
str << "\n";
#endif
}
}
return 0 == DFHack::DFSDL::DFSDL_SetClipboardText(str.str().c_str());
}
// Queue to run callbacks on the render thread.
// Semantics loosely based on SDL3's SDL_RunOnMainThread
static std::recursive_mutex render_cb_lock;
static std::vector<std::function<void()>> render_cb_queue;
DFHACK_EXPORT void DFHack::runOnRenderThread(std::function<void()> cb) {
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
render_cb_queue.push_back(std::move(cb));
}
DFHACK_EXPORT void DFHack::runRenderThreadCallbacks() {
static decltype(render_cb_queue) local_queue;
{
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
std::swap(local_queue, render_cb_queue);
}
for (auto& cb : local_queue) {
cb();
}
local_queue.clear();
}