Skip to content

Commit

Permalink
app no longer needs root to start, only asks when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
shadeyg56 committed Feb 26, 2023
1 parent 3a4fd1d commit 3a8eaaf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
5 changes: 2 additions & 3 deletions auto_cpufreq/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

HBOX_PADDING = 20

class MyWindow(Gtk.Window):
class ToolWindow(Gtk.Window):
def __init__(self):
super().__init__(title="auto-cpufreq")
self.set_default_size(600, 480)
Expand Down Expand Up @@ -80,8 +80,7 @@ def refresh(self):
return True



win = MyWindow()
win = ToolWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
GLib.set_application_name("auto-cpufreq")
Expand Down
19 changes: 14 additions & 5 deletions auto_cpufreq/gui/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import platform as pl

sys.path.append("../../")
from subprocess import getoutput, call
from subprocess import getoutput, run, PIPE
from auto_cpufreq.core import sysinfo, distro_info, set_override, get_override, get_formatted_version, dist_name, deploy_daemon, remove_daemon

from io import StringIO

PKEXEC_ERROR = "Error executing command as another user: Not authorized\n\nThis incident has been reported.\n"

if os.getenv("PKG_MARKER") == "SNAP":
auto_cpufreq_stats_path = "/var/snap/auto-cpufreq/current/auto-cpufreq.stats"
else:
Expand All @@ -32,7 +34,7 @@ def get_version():
return getoutput("echo \(Snap\) $SNAP_VERSION")
# aur package
elif dist_name in ["arch", "manjaro", "garuda"]:
aur_pkg_check = call("pacman -Qs auto-cpufreq > /dev/null", shell=True)
aur_pkg_check = run("pacman -Qs auto-cpufreq > /dev/null", shell=True)
if aur_pkg_check == 1:
return get_formatted_version()
else:
Expand Down Expand Up @@ -77,7 +79,10 @@ def __init__(self):

def on_button_toggled(self, button, override):
if button.get_active():
set_override(override)
result = run(f"pkexec auto-cpufreq --force={override}", shell=True, stdout=PIPE, stderr=PIPE)
if result.stderr.decode() == PKEXEC_ERROR:
self.set_selected()


def set_selected(self):
override = get_override()
Expand Down Expand Up @@ -170,7 +175,9 @@ def _remove_daemon(self, MenuItem, parent):
confirm.destroy()
if response == Gtk.ResponseType.YES:
try:
remove_daemon()
result = run("pkexec auto-cpufreq --remove", shell=True, stdout=PIPE, stderr=PIPE)
if result.stderr.decode() == PKEXEC_ERROR:
raise Exception("Authorization was cancelled")
dialog = Gtk.MessageDialog(
transient_for=parent,
message_type=Gtk.MessageType.INFO,
Expand Down Expand Up @@ -250,7 +257,9 @@ def __init__(self, parent):

def install_daemon(self, button, parent):
try:
deploy_daemon()
result = run("pkexec auto-cpufreq --install", shell=True, stdout=PIPE, stderr=PIPE)
if result.stderr.decode() == PKEXEC_ERROR:
raise Exception("Authorization was cancelled")
dialog = Gtk.MessageDialog(
transient_for=parent,
message_type=Gtk.MessageType.INFO,
Expand Down
6 changes: 3 additions & 3 deletions scripts/org.auto-cpufreq.pkexec.policy
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<allow_inactive>auth_admin</allow_inactive>
<allow_active>auth_admin</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/opt/auto-cpufreq/venv/bin/python</annotate>
<annotate key="org.freedesktop.policykit.exec.argv1">/opt/auto-cpufreq/venv/bin/app.py</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
<annotate key="org.freedesktop.policykit.exec.path">/opt/auto-cpufreq/venv/bin/auto-cpufreq</annotate>
<!-- <annotate key="org.freedesktop.policykit.exec.argv1">/opt/auto-cpufreq/venv/bin/auto-cpufreq</annotate> -->
<!-- <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate> -->
</action>
</policyconfig>
20 changes: 11 additions & 9 deletions scripts/start_app
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ venv_dir=/opt/auto-cpufreq/venv
. "${venv_dir}/bin/activate"
python_command="${venv_dir}/bin/python ${venv_dir}/bin/app.py"

if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then
# necessary for running on wayland
xhost +SI:localuser:root
pkexec ${python_command}
xhost -SI:localuser:root
xhost
else
pkexec ${python_command}
fi
# if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then
# # necessary for running on wayland
# xhost +SI:localuser:root
# pkexec ${python_command}
# xhost -SI:localuser:root
# xhost
# else
# pkexec ${python_command}
# fi

${python_command}

0 comments on commit 3a8eaaf

Please sign in to comment.