-
Notifications
You must be signed in to change notification settings - Fork 0
/
appfap.py
49 lines (32 loc) · 1.19 KB
/
appfap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from gi.repository import GLib, Gdk, Wnck
import sys
from processes import ProcessTable
def throttle_process(pid):
if pid:
with open('/sys/fs/cgroup/cpu/fapped/tasks', 'w') as f:
f.write(str(pid))
def unthrottle_process(pid):
if pid:
with open('/sys/fs/cgroup/cpu/tasks', 'a') as f:
f.write(str(pid))
def on_active_window_changed(screen, previously_active_window):
process_table = ProcessTable()
windows_pids = [w.get_pid() for w in screen.get_windows()]
active_window = screen.get_active_window()
active_pid = active_window.get_pid() if active_window else 0
if active_pid in windows_pids:
windows_pids.remove(active_pid)
to_throttle = windows_pids
for pid in windows_pids:
to_throttle.extend(process_table.get_children_pids(int(pid)))
to_unthrottle = [active_pid] + \
process_table.get_children_pids(int(active_pid))
for pid in to_throttle:
throttle_process(pid)
for pid in to_unthrottle:
unthrottle_process(pid)
Gdk.init(sys.argv)
loop = GLib.MainLoop(None)
screen = Wnck.Screen.get_default()
screen.connect('active-window-changed', on_active_window_changed)
loop.run()