OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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+1];
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=0;
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=0;
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, 0), "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 #undef LineFeed
69 #include <QDesktopServices>
70 #include <QUrl>
71 bool OpenURL(char const* url)
72 {
73  return QDesktopServices::openUrl(QUrl::fromUserInput(url));
74 }
75 #else
76 bool OpenURL(char const*) {return 0;}
77 #endif
78 
79 #endif
80 
81 
82 bool RestartApplication(std::vector<const char *> parameters)
83 {
84  // restart with given parameters
85  bool success = false;
86 #ifdef _WIN32
87  wchar_t buf[_MAX_PATH];
88  DWORD sz = ::GetModuleFileName(::GetModuleHandle(nullptr), buf, _MAX_PATH);
89  if (sz)
90  {
91  StdStrBuf params;
92  for (auto p : parameters)
93  {
94  params += "\"";
95  params += p;
96  params += "\" ";
97  }
98  intptr_t iError = (intptr_t)::ShellExecute(nullptr, nullptr, buf, params.GetWideChar(), Config.General.ExePath.GetWideChar(), SW_SHOW);
99  if (iError > 32) success = true;
100  }
101 #else
102  pid_t pid;
103  switch (pid = fork())
104  {
105  case -1: break; // error message shown below
106  case 0:
107  {
108  std::vector<const char*> params = {"openclonk"};
109  params.insert(params.end(), parameters.begin(), parameters.end());
110  params.push_back(nullptr);
111  execv("/proc/self/exe", const_cast<char *const *>(params.data()));
112  perror("editor launch failed");
113  exit(1);
114  }
115  default:
116  success = true;
117  }
118 #endif
119  // must quit ourselves for new instance to be shown
120  if (success) Application.Quit();
121  return success;
122 }
bool EraseItemSafe(const char *szFilename)
C4Config Config
Definition: C4Config.cpp:837
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
C4ConfigGeneral General
Definition: C4Config.h:252
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
size_t SLen(const char *sptr)
Definition: Standard.h:78
virtual void Quit()
bool OpenURL(char const *)
bool IsGermanSystem()
bool RestartApplication(std::vector< const char * > parameters)
StdCopyStrBuf ExePath
Definition: C4Config.h:53
uint32_t DWORD
C4Application Application
Definition: C4Globals.cpp:44