19
19
20
20
#include < boost/throw_exception.hpp>
21
21
22
+ #include < string>
22
23
#include < unistd.h>
23
24
#include < signal.h>
24
25
25
26
#include < cstring>
26
- #include < stdexcept>
27
27
#include < system_error>
28
28
#include < vector>
29
29
@@ -93,46 +93,21 @@ auto Environment::exec_env() const & -> std::vector<char const*>
93
93
result.push_back (nullptr );
94
94
return result;
95
95
}
96
- } // namespace
97
96
98
- auto miral::launch_app_env (
99
- std::vector<std::string> const & app,
100
- mir::optional_value<std::string> const & wayland_display,
101
- mir::optional_value<std::string> const & x11_display,
102
- miral::AppEnvironment const & app_env) -> pid_t
97
+ void assign_or_unset (Environment& env, std::string const & key, auto optional_value)
103
98
{
104
- Environment application_environment;
105
-
106
- for (auto const & [key, value]: app_env)
107
- {
108
- if (value)
109
- {
110
- application_environment.setenv (key, value.value ());
111
- }
112
- else
113
- {
114
- application_environment.unsetenv (key);
115
- }
116
- }
117
-
118
- if (wayland_display)
99
+ if (optional_value)
119
100
{
120
- application_environment .setenv (" WAYLAND_DISPLAY " , wayland_display .value ()); // configure Wayland socket
101
+ env .setenv (key, optional_value .value ());
121
102
}
122
103
else
123
104
{
124
- application_environment.unsetenv (" WAYLAND_DISPLAY" );
125
- }
126
-
127
- if (x11_display)
128
- {
129
- application_environment.setenv (" DISPLAY" , x11_display.value ()); // configure X11 socket
130
- }
131
- else
132
- {
133
- application_environment.unsetenv (" DISPLAY" );
105
+ env.unsetenv (key);
134
106
}
107
+ }
135
108
109
+ auto execute_with_environment (std::vector<std::string> const app, Environment& application_environment) -> pid_t
110
+ {
136
111
auto const exec_env = application_environment.exec_env ();
137
112
138
113
std::vector<char const *> exec_args;
@@ -155,12 +130,50 @@ auto miral::launch_app_env(
155
130
sigfillset (&all_signals);
156
131
pthread_sigmask (SIG_UNBLOCK, &all_signals, nullptr );
157
132
158
- // execvpe() isn't listed as being async-signal-safe, but the implementation looks fine and rewriting seems unnecessary
159
- execvpe (exec_args[0 ], const_cast <char *const *>(exec_args.data ()), const_cast <char *const *>(exec_env.data ()));
133
+ // execvpe() isn't listed as being async-signal-safe, but the implementation looks fine and rewriting seems
134
+ // unnecessary
135
+ execvpe (exec_args[0 ], const_cast <char * const *>(exec_args.data ()), const_cast <char * const *>(exec_env.data ()));
160
136
161
137
mir::log_warning (" Failed to execute client (\" %s\" ) error: %s" , exec_args[0 ], strerror (errno));
162
138
exit (EXIT_FAILURE);
163
139
}
164
140
165
141
return pid;
166
142
}
143
+ } // namespace
144
+
145
+ auto miral::launch_app_env (
146
+ std::vector<std::string> const & app,
147
+ mir::optional_value<std::string> const & wayland_display,
148
+ mir::optional_value<std::string> const & x11_display,
149
+ miral::AppEnvironment const & app_env) -> pid_t
150
+ {
151
+ Environment application_environment;
152
+
153
+ for (auto const & [key, value]: app_env)
154
+ assign_or_unset (application_environment, key, value);
155
+
156
+ assign_or_unset (application_environment, " WAYLAND_DISPLAY" , wayland_display); // configure Wayland socket
157
+ assign_or_unset (application_environment, " DISPLAY" , x11_display); // configure X11 socket
158
+
159
+ return execute_with_environment (app, application_environment);
160
+ }
161
+
162
+ auto miral::launch_app_env (
163
+ std::vector<std::string> const & app,
164
+ mir::optional_value<std::string> const & wayland_display,
165
+ mir::optional_value<std::string> const & x11_display,
166
+ std::optional<std::string> const & xdg_activation_token,
167
+ miral::AppEnvironment const & app_env) -> pid_t
168
+ {
169
+ Environment application_environment;
170
+
171
+ for (auto const & [key, value]: app_env)
172
+ assign_or_unset (application_environment, key, value);
173
+
174
+ assign_or_unset (application_environment, " WAYLAND_DISPLAY" , wayland_display); // configure Wayland socket
175
+ assign_or_unset (application_environment, " DISPLAY" , x11_display); // configure X11 socket
176
+ assign_or_unset (application_environment, " XDG_ACTIVATION_TOKEN" , xdg_activation_token);
177
+
178
+ return execute_with_environment (app, application_environment);
179
+ }
0 commit comments