Skip to content

Commit

Permalink
use SDL_OpenURL for love.system.openURL implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Jan 14, 2025
1 parent 176a3c1 commit 9aaee85
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 116 deletions.
117 changes: 2 additions & 115 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,10 @@
#include "common/config.h"
#include "System.h"

#if defined(LOVE_MACOS)
#include <CoreServices/CoreServices.h>
#elif defined(LOVE_IOS)
#if defined(LOVE_IOS)
#include "common/ios.h"
#elif defined(LOVE_LINUX) || defined(LOVE_ANDROID)
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#elif defined(LOVE_WINDOWS)
#include "common/utf8.h"
#include <shlobj.h>
#include <shellapi.h>
#pragma comment(lib, "shell32.lib")
#endif
#if defined(LOVE_ANDROID)
#elif defined(LOVE_ANDROID)
#include "common/android.h"
#elif defined(LOVE_LINUX)

#ifdef __has_include
#if __has_include(<spawn.h>)
#define LOVE_HAS_POSIX_SPAWN
#endif
#endif

#ifdef LOVE_HAS_POSIX_SPAWN
#include <spawn.h>
#else
#include <unistd.h>
#endif
#endif

namespace love
Expand Down Expand Up @@ -82,94 +57,6 @@ const char *System::getOS()
#endif
}

#ifdef LOVE_HAS_POSIX_SPAWN
extern "C"
{
extern char **environ; // The environment, always available
}
#endif

bool System::openURL(const std::string &url) const
{

#if defined(LOVE_MACOS)

bool success = false;
CFURLRef cfurl = CFURLCreateWithBytes(nullptr,
(const UInt8 *) url.c_str(),
url.length(),
kCFStringEncodingUTF8,
nullptr);

success = LSOpenCFURLRef(cfurl, nullptr) == noErr;
CFRelease(cfurl);
return success;

#elif defined(LOVE_IOS)

return love::ios::openURL(url);

#elif defined(LOVE_ANDROID)

return love::android::openURL(url);

#elif defined(LOVE_LINUX)

pid_t pid;
const char *argv[] = {"xdg-open", url.c_str(), nullptr};

#ifdef LOVE_HAS_POSIX_SPAWN
// Note: at the moment this process inherits our file descriptors.
// Note: the below const_cast is really ugly as well.
if (posix_spawnp(&pid, "xdg-open", nullptr, nullptr, const_cast<char **>(argv), environ) != 0)
return false;
#else
pid = fork();
if (pid == 0)
{
execvp("xdg-open", const_cast<char **>(argv));
return false;
}
#endif

// Check if xdg-open already completed (or failed.)
int status = 0;
if (waitpid(pid, &status, WNOHANG) > 0)
return (status == 0);
else
// We can't tell what actually happens without waiting for
// the process to finish, which could take forever (literally).
return true;

#elif defined(LOVE_WINDOWS)

// Unicode-aware WinAPI functions don't accept UTF-8, so we need to convert.
std::wstring wurl = to_widestr(url);

HINSTANCE result = 0;

#if defined(LOVE_WINDOWS_UWP)

Platform::String^ urlString = ref new Platform::String(wurl.c_str());
auto uwpUri = ref new Windows::Foundation::Uri(urlString);
Windows::System::Launcher::LaunchUriAsync(uwpUri);

#else

result = ShellExecuteW(nullptr,
L"open",
wurl.c_str(),
nullptr,
nullptr,
SW_SHOW);

#endif

return (ptrdiff_t) result > 32;

#endif
}

void System::vibrate(double seconds) const
{
#ifdef LOVE_ANDROID
Expand Down
2 changes: 1 addition & 1 deletion src/modules/system/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class System : public Module
*
* @return Whether the URL was opened successfully.
**/
virtual bool openURL(const std::string &url) const;
virtual bool openURL(const std::string &url) const = 0;

/**
* Vibrates for the specified amount of seconds.
Expand Down
6 changes: 6 additions & 0 deletions src/modules/system/sdl/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <SDL3/SDL_clipboard.h>
#include <SDL3/SDL_cpuinfo.h>
#include <SDL3/SDL_locale.h>
#include <SDL3/SDL_misc.h>

namespace love
{
Expand Down Expand Up @@ -87,6 +88,11 @@ love::system::System::PowerState System::getPowerInfo(int &seconds, int &percent
return state;
}

bool System::openURL(const std::string &url) const
{
return SDL_OpenURL(url.c_str());
}

std::vector<std::string> System::getPreferredLocales() const
{
std::vector<std::string> result;
Expand Down
1 change: 1 addition & 0 deletions src/modules/system/sdl/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class System : public love::system::System
std::string getClipboardText() const override;

PowerState getPowerInfo(int &seconds, int &percent) const override;
bool openURL(const std::string &url) const override;
std::vector<std::string> getPreferredLocales() const override;

private:
Expand Down

0 comments on commit 9aaee85

Please sign in to comment.