| 1 |
/* |
| 2 |
* This file is part of budgie-desktop |
| 3 |
* |
| 4 |
* Copyright Budgie Desktop Developers |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
*/ |
| 11 |
|
| 12 |
public class UsageMonitorRavenPlugin : Budgie.RavenPlugin, Peas.ExtensionBase { |
| 13 |
public Budgie.RavenWidget new_widget_instance(string uuid, GLib.Settings? settings) { |
| 14 |
return new UsageMonitorRavenWidget(uuid, settings); |
| 15 |
} |
| 16 |
|
| 17 |
public bool supports_settings() { |
| 18 |
return true; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public class UsageMonitorRavenWidget : Budgie.RavenWidget { |
| 23 |
private Gtk.Revealer? content_revealer = null; |
| 24 |
private UsageMonitorRow? cpu = null; |
| 25 |
private UsageMonitorRow? ram = null; |
| 26 |
private UsageMonitorRow? swap = null; |
| 27 |
private ProcStatContents? prev = null; |
| 28 |
|
| 29 |
private uint timeout_id = 0; |
| 30 |
|
| 31 |
public UsageMonitorRavenWidget(string uuid, GLib.Settings? settings) { |
| 32 |
initialize(uuid, settings); |
| 33 |
|
| 34 |
var main_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); |
| 35 |
add(main_box); |
| 36 |
|
| 37 |
var header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0); |
| 38 |
header.get_style_context().add_class("raven-header"); |
| 39 |
main_box.add(header); |
| 40 |
|
| 41 |
var icon = new Gtk.Image.from_icon_name("utilities-system-monitor-symbolic", Gtk.IconSize.MENU); |
| 42 |
icon.margin = 4; |
| 43 |
icon.margin_start = 12; |
| 44 |
icon.margin_end = 10; |
| 45 |
header.add(icon); |
| 46 |
|
| 47 |
var header_label = new Gtk.Label(_("Usage Monitor")); |
| 48 |
header.add(header_label); |
| 49 |
|
| 50 |
var content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); |
| 51 |
content.get_style_context().add_class("raven-background"); |
| 52 |
|
| 53 |
content_revealer = new Gtk.Revealer(); |
| 54 |
content_revealer.add(content); |
| 55 |
content_revealer.reveal_child = true; |
| 56 |
main_box.add(content_revealer); |
| 57 |
|
| 58 |
var header_reveal_button = new Gtk.Button.from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU); |
| 59 |
header_reveal_button.get_style_context().add_class("flat"); |
| 60 |
header_reveal_button.get_style_context().add_class("expander-button"); |
| 61 |
header_reveal_button.margin = 4; |
| 62 |
header_reveal_button.valign = Gtk.Align.CENTER; |
| 63 |
header_reveal_button.clicked.connect(() => { |
| 64 |
content_revealer.reveal_child = !content_revealer.child_revealed; |
| 65 |
var image = (Gtk.Image?) header_reveal_button.get_image(); |
| 66 |
if (content_revealer.reveal_child) { |
| 67 |
image.set_from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU); |
| 68 |
} else { |
| 69 |
image.set_from_icon_name("pan-end-symbolic", Gtk.IconSize.MENU); |
| 70 |
} |
| 71 |
}); |
| 72 |
header.pack_end(header_reveal_button, false, false, 0); |
| 73 |
|
| 74 |
var rows = new Gtk.Grid(); |
| 75 |
rows.hexpand = true; |
| 76 |
rows.margin_start = 12; |
| 77 |
rows.margin_end = 12; |
| 78 |
rows.margin_top = 8; |
| 79 |
rows.margin_bottom = 8; |
| 80 |
rows.set_column_spacing(8); |
| 81 |
content.add(rows); |
| 82 |
|
| 83 |
cpu = new UsageMonitorRow(_("CPU"), 0); |
| 84 |
rows.attach(cpu.label, 0, cpu.index, 1, 1); |
| 85 |
rows.attach(cpu.bar, 1, cpu.index, 1, 1); |
| 86 |
rows.attach(cpu.percentage, 2, cpu.index, 1, 1); |
| 87 |
|
| 88 |
ram = new UsageMonitorRow(_("RAM"), 1); |
| 89 |
rows.attach(ram.label, 0, ram.index, 1, 1); |
| 90 |
rows.attach(ram.bar, 1, ram.index, 1, 1); |
| 91 |
rows.attach(ram.percentage, 2, ram.index, 1, 1); |
| 92 |
|
| 93 |
swap = new UsageMonitorRow(_("Swap"), 2); |
| 94 |
rows.attach(swap.label, 0, swap.index, 1, 1); |
| 95 |
rows.attach(swap.bar, 1, swap.index, 1, 1); |
| 96 |
rows.attach(swap.percentage, 2, swap.index, 1, 1); |
| 97 |
|
| 98 |
show_all(); |
| 99 |
|
| 100 |
settings.changed.connect(settings_updated); |
| 101 |
settings_updated("show-swap-usage"); |
| 102 |
|
| 103 |
update_cpu(); |
| 104 |
update_ram_and_swap(); |
| 105 |
|
| 106 |
raven_expanded.connect((expanded) => { |
| 107 |
if (!expanded && timeout_id != 0) { |
| 108 |
Source.remove(timeout_id); |
| 109 |
timeout_id = 0; |
| 110 |
} else if (expanded && timeout_id == 0) { |
| 111 |
timeout_id = Timeout.add(1000, () => { |
| 112 |
update_cpu(); |
| 113 |
update_ram_and_swap(); |
| 114 |
return GLib.Source.CONTINUE; |
| 115 |
}); |
| 116 |
} |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
private void settings_updated(string key) { |
| 121 |
if (key == "show-swap-usage") { |
| 122 |
var should_show = get_instance_settings().get_boolean(key); |
| 123 |
swap.stay_hidden = !should_show; |
| 124 |
|
| 125 |
if (should_show) swap.show(); else swap.hide(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
private void update_cpu() { |
| 130 |
ProcStatContents? stat = read_proc_stat(); |
| 131 |
|
| 132 |
if (prev != null && stat != null) { |
| 133 |
float total_cpu_usage = (float) (stat.busy - prev.busy) / (float) (stat.total - prev.total); |
| 134 |
cpu.update(total_cpu_usage); |
| 135 |
} |
| 136 |
|
| 137 |
prev = stat; |
| 138 |
} |
| 139 |
|
| 140 |
private void update_ram_and_swap() { |
| 141 |
ProcMeminfoContents? meminfo = read_proc_meminfo(); |
| 142 |
|
| 143 |
if (meminfo == null) { |
| 144 |
ram.hide(); |
| 145 |
swap.hide(); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if (meminfo.swap_total > 0 && !swap.stay_hidden) { |
| 150 |
var swap_used = meminfo.swap_total - meminfo.swap_free - meminfo.swap_cached; |
| 151 |
swap.update((float) swap_used / (float) meminfo.swap_total); |
| 152 |
} else { |
| 153 |
swap.hide(); |
| 154 |
} |
| 155 |
|
| 156 |
if (meminfo.mem_total > 0) { |
| 157 |
var mem_used = meminfo.mem_total - meminfo.mem_available; |
| 158 |
ram.update((float) mem_used / (float) meminfo.mem_total); |
| 159 |
} else { |
| 160 |
ram.hide(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
private ProcStatContents? read_proc_stat() { |
| 165 |
var stat_file = File.new_for_path("/proc/stat"); |
| 166 |
if (!stat_file.query_exists()) return null; |
| 167 |
|
| 168 |
try { |
| 169 |
var input_stream = new DataInputStream(stat_file.read()); |
| 170 |
|
| 171 |
string line; |
| 172 |
while ((line = input_stream.read_line()) != null) { |
| 173 |
if (!line.has_prefix("cpu ")) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
|
| 177 |
ulong user = 0; |
| 178 |
ulong nice = 0; |
| 179 |
ulong system = 0; |
| 180 |
ulong idle = 0; |
| 181 |
ulong iowait = 0; |
| 182 |
ulong irq = 0; |
| 183 |
ulong softirq = 0; |
| 184 |
|
| 185 |
int read = line.scanf( |
| 186 |
"%*s %lu %lu %lu %lu %lu %lu %lu", |
| 187 |
&user, &nice, &system, &idle, &iowait, &irq, &softirq |
| 188 |
); |
| 189 |
|
| 190 |
if (read == 7) { |
| 191 |
ProcStatContents? contents = ProcStatContents(); |
| 192 |
contents.total = user + nice + system + idle + iowait + irq + softirq; |
| 193 |
contents.busy = contents.total - idle - iowait; |
| 194 |
return contents; |
| 195 |
} |
| 196 |
} |
| 197 |
} catch (Error e) {} |
| 198 |
|
| 199 |
return null; |
| 200 |
} |
| 201 |
|
| 202 |
private ProcMeminfoContents? read_proc_meminfo() { |
| 203 |
var meminfo_file = File.new_for_path("/proc/meminfo"); |
| 204 |
if (!meminfo_file.query_exists()) { |
| 205 |
return null; |
| 206 |
} |
| 207 |
|
| 208 |
try { |
| 209 |
var input_stream = new DataInputStream(meminfo_file.read()); |
| 210 |
|
| 211 |
var contents = ProcMeminfoContents(); |
| 212 |
string line; |
| 213 |
while ((line = input_stream.read_line()) != null) { |
| 214 |
string label = ""; |
| 215 |
ulong value = -1; |
| 216 |
|
| 217 |
line.scanf("%s %lu", label, &value); |
| 218 |
|
| 219 |
if (label == "MemTotal:") { |
| 220 |
contents.mem_total = value; |
| 221 |
} else if (label == "MemAvailable:") { |
| 222 |
contents.mem_available = value; |
| 223 |
} else if (label == "SwapTotal:") { |
| 224 |
contents.swap_total = value; |
| 225 |
} else if (label == "SwapFree:") { |
| 226 |
contents.swap_free = value; |
| 227 |
} else if (label == "SwapCached:") { |
| 228 |
contents.swap_cached = value; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return contents; |
| 233 |
} catch (Error e) { |
| 234 |
return null; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
public override Gtk.Widget build_settings_ui() { |
| 239 |
return new UsageMonitorRavenWidgetSettings(get_instance_settings()); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
[GtkTemplate (ui="/org/buddiesofbudgie/budgie-desktop/raven/widget/UsageMonitor/settings.ui")] |
| 244 |
public class UsageMonitorRavenWidgetSettings : Gtk.Grid { |
| 245 |
[GtkChild] |
| 246 |
private unowned Gtk.Switch? switch_show_swap_usage; |
| 247 |
|
| 248 |
public UsageMonitorRavenWidgetSettings(Settings? settings) { |
| 249 |
settings.bind("show-swap-usage", switch_show_swap_usage, "active", SettingsBindFlags.DEFAULT); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private struct ProcMeminfoContents { |
| 254 |
ulong mem_total; |
| 255 |
ulong mem_available; |
| 256 |
ulong swap_total; |
| 257 |
ulong swap_free; |
| 258 |
ulong swap_cached; |
| 259 |
} |
| 260 |
|
| 261 |
private struct ProcStatContents { |
| 262 |
ulong total; |
| 263 |
ulong busy; |
| 264 |
} |
| 265 |
|
| 266 |
private class UsageMonitorRow { |
| 267 |
public Gtk.Label label; |
| 268 |
public Gtk.LevelBar bar; |
| 269 |
public Gtk.Label percentage; |
| 270 |
public int index; |
| 271 |
public bool stay_hidden = false; |
| 272 |
|
| 273 |
public UsageMonitorRow(string name, int index) { |
| 274 |
this.index = index; |
| 275 |
|
| 276 |
label = new Gtk.Label(null); |
| 277 |
label.xalign = 0.0f; |
| 278 |
label.width_chars = 5; |
| 279 |
label.set_markup(name); |
| 280 |
|
| 281 |
bar = new Gtk.LevelBar(); |
| 282 |
bar.name="usagemonitorlevel"; |
| 283 |
bar.add_offset_value("full", 0.8); |
| 284 |
bar.add_offset_value("high", 0.9); |
| 285 |
bar.add_offset_value("low", 1.0); |
| 286 |
bar.valign = Gtk.Align.CENTER; |
| 287 |
bar.halign = Gtk.Align.FILL; |
| 288 |
bar.margin_top = 6; |
| 289 |
bar.margin_bottom = 6; |
| 290 |
bar.hexpand = true; |
| 291 |
bar.set_size_request(-1, 10); |
| 292 |
|
| 293 |
try { |
| 294 |
// alot of themes set the min-width for a level-bar - which isn't great since it |
| 295 |
// limits the granularity - rather than asking each individual theme to change lets |
| 296 |
// override a themes wishes in this very specific use-case |
| 297 |
var css = new Gtk.CssProvider (); |
| 298 |
css.load_from_data (""" |
| 299 |
levelbar#usagemonitorlevel trough block {min-width: 0px; } |
| 300 |
"""); |
| 301 |
Gtk.StyleContext.add_provider_for_screen ( |
| 302 |
Gdk.Screen.get_default (), |
| 303 |
css, |
| 304 |
Gtk.STYLE_PROVIDER_PRIORITY_USER |
| 305 |
); |
| 306 |
} catch (Error e) { |
| 307 |
warning("Could not load levelbar CSS %s", e.message); |
| 308 |
} |
| 309 |
|
| 310 |
percentage = new Gtk.Label(null); |
| 311 |
percentage.xalign = 1.0f; |
| 312 |
percentage.width_chars = 4; |
| 313 |
percentage.set_markup("<span size='small'>0%</span>"); |
| 314 |
} |
| 315 |
|
| 316 |
public void update(float new_value) { |
| 317 |
bar.value = new_value; |
| 318 |
bar.queue_draw(); |
| 319 |
percentage.set_markup("<span size='small'>%.0f%%</span>".printf(new_value * 100)); |
| 320 |
show(); |
| 321 |
} |
| 322 |
|
| 323 |
public void show() { |
| 324 |
if (stay_hidden) return; |
| 325 |
|
| 326 |
label.show(); |
| 327 |
bar.show(); |
| 328 |
percentage.show(); |
| 329 |
} |
| 330 |
|
| 331 |
public void hide() { |
| 332 |
label.hide(); |
| 333 |
bar.hide(); |
| 334 |
percentage.hide(); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
[ModuleInit] |
| 339 |
public void peas_register_types(TypeModule module) { |
| 340 |
// boilerplate - all modules need this |
| 341 |
var objmodule = module as Peas.ObjectModule; |
| 342 |
objmodule.register_extension_type(typeof(Budgie.RavenPlugin), typeof(UsageMonitorRavenPlugin)); |
| 343 |
} |