feat: basic plugin system

The wayland plugin now uses it.
This commit is contained in:
outfoxxed 2024-02-25 23:47:17 -08:00
parent 6c6272e523
commit 3bd587cfcc
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
8 changed files with 94 additions and 4 deletions

View file

@ -1,5 +1,6 @@
qt_add_executable(quickshell
main.cpp
plugin.cpp
shell.cpp
variants.cpp
rootwrapper.cpp
@ -17,5 +18,3 @@ qt_add_executable(quickshell
)
qt_add_qml_module(quickshell URI QuickShell)
target_link_libraries(quickshell PRIVATE ${QT_DEPS} quickshell-waylandplugin)

View file

@ -8,6 +8,7 @@
#include <qstandardpaths.h>
#include <qstring.h>
#include "plugin.hpp"
#include "rootwrapper.hpp"
int main(int argc, char** argv) {
@ -38,6 +39,8 @@ int main(int argc, char** argv) {
return -1;
}
QuickshellPlugin::initPlugins();
// Base window transparency appears to be additive.
// Use a fully transparent window with a colored rect.
QQuickWindow::setDefaultAlphaBuffer(true);

35
src/core/plugin.cpp Normal file
View file

@ -0,0 +1,35 @@
#include "plugin.hpp"
#include <algorithm>
#include <qvector.h> // NOLINT (what??)
// defined by moc. see below comment.
void qml_register_types_QuickShell(); // NOLINT
static QVector<QuickshellPlugin*> plugins; // NOLINT
void QuickshellPlugin::registerPlugin(QuickshellPlugin& plugin) { plugins.push_back(&plugin); }
void QuickshellPlugin::initPlugins() {
plugins.erase(
std::remove_if(
plugins.begin(),
plugins.end(),
[](QuickshellPlugin* plugin) { return !plugin->applies(); }
),
plugins.end()
);
for (QuickshellPlugin* plugin: plugins) {
plugin->init();
}
// This seems incredibly stupid but it appears qt will not register the module types
// if types are already defined for the module, meaning qmlRegisterType does not work
// unless the module types are already registered.
qml_register_types_QuickShell();
for (QuickshellPlugin* plugin: plugins) {
plugin->registerTypes();
}
}

29
src/core/plugin.hpp Normal file
View file

@ -0,0 +1,29 @@
#pragma once
#include <qcontainerfwd.h>
#include <qfunctionpointer.h>
class QuickshellPlugin {
public:
QuickshellPlugin() = default;
virtual ~QuickshellPlugin() = default;
QuickshellPlugin(QuickshellPlugin&&) = delete;
QuickshellPlugin(const QuickshellPlugin&) = delete;
void operator=(QuickshellPlugin&&) = delete;
void operator=(const QuickshellPlugin&) = delete;
virtual bool applies() { return true; }
virtual void init() {}
virtual void registerTypes() {}
static void registerPlugin(QuickshellPlugin& plugin);
static void initPlugins();
};
// NOLINTBEGIN
#define QS_REGISTER_PLUGIN(clazz) \
[[gnu::constructor]] void qsInitPlugin() { \
static clazz plugin; \
QuickshellPlugin::registerPlugin(plugin); \
}
// NOLINTEND