Running screensaver and song at once

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Ertain
Level 1
Level 1
Posts: 15
Joined: Tue Jan 17, 2012 4:41 pm
Contact:

Running screensaver and song at once

Post by Ertain »

Hello everyone. This is my first time posting here, so I'm making it a good one.

I've been trying to writie a script (or at one time even reprogramming the application) which plays a song while a screensaver is running. I understand that GNOME 3 doesn't really have that much support for screensavers, but I like them so much as to not let them go.

At first I tried changing the command in the desktop file for the screensaver. I made it execute a script file with these lines:

Code: Select all

/usr/lib/xscreensaver/le_screensaver -root &
play /usr/share/sounds/le_music.ogg &
Sadly, that doesn't work. The song doesn't play at all. I'm sure it's because le_screensaver starts running, but it doesn't get around to playing le_music. I can't exactly figure out how the screensaver could start running and possibly wait a little for the music to start playing.

I have also tried using a script that's similar to the one I found in the FAQs section of the GNOME website.

Code: Select all

#!/usr/bin/perl -w

#gnome
my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged\"";
open (IN, "$cmd |") or die "Couldn't run dbus-monitor.\n";
print("dbus-monitor started.\n");

#kde
# my $cmd = "dbus-monitor --session \"type='signal',interface='org.freedesktop.ScreenSaver',member='ActiveChanged'\"";

while (<IN>) {
if (m/^\s+boolean true/) {
  #when screensaver activates, run the following commands
  print("Playing song. \n");
  system("echo \"--------------------------------------------------------------------\" >> /tmp/play_sound.txt");
  system("echo \"started:\" >> /tmp/play_sound.txt");
  system("date >> /tmp/play_sound.txt");
  system("play \"/home/user/le_music.mp3\"");
	}
elsif (m/^\s+boolean false/) {
  system("echo \"--------------------------------------------------------------------\" >> /tmp/play_sound.txt");
  system("echo \"stopped:\" >> /tmp/play_sound.txt");
  system("date >> /tmp/play_sound.txt");
  system("echo \"--------------------------------------------------------------------\" >> /tmp/play_sound.txt");
  print("Stopping song. \n");
  system("kill `pgrep play`");
	}
}
What I do is run this script and, supposedly, it would start the music when the screensaver starts. This doesn't work, either, and I am not entirely certain why.

Might anyone else have an idea on this? If anyone's wondering about this I have the packages for xscreensaver installed (xscreensaver, xscreensaver-data, xscreensaver-data-extra, xscreensaver-gl, and xscreensaver-gl-extra).
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29612
Joined: Wed Jul 06, 2011 3:58 am

Re: Running screensaver and song at once

Post by xenopeek »

What happens if you reverse the steps? Start playing the music first, and then pull up the screensaver?
Image
User avatar
Ertain
Level 1
Level 1
Posts: 15
Joined: Tue Jan 17, 2012 4:41 pm
Contact:

Re: Running screensaver and song at once

Post by Ertain »

I have tried that, and that doesn't work, either.
User avatar
Ertain
Level 1
Level 1
Posts: 15
Joined: Tue Jan 17, 2012 4:41 pm
Contact:

Re: Running screensaver and song at once

Post by Ertain »

I think I have finally figured out how to run the song. Here's the Perl code I used.

Code: Select all

my $cmd = "xscreensaver-command -watch";

open (IN, "$cmd |") or die "Couldn't run xscreensaver-command.\n";

# Log it
open (LOG, "/tmp/play_sound.txt");

print("xscreensaver-command for watch started.\n");

while (<IN>) {
if (m/^(BLANK|LOCK)/) {
    #when screensaver activates, run the following commands
    print("Playing song. \n");
    print LOG "--------------------------------------------------------------------\n";
    print LOG "started:\n";
    system("date >> /tmp/play_sound.txt");
    system("play \"/home/user/Music/le_music.mp3\" &");
    }
elsif (m/^UNBLANK/) {
  system("echo \"--------------------------------------------------------------------\" >> /tmp/play_sound.txt");
  print LOG "--------------------------------------------------------------------\n";
  system("echo \"stopped:\" >> /tmp/play_sound.txt");
  print LOG "stopped:\n";
  system("date >> /tmp/play_sound.txt");
  system("echo \"--------------------------------------------------------------------\" >> /tmp/play_sound.txt");
  print LOG "--------------------------------------------------------------------\n";
  print("Stopping song. \n");
  system("killall play");
	}
}
If I run this in some terminal or as some daemon, it'll work. More testing, however, is needed to see if this'll work as I want it.

Edit: this script mostly works. Whenever I suspend my machine and come back the screensaver comes on and, subsquently, the script activitated. Would be nice to check for things like that.
äxl

Re: [SOLVED... Mostly] Running screensaver and song at once

Post by äxl »

I found this script on Gnome.org and Ubuntuforums. Gnome.org says idleness is now controlled by GnomeSession. So after finding Olivier Mehani's page and looking at the output of dbus-monitor I've reached this:

Code: Select all

#!/usr/bin/perl
#Gnome#
my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.SessionManager.Presence',member='StatusChanged'\"";
#KDE#my $cmd = "dbus-monitor --session \"type='signal',interface='org.freedesktop.ScreenSaver',member='ActiveChanged'\"";
#MATE#my $cmd = "dbus-monitor --session \"type='signal',interface='org.mate.SessionManager.Presence',member='StatusChanged'\"";

open (IN, "$cmd |");

while (<IN>)
{
#KDE#if (m/^\s+boolean true/)
#Gnome/MATE#
	if (m/^\s+uint32 3/)
	{
#when session idles these commands will run
		system("COMMAND");
		system("COMMAND"); #you can add system("COMMAND"); lines at will
	}
#KDE#elsif (m/^\s+boolean false/)
#Gnome/MATE#
	elsif (m/^\s+uint32 0/)
	{
#when session is active again these commands will run
		system("COMMAND");
		system("COMMAND"); #delete these two system lines if not wanted/needed
	}
}
You have to replace COMMAND with what you want to achieve of course. You can add an echo before the command to try the script.

Ertain, I don't know if it comes on after suspend. Thanks for the KDE call though provided it still works. ;) Maybe someone knows bus commands from other environments?

I've added a launcher to autostart with "perl script.pl" and it's works brilliantly. I'm running xrandr to turn my CRT's brightness to 0.3.
But I guess since nobody uses CRTs anymore xbacklight would be the fitting tool, right?
I removed the screensaver and power manager. I set up my idle time with my configuration editor and use DPMS to turn the screen off.
Have not found out so far how to disable Gnome screen locking. GnomeSession also goes 'inhibited' (uint32 8/) after some seconds if someone wants to call for that.

Also I don't know how to let the script sleep/wait before another command ... I've tried sleep() everywhere ... Maybe another while loop or just adding something like +60 ...?
User avatar
Ertain
Level 1
Level 1
Posts: 15
Joined: Tue Jan 17, 2012 4:41 pm
Contact:

Re: [SOLVED... Mostly] Running screensaver and song at once

Post by Ertain »

Sorry, äxl, I've been away for a while. I'll take a look into this newer script. Thank you for the reply, btw. :wink:
Locked

Return to “Scripts & Bash”