Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions include/AGraph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include <glibmm/markup.h>
#include <gtkmm/label.h>
#include <json/json.h>

#include <deque>
#include <vector>

#include "AModule.hpp"

namespace waybar {

enum class GraphType { LINE, BAR, GAUGE };

class AGraph : public AModule {
public:
AGraph(const Json::Value &, const std::string &, const std::string &, uint16_t interval = 0,
bool enable_click = false, bool enable_scroll = false);
virtual ~AGraph() = default;
auto update() -> void override;

protected:
Gtk::DrawingArea graph_;
std::deque<int> values_;
uint16_t datapoints_ = 20;
GraphType graph_type_ = GraphType::LINE;

void addValue(const int n);

const std::chrono::seconds interval_;

bool onDraw(const Cairo::RefPtr<Cairo::Context> &cr);

std::map<std::string, GtkMenuItem *> submenus_;
std::map<std::string, std::string> menuActionsMap_;
static void handleGtkMenuEvent(GtkMenuItem *menuitem, gpointer data);

private:
void drawFilledArea(const Cairo::RefPtr<Cairo::Context> &cr,
const std::vector<std::pair<double, double>> &points, double height,
const Gdk::RGBA &bg_color);

void drawLine(const Cairo::RefPtr<Cairo::Context> &cr,
const std::vector<std::pair<double, double>> &points, const Gdk::RGBA &fg_color);

void drawPath(const Cairo::RefPtr<Cairo::Context> &cr,
const std::vector<std::pair<double, double>> &points);

void drawBars(const Cairo::RefPtr<Cairo::Context> &cr,
double width, double height, int current_value,
const Gdk::RGBA &fg_color);

void drawGauge(const Cairo::RefPtr<Cairo::Context> &cr, double width, double height,
int current_value, const Gdk::RGBA &fg_color);
};

} // namespace waybar
32 changes: 32 additions & 0 deletions include/modules/cpu_graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <fmt/format.h>

#include <cstdint>
#include <fstream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

#include "AGraph.hpp"
#include "util/sleeper_thread.hpp"

namespace waybar::modules {

class CpuGraph : public AGraph {
public:
CpuGraph(const std::string&, const Json::Value&);
virtual ~CpuGraph() = default;
auto update() -> void override;

private:
static constexpr const char *MODERATE_CLASS = "cpu-moderate";
static constexpr const char *HIGH_CLASS = "cpu-high";
static constexpr const char *INTENSIVE_CLASS = "cpu-intensive";

std::vector<std::tuple<size_t, size_t>> prev_times_;
util::SleeperThread thread_;
};

} // namespace waybar::modules
49 changes: 49 additions & 0 deletions include/modules/custom_graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <fmt/format.h>

#include <csignal>
#include <string>

#include "AGraph.hpp"
#include "util/command.hpp"
#include "util/json.hpp"
#include "util/sleeper_thread.hpp"

namespace waybar::modules {

class CustomGraph : public AGraph {
public:
CustomGraph(const std::string&, const std::string&, const Json::Value&, const std::string&);
virtual ~CustomGraph();
auto update() -> void override;
void refresh(int /*signal*/) override;

private:
void delayWorker();
void continuousWorker();
void waitingWorker();
void parseOutputRaw();
void parseOutputJson();
void handleEvent();
bool handleScroll(GdkEventScroll* e) override;
bool handleToggle(GdkEventButton* const& e) override;

const std::string name_;
const std::string output_name_;
std::string text_;
std::string id_;
std::string alt_;
std::string tooltip_;
const bool tooltip_format_enabled_;
std::vector<std::string> class_;
int percentage_;
FILE* fp_;
int pid_;
util::command::res output_;
util::JsonParser parser_;

util::SleeperThread thread_;
};

} // namespace waybar::modules
80 changes: 80 additions & 0 deletions man/waybar-cpu-graph.5.scd
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
waybar-cpu-graph(5)

# NAME

waybar - cpu graph module

# DESCRIPTION

The *cpu graph* module displays a line graph with the CPU utilization.

# CONFIGURATION

*interval*: ++
typeof: integer ++
default: 10 ++
The interval in which the information gets polled.

*width*: ++
typeof: integer ++
The length in pixels the module should display.

*datapoints*: ++
typeof: integer ++
How many data points to show.

*on-click*: ++
typeof: string ++
Command to execute when clicked on the module.

*on-click-middle*: ++
typeof: string ++
Command to execute when middle-clicked on the module using mousewheel.

*on-click-right*: ++
typeof: string ++
Command to execute when you right-click on the module.

*on-update*: ++
typeof: string ++
Command to execute when the module is updated.

*on-scroll-up*: ++
typeof: string ++
Command to execute when scrolling up on the module.

*on-scroll-down*: ++
typeof: string ++
Command to execute when scrolling down on the module.

*smooth-scrolling-threshold*: ++
typeof: double ++
Threshold to be used when scrolling.

*tooltip*: ++
typeof: bool ++
default: true ++
Option to disable tooltip on hover.

*expand*: ++
typeof: bool ++
default: false ++
Enables this module to consume all left over space dynamically.

# EXAMPLES

Basic configuration:

```
"cpu_graph": {
"interval": 2,
"width": 10
}
```

# STYLE

- *#cpu_graph*
- *.cpu-intensive*
- *.cpu-high*
- *.cpu-moderate*
Loading