Disable splash screen with Python

What is the best way to cross DE disable screensaver in Linux? I found something here, but this is only for gnome-screensaver. I am wondering if there is a way to simulate a keystroke or some kind of X.Org API to disable splash screen activation.

+4
source share
3 answers

I studied this a while ago and finally ended up using xdg-screensaver , which I call through subprocess .

 import subprocess def suspend_screensaver(): window_id = subprocess.Popen('xwininfo -root | grep xwininfo | cut -d" " -f4', stdout=subprocess.PIPE, shell=True).stdout.read().strip() #run xdg-screensaver on root window subprocess.call(['xdg-screensaver', 'suspend', window_id]) def resume_screensaver(window_id): subprocess.Popen('xdg-screensaver resume ' + window_id, shell=True) 

This is not ideal, but apparently there is no other solution that would not include clutter with DE type data such as dbus or gnome-screensaver-command .

I don't really like the xwininfo call and wish there was a cleaner way, but so far I haven’t found anything better. Another problem with xwininfo 's approach is that it uses the root window identifier instead of the application window. Using the application window id instead of the root window will eliminate the need for the resume_screensaver method, as it will resume as soon as the window is destroyed.

And if you want to simulate keystrokes, then this is a naive bash script that I have been using for some time. This requires xdotool , which must be installed separately.

 #!/bin/bash while : do sleep 200 nice -n 1 xdotool key shift echo . done 

UPDATE

After using the python solution for over a year, it was discovered that sometimes zombie processes and / or too many xdg-screensaver instances are xdg-screensaver , so after digging I found a simpler alternative that is Gnome-specific , but works for me even in non-Gnome DE (XFCE), since the core Gnome libraries are required by many GTK-based applications, even if you don’t have a Gnome desktop.

 import subprocess def suspend_screensaver(): 'suspend linux screensaver' proc = subprocess.Popen('gsettings set org.gnome.desktop.screensaver idle-activation-enabled false', shell=True) proc.wait() def resume_screensaver(): 'resume linux screensaver' proc = subprocess.Popen('gsettings set org.gnome.desktop.screensaver idle-activation-enabled true', shell=True) proc.wait() 
+4
source

I know this question is old, but there is a more modern answer: now most DEs on linux use dbus for communication, and you can use this:

in a shell using kde tools:

 qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.Inhibit "myapps" "because I want it" 

this dbus call returns a cookie (number) that will need to be disabled by screesaver:

 qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.UnInhibit $COOKIE 

In python, it will be something like

 import dbus bus = dbus.SessionBus() saver = bus.get_object('org.freedesktop.ScreenSaver', '/ScreenSaver') saver_interface = dbus.Interface(saver, dbus_interface='org.freedesktop.ScreenSaver') # now we can inhibit the screensaver cookie=saver_interface.Inhibit("myapps", "because I want it") # we can also restore it saver_interface.UnInhibit(cookie) 
+1
source

Discussion

TL; DR; There is no way to cross-DE to block screensaver with pure Python. However, the question also asked about this with Linux (presumably from the shell). To be clear, on Linux there are no particularly good ways to cross-DE, but at least there is potential. You probably want to use the hacker shell xdg-screensaver script.

This is a topic that I spent some time on. I was not happy with any of the solutions there, so after playing with some homemade tools, I ended up changing the caffeine design to fit my needs and have been using it happily ever since. I will summarize some of what I learned in the process.

If you are not familiar with this, I would recommend checking out caffeine (Launchpad PPA @ https://launchpad.net/~caffeine-developers/+archive/ubuntu/ppa ).

The caffeine project mainly consists of the following scenarios: caffeine-screensaver caffeine caffeine-indicator caffeinate

caffeine and caffeine-indicator are written in Python 3, while caffeine-screensaver is a rebranding of xdg-screensaver written in a shell script.

The first half of ccpizza's answer is great because it is cross-DE and is very similar to caffeine sales. I had no problems with the zombie xdg-screensaver processes, but I am sure that this could be fixed if I knew better the use case or the conditions under which it occurred. Just the FYI that runs xdg-screensaver suspend ROOT_WINDOW_ID will close the tracking process and lower it indefinitely (until it resumes).

If you are interested in implementing a truly DE cross solution, I highly recommend exploring the above xdg-screensaver / caffeine-screensaver . Many lightsOn forks have also matured.

xdg-screensaver detects the desktop environment (KDE, Gnome, XFCE, LXDE, xscreensaver or gnome-screensaver) and acts accordingly. For the xdg-screensaver suspend xset -dpms , it runs xset -dpms to disable DPMS, DE-specific commands, and then looks at the provided window identifier for its duration. If the window disappears, the resume command is launched and the xdg-screensaver splash screen is issued. The xdg-screensaver resume command consists of xset +dpms (only if DPMS was originally enabled), then DE related commands.

Note that xdg-screensaver will only track the specified window id if xprop is available (it is part of x11-utils, so it should be). To track the window, it displays xprop -id WINDOW_ID -spy to control the change window. It stores the PID of the above process along with the window identifier in the lock file for reference to pause and resume. The initial state of the DPMS is referenced in a separate lock file. Blocking files must ensure that there are no duplicate processes.

The script is a complete mess, so I empathize with someone, unfortunately, to him, but I did not encounter any problems. You probably don't want to reinvent the wheel, so if you are creating something for public consumption, I would recommend that you simply co-opt it. If you are creating something for personal use, just go through it and use the commands specific to your DE.

ccpizza , suggesting to use subprocess.Popen('xwininfo -root | grep xwininfo | cut -d" " -f4', stdout=subprocess.PIPE, shell=True).stdout.read().strip() to get the identifier of the root window desktop. Using the desktop root window id with xdg-screensaver suspend will disable the desktop screen saver indefinitely. Using a specific window identifier will disable the desktop screen saver throughout the window.


Shell script

To determine the active window identifier , you can run the following:

 xprop -root _NET_ACTIVE_WINDOW | awk -F '[ ,]' '{print $5}' 

To determine the root window identifier, you can run the following:

 xwininfo -root | awk '/^xwininfo: Window id: / {print $4}' 

To determine the splash screen timeout , you can run the following:

 xset q | awk '/^ timeout: / {print $2}' 

NOTE: the result will be 0 if the screen saver is disabled.

To determine if DPMS is enabled or disabled , you can run the following:

 xset q | awk '/^ DPMS is / {print tolower($3)}' 

Python script

There are also clean Python options. The Xlib module is great for this, as is ewmh (which is a wrapper around Xlib).

To determine the active window identifier using ewmh :

 active_id = hex(ewmh.EWMH().getActiveWindow().id) 

To determine the root window id with ewmh :

 root_id = hex(ewmh.EWMH().root.id) 

To determine the active window identifier using Xlib :

 from Xlib.display import Display from Xlib.X import AnyPropertyType active_id = hex(Display().screen().root.get_full_property(Display().get_atom('_NET_ACTIVE_WINDOW'), AnyPropertyType).value[0]) 

To determine the root window identifier with Xlib :

 from Xlib.display import Display root_id = hex(Display().screen().root.id) 

To determine the screen saver timeout using Xlib :

 from Xlib.display import Display timeout = Display().get_screen_saver().timeout 
0
source

Source: https://habr.com/ru/post/1416006/


All Articles