OpenClonk
PlatformAbstraction.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001, Sven Eberhardt
5  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 #include "C4Include.h"
18 #include "game/C4Application.h"
19 
20 #ifdef _WIN32
22 #include <shellapi.h>
23 bool OpenURL(const char *szURL)
24 {
25  return (intptr_t)ShellExecuteW(nullptr, L"open", GetWideChar(szURL), nullptr, nullptr, SW_SHOW) > 32;
26 }
27 
28 bool EraseItemSafe(const char *szFilename)
29 {
30  char Filename[_MAX_PATH_LEN];
31  SCopy(szFilename, Filename, _MAX_PATH);
32  Filename[SLen(Filename)+1]=0;
33  auto wide_filename = GetWideChar(Filename, true); // wide_filename holds the buffer
34  SHFILEOPSTRUCTW shs;
35  shs.hwnd=nullptr;
36  shs.wFunc=FO_DELETE;
37  shs.pFrom = wide_filename;
38  shs.pTo=nullptr;
39  shs.fFlags=FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT;
40  shs.fAnyOperationsAborted=false;
41  shs.hNameMappings=nullptr;
42  shs.lpszProgressTitle=nullptr;
43  auto error = SHFileOperationW(&shs);
44  return !error;
45 }
46 
47 bool IsGermanSystem()
48 {
49  return PRIMARYLANGID(GetUserDefaultLangID()) == LANG_GERMAN;
50 }
51 
52 #elif !defined(__APPLE__)
53 
55 {
56  if (strstr(setlocale(LC_MESSAGES, nullptr), "de"))
57  return true;
58  else
59  return false;
60 }
61 
62 bool EraseItemSafe(const char *szFilename)
63 {
64  return false;
65 }
66 
67 #if defined(WITH_QT_EDITOR)
68 #include <QDesktopServices>
69 #include <QUrl>
70 bool OpenURL(char const* url)
71 {
72  return QDesktopServices::openUrl(QUrl::fromUserInput(url));
73 }
74 #else
75 bool OpenURL(char const*) {return 0;}
76 #endif
77 
78 #endif
79 
80 
81 bool RestartApplication(std::vector<const char *> parameters)
82 {
83  // restart with given parameters
84  bool success = false;
85 #ifdef _WIN32
86  wchar_t buf[_MAX_PATH];
87  DWORD sz = ::GetModuleFileName(::GetModuleHandle(nullptr), buf, _MAX_PATH);
88  if (sz)
89  {
90  StdStrBuf params;
91  for (auto p : parameters)
92  {
93  params += R"(")";
94  params += p;
95  params += R"(" )";
96  }
97  intptr_t iError = (intptr_t)::ShellExecute(nullptr, nullptr, buf, params.GetWideChar(), Config.General.ExePath.GetWideChar(), SW_SHOW);
98  if (iError > 32) success = true;
99  }
100 #else
101  pid_t pid;
102  switch (pid = fork())
103  {
104  case -1: break; // error message shown below
105  case 0:
106  {
107  std::vector<const char*> params = {Application.argv0};
108  params.insert(params.end(), parameters.begin(), parameters.end());
109  params.push_back(nullptr);
110 #ifdef PROC_SELF_EXE
111  execv(PROC_SELF_EXE, const_cast<char *const *>(params.data()));
112  perror("editor launch via " PROC_SELF_EXE " failed");
113 #endif
114  execvp(Application.argv0, const_cast<char *const *>(params.data()));
115  perror("editor launch via argv[0] failed");
116  exit(1);
117  }
118  default:
119  success = true;
120  }
121 #endif
122  // must quit ourselves for new instance to be shown
123  if (success) Application.Quit();
124  return success;
125 }
C4Config Config
Definition: C4Config.cpp:930
C4Application Application
Definition: C4Globals.cpp:44
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
bool EraseItemSafe(const char *szFilename)
bool IsGermanSystem()
bool RestartApplication(std::vector< const char * > parameters)
bool OpenURL(char const *)
#define _MAX_PATH
#define _MAX_PATH_LEN
uint32_t DWORD
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
size_t SLen(const char *sptr)
Definition: Standard.h:74
const char * argv0
Definition: C4Application.h:75
void Quit() override
StdCopyStrBuf ExePath
Definition: C4Config.h:54
C4ConfigGeneral General
Definition: C4Config.h:255