46 lines
1.6 KiB
Python
Executable File
46 lines
1.6 KiB
Python
Executable File
import dbus
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
from gi.repository import GLib
|
|
import sys
|
|
|
|
class TrayMonitor:
|
|
def __init__(self):
|
|
# настраиваем dbus и вешаем слушателей
|
|
DBusGMainLoop(set_as_default=True)
|
|
self.bus = dbus.SessionBus()
|
|
self.interface = "org.kde.StatusNotifierWatcher"
|
|
self.path = "/StatusNotifierWatcher"
|
|
|
|
# подписываемся на регистрацию и удаление иконок
|
|
self.bus.add_signal_receiver(self.push_update, "StatusNotifierItemRegistered", self.interface)
|
|
self.bus.add_signal_receiver(self.push_update, "StatusNotifierItemUnregistered", self.interface)
|
|
|
|
# выводим начальное состояние
|
|
self.push_update()
|
|
|
|
def get_count(self):
|
|
# опрашиваем шину на количество активных элементов
|
|
try:
|
|
obj = self.bus.get_object(self.interface, self.path)
|
|
props = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
|
|
items = props.Get(self.interface, "RegisteredStatusNotifierItems")
|
|
return len(items)
|
|
except:
|
|
return 0
|
|
|
|
def push_update(self, *args):
|
|
# шлем цифру в stdout
|
|
count = self.get_count()
|
|
sys.stdout.write(f"{count}\n")
|
|
sys.stdout.flush()
|
|
|
|
def run(self):
|
|
# цикл glib, чтобы слушал сигналы
|
|
self.loop = GLib.MainLoop()
|
|
try:
|
|
self.loop.run()
|
|
except KeyboardInterrupt:
|
|
sys.exit(0)
|
|
|
|
if __name__ == "__main__":
|
|
TrayMonitor().run() |