2023-02-23 12:47:39 +01:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
#include "terminalhooks.h"
|
|
|
|
|
|
|
|
#include "filepath.h"
|
2023-05-03 17:05:35 +02:00
|
|
|
#include "process.h"
|
2023-03-07 17:55:38 +01:00
|
|
|
#include "terminalcommand.h"
|
2023-03-14 09:09:55 +01:00
|
|
|
#include "terminalinterface.h"
|
2023-05-04 08:09:40 +02:00
|
|
|
#include "utilstr.h"
|
2023-03-07 17:55:38 +01:00
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
#include <QMutex>
|
2023-03-07 17:55:38 +01:00
|
|
|
#include <QTemporaryFile>
|
2023-02-23 12:47:39 +01:00
|
|
|
|
|
|
|
namespace Utils::Terminal {
|
|
|
|
|
2023-03-01 09:57:43 +01:00
|
|
|
FilePath defaultShellForDevice(const FilePath &deviceRoot)
|
|
|
|
{
|
2023-03-14 09:09:55 +01:00
|
|
|
if (deviceRoot.osType() == OsTypeWindows)
|
|
|
|
return deviceRoot.withNewPath("cmd.exe").searchInPath();
|
2023-03-01 09:57:43 +01:00
|
|
|
|
|
|
|
const Environment env = deviceRoot.deviceEnvironment();
|
|
|
|
FilePath shell = FilePath::fromUserInput(env.value_or("SHELL", "/bin/sh"));
|
|
|
|
|
|
|
|
if (!shell.isAbsolutePath())
|
|
|
|
shell = env.searchInPath(shell.nativePath());
|
|
|
|
|
|
|
|
if (shell.isEmpty())
|
|
|
|
return shell;
|
|
|
|
|
2023-03-29 13:45:42 +02:00
|
|
|
return deviceRoot.withNewMappedPath(shell);
|
2023-03-01 09:57:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-07 17:55:38 +01:00
|
|
|
class ExternalTerminalProcessImpl final : public TerminalInterface
|
|
|
|
{
|
|
|
|
class ProcessStubCreator : public StubCreator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProcessStubCreator(ExternalTerminalProcessImpl *interface)
|
|
|
|
: m_interface(interface)
|
|
|
|
{}
|
|
|
|
|
2023-05-04 08:09:40 +02:00
|
|
|
~ProcessStubCreator() override = default;
|
|
|
|
|
2023-05-04 09:54:24 +02:00
|
|
|
expected_str<qint64> startStubProcess(const ProcessSetupData &setupData) override
|
2023-03-07 17:55:38 +01:00
|
|
|
{
|
2023-03-14 09:09:55 +01:00
|
|
|
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
|
|
|
|
|
2023-05-04 08:09:40 +02:00
|
|
|
if (HostOsInfo::isMacHost() && terminal.command == "Terminal.app") {
|
2023-03-07 17:55:38 +01:00
|
|
|
QTemporaryFile f;
|
|
|
|
f.setAutoRemove(false);
|
|
|
|
f.open();
|
|
|
|
f.setPermissions(QFile::ExeUser | QFile::ReadUser | QFile::WriteUser);
|
|
|
|
f.write("#!/bin/sh\n");
|
2023-05-04 08:09:40 +02:00
|
|
|
f.write(QString("cd %1\n").arg(setupData.m_workingDirectory.nativePath()).toUtf8());
|
2023-03-14 09:09:55 +01:00
|
|
|
f.write("clear\n");
|
2023-03-07 17:55:38 +01:00
|
|
|
f.write(QString("exec '%1' %2\n")
|
2023-05-04 09:54:24 +02:00
|
|
|
.arg(setupData.m_commandLine.executable().nativePath())
|
|
|
|
.arg(setupData.m_commandLine.arguments())
|
2023-03-07 17:55:38 +01:00
|
|
|
.toUtf8());
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
const QString path = f.fileName();
|
|
|
|
const QString exe
|
|
|
|
= QString("tell app \"Terminal\" to do script \"'%1'; rm -f '%1'; exit\"")
|
|
|
|
.arg(path);
|
|
|
|
|
2023-05-04 08:09:40 +02:00
|
|
|
Process process;
|
|
|
|
|
|
|
|
process.setCommand(
|
2023-03-14 09:09:55 +01:00
|
|
|
{"osascript", {"-e", "tell app \"Terminal\" to activate", "-e", exe}});
|
2023-05-04 08:09:40 +02:00
|
|
|
process.runBlocking();
|
|
|
|
|
|
|
|
if (process.exitCode() != 0) {
|
|
|
|
return make_unexpected(Tr::tr("Failed to start terminal process: \"%1\"")
|
|
|
|
.arg(process.errorString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool detached = setupData.m_terminalMode == TerminalMode::Detached;
|
|
|
|
|
|
|
|
Process *process = new Process(detached ? nullptr : this);
|
|
|
|
if (detached)
|
|
|
|
QObject::connect(process, &Process::done, process, &Process::deleteLater);
|
|
|
|
|
|
|
|
QObject::connect(process,
|
|
|
|
&Process::done,
|
|
|
|
m_interface,
|
|
|
|
&ExternalTerminalProcessImpl::onStubExited);
|
|
|
|
|
|
|
|
process->setWorkingDirectory(setupData.m_workingDirectory);
|
|
|
|
|
|
|
|
if constexpr (HostOsInfo::isWindowsHost()) {
|
2023-05-04 09:54:24 +02:00
|
|
|
process->setCommand(setupData.m_commandLine);
|
2023-05-04 08:09:40 +02:00
|
|
|
process->setCreateConsoleOnWindows(true);
|
|
|
|
process->setProcessMode(ProcessMode::Writer);
|
2023-03-07 17:55:38 +01:00
|
|
|
} else {
|
2023-05-04 08:09:40 +02:00
|
|
|
QString extraArgsFromOptions = detached ? terminal.openArgs : terminal.executeArgs;
|
|
|
|
CommandLine cmdLine = {terminal.command, {}};
|
|
|
|
if (!extraArgsFromOptions.isEmpty())
|
|
|
|
cmdLine.addArgs(extraArgsFromOptions, CommandLine::Raw);
|
2023-05-04 09:54:24 +02:00
|
|
|
cmdLine.addCommandLineAsArgs(setupData.m_commandLine, CommandLine::Raw);
|
2023-05-04 08:09:40 +02:00
|
|
|
process->setCommand(cmdLine);
|
|
|
|
}
|
2023-03-07 17:55:38 +01:00
|
|
|
|
2023-05-04 08:09:40 +02:00
|
|
|
process->start();
|
|
|
|
process->waitForStarted();
|
|
|
|
if (process->error() != QProcess::UnknownError) {
|
|
|
|
return make_unexpected(
|
|
|
|
Tr::tr("Failed to start terminal process: \"%1\"").arg(process->errorString()));
|
2023-03-07 17:55:38 +01:00
|
|
|
}
|
2023-05-04 08:09:40 +02:00
|
|
|
|
|
|
|
qint64 pid = process->processId();
|
|
|
|
|
|
|
|
return pid;
|
2023-03-07 17:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ExternalTerminalProcessImpl *m_interface;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
ExternalTerminalProcessImpl() { setStubCreator(new ProcessStubCreator(this)); }
|
|
|
|
};
|
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
class HooksPrivate
|
2023-02-23 12:47:39 +01:00
|
|
|
{
|
2023-03-22 16:54:53 +01:00
|
|
|
public:
|
2023-02-23 12:47:39 +01:00
|
|
|
HooksPrivate()
|
2023-03-22 16:54:53 +01:00
|
|
|
: m_getTerminalCommandsForDevicesHook([] { return QList<NameAndCommandLine>{}; })
|
|
|
|
{
|
|
|
|
auto openTerminal = [](const OpenTerminalParameters ¶meters) {
|
2023-02-23 12:47:39 +01:00
|
|
|
DeviceFileHooks::instance().openTerminal(parameters.workingDirectory.value_or(
|
|
|
|
FilePath{}),
|
|
|
|
parameters.environment.value_or(Environment{}));
|
2023-03-22 16:54:53 +01:00
|
|
|
};
|
|
|
|
auto createProcessInterface = []() { return new ExternalTerminalProcessImpl(); };
|
|
|
|
|
|
|
|
addCallbackSet("External", {openTerminal, createProcessInterface});
|
|
|
|
}
|
|
|
|
|
|
|
|
void addCallbackSet(const QString &name, const Hooks::CallbackSet &callbackSet)
|
|
|
|
{
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
m_callbackSets.push_back(qMakePair(name, callbackSet));
|
|
|
|
|
|
|
|
m_createTerminalProcessInterface
|
|
|
|
= m_callbackSets.back().second.createTerminalProcessInterface;
|
|
|
|
m_openTerminal = m_callbackSets.back().second.openTerminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeCallbackSet(const QString &name)
|
|
|
|
{
|
|
|
|
if (name == "External")
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
m_callbackSets.removeIf([name](const auto &pair) { return pair.first == name; });
|
|
|
|
|
|
|
|
m_createTerminalProcessInterface
|
|
|
|
= m_callbackSets.back().second.createTerminalProcessInterface;
|
|
|
|
m_openTerminal = m_callbackSets.back().second.openTerminal;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hooks::CreateTerminalProcessInterface createTerminalProcessInterface()
|
|
|
|
{
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
return m_createTerminalProcessInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hooks::OpenTerminal openTerminal()
|
|
|
|
{
|
|
|
|
QMutexLocker lk(&m_mutex);
|
|
|
|
return m_openTerminal;
|
|
|
|
}
|
2023-02-23 12:47:39 +01:00
|
|
|
|
2023-02-25 10:47:21 +01:00
|
|
|
Hooks::GetTerminalCommandsForDevicesHook m_getTerminalCommandsForDevicesHook;
|
2023-03-22 16:54:53 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Hooks::OpenTerminal m_openTerminal;
|
|
|
|
Hooks::CreateTerminalProcessInterface m_createTerminalProcessInterface;
|
|
|
|
|
|
|
|
QMutex m_mutex;
|
|
|
|
QList<QPair<QString, Hooks::CallbackSet>> m_callbackSets;
|
2023-02-23 12:47:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Hooks &Hooks::instance()
|
|
|
|
{
|
|
|
|
static Hooks manager;
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hooks::Hooks()
|
|
|
|
: d(new HooksPrivate())
|
|
|
|
{}
|
|
|
|
|
|
|
|
Hooks::~Hooks() = default;
|
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
void Hooks::openTerminal(const OpenTerminalParameters ¶meters) const
|
2023-02-23 12:47:39 +01:00
|
|
|
{
|
2023-03-22 16:54:53 +01:00
|
|
|
d->openTerminal()(parameters);
|
2023-02-23 12:47:39 +01:00
|
|
|
}
|
2023-02-25 10:47:21 +01:00
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
ProcessInterface *Hooks::createTerminalProcessInterface() const
|
2023-02-23 12:47:39 +01:00
|
|
|
{
|
2023-03-22 16:54:53 +01:00
|
|
|
return d->createTerminalProcessInterface()();
|
2023-02-23 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
2023-02-25 10:47:21 +01:00
|
|
|
Hooks::GetTerminalCommandsForDevicesHook &Hooks::getTerminalCommandsForDevicesHook()
|
|
|
|
{
|
|
|
|
return d->m_getTerminalCommandsForDevicesHook;
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:54:53 +01:00
|
|
|
void Hooks::addCallbackSet(const QString &name, const CallbackSet &callbackSet)
|
|
|
|
{
|
|
|
|
d->addCallbackSet(name, callbackSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hooks::removeCallbackSet(const QString &name)
|
|
|
|
{
|
|
|
|
d->removeCallbackSet(name);
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:47:39 +01:00
|
|
|
} // namespace Utils::Terminal
|