From b4fdba2fb3afcb80e419c048504a118ec8376c09 Mon Sep 17 00:00:00 2001 From: yexo Date: Sun, 11 Dec 2011 11:47:08 +0000 Subject: (svn r23490) -Add [FS#2750]: OpenBrowser function to open a browser on major OSes --- src/openttd.cpp | 9 +++++++++ src/os/macosx/macos.mm | 4 ++++ src/os/unix/unix.cpp | 15 +++++++++++++++ src/os/windows/win32.cpp | 6 ++++++ 4 files changed, 34 insertions(+) diff --git a/src/openttd.cpp b/src/openttd.cpp index a902d197d..f6b00e421 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -359,6 +359,15 @@ void MakeNewgameSettingsLive() } } +void OpenBrowser(const char *url) +{ + /* Make sure we only accept urls that are sure to open a browser. */ + if (strstr(url, "http://") != url && strstr(url, "https://") != url) return; + + extern void OSOpenBrowser(const char *url); + OSOpenBrowser(url); +} + /** Callback structure of statements to be executed after the NewGRF scan. */ struct AfterNewGRFScan : NewGRFScanCallback { Year startyear; ///< The start year. diff --git a/src/os/macosx/macos.mm b/src/os/macosx/macos.mm index 7c5092403..38ddae8ee 100644 --- a/src/os/macosx/macos.mm +++ b/src/os/macosx/macos.mm @@ -118,6 +118,10 @@ void ShowOSErrorBox(const char *buf, bool system) } } +void OSOpenBrowser(const char *url) +{ + [ [ NSWorkspace sharedWorkspace ] openURL:[ NSURL URLWithString:[ NSString stringWithUTF8String:url ] ] ]; +} /** * Determine and return the current user's locale. diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 700a9050c..7cd9709f8 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -14,6 +14,7 @@ #include "../../openttd.h" #include "../../crashlog.h" #include "../../core/random_func.hpp" +#include "../../debug.h" #include @@ -348,4 +349,18 @@ uint GetCPUCoreCount() return count; } + +void OSOpenBrowser(const char *url) +{ + pid_t child_pid = fork(); + if (child_pid != 0) return; + + const char *args[3]; + args[0] = "/usr/bin/xdg-open"; + args[1] = url; + args[2] = NULL; + execv(args[0], const_cast(args)); + DEBUG(misc, 0, "Failed to open url: %s", url); + exit(0); +} #endif diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 6c7a3ec68..07227222a 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -18,6 +18,7 @@ #include #include #include /* SHGetFolderPath */ +#include #include "win32.h" #include "../../core/alloc_func.hpp" #include "../../openttd.h" @@ -79,6 +80,11 @@ void ShowOSErrorBox(const char *buf, bool system) MessageBox(GetActiveWindow(), MB_TO_WIDE(buf), _T("Error!"), MB_ICONSTOP); } +void OSOpenBrowser(const char *url) +{ + ShellExecute(GetActiveWindow(), _T("open"), MB_TO_WIDE(url), NULL, NULL, SW_SHOWNORMAL); +} + /* Code below for windows version of opendir/readdir/closedir copied and * modified from Jan Wassenberg's GPL implementation posted over at * http://www.gamedev.net/community/forums/topic.asp?topic_id=364584&whichpage=1� */ -- cgit v1.2.3-54-g00ecf