101 lines
2.2 KiB
Python
101 lines
2.2 KiB
Python
from PyQt6.QtGui import QIcon, QAction
|
|
from PyQt6.QtWidgets import QApplication, QMenu, QSystemTrayIcon
|
|
import os
|
|
import subprocess
|
|
|
|
path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
app = QApplication([])
|
|
app.setQuitOnLastWindowClosed(False)
|
|
|
|
# TODO está instalado xdg-utils?
|
|
# TODO está instalado xwininfo?
|
|
|
|
# estado
|
|
global estado
|
|
if os.popen("xdg-screensaver status").read() == "enabled\n":
|
|
estado = True
|
|
else:
|
|
estado = False
|
|
|
|
# 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 estado:
|
|
icon = QIcon(path + "/assets/cold_cofee.png")
|
|
menu.addAction(optionDesactivo)
|
|
else:
|
|
icon = QIcon(path + "/assets/hot_cofee.png")
|
|
menu.addAction(optionActivo)
|
|
|
|
|
|
# window id de screensaver
|
|
global window_id
|
|
window_id = (
|
|
subprocess.Popen(
|
|
'xwininfo -root | grep xwininfo | cut -d" " -f4',
|
|
stdout=subprocess.PIPE,
|
|
shell=True,
|
|
)
|
|
.stdout.read()
|
|
.strip()
|
|
)
|
|
|
|
|
|
# opción desactivar
|
|
def desactivar():
|
|
icon = QIcon(path + "/assets/hot_cofee.png")
|
|
tray.setIcon(icon)
|
|
menu.removeAction(optionDesactivo)
|
|
menu.removeAction(quit)
|
|
menu.addAction(optionActivo)
|
|
menu.addAction(quit)
|
|
global window_id
|
|
suspend_screensaver()
|
|
|
|
|
|
# opción activar
|
|
def activar():
|
|
icon = QIcon(path + "/assets/cold_cofee.png")
|
|
tray.setIcon(icon)
|
|
menu.removeAction(optionActivo)
|
|
menu.removeAction(quit)
|
|
menu.addAction(optionDesactivo)
|
|
menu.addAction(quit)
|
|
resume_screensaver()
|
|
|
|
|
|
def suspend_screensaver():
|
|
# run xdg-screensaver on root window
|
|
subprocess.Popen("xdg-screensaver suspend " + window_id.decode("utf-8"), shell=True)
|
|
|
|
|
|
def resume_screensaver():
|
|
subprocess.Popen("xdg-screensaver resume " + window_id.decode("utf-8"), shell=True)
|
|
|
|
|
|
# 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()
|