Skip to content

Commit 3a8eaaf

Browse files
committed
app no longer needs root to start, only asks when needed
1 parent 3a4fd1d commit 3a8eaaf

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

auto_cpufreq/gui/app.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
HBOX_PADDING = 20
1717

18-
class MyWindow(Gtk.Window):
18+
class ToolWindow(Gtk.Window):
1919
def __init__(self):
2020
super().__init__(title="auto-cpufreq")
2121
self.set_default_size(600, 480)
@@ -80,8 +80,7 @@ def refresh(self):
8080
return True
8181

8282

83-
84-
win = MyWindow()
83+
win = ToolWindow()
8584
win.connect("destroy", Gtk.main_quit)
8685
win.show_all()
8786
GLib.set_application_name("auto-cpufreq")

auto_cpufreq/gui/objects.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import platform as pl
1010

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

1515
from io import StringIO
1616

17+
PKEXEC_ERROR = "Error executing command as another user: Not authorized\n\nThis incident has been reported.\n"
18+
1719
if os.getenv("PKG_MARKER") == "SNAP":
1820
auto_cpufreq_stats_path = "/var/snap/auto-cpufreq/current/auto-cpufreq.stats"
1921
else:
@@ -32,7 +34,7 @@ def get_version():
3234
return getoutput("echo \(Snap\) $SNAP_VERSION")
3335
# aur package
3436
elif dist_name in ["arch", "manjaro", "garuda"]:
35-
aur_pkg_check = call("pacman -Qs auto-cpufreq > /dev/null", shell=True)
37+
aur_pkg_check = run("pacman -Qs auto-cpufreq > /dev/null", shell=True)
3638
if aur_pkg_check == 1:
3739
return get_formatted_version()
3840
else:
@@ -77,7 +79,10 @@ def __init__(self):
7779

7880
def on_button_toggled(self, button, override):
7981
if button.get_active():
80-
set_override(override)
82+
result = run(f"pkexec auto-cpufreq --force={override}", shell=True, stdout=PIPE, stderr=PIPE)
83+
if result.stderr.decode() == PKEXEC_ERROR:
84+
self.set_selected()
85+
8186

8287
def set_selected(self):
8388
override = get_override()
@@ -170,7 +175,9 @@ def _remove_daemon(self, MenuItem, parent):
170175
confirm.destroy()
171176
if response == Gtk.ResponseType.YES:
172177
try:
173-
remove_daemon()
178+
result = run("pkexec auto-cpufreq --remove", shell=True, stdout=PIPE, stderr=PIPE)
179+
if result.stderr.decode() == PKEXEC_ERROR:
180+
raise Exception("Authorization was cancelled")
174181
dialog = Gtk.MessageDialog(
175182
transient_for=parent,
176183
message_type=Gtk.MessageType.INFO,
@@ -250,7 +257,9 @@ def __init__(self, parent):
250257

251258
def install_daemon(self, button, parent):
252259
try:
253-
deploy_daemon()
260+
result = run("pkexec auto-cpufreq --install", shell=True, stdout=PIPE, stderr=PIPE)
261+
if result.stderr.decode() == PKEXEC_ERROR:
262+
raise Exception("Authorization was cancelled")
254263
dialog = Gtk.MessageDialog(
255264
transient_for=parent,
256265
message_type=Gtk.MessageType.INFO,

scripts/org.auto-cpufreq.pkexec.policy

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<allow_inactive>auth_admin</allow_inactive>
1313
<allow_active>auth_admin</allow_active>
1414
</defaults>
15-
<annotate key="org.freedesktop.policykit.exec.path">/opt/auto-cpufreq/venv/bin/python</annotate>
16-
<annotate key="org.freedesktop.policykit.exec.argv1">/opt/auto-cpufreq/venv/bin/app.py</annotate>
17-
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
15+
<annotate key="org.freedesktop.policykit.exec.path">/opt/auto-cpufreq/venv/bin/auto-cpufreq</annotate>
16+
<!-- <annotate key="org.freedesktop.policykit.exec.argv1">/opt/auto-cpufreq/venv/bin/auto-cpufreq</annotate> -->
17+
<!-- <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate> -->
1818
</action>
1919
</policyconfig>

scripts/start_app

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ venv_dir=/opt/auto-cpufreq/venv
55
. "${venv_dir}/bin/activate"
66
python_command="${venv_dir}/bin/python ${venv_dir}/bin/app.py"
77

8-
if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then
9-
# necessary for running on wayland
10-
xhost +SI:localuser:root
11-
pkexec ${python_command}
12-
xhost -SI:localuser:root
13-
xhost
14-
else
15-
pkexec ${python_command}
16-
fi
8+
# if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then
9+
# # necessary for running on wayland
10+
# xhost +SI:localuser:root
11+
# pkexec ${python_command}
12+
# xhost -SI:localuser:root
13+
# xhost
14+
# else
15+
# pkexec ${python_command}
16+
# fi
17+
18+
${python_command}

0 commit comments

Comments
 (0)