File: BasicUI.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (284 lines) | stat: -rw-r--r-- 5,874 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*!********************************************************************

Audacity: A Digital Audio Editor

@file BasicUI.cpp

Paul Licameli

**********************************************************************/
#include "BasicUI.h"

#include <mutex>
#include <vector>

#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#define HAS_XDG_OPEN_HELPER
#endif

#if defined(HAS_XDG_OPEN_HELPER)

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <string>

#if defined(__FreeBSD__) || defined(__OpenBSD__)
extern char** environ;
#endif

namespace
{

const char* ENV_PREFIX = "APPIMAGE_PRESERVED_";

bool IsPreservedEnvVar(const char* var)
{
   return strncmp(var, ENV_PREFIX, strlen(ENV_PREFIX)) == 0;
}

bool ResetEnv()
{
   for(auto envIterator = environ; *envIterator; ++envIterator)
   {
      auto envVar = *envIterator;

      if (!IsPreservedEnvVar(envVar))
         continue;

      auto separator = strchr(envVar, '=');

      // Why?
      if (!separator)
         continue;

      auto varName = std::string(envVar + strlen(ENV_PREFIX), separator);

      if (varName.empty())
         continue;

      auto varValue = separator + 1;

      const auto result = *varValue ?
         setenv(varName.c_str(), varValue, true) :
         unsetenv(varName.c_str());

      if (result != 0)
         return false;
   }

   return true;
}

std::string FindXDGOpen()
{
   const char *path = getenv("PATH");

   if (!path)
      return {};

   std::string result;

   while (*path) {
      const char *colon = strchr(path, ':');
      if (!colon)
         colon = path + strlen(path);

      result.assign(path, colon);
      result += "/xdg-open";

      if (access(result.c_str(), X_OK) == 0)
         return result;

      path = colon;

      if (*path == ':')
         ++path;
   }

   return {};
}

bool RunXDGOpen(const std::string& uri)
{
   std::string xdgOpen = FindXDGOpen();

   if (xdgOpen.empty())
      return false;

   pid_t pid = fork();

   if (pid == -1)
      return false;

   if (pid == 0)
   {
      // Fork again. This way OS is now responsible for cleaning up the
      // (grand)child process.
      int secondFork = fork();

      if (secondFork < 0)
         return false;

      if (secondFork == 0)
      {// Close all file descriptors except stdin, stdout, stderr
         struct rlimit rlim;
         if (getrlimit(RLIMIT_NOFILE, &rlim) == 0)
         {
            for (int fd = STDERR_FILENO + 1; fd < rlim.rlim_cur; ++fd)
               close(fd);
         }

         // Open /dev/null as stdin, stdout, stderr
         int fd = open("/dev/null", O_RDWR);

         if (fd != -1)
         {
            dup2(fd, STDIN_FILENO);
            dup2(fd, STDOUT_FILENO);
            dup2(fd, STDERR_FILENO);

            if (fd > STDERR_FILENO)
               close(fd);
         }

         if(!ResetEnv())
            _exit(1);

         char* const args[] = {
            const_cast<char*>(xdgOpen.c_str()),
            const_cast<char*>(uri.c_str()),
            nullptr
         };
         // Run xdg-open
         execv(xdgOpen.c_str(), args);

         // If we get here, something went wrong
         _exit(1);
      }
      else
      {
         // Grandchild has started, so we can exit
         _exit(0);
      }
   }
   else
   {
      int status;
      waitpid(pid, &status, 0);

      return WIFEXITED(status) && WEXITSTATUS(status) == 0;
   }

   return false;
}

}


#endif

namespace  BasicUI {
WindowPlacement::~WindowPlacement() = default;

WindowPlacement::operator bool() const { return false; }

Services::~Services() = default;

ProgressDialog::~ProgressDialog() = default;

GenericProgressDialog::~GenericProgressDialog() = default;

static Services *theInstance = nullptr;

Services *Get() { return theInstance; }

Services *Install(Services *pInstance)
{
   auto result = theInstance;
   theInstance = pInstance;
   return result;
}

static std::recursive_mutex sActionsMutex;
static std::vector<Action> sActions;

void CallAfter(Action action)
{
   if (auto p = Get())
      p->DoCallAfter(action);
   else {
      // No services yet -- but don't lose the action.  Put it in a queue
      auto guard = std::lock_guard{ sActionsMutex };
      sActions.emplace_back(std::move(action));
   }
}

void Yield()
{
   do {
      // Dispatch anything in the queue, added while there were no Services
      {
         auto guard = std::lock_guard{ sActionsMutex };
         std::vector<Action> actions;
         actions.swap(sActions);
         for (auto &action : actions)
            action();
      }

      // Dispatch according to Services, if present
      if (auto p = Get())
         p->DoYield();
   }
   // Re-test for more actions that might have been enqueued by actions just
   // dispatched
   while ( !sActions.empty() );
}

void ProcessIdle()
{
   do {
      // Dispatch anything in the queue, added while there were no Services
      {
         auto guard = std::lock_guard{ sActionsMutex };
         std::vector<Action> actions;
         actions.swap(sActions);
         for (auto &action : actions)
            action();
      }

      // Dispatch according to Services, if present
      if (auto p = Get())
         p->DoProcessIdle();
   }
   // Re-test for more actions that might have been enqueued by actions just
   // dispatched
   while ( !sActions.empty() );
}

bool OpenInDefaultBrowser(const wxString &url)
{
#if defined(HAS_XDG_OPEN_HELPER)
   if (RunXDGOpen(url.ToStdString()))
      return true;
#endif

   if(auto p = Get())
      return p->DoOpenInDefaultBrowser(url);

   return false;
}

TranslatableString DefaultCaption()
{
   return XO("Message");
}
}