Skip to content

Commit

Permalink
several improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shadeyg56 committed Feb 4, 2023
1 parent f9f7170 commit 136b449
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 58 deletions.
62 changes: 4 additions & 58 deletions auto_cpufreq/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,12 @@
from gi.repository import Gtk
from gi.repository import GLib

import sys
import os

sys.path.append("../../")
from auto_cpufreq.core import sysinfo, distro_info, getoutput, set_override

from io import StringIO
from objects import RadioButtonView, SystemStatsLabel, CPUFreqStatsLabel, CurrentGovernorBox

HBOX_PADDING = 20

class RadioButtonView(Gtk.Box):
def __init__(self):
super().__init__()
self.set_valign(Gtk.Align.START)

default = Gtk.RadioButton.new_with_label_from_widget(None, "Default")
default.connect("toggled", self.on_button_toggled, "reset")
powersave = Gtk.RadioButton.new_with_label_from_widget(default, "Powersave")
powersave.connect("toggled", self.on_button_toggled, "powersave")
performance = Gtk.RadioButton.new_with_label_from_widget(default, "Performance")
performance.connect("toggled", self.on_button_toggled, "performance")

self.pack_start(default, False, False, 0)
self.pack_start(powersave, False, False, 0)
self.pack_start(performance, False, False, 0)

def on_button_toggled(self, button, override):
if button.get_active():
set_override(override)


class CurrentGovernorBox(Gtk.Box):
def __init__(self):
super().__init__(spacing=60)

self.static = Gtk.Label(label="Current Governor")
self.governor = Gtk.Label(label=getoutput("cpufreqctl.auto-cpufreq --governor").strip().split(" ")[0])

self.pack_start(self.static, False, False, 0)
self.pack_start(self.governor, False, False, 0)

def refresh(self):
self.governor.set_label(getoutput("cpufreqctl.auto-cpufreq --governor").strip().split(" ")[0])

class SystemStatsLabel(Gtk.Label):
def __init__(self):
super().__init__()

self.update()

def update(self):
# change stdout and store label text to file-like object
old_stdout = sys.stdout
text = StringIO()
sys.stdout = text
sysinfo()
distro_info()
self.set_label(text.getvalue())
sys.stdout = old_stdout

def refresh(self):
self.update()

class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="auto-cpufreq")
Expand All @@ -92,6 +35,9 @@ def __init__(self):
self.vbox.pack_start(self.currentgovernor, False, False, 0)
self.vbox.pack_start(RadioButtonView(), False, False, 0)

self.cpufreqstats = CPUFreqStatsLabel()
self.vbox.pack_start(self.cpufreqstats, False, False, 0)

self.hbox.pack_start(self.vbox, False, False, 0)

GLib.timeout_add_seconds(2, self.refresh)
Expand Down
117 changes: 117 additions & 0 deletions auto_cpufreq/gui/objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import gi

gi.require_version("Gtk", "3.0")

from gi.repository import Gtk

import sys
import os

sys.path.append("../../")
from auto_cpufreq.core import sysinfo, distro_info, getoutput, set_override, get_override

from io import StringIO

if os.getenv("PKG_MARKER") == "SNAP":
auto_cpufreq_stats_path = "/var/snap/auto-cpufreq/current/auto-cpufreq.stats"
else:
auto_cpufreq_stats_path = "/var/run/auto-cpufreq.stats"


def get_stats():
if os.path.isfile(auto_cpufreq_stats_path):
with open(auto_cpufreq_stats_path, "r") as file:
stats = [line for line in (file.readlines() [-50:])]
return "".join(stats)


class RadioButtonView(Gtk.Box):
def __init__(self):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=5)
self.set_valign(Gtk.Align.START)

self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)

self.label = Gtk.Label("Governor Override")

self.default = Gtk.RadioButton.new_with_label_from_widget(None, "Default")
self.default.connect("toggled", self.on_button_toggled, "reset")
self.powersave = Gtk.RadioButton.new_with_label_from_widget(self.default, "Powersave")
self.powersave.connect("toggled", self.on_button_toggled, "powersave")
self.performance = Gtk.RadioButton.new_with_label_from_widget(self.default, "Performance")
self.performance.connect("toggled", self.on_button_toggled, "performance")

self.set_selected()

self.hbox.pack_start(self.default, False, False, 0)
self.hbox.pack_start(self.powersave, False, False, 0)
self.hbox.pack_start(self.performance, False, False, 0)

self.pack_start(self.label, False, False, 0)
self.pack_start(self.hbox, False, False, 0)

def on_button_toggled(self, button, override):
if button.get_active():
set_override(override)

def set_selected(self):
override = get_override()
match override:
case "powersave":
self.powersave.set_active(True)
case "performance":
self.performance.set_active(True)
case "default":
self.default.set_active(True)


class CurrentGovernorBox(Gtk.Box):
def __init__(self):
super().__init__(spacing=60)

self.static = Gtk.Label(label="Current Governor")
self.governor = Gtk.Label(label=getoutput("cpufreqctl.auto-cpufreq --governor").strip().split(" ")[0])

self.pack_start(self.static, False, False, 0)
self.pack_start(self.governor, False, False, 0)

def refresh(self):
self.governor.set_label(getoutput("cpufreqctl.auto-cpufreq --governor").strip().split(" ")[0])

class SystemStatsLabel(Gtk.Label):
def __init__(self):
super().__init__()

self.update()

def update(self):
# change stdout and store label text to file-like object
old_stdout = sys.stdout
text = StringIO()
sys.stdout = text
sysinfo()
distro_info()
self.set_label(text.getvalue())
sys.stdout = old_stdout

def refresh(self):
self.update()

class CPUFreqStatsLabel(Gtk.Label):
def __init__(self):
super().__init__()

self.update()

def update(self):
stats = get_stats().split("\n")
start = None
for i, line in enumerate(stats):
if line == ("-" * 28 + " CPU frequency scaling " + "-" * 28):
start = i
break
if start is not None:
del stats[:i]
del stats[-4:]
self.set_label("\n".join(stats))

0 comments on commit 136b449

Please sign in to comment.