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-03-07 17:55:38 +01:00
|
|
|
#include "qtcprocess.h"
|
|
|
|
#include "terminalcommand.h"
|
2023-03-14 09:09:55 +01:00
|
|
|
#include "terminalinterface.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;
|
|
|
|
|
|
|
|
return shell.onDevice(deviceRoot);
|
|
|
|
}
|
|
|
|
|
2023-03-07 17:55:38 +01:00
|
|
|
class ExternalTerminalProcessImpl final : public TerminalInterface
|
|
|
|
{
|
|
|
|
class ProcessStubCreator : public StubCreator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProcessStubCreator(ExternalTerminalProcessImpl *interface)
|
|
|
|
: m_interface(interface)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void startStubProcess(const CommandLine &cmd, const ProcessSetupData &) override
|
|
|
|
{
|
2023-03-14 09:09:55 +01:00
|
|
|
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
|
|
|
|
|
2023-03-07 17:55:38 +01:00
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
|
|
|
m_terminalProcess.setCommand(cmd);
|
|
|
|
QObject::connect(&m_terminalProcess, &QtcProcess::done, this, [this] {
|
|
|
|
m_interface->onStubExited();
|
|
|
|
});
|
2023-03-14 09:09:55 +01:00
|
|
|
m_terminalProcess.setCreateConsoleOnWindows(true);
|
|
|
|
m_terminalProcess.setProcessMode(ProcessMode::Writer);
|
2023-03-07 17:55:38 +01:00
|
|
|
m_terminalProcess.start();
|
2023-03-14 09:09:55 +01:00
|
|
|
} else 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-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")
|
|
|
|
.arg(cmd.executable().nativePath())
|
|
|
|
.arg(cmd.arguments())
|
|
|
|
.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-03-14 09:09:55 +01:00
|
|
|
m_terminalProcess.setCommand(
|
|
|
|
{"osascript", {"-e", "tell app \"Terminal\" to activate", "-e", exe}});
|
2023-03-07 17:55:38 +01:00
|
|
|
m_terminalProcess.runBlocking();
|
|
|
|
} else {
|
|
|
|
CommandLine cmdLine = {terminal.command, {terminal.executeArgs}};
|
|
|
|
cmdLine.addCommandLineAsArgs(cmd, CommandLine::Raw);
|
|
|
|
|
|
|
|
m_terminalProcess.setCommand(cmdLine);
|
|
|
|
m_terminalProcess.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternalTerminalProcessImpl *m_interface;
|
|
|
|
QtcProcess m_terminalProcess;
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|