plasma-show-stdout
A KDE Plasma 6 widget that displays stdout of shell scripts to the panel
Loading...
Searching...
No Matches
plasma-show-stdout.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) <2026> Anthony J. Greenberg
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 *
8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 *
10 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
14 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
15 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
16 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
17 * THE POSSIBILITY OF SUCH DAMAGE.
18 */
19
21
28
29#pragma once
30
31#include <QObject>
32#include <QString>
33#include <QVariantList>
34#include <QQmlExtensionPlugin>
35
36#include <chrono>
37#include <condition_variable>
38#include <cstddef>
39#include <cstdint>
40#include <deque>
41#include <filesystem>
42#include <memory>
43#include <mutex>
44#include <string>
45#include <thread>
46#include <vector>
47
48namespace PSSspace {
57 struct ModuleSpec {
59 enum class Kind { Timed, Signal };
63 std::filesystem::path script;
65 std::chrono::duration<uint32_t> interval{};
69 size_t outputLimit{300};
70 };
71
80 class ScriptOutput : public QObject {
81 // macros that define private declarations
82 Q_OBJECT
84 Q_PROPERTY(QString text READ text NOTIFY textChanged)
86 Q_PROPERTY(QString delimiter READ delimiter WRITE setDelimiter NOTIFY delimiterChanged)
88 Q_PROPERTY(int maxSignalOffset READ maxSignalOffset CONSTANT)
90 Q_PROPERTY(qint64 hostPid READ hostPid CONSTANT)
92 Q_PROPERTY(QString hostName READ hostName CONSTANT)
93 public:
103 explicit ScriptOutput(QObject *parent = nullptr);
113 explicit ScriptOutput(std::vector<ModuleSpec> specs, QObject *parent = nullptr);
115 ~ScriptOutput() override;
120 QString text() const { return text_; }
125 QString delimiter() const { return delimiter_; }
136 void setDelimiter(const QString &delimiter);
146 int maxSignalOffset() const;
157 qint64 hostPid() const;
170 QString hostName() const;
183 Q_INVOKABLE void setModules(const QVariantList &specs);
184 signals:
189 private:
197 struct ModuleSlot {
199 std::condition_variable *changeSignal{nullptr};
201 std::string *outputBuffer{nullptr};
203 QString fragment;
205 std::thread worker;
207 std::thread bridge;
209 std::shared_ptr<bool> execute;
211 std::condition_variable *executionSignal{nullptr};
213 int signalNumber{0};
214 };
215
228 void startModules_(std::vector<ModuleSpec> specs);
237 void stopModules_();
246 void bridgeLoop_(ModuleSlot *slot);
253 void signalWaitLoop_();
259 void rebuildCombinedText_();
260
262 QString text_;
268 QString delimiter_{ QStringLiteral(" | ") };
269
271 std::shared_ptr<std::mutex> mutex_;
273 std::shared_ptr<std::condition_variable> stopSignal_;
275 std::shared_ptr<bool> stop_;
276
283 std::deque<ModuleSlot> slots_;
285 std::thread signalWaitThread_;
294 bool signalsActive_{false};
295 };
296
297} // namespace PSSspace
298
305class ShowStdoutPlugin final : public QQmlExtensionPlugin {
306 // macros that define private declarations
307 Q_OBJECT
308 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
309public:
317 void registerTypes(const char *uri) override;
318};
Q_INVOKABLE void setModules(const QVariantList &specs)
(Re)configure the running modules from QML
QString delimiter
Separator inserted between adjacent module outputs.
Definition plasma-show-stdout.hpp:86
void textChanged()
Change-notification signal for the text property.
ScriptOutput(QObject *parent=nullptr)
QML constructor.
void delimiterChanged()
Change-notification signal for the delimiter property.
QString hostName
Command name of the host process, as pkill matches it (constant).
Definition plasma-show-stdout.hpp:92
void setDelimiter(const QString &delimiter)
Set the delimiter inserted between module outputs.
int maxSignalOffset() const
Largest valid realtime-signal offset.
int maxSignalOffset
Largest valid realtime-signal offset, SIGRTMAX - SIGRTMIN (constant).
Definition plasma-show-stdout.hpp:88
qint64 hostPid
PID of the host process the plugin is loaded into (constant).
Definition plasma-show-stdout.hpp:90
qint64 hostPid() const
PID of the process hosting this plugin.
QString text
Combined, delimiter-joined output of all modules (read-only).
Definition plasma-show-stdout.hpp:84
QString delimiter() const
Delimiter inserted between module outputs.
Definition plasma-show-stdout.hpp:125
QString hostName() const
Command name of the process hosting this plugin.
QML extension plugin registering the ScriptOutput type.
Definition plasma-show-stdout.hpp:305
void registerTypes(const char *uri) override
Register the plugin's QML types.
Inert description of one module.
Definition plasma-show-stdout.hpp:57
std::chrono::duration< uint32_t > interval
Polling interval (Timed modules only).
Definition plasma-show-stdout.hpp:65
size_t outputLimit
Maximum number of output characters to retain.
Definition plasma-show-stdout.hpp:69
Kind kind
Trigger kind.
Definition plasma-show-stdout.hpp:61
int signalNumber
Realtime signal to listen on, e.g. SIGRTMIN+2 (Signal modules only).
Definition plasma-show-stdout.hpp:67
Kind
How the module is triggered.
Definition plasma-show-stdout.hpp:59
std::filesystem::path script
Path to the script to run.
Definition plasma-show-stdout.hpp:63