Skip to content

Commit 5d2f3b6

Browse files
committed
SDL: Fix live edit when start path contains unicode characters #123
1 parent e68c809 commit 5d2f3b6

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

src/platform/sdl/main.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,14 @@
1010
#include <getopt.h>
1111
#include <locale.h>
1212

13-
#include "ui/utils.h"
14-
#include "ui/strlib.h"
1513
#include "platform/sdl/runtime.h"
1614
#include "platform/sdl/settings.h"
1715
#include "platform/sdl/syswm.h"
18-
#include "common/smbas.h"
19-
#include "lib/str.h"
2016

2117
#if !defined(_Win32)
2218
#include <fontconfig/fontconfig.h>
2319
#endif
2420

25-
extern "C" unsigned
26-
lodepng_decode32(unsigned char **out, unsigned *w, unsigned *h,
27-
const unsigned char *in, size_t insize);
28-
2921
using namespace strlib;
3022

3123
const char *FONTS[] = {
@@ -55,7 +47,6 @@ static struct option OPTIONS[] = {
5547
{0, 0, 0, 0}
5648
};
5749

58-
char g_appPath[OS_PATHNAME_SIZE + 1];
5950
int g_debugPort = 4000;
6051

6152
void appLog(const char *format, ...) {
@@ -235,33 +226,6 @@ void printKeywords() {
235226
}
236227
}
237228

238-
void setupAppPath(const char *path) {
239-
g_appPath[0] = '\0';
240-
if (path[0] == '/' ||
241-
(path[1] == ':' && ((path[2] == '\\') || path[2] == '/'))) {
242-
// full path or C:/
243-
strlcpy(g_appPath, path, sizeof(g_appPath));
244-
} else {
245-
// relative path
246-
char cwd[OS_PATHNAME_SIZE + 1];
247-
cwd[0] = '\0';
248-
getcwd(cwd, sizeof(cwd) - 1);
249-
strlcpy(g_appPath, cwd, sizeof(g_appPath));
250-
strlcat(g_appPath, "/", sizeof(g_appPath));
251-
strlcat(g_appPath, path, sizeof(g_appPath));
252-
#if defined(__linux__) || defined(__midipix__)
253-
if (access(g_appPath, X_OK) != 0) {
254-
// launched via PATH, retrieve full path
255-
ssize_t len = ::readlink("/proc/self/exe", g_appPath, sizeof(g_appPath));
256-
if (len == -1 || len == sizeof(g_appPath)) {
257-
len = 0;
258-
}
259-
g_appPath[len] = '\0';
260-
}
261-
#endif
262-
}
263-
}
264-
265229
void showHelp() {
266230
fprintf(stdout,
267231
"SmallBASIC version %s - kw:%d, pc:%d, fc:%d, ae:%d I=%d N=%d\n\n",

src/platform/sdl/syswm.cpp

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
#include "config.h"
1010
#include "platform/sdl/syswm.h"
11+
#include "lib/str.h"
1112

1213
#define DEFAULT_FONT_SIZE 12
1314
#define DEFAULT_FONT_SIZE_PTS 11
1415

15-
extern char g_appPath[];
1616
extern int g_debugPort;
1717

1818
void appLog(const char *format, ...);
@@ -21,6 +21,12 @@ void appLog(const char *format, ...);
2121
#include <SDL_syswm.h>
2222
#include <shellapi.h>
2323

24+
WCHAR g_appPath[MAX_PATH + 1];
25+
26+
void setupAppPath(const char *path) {
27+
GetModuleFileNameW(NULL, g_appPath, MAX_PATH);
28+
}
29+
2430
void loadIcon(SDL_Window *window) {
2531
HINSTANCE handle = ::GetModuleHandle(NULL);
2632
HICON icon = ::LoadIcon(handle, MAKEINTRESOURCE(101));
@@ -49,19 +55,19 @@ int getStartupFontSize(SDL_Window *window) {
4955
}
5056

5157
void launchDebug(const char *file) {
52-
STARTUPINFO info = {sizeof(info)};
58+
STARTUPINFOW info = {sizeof(info)};
5359
PROCESS_INFORMATION processInfo;
54-
char cmd[1024];
55-
sprintf(cmd, "-p %d -d %s", g_debugPort, file);
56-
if (!CreateProcess(g_appPath, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
60+
WCHAR cmd[MAX_PATH + 1];
61+
swprintf(cmd, MAX_PATH, L"-p %d -d %s", g_debugPort, file);
62+
if (!CreateProcessW(g_appPath, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
5763
appLog("failed to start %d %s %s\n", GetLastError(), g_appPath, cmd);
5864
}
5965
}
6066

6167
void launch(const char *command, const char *file) {
6268
STARTUPINFO info = {sizeof(info)};
6369
PROCESS_INFORMATION processInfo;
64-
char cmd[1024];
70+
char cmd[MAX_PATH + 1];
6571
sprintf(cmd, "%s -x %s", command, file);
6672
if (!CreateProcess(command, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
6773
appLog("failed to start %d %s %s\n", GetLastError(), command, cmd);
@@ -77,12 +83,25 @@ void browseFile(SDL_Window *window, const char *url) {
7783
}
7884
}
7985

86+
void launchExec(const char *file) {
87+
STARTUPINFOW info = {sizeof(info)};
88+
PROCESS_INFORMATION processInfo;
89+
WCHAR cmd[MAX_PATH + 1];
90+
swprintf(cmd, MAX_PATH, L"%s -x %s", g_appPath, file);
91+
if (!CreateProcessW(g_appPath, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
92+
appLog("failed to start %d %s %s\n", GetLastError(), g_appPath, cmd);
93+
}
94+
}
95+
8096
#else
8197
#include <unistd.h>
8298
#include <errno.h>
99+
#include <limits.h>
83100
#include "icon.h"
84101
#include "lib/lodepng/lodepng.h"
85102

103+
char g_appPath[PATH_MAX + 1];
104+
86105
void loadIcon(SDL_Window *window) {
87106
unsigned w, h;
88107
unsigned char *image;
@@ -165,8 +184,35 @@ void browseFile(SDL_Window *window, const char *url) {
165184
}
166185
}
167186

187+
void setupAppPath(const char *path) {
188+
g_appPath[0] = '\0';
189+
if (path[0] == '/' ||
190+
(path[1] == ':' && ((path[2] == '\\') || path[2] == '/'))) {
191+
// full path or C:/
192+
strlcpy(g_appPath, path, sizeof(g_appPath));
193+
} else {
194+
// relative path
195+
char cwd[PATH_MAX + 1];
196+
cwd[0] = '\0';
197+
getcwd(cwd, sizeof(cwd) - 1);
198+
strlcpy(g_appPath, cwd, sizeof(g_appPath));
199+
strlcat(g_appPath, "/", sizeof(g_appPath));
200+
strlcat(g_appPath, path, sizeof(g_appPath));
201+
#if defined(__linux__) || defined(__midipix__)
202+
if (access(g_appPath, X_OK) != 0) {
203+
// launched via PATH, retrieve full path
204+
ssize_t len = ::readlink("/proc/self/exe", g_appPath, sizeof(g_appPath));
205+
if (len == -1 || len == sizeof(g_appPath)) {
206+
len = 0;
207+
}
208+
g_appPath[len] = '\0';
209+
}
168210
#endif
211+
}
212+
}
169213

170214
void launchExec(const char *file) {
171215
launch(g_appPath, file);
172216
}
217+
218+
#endif

src/platform/sdl/syswm.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
#include <SDL_video.h>
1313

14-
void loadIcon(SDL_Window *window);
15-
int getStartupFontSize(SDL_Window *window);
14+
void browseFile(SDL_Window *window, const char *url);
15+
int getStartupFontSize(SDL_Window *window);
16+
void launch(const char *command, const char *file);
1617
void launchDebug(const char *file);
1718
void launchExec(const char *file);
18-
void launch(const char *command, const char *file);
19-
void browseFile(SDL_Window *window, const char *url);
19+
void loadIcon(SDL_Window *window);
20+
void setupAppPath(const char *path);
2021

2122
#endif

0 commit comments

Comments
 (0)