diff --git a/auto_cpufreq/gui/app.py b/auto_cpufreq/gui/app.py index 635cd9b7..244086c0 100644 --- a/auto_cpufreq/gui/app.py +++ b/auto_cpufreq/gui/app.py @@ -6,12 +6,18 @@ import os import sys +from threading import Thread 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" +if os.getenv("PKG_MARKER") == "SNAP": + ICON_FILE = "/snap/auto-cpufreq/current/icon.png" + CSS_FILE = "/snap/auto-cpufreq/current/style.css" +else: + ICON_FILE = "/usr/local/share/auto-cpufreq/images/icon.png" + CSS_FILE = "/usr/local/share/auto-cpufreq/scripts/style.css" HBOX_PADDING = 20 @@ -22,15 +28,11 @@ def __init__(self): 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) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=ICON_FILE, 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) @@ -54,14 +56,30 @@ def main(self): self.hbox.pack_start(self.vbox_right, False, False, 0) - GLib.timeout_add_seconds(5, self.refresh) + GLib.timeout_add_seconds(5, self.refresh_in_thread) + + def snap(self): + box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER) + + label = Gtk.Label(label="GUI not available due to Snap package confinement limitations.\nPlease install auto-cpufreq using auto-cpufreq-installer\nVisit the GitHub repo for more info") + label.set_justify(Gtk.Justification.CENTER) + button = Gtk.LinkButton.new_with_label( + uri="https://github.com/AdnanHodzic/auto-cpufreq", + label="GitHub Repo" + ) + + box.pack_start(label, False, False, 0) + box.pack_start(button, False, False, 0) + self.add(box) def daemon_not_running(self): self.box = DaemonNotRunningView(self) self.add(self.box) def build(self): - if is_running("auto-cpufreq", "--daemon"): + if os.getenv("PKG_MARKER") == "SNAP": + self.snap() + elif is_running("auto-cpufreq", "--daemon"): self.main() else: self.daemon_not_running() @@ -73,9 +91,12 @@ def load_css(self): 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): + def refresh_in_thread(self): + Thread(target=self._refresh).start() + return True + + def _refresh(self): self.systemstats.refresh() self.currentgovernor.refresh() self.cpufreqstats.refresh() - return True diff --git a/auto_cpufreq/gui/objects.py b/auto_cpufreq/gui/objects.py index af0901ac..5ecb5d7b 100644 --- a/auto_cpufreq/gui/objects.py +++ b/auto_cpufreq/gui/objects.py @@ -7,6 +7,7 @@ import sys import os import platform as pl +from concurrent.futures import ThreadPoolExecutor sys.path.append("../../") from subprocess import getoutput, run, PIPE @@ -77,9 +78,6 @@ def __init__(self): self.pack_start(self.powersave, True, True, 0) self.pack_start(self.performance, True, True, 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(): if not self.set_by_app: @@ -185,7 +183,11 @@ def _remove_daemon(self, MenuItem, parent): confirm.destroy() if response == Gtk.ResponseType.YES: try: - result = run("pkexec auto-cpufreq --remove", shell=True, stdout=PIPE, stderr=PIPE) + # run in thread to prevent GUI from hanging + with ThreadPoolExecutor() as executor: + kwargs = {"shell": True, "stdout": PIPE, "stderr": PIPE} + future = executor.submit(run, "pkexec auto-cpufreq --remove", **kwargs) + result = future.result() if result.stderr.decode() == PKEXEC_ERROR: raise Exception("Authorization was cancelled") dialog = Gtk.MessageDialog( @@ -215,7 +217,6 @@ def __init__(self, parent): super().__init__(title="About", transient_for=parent) app_version = get_version() self.box = self.get_content_area() - # self.box.set_homogeneous(True) self.box.set_spacing(10) self.add_button("Close", Gtk.ResponseType.CLOSE) self.set_default_size(400, 350) @@ -267,9 +268,16 @@ def __init__(self, parent): def install_daemon(self, button, parent): try: - result = run("pkexec auto-cpufreq --install", shell=True, stdout=PIPE, stderr=PIPE) + # run in thread to prevent GUI from hanging + with ThreadPoolExecutor() as executor: + kwargs = {"shell": True, "stdout": PIPE, "stderr": PIPE} + future = executor.submit(run, "pkexec auto-cpufreq --install", **kwargs) + result = future.result() if result.stderr.decode() == PKEXEC_ERROR: raise Exception("Authorization was cancelled") + # enable for debug. causes issues if kept + # elif result.stderr is not None: + # raise Exception(result.stderr.decode()) dialog = Gtk.MessageDialog( transient_for=parent, message_type=Gtk.MessageType.INFO, diff --git a/setup.py b/setup.py index 84171936..398d9450 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ def read(name): return f.read() # Used for the tar.gz/snap releases -VERSION = "1.9.9" +VERSION = "2.0" setup( name="auto-cpufreq", diff --git a/snap/gui/auto-cpufreq.png b/snap/gui/auto-cpufreq.png new file mode 100644 index 00000000..a1874576 Binary files /dev/null and b/snap/gui/auto-cpufreq.png differ diff --git a/snap/gui/auto-cpufreq_auto-cpufreq.desktop b/snap/gui/auto-cpufreq_auto-cpufreq.desktop new file mode 100644 index 00000000..b149ca0c --- /dev/null +++ b/snap/gui/auto-cpufreq_auto-cpufreq.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Application +Encoding=UTF-8 +Name=auto-cpufreq +Comment=Automatic CPU speed & power optimizer for Linux +Exec=auto-cpufreq.auto-cpufreq-gtk +StartupWMClass=app.py +Terminal=false +Icon=${SNAP}/meta/gui/auto-cpufreq.png +Categories=System; \ No newline at end of file diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index ebe16bc2..cadb6788 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -41,6 +41,10 @@ parts: cpufreqctl.sh: usr/bin/cpufreqctl.auto-cpufreq snapdaemon.sh: usr/bin/snapdaemon + copy-image: + plugin: dump + source: images + plugs: etc-auto-cpufreq-conf: interface: system-files @@ -78,7 +82,6 @@ apps: - cpu-control - system-observe - hardware-observe - - etc-auto-cpufreq-conf environment: LC_ALL: C.UTF-8 LANG: C.UTF-8