From 1a3b249efb42f77c1c5ba317189bdcee4d28d7eb Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Tue, 21 Apr 2015 22:06:58 -0400 Subject: [PATCH] support opening URLs in default browser --- include/platform.h | 1 + src/CMakeLists.txt | 1 + src/network/multi_pxo.cpp | 26 ++----------------- src/platform/unix.cpp | 47 ++++++++++++++++++++++++++++++++++ src/platform/win.cpp | 53 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 src/platform/win.cpp diff --git a/include/platform.h b/include/platform.h index f5af28a..7f93c51 100644 --- a/include/platform.h +++ b/include/platform.h @@ -17,5 +17,6 @@ void base_filename(const char *path, char *filename, const int max_fname); +int platform_open_url(const char *url); #endif // PLATFORM_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 95301f2..5eac7ad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -244,6 +244,7 @@ set(code_SOURCE set(platform_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/platform/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/platform/unix.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/platform/win.cpp PARENT_SCOPE ) diff --git a/src/network/multi_pxo.cpp b/src/network/multi_pxo.cpp index 6c27bc8..4a63310 100644 --- a/src/network/multi_pxo.cpp +++ b/src/network/multi_pxo.cpp @@ -1939,31 +1939,9 @@ void multi_pxo_strip_space(char *string1, char *string2, const int str2_len) // fire up the given URL void multi_pxo_url(char *url) { -#ifdef PLAT_UNIX - STUB_FUNCTION; -#else - // execute the shell command - int r = (int) ShellExecute(NULL, NOX("open"), url, NULL, NULL, SW_SHOW); - if (r < 32) { - switch (r) { - case 0: - case ERROR_BAD_FORMAT: - case SE_ERR_ACCESSDENIED: - case SE_ERR_ASSOCINCOMPLETE: - case SE_ERR_DDEBUSY: - case SE_ERR_DDEFAIL: - case SE_ERR_DDETIMEOUT: - case SE_ERR_DLLNOTFOUND: - case SE_ERR_OOM: - case SE_ERR_SHARE: - case SE_ERR_NOASSOC: - case ERROR_FILE_NOT_FOUND: - case ERROR_PATH_NOT_FOUND: - popup(PF_USE_AFFIRMATIVE_ICON | PF_TITLE_RED | PF_TITLE_BIG,1,POPUP_OK,XSTR("Warning\nCould not locate/launch default Internet Browser",943)); - break; - } + if ( platform_open_url(url) ) { + popup(PF_USE_AFFIRMATIVE_ICON | PF_TITLE_RED | PF_TITLE_BIG,1,POPUP_OK,XSTR("Warning\nCould not locate/launch default Internet Browser",943)); } -#endif } // load/set the palette diff --git a/src/platform/unix.cpp b/src/platform/unix.cpp index 391449b..9bf15f1 100644 --- a/src/platform/unix.cpp +++ b/src/platform/unix.cpp @@ -11,6 +11,11 @@ #include #include #include +#include +#include +#include + +#include "SDL.h" int filelength (int fd) @@ -27,5 +32,47 @@ int WSAGetLastError() return errno; } +int platform_open_url(const char *url) +{ +#ifdef __APPLE__ + const char *open_cmd = "open"; +#else + const char *open_cmd = "xdg-open"; +#endif + char s_url[256]; + int statval = 0; + + // make sure it's a valid www address + if ( !SDL_strncasecmp(url, "http://", 7) || !SDL_strncasecmp(url, "https://", 8) ) { + SDL_strlcpy(s_url, url, SDL_arraysize(s_url)); + } else { + SDL_strlcpy(s_url, "http://", SDL_arraysize(s_url)); + SDL_strlcat(s_url, url, SDL_arraysize(s_url)); + } + + pid_t mpid = fork(); + + if (mpid < 0) { + // nothing, will return error + } else if (mpid == 0) { + int rv = 0; + + rv = execlp(open_cmd, open_cmd, s_url, (char *)0); + + exit(rv); + } else { + waitpid(mpid, &statval, 0); + + if ( WIFEXITED(statval) ) { + if (WEXITSTATUS(statval) == 0) { + return 0; + } else { + return -1; + } + } + } + + return -1; +} #endif diff --git a/src/platform/win.cpp b/src/platform/win.cpp new file mode 100644 index 0000000..528b7d1 --- /dev/null +++ b/src/platform/win.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) Volition, Inc. 1999. All rights reserved. + * + * All source code herein is the property of Volition, Inc. You may not sell + * or otherwise commercially exploit the source or things you created based on + * the source. + */ + +#ifndef PLAT_UNIX + +#define WIN32_LEAN_AND_MEAN +#include + +#include "SDL.h" + + +int platform_open_url(const char *url) +{ + char s_url[256]; + + // make sure it's a valid www address + if ( !SDL_strncasecmp(url, "http://", 7) || !SDL_strncasecmp(url, "https://", 8) ) { + SDL_strlcpy(s_url, url, SDL_arraysize(s_url)); + } else { + SDL_strlcpy(s_url, "http://", SDL_arraysize(s_url)); + SDL_strlcat(s_url, url, SDL_arraysize(s_url)); + } + + int rval = (int) ShellExecute(NULL, "open", s_url, NULL, NULL, SW_SHOW); + + if (rval < 32) { + switch (rval) { + case 0: + case ERROR_BAD_FORMAT: + case SE_ERR_ACCESSDENIED: + case SE_ERR_ASSOCINCOMPLETE: + case SE_ERR_DDEBUSY: + case SE_ERR_DDEFAIL: + case SE_ERR_DDETIMEOUT: + case SE_ERR_DLLNOTFOUND: + case SE_ERR_OOM: + case SE_ERR_SHARE: + case SE_ERR_NOASSOC: + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + return -1; + } + } + + return 0; +} + +#endif -- 2.39.2