107 lines
2.7 KiB
Python
107 lines
2.7 KiB
Python
from PyQt6.QtGui import QIcon, QAction
|
|
from PyQt6.QtWidgets import QApplication, QMenu, QSystemTrayIcon
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
app = QApplication([])
|
|
app.setQuitOnLastWindowClosed(False)
|
|
|
|
|
|
# PID procesos
|
|
pidx = os.popen('pidof xscreensaver').read()
|
|
pid4 = os.popen('pidof xfce4-screensaver').read()
|
|
|
|
|
|
# Creating the options
|
|
menu = QMenu()
|
|
|
|
|
|
# Menú activar
|
|
optionActivo = QAction("Activar salvapantallas")
|
|
optionActivo.triggered.connect(lambda: activar())
|
|
|
|
# Menú desactivar
|
|
optionDesactivo = QAction("Desactivar salvapantallas")
|
|
optionDesactivo.triggered.connect(lambda: desactivar())
|
|
|
|
|
|
# Saber si está funcionando y cambiar el icono
|
|
if pidx == "" and pid4 == "":
|
|
icon = QIcon(path+"/cinema.png")
|
|
menu.addAction(optionActivo)
|
|
os.popen('xset -dpms').read()
|
|
result = subprocess.run(
|
|
["/usr/bin/bash", "-c", "xfce4-power-manager -q"])
|
|
print(result)
|
|
else:
|
|
icon = QIcon(path+"/work.png")
|
|
menu.addAction(optionDesactivo)
|
|
os.popen('xset +dpms').read()
|
|
result = subprocess.run(
|
|
["/usr/bin/bash", "-c", "nohup xfce4-power-manager & 2>&1 >/dev/null"])
|
|
print(result)
|
|
|
|
|
|
# opción desactivar
|
|
def desactivar():
|
|
global icon
|
|
os.popen('xset +dpms').read()
|
|
result = subprocess.run(
|
|
["/usr/bin/bash", "-c", "xfce4-power-manager -q"])
|
|
print(result)
|
|
icon = QIcon(path+"/cinema.png")
|
|
tray.setIcon(icon)
|
|
menu.removeAction(optionDesactivo)
|
|
menu.removeAction(quit)
|
|
menu.addAction(optionActivo)
|
|
menu.addAction(quit)
|
|
pidx = os.popen('pidof xscreensaver').read()
|
|
pid4 = os.popen('pidof xfce4-screensaver').read()
|
|
if pidx != "":
|
|
os.kill(int(pidx), signal.SIGKILL)
|
|
if pid4 != "":
|
|
os.kill(int(pid4), signal.SIGKILL)
|
|
|
|
|
|
# opción activar
|
|
def activar():
|
|
global icon
|
|
os.popen('xset -dpms').read()
|
|
result = subprocess.run(
|
|
["/usr/bin/bash", "-c", "nohup xfce4-power-manager & 2>&1 >/dev/null"])
|
|
print(result)
|
|
icon = QIcon(path+"/work.png")
|
|
tray.setIcon(icon)
|
|
menu.removeAction(optionActivo)
|
|
menu.removeAction(quit)
|
|
menu.addAction(optionDesactivo)
|
|
menu.addAction(quit)
|
|
if os.path.isfile('/usr/bin/xfce4-screensaver'):
|
|
result = subprocess.run(
|
|
["/usr/bin/bash", "-c", "xfce4-screensaver &"])
|
|
print(result)
|
|
elif os.path.isfile('/usr/bin/xscreensaver'):
|
|
result = subprocess.run(
|
|
["/usr/bin/bash", "-c", "xscreensaver --nosplash &"])
|
|
print(result)
|
|
|
|
|
|
# Adding item on the menu bar
|
|
tray = QSystemTrayIcon()
|
|
tray.setIcon(icon)
|
|
tray.setVisible(True)
|
|
|
|
|
|
# To quit the app
|
|
quit = QAction("Quit")
|
|
quit.triggered.connect(app.quit)
|
|
menu.addAction(quit)
|
|
|
|
# Adding options to the System Tray
|
|
tray.setContextMenu(menu)
|
|
|
|
app.exec()
|