Not literally what you asked for, but at least an (effectively) comparable solution would be to put the script below under a shortcut key.
What it does
When the shortcut key is used:
Then:
- If the user presses Enter, the system shuts down
- If the user does nothing, the system waits for 30 seconds (or any other period of time you'd like to set) and shuts down.
- If the user moves the mouse during the 30 seconds, the procedure is stopped
The script
#!/usr/bin/env python3
import subprocess
import time
#--- set the location of the close button x, y
q_loc = [1050, 525]
#--- set the time to wait before shutdown
countdown = 30
subprocess.Popen(["gnome-session-quit", "--power-off"])
# for slower systems, set a longer break, on faster systems, can be shorter:
time.sleep(0.4)
subprocess.Popen(["xdotool", "mousemove", str(q_loc[0]), str(q_loc[1])])
coords1 = q_loc
t = 0
while True:
time.sleep(1)
cmd = "xdotool", "getmouselocation"
currloc = subprocess.check_output(cmd).decode("utf-8").split()[:2]
coords2 = [int(n.split(":")[1]) for n in currloc]
if coords2 != coords1:
break
else:
if t >= countdown:
subprocess.Popen(["xdotool", "key", "KP_Enter"])
break
t += 1
How to use
I am pretty sure you know how to use it, but here we go for habbit reasons:
The script uses xdotool
sudo apt-get install xdotool
Copy the script into an empty file, save it as run_close.py
In the head section, set the screen's location of the shut-down button in the close- window (my first guess was right):
#--- set the location of the close button x, y
q_loc = [1050, 525]
and the time to wait before unattended shut down:
#--- set the time to wait before shutdown
countdown = 30
Test-run it by the command:
python3 /path/to/run_close.py
Test it with all of the option: pressing Enter for immediate shutdown, unattended shutdown and break the procedure by mouse- move
If all works fine, add it to a shortcut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
python3 /path/to/run_close.py
EDIT
Below a version of the script that does not need any additional setting. It calculates the coordinates of the quit button, no matter what is the screen's resolution.
The setup is pretty much the same, but [3.]
can be skipped.
#!/usr/bin/env python3
import subprocess
import time
#--- set the time to wait before shutdown
countdown = 30
def get_qloc():
xr = subprocess.check_output(["xrandr"]).decode("utf-8").split()
scrs = [s.split("+") for s in xr if all([s.count("x") == 1, s.count("+") == 2])]
center = [int(int(s)/2) for s in [scr[0] for scr in scrs if scr[1] == "0"][0].split("x")]
return [center[0] + 250, center[1]]
q_loc = get_qloc()
subprocess.Popen(["gnome-session-quit", "--power-off"])
# for slower systems, set a longer break, on faster systems, can be shorter:
time.sleep(0.4)
subprocess.Popen(["xdotool", "mousemove", str(q_loc[0]), str(q_loc[1])])
coords1 = q_loc
t = 0
while True:
time.sleep(1)
cmd = "xdotool", "getmouselocation"
currloc = subprocess.check_output(cmd).decode("utf-8").split()[:2]
coords2 = [int(n.split(":")[1]) for n in currloc]
if coords2 != coords1:
break
else:
if t >= countdown:
subprocess.Popen(["xdotool", "key", "KP_Enter"])
break
t += 1
Explanation
The size of the Session Manager window to close the system is always centred and of a fixed (absolute) size, independent to the screen's resolution. Therefore the position relative to the centre of the screen is a constant factor.
All we need to do then is to read the screen's resolution and calculate the button's position from there.
The applied function ( get_qloc()
) calculates the resolution of the left screen, since that is the one where the dialogue will appear.
Note
The time, set in the line time.sleep(0.4)
is set for relatively slow systems, to make sure the mouse is moved after the shut down window appears. On faster systems, it can be shorter, on slower systems (like possibly a VM) it might be needed to set to longer.