TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkrunobject.cpp
Go to the documentation of this file.
1 #include "ttkrunobject.h"
2 #include "ttkversion.h"
3 
4 #ifdef _WIN32
5 # define WIN32_LEAN_AND_MEAN
6 # include <windows.h>
7 # include <shellapi.h>
8 #else
9 # include <unistd.h>
10 #endif
11 
12 #ifdef __APPLE__
13 # include <mach-o/dyld.h>
14 #endif
15 
16 namespace TTK
17 {
18 static bool endWith(const TTKString &value, const TTKString &tail)
19 {
20  if(value.empty() || tail.empty() || value.size() < tail.size())
21  {
22  return false;
23  }
24 
25  return value.compare(value.length() - tail.size(), tail.size(), tail) == 0;
26 }
27 }
28 
29 
30 void TTKRunObject::execute(int argc, char **argv) const
31 {
32  TTKString args;
33  for(int i = 0; i < argc; ++i)
34  {
35  TTKString arg(argv[i]);
37  {
38  const size_t pos = arg.find('\"');
39  if(pos != TTKString::npos)
40  {
41  arg.insert(arg.begin() + pos, '\\');
42  }
43 
44  args.append(TTK_STR_QUOTES(arg));
45  args.append(TTK_SPACE);
46  }
47  }
48 
49  char path[TTK_LOW_BUFFER] = {};
51 #ifdef _WIN32
52  GetModuleFileNameA(nullptr, path, TTK_LOW_BUFFER);
53 
54  TTKString filePath = path;
55  const size_t pos = filePath.find_last_of('\\');
56  if(pos != TTKString::npos)
57  {
58  filePath.erase(pos);
59  }
60 
61  ShellExecuteA(nullptr, "open", (filePath + suffix).c_str(), args.c_str(), nullptr, SW_HIDE);
62 #elif defined __linux__
63  TTKString filePath = get_current_dir_name();
64  if(readlink("/proc/self/exe", path, sizeof(path) - 1) != 0)
65  {
66  filePath = path;
67  const size_t pos = filePath.find_last_of('/');
68  if(pos != TTKString::npos)
69  {
70  filePath.erase(pos);
71  }
72  }
73 
74  system((filePath + suffix + TTK_SPACE + args).c_str());
75 #elif defined __APPLE__
76  // macOS: get executable path
77  TTKString filePath("./");
78  uint32_t size = sizeof(path);
79  if(_NSGetExecutablePath(path, &size) == 0)
80  {
81  filePath = path;
82  const size_t pos = filePath.find_last_of('/');
83  if(pos != TTKString::npos)
84  {
85  filePath.erase(pos);
86  }
87  }
88  else
89  {
90  // fallback: use current working directory
91  char cwd[TTK_LOW_BUFFER] = {};
92  if(getcwd(cwd, sizeof(cwd)))
93  {
94  filePath = cwd;
95  }
96  }
97 
98  system((filePath + suffix + TTK_SPACE + args).c_str());
99 #endif
100 }
voidpf void uLong size
Definition: ioapi.h:136
std::string TTKString
Definition: ttkglobal.h:168
TTK_MODULE_EXPORT QString suffix(const QString &name)
The namespace of the application object.
Definition: ttkcompat.h:24
#define TTK_SEPARATOR
Definition: ttkglobal.h:269
#define TTK_LOW_BUFFER
Definition: ttkglobal.h:338
#define TTK_APP_FILE_NAME
Definition: ttkobject.h:33
#define TTK_VERSION_STR
Definition: ttkversion.h:175
#define TTK_STR_CAT(...)
Definition: ttkglobal.h:240
void execute(int argc, char **argv) const
#define TTK_SERVICE_RUN_NAME
Definition: ttkobject.h:35
#define TTK_STR_QUOTES(s)
Definition: ttkglobal.h:251
static bool endWith(const TTKString &value, const TTKString &tail)
#define TTK_SPACE
Definition: ttkglobal.h:268