Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gtk Application for auto-cpufreq #486

Merged
merged 14 commits into from
Aug 15, 2023
Merged
23 changes: 19 additions & 4 deletions auto-cpufreq-installer
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ function install {
python3 setup.py install --record files.txt
mkdir -p /usr/local/share/auto-cpufreq/
cp -r scripts/ /usr/local/share/auto-cpufreq/
cp -r images/ /usr/local/share/auto-cpufreq/
cp images/icon.png /usr/share/pixmaps/auto-cpufreq.png
cp scripts/org.auto-cpufreq.pkexec.policy /usr/share/polkit-1/actions

# this is necessary since we need this script before we can run auto-cpufreq itself
cp scripts/auto-cpufreq-venv-wrapper /usr/local/bin/auto-cpufreq
cp scripts/start_app /usr/local/bin/auto-cpufreq-gtk
chmod a+x /usr/local/bin/auto-cpufreq
chmod a+x /usr/local/bin/auto-cpufreq-gtk

desktop-file-install --dir=/usr/share/applications scripts/auto-cpufreq-gtk.desktop
update-desktop-database /usr/share/applications
}

# First argument is the distro
Expand Down Expand Up @@ -140,7 +148,7 @@ function tool_install {
separator
if [ -f /etc/debian_version ]; then
detected_distro "Debian based"
apt install python3-dev python3-pip python3-venv python3-setuptools dmidecode -y
apt install python3-dev python3-pip python3-venv python3-setuptools dmidecode libgirepository1.0-dev libcairo2-dev -y
completed
complete_msg
elif [ -f /etc/redhat-release ]; then
Expand Down Expand Up @@ -170,12 +178,12 @@ elif [ -f /etc/os-release ];then
opensuse)
detected_distro "OpenSUSE"
echo -e "\nDetected an OpenSUSE distribution\n\nSetting up Python environment\n"
zypper install -y python38 python3-pip python3-setuptools python3-devel gcc dmidecode
zypper install -y python38 python3-pip python3-setuptools python3-devel gcc dmidecode gobject-introspection-devel python3-cairo-devel
completed
;;
arch|manjaro|endeavouros|garuda|artix)
detected_distro "Arch Linux based"
pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode
pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode gobject-introspection
completed
;;
void)
Expand Down Expand Up @@ -208,12 +216,15 @@ function tool_remove {

tool_proc_rm="/usr/local/bin/auto-cpufreq --remove"
wrapper_script="/usr/local/bin/auto-cpufreq"
gui_wrapper_script="/usr/local/bin/auto-cpufreq-gtk"
unit_file="/etc/systemd/system/auto-cpufreq.service"
venv_path="/opt/auto-cpufreq"

cpufreqctl="/usr/local/bin/cpufreqctl.auto-cpufreq"
cpufreqctl_old="/usr/bin/cpufreqctl.auto-cpufreq"

desktop_file="/usr/share/applications/auto-cpufreq-gtk.desktop"

# stop any running auto-cpufreq argument (daemon/live/monitor)
tool_arg_pids=($(pgrep -f "auto-cpufreq --"))
for pid in "${tool_arg_pids[@]}"; do
Expand Down Expand Up @@ -246,10 +257,14 @@ function tool_remove {
[ -f $stats_file ] && rm $stats_file
[ -f $unit_file ] && rm $unit_file
[ -f $wrapper_script ] && rm $wrapper_script

[ -f $gui_wrapper_script ] && rm $gui_wrapper_script

[ -f $cpufreqctl ] && rm $cpufreqctl
[ -f $cpufreqctl_old ] && rm $cpufreqctl_old

[ -f $desktop_file ] && rm $desktop_file
update-desktop-database /usr/share/applications

# remove python virtual environment
rm -rf "${venv_path}"

Expand Down
3 changes: 3 additions & 0 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

warnings.filterwarnings("ignore")

# add path to auto-cpufreq executables for GUI
os.environ["PATH"] += ":/usr/local/bin"

# ToDo:
# - replace get system/CPU load from: psutil.getloadavg() | available in 5.6.2)

Expand Down
Empty file added auto_cpufreq/gui/__init__.py
Empty file.
81 changes: 81 additions & 0 deletions auto_cpufreq/gui/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import gi

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

from gi.repository import Gtk, GLib, Gdk, Gio, GdkPixbuf

import os
import sys

sys.path.append("../")
from auto_cpufreq.core import is_running
from auto_cpufreq.gui.objects import RadioButtonView, SystemStatsLabel, CPUFreqStatsLabel, CurrentGovernorBox, DropDownMenu, DaemonNotRunningView

CSS_FILE = "/usr/local/share/auto-cpufreq/scripts/style.css"

HBOX_PADDING = 20

class ToolWindow(Gtk.Window):
def __init__(self):
super().__init__(title="auto-cpufreq")
self.set_default_size(600, 480)
self.set_border_width(10)
self.set_resizable(False)
self.load_css()
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename="/usr/local/share/auto-cpufreq/images/icon.png", width=500, height=500, preserve_aspect_ratio=True)
self.set_icon(pixbuf)
self.build()

def main(self):
# self.vbox_top = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
# self.vbox_top.set_valign(Gtk.Align.CENTER)
# self.vbox_top.set_halign(Gtk.Align.CENTER)
#self.add(self.vbox_top)

# Main HBOX
self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=HBOX_PADDING)

self.systemstats = SystemStatsLabel()
self.hbox.pack_start(self.systemstats, False, False, 0)
self.add(self.hbox)

self.vbox_right = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=52)

self.menu = DropDownMenu(self)
self.hbox.pack_end(self.menu, False, False, 0)

self.currentgovernor = CurrentGovernorBox()
self.vbox_right.pack_start(self.currentgovernor, False, False, 0)
self.vbox_right.pack_start(RadioButtonView(), False, False, 0)

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

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


GLib.timeout_add_seconds(5, self.refresh)

def daemon_not_running(self):
self.box = DaemonNotRunningView(self)
self.add(self.box)

def build(self):
if is_running("auto-cpufreq", "--daemon"):
self.main()
else:
self.daemon_not_running()

def load_css(self):
screen = Gdk.Screen.get_default()
self.gtk_provider = Gtk.CssProvider()
self.gtk_context = Gtk.StyleContext()
self.gtk_context.add_provider_for_screen(screen, self.gtk_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
self.gtk_provider.load_from_file(Gio.File.new_for_path(CSS_FILE))

def refresh(self):
self.systemstats.refresh()
self.currentgovernor.refresh()
self.cpufreqstats.refresh()
return True

Loading